-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #293 from blinklabs-io/feat/ledger-address
feat: properly handle addresses in ledger
- Loading branch information
Showing
17 changed files
with
1,074 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright (c) 2015 The btcsuite developers | ||
// Use of this source code is governed by an ISC | ||
// license that can be found in the LICENSE file. | ||
|
||
// NOTE: this file was copied from: | ||
// https://github.com/btcsuite/btcd/tree/v0.23.4/btcutil/base58 | ||
|
||
// AUTOGENERATED by genalphabet.go; do not edit. | ||
|
||
package base58 | ||
|
||
const ( | ||
// alphabet is the modified base58 alphabet used by Bitcoin. | ||
alphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz" | ||
|
||
alphabetIdx0 = '1' | ||
) | ||
|
||
var b58 = [256]byte{ | ||
255, 255, 255, 255, 255, 255, 255, 255, | ||
255, 255, 255, 255, 255, 255, 255, 255, | ||
255, 255, 255, 255, 255, 255, 255, 255, | ||
255, 255, 255, 255, 255, 255, 255, 255, | ||
255, 255, 255, 255, 255, 255, 255, 255, | ||
255, 255, 255, 255, 255, 255, 255, 255, | ||
255, 0, 1, 2, 3, 4, 5, 6, | ||
7, 8, 255, 255, 255, 255, 255, 255, | ||
255, 9, 10, 11, 12, 13, 14, 15, | ||
16, 255, 17, 18, 19, 20, 21, 255, | ||
22, 23, 24, 25, 26, 27, 28, 29, | ||
30, 31, 32, 255, 255, 255, 255, 255, | ||
255, 33, 34, 35, 36, 37, 38, 39, | ||
40, 41, 42, 43, 255, 44, 45, 46, | ||
47, 48, 49, 50, 51, 52, 53, 54, | ||
55, 56, 57, 255, 255, 255, 255, 255, | ||
255, 255, 255, 255, 255, 255, 255, 255, | ||
255, 255, 255, 255, 255, 255, 255, 255, | ||
255, 255, 255, 255, 255, 255, 255, 255, | ||
255, 255, 255, 255, 255, 255, 255, 255, | ||
255, 255, 255, 255, 255, 255, 255, 255, | ||
255, 255, 255, 255, 255, 255, 255, 255, | ||
255, 255, 255, 255, 255, 255, 255, 255, | ||
255, 255, 255, 255, 255, 255, 255, 255, | ||
255, 255, 255, 255, 255, 255, 255, 255, | ||
255, 255, 255, 255, 255, 255, 255, 255, | ||
255, 255, 255, 255, 255, 255, 255, 255, | ||
255, 255, 255, 255, 255, 255, 255, 255, | ||
255, 255, 255, 255, 255, 255, 255, 255, | ||
255, 255, 255, 255, 255, 255, 255, 255, | ||
255, 255, 255, 255, 255, 255, 255, 255, | ||
255, 255, 255, 255, 255, 255, 255, 255, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
// Copyright (c) 2013-2015 The btcsuite developers | ||
// Use of this source code is governed by an ISC | ||
// license that can be found in the LICENSE file. | ||
|
||
// NOTE: this file was copied from: | ||
// https://github.com/btcsuite/btcd/tree/v0.23.4/btcutil/base58 | ||
|
||
package base58 | ||
|
||
import ( | ||
"math/big" | ||
) | ||
|
||
//go:generate go run genalphabet.go | ||
|
||
var bigRadix = [...]*big.Int{ | ||
big.NewInt(0), | ||
big.NewInt(58), | ||
big.NewInt(58 * 58), | ||
big.NewInt(58 * 58 * 58), | ||
big.NewInt(58 * 58 * 58 * 58), | ||
big.NewInt(58 * 58 * 58 * 58 * 58), | ||
big.NewInt(58 * 58 * 58 * 58 * 58 * 58), | ||
big.NewInt(58 * 58 * 58 * 58 * 58 * 58 * 58), | ||
big.NewInt(58 * 58 * 58 * 58 * 58 * 58 * 58 * 58), | ||
big.NewInt(58 * 58 * 58 * 58 * 58 * 58 * 58 * 58 * 58), | ||
bigRadix10, | ||
} | ||
|
||
var bigRadix10 = big.NewInt(58 * 58 * 58 * 58 * 58 * 58 * 58 * 58 * 58 * 58) // 58^10 | ||
|
||
// Decode decodes a modified base58 string to a byte slice. | ||
func Decode(b string) []byte { | ||
answer := big.NewInt(0) | ||
scratch := new(big.Int) | ||
|
||
// Calculating with big.Int is slow for each iteration. | ||
// x += b58[b[i]] * j | ||
// j *= 58 | ||
// | ||
// Instead we can try to do as much calculations on int64. | ||
// We can represent a 10 digit base58 number using an int64. | ||
// | ||
// Hence we'll try to convert 10, base58 digits at a time. | ||
// The rough idea is to calculate `t`, such that: | ||
// | ||
// t := b58[b[i+9]] * 58^9 ... + b58[b[i+1]] * 58^1 + b58[b[i]] * 58^0 | ||
// x *= 58^10 | ||
// x += t | ||
// | ||
// Of course, in addition, we'll need to handle boundary condition when `b` is not multiple of 58^10. | ||
// In that case we'll use the bigRadix[n] lookup for the appropriate power. | ||
for t := b; len(t) > 0; { | ||
n := len(t) | ||
if n > 10 { | ||
n = 10 | ||
} | ||
|
||
total := uint64(0) | ||
for _, v := range t[:n] { | ||
if v > 255 { | ||
return []byte("") | ||
} | ||
|
||
tmp := b58[v] | ||
if tmp == 255 { | ||
return []byte("") | ||
} | ||
total = total*58 + uint64(tmp) | ||
} | ||
|
||
answer.Mul(answer, bigRadix[n]) | ||
scratch.SetUint64(total) | ||
answer.Add(answer, scratch) | ||
|
||
t = t[n:] | ||
} | ||
|
||
tmpval := answer.Bytes() | ||
|
||
var numZeros int | ||
for numZeros = 0; numZeros < len(b); numZeros++ { | ||
if b[numZeros] != alphabetIdx0 { | ||
break | ||
} | ||
} | ||
flen := numZeros + len(tmpval) | ||
val := make([]byte, flen) | ||
copy(val[numZeros:], tmpval) | ||
|
||
return val | ||
} | ||
|
||
// Encode encodes a byte slice to a modified base58 string. | ||
func Encode(b []byte) string { | ||
x := new(big.Int) | ||
x.SetBytes(b) | ||
|
||
// maximum length of output is log58(2^(8*len(b))) == len(b) * 8 / log(58) | ||
maxlen := int(float64(len(b))*1.365658237309761) + 1 | ||
answer := make([]byte, 0, maxlen) | ||
mod := new(big.Int) | ||
for x.Sign() > 0 { | ||
// Calculating with big.Int is slow for each iteration. | ||
// x, mod = x / 58, x % 58 | ||
// | ||
// Instead we can try to do as much calculations on int64. | ||
// x, mod = x / 58^10, x % 58^10 | ||
// | ||
// Which will give us mod, which is 10 digit base58 number. | ||
// We'll loop that 10 times to convert to the answer. | ||
|
||
x.DivMod(x, bigRadix10, mod) | ||
if x.Sign() == 0 { | ||
// When x = 0, we need to ensure we don't add any extra zeros. | ||
m := mod.Int64() | ||
for m > 0 { | ||
answer = append(answer, alphabet[m%58]) | ||
m /= 58 | ||
} | ||
} else { | ||
m := mod.Int64() | ||
for i := 0; i < 10; i++ { | ||
answer = append(answer, alphabet[m%58]) | ||
m /= 58 | ||
} | ||
} | ||
} | ||
|
||
// leading zero bytes | ||
for _, i := range b { | ||
if i != 0 { | ||
break | ||
} | ||
answer = append(answer, alphabetIdx0) | ||
} | ||
|
||
// reverse | ||
alen := len(answer) | ||
for i := 0; i < alen/2; i++ { | ||
answer[i], answer[alen-1-i] = answer[alen-1-i], answer[i] | ||
} | ||
|
||
return string(answer) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Copyright (c) 2013-2014 The btcsuite developers | ||
// Use of this source code is governed by an ISC | ||
// license that can be found in the LICENSE file. | ||
|
||
// NOTE: this file was copied from: | ||
// https://github.com/btcsuite/btcd/tree/v0.23.4/btcutil/base58 | ||
|
||
package base58 | ||
|
||
import ( | ||
"crypto/sha256" | ||
"errors" | ||
) | ||
|
||
// ErrChecksum indicates that the checksum of a check-encoded string does not verify against | ||
// the checksum. | ||
var ErrChecksum = errors.New("checksum error") | ||
|
||
// ErrInvalidFormat indicates that the check-encoded string has an invalid format. | ||
var ErrInvalidFormat = errors.New("invalid format: version and/or checksum bytes missing") | ||
|
||
// checksum: first four bytes of sha256^2 | ||
func checksum(input []byte) (cksum [4]byte) { | ||
h := sha256.Sum256(input) | ||
h2 := sha256.Sum256(h[:]) | ||
copy(cksum[:], h2[:4]) | ||
return | ||
} | ||
|
||
// CheckEncode prepends a version byte and appends a four byte checksum. | ||
func CheckEncode(input []byte, version byte) string { | ||
b := make([]byte, 0, 1+len(input)+4) | ||
b = append(b, version) | ||
b = append(b, input...) | ||
cksum := checksum(b) | ||
b = append(b, cksum[:]...) | ||
return Encode(b) | ||
} | ||
|
||
// CheckDecode decodes a string that was encoded with CheckEncode and verifies the checksum. | ||
func CheckDecode(input string) (result []byte, version byte, err error) { | ||
decoded := Decode(input) | ||
if len(decoded) < 5 { | ||
return nil, 0, ErrInvalidFormat | ||
} | ||
version = decoded[0] | ||
var cksum [4]byte | ||
copy(cksum[:], decoded[len(decoded)-4:]) | ||
if checksum(decoded[:len(decoded)-4]) != cksum { | ||
return nil, 0, ErrChecksum | ||
} | ||
payload := decoded[1 : len(decoded)-4] | ||
result = append(result, payload...) | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright (c) 2014 The btcsuite developers | ||
// Use of this source code is governed by an ISC | ||
// license that can be found in the LICENSE file. | ||
|
||
// NOTE: this file was copied from: | ||
// https://github.com/btcsuite/btcd/tree/v0.23.4/btcutil/base58 | ||
|
||
/* | ||
Package base58 provides an API for working with modified base58 and Base58Check | ||
encodings. | ||
Modified Base58 Encoding | ||
Standard base58 encoding is similar to standard base64 encoding except, as the | ||
name implies, it uses a 58 character alphabet which results in an alphanumeric | ||
string and allows some characters which are problematic for humans to be | ||
excluded. Due to this, there can be various base58 alphabets. | ||
The modified base58 alphabet used by Bitcoin, and hence this package, omits the | ||
0, O, I, and l characters that look the same in many fonts and are therefore | ||
hard to humans to distinguish. | ||
Base58Check Encoding Scheme | ||
The Base58Check encoding scheme is primarily used for Bitcoin addresses at the | ||
time of this writing, however it can be used to generically encode arbitrary | ||
byte arrays into human-readable strings along with a version byte that can be | ||
used to differentiate the same payload. For Bitcoin addresses, the extra | ||
version is used to differentiate the network of otherwise identical public keys | ||
which helps prevent using an address intended for one network on another. | ||
*/ | ||
package base58 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// Copyright (c) 2015 The btcsuite developers | ||
// Use of this source code is governed by an ISC | ||
// license that can be found in the LICENSE file. | ||
|
||
// NOTE: this file was copied from: | ||
// https://github.com/btcsuite/btcd/tree/v0.23.4/btcutil/base58 | ||
|
||
//go:build ignore | ||
// +build ignore | ||
|
||
package main | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"log" | ||
"os" | ||
"strconv" | ||
) | ||
|
||
var ( | ||
start = []byte(`// Copyright (c) 2015 The btcsuite developers | ||
// Use of this source code is governed by an ISC | ||
// license that can be found in the LICENSE file. | ||
// AUTOGENERATED by genalphabet.go; do not edit. | ||
package base58 | ||
const ( | ||
// alphabet is the modified base58 alphabet used by Bitcoin. | ||
alphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz" | ||
alphabetIdx0 = '1' | ||
) | ||
var b58 = [256]byte{`) | ||
|
||
end = []byte(`}`) | ||
|
||
alphabet = []byte("123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz") | ||
tab = []byte("\t") | ||
invalid = []byte("255") | ||
comma = []byte(",") | ||
space = []byte(" ") | ||
nl = []byte("\n") | ||
) | ||
|
||
func write(w io.Writer, b []byte) { | ||
_, err := w.Write(b) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
|
||
func main() { | ||
fi, err := os.Create("alphabet.go") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer fi.Close() | ||
|
||
write(fi, start) | ||
write(fi, nl) | ||
for i := byte(0); i < 32; i++ { | ||
write(fi, tab) | ||
for j := byte(0); j < 8; j++ { | ||
idx := bytes.IndexByte(alphabet, i*8+j) | ||
if idx == -1 { | ||
write(fi, invalid) | ||
} else { | ||
write(fi, strconv.AppendInt(nil, int64(idx), 10)) | ||
} | ||
write(fi, comma) | ||
if j != 7 { | ||
write(fi, space) | ||
} | ||
} | ||
write(fi, nl) | ||
} | ||
write(fi, end) | ||
write(fi, nl) | ||
} |
Oops, something went wrong.