104 lines
2.7 KiB
Go
Raw Normal View History

2019-10-01 12:22:30 -03:00
package libwallet
import (
2021-01-29 18:51:08 -03:00
"errors"
"fmt"
2019-10-01 12:22:30 -03:00
"github.com/btcsuite/btcutil"
2020-11-09 10:05:29 -03:00
"github.com/muun/libwallet/addresses"
2019-10-01 12:22:30 -03:00
2020-11-09 10:05:29 -03:00
"github.com/btcsuite/btcd/chaincfg"
2019-10-01 12:22:30 -03:00
"github.com/btcsuite/btcd/wire"
)
func CreateAddressV3(userKey, muunKey *HDPublicKey) (MuunAddress, error) {
2020-11-09 10:05:29 -03:00
return addresses.CreateAddressV3(&userKey.key, &muunKey.key, userKey.Path, userKey.Network.network)
}
2019-10-01 12:22:30 -03:00
2020-11-09 10:05:29 -03:00
type coinV3 struct {
Network *chaincfg.Params
OutPoint wire.OutPoint
KeyPath string
Amount btcutil.Amount
MuunSignature []byte
}
func (c *coinV3) SignInput(index int, tx *wire.MsgTx, userKey *HDPrivateKey, muunKey *HDPublicKey) error {
userKey, err := userKey.DeriveTo(c.KeyPath)
2019-10-01 12:22:30 -03:00
if err != nil {
2021-01-29 18:51:08 -03:00
return fmt.Errorf("failed to derive user key: %w", err)
2019-10-01 12:22:30 -03:00
}
2020-11-09 10:05:29 -03:00
muunKey, err = muunKey.DeriveTo(c.KeyPath)
2019-10-01 12:22:30 -03:00
if err != nil {
2021-01-29 18:51:08 -03:00
return fmt.Errorf("failed to derive muun key: %w", err)
2019-10-01 12:22:30 -03:00
}
2020-11-09 10:05:29 -03:00
if len(c.MuunSignature) == 0 {
2021-01-29 18:51:08 -03:00
return errors.New("muun signature must be present")
2020-11-09 10:05:29 -03:00
}
2019-10-01 12:22:30 -03:00
2020-11-09 10:05:29 -03:00
witnessScript, err := createWitnessScriptV3(userKey.PublicKey(), muunKey)
2019-10-01 12:22:30 -03:00
if err != nil {
2020-11-09 10:05:29 -03:00
return err
2019-10-01 12:22:30 -03:00
}
2020-11-09 10:05:29 -03:00
sig, err := c.signature(index, tx, userKey.PublicKey(), muunKey, userKey)
if err != nil {
return err
}
2019-10-01 12:22:30 -03:00
2020-11-09 10:05:29 -03:00
zeroByteArray := []byte{}
txInput := tx.TxIn[index]
txInput.Witness = wire.TxWitness{zeroByteArray, sig, c.MuunSignature, witnessScript}
return nil
2019-10-01 12:22:30 -03:00
}
2020-11-09 10:05:29 -03:00
func (c *coinV3) FullySignInput(index int, tx *wire.MsgTx, userKey, muunKey *HDPrivateKey) error {
2019-10-01 12:22:30 -03:00
2020-11-09 10:05:29 -03:00
derivedUserKey, 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-10-01 12:22:30 -03:00
}
2020-11-09 10:05:29 -03:00
derivedMuunKey, err := muunKey.DeriveTo(c.KeyPath)
2019-10-01 12:22:30 -03:00
if err != nil {
2021-01-29 18:51:08 -03:00
return fmt.Errorf("failed to derive muun key: %w", err)
2019-10-01 12:22:30 -03:00
}
2020-11-09 10:05:29 -03:00
muunSignature, err := c.signature(index, tx, derivedUserKey.PublicKey(), derivedMuunKey.PublicKey(), derivedMuunKey)
2019-10-01 12:22:30 -03:00
if err != nil {
2020-11-09 10:05:29 -03:00
return err
2019-10-01 12:22:30 -03:00
}
2020-11-09 10:05:29 -03:00
c.MuunSignature = muunSignature
return c.SignInput(index, tx, userKey, muunKey.PublicKey())
}
2019-10-01 12:22:30 -03:00
2020-11-09 10:05:29 -03:00
func createRedeemScriptV3(userKey, muunKey *HDPublicKey) ([]byte, error) {
return addresses.CreateRedeemScriptV3(&userKey.key, &muunKey.key, userKey.Network.network)
}
2019-10-01 12:22:30 -03:00
2020-11-09 10:05:29 -03:00
func createWitnessScriptV3(userKey, muunKey *HDPublicKey) ([]byte, error) {
return addresses.CreateWitnessScriptV3(&userKey.key, &muunKey.key, userKey.Network.network)
2019-10-01 12:22:30 -03:00
}
2020-11-09 10:05:29 -03:00
func (c *coinV3) signature(index int, tx *wire.MsgTx, userKey *HDPublicKey, muunKey *HDPublicKey,
2019-10-01 12:22:30 -03:00
signingKey *HDPrivateKey) ([]byte, error) {
witnessScript, err := createWitnessScriptV3(userKey, muunKey)
if err != nil {
return nil, err
}
redeemScript, err := createRedeemScriptV3(userKey, muunKey)
if err != nil {
2021-01-29 18:51:08 -03:00
return nil, fmt.Errorf("failed to build reedem script for signing: %w", err)
2019-10-01 12:22:30 -03:00
}
2020-11-09 10:05:29 -03:00
return signNonNativeSegwitInput(
index, tx, signingKey, redeemScript, witnessScript, c.Amount)
2019-10-01 12:22:30 -03:00
}