Add keystore and account listing/clear functionalities

pull/61/head
Rongjian Lan 6 years ago
parent 4dff24ff05
commit b741fb5d92
  1. 3
      .gitignore
  2. 49
      client/wallet/main.go
  3. 4
      crypto/pki/utils.go

3
.gitignore vendored

@ -21,3 +21,6 @@ bin/
# Log
*.log
log-*
# Wallet
keystore

@ -4,9 +4,11 @@ import (
"crypto/rand"
"flag"
"fmt"
"github.com/dedis/kyber"
"github.com/simple-rules/harmony-benchmark/crypto"
"github.com/simple-rules/harmony-benchmark/crypto/pki"
"io"
"io/ioutil"
"os"
)
@ -52,9 +54,24 @@ func main() {
return
}
priKey := crypto.Ed25519Curve.Scalar().SetBytes(randomBytes[:])
priKeyBytes, err := priKey.MarshalBinary()
if err != nil {
panic("Failed to generate private key")
}
pubKey := pki.GetPublicKeyFromScalar(priKey)
address := pki.GetAddressFromPublicKey(pubKey)
StorePrivateKey(priKeyBytes)
fmt.Printf("New account created:\nAddress: {%x}\n", address)
case "list":
fmt.Println("Listing existing accounts...")
keys := ReadPrivateKeys()
for i, key := range keys {
address := pki.GetAddressFromPrivateKey(key)
fmt.Printf("Account %d:\n {%x}\n", i+1, address)
}
case "clearAll":
fmt.Println("Deleting existing accounts...")
DeletePrivateKey()
}
case "transaction":
switch os.Args[2] {
@ -66,3 +83,35 @@ func main() {
os.Exit(1)
}
}
func StorePrivateKey(priKey []byte) {
f, err := os.OpenFile("keystore", os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
panic("Failed to open keystore")
}
_, err = f.Write(priKey)
if err != nil {
panic("Failed to write to keystore")
}
f.Close()
}
func DeletePrivateKey() {
ioutil.WriteFile("keystore", []byte{}, 0644)
}
func ReadPrivateKeys() []kyber.Scalar {
keys, err := ioutil.ReadFile("keystore")
if err != nil {
return []kyber.Scalar{}
}
keyScalars := []kyber.Scalar{}
for i := 0; i < len(keys); i += 32 {
priKey := crypto.Ed25519Curve.Scalar()
priKey.UnmarshalBinary(keys[i : i+32])
keyScalars = append(keyScalars, priKey)
}
return keyScalars
}

@ -18,6 +18,10 @@ func GetAddressFromPublicKey(pubKey kyber.Point) [20]byte {
return address
}
func GetAddressFromPrivateKey(priKey kyber.Scalar) [20]byte {
return GetAddressFromPublicKey(GetPublicKeyFromScalar(priKey))
}
// Temporary helper function for benchmark use
func GetAddressFromInt(value int) [20]byte {
return GetAddressFromPublicKey(GetPublicKeyFromScalar(GetPrivateKeyFromInt(value)))

Loading…
Cancel
Save