Release v0.3.0

This commit is contained in:
Manu Herrera
2020-11-09 10:05:29 -03:00
parent 4e9aa7a3c5
commit 8107c4478b
1265 changed files with 440488 additions and 107809 deletions

View File

@@ -1,65 +1,83 @@
package libwallet
import (
"crypto/sha256"
"github.com/btcsuite/btcutil"
"github.com/muun/libwallet/addresses"
"github.com/pkg/errors"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/wire"
)
// CreateAddressV4 returns a P2WSH MuunAddress from a user HD-pubkey and a Muun co-signing HD-pubkey.
func CreateAddressV4(userKey, muunKey *HDPublicKey) (MuunAddress, error) {
witnessScript, err := createWitnessScriptV4(userKey, muunKey)
if err != nil {
return nil, errors.Wrapf(err, "failed to generate witness script v4")
}
witnessScript256 := sha256.Sum256(witnessScript)
address, err := btcutil.NewAddressWitnessScriptHash(witnessScript256[:], userKey.Network.network)
if err != nil {
return nil, err
}
return &muunAddress{
address: address.EncodeAddress(),
version: addressV4,
derivationPath: userKey.Path,
}, nil
return addresses.CreateAddressV4(&userKey.key, &muunKey.key, userKey.Path, userKey.Network.network)
}
func createWitnessScriptV4(userKey, muunKey *HDPublicKey) ([]byte, error) {
// createRedeemScriptV2 creates a valid script for V2, V3 and V4 schemes
return createRedeemScriptV2(userKey, muunKey)
type coinV4 struct {
Network *chaincfg.Params
OutPoint wire.OutPoint
KeyPath string
Amount btcutil.Amount
MuunSignature []byte
}
func addUserSignatureInputV4(input Input, index int, tx *wire.MsgTx, privateKey *HDPrivateKey, muunKey *HDPublicKey) (*wire.TxIn, error) {
func (c *coinV4) SignInput(index int, tx *wire.MsgTx, userKey *HDPrivateKey, muunKey *HDPublicKey) error {
if len(input.MuunSignature()) == 0 {
return nil, errors.Errorf("muun signature must be present")
userKey, err := userKey.DeriveTo(c.KeyPath)
if err != nil {
return errors.Wrapf(err, "failed to derive user key")
}
witnessScript, err := createWitnessScriptV4(privateKey.PublicKey(), muunKey)
muunKey, err = muunKey.DeriveTo(c.KeyPath)
if err != nil {
return nil, err
return errors.Wrapf(err, "failed to derive muun key")
}
sig, err := signInputV4(input, index, tx, privateKey.PublicKey(), muunKey, privateKey)
if len(c.MuunSignature) == 0 {
return errors.Errorf("muun signature must be present")
}
witnessScript, err := createWitnessScriptV4(userKey.PublicKey(), muunKey)
if err != nil {
return nil, err
return err
}
sig, err := c.signature(index, tx, userKey.PublicKey(), muunKey, userKey)
if err != nil {
return err
}
zeroByteArray := []byte{}
txInput := tx.TxIn[index]
txInput.Witness = wire.TxWitness{zeroByteArray, sig, input.MuunSignature(), witnessScript}
txInput.Witness = wire.TxWitness{zeroByteArray, sig, c.MuunSignature, witnessScript}
return txInput, nil
return nil
}
func signInputV4(input Input, index int, tx *wire.MsgTx, userKey *HDPublicKey, muunKey *HDPublicKey,
func (c *coinV4) FullySignInput(index int, tx *wire.MsgTx, userKey, muunKey *HDPrivateKey) error {
derivedUserKey, err := userKey.DeriveTo(c.KeyPath)
if err != nil {
return errors.Wrapf(err, "failed to derive user key")
}
derivedMuunKey, err := muunKey.DeriveTo(c.KeyPath)
if err != nil {
return errors.Wrapf(err, "failed to derive muun key")
}
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,
signingKey *HDPrivateKey) ([]byte, error) {
witnessScript, err := createWitnessScriptV4(userKey, muunKey)
@@ -67,5 +85,10 @@ func signInputV4(input Input, index int, tx *wire.MsgTx, userKey *HDPublicKey, m
return nil, err
}
return signNativeSegwitInput(input, index, tx, signingKey, witnessScript)
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)
}