Skip to content

Commit

Permalink
Release v0.7.0
Browse files Browse the repository at this point in the history
  • Loading branch information
slezica committed Jan 29, 2021
1 parent dd796f7 commit c7e3083
Show file tree
Hide file tree
Showing 33 changed files with 592 additions and 173 deletions.
17 changes: 9 additions & 8 deletions V1.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package libwallet

import (
"fmt"

"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcutil"
"github.com/muun/libwallet/addresses"
"github.com/pkg/errors"
)

// CreateAddressV1 returns a P2PKH MuunAddress from a publicKey for use in TransactionSchemeV1
Expand All @@ -23,20 +24,20 @@ type coinV1 struct {
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")
return fmt.Errorf("failed to derive user key: %w", err)
}

sig, err := c.signature(index, tx, userKey)
if err != nil {
return errors.Wrapf(err, "failed to sign V1 input")
return fmt.Errorf("failed to sign V1 input: %w", err)
}

builder := txscript.NewScriptBuilder()
builder.AddData(sig)
builder.AddData(userKey.PublicKey().Raw())
script, err := builder.Script()
if err != nil {
return errors.Wrapf(err, "failed to generate signing script")
return fmt.Errorf("failed to generate signing script: %w", err)
}

txInput := tx.TxIn[index]
Expand All @@ -52,7 +53,7 @@ func (c *coinV1) createRedeemScript(publicKey *HDPublicKey) ([]byte, error) {

userAddress, err := btcutil.NewAddressPubKey(publicKey.Raw(), c.Network)
if err != nil {
return nil, errors.Wrapf(err, "failed to generate address for user")
return nil, fmt.Errorf("failed to generate address for user: %w", err)
}

return txscript.PayToAddrScript(userAddress.AddressPubKeyHash())
Expand All @@ -62,17 +63,17 @@ func (c *coinV1) signature(index int, tx *wire.MsgTx, userKey *HDPrivateKey) ([]

redeemScript, err := c.createRedeemScript(userKey.PublicKey())
if err != nil {
return nil, errors.Wrapf(err, "failed to build reedem script for signing")
return nil, fmt.Errorf("failed to build reedem script for signing: %w", err)
}

privKey, err := userKey.key.ECPrivKey()
if err != nil {
return nil, errors.Wrapf(err, "failed to produce EC priv key for signing")
return nil, fmt.Errorf("failed to produce EC priv key for signing: %w", err)
}

sig, err := txscript.RawTxInSignature(tx, index, redeemScript, txscript.SigHashAll, privKey)
if err != nil {
return nil, errors.Wrapf(err, "failed to sign V1 input")
return nil, fmt.Errorf("failed to sign V1 input: %w", err)
}

return sig, nil
Expand Down
24 changes: 13 additions & 11 deletions V2.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package libwallet

import (
"errors"
"fmt"

"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/txscript"
"github.com/muun/libwallet/addresses"
"github.com/pkg/errors"

"github.com/btcsuite/btcd/wire"
)
Expand All @@ -24,23 +26,23 @@ type coinV2 struct {
func (c *coinV2) SignInput(index int, tx *wire.MsgTx, userKey *HDPrivateKey, muunKey *HDPublicKey) error {
userKey, err := userKey.DeriveTo(c.KeyPath)
if err != nil {
return errors.Wrapf(err, "failed to derive user key")
return fmt.Errorf("failed to derive user key: %w", err)
}

muunKey, err = muunKey.DeriveTo(c.KeyPath)
if err != nil {
return errors.Wrapf(err, "failed to derive muun key")
return fmt.Errorf("failed to derive muun key: %w", err)
}

if len(c.MuunSignature) == 0 {
return errors.Errorf("muun signature must be present")
return errors.New("muun signature must be present")
}

txInput := tx.TxIn[index]

redeemScript, err := createRedeemScriptV2(userKey.PublicKey(), muunKey)
if err != nil {
return errors.Wrapf(err, "failed to build reedem script for signing")
return fmt.Errorf("failed to build reedem script for signing: %w", err)
}

sig, err := c.signature(index, tx, userKey.PublicKey(), muunKey, userKey)
Expand All @@ -59,7 +61,7 @@ func (c *coinV2) SignInput(index int, tx *wire.MsgTx, userKey *HDPrivateKey, muu
builder.AddData(redeemScript)
script, err := builder.Script()
if err != nil {
return errors.Wrapf(err, "failed to generate signing script")
return fmt.Errorf("failed to generate signing script: %w", err)
}

txInput.SignatureScript = script
Expand All @@ -71,12 +73,12 @@ func (c *coinV2) FullySignInput(index int, tx *wire.MsgTx, userKey, muunKey *HDP

derivedUserKey, err := userKey.DeriveTo(c.KeyPath)
if err != nil {
return errors.Wrapf(err, "failed to derive user key")
return fmt.Errorf("failed to derive user key: %w", err)
}

derivedMuunKey, err := muunKey.DeriveTo(c.KeyPath)
if err != nil {
return errors.Wrapf(err, "failed to derive muun key")
return fmt.Errorf("failed to derive muun key: %w", err)
}

muunSignature, err := c.signature(index, tx, derivedUserKey.PublicKey(), derivedMuunKey.PublicKey(), derivedMuunKey)
Expand All @@ -92,17 +94,17 @@ func (c *coinV2) signature(index int, tx *wire.MsgTx, userKey, muunKey *HDPublic

redeemScript, err := createRedeemScriptV2(userKey, muunKey)
if err != nil {
return nil, errors.Wrapf(err, "failed to build reedem script for signing")
return nil, fmt.Errorf("failed to build reedem script for signing: %w", err)
}

privKey, err := signingKey.key.ECPrivKey()
if err != nil {
return nil, errors.Wrapf(err, "failed to produce EC priv key for signing")
return nil, fmt.Errorf("failed to produce EC priv key for signing: %w", err)
}

sig, err := txscript.RawTxInSignature(tx, index, redeemScript, txscript.SigHashAll, privKey)
if err != nil {
return nil, errors.Wrapf(err, "failed to sign V2 output")
return nil, fmt.Errorf("failed to sign V2 output: %w", err)
}

return sig, nil
Expand Down
17 changes: 9 additions & 8 deletions V3.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package libwallet

import (
"errors"
"fmt"

"github.com/btcsuite/btcutil"
"github.com/muun/libwallet/addresses"

"github.com/pkg/errors"

"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/wire"
)
Expand All @@ -26,16 +27,16 @@ func (c *coinV3) SignInput(index int, tx *wire.MsgTx, userKey *HDPrivateKey, muu

userKey, err := userKey.DeriveTo(c.KeyPath)
if err != nil {
return errors.Wrapf(err, "failed to derive user key")
return fmt.Errorf("failed to derive user key: %w", err)
}

muunKey, err = muunKey.DeriveTo(c.KeyPath)
if err != nil {
return errors.Wrapf(err, "failed to derive muun key")
return fmt.Errorf("failed to derive muun key: %w", err)
}

if len(c.MuunSignature) == 0 {
return errors.Errorf("muun signature must be present")
return errors.New("muun signature must be present")
}

witnessScript, err := createWitnessScriptV3(userKey.PublicKey(), muunKey)
Expand All @@ -60,12 +61,12 @@ func (c *coinV3) FullySignInput(index int, tx *wire.MsgTx, userKey, muunKey *HDP

derivedUserKey, err := userKey.DeriveTo(c.KeyPath)
if err != nil {
return errors.Wrapf(err, "failed to derive user key")
return fmt.Errorf("failed to derive user key: %w", err)
}

derivedMuunKey, err := muunKey.DeriveTo(c.KeyPath)
if err != nil {
return errors.Wrapf(err, "failed to derive muun key")
return fmt.Errorf("failed to derive muun key: %w", err)
}

muunSignature, err := c.signature(index, tx, derivedUserKey.PublicKey(), derivedMuunKey.PublicKey(), derivedMuunKey)
Expand Down Expand Up @@ -94,7 +95,7 @@ func (c *coinV3) signature(index int, tx *wire.MsgTx, userKey *HDPublicKey, muun

redeemScript, err := createRedeemScriptV3(userKey, muunKey)
if err != nil {
return nil, errors.Wrapf(err, "failed to build reedem script for signing")
return nil, fmt.Errorf("failed to build reedem script for signing: %w", err)
}

return signNonNativeSegwitInput(
Expand Down
14 changes: 7 additions & 7 deletions V4.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package libwallet

import (
"fmt"

"github.com/btcsuite/btcutil"
"github.com/muun/libwallet/addresses"

"github.com/pkg/errors"

"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/wire"
)
Expand All @@ -27,16 +27,16 @@ func (c *coinV4) SignInput(index int, tx *wire.MsgTx, userKey *HDPrivateKey, muu

userKey, err := userKey.DeriveTo(c.KeyPath)
if err != nil {
return errors.Wrapf(err, "failed to derive user key")
return fmt.Errorf("failed to derive user key: %w", err)
}

muunKey, err = muunKey.DeriveTo(c.KeyPath)
if err != nil {
return errors.Wrapf(err, "failed to derive muun key")
return fmt.Errorf("failed to derive muun key: %w", err)
}

if len(c.MuunSignature) == 0 {
return errors.Errorf("muun signature must be present")
return fmt.Errorf("muun signature must be present: %w", err)
}

witnessScript, err := createWitnessScriptV4(userKey.PublicKey(), muunKey)
Expand All @@ -61,12 +61,12 @@ func (c *coinV4) FullySignInput(index int, tx *wire.MsgTx, userKey, muunKey *HDP

derivedUserKey, err := userKey.DeriveTo(c.KeyPath)
if err != nil {
return errors.Wrapf(err, "failed to derive user key")
return fmt.Errorf("failed to derive user key: %w", err)
}

derivedMuunKey, err := muunKey.DeriveTo(c.KeyPath)
if err != nil {
return errors.Wrapf(err, "failed to derive muun key")
return fmt.Errorf("failed to derive muun key: %w", err)
}

muunSignature, err := c.signature(index, tx, derivedUserKey.PublicKey(), derivedMuunKey.PublicKey(), derivedMuunKey)
Expand Down
27 changes: 14 additions & 13 deletions address.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package libwallet

import (
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"strings"

"github.com/muun/libwallet/addresses"
"github.com/muun/libwallet/errors"

"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcutil"
"github.com/pkg/errors"
"google.golang.org/protobuf/proto"
)

Expand Down Expand Up @@ -44,11 +45,11 @@ func GetPaymentURI(rawInput string, network *Network) (*MuunPaymentURI, error) {

bitcoinUri, components := buildUriFromString(rawInput, bitcoinScheme)
if components == nil {
return nil, errors.Errorf("failed to parse uri %v", rawInput)
return nil, errors.Errorf(ErrInvalidURI, "failed to parse uri %v", rawInput)
}

if components.Scheme != "bitcoin" {
return nil, errors.New("Invalid scheme")
return nil, errors.New(ErrInvalidURI, "Invalid scheme")
}

base58Address := components.Opaque
Expand All @@ -61,7 +62,7 @@ func GetPaymentURI(rawInput string, network *Network) (*MuunPaymentURI, error) {

queryValues, err := url.ParseQuery(components.RawQuery)
if err != nil {
return nil, errors.Wrapf(err, "Couldnt parse query")
return nil, errors.Errorf(ErrInvalidURI, "Couldn't parse query: %v", err)
}

var label, message, amount string
Expand Down Expand Up @@ -110,11 +111,11 @@ func GetPaymentURI(rawInput string, network *Network) (*MuunPaymentURI, error) {
// Bech32 check
validatedBase58Address, err := btcutil.DecodeAddress(base58Address, network.network)
if err != nil {
return nil, err
return nil, fmt.Errorf("invalid address: %w", err)
}

if !validatedBase58Address.IsForNet(network.network) {
return nil, errors.Errorf("Network mismatch")
return nil, errors.New(ErrInvalidURI, "Network mismatch")
}

return &MuunPaymentURI{
Expand All @@ -131,43 +132,43 @@ func GetPaymentURI(rawInput string, network *Network) (*MuunPaymentURI, error) {
func DoPaymentRequestCall(url string, network *Network) (*MuunPaymentURI, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, errors.Wrapf(err, "Failed to create request to: %s", url)
return nil, fmt.Errorf("failed to create request to: %s", url)
}

req.Header.Set("Accept", "application/bitcoin-paymentrequest")

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, errors.Wrapf(err, "Failed to make request to: %s", url)
return nil, errors.Errorf(ErrNetwork, "failed to make request to: %s", url)
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, errors.Wrapf(err, "Failed to read body response")
return nil, errors.Errorf(ErrNetwork, "Failed to read body response: %w", err)
}

payReq := &PaymentRequest{}
err = proto.Unmarshal(body, payReq)
if err != nil {
return nil, errors.Wrapf(err, "Failed to Unmarshall paymentRequest")
return nil, fmt.Errorf("failed to unmarshal payment request: %w", err)
}

payDetails := &PaymentDetails{}

err = proto.Unmarshal(payReq.SerializedPaymentDetails, payDetails)
if err != nil {
return nil, errors.Wrapf(err, "Failed to Unmarshall paymentDetails")
return nil, fmt.Errorf("failed to unmarshall payment details: %w", err)
}

if len(payDetails.Outputs) == 0 {
return nil, errors.New("No outputs provided")
return nil, fmt.Errorf("no outputs provided")
}

address, err := getAddressFromScript(payDetails.Outputs[0].Script, network)
if err != nil {
return nil, errors.Wrapf(err, "Failed to get address")
return nil, fmt.Errorf("failed to get address: %w", err)
}

return &MuunPaymentURI{
Expand Down
2 changes: 1 addition & 1 deletion address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ func TestDoPaymentRequestCall(t *testing.T) {
serializedPaymentDetails, _ := proto.Marshal(&PaymentDetails{
Network: "test",
Outputs: []*Output{
&Output{
{
Script: script,
Amount: 2500,
},
Expand Down
Loading

0 comments on commit c7e3083

Please sign in to comment.