-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcashaddrconv.go
200 lines (167 loc) · 4.32 KB
/
cashaddrconv.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
// Copyright (c) 2018 The bcext developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package cashutil
import (
"bytes"
"errors"
"strings"
"github.com/bcext/gcash/chaincfg"
)
type addrType uint8
const (
pubKeyType addrType = iota
scriptType
)
var (
errEmptyAddressContent = errors.New("empty address content")
errInvalidHash160Size = errors.New("invalid hash160 size")
errInvalidAddressType = errors.New("invalid address type")
)
type addrContent struct {
t addrType
hash []byte
}
func encodeCashAddr(dst Address) string {
switch addr := dst.(type) {
case *AddressPubKeyHash:
data := packAddrData(addr.Hash160()[:], uint8(pubKeyType))
return encode(addr.net.CashAddrPrefix, data)
case *AddressScriptHash:
data := packAddrData(addr.Hash160()[:], uint8(scriptType))
return encode(addr.net.CashAddrPrefix, data)
default:
return ""
}
}
func decodeCashAddr(addr string, param *chaincfg.Params) (Address, error) {
// handle bitcoin address prefixed with net tag
if strings.Contains(addr, ":") {
pos := strings.LastIndex(addr, ":")
addr = addr[pos+1:]
}
content := decodeCashaddrContent(addr, param)
if content == nil || len(content.hash) == 0 {
return nil, errEmptyAddressContent
}
return decodeCashAddrDestination(content, param)
}
// Convert the data part to a 5 bit representation.
func packAddrData(id []byte, t uint8) []byte {
version := t << 3
size := len(id)
var encodedSize uint8
switch size * 8 {
case 160:
encodedSize = 0
case 192:
encodedSize = 1
case 224:
encodedSize = 2
case 256:
encodedSize = 3
case 320:
encodedSize = 4
case 384:
encodedSize = 5
case 448:
encodedSize = 6
case 512:
encodedSize = 7
default:
panic("Error packing cashaddr: invalid address length")
}
version |= encodedSize
data := bytes.NewBuffer(make([]byte, 0, len(id)+1))
data.WriteByte(version)
data.Write(id)
// Reserve the number of bytes required for a 5-bit packed version of a
// hash, with version byte. Add half a byte(4) so integer math provides
// the next multiple-of-5 that would fit all the data.
ret, _ := convertBits(8, 5, true, data.Bytes())
return ret
}
func convertBits(frombits uint, tobits uint, pad bool, data []byte) ([]byte, bool) {
var acc, bits int
maxv := (1 << tobits) - 1
maxAcc := (1 << (frombits + tobits - 1)) - 1
ret := bytes.NewBuffer(nil)
for _, bit := range data {
acc = ((acc << frombits) | int(bit)) & maxAcc
bits += int(frombits)
for bits >= int(tobits) {
bits -= int(tobits)
ret.WriteByte(byte((acc >> uint(bits)) & maxv))
}
}
// We have remaining bits to encode but do not pad.
if !pad && bits != 0 {
return ret.Bytes(), false
}
// We have remaining bits to encode so we do pad.
if pad && bits != 0 {
ret.WriteByte(byte(acc << (tobits - uint(bits)) & maxv))
}
return ret.Bytes(), true
}
func decodeCashaddrContent(addr string, param *chaincfg.Params) *addrContent {
prefix, payload := decode(addr, param.CashAddrPrefix)
if prefix != param.CashAddrPrefix {
return nil
}
if len(payload) == 0 {
return nil
}
// Check that the padding is zero.
extraBits := len(payload) * 5 % 8
if extraBits >= 5 {
// We have more padding than allowed.
return nil
}
last := payload[len(payload)-1]
mask := 1<<uint(extraBits) - 1
if int(last)&mask != 0 {
// We have non zero bits as padding.
return nil
}
data, _ := convertBits(5, 8, false, payload)
// Decode type and size from the version.
version := data[0]
if version&0x80 != 0 {
// First bit is reserved.
return nil
}
t := addrType((version >> 3) & 0x1f)
hashSize := 20 + 4*(version&0x03)
if version&0x04 != 0 {
hashSize *= 2
}
// Check that we decoded the exact number of bytes we expected.
if len(data) != int(hashSize)+1 {
return nil
}
// Pop the version.
data = data[1:]
return &addrContent{t, data}
}
func decodeCashAddrDestination(content *addrContent, params *chaincfg.Params) (Address, error) {
if len(content.hash) != 20 {
return nil, errInvalidHash160Size
}
switch content.t {
case pubKeyType:
addr, err := NewAddressPubKeyHash(content.hash, params)
if err != nil {
return nil, err
}
return addr, nil
case scriptType:
addr, err := NewAddressScriptHashFromHash(content.hash, params)
if err != nil {
return nil, err
}
return addr, nil
default:
return nil, errInvalidAddressType
}
}