koilib 5.5.3 → 5.5.5

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.
@@ -0,0 +1,767 @@
1
+ import { INamespace } from "protobufjs/light";
2
+ import { Serializer } from "./Serializer";
3
+
4
+ /**
5
+ * Application Binary Interface (ABI)
6
+ *
7
+ * ABIs are composed of 3 elements: methods, events, and types.
8
+ * - The methods define the names of the entries of the smart contract,
9
+ * the corresponding endpoints and the name of the types used.
10
+ * - The events define possible events triggered by the smart contract
11
+ * and the name of the types used.
12
+ * - The types contain the description to serialize and deserialize
13
+ * data using proto buffers. It is used to encode/decode the methods
14
+ * and events. These types can be provided in binary format or json
15
+ * format (koilib_types)
16
+ *
17
+ * To generate the types is necessary to use the dependency
18
+ * protobufjs. The following example shows how to generate the
19
+ * protobuf descriptor from a .proto file.
20
+ *
21
+ * ```js
22
+ * const fs = require("fs");
23
+ * const pbjs = require("protobufjs/cli/pbjs");
24
+ *
25
+ * pbjs.main(
26
+ * ["--target", "json", "./token.proto"],
27
+ * (err, output) => {
28
+ * if (err) throw err;
29
+ * fs.writeFileSync("./token-proto.json", output);
30
+ * }
31
+ * );
32
+ * ```
33
+ *
34
+ * Then this descriptor can be loaded to define the ABI:
35
+ * ```js
36
+ * const tokenJson = require("./token-proto.json");
37
+ * const abiToken = {
38
+ * methods: {
39
+ * balanceOf: {
40
+ * entry_point: 0x15619248,
41
+ * argument: "balance_of_arguments",
42
+ * return: "balance_of_result",
43
+ * read_only: true,
44
+ * default_output: { value: "0" },
45
+ * },
46
+ * transfer: {
47
+ * entry_point: 0x62efa292,
48
+ * argument: "transfer_arguments",
49
+ * return: "transfer_result",
50
+ * },
51
+ * mint: {
52
+ * entry_point: 0xc2f82bdc,
53
+ * argument: "mint_argumnets",
54
+ * return: "mint_result",
55
+ * },
56
+ * },
57
+ * events: {
58
+ * 'koinos.contracts.token.mint_event': {
59
+ * argument: "mint"
60
+ * },
61
+ * },
62
+ * koilib_types: tokenJson,
63
+ * };
64
+ * ```
65
+ *
66
+ * Note that this example uses "default_output" for the method
67
+ * "balanceOf". This is used when the smart contract returns an
68
+ * empty response (for instance when there are no balance records
69
+ * for a specific address) and you require a default output in
70
+ * such cases.
71
+ *
72
+ * **Definition of events**
73
+ *
74
+ * There are 2 ways to define events in koinos:
75
+ * - Event names as protobuffer names
76
+ * - Custom event names
77
+ *
78
+ * 1. Event names as protobuffer names: The name of the event links
79
+ * with the protobuffer definition. In this case there is no need
80
+ * to define the event in the ABI.
81
+ *
82
+ * Example:
83
+ *
84
+ * Proto definition
85
+ * ```
86
+ * package koinos.contracts.token;
87
+ *
88
+ * message transfer_arguments {
89
+ * bytes from = 1 [(btype) = ADDRESS];
90
+ * bytes to = 2 [(btype) = ADDRESS];
91
+ * uint64 value = 3 [jstype = JS_STRING];
92
+ * }
93
+ *
94
+ * message transfer_event {
95
+ * bytes from = 1 [(btype) = ADDRESS];
96
+ * bytes to = 2 [(btype) = ADDRESS];
97
+ * uint64 value = 3 [jstype = JS_STRING];
98
+ * }
99
+ * ```
100
+ *
101
+ * Contract
102
+ * ```ts
103
+ * // token-contract.ts
104
+ * transfer(args: token.transfer_arguments): token.transfer_result {
105
+ * ...
106
+ * System.event("koinos.contracts.token.transfer_event", Protobuf.encode(event, token.transfer_event.encode), impacted);
107
+ * }
108
+ * ```
109
+ *
110
+ * 2. Custom event names: The previous definition has a limitation. It's
111
+ * necessary to define a proto message for each event and argument, and they can
112
+ * not be reused. In this second solution, the event names are defined
113
+ * in the ABI and they are linked with the corresponding protobuffer definitions.
114
+ * In this sense, they can be reused.
115
+ *
116
+ * Example
117
+ *
118
+ * Proto definition
119
+ * ```
120
+ * package koinos.contracts.token;
121
+ *
122
+ * // only 1 message
123
+ * message transfer {
124
+ * bytes from = 1 [(btype) = ADDRESS];
125
+ * bytes to = 2 [(btype) = ADDRESS];
126
+ * uint64 value = 3 [jstype = JS_STRING];
127
+ * }
128
+ * ```
129
+ *
130
+ * Contract
131
+ * ```ts
132
+ * // token-contract.ts
133
+ *
134
+ * // Transfer of tokens
135
+ * // @event transfer_event token.transfer
136
+ * transfer(args: token.transfer): void {
137
+ * ...
138
+ * System.event("transfer_event", Protobuf.encode(event, token.transfer.encode), impacted);
139
+ * }
140
+ * ```
141
+ *
142
+ * ABI
143
+ * ```ts
144
+ * const abiToken = {
145
+ * methods: {
146
+ * transfer: {
147
+ * entry_point: 0x62efa292,
148
+ * argument: "transfer",
149
+ * return: "",
150
+ * },
151
+ * },
152
+ * events: {
153
+ * 'transfer_event': {
154
+ * argument: "transfer"
155
+ * },
156
+ * },
157
+ * koilib_types: tokenJson,
158
+ * };
159
+ * ```
160
+ */
161
+ export interface Abi {
162
+ methods: {
163
+ /** Name of the method */
164
+ [x: string]: {
165
+ /** Entry point ID */
166
+ entry_point: number;
167
+ /** Protobuffer type for argument */
168
+ argument?: string;
169
+ /** Protobuffer type for returned value */
170
+ return?: string;
171
+ /** Boolean to differentiate write methods
172
+ * (using transactions) from read methods
173
+ * (query the contract)
174
+ */
175
+ read_only?: boolean;
176
+ /** Default value when the output is undefined */
177
+ default_output?: unknown;
178
+ /** Optional function to preformat the argument */
179
+ preformat_argument?: (arg: unknown) => Record<string, unknown>;
180
+ /** Optional function to preformat the returned value */
181
+ preformat_return?: (output: Record<string, unknown>) => unknown;
182
+ /** Description of the method */
183
+ description?: string;
184
+ };
185
+ };
186
+
187
+ /**
188
+ * Protobuffers descriptor in binary format encoded in base64url.
189
+ */
190
+ types?: string;
191
+
192
+ /**
193
+ * Protobuffers descriptor in JSON format.
194
+ * See https://www.npmjs.com/package/protobufjs#using-json-descriptors
195
+ */
196
+ koilib_types?: INamespace;
197
+
198
+ /**
199
+ * Definition of events
200
+ */
201
+ events?: {
202
+ [x: string]: {
203
+ /** Protobuffer type for argument */
204
+ argument?: string;
205
+ /** Description of the event */
206
+ description?: string;
207
+ };
208
+ };
209
+ }
210
+
211
+ /**
212
+ * Human readable format operation
213
+ *
214
+ * @example
215
+ * ```ts
216
+ * const opDecoded = {
217
+ * name: "transfer",
218
+ * args: {
219
+ * from: "1Krs7v1rtpgRyfwEZncuKMQQnY5JhqXVSx",
220
+ * to: "1BqtgWBcqm9cSZ97avLGZGJdgso7wx6pCA",
221
+ * value: 1000,
222
+ * },
223
+ * };
224
+ * ```
225
+ */
226
+ export interface DecodedOperationJson {
227
+ /** Operation name */
228
+ name: string;
229
+
230
+ /** Arguments decoded. See [[Abi]] */
231
+ args?: Record<string, unknown>;
232
+ }
233
+
234
+ export interface SendTransactionOptions {
235
+ /**
236
+ * Broadcast
237
+ *
238
+ * Boolean to define if the transaction should be broadcasted
239
+ * to the different nodes in the network. By default it is true.
240
+ *
241
+ * Set it to false if you want to interact with a contract for
242
+ * testing purposes and check the possible events triggered.
243
+ */
244
+ broadcast?: boolean;
245
+
246
+ /**
247
+ * Collection of Abis so that the receiver can parse the
248
+ * operations in the transaction
249
+ */
250
+ abis?: Record<string, Abi>;
251
+
252
+ /**
253
+ * Send abis
254
+ *
255
+ * Boolean to define if the abis should be shared with
256
+ * the signer so it will be able to decode the operations.
257
+ * By default it is true.
258
+ */
259
+ sendAbis?: boolean;
260
+
261
+ /**
262
+ * Function to be called before sending a transaction to the
263
+ * blockchain. It is useful to apply multisignatures to
264
+ * the transaction.
265
+ *
266
+ * @example
267
+ * ```ts
268
+ * const signer2 = Signer.fromSeed("signer2");
269
+ * const signer3 = Signer.fromSeed("signer3");
270
+ *
271
+ * const addMoreSignatures = async (tx, opts) => {
272
+ * await signer2.signTransaction(tx);
273
+ * await signer3.signTransaction(tx);
274
+ * };
275
+ *
276
+ * const { transaction } = await koin.transfer(
277
+ * {
278
+ * from: "16MT1VQFgsVxEfJrSGinrA5buiqBsN5ViJ",
279
+ * to: "1Gvqdo9if6v6tFomEuTuMWP1D7H7U9yksb",
280
+ * value: "1000000",
281
+ * },
282
+ * {
283
+ * payer: signer2.getAddress(),
284
+ * beforeSend: addMoreSignatures,
285
+ * }
286
+ * );
287
+ * ```
288
+ */
289
+ beforeSend?: (
290
+ tx: TransactionJson,
291
+ options?: SendTransactionOptions
292
+ ) => Promise<void>;
293
+ }
294
+
295
+ export interface TransactionOptions extends SendTransactionOptions {
296
+ /**
297
+ * Chain ID
298
+ *
299
+ * If this option is not set it will be taken
300
+ * by querying the blockchain
301
+ */
302
+ chainId?: string;
303
+
304
+ /**
305
+ * Resource Credits limit
306
+ *
307
+ * Max amount of mana to be spent. If this option
308
+ * is not set it will be taken by querying the
309
+ * actual mana of the payer
310
+ */
311
+ rcLimit?: string;
312
+
313
+ /**
314
+ * Transaction nonce
315
+ *
316
+ * It can be the nonce of the payee or the nonce of the
317
+ * payer. Use the payee's nonce when the payer's nonce changes
318
+ * continuosly without your control, for instance, when the
319
+ * payer is a dApp.
320
+ *
321
+ * The nonce is not required to be consecutive,
322
+ * only greater than the previous one.
323
+ *
324
+ * If this option is not set it will be taken
325
+ * from the blockchain depending on the payer/payee
326
+ * configuration.
327
+ */
328
+ nonce?: string;
329
+
330
+ /**
331
+ * Payer
332
+ *
333
+ * Address that will pay the resource credits (aka mana)
334
+ * consumed in the transaction.
335
+ *
336
+ * If this option is not set it will take the address
337
+ * of the signer as payer.
338
+ */
339
+ payer?: string;
340
+
341
+ /**
342
+ * Payee
343
+ *
344
+ * Address that increases the nonce. When this option is
345
+ * not set the blockchain will increase the payer's nonce.
346
+ */
347
+ payee?: string;
348
+ }
349
+
350
+ export interface ContractTransactionOptions extends TransactionOptions {
351
+ /**
352
+ * Only operation
353
+ *
354
+ * Boolean to define if the intention is to get only the operation.
355
+ * No transaction or connection with the provider established.
356
+ * By default it is false.
357
+ */
358
+ onlyOperation?: boolean;
359
+
360
+ /**
361
+ * Previous operations
362
+ *
363
+ * List of extra operations to be included before the actual one in the transaction
364
+ */
365
+ previousOperations?: OperationJson[];
366
+
367
+ /**
368
+ * Next operations
369
+ *
370
+ * List of extra operations to be included after the actual one in the transaction
371
+ */
372
+ nextOperations?: OperationJson[];
373
+
374
+ /**
375
+ * Sign transaction
376
+ *
377
+ * Boolean to define if the transaction should be signed.
378
+ * By default it is true.
379
+ */
380
+ signTransaction?: boolean;
381
+
382
+ /**
383
+ * Send transaction
384
+ *
385
+ * Boolean to define if the transaction should be signed
386
+ * and sent to the RPC node. By default it is
387
+ * true.
388
+ */
389
+ sendTransaction?: boolean;
390
+ }
391
+
392
+ export type CallContractOptions = ContractTransactionOptions;
393
+
394
+ export interface DeployOptions extends ContractTransactionOptions {
395
+ /**
396
+ * ABI
397
+ *
398
+ * ABI to be stored in the koinos-contract-meta-store.
399
+ * This option is optional.
400
+ */
401
+ abi?: string;
402
+
403
+ /**
404
+ * Authorizes call contract
405
+ *
406
+ * Set it true if the contract implements the "authorize"
407
+ * function and can authorize calling other contracts in
408
+ * its name.
409
+ */
410
+ authorizesCallContract?: boolean;
411
+
412
+ /**
413
+ * Authorizes transaction application
414
+ *
415
+ * Set it true if the contract implements the "authorize"
416
+ * function and can authorize paying the mana to apply
417
+ * transactions, or can authorize the use of its nonce
418
+ * to apply transactions.
419
+ */
420
+ authorizesTransactionApplication?: boolean;
421
+
422
+ /**
423
+ * Authorizes upload contract
424
+ *
425
+ * Set it true if the contract implements the "authorize"
426
+ * function and can authorize upgrades of the actual contract
427
+ */
428
+ authorizesUploadContract?: boolean;
429
+ }
430
+
431
+ export interface RecoverPublicKeyOptions {
432
+ /**
433
+ * Boolean to define if the public key should
434
+ * be compressed or not. It is true by default
435
+ */
436
+ compressed?: boolean;
437
+
438
+ /**
439
+ * Asynchronous function to transform the signature
440
+ * before calculating the public key associated.
441
+ * This transformation is useful in cases were the
442
+ * signature contains additional data. For instance,
443
+ * the signatures for blocks in the PoW consensus
444
+ * algorithm contain the nonce.
445
+ */
446
+ transformSignature?: (signatureData: string) => Promise<string>;
447
+ }
448
+
449
+ /**
450
+ * Function to wait for a transaction to be mined.
451
+ * This function comes as a response after sending a transaction.
452
+ * See [[Provider.sendTransaction]]
453
+ *
454
+ * @param type - Type must be "byBlock" (default) or "byTransactionId".
455
+ * _byBlock_ will query the blockchain to get blocks and search for the
456
+ * transaction there. _byTransactionId_ will query the "transaction store"
457
+ * microservice to search the transaction by its id. If non of them is
458
+ * specified the function will use "byBlock" (as "byTransactionId"
459
+ * requires the transaction store, which is an optional microservice).
460
+ *
461
+ * When _byBlock_ is used it returns the block number.
462
+ *
463
+ * When _byTransactionId_ is used it returns the block id.
464
+ *
465
+ * @param timeout - Timeout in milliseconds. By default it is 15000
466
+ */
467
+ export type WaitFunction = (
468
+ type?: "byBlock" | "byTransactionId",
469
+ timeout?: number
470
+ ) => Promise<{
471
+ blockId: string;
472
+ blockNumber?: number;
473
+ }>;
474
+
475
+ export interface GenesisDataEntryEncoded {
476
+ space: {
477
+ system?: boolean;
478
+ zone?: string;
479
+ id?: number;
480
+ };
481
+ key?: string;
482
+ value: string;
483
+ error?: string;
484
+ }
485
+
486
+ export interface GenesisDataEncoded {
487
+ entries?: GenesisDataEntryEncoded[];
488
+ }
489
+
490
+ export interface GenesisDataEntryDecoded {
491
+ space: {
492
+ system?: boolean;
493
+ zone?: string;
494
+ id?: number;
495
+ };
496
+ key?: string;
497
+ alias?: string;
498
+ value: string | Record<string, unknown>;
499
+ error?: string;
500
+ }
501
+
502
+ export interface GenesisDataDecoded {
503
+ entries?: GenesisDataEntryDecoded[];
504
+ }
505
+
506
+ export interface DictionaryGenesisData {
507
+ /** key name */
508
+ [x: string]: {
509
+ /** alternative name for the key name */
510
+ alias?: string;
511
+
512
+ /** boolean defining if it's an address */
513
+ isAddress?: boolean;
514
+
515
+ /** custom serializer */
516
+ serializer?: Serializer;
517
+
518
+ /** type name for serialization */
519
+ typeName?: string;
520
+
521
+ /** preformat bytes for base64url, base58 or hex string */
522
+ bytesConversion?: boolean;
523
+ };
524
+ }
525
+
526
+ export interface TypeField {
527
+ type: string;
528
+ btype?: string;
529
+ subtypes?: Record<string, TypeField>;
530
+ rule?: "repeated";
531
+ }
532
+
533
+ export interface ContractCallBundle {
534
+ contract_id: Uint8Array;
535
+ entry_point: number;
536
+ }
537
+
538
+ export interface ContractCallBundleJson {
539
+ contract_id: string; // base58
540
+
541
+ entry_point: number;
542
+ }
543
+
544
+ export interface ThunkIdNested {
545
+ thunk_id: number;
546
+ }
547
+
548
+ export interface ContractCallBundleNested {
549
+ system_call_bundle: ContractCallBundle;
550
+ }
551
+
552
+ export type SystemCallTarget = ThunkIdNested | ContractCallBundleNested;
553
+
554
+ export interface SystemCallTargetJson {
555
+ thunk_id?: number;
556
+ system_call_bundle?: ContractCallBundleJson;
557
+ }
558
+
559
+ export interface UploadContractOperation {
560
+ contract_id?: Uint8Array;
561
+
562
+ bytecode?: Uint8Array;
563
+
564
+ abi?: string;
565
+
566
+ authorizes_call_contract?: boolean;
567
+
568
+ authorizes_transaction_application?: boolean;
569
+
570
+ authorizes_upload_contract?: boolean;
571
+ }
572
+
573
+ export interface UploadContractOperationJson {
574
+ contract_id?: string; // base58
575
+
576
+ bytecode?: string; // base64
577
+
578
+ abi?: string;
579
+
580
+ authorizes_call_contract?: boolean;
581
+
582
+ authorizes_transaction_application?: boolean;
583
+
584
+ authorizes_upload_contract?: boolean;
585
+ }
586
+
587
+ export interface CallContractOperation {
588
+ contract_id: Uint8Array;
589
+
590
+ entry_point: number;
591
+
592
+ args: Uint8Array;
593
+ }
594
+
595
+ export interface CallContractOperationJson {
596
+ contract_id: string; // base58
597
+
598
+ entry_point: number;
599
+
600
+ args: string; // base64
601
+ }
602
+
603
+ export interface SetSystemCallOperation {
604
+ call_id: number;
605
+
606
+ target: SystemCallTarget;
607
+ }
608
+
609
+ export interface SetSystemCallOperationJson {
610
+ call_id: number;
611
+
612
+ target: SystemCallTargetJson;
613
+ }
614
+
615
+ export interface SetSystemContractOperation {
616
+ contract_id: Uint8Array;
617
+
618
+ system_contract: boolean;
619
+ }
620
+
621
+ export interface SetSystemContractOperationJson {
622
+ contract_id: string; // base58
623
+
624
+ system_contract: boolean;
625
+ }
626
+
627
+ export interface UploadContractOperationNested {
628
+ upload_contract: UploadContractOperation;
629
+ }
630
+
631
+ export interface CallContractOperationNested {
632
+ call_contract: CallContractOperation;
633
+ }
634
+
635
+ export interface SetSystemCallOperationNested {
636
+ set_system_call: SetSystemCallOperation;
637
+ }
638
+
639
+ export interface SetSystemContractOperationNested {
640
+ set_system_contract: SetSystemContractOperation;
641
+ }
642
+
643
+ export type Operation =
644
+ | UploadContractOperationNested
645
+ | CallContractOperationNested
646
+ | SetSystemCallOperationNested
647
+ | SetSystemContractOperationNested;
648
+
649
+ export type OperationJson = {
650
+ upload_contract?: UploadContractOperationJson;
651
+ call_contract?: CallContractOperationJson;
652
+ set_system_call?: SetSystemCallOperationJson;
653
+ set_system_contract?: SetSystemContractOperationJson;
654
+ };
655
+
656
+ export interface TransactionHeaderJson {
657
+ /**
658
+ * ID of the chain
659
+ */
660
+ chain_id?: string;
661
+
662
+ /**
663
+ * Resource credits limit
664
+ */
665
+ rc_limit?: string;
666
+
667
+ /**
668
+ * Account nonce
669
+ */
670
+ nonce?: string;
671
+
672
+ /**
673
+ * Merkle root of the serialized operations's SHA2-256 hashes
674
+ */
675
+ operation_merkle_root?: string;
676
+
677
+ /**
678
+ * Transaction's payer
679
+ */
680
+ payer?: string;
681
+
682
+ /**
683
+ * Transaction's payee
684
+ */
685
+ payee?: string;
686
+
687
+ [x: string]: unknown;
688
+ }
689
+
690
+ /**
691
+ * Koinos Transaction
692
+ */
693
+ export interface TransactionJson {
694
+ /**
695
+ * Transaction ID. It must be the sha2-256 of the
696
+ * serialized header of the transaction
697
+ */
698
+ id?: string;
699
+
700
+ /**
701
+ * Header of the transaction
702
+ */
703
+ header?: TransactionHeaderJson;
704
+
705
+ /**
706
+ * Array of operations
707
+ */
708
+ operations?: OperationJson[];
709
+
710
+ /**
711
+ * Signatures in compact format
712
+ */
713
+ signatures?: string[];
714
+ }
715
+
716
+ export interface TransactionJsonWait extends TransactionJson {
717
+ wait: WaitFunction;
718
+ }
719
+
720
+ export interface BlockHeaderJson {
721
+ previous?: string;
722
+ height?: string;
723
+ timestamp?: string;
724
+ previous_state_merkle_root?: string;
725
+ transaction_merkle_root?: string;
726
+ signer?: string;
727
+ [x: string]: unknown;
728
+ }
729
+
730
+ export interface BlockJson {
731
+ id?: string;
732
+ header?: BlockHeaderJson;
733
+ transactions?: TransactionJson[];
734
+ signature?: string;
735
+ [x: string]: unknown;
736
+ }
737
+
738
+ export interface ValueType {
739
+ uint64_value?: string;
740
+ [x: string]: unknown;
741
+ }
742
+
743
+ export interface EventData {
744
+ sequence: number;
745
+ source: string;
746
+ name: string;
747
+ data: string;
748
+ impacted: string[];
749
+ }
750
+
751
+ export interface DecodedEventData extends EventData {
752
+ args: Record<string, unknown>;
753
+ }
754
+
755
+ export interface TransactionReceipt {
756
+ id: string;
757
+ payer: string;
758
+ max_payer_rc: string;
759
+ rc_limit: string;
760
+ rc_used: string;
761
+ disk_storage_used: string;
762
+ network_bandwidth_used: string;
763
+ compute_bandwidth_used: string;
764
+ reverted: boolean;
765
+ events: EventData[];
766
+ logs: string[];
767
+ }