-
Notifications
You must be signed in to change notification settings - Fork 7
/
factom.d.ts
1879 lines (1642 loc) · 58.8 KB
/
factom.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Type definitions for factomjs
// Project: https://github.com/PaulBernier/factomjs
// Definitions by: Schalk Bower <https://github.com/schalk-b>, Paul Bernier <https://github.com/PaulBernier>
import { EventEmitter } from 'events';
export = factom;
declare namespace factom {
/**
* Describe the options of connection to factomd or factom-walletd.
*
* @example
* const cli = new FactomdCli({
* host: '52.202.51.228',
* port: 8088,
* path: '/',
* debugPath: '/debug',
* user: 'paul',
* password: 'pwd',
* protocol: 'https',
* rejectUnauthorized: false,
* timeout: 5000,
* retry: {
* retries: 3,
* factor: 2,
* minTimeout: 500,
* maxTimeout: 2000
* }
* });
*/
interface ConnectionOptions {
/**
* IP or hostname. Default to localhost
*/
host?: string;
/**
* Port. Default to 8088 for factomd and 8089 for walletd.
*/
port?: number;
/**
* Path to V2 API. Default to /v2.
*/
path?: string;
/**
* Path to debug API. Default to /debug.
*/
debugPath?: string;
/**
* User for basic authentication.
*/
user?: string;
/**
* Password for basic authentication.
*/
password?: string;
/**
* http or https. Default to http.
*/
protocol?: string;
/**
* Set to false to allow connection to a node with a self-signed certificate. Default to true.
*/
rejectUnauthorized?: boolean;
/**
* Specifies the number of milliseconds before any API request times out.
* If a request takes longer than `timeout`, the request will be aborted. Default is `0` (no timeout).
*/
timeout?: number;
/**
* Retry strategy. For the detail of the options see https://github.com/tim-kos/node-retry#retrytimeoutsoptions. Default to {retries: 3, factor: 2, minTimeout: 500, maxTimeout: 2000}
*/
retry?: {
retries?: number;
factor?: number;
minTimeout?: number;
maxTimeout?: number;
};
}
interface AddOptions {
/**
* Time to wait in seconds for the commit ack. If negative value, doesn't wait for ack.
*/
commitTimeout?: number;
/**
* Time to wait in seconds for the reveal ack. If negative value, doesn't wait for ack.
*/
revealTimeout?: number;
/**
* Only if the obj argument is an iterable. Limits the number of concurrent Promises adding entries/chains.
*/
concurrency?: number;
/**
* Skip the validation that the EC address holds enough Entry Credits to pay for the commit.
*/
skipFundValidation?: boolean;
/**
* Signing function.
* Takes as input the data to sign with the EC public key paying for the commmit
* and should return its signature as a Buffer or a hex encoded string (or a Promise of those).
* The returned signature must have been made by the private key corresponding to the ecAddress argument.
*/
sign?: (data: Buffer) => Buffer | string | Promise<Buffer | string>;
}
interface AddResponse {
/**
* Transaction ID (commit)
*/
txId: string;
/**
* If it is a repeated commit (https://docs.factom.com/api#repeated-commit)
*/
repeatedCommit: boolean;
/**
* Chain id
*/
chainId: string;
/**
* Entry hash
*/
entryHash: string;
}
interface CommitOptions {
/**
* Time to wait in seconds for the commit ack. If negative value, doesn't wait for ack.
*/
ackTimeout?: number;
/**
* Signing function.
* Takes as input the data to sign with the EC public key paying for the commmit
* and should return its signature as a Buffer or a hex encoded string (or a Promise of those).
* The returned signature must have been made by the private key corresponding to the ecAddress argument.
*/
sign?: (data: Buffer) => Buffer | string | Promise<Buffer | string>;
}
/**
* Main class to read and write data from Factom blockchain.
* @example
* const cli = new FactomCli({
* factomd: {
* host: 'api.factomd.net',
* port: 443,
* protocol: 'https'
* },
* walletd: {
* host: 'localhost',
* user: 'paul',
* password: 'pass'
* }
* });
*/
export class FactomCli {
/**
* @param opts Options of connection to factomd and factom-walletd.
*/
constructor(
opts?:
| ConnectionOptions
| {
factomd?: ConnectionOptions;
walletd?: ConnectionOptions;
}
);
/**
*
* @param obj Entry or Chain to commit.
* @param ecAddress Entry Credit address that pays for the commit, either private (Es) or public (EC).
* If a public address is passed, the private key must either be stored in factom-walletd or
* a sign function must be provided as part of the options.
* @param options
*/
commit(
obj: Chain | Entry,
ecAddress: string,
options?: CommitOptions
): Promise<{
/**
* Transaction ID. This is undefined if this is a repeat commit.
*/
txId?: string;
/**
* If this is a repeated commit (https://docs.factom.com/api#repeated-commit). If repeatedCommit is true, txId is undefined.
*/
repeatedCommit: boolean;
}>;
/**
* Commit a Chain.
*
* @param chain Chain to commit.
* @param ecAddress Entry Credit address that pays for the commit, either private (Es) or public (EC).
* If a public address is passed, the private key must either be stored in factom-walletd or
* a sign function must be provided as part of the options.
* @param options
*/
commitChain(
chain: Chain,
ecAddress: string,
options?: CommitOptions
): Promise<{
/**
* Transaction ID. This is undefined if this is a repeat commit.
*/
txId?: string;
/**
* If this is a repeated commit (https://docs.factom.com/api#repeated-commit). If repeatedCommit is true, txId is undefined.
*/
repeatedCommit: boolean;
}>;
/**
* Commit an Entry.
*
* @param entry Entry to commit.
* @param ecAddress Entry Credit address that pays for the commit, either private (Es) or public (EC).
* If a public address is passed, the private key must either be stored in factom-walletd or
* a sign function must be provided as part of the options.
* @param options
*/
commitEntry(
entry: Entry,
ecAddress: string,
options?: CommitOptions
): Promise<{
/**
* Transaction ID. This is undefined if this is a repeat commit.
*/
txId?: string;
/**
* If this is a repeated commit (https://docs.factom.com/api#repeated-commit). If repeatedCommit is true, txId is undefined.
*/
repeatedCommit: boolean;
}>;
/**
* Reveal an Entry or Chain.
*
* @param obj Entry or Chain to reveal.
* @param revealAckTimeout Time to wait in seconds for the reveal ack. If negative value, doesn't wait for ack. Defaults to 60.
*/
reveal(
obj: Entry | Chain,
revealAckTimeout?: number
): Promise<{ chainId: string; entryHash: string }>;
/**
* Reveal a Chain.
*
* @param chain Chain to reveal.
* @param revealAckTimeout Time to wait in seconds for the reveal ack. If negative value, doesn't wait for ack. Defaults to 60.
*/
revealChain(
chain: Chain,
revealAckTimeout?: number
): Promise<{ chainId: string; entryHash: string }>;
/**
* Reveal a Entry.
*
* @param entry Entry to reveal.
* @param revealAckTimeout Time to wait in seconds for the reveal ack. If negative value, doesn't wait for ack. Defaults to 60.
*/
revealEntry(
entry: Entry,
revealAckTimeout?: number
): Promise<{ chainId: string; entryHash: string }>;
/**
* Add an Entry/Chain or a collection of either of those to the Factom blockchain. Performs both commits and reveals.
*
* @param obj Entry/Chain or array of Entry/Chain to add.
* @param ecAddress Entry Credit address that pays for the commit, either private (Es) or public (EC).
* If a public address is passed, the private key must either be stored in factom-walletd or
* a sign function must be provided as part of the options.
* @param options
*/
add(
obj: Chain | Chain[] | Entry | Entry[],
ecAddress: string,
options?: AddOptions
): Promise<AddResponse | AddResponse[]>;
/**
* Add a Chain to the Factom blockchain. Performs both commit and reveal.
*
* @param chain Chain to add.
* @param ecAddress Entry Credit address that pays for the commit, either private (Es) or public (EC).
* If a public address is passed, the private key must either be stored in factom-walletd or
* a sign function must be provided as part of the options.
* @param options
*/
addChain(chain: Chain, ecAddress: string, options?: AddOptions): Promise<AddResponse>;
/**
* Add a collection of Chains to the Factom blockchain. Performs both commits and reveals.
*
* @param chains Iterable of Chains to add.
* @param ecAddress Entry Credit address that pays for the commit, either private (Es) or public (EC).
* If a public address is passed, the private key must either be stored in factom-walletd or
* a sign function must be provided as part of the options.
* @param options
*/
addChains(chains: Chain[], ecAddress: string, options?: AddOptions): Promise<AddResponse[]>;
/**
* Add an Entry to the Factom blockchain. Performs both commit and reveal.
*
* @param entry Entry to add.
* @param ecAddress Entry Credit address that pays for the commit, either private (Es) or public (EC).
* If a public address is passed, the private key must either be stored in factom-walletd or
* a sign function must be provided as part of the options.
* @param options
*/
addEntry(entry: Entry, ecAddress: string, options?: AddOptions): Promise<AddResponse>;
/**
* Add a collection of Entries to the Factom blockchain. Performs both commits and reveals.
*
* @param entries Iterable of Entries to add.
* @param ecAddress Entry Credit address that pays for the commit, either private (Es) or public (EC).
* If a public address is passed, the private key must either be stored in factom-walletd or
* a sign function must be provided as part of the options.
* @param options
*/
addEntries(
entries: Entry[],
ecAddress: string,
options?: AddOptions
): Promise<AddResponse[]>;
// Wallet
/**
* Retrieve the corresponding private address of any type of address from factom-walletd if necessary.
*
* Returns corresponding private address.
*
* @param address Any address (EC or FCT, public or private).
*/
getPrivateAddress(address: string): Promise<string>;
// Get
/**
* Get all the entries of a given chain.
*
* Returns array of entries ordered from the oldest to the newest.
*
* @param chainId Chain ID of the chain to retrieve all the entries from.
*/
getAllEntriesOfChain(chainId: string): Promise<Entry[]>;
/**
* Get the head of a given chain.
*
* @param chainId Chain ID.
*/
getChainHead(chainId: string): Promise<{
/**
* keymr of the head of the chain
*/
keyMR: string;
/**
* Indicates if there is an Entry Block for that chain currently in the process list.
* If this is the case that would indicate that the head of that chain will change at the next block.
*/
chainInProcessList: boolean;
}>;
/**
* Get entry by hash (returned Entry does not contain an EntryBlockContext and a timestamp).
*
* @param entryHash Hash of the entry to query.
*/
getEntry(entryHash: string): Promise<Entry>;
/**
* Get entry by hash with its EntryBlockContext and timestamp.
* Note that this method is more expensive than getEntry as it also has to retrieve the Entry Block data.
*
* @param entryHash Hash of the entry to query.
*/
getEntryWithBlockContext(entryHash: string): Promise<Entry>;
/**
* Get the first entry of a chain. This methods has to rewind the entire chain which can be an expensive operation.
*
* Returns entry with its blockContext and timestamp populated.
*
* @param chainId Chain ID to retrieve the first entry from.
*/
getFirstEntry(chainId: string): Promise<Entry>;
/**
* Get the balance of an Entry Credit or Factoid address.
*
* Returns balance of EC or FCT. In the case of FCT the balance is in factoshis (10^-8 factoids).
*
* @param address Any type of address, FCT or EC, public or private.
*/
getBalance(address: string): Promise<number>;
/**
* Check if a chain exists.
*
* @param chainId Chain ID to check.
*/
chainExists(chainId: string): Promise<boolean>;
/**
* Get the current entry credit rate. The rate is the number of factoshis (10^-8 Factoids) necessary to purchase 1 EC.
*
* Returns entry credit rate.
*/
getEntryCreditRate(): Promise<number>;
/**
* Get Factoid transaction by id.
*
* @param txId Transaction id.
*/
getTransaction(txId: string): Promise<Transaction>;
/**
* Rewind a chain entry by entry (newest to oldest) while a predicate is true.
*
* @param chainId Chain to rewind.
* @param predicate Predicate of the while loop. Iteration stop if either the predicate is false or the end of the chain has been reached.
* @param func Function to apply at each iteration.
* @example
* cli.rewindChainWhile('dab6c095c22ec6db1b0961fdb82d504a95f0a31467bb7df73cc793532b0e9ae3', (entry) => true, function(entry) {
* // Do stuff with the entry
* })
*/
rewindChainWhile(
chainId: string,
predicate: (entry: Entry) => boolean,
func: (entry: Entry) => void
): void;
// Send transactions
/**
* Send a Factoid transaction.
* This method will throw if the transaction fees are too low given the current EC rate.
* Note that by default this method also rejects a transaction over paying the minimum required fees by a factor 10 as it is most likely a user input error. This can be overriden with the force option.
*
* Returns transaction id
*
* @param transaction
* @param options
*/
sendTransaction(
transaction: Transaction,
options?: {
/**
* Time to wait in seconds for transaction acknowledgment before timing out. If negative value, doesn't wait for ack. Defaults to 60.
*/
timeout?: number;
/**
* Set to true to bypass the checks of the transaction fee overpay and the minimum EC output amount.
*/
force?: boolean;
}
): Promise<string>;
/**
* Create a single input single output (SISO) Factoid transaction.
*
* @param originAddress Private or public Factoid address origin of the funds. If a public address is provided (FA) the corresponding private address must be stored in factom-walletd.
* @param recipientAddress Public Factoid address receiving the funds.
* @param amount Amount to transfer in factoshis (10^-8 Factoids).
* @param fees Value to override fees of the transaction (if not specified the library computes the lowest acceptable fee).
*/
createFactoidTransaction(
originAddress: string,
recipientAddress: string,
amount: number,
fees?: number
): Promise<Transaction>;
/**
* Create a transaction to convert Factoids to Entry Credit.
*
* @param originAddress Private or public Factoid address origin of the funds. If a public address is provided (FA) the corresponding private address must be stored in factom-walletd.
* @param recipientAddress Public Entry Credit address to receive the ECs.
* @param ecAmount Amount of Entry Credit (EC) to purchase.
* @param fees Value to override fees of the transaction (if not specified the library computes the lowest acceptable fee).
*/
createEntryCreditPurchaseTransaction(
originAddress: string,
recipientAddress: string,
ecAmount: number,
fees?: number
): Promise<Transaction>;
// Ack
/**
* Wait until an acknowlegment is received from the network for a commit.
*
* Returns status of the commit. See https://docs.factom.com/api#ack
*
* @param txId Commit transaction ID.
* @param timeout Wait time in seconds.
*/
waitOnCommitAck(txId: string, timeout: number): Promise<string>;
/**
* Wait until an acknowlegment is received from the network for a reveal.
*
* Returns status of the reveal. See https://docs.factom.com/api#ack
*
* @param hash Hash of the revealed entry.
* @param chainId Chain ID of the revealed entry.
* @param timeout Wait time in seconds. Defaults to 60.
*/
waitOnRevealAck(hash: string, chainId: string, timeout?: number): Promise<string>;
/**
* Wait until an acknowlegment is received from the network for a Factoid transaction.
*
* Returns status of the transaction. See https://docs.factom.com/api#ack
*
* @param txId Transaction ID.
* @param timeout Wait time in seconds.
*/
waitOnFactoidTransactionAck(txId: string, timeout?: number): Promise<string>;
// Raw APIs
/**
* Make a direct call to factomd API. See https://docs.factom.com/api#factomd-api.
*
* Returns Factomd API response.
*
* @param method Factomd API method name.
* @param params The object that the factomd API is expecting.
* @param requestConfig Request configuration.
*/
factomdApi(
method: string,
params?: any,
requestConfig?: {
timeout?: number;
retry?: {
retries?: number;
factor?: number;
minTimeout?: number;
maxTimeout?: number;
};
}
): Promise<any>;
/**
* Make a call to factom-walletd API. See https://docs.factom.com/api#factom-walletd-api.
*
* Returns Object Walletd API response.
*
* @param method Walletd API method name.
* @param params The object that the walletd API is expecting.
* @param requestConfig Request configuration.
*/
walletdApi(
method: string,
params?: any,
requestConfig?: {
timeout?: number;
retry?: {
retries?: number;
factor?: number;
minTimeout?: number;
maxTimeout?: number;
};
}
): Promise<any>;
// Blocks
/**
* Return blockchain heights. For the explanation of the different heights see https://docs.factom.com/api#heights.
*/
getHeights(): Promise<{
directoryBlockHeight: number;
leaderHeight: number;
entryBlockHeight: number;
entryHeight: number;
}>;
/**
* Return latest directory block saved.
*/
getDirectoryBlockHead(): Promise<DirectoryBlock>;
/**
* Get a directory block by keyMR or height.
*
* @param arg Either KeyMR (string) or height (number) of the directory block.
*/
getDirectoryBlock(arg: string | number): Promise<DirectoryBlock>;
/**
* Get an admin block by keyMR or height.
*
* @param arg Either KeyMR (string) or height (number) of the admin block.
*/
getAdminBlock(arg: string | number): Promise<AdminBlock>;
/**
* Get an entry credit block by keyMR or height.
*
* @param arg Either KeyMR (string) or height (number) of the entry credit block.
*/
getEntryCreditBlock(arg: string | number): Promise<EntryCreditBlock>;
/**
* Get a Factoid block by keyMR or height.
*
* @param arg Either KeyMR (string) or height (number) of the factoid block.
*/
getFactoidBlock(arg: string | number): Promise<FactoidBlock>;
/**
* Get an entry block.
*
* @param keyMR KeyMR of the entry block.
*/
getEntryBlock(keyMR: string): Promise<EntryBlock>;
}
/**
* Factomd API client.
*/
export class FactomdCli {
/**
* @param conf Factomd connection options.
*/
constructor(conf: ConnectionOptions);
/**
* Make a call to factomd API. See https://docs.factom.com/api#factomd-api.
*
* Returns Object Factomd API response.
*
* @param method Factomd API method name.
* @param params The object that the factomd API is expecting.
* @param requestConfig Request configuration.
*/
call(
method: string,
params?: any,
requestConfig?: {
timeout?: number;
retry?: {
retries?: number;
factor?: number;
minTimeout?: number;
maxTimeout?: number;
};
}
): Promise<any>;
}
/**
* Walletd API client.
*/
export class WalletdCli {
/**
* @param opts Walletd connection options.
*/
constructor(opts: ConnectionOptions);
/**
* Make a call to factom-walletd API. See https://docs.factom.com/api#factom-walletd-api.
*
* Returns Object Walletd API response.
*
* @param method Walletd API method name.
* @param params The object that the walletd API is expecting.
* @param requestConfig Request configuration.
*/
call(
method: string,
params?: any,
requestConfig?: {
timeout?: number;
retry?: {
retries?: number;
factor?: number;
minTimeout?: number;
maxTimeout?: number;
};
}
): Promise<any>;
}
/**
* Listen for new Factom Events:
*
* <ul>
* <li>newDirectoryBlock - Triggers when blockchain adds a new directory block. Listener receives new directory block.</li>
* <li>newFactoidBlock - Triggers when blockchain adds a new factoid block. Listener receives new factoid block.</li>
* <li>newAdminBlock - Triggers when blockchain adds a new admin block. Listener receives new admin block.</li>
* <li>newEntryCreditBlock - Triggers when blockchain adds a new entry credit block. Listener receives new entry credit block.</li>
* <li>newChain - Triggers when blockchain adds a new chain. Listener receives first entry block of new chain.</li>
* <li>FA29eyMVJaZ2tbGqJ3M49gANaXMXCjgfKcJGe5mx8p4iQFCvFDAC - Triggers when factoid address sends or receives a transaction. Listener receives transaction.</li>
* <li>4060c0192a421ca121ffff935889ef55a64574a6ef0e69b2b4f8a0ab919b2ca4 - Triggers when entry chain adds new entry block. Listener receives entry block.</li>
* <li>newPendingTransaction:FA29eyMVJaZ2tbGqJ3M49gANaXMXCjgfKcJGe5mx8p4iQFCvFDAC - Triggers when factoid address receives a new pending transaction.</li>
* </ul>
*
* @example
* const { FactomCli, FactomEventEmitter } = require('factom');
* const cli = new FactomCli();
* // Poll the blockchain every 10s
* const emitter = new FactomEventEmitter(cli, { interval: 10000 });
* emitter.on('newDirectoryBlock', (directoryBlock) => ...);
* emitter.on('newFactoidBlock', (factoidBlock) => ...);
* emitter.on('newAdminBlock', (adminBlock) => ...);
* emitter.on('newEntryCreditBlock', (entryCreditBlock) => ...);
* emitter.on('newChain', (entryBlock) => ...);
* // Listen to any transaction involving a given Factoid address
* emitter.on('FA29eyMVJaZ2tbGqJ3M49gANaXMXCjgfKcJGe5mx8p4iQFCvFDAC', (transaction) => ...);
* // Listen to any new entries in a given chain
* emitter.on('4060c0192a421ca121ffff935889ef55a64574a6ef0e69b2b4f8a0ab919b2ca4', (entryBlock) => ...);
* // Listen to any pending transactions involving a given Factoid address
* emitter.on(FactomEventEmitter.getSubscriptionToken({
* eventType: 'newPendingTransaction', address: 'FA29eyMVJaZ2tbGqJ3M49gANaXMXCjgfKcJGe5mx8p4iQFCvFDAC'
* }), (pendingTransaction) => ...);
*/
export class FactomEventEmitter extends EventEmitter {
/**
* @param cli - FactomCli instance to be used by the FactomEventEmitter instance to fetch blockchain data.
* @param opts - Options to set on the FactomEventEmitter instance
*/
constructor(cli: FactomCli, opts?: { interval?: number });
/**
* Get active chain id subscriptions
*/
chainSubscriptions: Set<string>;
/**
* Get active factoid address subscriptions
*/
factoidAddressSubscriptions: Set<string>;
/**
* Get active factoid pending transactions subscriptions
*/
factoidAddressPendingTransactionSubscriptions: Map<string, Set<string>>;
/**
* Determine whether or not polling is currently active.
*/
isPolling: boolean;
/**
* Given an event configuration object returns a tokenized string
* @param {Object} event - The event configuration object
* @param {string} event.eventType - The type of event e.g. newPendingTransaction
* @param {string} event.topic - The topic e.g. A Factoid address
* @returns {string}
*/
static getSubscriptionToken(event: Object): string;
}
/**
* Block context of an Entry.
*/
interface EntryBlockContext {
/**
* Epoch timestamp (in seconds) of the entry.
*/
entryTimestamp: number;
/**
* Directory Block height.
*/
directoryBlockHeight: number;
/**
* Epoch timestamp (in seconds) of the Entry Block.
*/
entryBlockTimestamp: number;
/**
* Entry Block sequence number.
*/
entryBlockSequenceNumber: number;
/**
* Entry Block KeyMR.
*/
entryBlockKeyMR: string;
}
/**
* Class representing an Entry.
*
* @example
* const myEntry = Entry.builder()
* .chainId('9107a308f91fd7962fecb321fdadeb37e2ca7d456f1d99d24280136c0afd55f2')
* .extId('6d79206578742069642031') // If no encoding parameter is passed as 2nd argument, 'hex' is used as default
* .extId('Some external ID', 'utf8')
* .content('My new content', 'utf8')
* .build();
*/
export class Entry {
/**
* @param builder builder
*/
constructor(builder: EntryBuilder);
/**
* Chain ID.
*/
chainId: Buffer;
/**
* External IDs.
*/
extIds: Buffer[];
/**
* Content.
*/
content: Buffer;
/**
* Timestamp in milliseconds for the commit. This property is *not* populated when using the method getEntry.
*/
timestamp?: number;
/**
* Block context. This property is *not* populated when using the method getEntry.
*/
blockContext?: EntryBlockContext;
/**
* Entry content as hex encoded string.
*/
chainIdHex: string;
/**
* Entry content as hex encoded string.
*/
contentHex: string;
/**
* External ids as hex encoded strings.
*/
extIdsHex: string[];
/**
* Gets the entry size in bytes.
*/
size(): number;
/**
* Gets the entry payload size in bytes (excluding the header).
*/
payloadSize(): number;
/**
* Gets the entry raw data size in bytes (payload size excluding the 2 byte overhead per extID).
*/
rawDataSize(): number;
/**
* Gets the remaining number of free bytes that can be added to the entry for the same EC cost.
*/
remainingFreeBytes(): number;
/**
* Gets the maximum number of bytes that can be added to the entry before hitting the maximum (10kb).
*/
remainingMaxBytes(): number;
/**
* Gets the hash of the entry.
*/
hash(): Buffer;
/**
* Gets the hash of the entry as hex encoded string.
*/
hashHex(): string;
/**
* Result of marshaling the entry.
*/
marshalBinary(): Buffer;
/**
* Result of marshaling the entry as hex encoded string.
*/
marshalBinaryHex(): string;
/**
* Get Entry Credit cost of the entry.
*/
ecCost(): number;
/**
* Convert to a JavaScript Object representation of the entry. Can be used as argument of EntryBuilder.
*/
toObject(): object;
/**
* Entry builder static factory.
*
* Returns a new EntryBuilder.
*
* @param entry Optional entry to use to initialize the attributes of the builder.
*/
static builder(entry?: Entry): EntryBuilder;
}
/**
* Class to build an Entry
*/
export class EntryBuilder {
/**
* Class to build an entry.
*
* @param entry Optional entry to use to initialize the attributes of the builder.
*/
constructor(entry?: Entry);
/**
* Set content.
*
* @param content Content.
* @param enc Encoding of the content if it is a string. Defaults to hex.
*/
content(content: string | Buffer, enc?: string): EntryBuilder;
/**
* Set chain ID.
* @param chainId Chain ID
* @param enc Encoding of the chainId if it is a string. Defaults to hex.
*/
chainId(chainId: string | Buffer, enc?: string): EntryBuilder;
/**
* Set external IDs.
*
* @param extIds External IDs.
* @param enc Encoding of the external ids if they are strings.
*/
extIds(extIds: string[] | Buffer[], enc?: string): EntryBuilder;
/**
*
* @param extId External ID.
* @param enc Encoding of the external id if it is a string. Defaults to hex.
*/
extId(extId: string | Buffer, enc?: string): EntryBuilder;
/**
* Set the timestamp for the entry commit.
* If not set the library will use Date.now() as the commit timestamp.
*
* @param timestamp Timestamp in milliseconds.
*/
timestamp(timestamp: number): EntryBuilder;
/**
* Build the Entry.
*
* Returns the built entry.
*/