2019-12-16 17:59:11 -03:00
|
|
|
package libwallet
|
|
|
|
|
|
|
|
import (
|
2021-01-29 18:51:08 -03:00
|
|
|
"fmt"
|
|
|
|
|
2019-12-16 17:59:11 -03:00
|
|
|
"github.com/btcsuite/btcutil"
|
2020-11-09 10:05:29 -03:00
|
|
|
"github.com/muun/libwallet/addresses"
|
2019-12-16 17:59:11 -03:00
|
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
2020-11-09 10:05:29 -03:00
|
|
|
// CreateAddressV4 returns a P2WSH MuunAddress from a user HD-pubkey and a Muun co-signing HD-pubkey.
|
2019-12-16 17:59:11 -03:00
|
|
|
func CreateAddressV4(userKey, muunKey *HDPublicKey) (MuunAddress, error) {
|
2020-11-09 10:05:29 -03:00
|
|
|
return addresses.CreateAddressV4(&userKey.key, &muunKey.key, userKey.Path, userKey.Network.network)
|
|
|
|
}
|
2019-12-16 17:59:11 -03:00
|
|
|
|
2020-11-09 10:05:29 -03:00
|
|
|
type coinV4 struct {
|
|
|
|
Network *chaincfg.Params
|
|
|
|
OutPoint wire.OutPoint
|
|
|
|
KeyPath string
|
|
|
|
Amount btcutil.Amount
|
|
|
|
MuunSignature []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *coinV4) SignInput(index int, tx *wire.MsgTx, userKey *HDPrivateKey, muunKey *HDPublicKey) error {
|
|
|
|
|
|
|
|
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
|
|
|
muunKey, err = muunKey.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 muun key: %w", err)
|
2019-12-16 17:59:11 -03:00
|
|
|
}
|
|
|
|
|
2020-11-09 10:05:29 -03:00
|
|
|
if len(c.MuunSignature) == 0 {
|
2021-01-29 18:51:08 -03:00
|
|
|
return fmt.Errorf("muun signature must be present: %w", err)
|
2019-12-16 17:59:11 -03:00
|
|
|
}
|
|
|
|
|
2020-11-09 10:05:29 -03:00
|
|
|
witnessScript, err := createWitnessScriptV4(userKey.PublicKey(), muunKey)
|
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 := c.signature(index, tx, userKey.PublicKey(), muunKey, userKey)
|
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
|
|
|
}
|
|
|
|
|
|
|
|
zeroByteArray := []byte{}
|
|
|
|
|
|
|
|
txInput := tx.TxIn[index]
|
2020-11-09 10:05:29 -03:00
|
|
|
txInput.Witness = wire.TxWitness{zeroByteArray, sig, c.MuunSignature, witnessScript}
|
2019-12-16 17:59:11 -03:00
|
|
|
|
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 *coinV4) 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 *coinV4) signature(index int, tx *wire.MsgTx, userKey *HDPublicKey, muunKey *HDPublicKey,
|
2019-12-16 17:59:11 -03:00
|
|
|
signingKey *HDPrivateKey) ([]byte, error) {
|
|
|
|
|
|
|
|
witnessScript, err := createWitnessScriptV4(userKey, muunKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-11-09 10:05:29 -03:00
|
|
|
return signNativeSegwitInput(
|
|
|
|
index, tx, signingKey, witnessScript, c.Amount)
|
|
|
|
}
|
|
|
|
|
|
|
|
func createWitnessScriptV4(userKey, muunKey *HDPublicKey) ([]byte, error) {
|
|
|
|
return addresses.CreateWitnessScriptV4(&userKey.key, &muunKey.key, userKey.Network.network)
|
2019-12-16 17:59:11 -03:00
|
|
|
}
|