/
wallet.go
274 lines (235 loc) · 5.31 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package com
import (
"crypto/aes"
"crypto/cipher"
"crypto/ed25519"
"crypto/rand"
"crypto/sha512"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"github.com/btcsuite/btcutil/base58"
"golang.org/x/crypto/scrypt"
"io"
"os"
)
const (
WalletVersion = "2"
)
var (
WOpenErr = errors.New("open wallet failed")
WVerifyErr = errors.New("verify signature failed")
WInvalidSig = errors.New("invalid signature data")
)
type CipherData struct {
KeyParam `json:"param"`
Code string `json:"code"`
KeyCrypto string `json:"key_crypto"`
Salt string `json:"salt"`
PriCipher string `json:"pri_cipher"`
}
type Wallet struct {
NickName string `json:"nick_name"`
Version string `json:"version"`
Addr Address `json:"address"`
Cipher *CipherData `json:"cipher"`
priKey ed25519.PrivateKey
}
type KeyParam struct {
S int `json:"s"`
N int `json:"n"`
R int `json:"r"`
P int `json:"p"`
L int `json:"l"`
}
var KP = KeyParam{
S: 8,
N: 1 << 15,
R: 8,
P: 1,
L: 32,
}
func Decrypt(key []byte, cipherTxt []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
if len(cipherTxt) < aes.BlockSize {
return nil, fmt.Errorf("cipher text too short")
}
iv := cipherTxt[:aes.BlockSize]
cipherTxt = cipherTxt[aes.BlockSize:]
stream := cipher.NewCFBDecrypter(block, iv)
stream.XORKeyStream(cipherTxt, cipherTxt)
return cipherTxt, nil
}
func Encrypt(key []byte, plainTxt []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
cipherText := make([]byte, aes.BlockSize+len(plainTxt))
iv := cipherText[:aes.BlockSize]
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
return nil, err
}
stream := cipher.NewCFBEncrypter(block, iv)
stream.XORKeyStream(cipherText[aes.BlockSize:], plainTxt)
return cipherText, nil
}
func AESKey(salt []byte, password string) ([]byte, error) {
return scrypt.Key([]byte(password), salt, KP.N, KP.R, KP.P, KP.L)
}
func encryptPriKey(priKey ed25519.PrivateKey, auth string) (*CipherData, error) {
salt := make([]byte, KP.S)
_, err := rand.Read(salt)
if err != nil {
return nil, err
}
aesKey, err := AESKey(salt, auth)
if err != nil {
return nil, err
}
ci, err := Encrypt(aesKey, priKey[:])
if err != nil {
return nil, err
}
ciData := &CipherData{
KeyParam: KP,
Code: "base58",
KeyCrypto: "scrypt",
Salt: base58.Encode(salt),
PriCipher: base58.Encode(ci),
}
return ciData, nil // ,base58.Encode(ci)
}
func PrivateKeyToCurve25519(curve25519Private *[32]byte, privateKey *[64]byte) {
h := sha512.New()
h.Write(privateKey[:32])
digest := h.Sum(nil)
digest[0] &= 248
digest[31] &= 127
digest[31] |= 64
copy(curve25519Private[:], digest)
}
func decryptPriKey(ciData *CipherData, auth string) (ed25519.PrivateKey, error) {
salt := base58.Decode(ciData.Salt)
aesKey, err := AESKey(salt, auth)
if err != nil {
return nil, err
}
priBytes := base58.Decode(ciData.PriCipher)
return Decrypt(aesKey, priBytes)
}
func CreateWallet(auth, name string) (*Wallet, error) {
pub, pri, err := ed25519.GenerateKey(rand.Reader)
if err != nil {
return nil, err
}
cipherTxt, err := encryptPriKey(pri, auth)
if err != nil {
return nil, err
}
addr, _ := PubToAddr(pub)
sw := &Wallet{
NickName: name,
Version: WalletVersion,
Cipher: cipherTxt,
Addr: addr,
priKey: pri,
}
return sw, nil
}
func LoadByJsonData(jsonStr string) (*Wallet, error) {
w := new(Wallet)
if err := json.Unmarshal([]byte(jsonStr), w); err != nil {
return nil, err
}
return w, nil
}
func LoadByFile(path string) (*Wallet, error) {
bts, err := os.ReadFile(path)
if err != nil {
return nil, err
}
w := new(Wallet)
if err := json.Unmarshal(bts, w); err != nil {
return nil, err
}
return w, nil
}
func (sw *Wallet) Close() {
sw.priKey = nil
}
func (sw *Wallet) Open(auth string) error {
if sw.priKey != nil {
return nil
}
pri, err := decryptPriKey(sw.Cipher, auth)
if err != nil {
return err
}
pub := pri.Public().(ed25519.PublicKey)
addr, _ := PubToAddr(pub)
if addr != sw.Addr {
return WOpenErr
}
sw.priKey = pri
return nil
}
func (sw *Wallet) Address() Address {
return sw.Addr
}
func (sw *Wallet) Verbose() string {
bts, _ := json.MarshalIndent(sw, "", "\t")
return string(bts)
}
func (sw *Wallet) Sign(s *RawStamp) *StampSig {
rawBytes, _ := json.Marshal(s)
pub := sw.priKey.Public().(ed25519.PublicKey)
sig := ed25519.Sign(sw.priKey, rawBytes)
_, suffix := PubToAddr(pub)
return &StampSig{
SigData: hex.EncodeToString(sig),
PubSuffix: suffix,
}
}
func VerifyStamp(stamp Stamp) error {
var (
err error
data, sig, pubBts []byte
)
if stamp.Data == nil || stamp.Sig == nil {
return WInvalidSig
}
data, _ = json.Marshal(stamp.Data)
sig, err = hex.DecodeString(stamp.Sig.Data())
if err != nil {
return err
}
pubBts, err = RecoverPub(stamp.Data.WAddr, stamp.Sig.Suffix())
if err != nil {
return err
}
if len(pubBts) != ed25519.PublicKeySize {
return WVerifyErr
}
if !ed25519.Verify(pubBts, data, sig) {
return WVerifyErr
}
return nil
}
func (sw *Wallet) IsOpen() bool {
return sw.priKey != nil
}
func (sw *Wallet) String() string {
bs, _ := json.MarshalIndent(sw, "", "\t")
return string(bs)
}
func (sw *Wallet) Name() string {
return sw.NickName
}
func (sw *Wallet) SetName(newName string) {
sw.NickName = newName
}