1
1
import TrezorConnect from './src' ;
2
2
3
3
import * as messages from '@trezor/protobuf/messages.json' ;
4
+ import { v2 as protocolV2 , thp } from '@trezor/protocol' ;
4
5
import { NodeUsbTransport , UdpTransport } from '../transport/src' ;
5
6
6
7
const abort = new AbortController ( ) ;
@@ -9,8 +10,20 @@ const debugTransport =
9
10
? new UdpTransport ( { messages, debugLink : true , signal : abort . signal } )
10
11
: new NodeUsbTransport ( { messages, debugLink : true , signal : abort . signal } ) ;
11
12
13
+ const loadTransport =
14
+ process . argv [ 2 ] === 'udp'
15
+ ? new UdpTransport ( { messages, signal : abort . signal } )
16
+ : new NodeUsbTransport ( { messages, signal : abort . signal } ) ;
17
+
12
18
const initDebugLink = async ( ) => {
19
+ if ( ! debugTransport . stopped ) {
20
+ return ;
21
+ }
22
+ const init = await debugTransport . init ( ) . promise ;
23
+ const init2 = await loadTransport . init ( ) . promise ;
24
+
13
25
const enumerate = await debugTransport . enumerate ( ) . promise ;
26
+ const enumerate2 = await loadTransport . enumerate ( ) . promise ;
14
27
if ( ! enumerate . success ) {
15
28
return ;
16
29
}
@@ -33,7 +46,7 @@ const debugLinkState = async channel => {
33
46
session : acquire . payload ,
34
47
} ) . promise ;
35
48
36
- console . warn ( 'DebugLinkState' , response . payload ) ;
49
+ console . warn ( 'DebugLinkState' , response ) ;
37
50
38
51
await debugTransport . release ( enumerate . payload [ 0 ] ) . promise ;
39
52
await debugTransport . enumerate ( ) . promise ;
@@ -42,20 +55,90 @@ const debugLinkState = async channel => {
42
55
} ;
43
56
44
57
const debugLinkDecision = async ( ) => {
58
+ console . warn ( 'debugLinkDecision-1' ) ;
45
59
const enumerate = await debugTransport . enumerate ( ) . promise ;
46
60
if ( ! enumerate . success ) {
47
61
return ;
48
62
}
63
+ console . warn ( 'debugLinkDecision-2' , enumerate ) ;
49
64
const acquire = await debugTransport . acquire ( { input : enumerate . payload [ 0 ] } ) . promise ;
65
+ console . warn ( 'debugLinkDecision-3' , acquire ) ;
66
+ const session = acquire . payload ;
50
67
51
- await debugTransport . send ( {
52
- name : 'DebugLinkDecision' ,
53
- data : { button : 1 } ,
54
- session : acquire . payload ,
55
- } ) . promise ;
68
+ const callRes = await debugTransport
69
+ . send ( {
70
+ name : 'DebugLinkDecision' ,
71
+ data : { button : 1 } ,
72
+ session,
73
+ } )
74
+ . promise . then ( r => {
75
+ console . warn ( 'debugLinkDecision-4' , r ) ;
56
76
57
- await debugTransport . release ( enumerate . payload [ 0 ] ) . promise ;
58
- await debugTransport . enumerate ( ) . promise ;
77
+ return r ;
78
+ } ) ;
79
+
80
+ console . warn ( 'debugLinkDecision-5' , callRes ) ;
81
+
82
+ const rele = await debugTransport . release ( { ...enumerate . payload [ 0 ] , session } ) . promise ;
83
+ console . warn ( 'debugLinkDecision-6' , rele ) ;
84
+ const enum2 = await debugTransport . enumerate ( ) . promise ;
85
+
86
+ console . warn ( 'debugLinkDecision-end' , enum2 ) ;
87
+ } ;
88
+
89
+ const debugLinkLoadSeed = async elo => {
90
+ console . warn ( 'debugLinkLoadSeed-1' , elo ) ;
91
+ // if (elo) return;
92
+
93
+ const enumerate = await loadTransport . enumerate ( ) . promise ;
94
+ if ( ! enumerate . success ) {
95
+ return ;
96
+ }
97
+ console . warn ( 'debugLinkLoadSeed-2' , enumerate ) ;
98
+ const acquire = await loadTransport . acquire ( { input : enumerate . payload [ 0 ] } ) . promise ;
99
+ const session = acquire . payload ;
100
+
101
+ console . warn ( 'debugLinkLoadSeed-3' , acquire ) ;
102
+
103
+ const protocolState = new thp . ThpProtocolState ( ) ;
104
+ protocolState . deserialize ( elo ) ;
105
+
106
+ const load = await loadTransport
107
+ . call ( {
108
+ session,
109
+ name : 'LoadDevice' ,
110
+ data : {
111
+ pin : '' ,
112
+ label : 'THP device' ,
113
+ passphrase_protection : true ,
114
+ mnemonics : [ 'all all all all all all all all all all all all' ] ,
115
+ skip_checksum : true ,
116
+ } ,
117
+ protocol : protocolV2 ,
118
+ protocolState,
119
+ } )
120
+ . promise . then ( r => {
121
+ console . warn ( 'debugLinkLoadSeed-4' , r ) ;
122
+ if ( r . success && r . payload . type === 'ButtonRequest' ) {
123
+ const ack = loadTransport . call ( {
124
+ session,
125
+ name : 'ButtonAck' ,
126
+ data : { } ,
127
+ } ) . promise ;
128
+
129
+ //return debugLinkDecision().then(() => ack);
130
+ return ack ;
131
+ }
132
+
133
+ return r ;
134
+ } ) ;
135
+
136
+ console . warn ( 'debugLinkLoadSeed-5' , load ) ;
137
+
138
+ const release = await loadTransport . release ( { ...enumerate . payload [ 0 ] , session } ) . promise ;
139
+ console . warn ( 'debugLinkLoadSeed-6' , release ) ;
140
+ const enumerate2 = await loadTransport . enumerate ( ) . promise ;
141
+ console . warn ( 'debugLinkLoadSeed-7' , enumerate2 ) ;
59
142
} ;
60
143
61
144
const getFeatures = device => {
@@ -66,7 +149,7 @@ const getFeatures = device => {
66
149
67
150
const signTx = device => {
68
151
const outputs = [ ] ;
69
- for ( let i = 0 ; i < 4 ; i ++ ) {
152
+ for ( let i = 0 ; i < 255 ; i ++ ) {
70
153
const output = {
71
154
address : 'momtnzR3XqXgDSsFmd8gkGxUiHZLde3RmA' ,
72
155
amount : 7129 ,
@@ -86,6 +169,12 @@ const signTx = device => {
86
169
prev_index : 1 ,
87
170
amount : 1827955 ,
88
171
} ,
172
+ {
173
+ address_n : "m/44'/1'/0'/0/0" ,
174
+ prev_hash : 'e5040e1bc1ae7667ffb9e5248e90b2fb93cd9150234151ce90e14ab2f5933bcd' ,
175
+ prev_index : 0 ,
176
+ amount : '31000000' ,
177
+ } ,
89
178
// {
90
179
// address_n: "m/84'/1'/0'/0/0",
91
180
// amount: '129999867',
@@ -134,24 +223,60 @@ const run = async () => {
134
223
// });
135
224
await initDebugLink ( ) ;
136
225
137
- signTx ( event . payload ) . then ( r => {
138
- console . warn ( r ) ;
139
- console . warn ( 'Time--->' , Math . round ( ( Date . now ( ) - testStart ) / 1000 ) ) ;
140
- process . exit ( 1 ) ;
141
- } ) ;
226
+ if ( event . payload . features && event . payload . mode === 'initialize' ) {
227
+ console . log ( 'Loading seed' ) ;
228
+ await new Promise ( resolve => setTimeout ( resolve , 2000 ) ) ;
229
+ if ( event . payload . protocolState ) {
230
+ await TrezorConnect . resetDevice ( {
231
+ pin : '' ,
232
+ label : 'THP device' ,
233
+ passphrase_protection : true ,
234
+ // mnemonics: ['all all all all all all all all all all all all'],
235
+ // skip_checksum: true,
236
+ } ) ;
237
+ } else {
238
+ await debugLinkLoadSeed ( event . payload . protocolState ) ;
239
+ }
240
+ }
241
+
242
+ if ( ! event . payload . features && event . payload . properties ) {
243
+ await getFeatures ( event . payload ) ;
244
+ } else {
245
+ signTx ( event . payload ) . then ( r => {
246
+ console . warn ( r ) ;
247
+ console . warn ( 'Time--->' , Math . round ( ( Date . now ( ) - testStart ) / 1000 ) ) ;
248
+ process . exit ( 1 ) ;
249
+ } ) ;
250
+ }
251
+
252
+ // getFeatures(event.payload);
253
+
254
+ // TrezorConnect.getAddress({
255
+ // device: { path: event.payload.path },
256
+ // path: "m/44'/0'/0'/0/0",
257
+ // }).then(r => {
258
+ // console.warn(r);
259
+ // process.exit(1);
260
+ // });
142
261
}
143
262
} ) ;
144
263
145
264
TrezorConnect . on ( 'UI_EVENT' , async event => {
146
265
console . warn ( 'UI_EVENT' , event ) ;
266
+
267
+ if ( event . type === 'ui-request_pin' ) {
268
+ setTimeout ( ( ) => TrezorConnect . cancel ( ) , 1000 ) ;
269
+
270
+ // TrezorConnect.uiResponse({
271
+ // type: 'ui-receive_pin',
272
+ // payload: null,
273
+ // });
274
+ }
275
+
147
276
if ( event . type === 'ui-request_passphrase' ) {
148
277
TrezorConnect . uiResponse ( {
149
278
type : 'ui-receive_passphrase' ,
150
- payload : {
151
- passphraseOnDevice : false ,
152
- // value: 'abcd',
153
- value : '' ,
154
- } ,
279
+ payload : { value : '' } ,
155
280
} ) ;
156
281
}
157
282
@@ -193,6 +318,8 @@ const run = async () => {
193
318
}
194
319
195
320
if ( event . type === 'ui-button' ) {
321
+ // console.warn('RESOLVE!');
322
+ // TrezorConnect.cancel();
196
323
await debugLinkDecision ( ) ;
197
324
}
198
325
} ) ;
0 commit comments