-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathusable_address.go
121 lines (98 loc) · 3.77 KB
/
usable_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
package cnlib
import (
"encoding/hex"
"errors"
"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcutil"
)
/// Type Definition
// usableAddress is a wrapper struct that can provide a usable output address.
type usableAddress struct {
Wallet *HDWallet
DerivationPath *DerivationPath
derivedPrivateKey *btcec.PrivateKey // derived from master along a derivation path, or specific pk from sweep.
}
/// Constructors
// newUsableAddressWithDerivationPath accepts a wallet and derivation path, and returns a pointer to a UsableAddress.
func newUsableAddressWithDerivationPath(wallet *HDWallet, derivationPath *DerivationPath) (*usableAddress, error) {
kf := keyFactory{masterPrivateKey: wallet.masterPrivateKey}
indexKey, err := kf.indexPrivateKey(derivationPath)
if err != nil {
return nil, err
}
ecPriv, err := indexKey.ECPrivKey()
if err != nil {
return nil, err
}
ua := usableAddress{Wallet: wallet, DerivationPath: derivationPath, derivedPrivateKey: ecPriv}
return &ua, nil
}
// newUsableAddressWithImportedPrivateKey accepts a wallet and imported private key, and returns a pointer to a UsableAddress.
func newUsableAddressWithImportedPrivateKey(wallet *HDWallet, importedPrivateKey *ImportedPrivateKey) *usableAddress {
ecPriv := importedPrivateKey.wif.PrivKey
ua := usableAddress{Wallet: wallet, DerivationPath: nil, derivedPrivateKey: ecPriv}
return &ua
}
/// Receiver methods
// MetaAddress returns a meta address with a given path based on wallet's BaseCoin, and uncompressed pubkey if a receive address. usableAddress's DerivationPath must not be nil.
func (ua *usableAddress) MetaAddress() (*MetaAddress, error) {
addr, err := ua.generateAddress()
if err != nil {
return nil, err
}
path := ua.DerivationPath
if path == nil {
return nil, errors.New("found nil derivation path")
}
ecPub := ua.derivedPrivateKey.PubKey()
pubkeyBytes := ecPub.SerializeUncompressed()
pubkey := ""
if path.Change == 0 {
pubkey = hex.EncodeToString(pubkeyBytes)
}
ma := MetaAddress{Address: addr, DerivationPath: path, UncompressedPublicKey: pubkey}
return &ma, nil
}
// BIP49AddressFromPubkeyHash returns a P2SH-P2WPKH address from a pubkey's Hash160.
func bip49AddressFromPubkeyHash(hash []byte, basecoin *BaseCoin) (string, error) {
scriptSig, err := txscript.NewScriptBuilder().AddOp(txscript.OP_0).AddData(hash).Script()
if err != nil {
return "", err
}
addrHash, err := btcutil.NewAddressScriptHash(scriptSig, basecoin.defaultNetParams())
if err != nil {
return "", err
}
return addrHash.EncodeAddress(), nil
}
// BIP84AddressFromPubkeyHash returns a native P2WPKH address from a pubkey's Hash160.
func bip84AddressFromPubkeyHash(hash []byte, basecoin *BaseCoin) (string, error) {
addrHash, err := btcutil.NewAddressWitnessPubKeyHash(hash, basecoin.defaultNetParams())
if err != nil {
return "", err
}
return addrHash.EncodeAddress(), nil
}
/// Unexposed methods
func (ua *usableAddress) generateAddress() (string, error) {
purpose := ua.DerivationPath.Purpose
if purpose == bip84purpose {
return ua.buildSegwitAddress(ua.DerivationPath)
} else if purpose == bip49purpose {
return ua.buildBIP49Address(ua.DerivationPath)
}
return "", errors.New("Unrecognized Address Purpose")
}
func (ua *usableAddress) buildBIP49Address(path *DerivationPath) (string, error) {
ecPub := ua.derivedPrivateKey.PubKey()
pubkeyBytes := ecPub.SerializeCompressed()
keyHash := btcutil.Hash160(pubkeyBytes)
return bip49AddressFromPubkeyHash(keyHash, ua.Wallet.BaseCoin)
}
func (ua *usableAddress) buildSegwitAddress(path *DerivationPath) (string, error) {
ecPub := ua.derivedPrivateKey.PubKey()
pubkeyBytes := ecPub.SerializeCompressed()
keyHash := btcutil.Hash160(pubkeyBytes)
return bip84AddressFromPubkeyHash(keyHash, ua.Wallet.BaseCoin)
}