forked from muun/recovery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
recovery_tool.go
144 lines (112 loc) · 2.92 KB
/
recovery_tool.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
package main
import (
"bytes"
"encoding/hex"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcutil"
"github.com/muun/libwallet"
"github.com/muun/libwallet/btcsuitew/txscriptw"
"github.com/muun/recovery/scanner"
)
func buildSweepTx(utxos []*scanner.Utxo, sweepAddress btcutil.Address, fee int64) ([]byte, error) {
tx := wire.NewMsgTx(2)
value := int64(0)
for _, utxo := range utxos {
chainHash, err := chainhash.NewHashFromStr(utxo.TxID)
if err != nil {
return nil, err
}
outpoint := wire.OutPoint{
Hash: *chainHash,
Index: uint32(utxo.OutputIndex),
}
tx.AddTxIn(wire.NewTxIn(&outpoint, []byte{}, [][]byte{}))
value += utxo.Amount
}
value -= fee
script, err := txscriptw.PayToAddrScript(sweepAddress)
if err != nil {
return nil, err
}
tx.AddTxOut(wire.NewTxOut(value, script))
writer := &bytes.Buffer{}
err = tx.Serialize(writer)
if err != nil {
return nil, err
}
if fee != 0 {
readConfirmation(value, fee, sweepAddress.String())
}
return writer.Bytes(), nil
}
func buildSignedTx(utxos []*scanner.Utxo, sweepTx []byte, userKey *libwallet.HDPrivateKey,
muunKey *libwallet.HDPrivateKey) (*wire.MsgTx, error) {
inputList := &libwallet.InputList{}
for _, utxo := range utxos {
inputList.Add(&input{
utxo,
[]byte{},
})
}
nonces := libwallet.GenerateMusigNonces(len(utxos))
pstx, err := libwallet.NewPartiallySignedTransaction(inputList, sweepTx, nonces)
if err != nil {
return nil, err
}
signedTx, err := pstx.FullySign(userKey, muunKey)
if err != nil {
return nil, err
}
wireTx := wire.NewMsgTx(0)
wireTx.BtcDecode(bytes.NewReader(signedTx.Bytes), 0, wire.WitnessEncoding)
return wireTx, nil
}
// input is a minimal type that implements libwallet.Input
type input struct {
utxo *scanner.Utxo
muunSignature []byte
}
func (i *input) OutPoint() libwallet.Outpoint {
return &outpoint{utxo: i.utxo}
}
func (i *input) Address() libwallet.MuunAddress {
return i.utxo.Address
}
func (i *input) UserSignature() []byte {
return []byte{}
}
func (i *input) MuunSignature() []byte {
return i.muunSignature
}
func (i *input) SubmarineSwapV1() libwallet.InputSubmarineSwapV1 {
return nil
}
func (i *input) SubmarineSwapV2() libwallet.InputSubmarineSwapV2 {
return nil
}
func (i *input) IncomingSwap() libwallet.InputIncomingSwap {
return nil
}
func (i *input) MuunPublicNonce() []byte {
// Will always be nil in the context of the tool
// Look at coinV5.signFirstWith for the reason why.
return nil
}
// outpoint is a minimal type that implements libwallet.Outpoint
type outpoint struct {
utxo *scanner.Utxo
}
func (o *outpoint) TxId() []byte {
raw, err := hex.DecodeString(o.utxo.TxID)
if err != nil {
panic(err) // we wrote this hex value ourselves, no input from anywhere else
}
return raw
}
func (o *outpoint) Index() int {
return o.utxo.OutputIndex
}
func (o *outpoint) Amount() int64 {
return o.utxo.Amount
}