/
wallet.go
185 lines (142 loc) · 3.87 KB
/
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
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
/*
Elliptic curve cryptography is used to generate public keys and Bitcoin addresses.
The public key can be generated by multiplying the private key and the generator point on the Elliptic curve.
K = k * G
G is the generator point on the Elliptic curve.
k is a private key in the form of a randomly generated number.
K is a public key.
How to generate Bitcoin address
Pulick Key --> SHA256 --> RIPEMD160 --> Base58Check Encoding --> Bitcoin Address
Base58Check Encoding
1. Payload
2. Version + Payload
3. (Version + Payload) --> SHA256 --> SHA256 --> First 4bytes --> Checksum
3. Version + Payload + Checksum
4. (Version + Payload + Checksum) --> Base58 encode
*/
package wallet
import (
"bytes"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/sha256"
"encoding/binary"
"encoding/gob"
"io/ioutil"
"log"
"os"
"github.com/mr-tron/base58"
"golang.org/x/crypto/ripemd160"
)
const (
checksumLength = 4
version = byte(0x80)
)
type Wallet struct {
PrivateKey *ecdsa.PrivateKey
PublicKey []byte
}
func NewWallet(path string) *Wallet {
w, err := LoadFile(path)
if err == nil {
return w
}
curve := elliptic.P256()
priKey, err := ecdsa.GenerateKey(curve, rand.Reader)
if err != nil {
log.Panic(err)
}
buf1 := make([]byte, 32)
buf2 := make([]byte, 32)
pubK := append(priKey.PublicKey.X.FillBytes(buf1), priKey.PublicKey.Y.FillBytes(buf2)...)
// pubK := append(priKey.PublicKey.X.Bytes(), priKey.PublicKey.Y.Bytes()...)
w = &Wallet{priKey, pubK}
saveFile(w, path)
return w
}
func getChecksum(payload []byte) []byte {
h1 := sha256.Sum256(payload)
h2 := sha256.Sum256(h1[:])
return h2[:checksumLength]
}
func HashPubKey(pubKey []byte) []byte {
publicSha256 := sha256.Sum256(pubKey)
ripemd160Hasher := ripemd160.New()
_, err := ripemd160Hasher.Write(publicSha256[:])
if err != nil {
log.Panic(err)
}
return ripemd160Hasher.Sum(nil)
}
func (w *Wallet) GetAddress() []byte {
payload := HashPubKey(w.PublicKey)
verPayload := append([]byte{version}, payload...)
checksum := getChecksum(verPayload)
fullPayload := append(verPayload, checksum...)
address := Base58Encode(fullPayload)
ich := binary.LittleEndian.Uint32(checksum)
log.Printf("checksum : %v", ich)
log.Printf("payload : %x", payload)
log.Printf("version : %d", version)
return address
}
func getPayload(addr []byte) (byte, []byte, []byte) {
fullPayload := Base58Decode(addr)
checksum := fullPayload[len(fullPayload)-checksumLength:]
version := fullPayload[0]
payload := fullPayload[1 : len(fullPayload)-checksumLength]
return version, payload, checksum
}
func ValidateAddress(addr []byte) bool {
version, payload, checksum := getPayload(addr)
targetChecksum := getChecksum(append([]byte{version}, payload...))
ich := binary.LittleEndian.Uint32(checksum)
log.Printf("checksum : %d", ich)
log.Printf("payload : %x", payload)
log.Printf("version : %d", version)
return bytes.Equal(checksum, targetChecksum)
}
func Base58Encode(input []byte) []byte {
encode := base58.Encode(input)
return []byte(encode)
}
func Base58Decode(input []byte) []byte {
decode, err := base58.Decode(string(input[:]))
if err != nil {
log.Panic(err)
}
return decode
}
func LoadFile(path string) (*Wallet, error) {
walletFile := path
if _, err := os.Stat(walletFile); os.IsNotExist(err) {
return nil, err
}
fileContent, err := ioutil.ReadFile(walletFile)
if err != nil {
return nil, err
}
var w Wallet
gob.Register(elliptic.P256())
decoder := gob.NewDecoder(bytes.NewReader(fileContent))
err = decoder.Decode(&w)
if err != nil {
return nil, err
}
return &w, nil
}
func saveFile(w *Wallet, path string) {
var content bytes.Buffer
walletFile := path
gob.Register(elliptic.P256())
encoder := gob.NewEncoder(&content)
err := encoder.Encode(w)
if err != nil {
log.Panic(err)
}
err = ioutil.WriteFile(walletFile, content.Bytes(), 0644)
if err != nil {
log.Panic(err)
}
}