2019-10-01 12:22:30 -03:00
|
|
|
package libwallet
|
|
|
|
|
|
|
|
import (
|
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"
|
|
|
|
"github.com/btcsuite/btcd/wire"
|
|
|
|
"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
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2020-11-09 10:05:29 -03:00
|
|
|
// CreateAddressV1 returns a P2PKH MuunAddress from a publicKey for use in TransactionSchemeV1
|
2019-10-01 12:22:30 -03:00
|
|
|
func CreateAddressV1(publicKey *HDPublicKey) (MuunAddress, error) {
|
2020-11-09 10:05:29 -03:00
|
|
|
return addresses.CreateAddressV1(&publicKey.key, publicKey.Path, publicKey.Network.network)
|
2019-10-01 12:22:30 -03:00
|
|
|
}
|
|
|
|
|
2020-11-09 10:05:29 -03:00
|
|
|
type coinV1 struct {
|
|
|
|
Network *chaincfg.Params
|
|
|
|
OutPoint wire.OutPoint
|
|
|
|
KeyPath string
|
|
|
|
}
|
2019-10-01 12:22:30 -03:00
|
|
|
|
2020-11-09 10:05:29 -03:00
|
|
|
func (c *coinV1) SignInput(index int, tx *wire.MsgTx, userKey *HDPrivateKey, _ *HDPublicKey) error {
|
|
|
|
userKey, err := userKey.DeriveTo(c.KeyPath)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "failed to derive user key")
|
|
|
|
}
|
2019-10-01 12:22:30 -03:00
|
|
|
|
2020-11-09 10:05:29 -03:00
|
|
|
sig, err := c.signature(index, tx, userKey)
|
2019-10-01 12:22:30 -03:00
|
|
|
if err != nil {
|
2020-11-09 10:05:29 -03:00
|
|
|
return errors.Wrapf(err, "failed to sign V1 input")
|
2019-10-01 12:22:30 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
builder := txscript.NewScriptBuilder()
|
|
|
|
builder.AddData(sig)
|
2020-11-09 10:05:29 -03:00
|
|
|
builder.AddData(userKey.PublicKey().Raw())
|
2019-10-01 12:22:30 -03:00
|
|
|
script, err := builder.Script()
|
|
|
|
if err != nil {
|
2020-11-09 10:05:29 -03:00
|
|
|
return errors.Wrapf(err, "failed to generate signing script")
|
2019-10-01 12:22:30 -03:00
|
|
|
}
|
|
|
|
|
2020-11-09 10:05:29 -03:00
|
|
|
txInput := tx.TxIn[index]
|
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 *coinV1) FullySignInput(index int, tx *wire.MsgTx, userKey, muunKey *HDPrivateKey) error {
|
|
|
|
return c.SignInput(index, tx, userKey, nil)
|
2019-10-01 12:22:30 -03:00
|
|
|
}
|
|
|
|
|
2020-11-09 10:05:29 -03:00
|
|
|
func (c *coinV1) createRedeemScript(publicKey *HDPublicKey) ([]byte, error) {
|
2019-10-01 12:22:30 -03:00
|
|
|
|
2020-11-09 10:05:29 -03:00
|
|
|
userAddress, err := btcutil.NewAddressPubKey(publicKey.Raw(), c.Network)
|
2019-10-01 12:22:30 -03:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "failed to generate address for user")
|
|
|
|
}
|
|
|
|
|
|
|
|
return txscript.PayToAddrScript(userAddress.AddressPubKeyHash())
|
|
|
|
}
|
|
|
|
|
2020-11-09 10:05:29 -03:00
|
|
|
func (c *coinV1) signature(index int, tx *wire.MsgTx, userKey *HDPrivateKey) ([]byte, error) {
|
2019-10-01 12:22:30 -03:00
|
|
|
|
2020-11-09 10:05:29 -03:00
|
|
|
redeemScript, err := c.createRedeemScript(userKey.PublicKey())
|
2019-10-01 12:22:30 -03:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "failed to build reedem script for signing")
|
|
|
|
}
|
|
|
|
|
2020-11-09 10:05:29 -03:00
|
|
|
privKey, err := userKey.key.ECPrivKey()
|
2019-10-01 12:22:30 -03:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "failed to produce EC priv key for signing")
|
|
|
|
}
|
|
|
|
|
|
|
|
sig, err := txscript.RawTxInSignature(tx, index, redeemScript, txscript.SigHashAll, privKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "failed to sign V1 input")
|
|
|
|
}
|
|
|
|
|
|
|
|
return sig, nil
|
|
|
|
}
|