Skip to content

Commit

Permalink
feat(cmd): Add support for outputting unsigned transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
kostko committed May 9, 2023
1 parent dabce52 commit 107c39a
Showing 1 changed file with 55 additions and 13 deletions.
68 changes: 55 additions & 13 deletions cmd/common/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,23 @@ import (
)

var (
txOffline bool
txNonce uint64
txGasLimit uint64
txGasPrice string
txEncrypted bool
txYes bool
txOffline bool
txNonce uint64
txGasLimit uint64
txGasPrice string
txEncrypted bool
txYes bool
txUnsigned bool
txFormat string
txOutputFile string
)

const (
invalidNonce = math.MaxUint64
invalidGasLimit = math.MaxUint64

formatJSON = "json"
formatCBOR = "cbor"
)

// TransactionFlags contains the common transaction flags.
Expand Down Expand Up @@ -75,7 +81,7 @@ func SignConsensusTransaction(
wallet wallet.Account,
conn connection.Connection,
tx *consensusTx.Transaction,
) (*consensusTx.SignedTransaction, error) {
) (interface{}, error) {
// Require consensus signer.
signer := wallet.ConsensusSigner()
if signer == nil {
Expand Down Expand Up @@ -135,6 +141,11 @@ func SignConsensusTransaction(
}
tx.Fee.Amount = *gasPrice

if txUnsigned {
// Return an unsigned transaction.
return tx, nil
}

PrintTransactionBeforeSigning(npa, tx)

// Sign the transaction.
Expand All @@ -159,7 +170,7 @@ func SignParaTimeTransaction(
conn connection.Connection,
tx *types.Transaction,
txDetails *signature.TxDetails,
) (*types.UnverifiedTransaction, interface{}, error) {
) (interface{}, interface{}, error) {
if npa.ParaTime == nil {
return nil, nil, fmt.Errorf("no paratime configured for paratime transaction signing")
}
Expand Down Expand Up @@ -253,6 +264,11 @@ func SignParaTimeTransaction(
tx.Call = *encCall
}

if txUnsigned {
// Return an unsigned transaction.
return tx, meta, nil
}

PrintTransactionBeforeSigning(npa, tx)

// Sign the transaction.
Expand Down Expand Up @@ -338,10 +354,33 @@ func PrintTransactionBeforeSigning(npa *NPASelection, tx interface{}) {

// PrintSignedTransaction prints a signed transaction.
func PrintSignedTransaction(sigTx interface{}) {
// TODO: Add some options for controlling output.
formatted, err := json.MarshalIndent(sigTx, "", " ")
cobra.CheckErr(err)
fmt.Println(string(formatted))
// Determine output destination.
var err error
outputFile := os.Stdout
if txOutputFile != "" {
outputFile, err = os.Create(txOutputFile)
if err != nil {
cobra.CheckErr(fmt.Errorf("failed to open output file: %w", err))
}
defer outputFile.Close()
}

// Determine output format.
var data []byte
switch txFormat {
case formatJSON:
data, err = json.MarshalIndent(sigTx, "", " ")
cobra.CheckErr(err)
case formatCBOR:
data = cbor.Marshal(sigTx)
default:
cobra.CheckErr(fmt.Errorf("unknown transaction format: %s", txFormat))
}

_, err = outputFile.Write(data)
if err != nil {
cobra.CheckErr(fmt.Errorf("failed to write output: %w", err))
}
}

// BroadcastTransaction broadcasts a transaction.
Expand All @@ -355,7 +394,7 @@ func BroadcastTransaction(
meta interface{},
result interface{},
) {
if txOffline {
if txOffline || txUnsigned {
PrintSignedTransaction(tx)
return
}
Expand Down Expand Up @@ -474,4 +513,7 @@ func init() {
TransactionFlags.StringVar(&txGasPrice, "gas-price", "", "override gas price to use")
TransactionFlags.BoolVar(&txEncrypted, "encrypted", false, "encrypt transaction call data (requires online mode)")
TransactionFlags.BoolVarP(&txYes, "yes", "y", false, "answer yes to all questions")
TransactionFlags.BoolVar(&txUnsigned, "unsigned", false, "do not sign transaction")
TransactionFlags.StringVar(&txFormat, "format", "json", "transaction output format (for offline/unsigned modes) [json, cbor]")
TransactionFlags.StringVar(&txOutputFile, "output-file", "", "output transaction into specified file (for offline/unsigned modes)")
}

0 comments on commit 107c39a

Please sign in to comment.