-
Notifications
You must be signed in to change notification settings - Fork 1
/
get_extkeypair_from_seed.go
85 lines (70 loc) · 2.06 KB
/
get_extkeypair_from_seed.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
package main
import (
"context"
"flag"
"fmt"
cfd "github.com/cryptogarageinc/cfd-go"
)
// GetExtkeypairFromSeedCmd deriveds a extended private key from seed.
type GetExtkeypairFromSeedCmd struct {
cmd string
flagSet *flag.FlagSet
seed *string
networkType *string
path *string
}
// NewGetExtkeypairFromSeedCmd returns a new GetExtkeypairFromSeedCmd struct.
func NewGetExtkeypairFromSeedCmd() *GetExtkeypairFromSeedCmd {
return &GetExtkeypairFromSeedCmd{}
}
// Command returns its command name.
func (cmd *GetExtkeypairFromSeedCmd) Command() string {
return cmd.cmd
}
// Parse parses the command arguments.
func (cmd *GetExtkeypairFromSeedCmd) Parse(args []string) {
cmd.flagSet.Parse(args)
}
func (cmd *GetExtkeypairFromSeedCmd) Init() {
cmd.cmd = "getextkeypairfromseed"
cmd.flagSet = flag.NewFlagSet(cmd.cmd, flag.ExitOnError)
cmd.seed = cmd.flagSet.String("seed", "", "seed in hex format")
cmd.networkType = cmd.flagSet.String("network", "", "mainnet | testnet | regtest | liquid | elementsregtest")
cmd.path = cmd.flagSet.String("path", "", "key path. i.e. m/44h/0h/0h/0/0")
}
func (cmd *GetExtkeypairFromSeedCmd) GetFlagSet() *flag.FlagSet {
return cmd.flagSet
}
func (cmd *GetExtkeypairFromSeedCmd) Do(ctx context.Context) {
if *cmd.seed == "" {
fmt.Println("seed is required")
return
}
networkType := cfd.KCfdNetworkMainnet
if cmd.networkType != nil && len(*cmd.networkType) > 0 {
switch *cmd.networkType {
case "testnet":
networkType = cfd.KCfdNetworkTestnet
case "regtest":
networkType = cfd.KCfdNetworkRegtest
}
}
xpriv, err := cfd.CfdGoCreateExtkeyFromSeed(*cmd.seed, int(networkType), int(cfd.KCfdExtPrivkey))
if err != nil {
fmt.Println(err)
return
}
if *cmd.path != "" {
xpriv, err = cfd.CfdGoCreateExtkeyFromParentPath(xpriv, *cmd.path, int(networkType), int(cfd.KCfdExtPrivkey))
if err != nil {
fmt.Println(err)
return
}
}
xpub, err := cfd.CfdGoCreateExtPubkey(xpriv, int(networkType))
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("xpriv: '%s'\nxpub: '%s'\n", xpriv, xpub)
}