116 lines
3.1 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"
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/txscript"
2020-11-09 10:05:29 -03:00
"github.com/muun/libwallet/addresses"
2019-10-01 12:22:30 -03:00
"github.com/btcsuite/btcd/wire"
)
func CreateAddressV2(userKey, muunKey *HDPublicKey) (MuunAddress, error) {
2020-11-09 10:05:29 -03:00
// TODO: check both paths match?
return addresses.CreateAddressV2(&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 coinV2 struct {
Network *chaincfg.Params
OutPoint wire.OutPoint
KeyPath string
MuunSignature []byte
}
2019-10-01 12:22:30 -03:00
2020-11-09 10:05:29 -03:00
func (c *coinV2) 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")
2019-10-01 12:22:30 -03:00
}
txInput := tx.TxIn[index]
2020-11-09 10:05:29 -03:00
redeemScript, err := createRedeemScriptV2(userKey.PublicKey(), muunKey)
2019-10-01 12:22:30 -03:00
if err != nil {
2021-01-29 18:51:08 -03:00
return 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
sig, err := c.signature(index, tx, userKey.PublicKey(), muunKey, userKey)
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
}
// This is a standard 2 of 2 multisig script
// 0 because of a bug in bitcoind
// Then the 2 sigs: first the users and then muuns
// Last, the script that contains the two pub keys and OP_CHECKMULTISIG
builder := txscript.NewScriptBuilder()
builder.AddInt64(0)
builder.AddData(sig)
2020-11-09 10:05:29 -03:00
builder.AddData(c.MuunSignature)
2019-10-01 12:22:30 -03:00
builder.AddData(redeemScript)
script, err := builder.Script()
if err != nil {
2021-01-29 18:51:08 -03:00
return fmt.Errorf("failed to generate signing script: %w", err)
2019-10-01 12:22:30 -03:00
}
txInput.SignatureScript = script
2020-11-09 10:05:29 -03:00
return nil
2019-10-01 12:22:30 -03:00
}
2020-11-09 10:05:29 -03:00
func (c *coinV2) FullySignInput(index int, tx *wire.MsgTx, userKey, muunKey *HDPrivateKey) error {
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)
2020-11-09 10:05:29 -03:00
}
derivedMuunKey, err := muunKey.DeriveTo(c.KeyPath)
if err != nil {
2021-01-29 18:51:08 -03:00
return fmt.Errorf("failed to derive muun key: %w", err)
2020-11-09 10:05:29 -03:00
}
muunSignature, err := c.signature(index, tx, derivedUserKey.PublicKey(), derivedMuunKey.PublicKey(), derivedMuunKey)
if err != nil {
return err
}
c.MuunSignature = muunSignature
return c.SignInput(index, tx, userKey, muunKey.PublicKey())
}
func (c *coinV2) signature(index int, tx *wire.MsgTx, userKey, muunKey *HDPublicKey,
2019-10-01 12:22:30 -03:00
signingKey *HDPrivateKey) ([]byte, error) {
redeemScript, err := createRedeemScriptV2(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
}
privKey, err := signingKey.key.ECPrivKey()
if err != nil {
2021-01-29 18:51:08 -03:00
return nil, fmt.Errorf("failed to produce EC priv key for signing: %w", err)
2019-10-01 12:22:30 -03:00
}
sig, err := txscript.RawTxInSignature(tx, index, redeemScript, txscript.SigHashAll, privKey)
if err != nil {
2021-01-29 18:51:08 -03:00
return nil, fmt.Errorf("failed to sign V2 output: %w", err)
2019-10-01 12:22:30 -03:00
}
return sig, nil
}
2020-11-09 10:05:29 -03:00
func createRedeemScriptV2(userKey, muunKey *HDPublicKey) ([]byte, error) {
return addresses.CreateRedeemScriptV2(&userKey.key, &muunKey.key, userKey.Network.network)
}