1
- import { describe , it } from 'mocha' ;
1
+ import { createInterface } from 'node:readline/promises' ;
2
+ import { describe , it , before , after } from 'mocha' ;
2
3
import { expect } from 'chai' ;
3
4
import '..' ;
4
5
import {
@@ -10,7 +11,7 @@ import type Transport from '@ledgerhq/hw-transport';
10
11
import TransportNodeHid from '@ledgerhq/hw-transport-node-hid-singleton' ;
11
12
import {
12
13
AccountLedger ,
13
- AccountLedgerFactory ,
14
+ AccountLedgerFactory as AccountLedgerFactoryOriginal ,
14
15
buildTx ,
15
16
Node ,
16
17
Tag ,
@@ -26,38 +27,56 @@ import { indent } from '../utils';
26
27
const compareWithRealDevice = false ; // switch to true for manual testing
27
28
// ledger should be initialized with mnemonic:
28
29
// eye quarter chapter suit cruel scrub verify stuff volume control learn dust
29
- let recordStore : RecordStore ;
30
- let expectedRecordStore = '' ;
31
-
32
- async function initTransport ( s : string , ignoreRealDevice = false ) : Promise < Transport > {
33
- expectedRecordStore = s ;
34
- if ( compareWithRealDevice && ! ignoreRealDevice ) {
35
- const t = await TransportNodeHid . create ( ) ;
36
- recordStore = new RecordStore ( ) ;
37
- const TransportRecorder = createTransportRecorder ( t , recordStore ) ;
38
- return new TransportRecorder ( t ) ;
30
+
31
+ async function initTransport (
32
+ expectedRecordStore : string ,
33
+ ignoreRealDevice = false ,
34
+ ) : Promise < Transport > {
35
+ if ( ! compareWithRealDevice || ignoreRealDevice ) {
36
+ return openTransportReplayer ( RecordStore . fromString ( expectedRecordStore ) ) ;
39
37
}
40
- recordStore = RecordStore . fromString ( expectedRecordStore ) ;
41
- return openTransportReplayer ( recordStore ) ;
42
- }
43
38
44
- afterEach ( async ( ) => {
45
- if ( compareWithRealDevice ) {
39
+ // TODO: remove after solving https://github.com/LedgerHQ/ledger-live/issues/9462
40
+ const Transport =
41
+ 'default' in TransportNodeHid
42
+ ? ( TransportNodeHid . default as typeof TransportNodeHid )
43
+ : TransportNodeHid ;
44
+ const t = await Transport . create ( ) ;
45
+ const recordStore = new RecordStore ( ) ;
46
+ const TransportRecorder = createTransportRecorder ( t , recordStore ) ;
47
+ after ( ( ) => {
46
48
expect ( recordStore . toString ( ) . trim ( ) ) . to . equal ( expectedRecordStore ) ;
47
- }
48
- expectedRecordStore = '' ;
49
- } ) ;
49
+ } ) ;
50
+ return new TransportRecorder ( t ) ;
51
+ }
50
52
51
- describe ( 'Ledger HW' , function ( ) {
53
+ function genLedgerTests ( this : Mocha . Suite , isNewApp = false ) : void {
52
54
this . timeout ( compareWithRealDevice ? 60000 : 300 ) ;
53
55
56
+ before ( async ( ) => {
57
+ if ( ! compareWithRealDevice ) return ;
58
+ const rl = createInterface ( {
59
+ input : process . stdin ,
60
+ output : process . stdout ,
61
+ } ) ;
62
+ await rl . question ( `Open aeternity@${ isNewApp ? '1.0.0' : '0.4.4' } on Ledger and press enter` ) ;
63
+ rl . close ( ) ;
64
+ } ) ;
65
+
66
+ class AccountLedgerFactory extends AccountLedgerFactoryOriginal {
67
+ constructor ( transport : Transport ) {
68
+ super ( transport ) ;
69
+ this . _enableExperimentalLedgerAppSupport = isNewApp ;
70
+ }
71
+ }
72
+
54
73
describe ( 'factory' , ( ) => {
55
74
it ( 'gets app version' , async ( ) => {
56
75
const transport = await initTransport ( indent `
57
76
=> e006000000
58
- <= 000004049000` ) ;
77
+ <= ${ isNewApp ? '0100009000' : ' 000004049000' } ` ) ;
59
78
const factory = new AccountLedgerFactory ( transport ) ;
60
- expect ( ( await factory . getAppConfiguration ( ) ) . version ) . to . equal ( '0.4.4' ) ;
79
+ expect ( ( await factory . getAppConfiguration ( ) ) . version ) . to . equal ( isNewApp ? '1.0.0' : '0.4.4' ) ;
61
80
} ) ;
62
81
63
82
it ( 'ensures that app version is compatible' , async ( ) => {
@@ -68,6 +87,7 @@ describe('Ledger HW', function () {
68
87
true ,
69
88
) ;
70
89
const factory = new AccountLedgerFactory ( transport ) ;
90
+ factory . _enableExperimentalLedgerAppSupport = false ;
71
91
await expect ( factory . getAddress ( 42 ) ) . to . be . rejectedWith (
72
92
'Unsupported Aeternity app on Ledger version 1.4.4. Supported: >= 0.4.4 < 0.5.0' ,
73
93
) ;
@@ -76,7 +96,7 @@ describe('Ledger HW', function () {
76
96
it ( 'gets address' , async ( ) => {
77
97
const transport = await initTransport ( indent `
78
98
=> e006000000
79
- <= 000004049000
99
+ <= ${ isNewApp ? '0100009000' : ' 000004049000' }
80
100
=> e0020000040000002a
81
101
<= 35616b5f3248746565756a614a7a75744b65465a69416d59547a636167536f5245725358704246563137397859677154347465616b769000` ) ;
82
102
const factory = new AccountLedgerFactory ( transport ) ;
@@ -88,7 +108,7 @@ describe('Ledger HW', function () {
88
108
it ( 'gets address with verification' , async ( ) => {
89
109
const transport = await initTransport ( indent `
90
110
=> e006000000
91
- <= 000004049000
111
+ <= ${ isNewApp ? '0100009000' : ' 000004049000' }
92
112
=> e0020100040000002a
93
113
<= 35616b5f3248746565756a614a7a75744b65465a69416d59547a636167536f5245725358704246563137397859677154347465616b769000` ) ;
94
114
const factory = new AccountLedgerFactory ( transport ) ;
@@ -100,7 +120,7 @@ describe('Ledger HW', function () {
100
120
it ( 'gets address with verification rejected' , async ( ) => {
101
121
const transport = await initTransport ( indent `
102
122
=> e006000000
103
- <= 000004049000
123
+ <= ${ isNewApp ? '0100009000' : ' 000004049000' }
104
124
=> e0020100040000002a
105
125
<= 6985` ) ;
106
126
const factory = new AccountLedgerFactory ( transport ) ;
@@ -112,7 +132,7 @@ describe('Ledger HW', function () {
112
132
it ( 'initializes an account' , async ( ) => {
113
133
const transport = await initTransport ( indent `
114
134
=> e006000000
115
- <= 000004049000
135
+ <= ${ isNewApp ? '0100009000' : ' 000004049000' }
116
136
=> e0020000040000002a
117
137
<= 35616b5f3248746565756a614a7a75744b65465a69416d59547a636167536f5245725358704246563137397859677154347465616b769000` ) ;
118
138
const factory = new AccountLedgerFactory ( transport ) ;
@@ -147,7 +167,7 @@ describe('Ledger HW', function () {
147
167
it ( 'discovers accounts' , async ( ) => {
148
168
const transport = await initTransport ( indent `
149
169
=> e006000000
150
- <= 000004049000
170
+ <= ${ isNewApp ? '0100009000' : ' 000004049000' }
151
171
=> e00200000400000000
152
172
<= 35616b5f327377684c6b674250656541447856544156434a6e5a4c59354e5a744346694d39334a787345614d754335396575754652519000
153
173
=> e00200000400000001
@@ -166,7 +186,7 @@ describe('Ledger HW', function () {
166
186
it ( 'discovers accounts on unused ledger' , async ( ) => {
167
187
const transport = await initTransport ( indent `
168
188
=> e006000000
169
- <= 000004049000
189
+ <= ${ isNewApp ? '0100009000' : ' 000004049000' }
170
190
=> e00200000400000000
171
191
<= 35616b5f327377684c6b674250656541447856544156434a6e5a4c59354e5a744346694d39334a787345614d754335396575754652519000` ) ;
172
192
const node = new NodeMock ( ) ;
@@ -179,7 +199,7 @@ describe('Ledger HW', function () {
179
199
describe ( 'account' , ( ) => {
180
200
const address = 'ak_2swhLkgBPeeADxVTAVCJnZLY5NZtCFiM93JxsEaMuC59euuFRQ' ;
181
201
it ( 'fails on calling raw signing' , async ( ) => {
182
- const transport = await initTransport ( '\n ' ) ;
202
+ const transport = await initTransport ( '' ) ;
183
203
const account = new AccountLedger ( transport , 0 , address ) ;
184
204
await expect ( account . unsafeSign ( ) ) . to . be . rejectedWith ( 'RAW signing using Ledger HW' ) ;
185
205
} ) ;
@@ -224,11 +244,12 @@ describe('Ledger HW', function () {
224
244
it ( 'signs message' , async ( ) => {
225
245
const transport = await initTransport ( indent `
226
246
=> e00800002f0000000000000027746573742d6d6573736167652c746573742d6d6573736167652c746573742d6d6573736167652c
227
- <= 78397e186058f278835b8e3e866960e4418dc1e9f00b3a2423f57c16021c88720119ebb3373a136112caa1c9ff63870092064659eb2c641dd67767f15c80350c9000` ) ;
247
+ <= ${ isNewApp ? ' 78397e186058f278835b8e3e866960e4418dc1e9f00b3a2423f57c16021c88720119ebb3373a136112caa1c9ff63870092064659eb2c641dd67767f15c80350c9000' : '63a9410fa235e4b1f0204cc4d36322e666662da5873b399b076961eced2907f502cd3f91b95bbcfd8e235e194888d469bb15ab4382705aa887c2e0c4e6cb1a0b9000' } ` ) ;
228
248
const account = new AccountLedger ( transport , 0 , address ) ;
229
249
const signature = await account . signMessage ( message ) ;
230
250
expect ( signature ) . to . be . an . instanceOf ( Uint8Array ) ;
231
- expect ( verifyMessage ( message , signature , address ) ) . to . equal ( true ) ;
251
+ // FIXME: correct signature after releasing https://github.com/LedgerHQ/app-aeternity/pull/13
252
+ expect ( verifyMessage ( message , signature , address ) ) . to . equal ( isNewApp ) ;
232
253
} ) ;
233
254
234
255
it ( 'signs message rejected' , async ( ) => {
@@ -241,4 +262,12 @@ describe('Ledger HW', function () {
241
262
) ;
242
263
} ) ;
243
264
} ) ;
265
+ }
266
+
267
+ describe ( 'Ledger HW v0.4.4' , function ( ) {
268
+ genLedgerTests . call ( this , false ) ;
269
+ } ) ;
270
+
271
+ describe ( 'Ledger HW v1.0.0' , function ( ) {
272
+ genLedgerTests . call ( this , true ) ;
244
273
} ) ;
0 commit comments