-
Notifications
You must be signed in to change notification settings - Fork 0
/
Base58Address.go
54 lines (48 loc) · 1.73 KB
/
Base58Address.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
package btcaddress
import (
"errors"
"crypto/sha256"
"github.com/chainlibs/btcaddress/base58"
)
// 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
}
// DecodeAddressToH160 decodes a string that was encoded with CheckEncode and verifies the checksum.
func DecodeAddressToH160(input string) (result []byte, version byte, err error) {
decoded := base58.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
}
// encodeAddressFromH160 returns a human-readable payment address given a ripemd160 hash
// and magic which encodes the bitcoin network and address type. It is used
// in both pay-to-pubkey-hash (P2PKH) and pay-to-script-hash (P2SH) address
// encoding.
func EncodeAddressFromH160(hash160 []byte, magic Magic) string {
// Format is 1 byte for a network and address class (i.e. P2PKH vs
// P2SH), 20 bytes for a RIPEMD160 hash, and 4 bytes of checksum.
b := make([]byte, 0, 1+len(hash160)+4)
b = append(b, byte(magic))
b = append(b, hash160[:]...)
cksum := checksum(b)
b = append(b, cksum[:]...)
return base58.Encode(b)
}