forked from libsv/go-bt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
address.go
132 lines (111 loc) · 3.95 KB
/
address.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
// Package bscript comment
package bscript
import (
"encoding/hex"
"fmt"
"github.com/libsv/go-bk/base58"
"github.com/libsv/go-bk/bec"
"github.com/libsv/go-bk/crypto"
)
const (
hashP2PKH = 0x00
hashTestNetP2PKH = 0x6f
hashP2SH = 0x05 // TODO: remove deprecated p2sh stuff
hashTestNetP2SH = 0xc4
)
// An Address struct contains the address string as well as the hash160 hex string of the public key.
// The address string will be human-readable and specific to the network type, but the public key hash
// is useful because it stays the same regardless of the network type (mainnet, testnet).
type Address struct {
AddressString string
PublicKeyHash string
}
// NewAddressFromString takes a string address (P2PKH) and returns a pointer to an Address
// which contains the address string as well as the public key hash string.
func NewAddressFromString(addr string) (*Address, error) {
pkh, err := addressToPubKeyHashStr(addr)
if err != nil {
return nil, err
}
return &Address{
AddressString: addr,
PublicKeyHash: pkh,
}, nil
}
func addressToPubKeyHashStr(address string) (string, error) {
decoded := base58.Decode(address)
if len(decoded) != 25 {
return "", fmt.Errorf("%w for '%s'", ErrInvalidAddressLength, address)
}
switch decoded[0] {
case hashP2PKH: // Pubkey hash (P2PKH address)
return hex.EncodeToString(decoded[1 : len(decoded)-4]), nil
case hashTestNetP2PKH: // Testnet pubkey hash (P2PKH address)
return hex.EncodeToString(decoded[1 : len(decoded)-4]), nil
case hashP2SH: // Script hash (P2SH address)
fallthrough
case hashTestNetP2SH: // Testnet script hash (P2SH address)
fallthrough
default:
return "", fmt.Errorf("%w %s", ErrUnsupportedAddress, address)
}
}
// NewAddressFromPublicKeyString takes a public key string and returns an Address struct pointer.
// If mainnet parameter is true it will return a mainnet address (starting with a 1).
// Otherwise, (mainnet is false) it will return a testnet address (starting with an m or n).
func NewAddressFromPublicKeyString(pubKey string, mainnet bool) (*Address, error) {
pubKeyBytes, err := hex.DecodeString(pubKey)
if err != nil {
return nil, err
}
return NewAddressFromPublicKeyHash(crypto.Hash160(pubKeyBytes), mainnet)
}
// NewAddressFromPublicKeyHash takes a public key hash in bytes and returns an Address struct pointer.
// If mainnet parameter is true it will return a mainnet address (starting with a 1).
// Otherwise, (mainnet is false) it will return a testnet address (starting with an m or n).
func NewAddressFromPublicKeyHash(hash []byte, mainnet bool) (*Address, error) {
// regtest := 111
// mainnet: 0
bb := make([]byte, 1)
if !mainnet {
bb[0] = 111
}
//nolint: makezero // we need to set up the array with 1
bb = append(bb, hash...)
return &Address{
AddressString: Base58EncodeMissingChecksum(bb),
PublicKeyHash: hex.EncodeToString(hash),
}, nil
}
// NewAddressFromPublicKey takes a bec public key and returns an Address struct pointer.
// If mainnet parameter is true it will return a mainnet address (starting with a 1).
// Otherwise, (mainnet is false) it will return a testnet address (starting with an m or n).
func NewAddressFromPublicKey(pubKey *bec.PublicKey, mainnet bool) (*Address, error) {
hash := crypto.Hash160(pubKey.SerialiseCompressed())
// regtest := 111
// mainnet: 0
bb := make([]byte, 1)
if !mainnet {
bb[0] = 111
}
//nolint: makezero // we need to set up the array with 1
bb = append(bb, hash...)
return &Address{
AddressString: Base58EncodeMissingChecksum(bb),
PublicKeyHash: hex.EncodeToString(hash),
}, nil
}
// Base58EncodeMissingChecksum appends a checksum to a byte sequence
// then encodes into base58 encoding.
func Base58EncodeMissingChecksum(input []byte) string {
b := make([]byte, 0, len(input)+4)
b = append(b, input[:]...)
ckSum := checksum(b)
b = append(b, ckSum[:]...)
return base58.Encode(b)
}
func checksum(input []byte) (ckSum [4]byte) {
h := crypto.Sha256d(input)
copy(ckSum[:], h[:4])
return
}