Skip to content

Commit 31251b4

Browse files
committed
added json return of keyset for Nostr and Bitcoin
1 parent 3963b75 commit 31251b4

File tree

10 files changed

+482
-229
lines changed

10 files changed

+482
-229
lines changed

README.md

Lines changed: 66 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ ENVIRONMENT VARIABLE OPTIONS
6060
DESCRIPTION
6161
hdkeys allows for the creation of mnemonic seeds, and Hierarchical Deterministic (HD) addresses.
6262

63+
- hdkeys supports BIP39 passphrase protection.
64+
- hdkeys creates Bitcoin and Nostr accounts from the same mnemonic seeds
65+
- hdkeys can create WIF (Wallet Import Format), and decode private keys from WIF
66+
6367
BIP32 - Hierarchical Deterministic Wallets
6468
BIP39 - Mnemonic code for generating deterministic keys
6569
BIP43 - Purpose Field for Deterministic Wallets
@@ -69,28 +73,36 @@ hdkeys allows for the creation of mnemonic seeds, and Hierarchical Deterministic
6973
BIP86 - Derivation scheme for Pay-to-Taproot (P2TR) based accounts
7074
BIP173 - Base32 address format for native v0-16 witness outputs
7175
SLIP44 - Registered coin types for BIP-0044
72-
NIP06 - Basic key derivation from mnemonic seed phrase (https://nostr-nips.com/nip-06)
73-
NIP19 - bech32-encoded entities
76+
NIP06 - Basic key derivation from mnemonic seed phrase
7477
...
7578

76-
COMMANDS:
77-
wif [prompt] or [Environment variable]
79+
COMMANDS
80+
wof
81+
Wall Of Fame, prints a whole set of keys
82+
COMMAND OPTIONS
83+
-count [int] (default = 1)
84+
Set number of keys to generate.
85+
86+
wif [string] or [Environment variable]
7887
Decode the private key from wif(Wallet Import Format), then generate the address.
7988

80-
OPTIONS:
81-
-h or --help [bool]
82-
Report usage information and exit.
89+
keyset
90+
Gets a Bitcoin and Nostr key set with the same WIF (Wallet Import Format) as JSON.
91+
COMMAND OPTIONS
92+
-no [int] (default = 0)
93+
Nostr Account number to generate
8394

84-
-wif [string]
85-
Decode the private key from wif(Wallet Import Format), then generate the address.
95+
OPTIONS
96+
-mnemonic [string]
97+
Mnemonic words
8698

8799
-pass [string]
88100
Protect bip39 mnemonic with a passphrase via flag,
89101
or use environment variable,
90102
or you will be asked to enter at a prompt.
91103

92-
-n [int] (default = 1)
93-
Set number of keys to generate.
104+
-h or --help [bool]
105+
Report usage information and exit.
94106

95107
-v [bool]
96108
Print version tag and exit.
@@ -189,6 +201,49 @@ $ hdkeys -wif KyHXurGfBovpZpgQjG37ujZaNQobN8rUcamafamJWXwXHkumzVEV
189201
190202
```
191203

204+
```bash
205+
$ hdkeys keyset -mnemonic "leader monkey parrot ring guide accident before fence cannon height naive bean"
206+
{"Nostr":{"Path":"m/44'/1237'/0'/0/0","Wif":"L1VZ55UPgF83k4ndU8BBf62eM9prgo4coie5ttZrvS8GBzddzrhD","NSec":"nsec10allq0gjx7fddtzef0ax00mdps9t2kmtrldkyjfs8l5xruwvh2dq0lhhkp","NPub":"npub1zutzeysacnf9rru6zqwmxd54mud0k44tst6l70ja5mhv8jjumytsd2x7nu"},"Bitcoin":{"Wif":"L1VZ55UPgF83k4ndU8BBf62eM9prgo4coie5ttZrvS8GBzddzrhD","Lagacy":"1FyKyNJKMVDLkhH29KmDPUNubcnSrQcoWR","Nested":"3Me6rM5XAfKNGDhVpcozk7NcRj7CDnGYow","SegWit":"bc1q5suwgj96zvqgjq2d8df6wrkxuffz93fxsx3n3l","Taproot":"bc1pmupac93le8w8ncqmsupfmcvc9667t836p9q78zc26pqff5zf5xxswh9re2"}}
207+
```
208+
```json
209+
{
210+
"Nostr": {
211+
"Path": "m/44'/1237'/0'/0/0",
212+
"Wif": "L1VZ55UPgF83k4ndU8BBf62eM9prgo4coie5ttZrvS8GBzddzrhD",
213+
"NSec": "nsec10allq0gjx7fddtzef0ax00mdps9t2kmtrldkyjfs8l5xruwvh2dq0lhhkp",
214+
"NPub": "npub1zutzeysacnf9rru6zqwmxd54mud0k44tst6l70ja5mhv8jjumytsd2x7nu"
215+
},
216+
"Bitcoin": {
217+
"Wif": "L1VZ55UPgF83k4ndU8BBf62eM9prgo4coie5ttZrvS8GBzddzrhD",
218+
"Lagacy": "1FyKyNJKMVDLkhH29KmDPUNubcnSrQcoWR",
219+
"Nested": "3Me6rM5XAfKNGDhVpcozk7NcRj7CDnGYow",
220+
"SegWit": "bc1q5suwgj96zvqgjq2d8df6wrkxuffz93fxsx3n3l",
221+
"Taproot": "bc1pmupac93le8w8ncqmsupfmcvc9667t836p9q78zc26pqff5zf5xxswh9re2"
222+
}
223+
}
224+
```
225+
226+
Example address path #7 with Passphrase
227+
```bash
228+
$ hdkeys keyset -no 7 -pass 2345 -mnemonic "leader monkey parrot ring guide accident before fence cannon height naive bean"
229+
```
230+
```json
231+
{
232+
"Nostr": {
233+
"Path": "m/44'/1237'/7'/0/0",
234+
"Wif": "L35EeWsQHijBKXJQJMdCnGrE8L2xLsYTicomb8kAgujKrcigZFYw",
235+
"NSec": "nsec1464z2lqk9q5a7ct79wunnax3w2urrkpmsz4u9c6zttws4ueg89ssfz8xa6",
236+
"NPub": "npub15jqt9xfzf7awawtls6qq9cy5hj4rpgxrs6meqnae7eaw2nvgsqksdr0lcp"
237+
},
238+
"Bitcoin": {
239+
"Wif": "L35EeWsQHijBKXJQJMdCnGrE8L2xLsYTicomb8kAgujKrcigZFYw",
240+
"Lagacy": "1LGj6Vyi24uZyLWURK2Gm3eu26vrn1L43D",
241+
"Nested": "3GygZBMK6vfQ4R2Vcw9K6dG7GWwfsV4dkq",
242+
"SegWit": "bc1q6d3uftavdr6hresf5xl3e7clr6kt2l5ednwlf8",
243+
"Taproot": "bc1pulhwyfv8e6akfksqsdugn2wyk8fn4n0hft3rdx0mtke3npmwzrxqgjc2sf"
244+
}
245+
}
246+
```
192247

193248
License
194249
-------

bin/flag.go

Lines changed: 101 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,67 @@ const FILE_NAME_MNEMONIC = "mnemonic-words"
1818
const compress = true // generate a compressed public key
1919

2020
var (
21-
flag_number int = 1
22-
flag_debug = false
21+
flag_number int
22+
flag_count int = 1
23+
flag_debug bool
24+
command string
25+
argsLength int
2326
)
2427

28+
func init() {
29+
argsLength = len(os.Args)
30+
}
31+
32+
func hasNext(no int) bool {
33+
if argsLength > no {
34+
return true
35+
}
36+
return false
37+
}
38+
39+
func getNext(no int) string {
40+
return os.Args[no+1]
41+
}
42+
43+
func getNextArgAsInt(no, varsayılan int) int {
44+
if hasNext(no) {
45+
if i, err := strconv.Atoi(getNext(no)); err == nil {
46+
return i
47+
} else {
48+
Warn.Println(err)
49+
}
50+
} else {
51+
Warn.Println("Value expected, was not found")
52+
}
53+
return varsayılan
54+
}
55+
2556
func flag() {
2657

58+
if argsLength > 1 {
59+
60+
// check for commands
61+
switch os.Args[1] {
62+
63+
case "wof":
64+
command = "wof"
65+
break
66+
67+
case "keyset":
68+
command = "keyset"
69+
break
70+
71+
case "wif":
72+
if hasNext(1) {
73+
fromWifInput(getNext(1))
74+
} else {
75+
fromWifInput("")
76+
}
77+
os.Exit(0)
78+
}
79+
}
80+
81+
// check for options
2782
for no, arg := range os.Args {
2883
switch arg {
2984

@@ -39,46 +94,41 @@ func flag() {
3994
fmt.Printf("%s\n", build.Info())
4095
os.Exit(0)
4196

42-
case "-mnemonic":
43-
var mnemonic = os.Args[no+1]
44-
os.Setenv(ENV_MNEMONIC, mnemonic)
45-
Info.Printf("Setting env veriable %s\n", ENV_MNEMONIC)
97+
case "-d", "--debug":
98+
debug(true)
4699
continue
47100

48-
case "-pass":
49-
var pass = os.Args[no+1]
50-
os.Setenv(ENV_PASSWORD, pass)
51-
Info.Printf("Setting env veriable %s\n", ENV_PASSWORD)
101+
case "--verbose":
102+
debug(true)
103+
verbose(true)
52104
continue
53105

54-
case "-debug":
55-
debug(true)
106+
case "-no":
107+
Debug.Println("In case -n")
108+
flag_number = getNextArgAsInt(no, 0)
109+
Debug.Println("113 flag_number: ", flag_number)
56110
continue
57111

58-
case "-verbose":
59-
verbose(true)
112+
case "-mnemonic":
113+
if hasNext(no) {
114+
os.Setenv(ENV_MNEMONIC, getNext(no))
115+
Info.Printf("Setting env veriable %s\n", ENV_MNEMONIC)
116+
}
60117
continue
61118

62-
case "-n":
63-
if num, err := strconv.Atoi(os.Args[no+1]); err == nil {
64-
flag_number = num
65-
Debug.Println("flag_number: ", flag_number)
119+
case "-pass", "-password", "-passphrase":
120+
if hasNext(no) {
121+
os.Setenv(ENV_PASSWORD, getNext(no))
122+
Info.Printf("Setting env veriable %s\n", ENV_PASSWORD)
66123
} else {
67-
Error.Println("Flag -n needs a number. 1 is the default.")
68-
Error.Fatal(err)
124+
Error.Println("No passphrase provided on command line")
69125
}
70126
continue
71127

72-
case "wif":
73-
fromWifInput()
74-
os.Exit(0)
75-
76-
case "-wif", "--wif":
77-
var a = os.Args[no+1]
78-
os.Setenv(ENV_WIF, a)
79-
Info.Printf("Setting env veriable %s\n", ENV_WIF)
80-
fromWifInput()
81-
os.Exit(0)
128+
case "-count":
129+
Debug.Println("In case -count")
130+
flag_count = getNextArgAsInt(no, 1)
131+
continue
82132
}
83133
}
84134
}
@@ -98,6 +148,10 @@ ENVIRONMENT VARIABLE OPTIONS
98148
DESCRIPTION
99149
hdkeys allows for the creation of mnemonic seeds, and Hierarchical Deterministic (HD) addresses.
100150
151+
- hdkeys supports BIP39 passphrase protection.
152+
- hdkeys creates Bitcoin and Nostr accounts from the same mnemonic seeds
153+
- hdkeys can create WIF (Wallet Import Format), and decode private keys from WIF
154+
101155
BIP32 - Hierarchical Deterministic Wallets
102156
BIP39 - Mnemonic code for generating deterministic keys
103157
BIP43 - Purpose Field for Deterministic Wallets
@@ -111,23 +165,32 @@ hdkeys allows for the creation of mnemonic seeds, and Hierarchical Deterministic
111165
...
112166
113167
COMMANDS
114-
wif [prompt] or [Environment variable]
168+
wof
169+
Wall Of Fame, prints a whole set of keys
170+
COMMAND OPTIONS
171+
-count [int] (default = 1)
172+
Set number of keys to generate.
173+
174+
wif [string] or [Environment variable]
115175
Decode the private key from wif(Wallet Import Format), then generate the address.
116176
117-
OPTIONS
118-
-h or --help [bool]
119-
Report usage information and exit.
177+
keyset
178+
Gets a Bitcoin and Nostr key set with the same WIF (Wallet Import Format) as JSON.
179+
COMMAND OPTIONS
180+
-no [int] (default = 0)
181+
Nostr Account number to generate
120182
121-
-wif [string]
122-
Decode the private key from wif(Wallet Import Format), then generate the address.
183+
OPTIONS
184+
-mnemonic [string]
185+
Mnemonic words
123186
124187
-pass [string]
125188
Protect bip39 mnemonic with a passphrase via flag,
126189
or use environment variable,
127190
or you will be asked to enter at a prompt.
128191
129-
-n [int] (default = 1)
130-
Set number of keys to generate.
192+
-h or --help [bool]
193+
Report usage information and exit.
131194
132195
-v [bool]
133196
Print version tag and exit.

bin/json.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
6+
"github.com/gotamer/hdkeys"
7+
)
8+
9+
func GetAllJson(no uint32) (string, error) {
10+
Info.Println("no: ", no)
11+
type Keys struct {
12+
Nostr *hdkeys.Nostr
13+
Bitcoin *hdkeys.Bitcoin
14+
}
15+
16+
var out = new(Keys)
17+
var err error
18+
19+
out.Nostr, err = km.GetNostr(no)
20+
if err != nil {
21+
return "", err
22+
}
23+
24+
out.Bitcoin, err = hdkeys.GetBitcoinFromWif(out.Nostr.Wif)
25+
if err != nil {
26+
return "", err
27+
}
28+
29+
jsonbyte, err := json.Marshal(&out)
30+
if err != nil {
31+
return "", err
32+
}
33+
return string(jsonbyte), nil
34+
}
35+
36+
func GetNostrJson(no uint32) (string, error) {
37+
38+
nostr, err := km.GetNostr(no)
39+
if err != nil {
40+
return "", err
41+
}
42+
43+
jsonbyte, err := json.Marshal(&nostr)
44+
if err != nil {
45+
return "", err
46+
}
47+
return string(jsonbyte), nil
48+
}
49+
50+
func GetBitcoinFromWifJson(wif string) (string, error) {
51+
52+
btc, err := hdkeys.GetBitcoinFromWif(wif)
53+
if err != nil {
54+
return "", err
55+
}
56+
57+
jsonbyte, err := json.Marshal(&btc)
58+
if err != nil {
59+
return "", err
60+
}
61+
return string(jsonbyte), nil
62+
}

0 commit comments

Comments
 (0)