-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparse_descriptor.go
More file actions
145 lines (133 loc) · 4.15 KB
/
parse_descriptor.go
File metadata and controls
145 lines (133 loc) · 4.15 KB
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
package main
import (
"context"
"flag"
"fmt"
"strconv"
"strings"
cfd "github.com/cryptogarageinc/cfd-go"
)
// ParseDescriptorCmd verify signature.
type ParseDescriptorCmd struct {
cmd string
flagSet *flag.FlagSet
descriptor *string
nettype *string
childNum *uint
}
// NewParseDescriptorCmd returns a new ParseDescriptorCmd struct.
func NewParseDescriptorCmd() *ParseDescriptorCmd {
return &ParseDescriptorCmd{}
}
// Command returns the command name.
func (cmd *ParseDescriptorCmd) Command() string {
return cmd.cmd
}
// Parse parses the command arguments.
func (cmd *ParseDescriptorCmd) Parse(args []string) {
cmd.flagSet.Parse(args)
}
// Init initializes the command.
func (cmd *ParseDescriptorCmd) Init() {
cmd.cmd = "parsedescriptor"
cmd.flagSet = flag.NewFlagSet(cmd.cmd, flag.ExitOnError)
cmd.descriptor = cmd.flagSet.String("descriptor", "", "txin's utxo output descriptor")
cmd.nettype = cmd.flagSet.String("network", "mainnet", "network type (mainnet/testnet/regtest/liquidv1/liquidregtest)")
cmd.childNum = cmd.flagSet.Uint("childnum", uint(0), "derive child number")
}
// GetFlagSet returns the flag set for this command.
func (cmd *ParseDescriptorCmd) GetFlagSet() *flag.FlagSet {
return cmd.flagSet
}
// Do performs the command action.
func (cmd *ParseDescriptorCmd) Do(ctx context.Context) {
networkType := int(cfd.KCfdNetworkMainnet)
switch *cmd.nettype {
case "mainnet":
networkType = int(cfd.KCfdNetworkMainnet)
case "testnet":
networkType = int(cfd.KCfdNetworkTestnet)
case "regtest":
networkType = int(cfd.KCfdNetworkRegtest)
case "liquidv1":
networkType = int(cfd.KCfdNetworkLiquidv1)
case "liquidregtest":
networkType = int(cfd.KCfdNetworkElementsRegtest)
case "elementsregtest":
networkType = int(cfd.KCfdNetworkElementsRegtest)
default:
fmt.Printf("nettype %s is unknown type.", *cmd.nettype)
return
}
derivePath := strconv.FormatUint(uint64(*cmd.childNum), 10)
descList, keyList, err := cfd.CfdGoParseDescriptor(*cmd.descriptor, networkType, derivePath)
if err != nil {
fmt.Println(err)
return
}
for i := 0; i < len(descList); i++ {
fmt.Printf("[Depth:%d]\n", descList[i].Depth)
fmt.Printf(" - LockingScript: %s\n", descList[i].LockingScript)
if descList[i].ScriptType != int(cfd.KCfdDescriptorScriptRaw) {
fmt.Printf(" - Address : %s\n", descList[i].Address)
hashType := ""
switch descList[i].HashType {
case int(cfd.KCfdP2pkh):
hashType = "p2pkh"
case int(cfd.KCfdP2sh):
hashType = "p2sh"
case int(cfd.KCfdP2wpkh):
hashType = "p2wpkh"
case int(cfd.KCfdP2wsh):
hashType = "p2wsh"
case int(cfd.KCfdP2shP2wpkh):
hashType = "p2sh-p2wpkh"
case int(cfd.KCfdP2shP2wsh):
hashType = "p2sh-p2wsh"
default:
break
}
fmt.Printf(" - Type : %s\n", hashType)
}
if (descList[i].ScriptType == int(cfd.KCfdDescriptorScriptSh)) ||
(descList[i].ScriptType == int(cfd.KCfdDescriptorScriptWsh)) {
fmt.Printf(" - RedeemScript : %s\n", descList[i].RedeemScript)
scripts, err := cfd.CfdGoParseScript(descList[i].RedeemScript)
if err == nil {
fmt.Printf(" -> %s\n", strings.Join(scripts, " "))
}
}
if descList[i].IsMultisig {
fmt.Printf(" - requireNum : %d\n", descList[i].ReqSigNum)
break
} else if descList[i].KeyType != int(cfd.KCfdDescriptorKeyNull) {
key := ""
if descList[i].KeyType == int(cfd.KCfdDescriptorKeyPublic) {
key = descList[i].Pubkey
} else if descList[i].KeyType == int(cfd.KCfdDescriptorKeyBip32) {
key = descList[i].ExtPubkey
} else if descList[i].KeyType == int(cfd.KCfdDescriptorKeyBip32Priv) {
key = descList[i].ExtPrivkey
}
if len(key) > 0 {
fmt.Printf(" - key : %s\n", key)
}
}
}
if len(keyList) > 0 {
fmt.Println(" - multisig keys:")
}
for i := 0; i < len(keyList); i++ {
key := ""
if keyList[i].KeyType == int(cfd.KCfdDescriptorKeyPublic) {
key = keyList[i].Pubkey
} else if keyList[i].KeyType == int(cfd.KCfdDescriptorKeyBip32) {
key = keyList[i].ExtPubkey
} else if keyList[i].KeyType == int(cfd.KCfdDescriptorKeyBip32Priv) {
key = keyList[i].ExtPrivkey
}
if len(key) > 0 {
fmt.Printf(" - [%d] %s\n", i, key)
}
}
}