Skip to content

Commit

Permalink
Upgrading to support both v1.5.x and v2.x.x (cosmos#10)
Browse files Browse the repository at this point in the history
This PR extends support to Cosmos' Ledger App v2.x.x while keeping backwards compatibility for v1.5.x
  • Loading branch information
jleni authored and Alessio Treglia committed Oct 15, 2019
1 parent 8363248 commit c03f491
Show file tree
Hide file tree
Showing 5 changed files with 252 additions and 161 deletions.
20 changes: 18 additions & 2 deletions common.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (c VersionInfo) String() string {
return fmt.Sprintf("%d.%d.%d", c.Major, c.Minor, c.Patch)
}

// NotSupportedError the command is not supported by this app
// VersionRequiredError the command is not supported by this app
type VersionRequiredError struct {
Found VersionInfo
Required VersionInfo
Expand Down Expand Up @@ -72,7 +72,7 @@ func CheckVersion(ver VersionInfo, req VersionInfo) error {
return NewVersionRequiredError(req, ver)
}

func GetBip32bytes(bip32Path []uint32, hardenCount int) ([]byte, error) {
func GetBip32bytesv1(bip32Path []uint32, hardenCount int) ([]byte, error) {
message := make([]byte, 41)
if len(bip32Path) > 10 {
return nil, fmt.Errorf("maximum bip32 depth = 10")
Expand All @@ -88,3 +88,19 @@ func GetBip32bytes(bip32Path []uint32, hardenCount int) ([]byte, error) {
}
return message, nil
}

func GetBip32bytesv2(bip44Path []uint32, hardenCount int) ([]byte, error) {
message := make([]byte, 40)
if len(bip44Path) != 5 {
return nil, fmt.Errorf("path should contain 5 elements")
}
for index, element := range bip44Path {
pos := index*4
value := element
if index < hardenCount {
value = 0x80000000 | element
}
binary.LittleEndian.PutUint32(message[pos:], value)
}
return message, nil
}
78 changes: 75 additions & 3 deletions common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func Test_PrintVersion(t *testing.T) {
func Test_PathGeneration0(t *testing.T) {
bip32Path := []uint32{44, 100, 0, 0, 0}

pathBytes, err := GetBip32bytes(bip32Path, 0)
pathBytes, err := GetBip32bytesv1(bip32Path, 0)

if err != nil {
t.Fatalf( "Detected error, err: %s\n", err.Error())
Expand All @@ -55,7 +55,7 @@ func Test_PathGeneration0(t *testing.T) {
func Test_PathGeneration2(t *testing.T) {
bip32Path := []uint32{44, 118, 0, 0, 0}

pathBytes, err := GetBip32bytes(bip32Path, 2)
pathBytes, err := GetBip32bytesv1(bip32Path, 2)

if err != nil {
t.Fatalf("Detected error, err: %s\n", err.Error())
Expand All @@ -79,7 +79,7 @@ func Test_PathGeneration2(t *testing.T) {
func Test_PathGeneration3(t *testing.T) {
bip32Path := []uint32{44, 118, 0, 0, 0}

pathBytes, err := GetBip32bytes(bip32Path, 3)
pathBytes, err := GetBip32bytesv1(bip32Path, 3)

if err != nil {
t.Fatalf("Detected error, err: %s\n", err.Error())
Expand All @@ -99,3 +99,75 @@ func Test_PathGeneration3(t *testing.T) {
fmt.Sprintf("%x", pathBytes),
"Unexpected PathBytes\n")
}

func Test_PathGeneration0v2(t *testing.T) {
bip32Path := []uint32{44, 100, 0, 0, 0}

pathBytes, err := GetBip32bytesv2(bip32Path, 0)

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

fmt.Printf("Path: %x\n", pathBytes)

assert.Equal(
t,
40,
len(pathBytes),
"PathBytes has wrong length: %x, expected length: %x\n", pathBytes, 40)

assert.Equal(
t,
"2c000000640000000000000000000000000000000000000000000000000000000000000000000000",
fmt.Sprintf("%x", pathBytes),
"Unexpected PathBytes\n")
}

func Test_PathGeneration2v2(t *testing.T) {
bip32Path := []uint32{44, 118, 0, 0, 0}

pathBytes, err := GetBip32bytesv2(bip32Path, 2)

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

fmt.Printf("Path: %x\n", pathBytes)

assert.Equal(
t,
40,
len(pathBytes),
"PathBytes has wrong length: %x, expected length: %x\n", pathBytes, 40)

assert.Equal(
t,
"2c000080760000800000000000000000000000000000000000000000000000000000000000000000",
fmt.Sprintf("%x", pathBytes),
"Unexpected PathBytes\n")
}

func Test_PathGeneration3v2(t *testing.T) {
bip32Path := []uint32{44, 118, 0, 0, 0}

pathBytes, err := GetBip32bytesv2(bip32Path, 3)

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

fmt.Printf("Path: %x\n", pathBytes)

assert.Equal(
t,
40,
len(pathBytes),
"PathBytes has wrong length: %x, expected length: %x\n", pathBytes, 40)

assert.Equal(
t,
"2c000080760000800000008000000000000000000000000000000000000000000000000000000000",
fmt.Sprintf("%x", pathBytes),
"Unexpected PathBytes\n")
}
Loading

0 comments on commit c03f491

Please sign in to comment.