-
Notifications
You must be signed in to change notification settings - Fork 27
/
address.go
326 lines (292 loc) · 7.76 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
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package cardano
import (
"errors"
"math/big"
"github.com/echovl/cardano-go/internal/bech32"
"github.com/echovl/cardano-go/internal/cbor"
"golang.org/x/crypto/blake2b"
)
type AddressType byte
const (
Base AddressType = 0x00
Ptr AddressType = 0x04
Enterprise AddressType = 0x06
)
// Address represents a Cardano address.
type Address struct {
Network Network
Type AddressType
Pointer Pointer
Payment StakeCredential
Stake StakeCredential
}
// NewAddress creates an Address from a bech32 encoded string.
func NewAddress(bech string) (Address, error) {
_, bytes, err := bech32.DecodeToBase256(bech)
if err != nil {
return Address{}, err
}
return NewAddressFromBytes(bytes)
}
// NewAddressFromBytes creates an Address from bytes.
func NewAddressFromBytes(bytes []byte) (Address, error) {
addr := Address{
Type: AddressType(bytes[0] >> 4),
Network: Network(bytes[0] & 0x01),
}
switch addr.Type {
case Base:
if len(bytes) != 57 {
return addr, errors.New("base address length should be 29")
}
addr.Payment = StakeCredential{
Type: KeyCredential,
KeyHash: bytes[1:29],
}
addr.Stake = StakeCredential{
Type: KeyCredential,
KeyHash: bytes[29:57],
}
case Base + 1:
if len(bytes) != 57 {
return addr, errors.New("base address length should be 29")
}
addr.Payment = StakeCredential{
Type: ScriptCredential,
ScriptHash: bytes[1:29],
}
addr.Stake = StakeCredential{
Type: KeyCredential,
KeyHash: bytes[29:57],
}
case Base + 2:
if len(bytes) != 57 {
return addr, errors.New("base address length should be 29")
}
addr.Payment = StakeCredential{
Type: KeyCredential,
KeyHash: bytes[1:29],
}
addr.Stake = StakeCredential{
Type: ScriptCredential,
ScriptHash: bytes[29:57],
}
case Base + 3:
if len(bytes) != 57 {
return addr, errors.New("base address length should be 29")
}
addr.Payment = StakeCredential{
Type: ScriptCredential,
ScriptHash: bytes[1:29],
}
addr.Stake = StakeCredential{
Type: ScriptCredential,
ScriptHash: bytes[29:57],
}
case Ptr:
if len(bytes) <= 29 {
return addr, errors.New("enterprise address length should be greater than 29")
}
index := uint(29)
slot, sn, err := decodeFromNat(bytes[29:])
if err != nil {
return addr, err
}
index += sn
txIndex, tn, err := decodeFromNat(bytes[index:])
if err != nil {
return addr, err
}
index += tn
certIndex, _, err := decodeFromNat(bytes[index:])
if err != nil {
return addr, err
}
addr.Payment = StakeCredential{
Type: KeyCredential,
KeyHash: bytes[1:29],
}
addr.Pointer = Pointer{Slot: slot, TxIndex: txIndex, CertIndex: certIndex}
case Ptr + 1:
if len(bytes) <= 29 {
return addr, errors.New("enterprise address length should be greater than 29")
}
index := uint(29)
slot, sn, err := decodeFromNat(bytes[29:])
if err != nil {
return addr, err
}
index += sn
txIndex, tn, err := decodeFromNat(bytes[index:])
if err != nil {
return addr, err
}
index += tn
certIndex, _, err := decodeFromNat(bytes[index:])
if err != nil {
return addr, err
}
addr.Payment = StakeCredential{
Type: ScriptCredential,
ScriptHash: bytes[1:29],
}
addr.Pointer = Pointer{Slot: slot, TxIndex: txIndex, CertIndex: certIndex}
case Enterprise:
if len(bytes) != 29 {
return addr, errors.New("enterprise address length should be 29")
}
addr.Payment = StakeCredential{
Type: KeyCredential,
KeyHash: bytes[1:29],
}
case Enterprise + 1:
if len(bytes) != 29 {
return addr, errors.New("enterprise address length should be 29")
}
addr.Payment = StakeCredential{
Type: ScriptCredential,
ScriptHash: bytes[1:29],
}
}
return addr, nil
}
// MarshalCBOR implements cbor.Marshaler.
func (addr *Address) MarshalCBOR() ([]byte, error) {
em, _ := cbor.CanonicalEncOptions().EncMode()
return em.Marshal(addr.Bytes())
}
// UnmarshalCBOR implements cbor.Unmarshaler.
func (addr *Address) UnmarshalCBOR(data []byte) error {
bytes := []byte{}
if err := cborDec.Unmarshal(data, &bytes); err != nil {
return nil
}
decoded, err := NewAddressFromBytes(bytes)
if err != nil {
return err
}
addr.Network = decoded.Network
addr.Type = decoded.Type
addr.Payment = decoded.Payment
addr.Stake = decoded.Stake
addr.Pointer = decoded.Pointer
return nil
}
// Bytes returns the CBOR encoding of the Address as bytes.
func (addr *Address) Bytes() []byte {
var networkByte uint8
switch addr.Network {
case Testnet, Preprod:
networkByte = 0
case Mainnet:
networkByte = 1
}
addrBytes := []byte{byte(addr.Type<<4) | (networkByte & 0xFF)}
switch addr.Type {
case Base, Base + 1, Base + 2, Base + 3:
addrBytes = append(addrBytes, addr.Payment.Hash()...)
addrBytes = append(addrBytes, addr.Stake.Hash()...)
case Enterprise, Enterprise + 1:
addrBytes = append(addrBytes, addr.Payment.Hash()...)
case Ptr, Ptr + 1:
addrBytes = append(addrBytes, addr.Payment.Hash()...)
addrBytes = append(addrBytes, encodeToNat(addr.Pointer.Slot)...)
addrBytes = append(addrBytes, encodeToNat(addr.Pointer.TxIndex)...)
addrBytes = append(addrBytes, encodeToNat(addr.Pointer.CertIndex)...)
}
return addrBytes
}
// Bech32 returns the Address encoded as bech32.
func (addr *Address) Bech32() string {
addrStr, err := bech32.EncodeFromBase256(getHrp(addr.Network), addr.Bytes())
if err != nil {
panic(err)
}
return addrStr
}
// String returns the Address encoded as bech32.
func (addr Address) String() string {
return addr.Bech32()
}
// NewBaseAddress returns a new Base Address.
func NewBaseAddress(network Network, payment StakeCredential, stake StakeCredential) (Address, error) {
addrType := Base
if payment.Type == ScriptCredential && stake.Type == KeyCredential {
addrType = Base + 1
} else if payment.Type == KeyCredential && stake.Type == ScriptCredential {
addrType = Base + 2
} else if payment.Type == ScriptCredential && stake.Type == ScriptCredential {
addrType = Base + 3
}
return Address{Type: addrType, Network: network, Payment: payment, Stake: stake}, nil
}
// NewEnterpriseAddress returns a new Enterprise Address.
func NewEnterpriseAddress(network Network, payment StakeCredential) (Address, error) {
addrType := Enterprise
if payment.Type == ScriptCredential {
addrType = Enterprise + 1
}
return Address{Type: addrType, Network: network, Payment: payment}, nil
}
// Pointer is the location of the Stake Registration Certificate in the blockchain.
type Pointer struct {
Slot uint64
TxIndex uint64
CertIndex uint64
}
// NewPointerAddress returns a new Pointer Address.
func NewPointerAddress(network Network, payment StakeCredential, ptr Pointer) (Address, error) {
addrType := Ptr
if payment.Type == ScriptCredential {
addrType = Ptr + 1
}
return Address{Type: addrType, Network: network, Payment: payment, Pointer: ptr}, nil
}
func decodeFromNat(data []byte) (uint64, uint, error) {
out := big.NewInt(0)
n := uint(0)
for _, b := range data {
out.Lsh(out, 7)
out.Or(out, big.NewInt(int64(b&0x7F)))
if !out.IsUint64() {
return 0, 0, errors.New("too big to decode (> math.MaxUint64)")
}
n += 1
if b&0x80 == 0 {
return out.Uint64(), n, nil
}
}
return 0, 0, errors.New("bad nat encoding")
}
func encodeToNat(n uint64) []byte {
out := []byte{byte(n) & 0x7F}
n >>= 7
for n != 0 {
out = append(out, byte(n)|0x80)
n >>= 7
}
// reverse
for i, j := 0, len(out)-1; i < j; i, j = i+1, j-1 {
out[i], out[j] = out[j], out[i]
}
return out
}
func Blake224Hash(b []byte) ([]byte, error) {
hash, err := blake2b.New(224/8, nil)
if err != nil {
return nil, err
}
_, err = hash.Write(b)
if err != nil {
return nil, err
}
return hash.Sum(nil), err
}
func getHrp(network Network) string {
switch network {
case Testnet, Preprod:
return "addr_test"
default:
return "addr"
}
}