Skip to content

Commit

Permalink
Release v0.1.3
Browse files Browse the repository at this point in the history
  • Loading branch information
mdb1 committed Nov 28, 2019
1 parent 61b339e commit e7b107b
Show file tree
Hide file tree
Showing 51 changed files with 1,366 additions and 1,951 deletions.
1 change: 0 additions & 1 deletion V2.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ func CreateAddressV2(userKey, muunKey *HDPublicKey) (MuunAddress, error) {
address: address.String(),
version: addressV2,
derivationPath: userKey.Path,
redeemScript: script,
}, nil
}

Expand Down
10 changes: 3 additions & 7 deletions V2_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package libwallet

import (
"encoding/hex"
"reflect"
"testing"
)
Expand All @@ -18,13 +17,10 @@ func Test_CreateAddressV2(t *testing.T) {
v2EncodedScript = "5221029fa5af7a34c142c1ce348b360abeb7de01df25b1d50129e58a67a6b846c9303b21025714f6b3670d4a38f5e2d6e8f239c9fc072543ce33dca54fcb4f4886a5cb87a652ae"
)

v2Script := make([]byte, 71)
hex.Decode(v2Script[:], []byte(v2EncodedScript))

baseMuunKey, _ := NewHDPublicKeyFromString(encodedMuunKey, basePath)
baseMuunKey, _ := NewHDPublicKeyFromString(encodedMuunKey, basePath, Regtest())
muunKey, _ := baseMuunKey.DeriveTo(addressPath)

baseUserKey, _ := NewHDPrivateKeyFromString(encodedUserKey, basePath)
baseUserKey, _ := NewHDPrivateKeyFromString(encodedUserKey, basePath, Regtest())
userKey, _ := baseUserKey.DeriveTo(addressPath)

