forked from dgyoshi/cfd-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode_raw_transaction.go
98 lines (85 loc) · 2.28 KB
/
decode_raw_transaction.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
import (
"bytes"
"context"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"os"
"strings"
cfd "github.com/cryptogarageinc/cfd-go"
)
// DecodeRawTransactionCmd decode transaction hex.
type DecodeRawTransactionCmd struct {
cmd string
flagSet *flag.FlagSet
tx *string
txFilePath *string
nettype *string
isElements *bool
}
// NewDecodeRawTransactionCmd returns a new DecodeRawTransactionCmd struct.
func NewDecodeRawTransactionCmd() *DecodeRawTransactionCmd {
return &DecodeRawTransactionCmd{}
}
// Command returns the command name.
func (cmd *DecodeRawTransactionCmd) Command() string {
return cmd.cmd
}
// Parse parses the command arguments.
func (cmd *DecodeRawTransactionCmd) Parse(args []string) {
cmd.flagSet.Parse(args)
}
// Init initializes the command.
func (cmd *DecodeRawTransactionCmd) Init() {
cmd.cmd = "decoderawtransaction"
cmd.flagSet = flag.NewFlagSet(cmd.cmd, flag.ExitOnError)
cmd.tx = cmd.flagSet.String("tx", "", "transaction in hex format")
cmd.txFilePath = cmd.flagSet.String("file", "", "transaction data file path")
cmd.nettype = cmd.flagSet.String("network", "mainnet", "network type (mainnet/testnet/regtest)")
cmd.isElements = cmd.flagSet.Bool("elements", false, "elements mode")
}
// GetFlagSet returns the flag set for this command.
func (cmd *DecodeRawTransactionCmd) GetFlagSet() *flag.FlagSet {
return cmd.flagSet
}
// Do performs the command action.
func (cmd *DecodeRawTransactionCmd) Do(ctx context.Context) {
tx := *cmd.tx
if *cmd.tx == "" && *cmd.txFilePath != "" {
_, err := os.Stat(*cmd.txFilePath)
if err != nil {
fmt.Println("tx data file not found.")
return
}
txcache, err := ReadTransactionCache(*cmd.txFilePath)
if err == nil {
tx = txcache.Hex
} else {
bytes, err := ioutil.ReadFile(*cmd.txFilePath)
if err != nil {
fmt.Println(err)
return
}
tx = strings.TrimSpace(string(bytes))
}
}
if tx == "" {
fmt.Println("tx is required")
return
}
jsonData, err := cfd.CfdGoDecodeRawTransactionJson(tx, *cmd.nettype, *cmd.isElements)
if err != nil {
fmt.Println(err)
return
}
var buf bytes.Buffer
err = json.Indent(&buf, []byte(jsonData), "", " ")
if err != nil {
fmt.Println(err)
return
}
indentJSON := buf.String()
fmt.Printf("decode transaction:\n%s\n", indentJSON)
}