-
Notifications
You must be signed in to change notification settings - Fork 0
/
tezos-wallet.go
82 lines (70 loc) · 1.89 KB
/
tezos-wallet.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
package gokeygen
import (
"github.com/Messer4/base58check"
"github.com/pkg/errors"
"golang.org/x/crypto/blake2b"
"golang.org/x/crypto/ed25519"
)
var (
// maxBatchSize tells how many Transactions per batch are allowed.
maxBatchSize = 200
// For (de)constructing addresses
tz1 = []byte{6, 161, 159}
edsk = []byte{43, 246, 78, 7}
edsk2 = []byte{13, 15, 58, 7}
edpk = []byte{13, 15, 37, 217}
edesk = []byte{7, 90, 60, 179, 41}
)
//Wallet needed for signing operations
type TezosWallet struct {
Address string
Seed []byte
Kp keyPair
Sk string
Pk string
}
// Key Pair Storage
type keyPair struct {
PrivKey []byte
PubKey []byte
}
func generatePublicHash(publicKey []byte) (string, error) {
hash, err := blake2b.New(20, []byte{})
hash.Write(publicKey)
if err != nil {
return "", errors.Wrapf(err, "could not generate public hash from public key %s", string(publicKey))
}
return b58cencode(hash.Sum(nil), tz1), nil
}
// CreateWallet returns Wallet with the mnemonic and password provided
func CreateWallet(seed []byte) (TezosWallet, error) {
var seed32 = seed[0:32]
privKey := ed25519.NewKeyFromSeed(seed32)
pubKey := privKey.Public().(ed25519.PublicKey)
pubKeyBytes := []byte(pubKey)
signKp := keyPair{PrivKey: privKey, PubKey: pubKeyBytes}
address, err := generatePublicHash(pubKeyBytes)
if err != nil {
return TezosWallet{}, errors.Wrapf(err, "could not create wallet")
}
wallet := TezosWallet{
Address: address,
Kp: signKp,
Seed: seed,
Sk: b58cencode(privKey, edsk),
Pk: b58cencode(pubKeyBytes, edpk),
}
return wallet, nil
}
//Helper Function to get the right format for wallet.
func b58cencode(payload []byte, prefix []byte) string {
n := make([]byte, (len(prefix) + len(payload)))
for k := range prefix {
n[k] = prefix[k]
}
for l := range payload {
n[l+len(prefix)] = payload[l]
}
b58c := base58check.Encode(n)
return b58c
}