forked from shuttleone/sanswallet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
p2sh.go
132 lines (106 loc) · 3.65 KB
/
p2sh.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
/*
SansWallet is a BIP32, BIP44, BIP49 and BIP84 compatible hierarchical determinstic wallet
Copyright (C) 2018 Sans Central
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package sanswallet
import (
"errors"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcutil"
"github.com/btcsuite/btcutil/hdkeychain"
"github.com/sanscentral/sanswallet/keys"
"github.com/sanscentral/sanswallet/network"
)
var (
// Version bytes of master public/private keys indicate what type of output script should be used.
privP2WPKHinP2SHVer = [4]byte{0x04, 0x9d, 0x78, 0x78}
pubP2WPKHinP2SHVer = [4]byte{0x04, 0x9d, 0x7c, 0xb2}
// possible prefixes for a P2SH key (Used for key validation)
p2shHumanPre = map[string]bool{"ypub": true, "yprv": true}
)
// GetExtPrvForP2SHAccount returns extended private key for BIP49 P2SH account
func GetExtPrvForP2SHAccount(seed []byte, accountIndex int, testnet bool) (string, error) {
index, err := intToUint32(accountIndex)
if err != nil {
return "", err
}
net := network.BTCMainnet
if testnet {
net = network.BTCTestnet
}
m, err := keys.GetExtendedMasterPrivateKeyFromSeedBytes(seed, net)
if err != nil {
return "", err
}
k, err := keys.GetBIP49AccountKey(m, index, true)
if err != nil {
return "", err
}
return hdkeychain.VersionedStringFromExtendedKeyString(k, privP2WPKHinP2SHVer)
}
// GetExtPubForP2SHAccount returns extended public key for BIP49 P2SH account
func GetExtPubForP2SHAccount(seed []byte, accountIndex int, testnet bool) (string, error) {
index, err := intToUint32(accountIndex)
if err != nil {
return "", err
}
net := network.BTCMainnet
if testnet {
net = network.BTCTestnet
}
m, err := keys.GetExtendedMasterPrivateKeyFromSeedBytes(seed, net)
if err != nil {
return "", err
}
k, err := keys.GetBIP49AccountKey(m, index, false)
return hdkeychain.VersionedStringFromExtendedKeyString(k, pubP2WPKHinP2SHVer)
}
// GetP2SHAddressForIndex returns address for BTC account at given index
// P2SH ('3' prefixed addresses) pay-to-script-hash includes P2WPKH-wrapped in P2SH segwit outputs (use BIP49 derived key)
func GetP2SHAddressForIndex(accountKey string, addressIndex int, isChange bool, testnet bool) (string, error) {
validPre := p2shHumanPre[accountKey[:4]]
if !validPre {
return "", errors.New("Key does not start with a P2SH prefix")
}
index, err := intToUint32(addressIndex)
if err != nil {
return "", err
}
addt := keys.ExternalAddress
if isChange {
addt = keys.ChangeAddress
}
k, err := keys.GetAccountAddressKey(accountKey, addt, index)
if err != nil {
return "", err
}
pk, err := k.ECPubKey()
if err != nil {
return "", err
}
keyHash := btcutil.Hash160(pk.SerializeCompressed())
scriptSig, err := txscript.NewScriptBuilder().AddOp(txscript.OP_0).AddData(keyHash).Script()
if err != nil {
return "", err
}
netParam := &chaincfg.MainNetParams
if testnet {
netParam = &chaincfg.TestNet3Params
}
segAddr, err := btcutil.NewAddressScriptHash(scriptSig, netParam)
if err != nil {
return "", err
}
return segAddr.EncodeAddress(), nil
}