Skip to content

Commit

Permalink
Release v0.1.5
Browse files Browse the repository at this point in the history
  • Loading branch information
mdb1 committed Dec 17, 2019
1 parent 3acb2bc commit 33ea2cc
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 27 deletions.
26 changes: 13 additions & 13 deletions address.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -146,15 +146,15 @@ func GetPaymentURI(address string, network *Network) (*MuunPaymentURI, error) {
Label: label,
Message: message,
Amount: amount,
URI: uriAddress,
URI: bitcoinUri,
BIP70Url: queryValues["r"][0],
}, nil
}
return &MuunPaymentURI{
Label: label,
Message: message,
Amount: amount,
URI: uriAddress,
URI: bitcoinUri,
BIP70Url: queryValues["r"][0],
}, nil
}
Expand All @@ -174,7 +174,7 @@ func GetPaymentURI(address string, network *Network) (*MuunPaymentURI, error) {
Label: label,
Message: message,
Amount: amount,
URI: uriAddress,
URI: bitcoinUri,
}, nil

}
Expand Down Expand Up @@ -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
}
6 changes: 3 additions & 3 deletions address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
})
}
Expand Down
4 changes: 2 additions & 2 deletions challenge_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
4 changes: 2 additions & 2 deletions challenge_public_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
16 changes: 16 additions & 0 deletions encodings.go
Original file line number Diff line number Diff line change
@@ -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...)
}
46 changes: 46 additions & 0 deletions encodings_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/muun/libwallet
module libwallet

go 1.12

Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
4 changes: 2 additions & 2 deletions invoice.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type Invoice struct {
Network *Network
MilliSat string
Destination []byte
PaymentHash [32]byte
PaymentHash []byte
Expiry int64
Description string
}
Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions invoice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 33ea2cc

Please sign in to comment.