@@ -24,7 +24,7 @@ function numberToBuffer(value) {
24
24
bytes [ i ] = byte ;
25
25
value = ( value - byte ) / 256 ;
26
26
}
27
- return new Buffer ( bytes ) ;
27
+ return new Buffer . from ( bytes ) ;
28
28
}
29
29
30
30
/**
@@ -43,43 +43,55 @@ function numberToBuffer(value) {
43
43
*/
44
44
const Transaction = module . exports = class Transaction {
45
45
constructor ( data ) {
46
- this . type = data . type !== undefined ? data . type : 0 ; // default to token transfer
47
- this . from = data . from ;
48
- this . to = data . to || '' ;
49
- this . value = data . value ;
50
- this . time = data . time !== undefined ? data . time : new Date ( ) ;
51
-
52
- this . code = data . code ;
53
- this . abi = data . abi ;
54
- this . method = data . method ;
55
- this . params = data . params ;
56
-
57
- this . hash = data . hash ;
58
- this . signature = data . signature ;
59
- this . address = data . address ;
60
- this . _id = data . id ;
61
-
62
- this . hertz = data . hertz ;
46
+ // Object argument is a full transaction
47
+ if ( Object . prototype . toString . call ( data ) === '[object Object]' ) {
48
+ this . type = data . type !== undefined ? data . type : 0 ; // default to token transfer
49
+ this . from = data . from ;
50
+ this . to = data . to || '' ;
51
+ this . value = data . value ;
52
+ this . time = data . time !== undefined ? data . time : new Date ( ) ;
53
+
54
+ this . code = data . code ;
55
+ this . abi = data . abi ;
56
+ this . method = data . method ;
57
+ this . params = data . params ;
58
+
59
+ this . hash = data . hash ;
60
+ this . signature = data . signature ;
61
+ this . address = data . address ;
62
+ this . gossip = data . gossip ;
63
+ this . _id = data . id ;
64
+
65
+ this . hertz = data . hertz ;
66
+
67
+ // String argument is assumed to be a hash
68
+ } else if ( Object . prototype . toString . call ( data ) === '[object String]' ) {
69
+ this . hash = data ;
70
+ }
63
71
}
64
72
65
73
set type ( type ) {
66
- assert . isNumber ( type , messages . TRANSACTION_TYPE_ISNUMBERINRANGE ) ;
67
- assert . isNumberInRange ( type , 0 , 2 , messages . TRANSACTION_TYPE_ISNUMBERINRANGE ) ;
68
- this . _type = type ;
74
+ if ( type !== undefined ) {
75
+ assert . isNumber ( type , messages . TRANSACTION_TYPE_ISNUMBERINRANGE ) ;
76
+ assert . isNumberInRange ( type , 0 , 2 , messages . TRANSACTION_TYPE_ISNUMBERINRANGE ) ;
77
+ this . _type = type ;
78
+ }
69
79
}
70
80
get type ( ) {
71
81
return this . _type || 0 ;
72
82
}
73
83
get typeBuffer ( ) {
74
- return Buffer . from ( ( '0' + this . type ) . slice ( - 2 ) , 'hex' ) ;
84
+ return new Buffer . from ( ( '0' + this . type ) . slice ( - 2 ) , 'hex' ) ;
75
85
}
76
86
77
87
set from ( from ) {
78
- assert . isAccountable ( from , messages . TRANSACTION_FROM_ISACCOUNTABLE ) ;
79
- if ( from . constructor . name !== 'Account' ) {
80
- this . _from = new Account ( from ) ;
81
- } else {
82
- this . _from = from ;
88
+ if ( from !== undefined ) {
89
+ assert . isAccountable ( from , messages . TRANSACTION_FROM_ISACCOUNTABLE ) ;
90
+ if ( from . constructor . name !== 'Account' ) {
91
+ this . _from = new Account ( from ) ;
92
+ } else {
93
+ this . _from = from ;
94
+ }
83
95
}
84
96
}
85
97
get from ( ) {
@@ -145,18 +157,24 @@ const Transaction = module.exports = class Transaction {
145
157
set abi ( abi ) {
146
158
if ( abi !== undefined ) {
147
159
if ( Object . prototype . toString . call ( abi ) === '[object String]' ) {
148
- abi = JSON . parse ( abi ) ;
160
+ try {
161
+ abi = JSON . parse ( abi ) ;
162
+ } catch ( e ) {
163
+ abi = undefined ;
164
+ }
165
+ }
166
+ if ( abi !== undefined ) {
167
+ assert . isArray ( abi , messages . TRANSACTION_ABI_ISARRAY ) ;
168
+ this . _abi = abi ;
149
169
}
150
- assert . isArray ( abi , messages . TRANSACTION_ABI_ISARRAY ) ;
151
- this . _abi = abi ;
152
170
}
153
171
}
154
172
get abi ( ) {
155
173
return this . _abi || [ ] ;
156
174
}
157
175
get abiString ( ) {
158
176
if ( this . type === 2 ) {
159
- return new Buffer ( JSON . stringify ( this . abi ) ) . toString ( 'hex' ) ;
177
+ return new Buffer . from ( JSON . stringify ( this . abi ) ) . toString ( 'hex' ) ;
160
178
}
161
179
return JSON . stringify ( this . abi ) ;
162
180
}
@@ -191,17 +209,19 @@ const Transaction = module.exports = class Transaction {
191
209
if ( this . _hash !== undefined ) {
192
210
return this . _hash ;
193
211
}
194
- assert . isString ( this . from . address , messages . TRANSACTION_FROMACCOUNT_ISVALID ) ;
195
- this . _hash = keccak ( 'keccak256' ) . update ( Buffer . concat ( [
196
- this . typeBuffer ,
197
- new Buffer . from ( this . from . address , 'hex' ) ,
198
- new Buffer . from ( this . to . address !== null ? this . to . address : '' , 'hex' ) ,
199
- new numberToBuffer ( this . value ) ,
200
- new Buffer . from ( this . code , 'hex' ) ,
201
- new Buffer . from ( this . abiString ) ,
202
- new Buffer . from ( this . method ) ,
203
- this . timeBuffer
204
- ] ) ) . digest ( ) . toString ( 'hex' ) ;
212
+ if ( this . from !== undefined ) {
213
+ assert . isString ( this . from . address , messages . TRANSACTION_FROMACCOUNT_ISVALID ) ;
214
+ this . _hash = keccak ( 'keccak256' ) . update ( Buffer . concat ( [
215
+ this . typeBuffer ,
216
+ new Buffer . from ( this . from . address , 'hex' ) ,
217
+ new Buffer . from ( this . to . address !== null ? this . to . address : '' , 'hex' ) ,
218
+ new numberToBuffer ( this . value ) ,
219
+ new Buffer . from ( this . code , 'hex' ) ,
220
+ new Buffer . from ( this . abiString ) ,
221
+ new Buffer . from ( this . method ) ,
222
+ this . timeBuffer
223
+ ] ) ) . digest ( ) . toString ( 'hex' ) ;
224
+ }
205
225
return this . _hash ;
206
226
}
207
227
get id ( ) {
@@ -218,14 +238,14 @@ const Transaction = module.exports = class Transaction {
218
238
if ( this . _signature !== undefined ) {
219
239
return this . _signature ;
220
240
}
221
- if ( this . from . privateKey !== undefined ) {
241
+ if ( this . from !== undefined && this . from . privateKey !== undefined ) {
222
242
const sig = secp256k1 . sign ( Buffer . from ( this . hash , 'hex' ) , Buffer . from ( this . from . privateKey , 'hex' ) ) ;
223
243
const signatureBytes = new Uint8Array ( 65 ) ;
224
244
for ( let i = 0 ; i < 64 ; i ++ ) {
225
245
signatureBytes [ i ] = sig . signature [ i ] ;
226
246
}
227
247
signatureBytes [ 64 ] = sig . recovery ;
228
- this . _signature = new Buffer ( signatureBytes ) . toString ( 'hex' ) ;
248
+ this . _signature = new Buffer . from ( signatureBytes ) . toString ( 'hex' ) ;
229
249
}
230
250
return this . _signature ;
231
251
}
@@ -250,8 +270,8 @@ const Transaction = module.exports = class Transaction {
250
270
return {
251
271
hash : this . hash ,
252
272
type : this . type ,
253
- from : this . from . address ,
254
- to : this . to . address ,
273
+ from : this . from !== undefined ? this . from . address : null ,
274
+ to : this . to !== undefined ? this . to . address : null ,
255
275
value : this . value ,
256
276
code : this . code ,
257
277
abi : this . abiString ,
@@ -260,9 +280,10 @@ const Transaction = module.exports = class Transaction {
260
280
time : + ( this . time ) ,
261
281
signature : this . signature ,
262
282
hertz : this . hertz || 0 ,
263
- fromName : this . from . name ,
264
- toName : this . to . name ,
265
- address : this . address
283
+ fromName : this . from !== undefined ? this . from . name : null ,
284
+ toName : this . to !== undefined ? this . to . name : null ,
285
+ address : this . address ,
286
+ gossip : this . gossip
266
287
} ;
267
288
}
268
289
@@ -294,6 +315,7 @@ const Transaction = module.exports = class Transaction {
294
315
*/
295
316
send ( ) {
296
317
if ( ! this . _sendCall ) {
318
+ assert . exists ( this . from , messages . TRANSACTION_FROM_ISACCOUNTABLE ) ;
297
319
let network = new Network ( ) ;
298
320
this . _sendCall = network . postToDelegate (
299
321
{
@@ -342,13 +364,23 @@ const Transaction = module.exports = class Transaction {
342
364
path : '/' + network . config . apiVersion + network . config . routes . transactionStatus + this . id
343
365
}
344
366
) . then ( ( d ) => {
345
- if ( d . data && d . data . status ) {
346
- if ( d . data . contractAddress ) {
347
- this . address = d . data . contractAddress ;
348
- }
349
- resolve ( d . data ) ;
350
- } else {
367
+ if ( d . status === undefined || d . status !== 'Ok' ) {
351
368
reject ( d ) ;
369
+ } else {
370
+ if ( d . data && d . data . receipt && d . data . receipt . status === 'Ok' ) {
371
+ if ( d . data . receipt . contractAddress ) {
372
+ this . address = d . data . receipt . contractAddress ;
373
+ }
374
+ if ( d . data . abi ) {
375
+ this . abi = d . data . abi ;
376
+ }
377
+ if ( d . data . gossip ) {
378
+ this . gossip = d . data . gossip ;
379
+ }
380
+ resolve ( d . data ) ;
381
+ } else {
382
+ reject ( d ) ;
383
+ }
352
384
}
353
385
delete this . _statusCall ;
354
386
} , ( e ) => {
@@ -412,10 +444,10 @@ const Transaction = module.exports = class Transaction {
412
444
}
413
445
self . status ( )
414
446
. then ( ( data ) => {
415
- if ( data . status === status ) {
447
+ if ( data . receipt . status === status ) {
416
448
resolve ( data ) ;
417
449
} else {
418
- if ( [ 'Pending' , 'NotFound' ] . indexOf ( data . status ) > - 1 ) {
450
+ if ( [ 'Pending' , 'NotFound' ] . indexOf ( data . receipt . status ) > - 1 ) {
419
451
last = data ;
420
452
setTimeout ( getStatus , 500 ) ;
421
453
} else {
@@ -424,7 +456,12 @@ const Transaction = module.exports = class Transaction {
424
456
}
425
457
} )
426
458
. catch ( ( e ) => {
427
- reject ( e ) ;
459
+ if ( [ 'NotFound' ] . indexOf ( e . status ) > - 1 ) {
460
+ last = e ;
461
+ setTimeout ( getStatus , 500 ) ;
462
+ } else {
463
+ reject ( e ) ;
464
+ }
428
465
} ) ;
429
466
}
430
467
getStatus ( ) ;
@@ -487,7 +524,7 @@ const Transaction = module.exports = class Transaction {
487
524
errors : [ ] ,
488
525
warnings : [ ]
489
526
} ;
490
- if ( compiled . contracts . source ) {
527
+ if ( compiled . contracts !== undefined && compiled . contracts . source !== undefined ) {
491
528
Object . keys ( compiled . contracts . source ) . forEach ( ( k ) => {
492
529
ret . contracts . push ( {
493
530
contract : k ,
@@ -528,6 +565,7 @@ const Transaction = module.exports = class Transaction {
528
565
return findImports ( path ) ;
529
566
}
530
567
} ;
568
+ input . language = 'Solidity' ;
531
569
input . settings = Object . assign ( {
532
570
optimizer : { enabled : true } ,
533
571
outputSelection : {
0 commit comments