13 // TrivialBase58Encoding encodes the passed bytes into a base58 encoded string
15 func TrivialBase58Encoding(a []byte) string {
16 return TrivialBase58EncodingAlphabet(a, BTCAlphabet)
19 // TrivialBase58EncodingAlphabet encodes the passed bytes into a base58 encoded
20 // string (inefficiently) with the passed alphabet.
21 func TrivialBase58EncodingAlphabet(a []byte, alphabet *Alphabet) string {
22 zero := alphabet.encode[0]
23 idx := len(a)*138/100 + 1
24 buf := make([]byte, idx)
25 bn := new(big.Int).SetBytes(a)
27 for bn.Cmp(bn0) != 0 {
28 bn, mo = bn.DivMod(bn, bn58, new(big.Int))
30 buf[idx] = alphabet.encode[mo.Int64()]
39 return string(buf[idx:])
42 // TrivialBase58Decoding decodes the base58 encoded bytes (inefficiently).
43 func TrivialBase58Decoding(str string) ([]byte, error) {
44 return TrivialBase58DecodingAlphabet(str, BTCAlphabet)
47 // TrivialBase58DecodingAlphabet decodes the base58 encoded bytes
48 // (inefficiently) using the given b58 alphabet.
49 func TrivialBase58DecodingAlphabet(str string, alphabet *Alphabet) ([]byte, error) {
50 zero := alphabet.encode[0]
53 for i := 0; i < len(str) && str[i] == zero; i++ {
56 leading := make([]byte, zcount)
61 for ; j < len(src) && src[j] == byte(padChar); j++ {
65 for i := range src[j:] {
66 c := alphabet.decode[src[i]]
68 return nil, fmt.Errorf("illegal base58 data at input index: %d", i)
71 n.Add(n, big.NewInt(int64(c)))
73 return append(leading, n.Bytes()...), nil