@aioha/tx-digest 1.0.2 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,48 @@
1
+ # @aioha/tx-digest
2
+
3
+ Hive transaction serializer and digest. Useful for generating ID for a transaction. Serializers extracted from hive-tx and adapted for HF26 serialization. Intended for browser use as it uses `window.crypto` for sha256.
4
+
5
+ ## Usage
6
+
7
+ ```js
8
+ import { transactionDigest } from '@aioha/tx-digest'
9
+
10
+ // https://hafscan.techcoderx.com/tx/543058b4465cd93132b2843b751d5dcdd8efd341
11
+ const recurrent_transfer = {
12
+ ref_block_num: 23679,
13
+ ref_block_prefix: 291568045,
14
+ expiration: '2025-11-20T07:59:08',
15
+ operations: [
16
+ {
17
+ type: 'recurrent_transfer_operation',
18
+ value: {
19
+ to: 'techcoderx.vsc',
20
+ from: 'techcoderx',
21
+ memo: '',
22
+ amount: {
23
+ nai: '@@000000021',
24
+ amount: '2',
25
+ precision: 3
26
+ },
27
+ executions: 5,
28
+ extensions: [
29
+ {
30
+ type: 'recurrent_transfer_pair_id',
31
+ value: {
32
+ pair_id: 1
33
+ }
34
+ }
35
+ ],
36
+ recurrence: 48
37
+ }
38
+ }
39
+ ],
40
+ signatures: [
41
+ '1f3f2ddfc755936e0b998c78a1aec35a9663d1d9011cd4a83be84c6340f1b8689826fbd0e32c918afca38ab1e6ec44eac85a0e281c39bf1400f5e53c8b810a3bf5'
42
+ ],
43
+ extensions: []
44
+ }
45
+
46
+ const serialized = await transactionDigest(recurrent_transfer)
47
+ console.log(serialized.txId) // 85db372428a47aba8aeb154df8650a900c612fa5
48
+ ```
@@ -1,7 +1,25 @@
1
1
  import { PublicKey } from './PublicKey.js'
2
- import { Asset } from './Asset.js'
3
2
  import { HexBuffer } from './HexBuffer.js'
4
3
 
4
+ const Extensions = {
5
+ comment_payout_beneficiaries: 0,
6
+ update_proposal_end_date: 1,
7
+ recurrent_transfer_pair_id: 1
8
+ }
9
+
10
+ const NaiUint32 = (nai) => {
11
+ switch (nai) {
12
+ case '@@000000021': // HIVE
13
+ return 3200000035 // ((99999999+2) << 5) | 3
14
+ case '@@000000013': // HBD
15
+ return 3200000003 // ((99999999+1) << 5) | 3
16
+ case '@@000000037': // VESTS
17
+ return 3200000070 // ((99999999+3) << 5) | 6
18
+ default:
19
+ throw new Error('Invalid NAI')
20
+ }
21
+ }
22
+
5
23
  const VoidSerializer = () => {
6
24
  throw new Error('Void can not be serialized')
7
25
  }
@@ -45,36 +63,10 @@ const BooleanSerializer = (buffer, data) => {
45
63
  buffer.writeByte(data ? 1 : 0)
46
64
  }
47
65
 
