forked from blocktree/qtum-adapter
/
base58.go
173 lines (152 loc) · 4.79 KB
/
base58.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
package btcLikeTxDriver
import (
"errors"
"fmt"
"github.com/blocktree/go-owcrypt"
)
// Errors
var (
ErrorInvalidBase58String = errors.New("invalid base58 string")
)
// Alphabet: copy from https://en.wikipedia.org/wiki/Base58
var (
BitcoinAlphabet = NewAlphabet("123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz")
)
// Alphabet The base58 alphabet object.
type Alphabet struct {
encodeTable [58]rune
decodeTable [256]int
unicodeDecodeTable []rune
}
// Alphabet's string representation
func (alphabet Alphabet) String() string {
return string(alphabet.encodeTable[:])
}
// NewAlphabet create a custom alphabet from 58-length string.
// Note: len(rune(alphabet)) must be 58.
func NewAlphabet(alphabet string) *Alphabet {
alphabetRunes := []rune(alphabet)
if len(alphabetRunes) != 58 {
panic(fmt.Sprintf("Base58 Alphabet length must 58, but %d", len(alphabetRunes)))
}
ret := new(Alphabet)
for i := range ret.decodeTable {
ret.decodeTable[i] = -1
}
ret.unicodeDecodeTable = make([]rune, 0, 58*2)
for idx, ch := range alphabetRunes {
ret.encodeTable[idx] = ch
if ch >= 0 && ch < 256 {
ret.decodeTable[byte(ch)] = idx
} else {
ret.unicodeDecodeTable = append(ret.unicodeDecodeTable, ch)
ret.unicodeDecodeTable = append(ret.unicodeDecodeTable, rune(idx))
}
}
return ret
}
// Encode encode with custom alphabet
func Encode(input []byte, alphabet *Alphabet) string {
// prefix 0
inputLength := len(input)
prefixZeroes := 0
for prefixZeroes < inputLength && input[prefixZeroes] == 0 {
prefixZeroes++
}
capacity := inputLength*138/100 + 1 // log256 / log58
output := make([]byte, capacity)
outputReverseEnd := capacity - 1
for inputPos := prefixZeroes; inputPos < inputLength; inputPos++ {
carry := uint32(input[inputPos])
outputIdx := capacity - 1
for ; carry != 0 || outputIdx > outputReverseEnd; outputIdx-- {
carry += (uint32(output[outputIdx]) << 8) // XX << 8 same as: 256 * XX
output[outputIdx] = byte(carry % 58)
carry /= 58
}
outputReverseEnd = outputIdx
}
encodeTable := alphabet.encodeTable
// when not contains unicode, use []byte to improve performance
if len(alphabet.unicodeDecodeTable) == 0 {
retStrBytes := make([]byte, prefixZeroes+(capacity-1-outputReverseEnd))
for i := 0; i < prefixZeroes; i++ {
retStrBytes[i] = byte(encodeTable[0])
}
for i, n := range output[outputReverseEnd+1:] {
retStrBytes[prefixZeroes+i] = byte(encodeTable[n])
}
return string(retStrBytes)
}
retStrRunes := make([]rune, prefixZeroes+(capacity-1-outputReverseEnd))
for i := 0; i < prefixZeroes; i++ {
retStrRunes[i] = encodeTable[0]
}
for i, n := range output[outputReverseEnd+1:] {
retStrRunes[prefixZeroes+i] = encodeTable[n]
}
return string(retStrRunes)
}
// Decode docode with custom alphabet
func Decode(input string, alphabet *Alphabet) ([]byte, error) {
inputBytes := []rune(input)
inputLength := len(inputBytes)
capacity := inputLength*733/1000 + 1 // log(58) / log(256)
output := make([]byte, capacity)
outputReverseEnd := capacity - 1
// prefix 0
zero58Byte := alphabet.encodeTable[0]
prefixZeroes := 0
for prefixZeroes < inputLength && inputBytes[prefixZeroes] == zero58Byte {
prefixZeroes++
}
for inputPos := 0; inputPos < inputLength; inputPos++ {
carry := -1
target := inputBytes[inputPos]
if target >= 0 && target < 256 {
carry = alphabet.decodeTable[target]
} else { // unicode
for i := 0; i < len(alphabet.unicodeDecodeTable); i += 2 {
if alphabet.unicodeDecodeTable[i] == target {
carry = int(alphabet.unicodeDecodeTable[i+1])
break
}
}
}
if carry == -1 {
return nil, ErrorInvalidBase58String
}
outputIdx := capacity - 1
for ; carry != 0 || outputIdx > outputReverseEnd; outputIdx-- {
carry += 58 * int(output[outputIdx])
output[outputIdx] = byte(uint32(carry) & 0xff) // same as: byte(uint32(carry) % 256)
carry >>= 8 // same as: carry /= 256
}
outputReverseEnd = outputIdx
}
retBytes := make([]byte, prefixZeroes+(capacity-1-outputReverseEnd))
for i, n := range output[outputReverseEnd+1:] {
retBytes[prefixZeroes+i] = n
}
return retBytes, nil
}
//return prefix + hash + error
func DecodeCheck(address string) (byte, []byte, error) {
ret, err := Decode(address, BitcoinAlphabet)
if err != nil {
return 0, nil, errors.New("Invalid address!")
}
checksum := owcrypt.Hash(ret[:len(ret)-4], 0, owcrypt.HASh_ALG_DOUBLE_SHA256)[:4]
for i := 0; i < 4; i++ {
if checksum[i] != ret[len(ret)-4+i] {
return 0, nil, errors.New("Invalid address!")
}
}
return ret[0], ret[1 : len(ret)-4], nil
}
func EncodeCheck(prefix byte, hash []byte) string {
data := append([]byte{prefix}, hash...)
checksum := owcrypt.Hash(data, 0, owcrypt.HASh_ALG_DOUBLE_SHA256)[:4]
data = append(data, checksum...)
return Encode(data, BitcoinAlphabet)
}