Skip to content

Commit

Permalink
Fixing unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jleni committed Apr 25, 2019
1 parent 5cbad06 commit ee19482
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
7 changes: 5 additions & 2 deletions user_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,13 @@ func (ledger *LedgerCosmos) GetVersion() (*VersionInfo, error) {
}

// SignSECP256K1 signs a transaction using Cosmos user app
// this command requires user confirmation in the device
func (ledger *LedgerCosmos) SignSECP256K1(bip32Path []uint32, transaction []byte) ([]byte, error) {
return ledger.sign(userINSSignSECP256K1, bip32Path, transaction)
}

// GetPublicKeySECP256K1 retrieves the public key for the corresponding bip32 derivation path
// GetPublicKeySECP256K1 retrieves the public key for the corresponding bip32 derivation path (compressed)
// this command DOES NOT require user confirmation in the device
func (ledger *LedgerCosmos) GetPublicKeySECP256K1(bip32Path []uint32) ([]byte, error) {
pathBytes, err := GetBip32bytes(bip32Path, 3)
if err != nil {
Expand Down Expand Up @@ -144,7 +146,8 @@ func validHRPByte(b byte) bool {
return b >= 33 && b <= 126
}

// ShowAddressSECP256K1 shows the address for the corresponding bip32 derivation path
// GetAddressPubKeySECP256K1 returns the pubkey (compressed) and address (bech(
// this command requires user confirmation in the device
func (ledger *LedgerCosmos) GetAddressPubKeySECP256K1(bip32Path []uint32, hrp string) (pubkey []byte, addr string, err error) {
// Check that app is at least 1.3.1
requiredVersion := VersionInfo{0, 1, 3, 1,}
Expand Down
30 changes: 29 additions & 1 deletion user_app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,34 @@ func Test_UserGetPublicKey(t *testing.T) {

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

assert.Equal(t, "03cb5a33c61595206294140c45efa8a817533e31aa05ea18343033a0732a677005", hex.EncodeToString(pubKey), "Unexpected pubkey")
}

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

userApp.api.Logging = true

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

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

fmt.Printf("PUBLIC KEY : %x\n", pubKey)
fmt.Printf("BECH32 ADDR: %s\n", addr)

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

assert.Equal(t, "034fef9cd7c4c63588d3b03feb5281b9d232cba34d6f3d71aee59211ffbfe1fe87", hex.EncodeToString(pubKey), "Unexpected pubkey")
assert.Equal(t, "cosmos1w34k53py5v5xyluazqpq65agyajavep2rflq6h", addr, "Unexpected addr")
}

func Test_GetAddressPubKeySECP256K1(t *testing.T) {
Expand All @@ -100,7 +128,7 @@ func Test_GetAddressPubKeySECP256K1(t *testing.T) {

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

assert.Equal(t, "b5b4b8d01844d6c4c37dd4626694c216fbf93821d50641a7ed8104394e19ece105", hex.EncodeToString(pubKey), "Unexpected pubkey")
assert.Equal(t, "03cb5a33c61595206294140c45efa8a817533e31aa05ea18343033a0732a677005", hex.EncodeToString(pubKey), "Unexpected pubkey")
assert.Equal(t, "cosmos162zm3k8mc685592d7vej2lxrp58mgmkcec76d6", addr, "Unexpected addr")
}

Expand Down

0 comments on commit ee19482

Please sign in to comment.