-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate.go
85 lines (58 loc) · 1.3 KB
/
generate.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 (
"encoding/hex"
"fmt"
nkcli "github.com/mdzz-club/nkcli/internal"
"github.com/nbd-wtf/go-nostr"
"github.com/nbd-wtf/go-nostr/nip06"
"github.com/nbd-wtf/go-nostr/nip19"
"github.com/tyler-smith/go-bip39"
"github.com/urfave/cli/v2"
"golang.org/x/crypto/ssh/terminal"
)
func generateAction(c *cli.Context) error {
fmt.Print("Here's your mnemonic words:\n\n")
ent, err := bip39.NewEntropy(128)
if err != nil {
return err
}
words, err := bip39.NewMnemonic(ent)
if err != nil {
return err
}
fmt.Printf("%v\n\n", words)
priv, err := nip06.PrivateKeyFromSeed(nip06.SeedFromWords(words))
if err != nil {
return err
}
pub, err := nostr.GetPublicKey(priv)
if err != nil {
return err
}
fmt.Print("Enter a passphrase to protect your key:")
password, err := terminal.ReadPassword(0)
if err != nil {
return err
}
keyBuf, err := hex.DecodeString(priv)
if err != nil {
return err
}
encKey, err := nkcli.Encrypt(keyBuf, password)
dbpath := c.String("db")
db, err := nkcli.Open(dbpath)
if err != nil {
return err
}
defer db.Close()
err = db.SaveKey(pub, encKey)
if err != nil {
return err
}
bech32Pub, err := nip19.EncodePublicKey(pub)
if err != nil {
return err
}
fmt.Printf("\n\nYour public key:\n%v\n%v\n", pub, bech32Pub)
return nil
}