change func naming

pull/401/head
Minh Doan 6 years ago committed by Minh Doan
parent 3c2ca578b7
commit 22b47dafc0
  1. 29
      cmd/client/wallet/main.go

@ -99,7 +99,7 @@ func main() {
case "list": case "list":
processListCommand() processListCommand()
case "removeAll": case "removeAll":
ClearKeystore() clearKeystore()
fmt.Println("All existing accounts deleted...") fmt.Println("All existing accounts deleted...")
case "import": case "import":
processImportCommnad() processImportCommnad()
@ -128,13 +128,13 @@ func processNewCommnad() {
if err != nil { if err != nil {
panic("Failed to generate the private key") panic("Failed to generate the private key")
} }
StorePrivateKey(crypto2.FromECDSA(priKey)) storePrivateKey(crypto2.FromECDSA(priKey))
fmt.Printf("New account created with address:\n {%s}\n", crypto2.PubkeyToAddress(priKey.PublicKey).Hex()) fmt.Printf("New account created with address:\n {%s}\n", crypto2.PubkeyToAddress(priKey.PublicKey).Hex())
fmt.Printf("Please keep a copy of the private key:\n {%s}\n", hex.EncodeToString(crypto2.FromECDSA(priKey))) fmt.Printf("Please keep a copy of the private key:\n {%s}\n", hex.EncodeToString(crypto2.FromECDSA(priKey)))
} }
func processListCommand() { func processListCommand() {
for i, key := range ReadPrivateKeys() { for i, key := range readPrivateKeys() {
fmt.Printf("Account %d:\n {%s}\n", i, crypto2.PubkeyToAddress(key.PublicKey).Hex()) fmt.Printf("Account %d:\n {%s}\n", i, crypto2.PubkeyToAddress(key.PublicKey).Hex())
fmt.Printf(" PrivateKey: {%s}\n", hex.EncodeToString(key.D.Bytes())) fmt.Printf(" PrivateKey: {%s}\n", hex.EncodeToString(key.D.Bytes()))
} }
@ -154,7 +154,7 @@ func processImportCommnad() {
if err != nil { if err != nil {
panic("Failed to parse the private key into bytes") panic("Failed to parse the private key into bytes")
} }
StorePrivateKey(priKeyBytes) storePrivateKey(priKeyBytes)
fmt.Println("Private key imported...") fmt.Println("Private key imported...")
} }
@ -166,14 +166,14 @@ func processBalancesCommand() {
for i, address := range ReadAddresses() { for i, address := range ReadAddresses() {
fmt.Printf("Account %d: %s:\n", i, address.Hex()) fmt.Printf("Account %d: %s:\n", i, address.Hex())
for shardID, balanceNonce := range FetchBalance(address, walletNode) { for shardID, balanceNonce := range FetchBalance(address, walletNode) {
fmt.Printf(" Balance in Shard %d: %s \n", shardID, ConvertBalanceIntoReadableFormat(balanceNonce.balance)) fmt.Printf(" Balance in Shard %d: %s \n", shardID, convertBalanceIntoReadableFormat(balanceNonce.balance))
} }
} }
} else { } else {
address := common.HexToAddress(*balanceAddressPtr) address := common.HexToAddress(*balanceAddressPtr)
fmt.Printf("Account: %s:\n", address.Hex()) fmt.Printf("Account: %s:\n", address.Hex())
for shardID, balanceNonce := range FetchBalance(address, walletNode) { for shardID, balanceNonce := range FetchBalance(address, walletNode) {
fmt.Printf(" Balance in Shard %d: %s \n", shardID, ConvertBalanceIntoReadableFormat(balanceNonce.balance)) fmt.Printf(" Balance in Shard %d: %s \n", shardID, convertBalanceIntoReadableFormat(balanceNonce.balance))
} }
} }
} }
@ -209,7 +209,7 @@ func processTransferCommand() {
fmt.Println("Please specify positive amount to transfer") fmt.Println("Please specify positive amount to transfer")
return return
} }
priKeys := ReadPrivateKeys() priKeys := readPrivateKeys()
if len(priKeys) == 0 { if len(priKeys) == 0 {
fmt.Println("No imported account to use.") fmt.Println("No imported account to use.")
return return
@ -265,7 +265,7 @@ func processTransferCommand() {
lib.SubmitTransaction(tx, walletNode, uint32(shardID)) lib.SubmitTransaction(tx, walletNode, uint32(shardID))
} }
func ConvertBalanceIntoReadableFormat(balance *big.Int) string { func convertBalanceIntoReadableFormat(balance *big.Int) string {
balance = balance.Div(balance, big.NewInt(params.GWei)) balance = balance.Div(balance, big.NewInt(params.GWei))
strBalance := fmt.Sprintf("%d", balance.Uint64()) strBalance := fmt.Sprintf("%d", balance.Uint64())
@ -334,7 +334,7 @@ func GetFreeToken(address common.Address, walletNode *node.Node) {
// ReadAddresses reads the addresses stored in local keystore // ReadAddresses reads the addresses stored in local keystore
func ReadAddresses() []common.Address { func ReadAddresses() []common.Address {
priKeys := ReadPrivateKeys() priKeys := readPrivateKeys()
addresses := []common.Address{} addresses := []common.Address{}
for _, key := range priKeys { for _, key := range priKeys {
addresses = append(addresses, crypto2.PubkeyToAddress(key.PublicKey)) addresses = append(addresses, crypto2.PubkeyToAddress(key.PublicKey))
@ -342,8 +342,8 @@ func ReadAddresses() []common.Address {
return addresses return addresses
} }
// StorePrivateKey stores the specified private key in local keystore // storePrivateKey stores the specified private key in local keystore
func StorePrivateKey(priKey []byte) { func storePrivateKey(priKey []byte) {
privateKey, err := crypto2.ToECDSA(priKey) privateKey, err := crypto2.ToECDSA(priKey)
if err != nil { if err != nil {
panic("Failed to deserialize private key") panic("Failed to deserialize private key")
@ -367,12 +367,13 @@ func StorePrivateKey(priKey []byte) {
f.Close() f.Close()
} }
func ClearKeystore() { // clearKeystore deletes all data in the local keystore
func clearKeystore() {
ioutil.WriteFile("keystore", []byte{}, 0644) ioutil.WriteFile("keystore", []byte{}, 0644)
} }
// ReadPrivateKeys reads all the private key stored in local keystore // readPrivateKeys reads all the private key stored in local keystore
func ReadPrivateKeys() []*ecdsa.PrivateKey { func readPrivateKeys() []*ecdsa.PrivateKey {
keys, err := ioutil.ReadFile("keystore") keys, err := ioutil.ReadFile("keystore")
if err != nil { if err != nil {
return []*ecdsa.PrivateKey{} return []*ecdsa.PrivateKey{}

Loading…
Cancel
Save