2019-10-01 11:22:30 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/btcsuite/btcd/txscript"
|
|
|
|
"github.com/btcsuite/btcd/wire"
|
|
|
|
"github.com/btcsuite/btcutil"
|
|
|
|
|
|
|
|
"github.com/muun/libwallet"
|
|
|
|
)
|
|
|
|
|
2020-11-09 08:05:29 -05:00
|
|
|
func buildSweepTx(utxos []*RelevantTx, sweepAddress btcutil.Address, fee int64) []byte {
|
2019-10-01 11:22:30 -04:00
|
|
|
|
|
|
|
tx := wire.NewMsgTx(2)
|
|
|
|
value := int64(0)
|
|
|
|
|
|
|
|
for _, utxo := range utxos {
|
|
|
|
tx.AddTxIn(wire.NewTxIn(&utxo.Outpoint, []byte{}, [][]byte{}))
|
|
|
|
value += utxo.Satoshis
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println()
|
|
|
|
fmt.Printf("Total balance in satoshis: %v", value)
|
|
|
|
fmt.Println()
|
|
|
|
|
|
|
|
value -= fee
|
|
|
|
|
|
|
|
script, err := txscript.PayToAddrScript(sweepAddress)
|
|
|
|
if err != nil {
|
|
|
|
printError(err)
|
|
|
|
}
|
|
|
|
tx.AddTxOut(wire.NewTxOut(value, script))
|
|
|
|
|
|
|
|
writer := &bytes.Buffer{}
|
|
|
|
err = tx.Serialize(writer)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if fee != 0 {
|
|
|
|
readConfirmation(value, fee, sweepAddress.String())
|
|
|
|
}
|
|
|
|
|
2020-11-09 08:05:29 -05:00
|
|
|
return writer.Bytes()
|
2019-10-01 11:22:30 -04:00
|
|
|
}
|
|
|
|
|
2020-11-09 08:05:29 -05:00
|
|
|
func buildSignedTx(utxos []*RelevantTx, sweepTx []byte, userKey *libwallet.HDPrivateKey,
|
2019-10-01 11:22:30 -04:00
|
|
|
muunKey *libwallet.HDPrivateKey) (*wire.MsgTx, error) {
|
|
|
|
|
2020-11-09 08:05:29 -05:00
|
|
|
inputList := &libwallet.InputList{}
|
|
|
|
for _, utxo := range utxos {
|
|
|
|
inputList.Add(&input{
|
2019-10-01 11:22:30 -04:00
|
|
|
utxo,
|
|
|
|
[]byte{},
|
2020-11-09 08:05:29 -05:00
|
|
|
})
|
|
|
|
}
|
2019-10-01 11:22:30 -04:00
|
|
|
|
2020-11-09 08:05:29 -05:00
|
|
|
pstx, err := libwallet.NewPartiallySignedTransaction(inputList, sweepTx)
|
|
|
|
if err != nil {
|
|
|
|
printError(err)
|
2019-10-01 11:22:30 -04:00
|
|
|
}
|
|
|
|
|
2020-11-09 08:05:29 -05:00
|
|
|
signedTx, err := pstx.FullySign(userKey, muunKey)
|
2019-10-01 11:22:30 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
wireTx := wire.NewMsgTx(0)
|
|
|
|
wireTx.BtcDecode(bytes.NewReader(signedTx.Bytes), 0, wire.WitnessEncoding)
|
|
|
|
return wireTx, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type input struct {
|
|
|
|
tx *RelevantTx
|
|
|
|
muunSignature []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *input) OutPoint() libwallet.Outpoint {
|
|
|
|
return &outpoint{tx: i.tx}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *input) Address() libwallet.MuunAddress {
|
|
|
|
return i.tx.SigningDetails.Address
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *input) UserSignature() []byte {
|
|
|
|
return []byte{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *input) MuunSignature() []byte {
|
|
|
|
return i.muunSignature
|
|
|
|
}
|
|
|
|
|
2019-11-28 18:13:09 -05:00
|
|
|
func (i *input) SubmarineSwapV1() libwallet.InputSubmarineSwapV1 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *input) SubmarineSwapV2() libwallet.InputSubmarineSwapV2 {
|
2019-10-01 11:22:30 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-09 08:05:29 -05:00
|
|
|
func (i *input) IncomingSwap() libwallet.InputIncomingSwap {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-10-01 11:22:30 -04:00
|
|
|
type outpoint struct {
|
|
|
|
tx *RelevantTx
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *outpoint) TxId() []byte {
|
|
|
|
return o.tx.Outpoint.Hash.CloneBytes()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *outpoint) Index() int {
|
|
|
|
return int(o.tx.Outpoint.Index)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *outpoint) Amount() int64 {
|
|
|
|
return o.tx.Satoshis
|
|
|
|
}
|