-
Notifications
You must be signed in to change notification settings - Fork 13
/
account.go
302 lines (257 loc) · 7.36 KB
/
account.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
// Copyright (c) 2014-2017 Bitmark Inc.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package account
import (
"bytes"
"github.com/bitmark-inc/bitmarkd/fault"
"github.com/bitmark-inc/bitmarkd/util"
"golang.org/x/crypto/ed25519"
"golang.org/x/crypto/sha3"
)
// enumeration of supported key algorithms
const (
// list of valid algorithms
Nothing = iota // zero keytype **Just for Testing**
ED25519 = iota
// end of list (one greater than last item)
algorithmLimit = iota
)
// miscellaneous constants
const (
checksumLength = 4
// bits in key code starting from LSB
publicKeyCode = 0x01
testKeyCode = 0x02
spare1KeyCode = 0x04
spare2KeyCode = 0x08
algorithmShift = 4 // shift 4 bits to get algorithm
)
// base type for accounts
type Account struct {
AccountInterface
}
type AccountInterface interface {
KeyType() int
PublicKeyBytes() []byte
CheckSignature(message []byte, signature Signature) error
Bytes() []byte
String() string
MarshalText() ([]byte, error)
IsTesting() bool
}
// for ed25519 signatures
type ED25519Account struct {
Test bool
PublicKey []byte
}
// just for debugging
type NothingAccount struct {
Test bool
PublicKey []byte
}
// this converts a Base58 encoded string and returns an account
//
// one of the specific account types are returned using the base "AccountInterface"
// interface type to allow individual methods to be called.
func AccountFromBase58(accountBase58Encoded string) (*Account, error) {
// Decode the account
accountDecoded := util.FromBase58(accountBase58Encoded)
if 0 == len(accountDecoded) {
return nil, fault.ErrCannotDecodeAccount
}
// Parse the key variant
keyVariant, keyVariantLength := util.FromVarint64(accountDecoded)
// Check key type
if 0 == keyVariantLength || keyVariant&publicKeyCode != publicKeyCode {
return nil, fault.ErrNotPublicKey
}
// compute algorithm
keyAlgorithm := keyVariant >> algorithmShift
if keyAlgorithm < 0 || keyAlgorithm >= algorithmLimit {
return nil, fault.ErrInvalidKeyType
}
// network selection
isTest := 0 != keyVariant&testKeyCode
// Compute key length
keyLength := len(accountDecoded) - keyVariantLength - checksumLength
if keyLength <= 0 {
return nil, fault.ErrInvalidKeyLength
}
// Checksum
checksumStart := len(accountDecoded) - checksumLength
checksum := sha3.Sum256(accountDecoded[:checksumStart])
if !bytes.Equal(checksum[:checksumLength], accountDecoded[checksumStart:]) {
return nil, fault.ErrChecksumMismatch
}
// return a pointer to the specific account type
switch keyAlgorithm {
case ED25519:
if keyLength != ed25519.PublicKeySize {
return nil, fault.ErrInvalidKeyLength
}
publicKey := accountDecoded[keyVariantLength:checksumStart]
account := &Account{
AccountInterface: &ED25519Account{
Test: isTest,
PublicKey: publicKey,
},
}
return account, nil
case Nothing:
if 2 != keyLength {
return nil, fault.ErrInvalidKeyLength
}
publicKey := accountDecoded[keyVariantLength:checksumStart]
account := &Account{
AccountInterface: &NothingAccount{
Test: isTest,
PublicKey: publicKey,
},
}
return account, nil
default:
return nil, fault.ErrInvalidKeyType
}
}
// this converts a byte encoded buffer and returns an account
//
// one of the specific account types are returned using the base "AccountInterface"
// interface type to allow individual methods to be called.
func AccountFromBytes(accountBytes []byte) (*Account, error) {
// Parse the key variant
keyVariant, keyVariantLength := util.FromVarint64(accountBytes)
// Check key type
if 0 == keyVariantLength || keyVariant&publicKeyCode != publicKeyCode {
return nil, fault.ErrNotPublicKey
}
// compute algorithm
keyAlgorithm := keyVariant >> algorithmShift
if keyAlgorithm < 0 || keyAlgorithm >= algorithmLimit {
return nil, fault.ErrInvalidKeyType
}
// network selection
isTest := 0 != keyVariant&testKeyCode
// Compute key length
keyLength := len(accountBytes) - keyVariantLength
if keyLength <= 0 {
return nil, fault.ErrInvalidKeyLength
}
// return a pointer to the specific account type
switch keyAlgorithm {
case ED25519:
if keyLength != ed25519.PublicKeySize {
return nil, fault.ErrInvalidKeyLength
}
publicKey := accountBytes[keyVariantLength:]
account := &Account{
AccountInterface: &ED25519Account{
Test: isTest,
PublicKey: publicKey,
},
}
return account, nil
case Nothing:
if 2 != keyLength {
return nil, fault.ErrInvalidKeyLength
}
publicKey := accountBytes[keyVariantLength:]
account := &Account{
AccountInterface: &NothingAccount{
Test: isTest,
PublicKey: publicKey,
},
}
return account, nil
default:
return nil, fault.ErrInvalidKeyType
}
}
func (account *Account) UnmarshalText(s []byte) error {
a, err := AccountFromBase58(string(s))
if nil != err {
return err
}
account.AccountInterface = a.AccountInterface
return nil
}
// ED25519
// -------
// key type code (see enumeration above)
func (account *ED25519Account) KeyType() int {
return ED25519
}
// fetch the public key as byte slice
func (account *ED25519Account) PublicKeyBytes() []byte {
return account.PublicKey[:]
}
// check the signature of a message
func (account *ED25519Account) CheckSignature(message []byte, signature Signature) error {
if ed25519.SignatureSize != len(signature) {
return fault.ErrInvalidSignature
}
if !ed25519.Verify(account.PublicKey[:], message, signature) {
return fault.ErrInvalidSignature
}
return nil
}
// byte slice for encoded key
func (account *ED25519Account) Bytes() []byte {
keyVariant := byte(ED25519<<algorithmShift) | publicKeyCode
if account.Test {
keyVariant |= testKeyCode
}
return append([]byte{keyVariant}, account.PublicKey[:]...)
}
// base58 encoding of encoded key
func (account *ED25519Account) String() string {
buffer := account.Bytes()
checksum := sha3.Sum256(buffer)
buffer = append(buffer, checksum[:checksumLength]...)
return util.ToBase58(buffer)
}
// convert an account to its Base58 JSON form
func (account ED25519Account) MarshalText() ([]byte, error) {
return []byte(account.String()), nil
}
// return whether the public key is in test mode or not
func (account ED25519Account) IsTesting() bool {
return account.Test
}
// Nothing
// -------
// key type code (see enumeration above)
func (account *NothingAccount) KeyType() int {
return Nothing
}
// fetch the public key as byte slice
func (account *NothingAccount) PublicKeyBytes() []byte {
return account.PublicKey[:]
}
// check the signature of a message
func (account *NothingAccount) CheckSignature(message []byte, signature Signature) error {
return fault.ErrInvalidSignature
}
// byte slice for encoded key
func (account *NothingAccount) Bytes() []byte {
keyVariant := byte(Nothing<<algorithmShift) | publicKeyCode
if account.Test {
keyVariant |= testKeyCode
}
return append([]byte{keyVariant}, account.PublicKey[:]...)
}
// base58 encoding of encoded key
func (account *NothingAccount) String() string {
buffer := account.Bytes()
checksum := sha3.Sum256(buffer)
buffer = append(buffer, checksum[:checksumLength]...)
return util.ToBase58(buffer)
}
// convert an account to its Base58 JSON form
func (account NothingAccount) MarshalText() ([]byte, error) {
return []byte(account.String()), nil
}
// return whether the public key is in test mode or not
func (account NothingAccount) IsTesting() bool {
return account.Test
}