mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-03-30 15:08:33 +00:00

* Add --transaction-file to the sign wallet subcommand. * Fix bad short sign config option. * Trim whitespace around the hex file. * Add --transaction-file to the broadcast subcommand.
55 lines
1.4 KiB
Go
55 lines
1.4 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"github.com/kaspanet/kaspad/cmd/kaspawallet/daemon/client"
|
|
"github.com/kaspanet/kaspad/cmd/kaspawallet/daemon/pb"
|
|
"github.com/pkg/errors"
|
|
"io/ioutil"
|
|
"strings"
|
|
)
|
|
|
|
func broadcast(conf *broadcastConfig) error {
|
|
daemonClient, tearDown, err := client.Connect(conf.DaemonAddress)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer tearDown()
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), daemonTimeout)
|
|
defer cancel()
|
|
|
|
if conf.Transaction == "" && conf.TransactionFile == "" {
|
|
return errors.Errorf("Either --transaction or --transaction-file is required")
|
|
}
|
|
if conf.Transaction != "" && conf.TransactionFile != "" {
|
|
return errors.Errorf("Both --transaction and --transaction-file cannot be passed at the same time")
|
|
}
|
|
|
|
transactionHex := conf.Transaction
|
|
if conf.TransactionFile != "" {
|
|
transactionHexBytes, err := ioutil.ReadFile(conf.TransactionFile)
|
|
if err != nil {
|
|
return errors.Wrapf(err, "Could not read hex from %s", conf.TransactionFile)
|
|
}
|
|
transactionHex = strings.TrimSpace(string(transactionHexBytes))
|
|
}
|
|
|
|
transaction, err := hex.DecodeString(transactionHex)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
response, err := daemonClient.Broadcast(ctx, &pb.BroadcastRequest{Transaction: transaction})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
fmt.Println("Transaction was sent successfully")
|
|
fmt.Printf("Transaction ID: \t%s\n", response.TxID)
|
|
|
|
return nil
|
|
}
|