-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathkey.go
103 lines (80 loc) · 3.12 KB
/
key.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package vanity
// Key consists of a ed25519 256-bit SpendKey and a ViewKey.
// In order to make the mnemonic seeds work, ViewKey is actually derived from
// SpendKey with Keccak256, also leading to SpendKey serving as the seed.
type Key struct {
SpendKey, ViewKey *[32]byte
}
func (k *Key) PublicSpendKey() *[32]byte {
return publicKeyFromPrivateKey(k.SpendKey)
}
func (k *Key) PublicViewKey() *[32]byte {
return publicKeyFromPrivateKey(k.ViewKey)
}
// Add adds k2 to k1 in ellipse curve sense (scalar add), returning the sum.
func (k1 *Key) Add(k2 *Key) *Key {
sum := &Key{new([32]byte), new([32]byte)}
scAdd(sum.SpendKey, k1.SpendKey, k2.SpendKey)
scAdd(sum.ViewKey, k1.ViewKey, k2.ViewKey)
return sum
}
// HalfToFull transfroms a Key construsted by HalfKeyFromSeed to a full key.
// Basically, it triggers the derivation of its view key from spend key.
func (k *Key) HalfToFull() {
k.ViewKey = keccak256(k.SpendKey[:])
scReduce32(k.ViewKey)
}
// HalfAddress encodes network and spend key only. This should only be used for
// vanity.
func (k *Key) HalfAddress(network Network) string {
spendPub := k.PublicSpendKey()
address := encodeBase58(network, spendPub[:])
return address
}
// Address encodes network, public spend key and view key in base58 format.
func (k *Key) Address(network Network) string {
spendPub := k.PublicSpendKey()
viewPub := k.PublicViewKey()
hash := keccak256(network, spendPub[:], viewPub[:])
address := encodeBase58(network, spendPub[:], viewPub[:], hash[:4])
return address
}
// AddressWithAdditionalPublicKey adds extra spendPub and viewPub to k in an
// ellipse curve sense (point add), returning the base58 encoded address of the sum.
func (k *Key) AddressWithAdditionalPublicKey(network Network, spendPub, viewPub *[32]byte) string {
finalSpendPub, finalViewPub := new([32]byte), new([32]byte)
pointAdd(finalSpendPub, k.PublicSpendKey(), spendPub)
pointAdd(finalViewPub, k.PublicViewKey(), viewPub)
hash := keccak256(network, finalSpendPub[:], finalViewPub[:])
address := encodeBase58(network, finalSpendPub[:], finalViewPub[:], hash[:4])
return address
}
// HalfAddressWithAdditionalPublicKey adds extra spendPub to k in an
// ellipse curve sense (point add), returning the base58 encoded address of the sum.
func (k *Key) HalfAddressWithAdditionalPublicKey(network Network, spendPub *[32]byte) string {
finalSpendPub := new([32]byte)
pointAdd(finalSpendPub, k.PublicSpendKey(), spendPub)
address := encodeBase58(network, finalSpendPub[:])
return address
}
func (k *Key) Seed() *[32]byte {
return k.SpendKey
}
// KeyFromSeed construsts a Key with given seed.
func KeyFromSeed(seed *[32]byte) *Key {
k := &Key{new([32]byte), new([32]byte)}
copy(k.SpendKey[:], seed[:])
scReduce32(k.SpendKey)
k.ViewKey = keccak256(k.SpendKey[:])
scReduce32(k.ViewKey)
return k
}
// HalfKeyFromSeed construsts a half size of Key (i.e. only spend key) with
// given seed. This should only be used for vanity address generation, which it
// boosts up the generation speed.
func HalfKeyFromSeed(seed *[32]byte) *Key {
k := &Key{new([32]byte), new([32]byte)}
copy(k.SpendKey[:], seed[:])
scReduce32(k.SpendKey)
return k
}