Skip to content

Commit

Permalink
Release v0.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
mdb1 committed Aug 3, 2020
1 parent 8b0064d commit 1782413
Show file tree
Hide file tree
Showing 1,167 changed files with 324,077 additions and 18,203 deletions.
18 changes: 2 additions & 16 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
# Mac OS X finder
.DS_Store
libwallet/.gitignore# binary
libwallet

# Vim swap files
*~
.*.sw[a-z]

# VS code
.vscode

# Goland
.idea

# Golang
/bin
/pkg
/src
17 changes: 17 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${fileDirname}",
"env": {},
"args": []
}
]
}
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"go.buildFlags": [
"-v"
]
}
33 changes: 18 additions & 15 deletions address.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (

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

// These constants are here for clients usage.
Expand Down Expand Up @@ -92,11 +92,9 @@ const (
// GetPaymentURI builds a MuunPaymentURI from text (Bitcoin Uri, Muun Uri or address) and a network
func GetPaymentURI(rawInput string, network *Network) (*MuunPaymentURI, error) {

bitcoinUri := buildUriFromString(rawInput)

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

if components.Scheme != "bitcoin" {
Expand Down Expand Up @@ -224,11 +222,11 @@ func DoPaymentRequestCall(url string, network *Network) (*MuunPaymentURI, error)

return &MuunPaymentURI{
Address: address,
Message: *payDetails.Memo,
Amount: strconv.FormatUint(*payDetails.Outputs[0].Amount, 10),
Message: payDetails.Memo,
Amount: strconv.FormatUint(payDetails.Outputs[0].Amount, 10),
BIP70Url: url,
CreationTime: strconv.FormatUint(*payDetails.Time, 10),
ExpiresTime: strconv.FormatUint(*payDetails.Expires, 10),
CreationTime: strconv.FormatUint(payDetails.Time, 10),
ExpiresTime: strconv.FormatUint(payDetails.Expires, 10),
}, nil
}

Expand All @@ -244,14 +242,19 @@ func getAddressFromScript(script []byte, network *Network) (string, error) {
return address.String(), nil
}

func buildUriFromString(rawInput string) string {
func buildUriFromString(rawInput string, targetScheme string) (string, *url.URL) {
newUri := rawInput

newUri = strings.Replace(newUri, muunScheme, bitcoinScheme, 1)
newUri = strings.Replace(newUri, muunScheme, targetScheme, 1)

if !strings.Contains(newUri, bitcoinScheme) {
newUri = bitcoinScheme + rawInput
if !strings.HasPrefix(strings.ToLower(newUri), targetScheme) {
newUri = targetScheme + rawInput
}

components, err := url.Parse(newUri)
if err != nil {
return "", nil
}

return newUri
return newUri, components
}
55 changes: 49 additions & 6 deletions address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package libwallet
import (
"encoding/hex"
"reflect"
"strings"
"testing"
)

Expand All @@ -13,11 +14,11 @@ const (
uriWithSlashes = "bitcoin://" + amountURI

invalidAddress = "2NDhvuRPCYXq4fB8SprminieZ2a1i3JFXya"
randomText = "fooo"

bip70URL = "https://bitpay.com/i/KXCEAtJQssR9vG2BxdjFwx"
bip70NonRetroCompatAddress = bitcoinScheme + "?r=" + bip70URL
bip70RetroCompatAddress = bitcoinScheme + address + "?r=" + bip70URL

)

func TestGetPaymentURI(t *testing.T) {
Expand Down Expand Up @@ -89,6 +90,14 @@ func TestGetPaymentURI(t *testing.T) {
},
wantErr: true,
},
{
name: "randomText",
args: args{
address: randomText,
network: *Regtest(),
},
wantErr: true,
},
{
name: "BIP70NonRetroCompatAddress",
args: args{
Expand Down Expand Up @@ -164,6 +173,28 @@ func TestGetPaymentURI(t *testing.T) {
Description: "",
}},
},
{
name: "ALL CAPS",
args: args{
address: "BITCOIN:BC1QSQP0D3TY8AAA8N9J8R0D2PF3G40VN4AS9TPWY3J9R3GK5K64VX6QWPAXH2",
network: *Mainnet(),
},
want: &MuunPaymentURI{
Address: strings.ToLower("BC1QSQP0D3TY8AAA8N9J8R0D2PF3G40VN4AS9TPWY3J9R3GK5K64VX6QWPAXH2"),
URI: "BITCOIN:BC1QSQP0D3TY8AAA8N9J8R0D2PF3G40VN4AS9TPWY3J9R3GK5K64VX6QWPAXH2",
},
},
{
name: "MiXeD Case",
args: args{
address: "BiTcOiN:BC1QSQP0D3TY8AAA8N9J8R0D2PF3G40VN4AS9TPWY3J9R3GK5K64VX6QWPAXH2",
network: *Mainnet(),
},
want: &MuunPaymentURI{
Address: strings.ToLower("BC1QSQP0D3TY8AAA8N9J8R0D2PF3G40VN4AS9TPWY3J9R3GK5K64VX6QWPAXH2"),
URI: "BiTcOiN:BC1QSQP0D3TY8AAA8N9J8R0D2PF3G40VN4AS9TPWY3J9R3GK5K64VX6QWPAXH2",
},
},
}

for _, tt := range tests {
Expand All @@ -186,7 +217,8 @@ func TestGetPaymentURI(t *testing.T) {

func Test_normalizeAddress(t *testing.T) {
type args struct {
rawAddress string
rawAddress string
targetScheme string
}
tests := []struct {
name string
Expand All @@ -196,28 +228,39 @@ func Test_normalizeAddress(t *testing.T) {
{
name: "normalAddress",
args: args{
rawAddress: address,
rawAddress: address,
targetScheme: bitcoinScheme,
},
want: bitcoinScheme + address,
},
{
name: "bitcoinAddress",
args: args{
rawAddress: bitcoinScheme + address,
rawAddress: bitcoinScheme + address,
targetScheme: bitcoinScheme,
},
want: bitcoinScheme + address,
},
{
name: "muunAddress",
args: args{
rawAddress: muunScheme + address,
rawAddress: muunScheme + address,
targetScheme: bitcoinScheme,
},
want: bitcoinScheme + address,
},
{
name: "muun to lightning",
args: args{
rawAddress: muunScheme + address,
targetScheme: lightningScheme,
},
want: lightningScheme + address,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := buildUriFromString(tt.args.rawAddress); got != tt.want {
if got, _ := buildUriFromString(tt.args.rawAddress, tt.args.targetScheme); got != tt.want {
t.Errorf("buildUriFromString() = %v, want %v", got, tt.want)
}
})
Expand Down
22 changes: 15 additions & 7 deletions aes.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ import (
"github.com/pkg/errors"
)

func encrypt(key []byte, iv []byte, plaintext []byte) ([]byte, error) {
const aesKeySize = 32

func encryptAesCbcPkcs7(key []byte, iv []byte, plaintext []byte) ([]byte, error) {
plaintext = pkcs7Padding(plaintext)
return encryptAesCbcNoPadding(key, iv, plaintext)
}

func encryptAesCbcNoPadding(key []byte, iv []byte, plaintext []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
Expand All @@ -25,8 +29,17 @@ func encrypt(key []byte, iv []byte, plaintext []byte) ([]byte, error) {
return ciphertext, nil
}

func decrypt(key []byte, iv []byte, cypertext []byte) ([]byte, error) {
func decryptAesCbcPkcs7(key []byte, iv []byte, cypertext []byte) ([]byte, error) {

paddedPlaintext, err := decryptAesCbcNoPadding(key, iv, cypertext)
if err != nil {
return nil, err
}

return pkcs7UnPadding(paddedPlaintext)
}

func decryptAesCbcNoPadding(key []byte, iv []byte, cypertext []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
Expand All @@ -37,11 +50,6 @@ func decrypt(key []byte, iv []byte, cypertext []byte) ([]byte, error) {
mode := cipher.NewCBCDecrypter(block, iv)
mode.CryptBlocks(plaintext, cypertext)

plaintext, err = pkcs7UnPadding(plaintext)
if err != nil {
return nil, err
}

return plaintext, nil
}

Expand Down
Loading

0 comments on commit 1782413

Please sign in to comment.