-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathverify_sign_transaction.go
157 lines (142 loc) · 4.08 KB
/
verify_sign_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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package main
import (
"context"
"flag"
"fmt"
"io/ioutil"
"os"
"strings"
cfd "github.com/cryptogarageinc/cfd-go"
)
// VerifySignTransactionCmd verify sign from transaction.
type VerifySignTransactionCmd struct {
cmd string
flagSet *flag.FlagSet
tx *string
txFilePath *string
isElements *bool
txid *string
vout *uint
address *string
addrType *string
descriptor *string
amount *uint64
commitment *string
}
// NewVerifySignTransactionCmd returns a new VerifySignTransactionCmd struct.
func NewVerifySignTransactionCmd() *VerifySignTransactionCmd {
return &VerifySignTransactionCmd{}
}
// Command returns the command name.
func (cmd *VerifySignTransactionCmd) Command() string {
return cmd.cmd
}
// Parse parses the command arguments.
func (cmd *VerifySignTransactionCmd) Parse(args []string) {
cmd.flagSet.Parse(args)
}
// Init initializes the command.
func (cmd *VerifySignTransactionCmd) Init() {
cmd.cmd = "verifysigntransaction"
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.isElements = cmd.flagSet.Bool("elements", false, "elements mode")
cmd.txid = cmd.flagSet.String("txid", "", "txin's txid")
cmd.vout = cmd.flagSet.Uint("vout", 0, "txin's vout")
cmd.descriptor = cmd.flagSet.String("descriptor", "", "txin's utxo output descriptor")
cmd.address = cmd.flagSet.String("address", "", "txin's utxo address (not exist descriptor)")
cmd.addrType = cmd.flagSet.String("addresstype", "", "txin's utxo addressType (p2wpkh, p2wsh, p2sh-p2wpkh, p2sh-p2wsh, p2pkh, p2sh)")
cmd.amount = cmd.flagSet.Uint64("amount", 0, "txin's utxo amount")
cmd.commitment = cmd.flagSet.String("commitment", "", "txin's utxo amount commitment (elements mode only)")
}
// GetFlagSet returns the flag set for this command.
func (cmd *VerifySignTransactionCmd) GetFlagSet() *flag.FlagSet {
return cmd.flagSet
}
// Do performs the command action.
func (cmd *VerifySignTransactionCmd) 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
}
netType := int(cfd.KCfdNetworkMainnet)
if *cmd.isElements {
netType = int(cfd.KCfdNetworkLiquidv1)
}
addrType := -1
address := *cmd.address
if len(*cmd.descriptor) > 0 {
_, _, tempHashType, tempAddr, err := ParseDescriptor(*cmd.descriptor, netType)
if err != nil {
fmt.Println(err)
return
}
if len(*cmd.addrType) == 0 {
addrType = tempHashType
}
if len(address) == 0 {
address = tempAddr
}
}
if addrType == -1 {
switch *cmd.addrType {
case "p2pkh":
addrType = int(cfd.KCfdP2pkhAddress)
case "p2sh":
addrType = int(cfd.KCfdP2shAddress)
case "p2sh-p2wpkh":
addrType = int(cfd.KCfdP2shP2wpkhAddress)
case "p2sh-p2wsh":
addrType = int(cfd.KCfdP2shP2wshAddress)
case "p2wpkh":
addrType = int(cfd.KCfdP2wpkhAddress)
case "p2wsh":
addrType = int(cfd.KCfdP2wshAddress)
default:
fmt.Printf("addresstype %s is unknown type.", *cmd.addrType)
return
}
}
var isVerify bool
var err error
var reason string
if *cmd.isElements {
isVerify, reason, err = cfd.CfdGoVerifyConfidentialTxSignReason(
tx, *cmd.txid, uint32(*cmd.vout), address,
addrType, "", int64(*cmd.amount), *cmd.commitment)
} else {
isVerify, reason, err = cfd.CfdGoVerifyTxSignReason(netType, tx,
*cmd.txid, uint32(*cmd.vout), address, addrType,
"", int64(*cmd.amount), *cmd.commitment)
}
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("outpoint: %s,%d\n", *cmd.txid, *cmd.vout)
if isVerify {
fmt.Println("verify: success.")
} else {
fmt.Printf("verify: fail. reason: %s\n", reason)
}
}