Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 45 additions & 16 deletions internal/cli/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/base64"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -1418,13 +1419,8 @@ func NewSignTransactionCommand(inv *CommandParseResult) Command {
}
}

// Execute signs a transaction
func (c *SignTransactionCommand) Execute(ctx context.Context, ee *ExecutionEnvironment) (*ExecutionResult, error) {
if !ee.IsWalletOpen() {
return nil, fmt.Errorf("%w: cannot sign transaction", cliutil.ErrWalletClosed)
}

trxBytes, err := base64.URLEncoding.DecodeString(c.Transaction)
func parseTransaction(transaction string) (*protocol.Transaction, error) {
trxBytes, err := base64.URLEncoding.DecodeString(transaction)
if err != nil {
return nil, err
}
Expand All @@ -1435,25 +1431,58 @@ func (c *SignTransactionCommand) Execute(ctx context.Context, ee *ExecutionEnvir
return nil, err
}

err = util.SignTransaction(ee.Key.PrivateBytes(), trx)
if err != nil {
return nil, err
return trx, nil
}

func parseTransactionId(tid string) ([]byte, error) {
return hex.DecodeString(tid[2:])
}

// Execute signs a transaction
func (c *SignTransactionCommand) Execute(ctx context.Context, ee *ExecutionEnvironment) (*ExecutionResult, error) {
if !ee.IsWalletOpen() {
return nil, fmt.Errorf("%w: cannot sign transaction", cliutil.ErrWalletClosed)
}

trxBytes, err = proto.Marshal(trx)
trx, err := parseTransaction(c.Transaction)
if err == nil {
err = util.SignTransaction(ee.Key.PrivateBytes(), trx)
if err != nil {
return nil, err
}

trxBytes, err := proto.Marshal(trx)
if err != nil {
return nil, err
}

jsonTrx, err := json.MarshalIndent(trx, "", " ")
if err != nil {
return nil, err
}

encodedTrx := base64.URLEncoding.EncodeToString(trxBytes)

result := NewExecutionResult()
result.AddMessage(fmt.Sprintf("Signed Transaction:\nJSON:\n%v\nBase64:\n%v", string(jsonTrx), encodedTrx))

return result, nil
}

trxErr := err

tid, err := parseTransactionId(c.Transaction)
if err != nil {
return nil, err
return nil, trxErr
}

jsonTrx, err := json.MarshalIndent(trx, "", " ")
signature, err := cliutil.SignTransactionId(ee.Key.PrivateBytes(), tid)
if err != nil {
return nil, err
}

encodedTrx := base64.URLEncoding.EncodeToString(trxBytes)

result := NewExecutionResult()
result.AddMessage(fmt.Sprintf("Signed Transaction:\nJSON:\n%v\nBase64:\n%v", string(jsonTrx), encodedTrx))
result.AddMessage(fmt.Sprintf("Transaction Signature: %v", base64.URLEncoding.EncodeToString(signature)))

return result, nil
}
Expand Down
13 changes: 13 additions & 0 deletions internal/cliutil/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,16 @@ func SignTransaction(key []byte, tx *protocol.Transaction) error {

return nil
}

// SignTransactionId signs the transaction ID with the given key
func SignTransactionId(key []byte, tid []byte) ([]byte, error) {
privateKey, _ := btcec.PrivKeyFromBytes(btcec.S256(), key)

// Decode to multihash ID
idBytes, err := multihash.Decode(tid)
if err != nil {
return nil, err
}

return btcec.SignCompact(btcec.S256(), privateKey, idBytes.Digest, true)
}