/
main.go
85 lines (68 loc) · 1.66 KB
/
main.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 (
"flag"
"fmt"
"log"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcutil"
"github.com/opentradingnetworkfoundation/otn-go/crypto"
)
var prefix string
type Keypair struct {
pvt *btcec.PrivateKey
prefix string
}
func (k *Keypair) GetPublicKeyString() string {
return crypto.GetPublicKeyString(k.prefix, k.pvt.PubKey())
}
func (k *Keypair) GetAddressString() string {
return crypto.GetAddressString(k.prefix, k.pvt.PubKey())
}
func (k *Keypair) GetPrivateKeyString() string {
wif, err := btcutil.NewWIF(k.pvt, &chaincfg.MainNetParams, false)
if err != nil {
log.Fatal("Failed to create WIF:", err)
}
return wif.String()
}
func NewKeypair(prefix string) (*Keypair, error) {
pvt, err := btcec.NewPrivateKey(btcec.S256())
if err != nil {
return nil, err
}
return &Keypair{pvt, prefix}, nil
}
func generateKeys(count int) {
for i := 0; i < count; i++ {
kp, err := NewKeypair(prefix)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s\t%s\t%s\n",
kp.GetAddressString(),
kp.GetPublicKeyString(),
kp.GetPrivateKeyString())
}
}
func decodeKey(key string) {
wif, err := btcutil.DecodeWIF(key)
if err != nil {
log.Fatal(err)
}
pubKey := wif.PrivKey.PubKey()
pubstr := crypto.GetPublicKeyString(prefix, pubKey)
address := crypto.GetAddressString(prefix, pubKey)
fmt.Println(pubstr, address)
}
func main() {
flag.StringVar(&prefix, "p", "OTN", "Address prefix")
count := flag.Int("n", 1, "Number of items to generate")
parseKey := flag.String("parse", "", "Parse private key")
flag.Parse()
if len(*parseKey) > 0 {
decodeKey(*parseKey)
return
}
generateKeys(*count)
}