63 lines
1.5 KiB
Go
Raw Normal View History

2019-12-16 17:59:11 -03:00
package libwallet
import (
2021-01-29 18:51:08 -03:00
"errors"
"fmt"
2020-11-09 10:05:29 -03:00
"github.com/btcsuite/btcd/chaincfg"
2019-12-16 17:59:11 -03:00
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcutil"
2020-11-09 10:05:29 -03:00
"github.com/muun/libwallet/swaps"
2019-12-16 17:59:11 -03:00
)
2020-11-09 10:05:29 -03:00
type coinSubmarineSwapV1 struct {
Network *chaincfg.Params
OutPoint wire.OutPoint
KeyPath string
Amount btcutil.Amount
RefundAddress string
PaymentHash256 []byte
ServerPublicKey []byte
LockTime int64
2019-12-16 17:59:11 -03:00
}
2020-11-09 10:05:29 -03:00
func (c *coinSubmarineSwapV1) SignInput(index int, tx *wire.MsgTx, userKey *HDPrivateKey,
_ *HDPublicKey) error {
2019-12-16 17:59:11 -03:00
2020-11-09 10:05:29 -03:00
userKey, err := userKey.DeriveTo(c.KeyPath)
2019-12-16 17:59:11 -03:00
if err != nil {
2021-01-29 18:51:08 -03:00
return fmt.Errorf("failed to derive user key: %w", err)
2019-12-16 17:59:11 -03:00
}
2020-11-09 10:05:29 -03:00
witnessScript, err := swaps.CreateWitnessScriptSubmarineSwapV1(
c.RefundAddress,
c.PaymentHash256,
c.ServerPublicKey,
c.LockTime,
userKey.Network.network,
)
2019-12-16 17:59:11 -03:00
if err != nil {
2020-11-09 10:05:29 -03:00
return err
2019-12-16 17:59:11 -03:00
}
redeemScript, err := createNonNativeSegwitRedeemScript(witnessScript)
if err != nil {
2021-01-29 18:51:08 -03:00
return fmt.Errorf("failed to build reedem script for signing: %w", err)
2019-12-16 17:59:11 -03:00
}
2020-11-09 10:05:29 -03:00
sig, err := signNonNativeSegwitInput(
index, tx, userKey, redeemScript, witnessScript, c.Amount)
2019-12-16 17:59:11 -03:00
if err != nil {
2020-11-09 10:05:29 -03:00
return err
2019-12-16 17:59:11 -03:00
}
2020-11-09 10:05:29 -03:00
txInput := tx.TxIn[index]
txInput.Witness = wire.TxWitness{sig, userKey.PublicKey().Raw(), witnessScript}
2019-12-16 17:59:11 -03:00
return nil
}
2020-11-09 10:05:29 -03:00
func (c *coinSubmarineSwapV1) FullySignInput(index int, tx *wire.MsgTx, userKey, muunKey *HDPrivateKey) error {
return errors.New("cannot fully sign submarine swap transactions")
}