-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathzecaddr.go
194 lines (158 loc) · 4.98 KB
/
zecaddr.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
package zecutil
import (
"crypto/sha256"
"errors"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcutil"
"github.com/btcsuite/btcutil/base58"
"golang.org/x/crypto/ripemd160"
)
type ChainParams struct {
PubHashPrefixes []byte
ScriptHashPrefixes []byte
}
var (
MainNet = ChainParams{
PubHashPrefixes: []byte{0x1C, 0xB8},
ScriptHashPrefixes: []byte{0x1C, 0xBD},
}
TestNet3 = ChainParams{
PubHashPrefixes: []byte{0x1D, 0x25},
ScriptHashPrefixes: []byte{0x1C, 0xBA},
}
NetList = map[string]ChainParams{
"mainnet": MainNet,
"testnet3": TestNet3,
"regtest": TestNet3,
}
)
type ZecAddressScriptHash struct {
hash [ripemd160.Size]byte
prefix string
}
type ZecAddressPubKeyHash struct {
hash [ripemd160.Size]byte
prefix string
}
func NewAddressPubKeyHash(hash [ripemd160.Size]byte, prefix string) *ZecAddressPubKeyHash {
return &ZecAddressPubKeyHash{hash, prefix}
}
func NewAddressScriptHash(hash [ripemd160.Size]byte, prefix string) *ZecAddressScriptHash {
return &ZecAddressScriptHash{hash, prefix}
}
// Encode pubHash to zec address
func Encode(pkHash []byte, net *chaincfg.Params) (_ string, err error) {
if _, ok := NetList[net.Name]; !ok {
return "", errors.New("unknown network parameters")
}
var addrPubKey *btcutil.AddressPubKey
if addrPubKey, err = btcutil.NewAddressPubKey(pkHash, net); err != nil {
return "", err
}
return EncodeHash(btcutil.Hash160(addrPubKey.ScriptAddress())[:ripemd160.Size], NetList[net.Name].PubHashPrefixes)
}
func EncodeHash(addrHash []byte, prefix []byte) (_ string, err error) {
if len(addrHash) != ripemd160.Size {
return "", errors.New("incorrect hash length")
}
var (
body = append(prefix, addrHash[:ripemd160.Size]...)
chk = addrChecksum(body)
cksum [4]byte
)
copy(cksum[:], chk[:4])
return base58.Encode(append(body, cksum[:]...)), nil
}
// DecodeAddress zec address string
func DecodeAddress(address string, netName string) (btcutil.Address, error) {
var (
net ChainParams
ok bool
)
if net, ok = NetList[netName]; !ok {
return nil, errors.New("unknown net")
}
var decoded = base58.Decode(address)
if len(decoded) != 26 {
return nil, base58.ErrInvalidFormat
}
var cksum [4]byte
copy(cksum[:], decoded[len(decoded)-4:])
if addrChecksum(decoded[:len(decoded)-4]) != cksum {
return nil, base58.ErrChecksum
}
if len(decoded)-6 != ripemd160.Size {
return nil, errors.New("incorrect payload len")
}
switch {
case net.PubHashPrefixes[0] == decoded[0] && net.PubHashPrefixes[1] == decoded[1]:
addr := &ZecAddressPubKeyHash{prefix: netName}
copy(addr.hash[:], decoded[2:len(decoded)-4])
return addr, nil
case net.ScriptHashPrefixes[0] == decoded[0] && net.ScriptHashPrefixes[1] == decoded[1]:
addr := &ZecAddressScriptHash{prefix: netName}
copy(addr.hash[:], decoded[2:len(decoded)-4])
return addr, nil
}
return nil, errors.New("unknown address")
}
// EncodeAddress returns the string encoding of a pay-to-pubkey-hash
// address. Part of the Address interface.
func (a *ZecAddressPubKeyHash) EncodeAddress() (addr string) {
addr, _ = EncodeHash(a.hash[:], NetList[a.prefix].PubHashPrefixes)
return addr
}
// ScriptAddress returns the bytes to be included in a txout script to pay
// to a pubkey hash. Part of the Address interface.
func (a *ZecAddressPubKeyHash) ScriptAddress() []byte {
return a.hash[:]
}
// IsForNet returns whether or not the pay-to-pubkey-hash address is associated
// with the passed bitcoin cash network.
func (a *ZecAddressPubKeyHash) IsForNet(net *chaincfg.Params) bool {
_, ok := NetList[net.Name]
if !ok {
return false
}
return a.prefix == net.Name
}
// String returns a human-readable string for the pay-to-pubkey-hash address.
// This is equivalent to calling EncodeAddress, but is provided so the type can
// be used as a fmt.Stringer.
func (a *ZecAddressPubKeyHash) String() string {
return a.EncodeAddress()
}
// EncodeAddress returns the string encoding of a pay-to-pubkey-hash
// address. Part of the Address interface.
func (a *ZecAddressScriptHash) EncodeAddress() (addr string) {
addr, _ = EncodeHash(a.hash[:], NetList[a.prefix].ScriptHashPrefixes)
return addr
}
// ScriptAddress returns the bytes to be included in a txout script to pay
// to a pubkey hash. Part of the Address interface.
func (a *ZecAddressScriptHash) ScriptAddress() []byte {
return a.hash[:]
}
// IsForNet returns whether or not the pay-to-pubkey-hash address is associated
// with the passed bitcoin cash network.
func (a *ZecAddressScriptHash) IsForNet(net *chaincfg.Params) bool {
_, ok := NetList[net.Name]
if !ok {
return false
}
return a.prefix == net.Name
}
// String returns a human-readable string for the pay-to-pubkey-hash address.
// This is equivalent to calling EncodeAddress, but is provided so the type can
// be used as a fmt.Stringer.
func (a *ZecAddressScriptHash) String() string {
return a.EncodeAddress()
}
func addrChecksum(input []byte) (cksum [4]byte) {
var (
h = sha256.Sum256(input)
h2 = sha256.Sum256(h[:])
)
copy(cksum[:], h2[:4])
return
}