Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improving HID connections for Linux and Windows #22

Merged
merged 2 commits into from
Jan 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 29 additions & 30 deletions Gopkg.lock

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

4 changes: 2 additions & 2 deletions Gopkg.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[[constraint]]
name = "github.com/ZondaX/ledger-go"
version = "v0.5.0"
version = "v0.6.0"

[prune]
go-tests = true
unused-packages = true
[[prune.project]]
name = "github.com/karalabe/hid"
name = "github.com/ZondaX/hid"
unused-packages = false
73 changes: 66 additions & 7 deletions user_app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package ledger_cosmos_go

import (
"encoding/hex"
"fmt"
secp256k1 "github.com/btcsuite/btcd/btcec"
"github.com/stretchr/testify/assert"
Expand All @@ -28,10 +29,19 @@ import (
"testing"
)

func Test_UserFindLedger(t *testing.T) {
userApp, err := FindLedgerCosmosUserApp()
if err != nil {
t.Fatalf(err.Error())
}

assert.NotNil(t, userApp)
}

func Test_UserGetVersion(t *testing.T) {
userApp, err := FindLedgerCosmosUserApp()
if err != nil {
t.Fatalf( err.Error())
t.Fatalf(err.Error())
}

userApp.api.Logging = true
Expand All @@ -49,7 +59,7 @@ func Test_UserGetVersion(t *testing.T) {
func Test_UserGetPublicKey(t *testing.T) {
userApp, err := FindLedgerCosmosUserApp()
if err != nil {
t.Fatalf( err.Error())
t.Fatalf(err.Error())
}

userApp.api.Logging = true
Expand All @@ -75,6 +85,55 @@ func Test_UserGetPublicKey(t *testing.T) {

// Ledger Test Mnemonic: equip will roof matter pink blind book anxiety banner elbow sun young

func Test_UserPK_HDPaths(t *testing.T) {
userApp, err := FindLedgerCosmosUserApp()
if err != nil {
t.Fatalf(err.Error())
}

userApp.api.Logging = true

path := []uint32{44, 118, 0, 0, 0}

expected := []string{
"044fef9cd7c4c63588d3b03feb5281b9d232cba34d6f3d71aee59211ffbfe1fe877bff8521bf5243e80be922e51cee0faa8346b113fdec822c4d902e42b22bc345",
"0460d0487a3dfce9228eee2d0d83a40f6131f551526c8e52066fe7fe1e4a509666d60da24e97777510db9b238870e184891b580610ec6dafaf12c7abffed3670c6",
"04a2670393d02b162d0ed06a08041e80d86be36c0564335254df7462447eb69ab3f5a54ab07a8622ab23c28e9240ce58f4015ec401d95b08221b74e2a4a209ba6d",
"043222fc61795077791665544a90740e8ead638a391a3b8f9261f4a226b396c042a118bb64eccd89941d73de7cb12beed5a47de61049c7fc0d4708a4a0f5637957",
"04f577473348d7b01e7af2f245e36b98d181bc935ec8b552cde5932b646dc7be0415b9fd94af37dc295e25e35d3840fdd3cb1d0baa411bdc00d15dca427abdff3f",
"0422b1a5486be0a2d5f3c5866be46e05d1bde8cda5ea1c4c77a9bc48d2fa2753bcbb49b6c9d4be25bed7fb75c2e0c43c25175e88893c4f7963398a5aac3230c79e",
"0477a1c826d3a03ca4ee94fc4dea6bccb2bac5f2ac0419a128c29f8e88f1ff295ac6a16c770d38ee0e55bec83e8d8e3f1b1616ce77055a928255919340053a477d",
"041b75c84453935ab76f8c8d0b6566c3fcc101cc5c59d7000bfc9101961e9308d9228b0af378c4e6a38eeaf18175d2b2a7ab3fad9c9a4b117775f2e4a4ac633aff",
"048905a42433b1d677cc8afd36861430b9a8529171b0616f733659f131c3f80221e222d162dbcde7c77be3d82b4f666c2acc1e25aaeb3e4fadfb8c7c6b1282374b",
"048be7f348902d8c20bc88d32294f4f3b819284548122229decd1adf1a7eb0848bc4fbd7ac5bae3a854f2bcb0831c4550f48752f630a33a088d0fd166d8d3435d9",
}

for i := uint32(0); i < 10; i++ {
path[4] = i

pubKey, err := userApp.GetPublicKeySECP256K1(path)
if err != nil {
t.Fatalf("Detected error, err: %s\n", err.Error())
}

assert.Equal(
t,
65,
len(pubKey),
"Public key has wrong length: %x, expected length: %x\n", pubKey, 65)

assert.Equal(
t,
expected[i],
hex.EncodeToString(pubKey),
"Public key 44'/118'/0'/0/%d does not match\n", i)

_, err = secp256k1.ParsePubKey(pubKey[:], secp256k1.S256())
require.Nil(t, err, "Error parsing public key err: %s\n", err)

}
}

func getDummyTx() []byte {
dummyTx := `{
"account_number": 1,
Expand Down Expand Up @@ -102,7 +161,7 @@ func Test_UserSign(t *testing.T) {

userApp.api.Logging = true

path := []uint32{44, 118, 0, 0, 0}
path := []uint32{44, 118, 0, 0, 5}

message := getDummyTx()
signature, err := userApp.SignSECP256K1(path, message)
Expand All @@ -117,25 +176,25 @@ func Test_UserSign(t *testing.T) {
}

if err != nil {
t.Fatalf( "[GetPK] Error: " + err.Error())
t.Fatalf("[GetPK] Error: " + err.Error())
return
}

pub__, err := secp256k1.ParsePubKey(pubKey[:], secp256k1.S256())
if err != nil {
t.Fatalf( "[ParsePK] Error: " + err.Error())
t.Fatalf("[ParsePK] Error: " + err.Error())
return
}

sig__, err := secp256k1.ParseDERSignature(signature[:], secp256k1.S256())
if err != nil {
t.Fatalf( "[ParseSig] Error: " + err.Error())
t.Fatalf("[ParseSig] Error: " + err.Error())
return
}

verified := sig__.Verify(crypto.Sha256(message), pub__)
if !verified {
t.Fatalf( "[VerifySig] Error verifying signature: " + err.Error())
t.Fatalf("[VerifySig] Error verifying signature: " + err.Error())
return
}
}