From 33ea2cc803c52fb1607b2a66167ca3f9f7dd0b91 Mon Sep 17 00:00:00 2001 From: Manu Herrera Date: Tue, 17 Dec 2019 12:35:15 -0300 Subject: [PATCH] Release v0.1.5 --- address.go | 26 +++++++++++------------ address_test.go | 6 +++--- challenge_keys.go | 4 ++-- challenge_public_key.go | 4 ++-- encodings.go | 16 ++++++++++++++ encodings_test.go | 46 +++++++++++++++++++++++++++++++++++++++++ go.mod | 2 +- go.sum | 1 + invoice.go | 4 ++-- invoice_test.go | 8 +++---- 10 files changed, 90 insertions(+), 27 deletions(-) create mode 100644 encodings.go create mode 100644 encodings_test.go diff --git a/address.go b/address.go index b047a87..05183b9 100644 --- a/address.go +++ b/address.go @@ -89,12 +89,12 @@ const ( muunScheme = "muun:" ) -// GetPaymentURI builds a MuunPaymentURI from an address and a network -func GetPaymentURI(address string, network *Network) (*MuunPaymentURI, error) { +// GetPaymentURI builds a MuunPaymentURI from text (Bitcoin Uri, Muun Uri or address) and a network +func GetPaymentURI(rawInput string, network *Network) (*MuunPaymentURI, error) { - uriAddress := normalizeAddress(address) + bitcoinUri := buildUriFromString(rawInput) - components, err := url.Parse(uriAddress) + components, err := url.Parse(bitcoinUri) if err != nil { return nil, err } @@ -146,7 +146,7 @@ func GetPaymentURI(address string, network *Network) (*MuunPaymentURI, error) { Label: label, Message: message, Amount: amount, - URI: uriAddress, + URI: bitcoinUri, BIP70Url: queryValues["r"][0], }, nil } @@ -154,7 +154,7 @@ func GetPaymentURI(address string, network *Network) (*MuunPaymentURI, error) { Label: label, Message: message, Amount: amount, - URI: uriAddress, + URI: bitcoinUri, BIP70Url: queryValues["r"][0], }, nil } @@ -174,7 +174,7 @@ func GetPaymentURI(address string, network *Network) (*MuunPaymentURI, error) { Label: label, Message: message, Amount: amount, - URI: uriAddress, + URI: bitcoinUri, }, nil } @@ -244,14 +244,14 @@ func getAddressFromScript(script []byte, network *Network) (string, error) { return address.String(), nil } -func normalizeAddress(rawAddress string) string { - newAddress := rawAddress +func buildUriFromString(rawInput string) string { + newUri := rawInput - newAddress = strings.Replace(newAddress, muunScheme, bitcoinScheme, 1) + newUri = strings.Replace(newUri, muunScheme, bitcoinScheme, 1) - if !strings.Contains(newAddress, bitcoinScheme) { - newAddress = bitcoinScheme + rawAddress + if !strings.Contains(newUri, bitcoinScheme) { + newUri = bitcoinScheme + rawInput } - return newAddress + return newUri } diff --git a/address_test.go b/address_test.go index 3fad7ea..0aec6a7 100644 --- a/address_test.go +++ b/address_test.go @@ -29,7 +29,7 @@ func TestGetPaymentURI(t *testing.T) { ) invoiceDestination, _ := hex.DecodeString(invoiceDestinationHex) - invoicePaymentHash := [32]byte{} + invoicePaymentHash := make([]byte, 32) hex.Decode(invoicePaymentHash[:], []byte(invoiceHashHex)) type args struct { @@ -217,8 +217,8 @@ func Test_normalizeAddress(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := normalizeAddress(tt.args.rawAddress); got != tt.want { - t.Errorf("normalizeAddress() = %v, want %v", got, tt.want) + if got := buildUriFromString(tt.args.rawAddress); got != tt.want { + t.Errorf("buildUriFromString() = %v, want %v", got, tt.want) } }) } diff --git a/challenge_keys.go b/challenge_keys.go index 6a769bb..db00be6 100644 --- a/challenge_keys.go +++ b/challenge_keys.go @@ -99,9 +99,9 @@ func (k *ChallengePrivateKey) DecryptKey(encryptedKey string, network *Network) iv := rawPubEph[len(rawPubEph)-aes.BlockSize:] - block, err := aes.NewCipher(sharedSecret.Bytes()) + block, err := aes.NewCipher(paddedSerializeBigInt(32, sharedSecret)) if err != nil { - return nil, err + return nil, errors.Wrapf(err, "challenge_key: failed to generate encryption key") } plaintext := make([]byte, len(ciphertext)) diff --git a/challenge_public_key.go b/challenge_public_key.go index 4a63125..bf578b3 100644 --- a/challenge_public_key.go +++ b/challenge_public_key.go @@ -51,9 +51,9 @@ func (k *ChallengePublicKey) EncryptKey(privKey *HDPrivateKey, recoveryCodeSalt serializedPubkey := privEph.PubKey().SerializeCompressed() iv := serializedPubkey[len(serializedPubkey)-aes.BlockSize:] - block, err := aes.NewCipher(sharedSecret.Bytes()) + block, err := aes.NewCipher(paddedSerializeBigInt(32, sharedSecret)) if err != nil { - return "", err + return "", errors.Wrapf(err, "challenge_public_key: failed to generate encryption key") } ciphertext := make([]byte, len(plaintext)) diff --git a/encodings.go b/encodings.go new file mode 100644 index 0000000..06a1f79 --- /dev/null +++ b/encodings.go @@ -0,0 +1,16 @@ +package libwallet + +import ( + "math/big" +) + +func paddedSerializeBigInt(size uint, x *big.Int) []byte { + src := x.Bytes() + dst := make([]byte, 0, size) + + for i := 0; i < int(size)-len(src); i++ { + dst = append(dst, 0) + } + + return append(dst, src...) +} diff --git a/encodings_test.go b/encodings_test.go new file mode 100644 index 0000000..3802cc5 --- /dev/null +++ b/encodings_test.go @@ -0,0 +1,46 @@ +package libwallet + +import ( + "encoding/hex" + "math/big" + "reflect" + "testing" +) + +func hexToBigInt(value string) *big.Int { + result := &big.Int{} + bytes, _ := hex.DecodeString(value) + result.SetBytes(bytes) + return result +} + +func hexToBytes(value string) []byte { + bytes, _ := hex.DecodeString(value) + return bytes +} + +func Test_paddedSerializeBigInt(t *testing.T) { + + type args struct { + size uint + x *big.Int + } + tests := []struct { + name string + args args + want []byte + }{ + { + name: "31 bytes key", + args: args{size: 32, x: hexToBigInt("0e815b7892396a2e28e09c0d50082931eedd7fec16ef2e06724fe48f877ea6")}, + want: hexToBytes("000e815b7892396a2e28e09c0d50082931eedd7fec16ef2e06724fe48f877ea6"), + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := paddedSerializeBigInt(tt.args.size, tt.args.x); !reflect.DeepEqual(got, tt.want) { + t.Errorf("paddedSerializeBigInt() = %v, want %v", got, tt.want) + } + }) + } +} \ No newline at end of file diff --git a/go.mod b/go.mod index 3d63b63..d1c7bc5 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/muun/libwallet +module libwallet go 1.12 diff --git a/go.sum b/go.sum index 4a788bd..48ae349 100644 --- a/go.sum +++ b/go.sum @@ -91,6 +91,7 @@ github.com/lightningnetwork/lnd v0.8.0-beta h1:HmmhSRTq48qobqQF8YLqNa8eKU8dDBNbW github.com/lightningnetwork/lnd v0.8.0-beta/go.mod h1:nq06y2BDv7vwWeMmwgB7P3pT7/Uj7sGf5FzHISVD6t4= github.com/lightningnetwork/lnd/queue v1.0.1/go.mod h1:vaQwexir73flPW43Mrm7JOgJHmcEFBWWSl9HlyASoms= github.com/lightningnetwork/lnd/ticker v1.0.0/go.mod h1:iaLXJiVgI1sPANIF2qYYUJXjoksPNvGNYowB8aRbpX0= +github.com/ltcsuite/ltcd v0.0.0-20190101042124-f37f8bf35796 h1:sjOGyegMIhvgfq5oaue6Td+hxZuf3tDC8lAPrFldqFw= github.com/ltcsuite/ltcd v0.0.0-20190101042124-f37f8bf35796/go.mod h1:3p7ZTf9V1sNPI5H8P3NkTFF4LuwMdPl2DodF60qAKqY= github.com/ltcsuite/ltcutil v0.0.0-20181217130922-17f3b04680b6/go.mod h1:8Vg/LTOO0KYa/vlHWJ6XZAevPQThGH5sufO0Hrou/lA= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= diff --git a/invoice.go b/invoice.go index 73fa989..ed477d7 100644 --- a/invoice.go +++ b/invoice.go @@ -15,7 +15,7 @@ type Invoice struct { Network *Network MilliSat string Destination []byte - PaymentHash [32]byte + PaymentHash []byte Expiry int64 Description string } @@ -60,7 +60,7 @@ func ParseInvoice(invoice string, network *Network) (*Invoice, error) { Network: network, MilliSat: milliSats, Destination: parsedInvoice.Destination.SerializeCompressed(), - PaymentHash: *parsedInvoice.PaymentHash, + PaymentHash: parsedInvoice.PaymentHash[:], Expiry: parsedInvoice.Timestamp.Unix() + int64(parsedInvoice.Expiry().Seconds()), Description: description, }, nil diff --git a/invoice_test.go b/invoice_test.go index 62cdb42..2a8031b 100644 --- a/invoice_test.go +++ b/invoice_test.go @@ -24,14 +24,14 @@ func TestParseInvoice(t *testing.T) { invoiceDestination, _ := hex.DecodeString(invoiceDestinationHex) - invoicePaymentHash := [32]byte{} + invoicePaymentHash := make([]byte, 32) hex.Decode(invoicePaymentHash[:], []byte(invoiceHashHex)) - invoiceWithAmountPaymentHash := [32]byte{} + invoiceWithAmountPaymentHash := make([]byte, 32) hex.Decode(invoiceWithAmountPaymentHash[:], []byte(invoiceWithAmountHashHex)) - invoiceWithDescriptionPaymentHash := [32]byte{} + invoiceWithDescriptionPaymentHash := make([]byte, 32) hex.Decode(invoiceWithDescriptionPaymentHash[:], []byte(invoiceWithDescriptionHashHex)) - invoiceWithFallbackAddrPaymentHash := [32]byte{} + invoiceWithFallbackAddrPaymentHash := make([]byte, 32) hex.Decode(invoiceWithFallbackAddrPaymentHash[:], []byte(invoiceWithFallbackAddrHashHex)) fallbackAddr, _ := GetPaymentURI("bcrt1qhv0a0uhrt2crdehgfge8e8e6texw3q4has8jh7", network)