17
17
package ledger_cosmos_go
18
18
19
19
import (
20
+ "crypto/sha256"
20
21
"encoding/hex"
21
22
"fmt"
22
- secp256k1 "github.com/btcsuite/btcd/btcec"
23
+ "github.com/btcsuite/btcd/btcec"
23
24
"github.com/stretchr/testify/assert"
24
25
"github.com/stretchr/testify/require"
25
- "github.com/tendermint/tendermint/crypto"
26
26
"strings"
27
27
"testing"
28
28
)
@@ -54,7 +54,7 @@ func Test_UserGetVersion(t *testing.T) {
54
54
55
55
assert .Equal (t , uint8 (0x0 ), version .AppMode , "TESTING MODE ENABLED!!" )
56
56
assert .Equal (t , uint8 (0x1 ), version .Major , "Wrong Major version" )
57
- assert .Equal (t , uint8 (0x1 ), version .Minor , "Wrong Minor version" )
57
+ assert .Equal (t , uint8 (0x5 ), version .Minor , "Wrong Minor version" )
58
58
assert .Equal (t , uint8 (0x0 ), version .Patch , "Wrong Patch version" )
59
59
}
60
60
@@ -67,26 +67,20 @@ func Test_UserGetPublicKey(t *testing.T) {
67
67
68
68
userApp .api .Logging = true
69
69
70
- path := []uint32 {44 , 118 , 0 , 0 , 0 }
70
+ path := []uint32 {44 , 118 , 5 , 0 , 21 }
71
71
72
72
pubKey , err := userApp .GetPublicKeySECP256K1 (path )
73
73
if err != nil {
74
74
t .Fatalf ("Detected error, err: %s\n " , err .Error ())
75
75
}
76
76
77
- assert .Equal (
78
- t ,
79
- 65 ,
80
- len (pubKey ),
81
- "Public key has wrong length: %x, expected length: %x\n " , pubKey , 65 )
82
-
77
+ assert .Equal (t , 33 , len (pubKey ), "Public key has wrong length: %x, expected length: %x\n " , pubKey , 65 )
83
78
fmt .Printf ("PUBLIC KEY: %x\n " , pubKey )
84
79
85
- _ , err = secp256k1 .ParsePubKey (pubKey [:], secp256k1 .S256 ())
86
- require .Nil (t , err , "Error parsing public key err: %s\n " , err )
80
+ assert .Equal (t , "03cb5a33c61595206294140c45efa8a817533e31aa05ea18343033a0732a677005" , hex .EncodeToString (pubKey ), "Unexpected pubkey" )
87
81
}
88
82
89
- func Test_UserShowAddresses (t * testing.T ) {
83
+ func Test_GetAddressPubKeySECP256K1_Zero (t * testing.T ) {
90
84
userApp , err := FindLedgerCosmosUserApp ()
91
85
if err != nil {
92
86
t .Fatalf (err .Error ())
@@ -98,10 +92,44 @@ func Test_UserShowAddresses(t *testing.T) {
98
92
hrp := "cosmos"
99
93
path := []uint32 {44 , 118 , 0 , 0 , 0 }
100
94
101
- err = userApp .ShowAddressSECP256K1 (path , hrp )
95
+ pubKey , addr , err : = userApp .GetAddressPubKeySECP256K1 (path , hrp )
102
96
if err != nil {
103
97
t .Fatalf ("Detected error, err: %s\n " , err .Error ())
104
98
}
99
+
100
+ fmt .Printf ("PUBLIC KEY : %x\n " , pubKey )
101
+ fmt .Printf ("BECH32 ADDR: %s\n " , addr )
102
+
103
+ assert .Equal (t , 33 , len (pubKey ), "Public key has wrong length: %x, expected length: %x\n " , pubKey , 65 )
104
+
105
+ assert .Equal (t , "034fef9cd7c4c63588d3b03feb5281b9d232cba34d6f3d71aee59211ffbfe1fe87" , hex .EncodeToString (pubKey ), "Unexpected pubkey" )
106
+ assert .Equal (t , "cosmos1w34k53py5v5xyluazqpq65agyajavep2rflq6h" , addr , "Unexpected addr" )
107
+ }
108
+
109
+ func Test_GetAddressPubKeySECP256K1 (t * testing.T ) {
110
+ userApp , err := FindLedgerCosmosUserApp ()
111
+ if err != nil {
112
+ t .Fatalf (err .Error ())
113
+ }
114
+ defer userApp .Close ()
115
+
116
+ userApp .api .Logging = true
117
+
118
+ hrp := "cosmos"
119
+ path := []uint32 {44 , 118 , 5 , 0 , 21 }
120
+
121
+ pubKey , addr , err := userApp .GetAddressPubKeySECP256K1 (path , hrp )
122
+ if err != nil {
123
+ t .Fatalf ("Detected error, err: %s\n " , err .Error ())
124
+ }
125
+
126
+ fmt .Printf ("PUBLIC KEY : %x\n " , pubKey )
127
+ fmt .Printf ("BECH32 ADDR: %s\n " , addr )
128
+
129
+ assert .Equal (t , 33 , len (pubKey ), "Public key has wrong length: %x, expected length: %x\n " , pubKey , 65 )
130
+
131
+ assert .Equal (t , "03cb5a33c61595206294140c45efa8a817533e31aa05ea18343033a0732a677005" , hex .EncodeToString (pubKey ), "Unexpected pubkey" )
132
+ assert .Equal (t , "cosmos162zm3k8mc685592d7vej2lxrp58mgmkcec76d6" , addr , "Unexpected addr" )
105
133
}
106
134
107
135
func Test_UserPK_HDPaths (t * testing.T ) {
@@ -116,16 +144,16 @@ func Test_UserPK_HDPaths(t *testing.T) {
116
144
path := []uint32 {44 , 118 , 0 , 0 , 0 }
117
145
118
146
expected := []string {
119
- "044fef9cd7c4c63588d3b03feb5281b9d232cba34d6f3d71aee59211ffbfe1fe877bff8521bf5243e80be922e51cee0faa8346b113fdec822c4d902e42b22bc345 " ,
120
- "0460d0487a3dfce9228eee2d0d83a40f6131f551526c8e52066fe7fe1e4a509666d60da24e97777510db9b238870e184891b580610ec6dafaf12c7abffed3670c6 " ,
121
- "04a2670393d02b162d0ed06a08041e80d86be36c0564335254df7462447eb69ab3f5a54ab07a8622ab23c28e9240ce58f4015ec401d95b08221b74e2a4a209ba6d " ,
122
- "043222fc61795077791665544a90740e8ead638a391a3b8f9261f4a226b396c042a118bb64eccd89941d73de7cb12beed5a47de61049c7fc0d4708a4a0f5637957 " ,
123
- "04f577473348d7b01e7af2f245e36b98d181bc935ec8b552cde5932b646dc7be0415b9fd94af37dc295e25e35d3840fdd3cb1d0baa411bdc00d15dca427abdff3f " ,
124
- "0422b1a5486be0a2d5f3c5866be46e05d1bde8cda5ea1c4c77a9bc48d2fa2753bcbb49b6c9d4be25bed7fb75c2e0c43c25175e88893c4f7963398a5aac3230c79e " ,
125
- "0477a1c826d3a03ca4ee94fc4dea6bccb2bac5f2ac0419a128c29f8e88f1ff295ac6a16c770d38ee0e55bec83e8d8e3f1b1616ce77055a928255919340053a477d " ,
126
- "041b75c84453935ab76f8c8d0b6566c3fcc101cc5c59d7000bfc9101961e9308d9228b0af378c4e6a38eeaf18175d2b2a7ab3fad9c9a4b117775f2e4a4ac633aff " ,
127
- "048905a42433b1d677cc8afd36861430b9a8529171b0616f733659f131c3f80221e222d162dbcde7c77be3d82b4f666c2acc1e25aaeb3e4fadfb8c7c6b1282374b " ,
128
- "048be7f348902d8c20bc88d32294f4f3b819284548122229decd1adf1a7eb0848bc4fbd7ac5bae3a854f2bcb0831c4550f48752f630a33a088d0fd166d8d3435d9 " ,
147
+ "034fef9cd7c4c63588d3b03feb5281b9d232cba34d6f3d71aee59211ffbfe1fe87 " ,
148
+ "0260d0487a3dfce9228eee2d0d83a40f6131f551526c8e52066fe7fe1e4a509666 " ,
149
+ "03a2670393d02b162d0ed06a08041e80d86be36c0564335254df7462447eb69ab3 " ,
150
+ "033222fc61795077791665544a90740e8ead638a391a3b8f9261f4a226b396c042 " ,
151
+ "03f577473348d7b01e7af2f245e36b98d181bc935ec8b552cde5932b646dc7be04 " ,
152
+ "0222b1a5486be0a2d5f3c5866be46e05d1bde8cda5ea1c4c77a9bc48d2fa2753bc " ,
153
+ "0377a1c826d3a03ca4ee94fc4dea6bccb2bac5f2ac0419a128c29f8e88f1ff295a " ,
154
+ "031b75c84453935ab76f8c8d0b6566c3fcc101cc5c59d7000bfc9101961e9308d9 " ,
155
+ "038905a42433b1d677cc8afd36861430b9a8529171b0616f733659f131c3f80221 " ,
156
+ "038be7f348902d8c20bc88d32294f4f3b819284548122229decd1adf1a7eb0848b " ,
129
157
}
130
158
131
159
for i := uint32 (0 ); i < 10 ; i ++ {
@@ -138,7 +166,7 @@ func Test_UserPK_HDPaths(t *testing.T) {
138
166
139
167
assert .Equal (
140
168
t ,
141
- 65 ,
169
+ 33 ,
142
170
len (pubKey ),
143
171
"Public key has wrong length: %x, expected length: %x\n " , pubKey , 65 )
144
172
@@ -148,7 +176,7 @@ func Test_UserPK_HDPaths(t *testing.T) {
148
176
hex .EncodeToString (pubKey ),
149
177
"Public key 44'/118'/0'/0/%d does not match\n " , i )
150
178
151
- _ , err = secp256k1 .ParsePubKey (pubKey [:], secp256k1 .S256 ())
179
+ _ , err = btcec .ParsePubKey (pubKey [:], btcec .S256 ())
152
180
require .Nil (t , err , "Error parsing public key err: %s\n " , err )
153
181
154
182
}
@@ -201,19 +229,20 @@ func Test_UserSign(t *testing.T) {
201
229
return
202
230
}
203
231
204
- pub2 , err := secp256k1 .ParsePubKey (pubKey [:], secp256k1 .S256 ())
232
+ pub2 , err := btcec .ParsePubKey (pubKey [:], btcec .S256 ())
205
233
if err != nil {
206
234
t .Fatalf ("[ParsePK] Error: " + err .Error ())
207
235
return
208
236
}
209
237
210
- sig2 , err := secp256k1 .ParseDERSignature (signature [:], secp256k1 .S256 ())
238
+ sig2 , err := btcec .ParseDERSignature (signature [:], btcec .S256 ())
211
239
if err != nil {
212
240
t .Fatalf ("[ParseSig] Error: " + err .Error ())
213
241
return
214
242
}
215
243
216
- verified := sig2 .Verify (crypto .Sha256 (message ), pub2 )
244
+ hash := sha256 .Sum256 (message )
245
+ verified := sig2 .Verify (hash [:], pub2 )
217
246
if ! verified {
218
247
t .Fatalf ("[VerifySig] Error verifying signature: " + err .Error ())
219
248
return
@@ -236,5 +265,5 @@ func Test_UserSign_Fails(t *testing.T) {
236
265
message = append (garbage , message ... )
237
266
238
267
_ , err = userApp .SignSECP256K1 (path , message )
239
- assert .EqualError (t , err , "Unexpected character in JSON string" )
268
+ assert .EqualError (t , err , "Invalid character in JSON string" )
240
269
}
0 commit comments