67 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 coinSubmarineSwapV2 struct {
Network *chaincfg.Params
OutPoint wire.OutPoint
Amount btcutil.Amount
KeyPath string
PaymentHash256 []byte
UserPublicKey []byte
MuunPublicKey []byte
ServerPublicKey []byte
BlocksForExpiration int64
ServerSignature []byte
}
func (c *coinSubmarineSwapV2) 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)
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
if len(c.ServerSignature) == 0 {
2021-01-29 18:51:08 -03:00
return errors.New("swap server must provide signature")
2019-12-16 17:59:11 -03:00
}
2020-11-09 10:05:29 -03:00
witnessScript, err := swaps.CreateWitnessScriptSubmarineSwapV2(
c.PaymentHash256,
c.UserPublicKey,
c.MuunPublicKey,
c.ServerPublicKey,
c.BlocksForExpiration)
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
sig, err := signNativeSegwitInput(
index, tx, userKey, 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
}
txInput := tx.TxIn[index]
txInput.Witness = wire.TxWitness{
sig,
2020-11-09 10:05:29 -03:00
c.ServerSignature,
2019-12-16 17:59:11 -03:00
witnessScript,
}
2020-11-09 10:05:29 -03:00
return nil
2019-12-16 17:59:11 -03:00
}
2020-11-09 10:05:29 -03:00
func (c *coinSubmarineSwapV2) FullySignInput(index int, tx *wire.MsgTx, userKey, muunKey *HDPrivateKey) error {
return errors.New("cannot fully sign submarine swap transactions")
2019-12-16 17:59:11 -03:00
}