Add flag parser for wallet transfer functionality

pull/69/head
Rongjian Lan 6 years ago
parent 32d7047512
commit 9a1877d41a
  1. 64
      client/wallet/main.go
  2. 4
      deploy.sh

@ -17,12 +17,14 @@ import (
"io" "io"
"io/ioutil" "io/ioutil"
"os" "os"
"strconv"
"time" "time"
) )
func main() { func main() {
// Account subcommands // Account subcommands
accountImportCommand := flag.NewFlagSet("import", flag.ExitOnError) accountImportCommand := flag.NewFlagSet("import", flag.ExitOnError)
transferCommand := flag.NewFlagSet("transfer", flag.ExitOnError)
//accountListCommand := flag.NewFlagSet("list", flag.ExitOnError) //accountListCommand := flag.NewFlagSet("list", flag.ExitOnError)
// //
//// Transaction subcommands //// Transaction subcommands
@ -31,6 +33,10 @@ func main() {
//// Account subcommand flag pointers //// Account subcommand flag pointers
//// Adding a new choice for --metric of 'substring' and a new --substring flag //// Adding a new choice for --metric of 'substring' and a new --substring flag
accountImportPtr := accountImportCommand.String("privateKey", "", "Specify the private key to import") accountImportPtr := accountImportCommand.String("privateKey", "", "Specify the private key to import")
transferSenderPtr := transferCommand.String("sender", "0", "Specify the sender account address or index")
transferReceiverPtr := transferCommand.String("receiver", "", "Specify the receiver account")
transferAmountPtr := transferCommand.Int("amount", 0, "Specify the amount to transfer")
//accountListPtr := accountNewCommand.Bool("new", false, "N/A") //accountListPtr := accountNewCommand.Bool("new", false, "N/A")
// //
//// Transaction subcommand flag pointers //// Transaction subcommand flag pointers
@ -115,7 +121,7 @@ func main() {
}() }()
time.Sleep(3 * time.Second) // Wait 3 seconds for the response. Exit afterward. time.Sleep(3 * time.Second) // Wait 3 seconds for the response. Exit afterward.
case "test": case "test":
priKey := pki.GetPrivateKeyScalarFromInt(33) priKey := pki.GetPrivateKeyScalarFromInt(444)
address := pki.GetAddressFromPrivateKey(priKey) address := pki.GetAddressFromPrivateKey(priKey)
priKeyBytes, err := priKey.MarshalBinary() priKeyBytes, err := priKey.MarshalBinary()
if err != nil { if err != nil {
@ -124,11 +130,59 @@ func main() {
fmt.Printf("Private Key :\n {%x}\n", priKeyBytes) fmt.Printf("Private Key :\n {%x}\n", priKeyBytes)
fmt.Printf("Address :\n {%x}\n", address) fmt.Printf("Address :\n {%x}\n", address)
} }
case "transaction": case "transfer":
switch os.Args[2] { fmt.Println("Transfer...")
case "new": transferCommand.Parse(os.Args[2:])
fmt.Println("Creating new transaction...") priKey := *accountImportPtr
if transferCommand.Parsed() {
fmt.Println(priKey)
} else {
fmt.Println("Failed to parse flags")
} }
sender := *transferSenderPtr
receiver := *transferReceiverPtr
amount := *transferAmountPtr
if amount <= 0 {
fmt.Println("Please specify positive amount to transfer")
}
priKeys := ReadPrivateKeys()
if len(priKeys) == 0 {
fmt.Println("No existing account to send money from.")
return
}
senderIndex, err := strconv.Atoi(sender)
senderAddress := ""
addresses := ReadAddresses()
if err != nil {
senderIndex = -1
for i, address := range addresses {
if fmt.Sprintf("%x", address) == senderAddress {
senderIndex = i
break
}
}
if senderIndex == -1 {
fmt.Println("Specified sender account is not imported yet.")
break
}
}
if senderIndex >= len(priKeys) {
fmt.Println("Sender account index out of bounds.")
return
}
senderPriKey := priKeys[senderIndex]
receiverAddress, err := hex.DecodeString(receiver)
if err != nil || len(receiverAddress) != 20 {
fmt.Println("The receiver address is not a valid address.")
return
}
fmt.Println(senderPriKey)
fmt.Println(amount)
fmt.Println(receiverAddress)
// Generate transaction
default: default:
flag.PrintDefaults() flag.PrintDefaults()
os.Exit(1) os.Exit(1)

@ -58,8 +58,8 @@ while IFS='' read -r line || [[ -n "$line" ]]; do
fi fi
done < $config done < $config
txgen_enabled=$3 txgen_disabled=$3
# Generate transactions # Generate transactions
if [ -z "$txgen_enabled" ]; then if [ -z "$txgen_disabled" ]; then
./bin/txgen -config_file $config -log_folder $log_folder ./bin/txgen -config_file $config -log_folder $log_folder
fi fi

Loading…
Cancel
Save