From fdb06dfebd467a57af7f3cddd9cc65b1488cd5a8 Mon Sep 17 00:00:00 2001 From: stdevAdrianPaez Date: Sat, 27 Jul 2019 07:43:38 -0400 Subject: [PATCH] [cli] refs #146 Cleaning transactionSign command --- src/cli/sign_tx.go | 97 ------------------ src/cli/transaction_sign.go | 41 ++++---- src/cli/tx_ack.go | 194 ------------------------------------ 3 files changed, 20 insertions(+), 312 deletions(-) delete mode 100644 src/cli/sign_tx.go delete mode 100644 src/cli/tx_ack.go diff --git a/src/cli/sign_tx.go b/src/cli/sign_tx.go deleted file mode 100644 index fd0ddb4..0000000 --- a/src/cli/sign_tx.go +++ /dev/null @@ -1,97 +0,0 @@ -package cli - -import ( - // "fmt" - "os" - "runtime" - - "github.com/gogo/protobuf/proto" - - gcli "github.com/urfave/cli" - - messages "github.com/skycoin/hardware-wallet-protob/go" - - skyWallet "github.com/skycoin/hardware-wallet-go/src/skywallet" -) - -func signTxCmd() gcli.Command { - name := "signTx" - return gcli.Command{ - Name: name, - Usage: "Start a transaction with more than 8 inputs and 8 outputs.", - Description: "", - Flags: []gcli.Flag{ - gcli.IntFlag{ - Name: "outputs", - Usage: "Number of outputs in transaction.", - }, - gcli.IntFlag{ - Name: "inputs", - Usage: "Number of outputs in transaction.", - }, - gcli.StringFlag{ - Name: "coin", - Usage: "The name of coin to use.", - }, - gcli.IntFlag{ - Name: "version", - Usage: "Transaction version.", - }, - gcli.IntFlag{ - Name: "lockTime", - Usage: "Transaction lock time.", - }, - gcli.StringFlag{ - Name: "txHash", - Usage: "Transaction Hash", - }, - gcli.StringFlag{ - Name: "deviceType", - Usage: "Device type to send instructions to, hardware wallet (USB) or emulator.", - EnvVar: "DEVICE_TYPE", - }, - }, - OnUsageError: onCommandUsageError(name), - Action: func(c *gcli.Context) { - outputsCount := c.Int("outputs") - inputsCount := c.Int("inputs") - coinName := c.String("coin") - version := c.Int("version") - lockTime := c.Int("lockTime") - txHash := []byte(c.String("txHash")) - - device := skyWallet.NewDevice(skyWallet.DeviceTypeFromString(c.String("deviceType"))) - log.Info("Start Action on signTx function") - if device == nil { - return - } - defer device.Close() - - if os.Getenv("AUTO_PRESS_BUTTONS") == "1" && device.Driver.DeviceType() == skyWallet.DeviceTypeEmulator && runtime.GOOS == "linux" { - err := device.SetAutoPressButton(true, skyWallet.ButtonRight) - if err != nil { - log.Error(err) - return - } - } - - msg, err := device.SignTx(outputsCount, inputsCount, coinName, version, lockTime, txHash) - if err != nil { - log.Error(err) - return - } - switch msg.Kind { - case uint16(messages.MessageType_MessageType_TxRequest): - txRequest := &messages.TxRequest{} - err = proto.Unmarshal(msg.Data, txRequest) - if err != nil { - log.Error(err) - return - } - log.Info(txRequest) - default: - log.Info("Unexpected response message") - } - }, - } -} diff --git a/src/cli/transaction_sign.go b/src/cli/transaction_sign.go index d3b7e81..141bb18 100644 --- a/src/cli/transaction_sign.go +++ b/src/cli/transaction_sign.go @@ -93,7 +93,6 @@ func transactionSignCmd() gcli.Command { state := 0 index := 0 - // Building an send first SignTx message msg, err := device.SignTx(len(outputs), len(inputs), coinName, version, lockTime, txHash) if err != nil { log.Error(err) @@ -114,45 +113,33 @@ func transactionSignCmd() gcli.Command { } switch *txRequest.RequestType { case messages.TxRequest_TXINPUT: - if state == 0 { - // Sending Inputs + if state == 0 { // Sending Inputs for InnerHash msg, err = sendInputs(device,&inputs,&inputIndex,version,lockTime,&index,&state) - } else if state == 2 { - // Printing Signatures - txRequest := &messages.TxRequest{} - err = proto.Unmarshal(msg.Data, txRequest) + } else if state == 2 { // Sending Inputs for Signatures + err = printSignatures(&msg) if err != nil { log.Error(err) return } - for _, sign := range txRequest.SignResult { - println(*sign.Signature) - } - // Sending Inputs for signatures msg, err = sendInputs(device,&inputs,&inputIndex,version,lockTime,&index,&state) } else { - log.Error("Protocol error: unexpected TxRequest type") + log.Error("protocol error: unexpected TxRequest type") return } case messages.TxRequest_TXOUTPUT: - if state == 1 { - // Sending Outputs + if state == 1 { // Sending Outputs for InnerHash msg, err = sendOutputs(device,&outputs,&addressIndex,&coins,&hours,version,lockTime,&index,&state) } else { - log.Error("Protocol error: unexpected TxRequest type") + log.Error("protocol error: unexpected TxRequest type") return } case messages.TxRequest_TXFINISHED: if state == 3 { - txRequest := &messages.TxRequest{} - err = proto.Unmarshal(msg.Data, txRequest) + err = printSignatures(&msg) if err != nil { log.Error(err) return } - for _, sign := range txRequest.SignResult { - println(*sign.Signature) - } return } else { log.Error("protocol error: unexpected TXFINISHED message") @@ -170,7 +157,7 @@ func transactionSignCmd() gcli.Command { case uint16(messages.MessageType_MessageType_ButtonRequest): msg, err = device.ButtonAck() default: - log.Error("Unexpected response message type from hardware wallet.") + log.Error("unexpected response message type from hardware wallet.") return } } @@ -301,3 +288,15 @@ func sendOutputs(device *skyWallet.Device, outputs *[]string, addressIndex *[]in } return wire.Message{}, errors.New("empty outputs") } + +func printSignatures(msg *wire.Message) error { + txRequest := &messages.TxRequest{} + err := proto.Unmarshal(msg.Data, txRequest) + if err != nil { + return err + } + for _, sign := range txRequest.SignResult { + println(*sign.Signature) + } + return nil +} diff --git a/src/cli/tx_ack.go b/src/cli/tx_ack.go deleted file mode 100644 index 6c84f57..0000000 --- a/src/cli/tx_ack.go +++ /dev/null @@ -1,194 +0,0 @@ -package cli - -import ( - // "fmt" - "os" - "runtime" - "strconv" - "strings" - - "github.com/gogo/protobuf/proto" - - gcli "github.com/urfave/cli" - - messages "github.com/skycoin/hardware-wallet-protob/go" - - skyWallet "github.com/skycoin/hardware-wallet-go/src/skywallet" -) - -func toUnit32Array(array *[]string) ([]uint32, error) { - addressN := make([]uint32, len(*array)) - for index2, number := range *array { - parseResult, err := strconv.ParseUint(number, 10, 32) - if err != nil { - return nil, err - } - addressN[index2] = uint32(parseResult) - } - return addressN, nil -} - -func txAckCmd() gcli.Command { - name := "txAck" - return gcli.Command{ - Name: name, - Usage: "Send a set of transactions or inputs requested by hardware wallet.", - Description: "", - Flags: []gcli.Flag{ - gcli.IntFlag{ - Name: "version", - Usage: "Transaction version.", - }, - gcli.IntFlag{ - Name: "lockTime", - Usage: "Lock time of transaction", - }, - gcli.StringSliceFlag{ - Name: "input", - Usage: "Item with next structure: \"[address_n] [hashIn]\" .\n\t [address_n]: list of numbers separated for comma and represents BIP-32 path to derive the key from master node.\n\t [hashIn]: input hash.", - }, - gcli.StringSliceFlag{ - Name: "output", - Usage: "Item with nest structure: \"[address] [address_n] [coins] [hours]\".\n\t [address]: target coin address in Base58 encoding. \n\t [address_n]: list of number separated for comma and represents BIP-32 path to derive the key from master node, has higher priority. \n\t [coins]: amount of transaction output.\n\t [hours]: accumulated hours.", - }, - gcli.StringFlag{ - Name: "deviceType", - Usage: "Device type to send instructions to, hardware wallet (USB) or emulator.", - EnvVar: "DEVICE_TYPE", - }, - }, - OnUsageError: onCommandUsageError(name), - Action: func(c *gcli.Context) { - version := uint32(c.Int("version")) - lockTime := uint32(c.Int("lockTime")) - inputsString := c.StringSlice("input") - outputsString := c.StringSlice("output") - - device := skyWallet.NewDevice(skyWallet.DeviceTypeFromString(c.String("deviceType"))) - log.Info("Start Action on txAck function") - if device == nil { - return - } - defer device.Close() - - if os.Getenv("AUTO_PRESS_BUTTONS") == "1" && device.Driver.DeviceType() == skyWallet.DeviceTypeEmulator && runtime.GOOS == "linux" { - err := device.SetAutoPressButton(true, skyWallet.ButtonRight) - if err != nil { - log.Error(err) - return - } - } - txType := &messages.TxAck_TransactionType{ - Version: &version, - LockTime: &lockTime, - Inputs: nil, - Outputs: nil, - } - // Building txType - if len(inputsString) != 0 { - if len(outputsString) != 0 { - log.Error("Inputs and outputs cannot be 0 in the same TxAck.") - return - } - // Building Inputs - Inputs := make([]*messages.TxAck_TransactionType_TxInputType, len(inputsString)) - for index, inputS := range inputsString { - input := strings.Split(inputS, " ") - /* - * input[0] --> address_n - * input[1] --> hashIn - */ - if len(input) != 2 { - log.Error("Bad Input sintax: ", inputS) - return - } - - // Building address_n - addressNString := strings.Split(input[0], ",") - addressN, err := toUnit32Array(&addressNString) - if err != nil { - log.Error(err) - return - } - - // Buildeing hashIn - hashIn := []byte(input[1]) - - finalInput := &messages.TxAck_TransactionType_TxInputType{ - AddressN: addressN, - HashIn: hashIn, - } - Inputs[index] = finalInput - } - txType.Inputs = Inputs - } else if len(outputsString) != 0 { - // Building outputs - Outputs := make([]*messages.TxAck_TransactionType_TxOutputType, len(outputsString)) - for index, outputS := range outputsString { - output := strings.Split(outputS, " ") - /* - * output[0] --> address - * output[1] --> address_n - * output[2] --> coins - * output[3] --> hours - */ - if len(output) != 4 { - log.Error("Bad Output sintax: ", outputS) - } - - // Building address - address := output[0] - - // Building address_n - addressNString := strings.Split(output[1], ",") - addressN, err := toUnit32Array(&addressNString) - if err != nil { - log.Error(err) - return - } - - // Build coins - coins, err := strconv.ParseUint(output[2], 10, 64) - if err != nil { - log.Error(err.Error()) - return - } - - // Build hours - hours, err := strconv.ParseUint(output[3], 10, 64) - if err != nil { - log.Error(err.Error()) - return - } - - finalOutput := &messages.TxAck_TransactionType_TxOutputType{ - Address: &address, - AddressN: addressN, - Coins: &coins, - Hours: &hours, - } - Outputs[index] = finalOutput - } - txType.Outputs = Outputs - } - msg, err := device.TxAck(txType) - if err != nil { - log.Error(err.Error()) - return - } - - switch msg.Kind { - case uint16(messages.MessageType_MessageType_TxRequest): - txRequest := &messages.TxRequest{} - err := proto.Unmarshal(msg.Data, txRequest) - if err != nil { - log.Error(err.Error()) - return - } - println(txRequest.String()) - default: - println("Unexpected response type\n") - } - }, - } -}