/
private_key.go
193 lines (154 loc) · 4.57 KB
/
private_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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package neo
import (
"bytes"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"errors"
"fmt"
"math/big"
"github.com/CityOfZion/neo-go-sdk/utility"
"golang.org/x/crypto/ripemd160"
)
type (
// PrivateKey is a struct which holds the value of a NEO address private key, and has
// a number of utility functions attached to the struct.
PrivateKey struct {
bytes []byte
}
)
// NewPrivateKeyFromWIF creates a PrivateKey struct using a WIF.
func NewPrivateKeyFromWIF(wif string) (*PrivateKey, error) {
base58 := utility.NewBase58()
decodedWIF, err := base58.Decode(wif)
if err != nil {
return nil, err
}
if len(decodedWIF) != 38 {
return nil, fmt.Errorf(
"Expected length of decoded WIF to be 38, got: %d", len(decodedWIF),
)
}
if decodedWIF[0] != 0x80 {
return nil, fmt.Errorf(
"Expected first byte of decoded WIF to be '0x80', got: %x", decodedWIF[0],
)
}
if decodedWIF[33] != 0x01 {
return nil, fmt.Errorf(
"Expected 34th byte of decoded WIF to be '0x01', got: %x", decodedWIF[33],
)
}
subString := decodedWIF[:len(decodedWIF)-4]
rawFirstSHA := sha256.Sum256([]byte(subString))
firstSHA := rawFirstSHA[:]
rawSecondSHA := sha256.Sum256(firstSHA)
secondSHA := rawSecondSHA[:]
firstFourBytes := secondSHA[:4]
lastFourBytes := decodedWIF[len(decodedWIF)-4 : len(decodedWIF)]
if !bytes.Equal(firstFourBytes, lastFourBytes) {
return nil, fmt.Errorf("WIF failed checksum validation")
}
return &PrivateKey{
bytes: decodedWIF[1:33],
}, nil
}
// PublicAddress derives the public NEO address that is coupled with the private key, and
// returns it as a string.
func (p PrivateKey) PublicAddress() (string, error) {
bytes, err := p.Signature()
if err != nil {
return "", err
}
var ver uint8 = 0x17
bytes = append([]byte{ver}, bytes...)
sha256H := sha256.New()
sha256H.Reset()
sha256H.Write(bytes)
hashThree := sha256H.Sum(nil)
sha256H.Reset()
sha256H.Write(hashThree)
hashFour := sha256H.Sum(nil)
bytes = append(bytes, hashFour[0:4]...)
base58 := utility.NewBase58()
address := base58.Encode(bytes)
return address, nil
}
// PublicKey derives the public key that is coupled with the private key, and returns it
// in an array of bytes.
func (p PrivateKey) PublicKey() ([]byte, error) {
ellipticCurve := p.createEllipticCurve()
bytesInt := new(big.Int).SetBytes(p.bytes)
point, err := ellipticCurve.ScalarBaseMult(bytesInt)
if err != nil {
return nil, err
}
if !ellipticCurve.IsOnCurve(*point) {
return nil, errors.New("failed to derive public key using elliptic curve")
}
pointXBytes := point.X.Bytes()
paddedPointXBytes := append(
bytes.Repeat(
[]byte{0x00},
32-len(pointXBytes),
),
pointXBytes...,
)
var bytes []byte
if point.Y.Bit(0) == 0 {
bytes = append([]byte{0x02}, paddedPointXBytes...)
} else {
bytes = append([]byte{0x03}, paddedPointXBytes...)
}
return bytes, nil
}
// Output converts the 32-byte slice representation of the private key to a string.
func (p PrivateKey) Output() string {
return hex.EncodeToString(p.bytes)
}
// OutputBase64 converts the 32-byte slice representation of the private key to a base64
// encoded string.
func (p PrivateKey) OutputBase64() string {
return base64.StdEncoding.EncodeToString(p.bytes)
}
// Signature creates the signature using the private key.
func (p PrivateKey) Signature() ([]byte, error) {
bytes, err := p.PublicKey()
if err != nil {
return nil, err
}
bytes = append([]byte{0x21}, bytes...)
bytes = append(bytes, 0xAC)
sha256H := sha256.New()
sha256H.Reset()
sha256H.Write(bytes)
hashOne := sha256H.Sum(nil)
ripemd160H := ripemd160.New()
ripemd160H.Reset()
ripemd160H.Write(hashOne)
hashTwo := ripemd160H.Sum(nil)
return hashTwo, nil
}
func (p PrivateKey) createEllipticCurve() utility.EllipticCurve {
var ellipticCurve utility.EllipticCurve
ellipticCurve.P, _ = new(big.Int).SetString(
"FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF", 16,
)
ellipticCurve.A, _ = new(big.Int).SetString(
"FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC", 16,
)
ellipticCurve.B, _ = new(big.Int).SetString(
"5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B", 16,
)
ellipticCurve.G.X, _ = new(big.Int).SetString(
"6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296", 16,
)
ellipticCurve.G.Y, _ = new(big.Int).SetString(
"4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5", 16,
)
ellipticCurve.N, _ = new(big.Int).SetString(
"FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551", 16,
)
ellipticCurve.H, _ = new(big.Int).SetString("01", 16)
return ellipticCurve
}