-
Notifications
You must be signed in to change notification settings - Fork 1
/
add_sign_transaction.go
295 lines (275 loc) · 7.83 KB
/
add_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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package main
import (
"context"
"flag"
"fmt"
"strings"
cfd "github.com/cryptogarageinc/cfd-go"
)
// AddSignTransactionCmd append tx input.
type AddSignTransactionCmd struct {
cmd string
flagSet *flag.FlagSet
txFilePath *string
tx *string
isElements *bool
txid *string
vout *uint
signature *string
pubkey *string
redeemScript *string
addrType *string
sigHashType *string
anyoneCanPay *bool
}
// NewAddSignTransactionCmd returns a new AddSignTransactionCmd struct.
func NewAddSignTransactionCmd() *AddSignTransactionCmd {
return &AddSignTransactionCmd{}
}
// Command returns the command name.
func (cmd *AddSignTransactionCmd) Command() string {
return cmd.cmd
}
// Parse parses the command arguments.
func (cmd *AddSignTransactionCmd) Parse(args []string) {
cmd.flagSet.Parse(args)
}
// Init initializes the command.
func (cmd *AddSignTransactionCmd) Init() {
cmd.cmd = "addsigntransaction"
cmd.flagSet = flag.NewFlagSet(cmd.cmd, flag.ExitOnError)
cmd.txFilePath = cmd.flagSet.String("file", "", "transaction data file path")
cmd.tx = cmd.flagSet.String("tx", "", "transaction in hex format")
cmd.isElements = cmd.flagSet.Bool("elements", false, "elements mode")
cmd.txid = cmd.flagSet.String("txid", "", "append transaction id")
cmd.vout = cmd.flagSet.Uint("vout", uint(0), "append transaction output number")
cmd.signature = cmd.flagSet.String("signature", "", "signature")
cmd.pubkey = cmd.flagSet.String("pubkey", "", "pubkey")
cmd.redeemScript = cmd.flagSet.String("script", "", "redeem script")
cmd.addrType = cmd.flagSet.String("addresstype", "",
"txin's utxo addressType (p2wpkh, p2wsh, p2sh-p2wpkh, p2sh-p2wsh, p2pkh, p2sh)")
cmd.sigHashType = cmd.flagSet.String("sighashtype", "all",
"sighashtype (all,single,none)")
cmd.anyoneCanPay = cmd.flagSet.Bool("anyonecanpay", false, "sighash anyonecanpay flag")
}
// GetFlagSet returns the flag set for this command.
func (cmd *AddSignTransactionCmd) GetFlagSet() *flag.FlagSet {
return cmd.flagSet
}
// Do performs the command action.
func (cmd *AddSignTransactionCmd) Do(ctx context.Context) {
var err error
data := NewTransactionCacheData()
tx := *cmd.tx
if *cmd.tx == "" && *cmd.txFilePath != "" {
data, err = ReadTransactionCache(*cmd.txFilePath)
if err != nil {
fmt.Println(err)
return
}
tx = data.Hex
}
if tx == "" {
fmt.Println("tx is required")
return
}
if *cmd.isElements == false {
fmt.Println("bitcoin tx sign is not implements.")
return
}
// other input parameter check
if len(*cmd.txid) != 64 {
fmt.Println("txid size invalid.")
return
}
sigHashType := int(cfd.KCfdSigHashAll)
switch *cmd.sigHashType {
case "all":
sigHashType = int(cfd.KCfdSigHashAll)
case "none":
sigHashType = int(cfd.KCfdSigHashNone)
case "single":
sigHashType = int(cfd.KCfdSigHashSingle)
default:
fmt.Printf("sighashtype %s is unknown type.", *cmd.sigHashType)
return
}
addrType := -1
redeemScript := *cmd.redeemScript
pubkey := *cmd.pubkey
tempPubkey, tempScript, tempAddrType, _, _, err := GetDescriptorInfoFromUtxoList(
*cmd.txid, uint32(*cmd.vout), data.Utxos)
if tempAddrType != -1 {
if len(*cmd.addrType) == 0 {
addrType = tempAddrType
}
if len(redeemScript) == 0 {
redeemScript = tempScript
}
if len(pubkey) == 0 {
pubkey = tempPubkey
}
}
if addrType == -1 {
switch *cmd.addrType {
case "p2pkh":
addrType = int(cfd.KCfdP2pkh)
case "p2sh":
addrType = int(cfd.KCfdP2sh)
case "p2sh-p2wpkh":
addrType = int(cfd.KCfdP2shP2wpkh)
case "p2sh-p2wsh":
addrType = int(cfd.KCfdP2shP2wsh)
case "p2wpkh":
addrType = int(cfd.KCfdP2wpkh)
case "p2wsh":
addrType = int(cfd.KCfdP2wsh)
default:
fmt.Printf("addresstype %s is unknown type.", *cmd.addrType)
return
}
}
isMulti := false
isScript := true
if addrType == int(cfd.KCfdP2pkh) || addrType == int(cfd.KCfdP2wpkh) ||
addrType == int(cfd.KCfdP2shP2wpkh) {
isScript = false
} else {
_, _, err = cfd.CfdGoGetAddressesFromMultisig(
redeemScript, int(cfd.KCfdNetworkMainnet), int(cfd.KCfdP2wpkh))
if err == nil {
isMulti = true
}
}
var txHex string
if !isScript {
signData := cfd.CfdSignParameter{
Data: *cmd.signature,
IsDerEncode: true,
SighashType: sigHashType,
SighashAnyoneCanPay: *cmd.anyoneCanPay,
}
txHex, err = cfd.CfdGoAddConfidentialTxPubkeyHashSign(
tx, *cmd.txid, uint32(*cmd.vout), addrType, pubkey,
signData)
} else if isMulti {
sigList := strings.Split(*cmd.signature, ",")
pubkeyList := strings.Split(*cmd.pubkey, ",")
if len(pubkeyList) > 0 && len(sigList) != len(pubkeyList) {
fmt.Println("pubkey count is unmatch signature count.")
return
}
signList := []cfd.CfdMultisigSignData{}
for index, signature := range sigList {
if len(signature) > 0 {
pubkey := ""
if len(pubkeyList) > 0 {
pubkey = pubkeyList[index]
}
data := cfd.CfdMultisigSignData{
Signature: signature,
IsDerEncode: true,
SighashType: sigHashType,
SighashAnyoneCanPay: *cmd.anyoneCanPay,
RelatedPubkey: pubkey,
}
signList = append(signList, data)
}
}
txHex, err = cfd.CfdGoAddConfidentialTxMultisigSign(
tx, *cmd.txid, uint32(*cmd.vout), addrType,
signList, redeemScript)
} else {
sigList := strings.Split(*cmd.signature, ",")
signList := []cfd.CfdSignParameter{}
for _, signature := range sigList {
if len(signature) > 0 {
data := cfd.CfdSignParameter{
Data: signature,
IsDerEncode: false,
SighashType: int(cfd.KCfdSigHashAll),
SighashAnyoneCanPay: false,
}
signList = append(signList, data)
}
}
txHex, err = cfd.CfdGoAddConfidentialTxScriptHashSign(
tx, *cmd.txid, uint32(*cmd.vout), addrType,
signList, redeemScript)
}
if err != nil {
fmt.Println(err)
return
}
if *cmd.txFilePath != "" {
data.Hex = txHex
_, err = WriteTransactionCache(*cmd.txFilePath, data)
if err != nil {
fmt.Println(err)
return
}
}
}
// GetDescriptorInfoFromUtxoList get descriptor info.
func GetDescriptorInfoFromUtxoList(txid string, vout uint32, utxoList []UtxoData) (
pubkey, redeemScript string, hashType int, amount int64,
amountCommitment string, err error) {
hashType = -1
if utxoList != nil {
desc := ""
for _, utxo := range utxoList {
if utxo.Txid == txid && utxo.Vout == vout {
amount = utxo.Amount
amountCommitment = utxo.AmountCommitment
desc = utxo.Descriptor
break
}
}
if len(desc) > 0 {
pubkey, redeemScript, hashType, _, err = ParseDescriptor(
desc, int(cfd.KCfdNetworkMainnet))
if err != nil {
fmt.Println(err)
return "", "", -1, -1, "", err
}
}
}
return pubkey, redeemScript, hashType, amount, amountCommitment, nil
}
// ParseDescriptor parse descriptor
func ParseDescriptor(descriptor string, networkType int) (pubkey, redeemScript string, hashType int, address string, err error) {
descList, _, err := cfd.CfdGoParseDescriptor(
descriptor, networkType, "")
if err != nil {
fmt.Println(err)
return "", "", -1, "", err
}
hashType = descList[0].HashType
address = descList[0].Address
redeemScript = descList[len(descList)-1].RedeemScript
extkey := ""
switch descList[len(descList)-1].KeyType {
case int(cfd.KCfdDescriptorKeyPublic):
pubkey = descList[len(descList)-1].Pubkey
case int(cfd.KCfdDescriptorKeyBip32):
extkey = descList[len(descList)-1].ExtPubkey
case int(cfd.KCfdDescriptorKeyBip32Priv):
extkey = descList[len(descList)-1].ExtPrivkey
default:
// do nothing
}
if len(extkey) > 0 {
key, err := cfd.CfdGoGetPubkeyFromExtkey(
extkey, int(cfd.KCfdNetworkMainnet))
if err != nil {
key, err = cfd.CfdGoGetPubkeyFromExtkey(
extkey, int(cfd.KCfdNetworkTestnet))
}
if err != nil {
fmt.Println(err)
return "", "", -1, "", err
}
pubkey = key
}
return pubkey, redeemScript, hashType, address, nil
}