type args struct {
Expand All @@ -39,7 +35,7 @@ func Test_CreateAddressV2(t *testing.T) {
}{
{name: "gen address",
args: args{userKey: userKey.PublicKey(), muunKey: muunKey},
want: &muunAddress{address: originAddress, derivationPath: addressPath, version: addressV2, redeemScript: v2Script}},
want: &muunAddress{address: originAddress, derivationPath: addressPath, version: addressV2}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
4 changes: 1 addition & 3 deletions V3.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ func CreateAddressV3(userKey, muunKey *HDPublicKey) (MuunAddress, error) {
address: address.EncodeAddress(),
version: addressV3,
derivationPath: userKey.Path,
redeemScript: redeemScript,
}, nil
}

Expand All @@ -48,7 +47,6 @@ func addUserSignatureInputV3(input Input, index int, tx *wire.MsgTx, privateKey
return nil, errors.Errorf("muun signature must be present")
}


witnessScript, err := createWitnessScriptV3(privateKey.PublicKey(), muunKey)
if err != nil {
return nil, err
Expand Down Expand Up @@ -77,7 +75,7 @@ func signInputV3(input Input, index int, tx *wire.MsgTx, userKey *HDPublicKey, m

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

return signNonNativeSegwitInput(input, index, tx, signingKey, redeemScript, witnessScript)
Expand Down
10 changes: 3 additions & 7 deletions V3_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package libwallet

import (
"encoding/hex"
"reflect"
"testing"
)
Expand All @@ -18,13 +17,10 @@ func Test_CreateAddressV3(t *testing.T) {
v3EncodedScript = "0020e1fbfbd395aff8b4087fee3e4488815ef659b559b3cd0d6800b5a591efd99f38"
)

v3Script := make([]byte, 34)
hex.Decode(v3Script[:], []byte(v3EncodedScript))

baseMuunKey, _ := NewHDPublicKeyFromString(baseCosigningPK, basePath)
baseMuunKey, _ := NewHDPublicKeyFromString(baseCosigningPK, basePath, Regtest())
muunKey, _ := baseMuunKey.DeriveTo(addressPath)

baseUserKey, _ := NewHDPublicKeyFromString(basePK, basePath)
baseUserKey, _ := NewHDPublicKeyFromString(basePK, basePath, Regtest())
userKey, _ := baseUserKey.DeriveTo(addressPath)

type args struct {
Expand All @@ -39,7 +35,7 @@ func Test_CreateAddressV3(t *testing.T) {
}{
{name: "gen address",
args: args{userKey: userKey, muunKey: muunKey},
want: &muunAddress{address: v3Address, derivationPath: addressPath, version: addressV3, redeemScript: v3Script}},
want: &muunAddress{address: v3Address, derivationPath: addressPath, version: addressV3}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
71 changes: 71 additions & 0 deletions V4.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package libwallet

import (
"crypto/sha256"

"github.com/btcsuite/btcutil"

"github.com/pkg/errors"

"github.com/btcsuite/btcd/wire"
)

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
}

func createWitnessScriptV4(userKey, muunKey *HDPublicKey) ([]byte, error) {
// createRedeemScriptV2 creates a valid script for V2, V3 and V4 schemes
return createRedeemScriptV2(userKey, muunKey)
}

func addUserSignatureInputV4(input Input, index int, tx *wire.MsgTx, privateKey *HDPrivateKey, muunKey *HDPublicKey) (*wire.TxIn, error) {

if len(input.MuunSignature()) == 0 {
return nil, errors.Errorf("muun signature must be present")
}

witnessScript, err := createWitnessScriptV4(privateKey.PublicKey(), muunKey)
if err != nil {
return nil, err
}

sig, err := signInputV4(input, index, tx, privateKey.PublicKey(), muunKey, privateKey)
if err != nil {
return nil, err
}

zeroByteArray := []byte{}

txInput := tx.TxIn[index]
txInput.Witness = wire.TxWitness{zeroByteArray, sig, input.MuunSignature(), witnessScript}

return txInput, nil
}

func signInputV4(input Input, index int, tx *wire.MsgTx, userKey *HDPublicKey, muunKey *HDPublicKey,
signingKey *HDPrivateKey) ([]byte, error) {

witnessScript, err := createWitnessScriptV4(userKey, muunKey)
if err != nil {
return nil, err
}

return signNativeSegwitInput(input, index, tx, signingKey, witnessScript)
}
50 changes: 50 additions & 0 deletions V4_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package libwallet

import (
"reflect"
"testing"
)

func TestCreateAddressV4(t *testing.T) {
const (
addressPath = "m/schema:1'/recovery:1'/external:1/2"

v4Address = "bcrt1qrs3vk4dzv70syck2qdz3g06tgckq4pftenuk5p77st9glnskpvtqe2tvvk"
basePK = "tpubDBf5wCeqg3KrLJiXaveDzD5JtFJ1ss9NVvFMx4RYS73SjwPEEawcAQ7V1B5DGM4gunWDeYNrnkc49sUaf7mS1wUKiJJQD6WEctExUQoLvrg"
baseCosigningPK = "tpubDB22PFkUaHoB7sgxh7exCivV5rAevVSzbB8WkFCCdbHq39r8xnYexiot4NGbi8PM6E1ySVeaHsoDeMYb6EMndpFrzVmuX8iQNExzwNpU61B"
basePath = "m/schema:1'/recovery:1'"
)

baseMuunKey, _ := NewHDPublicKeyFromString(baseCosigningPK, basePath, Regtest())
muunKey, _ := baseMuunKey.DeriveTo(addressPath)

baseUserKey, _ := NewHDPublicKeyFromString(basePK, basePath, Regtest())
userKey, _ := baseUserKey.DeriveTo(addressPath)

type args struct {
userKey *HDPublicKey
muunKey *HDPublicKey
}
tests := []struct {
name string
args args
want MuunAddress
wantErr bool
}{
{name: "gen bech32 address",
args: args{userKey: userKey, muunKey: muunKey},
want: &muunAddress{address: v4Address, derivationPath: addressPath, version: addressV4}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := CreateAddressV4(tt.args.userKey, tt.args.muunKey)
if (err != nil) != tt.wantErr {
t.Errorf("CreateAddressV4() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("CreateAddressV4() = %v, want %v", got, tt.want)
}
})
}
}
35 changes: 20 additions & 15 deletions address.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,27 @@ import (
"github.com/pkg/errors"
)

// These constants are here for clients usage.
const (
AddressVersionSwapsV1 = 101
AddressVersionSwapsV2 = 102
)

type AddressVersion int

const (
addressV1 AddressVersion = 1
addressV2 AddressVersion = 2
addressV3 AddressVersion = 3
addressSubmarineSwap AddressVersion = 101
addressV1 AddressVersion = 1
addressV2 AddressVersion = 2
addressV3 AddressVersion = 3
addressV4 AddressVersion = 4
addressSubmarineSwapV1 AddressVersion = AddressVersionSwapsV1
addressSubmarineSwapV2 AddressVersion = AddressVersionSwapsV2
)

type muunAddress struct {
version AddressVersion
derivationPath string
address string
redeemScript []byte
}

func newMuunAddress(version AddressVersion, userPublicKey, muunPublicKey *HDPublicKey) (MuunAddress, error) {
Expand All @@ -41,8 +48,12 @@ func newMuunAddress(version AddressVersion, userPublicKey, muunPublicKey *HDPubl
return CreateAddressV2(userPublicKey, muunPublicKey)
case addressV3:
return CreateAddressV3(userPublicKey, muunPublicKey)
case addressSubmarineSwap:
return CreateAddressSubmarineSwap(userPublicKey)
case addressV4:
return CreateAddressV4(userPublicKey, muunPublicKey)
case addressSubmarineSwapV1:
return nil, errors.Errorf("can't manually create a submarine swap v1 address")
case addressSubmarineSwapV2:
return nil, errors.Errorf("can't manually create a submarine swap v2 address")
}

return nil, errors.Errorf("unknown version %v", version)
Expand All @@ -60,10 +71,6 @@ func (a *muunAddress) Address() string {
return a.address
}

func (a *muunAddress) RedeemScript() []byte {
return a.redeemScript
}

// MuunPaymentURI is muun's uri struct
type MuunPaymentURI struct {
Address string
Expand Down Expand Up @@ -127,7 +134,7 @@ func GetPaymentURI(address string, network *Network) (*MuunPaymentURI, error) {
invoice, err := ParseInvoice(queryValues["lightning"][0], network)

if err == nil {
return &MuunPaymentURI{Invoice:invoice}, nil
return &MuunPaymentURI{Invoice: invoice}, nil
}
}

Expand Down Expand Up @@ -240,9 +247,7 @@ func getAddressFromScript(script []byte, network *Network) (string, error) {
func normalizeAddress(rawAddress string) string {
newAddress := rawAddress

if strings.Contains(newAddress, muunScheme) {
newAddress = strings.Replace(newAddress, muunScheme, bitcoinScheme, 1)
}
newAddress = strings.Replace(newAddress, muunScheme, bitcoinScheme, 1)

if !strings.Contains(newAddress, bitcoinScheme) {
newAddress = bitcoinScheme + rawAddress
Expand Down
56 changes: 0 additions & 56 deletions bip70.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e7b107b

Please sign in to comment.