@@ -14,6 +14,7 @@ import (
14
14
"strings"
15
15
"sync"
16
16
17
+ "github.com/dop251/goja"
17
18
"github.com/ethereum/go-ethereum"
18
19
"github.com/ethereum/go-ethereum/accounts"
19
20
"github.com/ethereum/go-ethereum/accounts/keystore"
@@ -22,10 +23,8 @@ import (
22
23
"github.com/ethereum/go-ethereum/common/hexutil"
23
24
"github.com/ethereum/go-ethereum/core/types"
24
25
"github.com/ethereum/go-ethereum/crypto"
25
- "github.com/ethereum/go-ethereum/log"
26
26
"github.com/ethereum/go-ethereum/rlp"
27
27
"github.com/pborman/uuid"
28
- "github.com/robertkrimen/otto"
29
28
)
30
29
31
30
var (
@@ -66,17 +65,6 @@ type gethAccount struct {
66
65
key * keystore.Key
67
66
}
68
67
69
- // copied from 'console' package.
70
- // throwJSException panics on an otto.Value. The Otto VM will recover from the
71
- // Go panic and throw msg as a JavaScript error.
72
- func throwJSException (err error ) otto.Value {
73
- val , err2 := otto .ToValue (err .Error ())
74
- if err2 != nil {
75
- log .Error ("Failed to serialize JavaScript exception" , "exception" , err , "err" , err2 )
76
- }
77
- panic (val )
78
- }
79
-
80
68
// id is sha3 of random uuid
81
69
func offlineWalletNewId () string {
82
70
return hex .EncodeToString (
@@ -165,28 +153,25 @@ func openUsbWallet(scheme, path string) (string, *common.Address, error) {
165
153
offlineWallets [id ] = drv
166
154
addr , err := drv .Derive (accounts .DefaultBaseDerivationPath )
167
155
if err != nil {
168
- throwJSException ( err )
156
+ return "" , nil , err
169
157
}
170
158
return id , & addr , nil
171
159
}
172
160
173
161
// { "id": string, "address": address } offlneWalletOpen(string url, string password)
174
- func (re * JSRE ) offlineWalletOpen (call otto.FunctionCall ) otto.Value {
175
- rawurl , err := call .Argument (0 ).ToString ()
176
- if err != nil {
177
- throwJSException (err )
178
- }
162
+ func (re * JSRE ) offlineWalletOpen (call Call ) (goja.Value , error ) {
163
+ rawurl := call .Argument (0 ).ToString ().String ()
179
164
password := ""
180
- if call .Argument (1 ).IsString () {
181
- password , _ = call .Argument (1 ).ToString ()
165
+ if len ( call .Arguments ) >= 2 && call . Argument (1 ).ToString () != nil {
166
+ password = call .Argument (1 ).ToString (). String ()
182
167
}
183
168
184
169
offlineWalletLock .Lock ()
185
170
defer offlineWalletLock .Unlock ()
186
171
187
172
u , err := url .Parse (rawurl )
188
173
if err != nil {
189
- throwJSException ( err )
174
+ return nil , err
190
175
}
191
176
192
177
path := u .Path
@@ -199,86 +184,62 @@ func (re *JSRE) offlineWalletOpen(call otto.FunctionCall) otto.Value {
199
184
case "" , "geth" , "gmet" :
200
185
id , addr , err := loadGethAccount (password , path )
201
186
if err != nil {
202
- throwJSException ( err )
187
+ return nil , err
203
188
} else {
204
189
r := map [string ]string {
205
190
"id" : id ,
206
191
"address" : addr .Hex (),
207
192
}
208
- env := otto .New ()
209
- v , err := env .ToValue (r )
210
- if err != nil {
211
- throwJSException (err )
212
- }
213
- return v
193
+ return re .vm .ToValue (r ), nil
214
194
}
215
195
case "ledger" , "trezor" :
216
196
id , addr , err := openUsbWallet (u .Scheme , path )
217
197
if err != nil {
218
- throwJSException ( err )
198
+ return nil , err
219
199
} else {
220
200
r := map [string ]string {
221
201
"id" : id ,
222
202
"address" : addr .Hex (),
223
203
}
224
- env := otto .New ()
225
- v , err := env .ToValue (r )
226
- if err != nil {
227
- throwJSException (err )
228
- }
229
- return v
204
+ return re .vm .ToValue (r ), nil
230
205
}
231
206
default :
232
207
// not supported
233
- throwJSException ( errors .New ("Not Supported" ) )
208
+ return nil , errors .New ("Not Supported" )
234
209
}
235
- return otto .UndefinedValue ()
236
210
}
237
211
238
212
// address offlneWalletAddress(string id)
239
- func (re * JSRE ) offlineWalletAddress (call otto.FunctionCall ) otto.Value {
240
- id , err := call .Argument (0 ).ToString ()
241
- if err != nil {
242
- throwJSException (err )
243
- }
213
+ func (re * JSRE ) offlineWalletAddress (call Call ) (goja.Value , error ) {
214
+ id := call .Argument (0 ).ToString ().String ()
244
215
245
216
offlineWalletLock .Lock ()
246
217
w , ok := offlineWallets [id ]
247
218
offlineWalletLock .Unlock ()
248
219
249
220
if ! ok {
250
- throwJSException (ethereum .NotFound )
251
- return otto .UndefinedValue ()
221
+ return nil , ethereum .NotFound
252
222
} else {
253
223
addr , err := w .Derive (accounts .DefaultBaseDerivationPath )
254
224
if err != nil {
255
- throwJSException (err )
256
- }
257
- v , err := otto .ToValue (addr .Hex ())
258
- if err != nil {
259
- throwJSException (err )
225
+ return nil , err
260
226
}
261
- return v
227
+ return re . vm . ToValue ( addr . Hex ()), nil
262
228
}
263
229
}
264
230
265
231
// address offlneWalletClose(string id)
266
- func (re * JSRE ) offlineWalletClose (call otto.FunctionCall ) otto.Value {
267
- id , err := call .Argument (0 ).ToString ()
268
- if err != nil {
269
- throwJSException (err )
270
- }
271
-
232
+ func (re * JSRE ) offlineWalletClose (call Call ) (goja.Value , error ) {
233
+ id := call .Argument (0 ).ToString ().String ()
272
234
offlineWalletLock .Lock ()
273
235
defer offlineWalletLock .Unlock ()
274
236
275
237
if w , ok := offlineWallets [id ]; ! ok {
276
- throwJSException (ethereum .NotFound )
277
- return otto .FalseValue ()
238
+ return re .vm .ToValue (false ), nil
278
239
} else {
279
240
delete (offlineWallets , id )
280
241
w .Close ()
281
- return otto . TrueValue ()
242
+ return re . vm . ToValue ( true ), nil
282
243
}
283
244
}
284
245
@@ -382,31 +343,22 @@ func (re *JSRE) getTxArgs(jtx string) (*SendTxArgs, error) {
382
343
}
383
344
384
345
// []byte offlineWalletSignTx(string id, transaction tx, chainID int)
385
- func (re * JSRE ) offlineWalletSignTx (call otto.FunctionCall ) otto.Value {
386
- id , err := call .Argument (0 ).ToString ()
387
- if err != nil {
388
- throwJSException (err )
389
- }
390
- chainID , err := call .Argument (2 ).ToInteger ()
391
- if err != nil {
392
- throwJSException (err )
393
- }
346
+ func (re * JSRE ) offlineWalletSignTx (call Call ) (goja.Value , error ) {
347
+ id := call .Argument (0 ).ToString ().String ()
348
+ chainID := call .Argument (2 ).ToInteger ()
394
349
395
350
var (
396
351
tx * types.Transaction
397
352
input []byte
398
353
)
399
354
400
- // javascript json object -> string -> jsTx -> types.Transaction
401
- JSON , _ := call .Otto .Object ("JSON" )
402
- jtx , err := JSON .Call ("stringify" , call .Argument (1 ))
355
+ jtx , err := call .Argument (1 ).ToObject (re .vm ).MarshalJSON ()
403
356
if err != nil {
404
- throwJSException ( err )
357
+ return nil , err
405
358
}
406
-
407
- txargs , err := re .getTxArgs (jtx .String ())
359
+ txargs , err := re .getTxArgs (string (jtx ))
408
360
if err != nil {
409
- throwJSException ( err )
361
+ return nil , err
410
362
}
411
363
412
364
if txargs .Data != nil {
@@ -429,41 +381,31 @@ func (re *JSRE) offlineWalletSignTx(call otto.FunctionCall) otto.Value {
429
381
offlineWalletLock .Unlock ()
430
382
431
383
if ! ok {
432
- throwJSException (ethereum .NotFound )
433
- return otto .UndefinedValue ()
384
+ return nil , ethereum .NotFound
434
385
} else {
435
386
_ , stx , err := w .SignTx (accounts .DefaultBaseDerivationPath , tx ,
436
387
big .NewInt (chainID ))
437
388
if err != nil {
438
- throwJSException ( err )
389
+ return nil , err
439
390
}
440
391
data , err := rlp .EncodeToBytes (stx )
441
392
if err != nil {
442
- throwJSException ( err )
393
+ return nil , err
443
394
}
444
- v , err := otto .ToValue (hexutil .Encode (data ))
445
- if err != nil {
446
- throwJSException (err )
447
- }
448
- return v
395
+ return re .vm .ToValue (hexutil .Encode (data )), nil
449
396
}
450
397
}
451
398
452
399
// offlineWalletList returns the array of ledger or trezor device paths
453
400
// for mostly informational use
454
- func (re * JSRE ) offlineWalletList (call otto. FunctionCall ) otto .Value {
401
+ func (re * JSRE ) offlineWalletList (call Call ) (goja .Value , error ) {
455
402
scheme := ""
456
- if call .Argument (0 ).IsString () {
457
- scheme , _ = call .Argument (0 ).ToString ()
403
+ if len ( call .Arguments ) >= 1 && call . Argument (0 ).ToString () != nil {
404
+ scheme = call .Argument (0 ).ToString (). String ()
458
405
}
459
406
460
407
paths := usbwallet .ListDevices (scheme )
461
- env := otto .New ()
462
- v , err := env .ToValue (paths )
463
- if err != nil {
464
- throwJSException (err )
465
- }
466
- return v
408
+ return re .vm .ToValue (paths ), nil
467
409
}
468
410
469
411
// EOF
0 commit comments