Skip to content

Commit c03f491

Browse files
jleniAlessio Treglia
authored and
Alessio Treglia
committed
Upgrading to support both v1.5.x and v2.x.x (#10)
This PR extends support to Cosmos' Ledger App v2.x.x while keeping backwards compatibility for v1.5.x
1 parent 8363248 commit c03f491

File tree

5 files changed

+252
-161
lines changed

5 files changed

+252
-161
lines changed

common.go

+18-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func (c VersionInfo) String() string {
3333
return fmt.Sprintf("%d.%d.%d", c.Major, c.Minor, c.Patch)
3434
}
3535

36-
// NotSupportedError the command is not supported by this app
36+
// VersionRequiredError the command is not supported by this app
3737
type VersionRequiredError struct {
3838
Found VersionInfo
3939
Required VersionInfo
@@ -72,7 +72,7 @@ func CheckVersion(ver VersionInfo, req VersionInfo) error {
7272
return NewVersionRequiredError(req, ver)
7373
}
7474

75-
func GetBip32bytes(bip32Path []uint32, hardenCount int) ([]byte, error) {
75+
func GetBip32bytesv1(bip32Path []uint32, hardenCount int) ([]byte, error) {
7676
message := make([]byte, 41)
7777
if len(bip32Path) > 10 {
7878
return nil, fmt.Errorf("maximum bip32 depth = 10")
@@ -88,3 +88,19 @@ func GetBip32bytes(bip32Path []uint32, hardenCount int) ([]byte, error) {
8888
}
8989
return message, nil
9090
}
91+
92+
func GetBip32bytesv2(bip44Path []uint32, hardenCount int) ([]byte, error) {
93+
message := make([]byte, 40)
94+
if len(bip44Path) != 5 {
95+
return nil, fmt.Errorf("path should contain 5 elements")
96+
}
97+
for index, element := range bip44Path {
98+
pos := index*4
99+
value := element
100+
if index < hardenCount {
101+
value = 0x80000000 | element
102+
}
103+
binary.LittleEndian.PutUint32(message[pos:], value)
104+
}
105+
return message, nil
106+
}

common_test.go

+75-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func Test_PrintVersion(t *testing.T) {
3131
func Test_PathGeneration0(t *testing.T) {
3232
bip32Path := []uint32{44, 100, 0, 0, 0}
3333

34-
pathBytes, err := GetBip32bytes(bip32Path, 0)
34+
pathBytes, err := GetBip32bytesv1(bip32Path, 0)
3535

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

58-
pathBytes, err := GetBip32bytes(bip32Path, 2)
58+
pathBytes, err := GetBip32bytesv1(bip32Path, 2)
5959

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

82-
pathBytes, err := GetBip32bytes(bip32Path, 3)
82+
pathBytes, err := GetBip32bytesv1(bip32Path, 3)
8383

8484
if err != nil {
8585
t.Fatalf("Detected error, err: %s\n", err.Error())
@@ -99,3 +99,75 @@ func Test_PathGeneration3(t *testing.T) {
9999
fmt.Sprintf("%x", pathBytes),
100100
"Unexpected PathBytes\n")
101101
}
102+
103+
func Test_PathGeneration0v2(t *testing.T) {
104+
bip32Path := []uint32{44, 100, 0, 0, 0}
105+
106+
pathBytes, err := GetBip32bytesv2(bip32Path, 0)
107+
108+
if err != nil {
109+
t.Fatalf( "Detected error, err: %s\n", err.Error())
110+
}
111+
112+
fmt.Printf("Path: %x\n", pathBytes)
113+
114+
assert.Equal(
115+
t,
116+
40,
117+
len(pathBytes),
118+
"PathBytes has wrong length: %x, expected length: %x\n", pathBytes, 40)
119+
120+
assert.Equal(
121+
t,
122+
"2c000000640000000000000000000000000000000000000000000000000000000000000000000000",
123+
fmt.Sprintf("%x", pathBytes),
124+
"Unexpected PathBytes\n")
125+
}
126+
127+
func Test_PathGeneration2v2(t *testing.T) {
128+
bip32Path := []uint32{44, 118, 0, 0, 0}
129+
130+
pathBytes, err := GetBip32bytesv2(bip32Path, 2)
131+
132+
if err != nil {
133+
t.Fatalf("Detected error, err: %s\n", err.Error())
134+
}
135+
136+
fmt.Printf("Path: %x\n", pathBytes)
137+
138+
assert.Equal(
139+
t,
140+
40,
141+
len(pathBytes),
142+
"PathBytes has wrong length: %x, expected length: %x\n", pathBytes, 40)
143+
144+
assert.Equal(
145+
t,
146+
"2c000080760000800000000000000000000000000000000000000000000000000000000000000000",
147+
fmt.Sprintf("%x", pathBytes),
148+
"Unexpected PathBytes\n")
149+
}
150+
151+
func Test_PathGeneration3v2(t *testing.T) {
152+
bip32Path := []uint32{44, 118, 0, 0, 0}
153+
154+
pathBytes, err := GetBip32bytesv2(bip32Path, 3)
155+
156+
if err != nil {
157+
t.Fatalf("Detected error, err: %s\n", err.Error())
158+
}
159+
160+
fmt.Printf("Path: %x\n", pathBytes)
161+
162+
assert.Equal(
163+
t,
164+
40,
165+
len(pathBytes),
166+
"PathBytes has wrong length: %x, expected length: %x\n", pathBytes, 40)
167+
168+
assert.Equal(
169+
t,
170+
"2c000080760000800000008000000000000000000000000000000000000000000000000000000000",
171+
fmt.Sprintf("%x", pathBytes),
172+
"Unexpected PathBytes\n")
173+
}

0 commit comments

Comments
 (0)