48
- const StaticVariantSerializer = (itemSerializers) => {
66
+ const ExtensionSerializer = (itemSerializer) => {
49
67
  return (buffer, data) => {
50
- let id = data[0]
51
- const item = data[1]
52
- // id was/is supposed to be 0 or integer here but seems to have been changed in e.g. comment_options
53
- // extensions: [
54
- // [
55
- // "comment_payout_beneficiaries",
56
- // {
57
- // "beneficiaries": [
58
- // {
59
- // "account": "vimm",
60
- // "weight": 1000
61
- // }
62
- // ]
63
- // }
64
- // ]
65
- // ]
66
- // "comment_payout_beneficiaries" was 0 and at some point it got changed
67
- // It should still be serialized as a 0 or an integer
68
- // Now the question is, always 0? will need an example transaction to prove otherwise
69
- if (typeof id === 'string') {
70
- if (id === 'update_proposal_end_date') {
71
- id = 1
72
- } else {
73
- id = 0
74
- }
75
- }
76
- buffer.writeVarint32(id)
77
- itemSerializers[id](buffer, item)
68
+ buffer.writeVarint32(Extensions[data.type])
69
+ itemSerializer(buffer, data.value)
78
70
  }
79
71
  }
80
72
 
@@ -84,13 +76,8 @@ const StaticVariantSerializer = (itemSerializers) => {
84
76
  * Should not be a problem in real-word usage.
85
77
  */
86
78
  const AssetSerializer = (buffer, data) => {
87
- const asset = Asset.from(data)
88
- const precision = asset.getPrecision()
89
- buffer.writeInt64(Math.round(asset.amount * Math.pow(10, precision)))
90
- buffer.writeUint8(precision)
91
- for (let i = 0; i < 7; i++) {
92
- buffer.writeUint8(asset.symbol.charCodeAt(i) || 0)
93
- }
79
+ buffer.writeInt64(data.amount)
80
+ buffer.writeUint32(NaiUint32(data.nai))
94
81
  }
95
82
 
96
83
  const DateSerializer = (buffer, data) => {
@@ -206,7 +193,7 @@ const OperationDataSerializer = (operationId, definitions) => {
206
193
 
207
194
  const OperationSerializers = {}
208
195
 
209
- OperationSerializers.account_create = OperationDataSerializer(9, [
196
+ OperationSerializers.account_create_operation = OperationDataSerializer(9, [
210
197
  ['fee', AssetSerializer],
211
198
  ['creator', StringSerializer],
212
199
  ['new_account_name', StringSerializer],
@@ -217,7 +204,7 @@ OperationSerializers.account_create = OperationDataSerializer(9, [
217
204
  ['json_metadata', StringSerializer]
218
205
  ])
219
206
 
220
- OperationSerializers.account_create_with_delegation = OperationDataSerializer(41, [
207
+ OperationSerializers.account_create_with_delegation_operation = OperationDataSerializer(41, [
221
208
  ['fee', AssetSerializer],
222
209
  ['delegation', AssetSerializer],
223
210
  ['creator', StringSerializer],
@@ -230,7 +217,7 @@ OperationSerializers.account_create_with_delegation = OperationDataSerializer(41
230
217
  ['extensions', ArraySerializer(VoidSerializer)]
231
218
  ])
232
219
 
233
- OperationSerializers.account_update = OperationDataSerializer(10, [
220
+ OperationSerializers.account_update_operation = OperationDataSerializer(10, [
234
221
  ['account', StringSerializer],
235
222
  ['owner', OptionalSerializer(AuthoritySerializer)],
236
223
  ['active', OptionalSerializer(AuthoritySerializer)],
@@ -239,42 +226,42 @@ OperationSerializers.account_update = OperationDataSerializer(10, [
239
226
  ['json_metadata', StringSerializer]
240
227
  ])
241
228
 
242
- OperationSerializers.account_witness_proxy = OperationDataSerializer(13, [
229
+ OperationSerializers.account_witness_proxy_operation = OperationDataSerializer(13, [
243
230
  ['account', StringSerializer],
244
231
  ['proxy', StringSerializer]
245
232
  ])
246
233
 
247
- OperationSerializers.account_witness_vote = OperationDataSerializer(12, [
234
+ OperationSerializers.account_witness_vote_operation = OperationDataSerializer(12, [
248
235
  ['account', StringSerializer],
249
236
  ['witness', StringSerializer],
250
237
  ['approve', BooleanSerializer]
251
238
  ])
252
239
 
253
- OperationSerializers.cancel_transfer_from_savings = OperationDataSerializer(34, [
240
+ OperationSerializers.cancel_transfer_from_savings_operation = OperationDataSerializer(34, [
254
241
  ['from', StringSerializer],
255
242
  ['request_id', UInt32Serializer]
256
243
  ])
257
244
 
258
- OperationSerializers.change_recovery_account = OperationDataSerializer(26, [
245
+ OperationSerializers.change_recovery_account_operation = OperationDataSerializer(26, [
259
246
  ['account_to_recover', StringSerializer],
260
247
  ['new_recovery_account', StringSerializer],
261
248
  ['extensions', ArraySerializer(VoidSerializer)]
262
249
  ])
263
250
 
264
- OperationSerializers.claim_account = OperationDataSerializer(22, [
251
+ OperationSerializers.claim_account_operation = OperationDataSerializer(22, [
265
252
  ['creator', StringSerializer],
266
253
  ['fee', AssetSerializer],
267
254
  ['extensions', ArraySerializer(VoidSerializer)]
268
255
  ])
269
256
 
270
- OperationSerializers.claim_reward_balance = OperationDataSerializer(39, [
257
+ OperationSerializers.claim_reward_balance_operation = OperationDataSerializer(39, [
271
258
  ['account', StringSerializer],
272
259
  ['reward_hive', AssetSerializer],
273
260
  ['reward_hbd', AssetSerializer],
274
261
  ['reward_vests', AssetSerializer]
275
262
  ])
276
263
 
277
- OperationSerializers.comment = OperationDataSerializer(1, [
264
+ OperationSerializers.comment_operation = OperationDataSerializer(1, [
278
265
  ['parent_author', StringSerializer],
279
266
  ['parent_permlink', StringSerializer],
280
267
  ['author', StringSerializer],
@@ -284,7 +271,7 @@ OperationSerializers.comment = OperationDataSerializer(1, [
284
271
  ['json_metadata', StringSerializer]
285
272
  ])
286
273
 
287
- OperationSerializers.comment_options = OperationDataSerializer(19, [
274
+ OperationSerializers.comment_options_operation = OperationDataSerializer(19, [
288
275
  ['author', StringSerializer],
289
276
  ['permlink', StringSerializer],
290
277
  ['max_accepted_payout', AssetSerializer],
@@ -293,17 +280,17 @@ OperationSerializers.comment_options = OperationDataSerializer(19, [
293
280
  ['allow_curation_rewards', BooleanSerializer],
294
281
  [
295
282
  'extensions',
296
- ArraySerializer(StaticVariantSerializer([ObjectSerializer([['beneficiaries', ArraySerializer(BeneficiarySerializer)]])]))
283
+ ArraySerializer(ExtensionSerializer(ObjectSerializer([['beneficiaries', ArraySerializer(BeneficiarySerializer)]])))
297
284
  ]
298
285
  ])
299
286
 
300
- OperationSerializers.convert = OperationDataSerializer(8, [
287
+ OperationSerializers.convert_operation = OperationDataSerializer(8, [
301
288
  ['owner', StringSerializer],
302
289
  ['requestid', UInt32Serializer],
303
290
  ['amount', AssetSerializer]
304
291
  ])
305
292
 
306
- OperationSerializers.create_claimed_account = OperationDataSerializer(23, [
293
+ OperationSerializers.create_claimed_account_operation = OperationDataSerializer(23, [
307
294
  ['creator', StringSerializer],
308
295
  ['new_account_name', StringSerializer],
309
296
  ['owner', AuthoritySerializer],
@@ -314,13 +301,13 @@ OperationSerializers.create_claimed_account = OperationDataSerializer(23, [
314
301
  ['extensions', ArraySerializer(VoidSerializer)]
315
302
  ])
316
303
 
317
- OperationSerializers.custom = OperationDataSerializer(15, [
304
+ OperationSerializers.custom_operation = OperationDataSerializer(15, [
318
305
  ['required_auths', ArraySerializer(StringSerializer)],
319
306
  ['id', UInt16Serializer],
320
307
  ['data', VariableBinarySerializer]
321
308
  ])
322
309
 
323
- OperationSerializers.custom_binary = OperationDataSerializer(35, [
310
+ OperationSerializers.custom_binary_operation = OperationDataSerializer(35, [
324
311
  ['required_owner_auths', ArraySerializer(StringSerializer)],
325
312
  ['required_active_auths', ArraySerializer(StringSerializer)],
326
313
  ['required_posting_auths', ArraySerializer(StringSerializer)],
@@ -329,30 +316,30 @@ OperationSerializers.custom_binary = OperationDataSerializer(35, [
329
316
  ['data', VariableBinarySerializer]
330
317
  ])
331
318
 
332
- OperationSerializers.custom_json = OperationDataSerializer(18, [
319
+ OperationSerializers.custom_json_operation = OperationDataSerializer(18, [
333
320
  ['required_auths', ArraySerializer(StringSerializer)],
334
321
  ['required_posting_auths', ArraySerializer(StringSerializer)],
335
322
  ['id', StringSerializer],
336
323
  ['json', StringSerializer]
337
324
  ])
338
325
 
339
- OperationSerializers.decline_voting_rights = OperationDataSerializer(36, [
326
+ OperationSerializers.decline_voting_rights_operation = OperationDataSerializer(36, [
340
327
  ['account', StringSerializer],
341
328
  ['decline', BooleanSerializer]
342
329
  ])
343
330
 
344
- OperationSerializers.delegate_vesting_shares = OperationDataSerializer(40, [
331
+ OperationSerializers.delegate_vesting_shares_operation = OperationDataSerializer(40, [
345
332
  ['delegator', StringSerializer],
346
333
  ['delegatee', StringSerializer],
347
334
  ['vesting_shares', AssetSerializer]
348
335
  ])
349
336
 
350
- OperationSerializers.delete_comment = OperationDataSerializer(17, [
337
+ OperationSerializers.delete_comment_operation = OperationDataSerializer(17, [
351
338
  ['author', StringSerializer],
352
339
  ['permlink', StringSerializer]
353
340
  ])
354
341
 
355
- OperationSerializers.escrow_approve = OperationDataSerializer(31, [
342
+ OperationSerializers.escrow_approve_operation = OperationDataSerializer(31, [
356
343
  ['from', StringSerializer],
357
344
  ['to', StringSerializer],
358
345
  ['agent', StringSerializer],
@@ -361,7 +348,7 @@ OperationSerializers.escrow_approve = OperationDataSerializer(31, [
361
348
  ['approve', BooleanSerializer]
362
349
  ])
363
350
 
364
- OperationSerializers.escrow_dispute = OperationDataSerializer(28, [
351
+ OperationSerializers.escrow_dispute_operation = OperationDataSerializer(28, [
365
352
  ['from', StringSerializer],
366
353
  ['to', StringSerializer],
367
354
  ['agent', StringSerializer],
@@ -369,7 +356,7 @@ OperationSerializers.escrow_dispute = OperationDataSerializer(28, [
369
356
  ['escrow_id', UInt32Serializer]
370
357
  ])
371
358
 
372
- OperationSerializers.escrow_release = OperationDataSerializer(29, [
359
+ OperationSerializers.escrow_release_operation = OperationDataSerializer(29, [
373
360
  ['from', StringSerializer],
374
361
  ['to', StringSerializer],
375
362
  ['agent', StringSerializer],
@@ -380,30 +367,30 @@ OperationSerializers.escrow_release = OperationDataSerializer(29, [
380
367
  ['hive_amount', AssetSerializer]
381
368
  ])
382
369
 
383
- OperationSerializers.escrow_transfer = OperationDataSerializer(27, [
370
+ OperationSerializers.escrow_transfer_operation = OperationDataSerializer(27, [
384
371
  ['from', StringSerializer],
385
372
  ['to', StringSerializer],
386
- ['agent', StringSerializer],
387
- ['escrow_id', UInt32Serializer],
388
373
  ['hbd_amount', AssetSerializer],
389
374
  ['hive_amount', AssetSerializer],
375
+ ['escrow_id', UInt32Serializer],
376
+ ['agent', StringSerializer],
390
377
  ['fee', AssetSerializer],
378
+ ['json_meta', StringSerializer],
391
379
  ['ratification_deadline', DateSerializer],
392
- ['escrow_expiration', DateSerializer],
393
- ['json_meta', StringSerializer]
380
+ ['escrow_expiration', DateSerializer]
394
381
  ])
395
382
 
396
- OperationSerializers.feed_publish = OperationDataSerializer(7, [
383
+ OperationSerializers.feed_publish_operation = OperationDataSerializer(7, [
397
384
  ['publisher', StringSerializer],
398
385
  ['exchange_rate', PriceSerializer]
399
386
  ])
400
387
 
401
- OperationSerializers.limit_order_cancel = OperationDataSerializer(6, [
388
+ OperationSerializers.limit_order_cancel_operation = OperationDataSerializer(6, [
402
389
  ['owner', StringSerializer],
403
390
  ['orderid', UInt32Serializer]
404
391
  ])
405
392
 
406
- OperationSerializers.limit_order_create = OperationDataSerializer(5, [
393
+ OperationSerializers.limit_order_create_operation = OperationDataSerializer(5, [
407
394
  ['owner', StringSerializer],
408
395
  ['orderid', UInt32Serializer],
409
396
  ['amount_to_sell', AssetSerializer],
@@ -412,7 +399,7 @@ OperationSerializers.limit_order_create = OperationDataSerializer(5, [
412
399
  ['expiration', DateSerializer]
413
400
  ])
414
401
 
415
- OperationSerializers.limit_order_create2 = OperationDataSerializer(21, [
402
+ OperationSerializers.limit_order_create2_operation = OperationDataSerializer(21, [
416
403
  ['owner', StringSerializer],
417
404
  ['orderid', UInt32Serializer],
418
405
  ['amount_to_sell', AssetSerializer],
@@ -421,53 +408,53 @@ OperationSerializers.limit_order_create2 = OperationDataSerializer(21, [
421
408
  ['expiration', DateSerializer]
422
409
  ])
423
410
 
424
- OperationSerializers.recover_account = OperationDataSerializer(25, [
411
+ OperationSerializers.recover_account_operation = OperationDataSerializer(25, [
425
412
  ['account_to_recover', StringSerializer],
426
413
  ['new_owner_authority', AuthoritySerializer],
427
414
  ['recent_owner_authority', AuthoritySerializer],
428
415
  ['extensions', ArraySerializer(VoidSerializer)]
429
416
  ])
430
417
 
431
- OperationSerializers.report_over_production = OperationDataSerializer(16, [
418
+ OperationSerializers.report_over_production_operation = OperationDataSerializer(16, [
432
419
  ['reporter', StringSerializer],
433
420
  ['first_block', SignedBlockHeaderSerializer],
434
421
  ['second_block', SignedBlockHeaderSerializer]
435
422
  ])
436
423
 
437
- OperationSerializers.request_account_recovery = OperationDataSerializer(24, [
424
+ OperationSerializers.request_account_recovery_operation = OperationDataSerializer(24, [
438
425
  ['recovery_account', StringSerializer],
439
426
  ['account_to_recover', StringSerializer],
440
427
  ['new_owner_authority', AuthoritySerializer],
441
428
  ['extensions', ArraySerializer(VoidSerializer)]
442
429
  ])
443
430
 
444
- OperationSerializers.reset_account = OperationDataSerializer(37, [
431
+ OperationSerializers.reset_account_operation = OperationDataSerializer(37, [
445
432
  ['reset_account', StringSerializer],
446
433
  ['account_to_reset', StringSerializer],
447
434
  ['new_owner_authority', AuthoritySerializer]
448
435
  ])
449
436
 
450
- OperationSerializers.set_reset_account = OperationDataSerializer(38, [
437
+ OperationSerializers.set_reset_account_operation = OperationDataSerializer(38, [
451
438
  ['account', StringSerializer],
452
439
  ['current_reset_account', StringSerializer],
453
440
  ['reset_account', StringSerializer]
454
441
  ])
455
442
 
456
- OperationSerializers.set_withdraw_vesting_route = OperationDataSerializer(20, [
443
+ OperationSerializers.set_withdraw_vesting_route_operation = OperationDataSerializer(20, [
457
444
  ['from_account', StringSerializer],
458
445
  ['to_account', StringSerializer],
459
446
  ['percent', UInt16Serializer],
460
447
  ['auto_vest', BooleanSerializer]
461
448
  ])
462
449
 
463
- OperationSerializers.transfer = OperationDataSerializer(2, [
450
+ OperationSerializers.transfer_operation = OperationDataSerializer(2, [
464
451
  ['from', StringSerializer],
465
452
  ['to', StringSerializer],
466
453
  ['amount', AssetSerializer],
467
454
  ['memo', StringSerializer]
468
455
  ])
469
456
 
470
- OperationSerializers.transfer_from_savings = OperationDataSerializer(33, [
457
+ OperationSerializers.transfer_from_savings_operation = OperationDataSerializer(33, [
471
458
  ['from', StringSerializer],
472
459
  ['request_id', UInt32Serializer],
473
460
  ['to', StringSerializer],
@@ -475,32 +462,32 @@ OperationSerializers.transfer_from_savings = OperationDataSerializer(33, [
475
462
  ['memo', StringSerializer]
476
463
  ])
477
464
 
478
- OperationSerializers.transfer_to_savings = OperationDataSerializer(32, [
465
+ OperationSerializers.transfer_to_savings_operation = OperationDataSerializer(32, [
479
466
  ['from', StringSerializer],
480
467
  ['to', StringSerializer],
481
468
  ['amount', AssetSerializer],
482
469
  ['memo', StringSerializer]
483
470
  ])
484
471
 
485
- OperationSerializers.transfer_to_vesting = OperationDataSerializer(3, [
472
+ OperationSerializers.transfer_to_vesting_operation = OperationDataSerializer(3, [
486
473
  ['from', StringSerializer],
487
474
  ['to', StringSerializer],
488
475
  ['amount', AssetSerializer]
489
476
  ])
490
477
 
491
- OperationSerializers.vote = OperationDataSerializer(0, [
478
+ OperationSerializers.vote_operation = OperationDataSerializer(0, [
492
479
  ['voter', StringSerializer],
493
480
  ['author', StringSerializer],
494
481
  ['permlink', StringSerializer],
495
482
  ['weight', Int16Serializer]
496
483
  ])
497
484
 
498
- OperationSerializers.withdraw_vesting = OperationDataSerializer(4, [
485
+ OperationSerializers.withdraw_vesting_operation = OperationDataSerializer(4, [
499
486
  ['account', StringSerializer],
500
487
  ['vesting_shares', AssetSerializer]
501
488
  ])
502
489
 
503
- OperationSerializers.witness_update = OperationDataSerializer(11, [
490
+ OperationSerializers.witness_update_operation = OperationDataSerializer(11, [
504
491
  ['owner', StringSerializer],
505
492
  ['url', StringSerializer],
506
493
  ['block_signing_key', PublicKeySerializer],
@@ -508,13 +495,13 @@ OperationSerializers.witness_update = OperationDataSerializer(11, [
508
495
  ['fee', AssetSerializer]
509
496
  ])
510
497
 
511
- OperationSerializers.witness_set_properties = OperationDataSerializer(42, [
498
+ OperationSerializers.witness_set_properties_operation = OperationDataSerializer(42, [
512
499
  ['owner', StringSerializer],
513
500
  ['props', FlatMapSerializer(StringSerializer, VariableBinarySerializer)],
514
501
  ['extensions', ArraySerializer(VoidSerializer)]
515
502
  ])
516
503
 
517
- OperationSerializers.account_update2 = OperationDataSerializer(43, [
504
+ OperationSerializers.account_update2_operation = OperationDataSerializer(43, [
518
505
  ['account', StringSerializer],
519
506
  ['owner', OptionalSerializer(AuthoritySerializer)],
520
507
  ['active', OptionalSerializer(AuthoritySerializer)],
@@ -525,7 +512,7 @@ OperationSerializers.account_update2 = OperationDataSerializer(43, [
525
512
  ['extensions', ArraySerializer(VoidSerializer)]
526
513
  ])
527
514
 
528
- OperationSerializers.create_proposal = OperationDataSerializer(44, [
515
+ OperationSerializers.create_proposal_operation = OperationDataSerializer(44, [
529
516
  ['creator', StringSerializer],
530
517
  ['receiver', StringSerializer],
531
518
  ['start_date', DateSerializer],
@@ -536,14 +523,14 @@ OperationSerializers.create_proposal = OperationDataSerializer(44, [
536
523
  ['extensions', ArraySerializer(VoidSerializer)]
537
524
  ])
538
525
 
539
- OperationSerializers.update_proposal_votes = OperationDataSerializer(45, [
526
+ OperationSerializers.update_proposal_votes_operation = OperationDataSerializer(45, [
540
527
  ['voter', StringSerializer],
541
528
  ['proposal_ids', ArraySerializer(Int64Serializer)],
542
529
  ['approve', BooleanSerializer],
543
530
  ['extensions', ArraySerializer(VoidSerializer)]
544
531
  ])
545
532
 
546
- OperationSerializers.remove_proposal = OperationDataSerializer(46, [
533
+ OperationSerializers.remove_proposal_operation = OperationDataSerializer(46, [
547
534
  ['proposal_owner', StringSerializer],
548
535
  ['proposal_ids', ArraySerializer(Int64Serializer)],
549
536
  ['extensions', ArraySerializer(VoidSerializer)]
@@ -551,40 +538,40 @@ OperationSerializers.remove_proposal = OperationDataSerializer(46, [
551
538
 
552
539
  const ProposalUpdateSerializer = ObjectSerializer([['end_date', DateSerializer]])
553
540
 
554
- OperationSerializers.update_proposal = OperationDataSerializer(47, [
541
+ OperationSerializers.update_proposal_operation = OperationDataSerializer(47, [
555
542
  ['proposal_id', UInt64Serializer],
556
543
  ['creator', StringSerializer],
557
544
  ['daily_pay', AssetSerializer],
558
545
  ['subject', StringSerializer],
559
546
  ['permlink', StringSerializer],
560
- ['extensions', ArraySerializer(StaticVariantSerializer([VoidSerializer, ProposalUpdateSerializer]))]
547
+ ['extensions', ArraySerializer(ExtensionSerializer(ProposalUpdateSerializer))]
561
548
  ])
562
549
 
563
- OperationSerializers.collateralized_convert = OperationDataSerializer(48, [
550
+ OperationSerializers.collateralized_convert_operation = OperationDataSerializer(48, [
564
551
  ['owner', StringSerializer],
565
552
  ['requestid', UInt32Serializer],
566
553
  ['amount', AssetSerializer]
567
554
  ])
568
555
 
569
- OperationSerializers.recurrent_transfer = OperationDataSerializer(49, [
556
+ OperationSerializers.recurrent_transfer_operation = OperationDataSerializer(49, [
570
557
  ['from', StringSerializer],
571
558
  ['to', StringSerializer],
572
559
  ['amount', AssetSerializer],
573
560
  ['memo', StringSerializer],
574
561
  ['recurrence', UInt16Serializer],
575
562
  ['executions', UInt16Serializer],
576
- ['extensions', ArraySerializer(VoidSerializer)]
563
+ ['extensions', ArraySerializer(ExtensionSerializer(ObjectSerializer([['pair_id', UInt8Serializer]])))]
577
564
  ])
578
565
 
579
566
  const OperationSerializer = (buffer, operation) => {
580
- const serializer = OperationSerializers[operation[0]]
567
+ const serializer = OperationSerializers[operation.type]
581
568
  if (!serializer) {
582
- throw new Error(`No serializer for operation: ${operation[0]}`)
569
+ throw new Error(`No serializer for operation: ${operation.type}`)
583
570
  }
584
571
  try {
585
- serializer(buffer, operation[1])
572
+ serializer(buffer, operation.value)
586
573
  } catch (error) {
587
- error.message = `${operation[0]}: ${error.message}`
574
+ error.message = `${operation.type}: ${error.message}`
588
575
  throw error
589
576
  }
590
577
  }
@@ -623,7 +610,7 @@ export const Serializer = {
623
610
  Optional: OptionalSerializer,
624
611
  Price: PriceSerializer,
625
612
  PublicKey: PublicKeySerializer,
626
- StaticVariant: StaticVariantSerializer,
613
+ Extension: ExtensionSerializer,
627
614
  String: StringSerializer,
628
615
  Transaction: TransactionSerializer,
629
616
  UInt16: UInt16Serializer,
package/index.d.ts CHANGED
@@ -4,4 +4,5 @@ export const transactionDigest: (
4
4
  ) => Promise<{
5
5
  digest: Uint8Array
6
6
  txId: string
7
+ bin: Uint8Array<ArrayBuffer>
7
8
  }>
package/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { ByteBuffer } from './helpers/ByteBuffer.js'
2
2
  import { Serializer } from './helpers/serializer.js'
3
3
  import { hexToUint8Array, uint8ArrayToHex } from './helpers/uint8Array.js'
4
- // import { sha256 } from '@noble/hashes/sha256'
4
+ // import { sha256 } from '@noble/hashes/sha2'
5
5
 
6
6
  export const sha256 = async (message) => {
7
7
  const hashBuffer = await window.crypto.subtle.digest('SHA-256', message)
@@ -20,7 +20,8 @@ export const transactionDigest = async (transaction, chainId = CHAIN_ID) => {
20
20
  }
21
21
  buffer.flip()
22
22
  const transactionData = new Uint8Array(buffer.toBuffer())
23
+ const bin = new Uint8Array([...chainId, ...transactionData])
23
24
  const txId = uint8ArrayToHex(await sha256(transactionData)).slice(0, 40)
24
- const digest = await sha256(new Uint8Array([...chainId, ...transactionData]))
25
- return { digest, txId }
25
+ const digest = await sha256(bin)
26
+ return { digest, txId, bin }
26
27
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aioha/tx-digest",
3
- "version": "1.0.2",
3
+ "version": "2.0.1",
4
4
  "description": "Hive transaction serializer and digest",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
package/helpers/Asset.js DELETED
@@ -1,69 +0,0 @@
1
- /** Class representing a hive asset,
2
- * e.g. `1.000 HIVE` or `12.112233 VESTS`. */
3
- export class Asset {
4
- /** Create a new Asset instance from a string, e.g. `42.000 HIVE`. */
5
- static fromString(string, expectedSymbol = null) {
6
- const [amountString, symbol] = string.split(' ')
7
- if (['STEEM', 'VESTS', 'SBD', 'TESTS', 'TBD', 'HIVE', 'HBD'].indexOf(symbol) === -1) {
8
- throw new Error(`Invalid asset symbol: ${symbol}`)
9
- }
10
- if (expectedSymbol && symbol !== expectedSymbol) {
11
- throw new Error(`Invalid asset, expected symbol: ${expectedSymbol} got: ${symbol}`)
12
- }
13
- const amount = Number.parseFloat(amountString)
14
- if (!Number.isFinite(amount)) {
15
- throw new Error(`Invalid asset amount: ${amountString}`)
16
- }
17
- return new Asset(amount, symbol)
18
- }
19
-
20
- /**
21
- * Convenience to create new Asset.
22
- * @param symbol Symbol to use when created from number. Will also be used to validate
23
- * the asset, throws if the passed value has a different symbol than this.
24
- */
25
- static from(value, symbol = null) {
26
- if (value instanceof Asset) {
27
- if (symbol && value.symbol !== symbol) {
28
- throw new Error(`Invalid asset, expected symbol: ${symbol} got: ${value.symbol}`)
29
- }
30
- return value
31
- } else if (typeof value === 'number' && Number.isFinite(value)) {
32
- return new Asset(value, symbol || 'STEEM')
33
- } else if (typeof value === 'string') {
34
- return Asset.fromString(value, symbol)
35
- } else {
36
- throw new Error(`Invalid asset '${String(value)}'`)
37
- }
38
- }
39
-
40
- // We convert HIVE & HBD strings to STEEM & SBD because the serialization should be based on STEEM & SBD
41
- constructor(amount, symbol) {
42
- this.amount = amount
43
- this.symbol = symbol === 'HIVE' ? 'STEEM' : symbol === 'HBD' ? 'SBD' : symbol
44
- }
45
-
46
- /** Return asset precision. */
47
- getPrecision() {
48
- switch (this.symbol) {
49
- case 'TESTS':
50
- case 'TBD':
51
- case 'STEEM':
52
- case 'SBD':
53
- case 'HBD':
54
- case 'HIVE':
55
- return 3
56
- case 'VESTS':
57
- return 6
58
- }
59
- }
60
-
61
- /** Return a string representation of this asset, e.g. `42.000 HIVE`. */
62
- toString() {
63
- return `${this.amount.toFixed(this.getPrecision())} ${this.symbol}`
64
- }
65
-
66
- toJSON() {
67
- return this.toString()
68
- }
69
- }