forked from renproject/multichain
/
address.go
293 lines (253 loc) · 9.22 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
package zcash
import (
"bytes"
"crypto/sha256"
"fmt"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcutil"
"github.com/btcsuite/btcutil/base58"
"github.com/renproject/multichain/api/address"
"github.com/renproject/pack"
"golang.org/x/crypto/ripemd160"
)
// AddressEncodeDecoder implements the address.EncodeDecoder interface
type AddressEncodeDecoder struct {
AddressEncoder
AddressDecoder
}
// AddressEncoder encapsulates the chain specific configurations and implements
// the address.Encoder interface
type AddressEncoder struct {
params *Params
}
// AddressDecoder encapsulates the chain specific configurations and implements
// the address.Decoder interface
type AddressDecoder struct {
params *Params
}
// NewAddressEncoder constructs a new AddressEncoder with the chain specific
// configurations
func NewAddressEncoder(params *Params) AddressEncoder {
return AddressEncoder{params: params}
}
// NewAddressDecoder constructs a new AddressDecoder with the chain specific
// configurations
func NewAddressDecoder(params *Params) AddressDecoder {
return AddressDecoder{params: params}
}
// NewAddressEncodeDecoder constructs a new AddressEncodeDecoder with the
// chain specific configurations
func NewAddressEncodeDecoder(params *Params) AddressEncodeDecoder {
return AddressEncodeDecoder{
AddressEncoder: NewAddressEncoder(params),
AddressDecoder: NewAddressDecoder(params),
}
}
// EncodeAddress implements the address.Encoder interface
func (encoder AddressEncoder) EncodeAddress(rawAddr address.RawAddress) (address.Address, error) {
var addrType uint8
var err error
var hash [20]byte
var prefix []byte
switch len(rawAddr) {
case ripemd160.Size + 5:
prefix = rawAddr[:1]
addrType, err = addressType(prefix, encoder.params)
copy(hash[:], rawAddr[1:21])
case ripemd160.Size + 6:
prefix = rawAddr[:2]
addrType, err = addressType(prefix, encoder.params)
copy(hash[:], rawAddr[2:22])
default:
return address.Address(""), fmt.Errorf("validating address length: expected %v or %v, got %v", ripemd160.Size+5, ripemd160.Size+6, len(rawAddr))
}
if err != nil {
return address.Address(""), fmt.Errorf("parsing address type: %v", err)
}
switch addrType {
case 0, 1: // P2PKH or P2SH
return address.Address(pack.String(encodeAddress(hash[:], prefix))), nil
default:
return address.Address(""), fmt.Errorf("unexpected address type: %v", addrType)
}
}
// DecodeAddress implements the address.Decoder interface
func (decoder AddressDecoder) DecodeAddress(addr address.Address) (address.RawAddress, error) {
var decoded = base58.Decode(string(addr))
var addrType uint8
var err error
var hash [20]byte
switch len(decoded) {
case ripemd160.Size + 5:
addrType, err = addressType(decoded[:1], decoder.params)
copy(hash[:], decoded[1:21])
case ripemd160.Size + 6:
addrType, err = addressType(decoded[:2], decoder.params)
copy(hash[:], decoded[2:22])
default:
return nil, fmt.Errorf("validating address length: expected %v or %v, got %v", ripemd160.Size+5, ripemd160.Size+6, len(decoded))
}
if err != nil {
return nil, fmt.Errorf("parsing address type: %v", err)
}
var cksum [4]byte
copy(cksum[:], decoded[len(decoded)-4:])
if checksum(decoded[:len(decoded)-4]) != cksum {
return nil, fmt.Errorf("validating checksum: %v", base58.ErrChecksum)
}
switch addrType {
case 0, 1: // P2PKH or P2SH
return address.RawAddress(pack.Bytes(decoded)), nil
default:
return nil, fmt.Errorf("unexpected address type: %v", addrType)
}
}
func addressType(prefix []byte, params *Params) (uint8, error) {
if bytes.Equal(prefix, params.P2PKHPrefix) {
return 0, nil
}
if bytes.Equal(prefix, params.P2SHPrefix) {
return 1, nil
}
return 0, btcutil.ErrUnknownAddressType
}
// An Address represents a Zcash address.
type Address interface {
btcutil.Address
BitcoinAddress() btcutil.Address
}
// AddressPubKeyHash represents an address for P2PKH transactions for Zcash that
// is compatible with the Bitcoin Compat API.
type AddressPubKeyHash struct {
*btcutil.AddressPubKeyHash
params *Params
}
// NewAddressPubKeyHash returns a new AddressPubKeyHash that is compatible with
// the Bitcoin Compat API.
func NewAddressPubKeyHash(pkh []byte, params *Params) (AddressPubKeyHash, error) {
addr, err := btcutil.NewAddressPubKeyHash(pkh, params.Params)
return AddressPubKeyHash{AddressPubKeyHash: addr, params: params}, err
}
// String returns the string encoding of the transaction output destination.
//
// Please note that String differs subtly from EncodeAddress: String will return
// the value as a string without any conversion, while EncodeAddress may convert
// destination types (for example, converting pubkeys to P2PKH addresses) before
// encoding as a payment address string.
func (addr AddressPubKeyHash) String() string {
return addr.EncodeAddress()
}
// EncodeAddress returns the string encoding of the payment address associated
// with the Address value. See the comment on String for how this method differs
// from String.
func (addr AddressPubKeyHash) EncodeAddress() string {
hash := *addr.AddressPubKeyHash.Hash160()
return encodeAddress(hash[:], addr.params.P2PKHPrefix)
}
// ScriptAddress returns the raw bytes of the address to be used when inserting
// the address into a txout's script.
func (addr AddressPubKeyHash) ScriptAddress() []byte {
return addr.AddressPubKeyHash.ScriptAddress()
}
// IsForNet returns whether or not the address is associated with the passed
// bitcoin network.
func (addr AddressPubKeyHash) IsForNet(params *chaincfg.Params) bool {
return addr.AddressPubKeyHash.IsForNet(params)
}
// BitcoinAddress returns the address as if it was a Bitcoin address.
func (addr AddressPubKeyHash) BitcoinAddress() btcutil.Address {
return addr.AddressPubKeyHash
}
// AddressScriptHash represents an address for P2SH transactions for Zcash that
// is compatible with the Bitcoin Compat API.
type AddressScriptHash struct {
*btcutil.AddressScriptHash
params *Params
}
// NewAddressScriptHash returns a new AddressScriptHash that is compatible with
// the Bitcoin Compat API.
func NewAddressScriptHash(script []byte, params *Params) (AddressScriptHash, error) {
addr, err := btcutil.NewAddressScriptHash(script, params.Params)
return AddressScriptHash{AddressScriptHash: addr, params: params}, err
}
// NewAddressScriptHashFromHash returns a new AddressScriptHash that is compatible with
// the Bitcoin Compat API.
func NewAddressScriptHashFromHash(scriptHash []byte, params *Params) (AddressScriptHash, error) {
addr, err := btcutil.NewAddressScriptHashFromHash(scriptHash, params.Params)
return AddressScriptHash{AddressScriptHash: addr, params: params}, err
}
// String returns the string encoding of the transaction output destination.
//
// Please note that String differs subtly from EncodeAddress: String will return
// the value as a string without any conversion, while EncodeAddress may convert
// destination types (for example, converting pubkeys to P2PKH addresses) before
// encoding as a payment address string.
func (addr AddressScriptHash) String() string {
return addr.EncodeAddress()
}
// BitcoinAddress returns the address as if it was a Bitcoin address.
func (addr AddressScriptHash) BitcoinAddress() btcutil.Address {
return addr.AddressScriptHash
}
// EncodeAddress returns the string encoding of the payment address associated
// with the Address value. See the comment on String for how this method differs
// from String.
func (addr AddressScriptHash) EncodeAddress() string {
hash := *addr.AddressScriptHash.Hash160()
return encodeAddress(hash[:], addr.params.P2SHPrefix)
}
// ScriptAddress returns the raw bytes of the address to be used when inserting
// the address into a txout's script.
func (addr AddressScriptHash) ScriptAddress() []byte {
return addr.AddressScriptHash.ScriptAddress()
}
// IsForNet returns whether or not the address is associated with the passed
// bitcoin network.
func (addr AddressScriptHash) IsForNet(params *chaincfg.Params) bool {
return addr.AddressScriptHash.IsForNet(params)
}
// addressFromRawBytes decodes a string-representation of an address to an address
// type that implements the zcash.Address interface
func addressFromRawBytes(addrBytes []byte, params *Params) (Address, error) {
var addrType uint8
var err error
var hash [20]byte
switch len(addrBytes) {
case ripemd160.Size + 5:
addrType, err = addressType(addrBytes[:1], params)
copy(hash[:], addrBytes[1:21])
case ripemd160.Size + 6:
addrType, err = addressType(addrBytes[:2], params)
copy(hash[:], addrBytes[2:22])
default:
return nil, fmt.Errorf("validating address length: expected %v or %v, got %v", ripemd160.Size+5, ripemd160.Size+6, len(addrBytes))
}
if err != nil {
return nil, fmt.Errorf("parsing address type: %v", err)
}
switch addrType {
case 0: // P2PKH
return NewAddressPubKeyHash(hash[:], params)
case 1: // P2SH
return NewAddressScriptHashFromHash(hash[:], params)
default:
return nil, fmt.Errorf("unexpected address type: %v", addrType)
}
}
func encodeAddress(hash, prefix []byte) string {
var (
body = append(prefix, hash...)
chk = checksum(body)
cksum [4]byte
)
copy(cksum[:], chk[:4])
return base58.Encode(append(body, cksum[:]...))
}
func checksum(input []byte) (cksum [4]byte) {
var (
h = sha256.Sum256(input)
h2 = sha256.Sum256(h[:])
)
copy(cksum[:], h2[:4])
return
}