/
import_sweep.go
223 lines (203 loc) · 6.27 KB
/
import_sweep.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package btc
import (
"bytes"
"context"
"encoding/hex"
"errors"
"fmt"
"github.com/btcsuite/btcd/btcutil"
"github.com/dev-warrior777/go-electrum-client/wallet"
)
// Import UTXO's for a known privkey from another wallet from electrumX. Partially
// implemented (P2WPKH,P2PKH) as it is not the most important tool for this wallet.
func (ec *BtcElectrumClient) getWitnessScriptHashRedeemUtxos( /*ctx*/ context.Context /*keyPair*/, *btcutil.WIF) ([]wallet.InputInfo, error) {
utxoList := make([]wallet.InputInfo, 0)
fmt.Println("looking for P2WSH redemptions - Not implemented")
return utxoList, nil
}
func (ec *BtcElectrumClient) getScriptHashRedeemUtxos( /*ctx*/ context.Context /*keyPair*/, *btcutil.WIF) ([]wallet.InputInfo, error) {
utxoList := make([]wallet.InputInfo, 0)
fmt.Println("looking for P2SH Redemptions - Not implemented")
return utxoList, nil
}
func (ec *BtcElectrumClient) getPubKeyUtxos( /*ctx*/ context.Context /*keyPair*/, *btcutil.WIF) ([]wallet.InputInfo, error) {
utxoList := make([]wallet.InputInfo, 0)
fmt.Println("looking for P2PK - Not implemented")
return utxoList, nil
}
func (ec *BtcElectrumClient) getPubKeyHashUtxos(ctx context.Context, keyPair *btcutil.WIF) ([]wallet.InputInfo, error) {
inputList := make([]wallet.InputInfo, 0, 1)
pubKey := keyPair.SerializePubKey()
node := ec.GetNode()
if node == nil {
return inputList, ErrNoNode
}
// make address p2pkh
pkHash := btcutil.Hash160(pubKey)
addressPubKeyHash, err := btcutil.NewAddressPubKeyHash(pkHash, ec.GetConfig().Params)
if err != nil {
return inputList, err
}
// make scripthash
scripthash, err := addressToElectrumScripthash(addressPubKeyHash)
if err != nil {
return inputList, err
}
// ask electrumX
listUnspent, err := node.GetListUnspent(ctx, scripthash)
if err != nil {
return inputList, err
}
for _, unspent := range listUnspent {
op, err := wallet.NewOutPointFromString(
fmt.Sprintf("%s:%d", unspent.TxHash, unspent.TxPos))
if err != nil {
return inputList, err
}
input := wallet.InputInfo{
Outpoint: op,
Height: unspent.Height,
Value: unspent.Value,
LinkedAddress: addressPubKeyHash,
PkScript: []byte{},
KeyPair: keyPair,
}
inputList = append(inputList, input)
}
return inputList, nil
}
func (ec *BtcElectrumClient) getWitnessPubKeyHashUtxos(ctx context.Context, keyPair *btcutil.WIF) ([]wallet.InputInfo, error) {
inputList := make([]wallet.InputInfo, 0, 1)
pubKey := keyPair.SerializePubKey()
node := ec.GetNode()
if node == nil {
return inputList, ErrNoNode
}
// make address p2wpkh
pkHash := btcutil.Hash160(pubKey)
addressWitnessPubKeyHash, err := btcutil.NewAddressWitnessPubKeyHash(pkHash, ec.GetConfig().Params)
if err != nil {
return inputList, err
}
// make scripthash
scripthash, err := addressToElectrumScripthash(addressWitnessPubKeyHash)
if err != nil {
return inputList, err
}
// ask electrum
listUnspent, err := node.GetListUnspent(ctx, scripthash)
if err != nil {
return inputList, err
}
for _, unspent := range listUnspent {
op, err := wallet.NewOutPointFromString(
fmt.Sprintf("%s:%d", unspent.TxHash, unspent.TxPos))
if err != nil {
return inputList, err
}
input := wallet.InputInfo{
Outpoint: op,
Height: unspent.Height,
Value: unspent.Value,
LinkedAddress: addressWitnessPubKeyHash,
PkScript: []byte{},
KeyPair: keyPair,
}
inputList = append(inputList, input)
}
return inputList, nil
}
func (ec *BtcElectrumClient) getUtxos(ctx context.Context, keyPair *btcutil.WIF) ([]wallet.InputInfo, error) {
inputList := make([]wallet.InputInfo, 0, 1)
// P2WSH Redeem - not yet implemented
p2wshInputList, err := ec.getWitnessScriptHashRedeemUtxos(ctx, keyPair)
if err != nil {
return inputList, err
}
fmt.Printf("found %d P2WSH Redemption utxos\n", len(p2wshInputList))
if len(p2wshInputList) > 0 {
inputList = append(inputList, p2wshInputList...)
}
// P2SH Redeem - not yet implemented
p2shInputList, err := ec.getScriptHashRedeemUtxos(ctx, keyPair)
if err != nil {
return inputList, err
}
fmt.Printf("found %d P2SH Redemption utxos\n", len(p2shInputList))
if len(p2shInputList) > 0 {
inputList = append(inputList, p2shInputList...)
}
// P2PK - including satoshi's coins maybe .. not yet implemented
p2pkInputList, err := ec.getPubKeyUtxos(ctx, keyPair)
if err != nil {
return inputList, err
}
fmt.Printf("found %d P2PK utxos\n", len(p2pkInputList))
if len(p2pkInputList) > 0 {
inputList = append(inputList, p2pkInputList...)
}
// P2PKH
p2pkhInputList, err := ec.getPubKeyHashUtxos(ctx, keyPair)
if err != nil {
return inputList, err
}
fmt.Printf("found %d P2PKH utxos\n", len(p2pkhInputList))
if len(p2pkhInputList) > 0 {
inputList = append(inputList, p2pkhInputList...)
}
// P2WPKH
p2wpkhInputList, err := ec.getWitnessPubKeyHashUtxos(ctx, keyPair)
if err != nil {
return inputList, err
}
fmt.Printf("found %d P2WPKH utxos\n", len(p2wpkhInputList))
if len(p2wpkhInputList) > 0 {
inputList = append(inputList, p2wpkhInputList...)
}
return inputList, nil
}
// ImportAndSweep imports privkeys from other wallets and builds a transaction that
// sweeps their funds into our wallet.
func (ec *BtcElectrumClient) ImportAndSweep(ctx context.Context, importedKeyPairs []string) error {
w := ec.GetWallet()
if w == nil {
return ErrNoWallet
}
if len(importedKeyPairs) <= 0 {
return errors.New("no keys")
}
var inputs []wallet.InputInfo = make([]wallet.InputInfo, 0)
for _, k := range importedKeyPairs {
wif, err := btcutil.DecodeWIF(k)
if err != nil {
fmt.Printf("warning cannot decode WIF from string: %s\n", k)
continue
}
inputsForKey, err := ec.getUtxos(ctx, wif)
if err != nil {
fmt.Printf("warning cannot get utxos for pubkey: %s\n",
hex.EncodeToString(wif.SerializePubKey()))
continue
}
if len(inputsForKey) <= 0 {
continue
}
inputs = append(inputs, inputsForKey...)
}
if len(inputs) <= 0 {
return errors.New("no inputs found")
}
// wallet sweep []tx
txs, err := w.SweepCoins(inputs, wallet.NORMAL, 50)
if err != nil {
return err
}
for _, tx := range txs {
var sweepBuf bytes.Buffer
sweepBuf.Grow(tx.SerializeSize())
tx.Serialize(&sweepBuf)
fmt.Printf("Sweep Tx: %x\n\n", sweepBuf.Bytes())
fmt.Printf("Sweep TxHash: (%v):\n", tx.TxHash())
}
return nil
}