-
Notifications
You must be signed in to change notification settings - Fork 14
/
address.go
260 lines (211 loc) · 6.51 KB
/
address.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
package types
import (
"bytes"
"crypto/rand"
"encoding/base32"
"encoding/binary"
"encoding/hex"
"errors"
"fmt"
"strings"
"github.com/tinylib/msgp/msgp"
"golang.org/x/crypto/blake2b"
"github.com/qlcchain/go-qlc/common/util"
"github.com/qlcchain/go-qlc/crypto/ed25519"
)
func init() {
msgp.RegisterExtension(AddressExtensionType, func() msgp.Extension { return new(Address) })
}
const (
// AddressPrefix is the prefix of qlc addresses.
AddressPrefix = "qlc_"
// AddressSize represents the binary size of a qlc address (a public key).
AddressSize = ed25519.PublicKeySize
addressChecksumSize = 5
addressPrefixLen = len(AddressPrefix)
// AddressLen represents the string length of a qlc address.
AddressLen = 60
// The following 52 characters form the address, and the final
// 8 are a checksum.
hexAddressLength = addressPrefixLen + AddressLen
// custom alphabet for base32 encoding
addressEncodingAlphabet = "13456789abcdefghijkmnopqrstuwxyz"
)
var (
// ZeroAddress
ZeroAddress = Address{}
// AddressEncoding is a base32 encoding using addressEncodingAlphabet as its
// alphabet.
AddressEncoding = base32.NewEncoding(addressEncodingAlphabet)
errAddressLen = errors.New("bad address length")
errAddressPrefix = errors.New("bad address prefix")
errAddressEncoding = errors.New("bad address encoding")
errAddressChecksum = errors.New("bad address checksum")
)
//Address of account
//go:generate msgp
type Address [AddressSize]byte
//BytesToAddress convert byte array to Address
func BytesToAddress(b []byte) (Address, error) {
var a Address
err := a.SetBytes(b)
return a, err
}
// HexToAddress convert hex address string to Address
func HexToAddress(hexStr string) (Address, error) {
s := util.TrimQuotes(hexStr)
if len(s) != hexAddressLength {
return Address{}, errAddressLen
}
if !strings.HasPrefix(s, AddressPrefix) {
return Address{}, errAddressPrefix
}
addr := s[addressPrefixLen:]
key, err := AddressEncoding.DecodeString("1111" + addr[0:52])
if err != nil {
return Address{}, errAddressEncoding
}
checksum, err := AddressEncoding.DecodeString(addr[52:])
if err != nil {
return Address{}, errAddressEncoding
}
// strip off upper 24 bits (3 bytes). 20 padding was added by us,
// 4 is unused as account is 256 bits.
var address Address
copy(address[:], key[3:])
if !bytes.Equal(address.Checksum(), checksum) {
return Address{}, errAddressChecksum
}
return address, nil
}
// IsValidHexAddress check Hex address string is valid
func IsValidHexAddress(hexStr string) bool {
_, err := HexToAddress(hexStr)
return err == nil
}
// PubToAddress convert ed25519.PublicKey to Address
func PubToAddress(pub ed25519.PublicKey) Address {
// Public key is 256bits, base32 must be multiple of 5 bits
// to encode properly.
addr, _ := BytesToAddress(pub)
return addr
}
// GenerateAddress generate qlc address
func GenerateAddress() (Address, ed25519.PrivateKey, error) {
pub, pri, err := ed25519.GenerateKey(rand.Reader)
return PubToAddress(pub), pri, err
}
// KeypairFromPrivateKey generate key pair from private key
func KeypairFromPrivateKey(privateKey string) (ed25519.PublicKey, ed25519.PrivateKey) {
privateBytes, _ := hex.DecodeString(privateKey)
pub, priv, _ := ed25519.GenerateKey(bytes.NewReader(privateBytes))
return pub, priv
}
// KeypairFromSeed generate key pair from seed
func KeypairFromSeed(seed string, index uint32) (ed25519.PublicKey, ed25519.PrivateKey, error) {
hash, err := blake2b.New(32, nil)
if err != nil {
return ed25519.PublicKey{}, ed25519.PrivateKey{}, errors.New("unable to create hash")
}
seedData, err := hex.DecodeString(seed)
if err != nil {
return ed25519.PublicKey{}, ed25519.PrivateKey{}, errors.New("invalid seed")
}
bs := make([]byte, 4)
binary.BigEndian.PutUint32(bs, index)
hash.Write(seedData)
hash.Write(bs)
seedBytes := hash.Sum(nil)
pub, priv, err := ed25519.GenerateKey(bytes.NewReader(seedBytes))
if err != nil {
return ed25519.PublicKey{}, ed25519.PrivateKey{}, errors.New("unable to generate ed25519 key")
}
return pub, priv, nil
}
// SetBytes new address bytes
func (addr *Address) SetBytes(b []byte) error {
if length := len(b); length != AddressSize {
return fmt.Errorf("error address size %v", length)
}
copy(addr[:], b)
return nil
}
//Bytes get Address byte array
func (addr Address) Bytes() []byte { return addr[:] }
// Checksum calculates the checksum for this address' public key.
func (addr Address) Checksum() []byte {
hash, err := blake2b.New(addressChecksumSize, nil)
if err != nil {
panic(err)
}
hash.Write(addr[:])
return util.ReverseBytes(hash.Sum(nil))
}
// IsZero check address is zero
func (addr Address) IsZero() bool {
for _, b := range addr {
if b != 0 {
return false
}
}
return true
}
// String implements the fmt.Stringer interface.
func (addr Address) String() string {
key := append([]byte{0, 0, 0}, addr[:]...)
encodedKey := AddressEncoding.EncodeToString(key)[4:]
encodedChecksum := AddressEncoding.EncodeToString(addr.Checksum())
var buf bytes.Buffer
buf.WriteString(AddressPrefix)
buf.WriteString(encodedKey)
buf.WriteString(encodedChecksum)
return buf.String()
}
// Verify reports whether the given signature is valid for the given data.
func (addr Address) Verify(data []byte, signature []byte) bool {
return ed25519.Verify(addr[:], data, signature)
}
//ExtensionType implements Extension.ExtensionType interface
func (addr *Address) ExtensionType() int8 {
return AddressExtensionType
}
//ExtensionType implements Extension.Len interface
func (addr *Address) Len() int {
return AddressSize
}
//ExtensionType implements Extension.MarshalBinaryTo interface
func (addr Address) MarshalBinaryTo(text []byte) error {
copy(text, addr[:])
return nil
}
//ExtensionType implements Extension.UnmarshalBinary interface
func (addr *Address) UnmarshalBinary(text []byte) error {
size := len(text)
if len(text) != AddressSize {
return fmt.Errorf("bad address size: %d", size)
}
copy((*addr)[:], text)
return nil
}
//UnmarshalText implements encoding.TextUnmarshaler
func (addr *Address) UnmarshalText(text []byte) error {
tmp, err := HexToAddress(string(text))
if err != nil {
return err
}
copy((*addr)[:], tmp[:])
return nil
}
//MarshalText implements encoding.Textmarshaler
func (addr Address) MarshalText() (text []byte, err error) {
return []byte(addr.String()), nil
}
func (addr *Address) ToHash() Hash {
return Hash(*addr)
}
func (addr Address) Serialize() ([]byte, error) {
return addr[:], nil
}
func (addr *Address) Deserialize(text []byte) error {
return addr.UnmarshalBinary(text)
}