koilib 7.1.1 → 8.0.0
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 +31 -0
- package/dist/koinos.js +19 -89
- package/dist/koinos.min.js +1 -1
- package/lib/Provider.d.ts +5 -8
- package/lib/Provider.js +1 -1
- package/lib/Provider.js.map +1 -1
- package/lib/Signer.d.ts +3 -76
- package/lib/Signer.js +2 -88
- package/lib/Signer.js.map +1 -1
- package/lib/Transaction.d.ts +7 -0
- package/lib/Transaction.js +16 -0
- package/lib/Transaction.js.map +1 -1
- package/lib/browser/Provider.d.ts +5 -8
- package/lib/browser/Provider.js +1 -1
- package/lib/browser/Provider.js.map +1 -1
- package/lib/browser/Signer.d.ts +3 -76
- package/lib/browser/Signer.js +2 -88
- package/lib/browser/Signer.js.map +1 -1
- package/lib/browser/Transaction.d.ts +7 -0
- package/lib/browser/Transaction.js +16 -0
- package/lib/browser/Transaction.js.map +1 -1
- package/lib/browser/interface.d.ts +13 -29
- package/lib/interface.d.ts +13 -29
- package/package.json +1 -1
- package/src/Provider.ts +6 -10
- package/src/Signer.ts +2 -127
- package/src/Transaction.ts +18 -0
- package/src/interface.ts +14 -33
package/src/Signer.ts
CHANGED
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
import { sha256 } from "@noble/hashes/sha256";
|
|
3
3
|
import * as secp from "@noble/secp256k1";
|
|
4
4
|
import { Provider } from "./Provider";
|
|
5
|
-
import { Transaction } from "./Transaction";
|
|
6
5
|
import {
|
|
7
6
|
TransactionJson,
|
|
8
7
|
TransactionJsonWait,
|
|
@@ -12,7 +11,6 @@ import {
|
|
|
12
11
|
TypeField,
|
|
13
12
|
TransactionReceipt,
|
|
14
13
|
SendTransactionOptions,
|
|
15
|
-
ResourceCreditsOptions,
|
|
16
14
|
} from "./interface";
|
|
17
15
|
import {
|
|
18
16
|
bitcoinAddress,
|
|
@@ -153,33 +151,6 @@ export class Signer implements SignerInterface {
|
|
|
153
151
|
*/
|
|
154
152
|
sendOptions?: SendTransactionOptions;
|
|
155
153
|
|
|
156
|
-
/**
|
|
157
|
-
* Options related to the estimation of the rcLimit.
|
|
158
|
-
* By default the estimation is enabled and increased
|
|
159
|
-
* by 10%.
|
|
160
|
-
*
|
|
161
|
-
* @example
|
|
162
|
-
* This code estimates the mana by multiplying the rc_used
|
|
163
|
-
* by 2.
|
|
164
|
-
* ```ts
|
|
165
|
-
* const signer = new Signer({
|
|
166
|
-
* privateKey,
|
|
167
|
-
* provider,
|
|
168
|
-
* rcOptions: {
|
|
169
|
-
* estimateRc: true,
|
|
170
|
-
* adjustRcLimit: (receipt) => 2 * Number(receipt.rc_used),
|
|
171
|
-
* },
|
|
172
|
-
* });
|
|
173
|
-
*
|
|
174
|
-
* // you can also update rcOptions
|
|
175
|
-
* signer.rcOptions = {
|
|
176
|
-
* estimateRc: true,
|
|
177
|
-
* adjustRcLimit: (receipt) => 2 * Number(receipt.rc_used),
|
|
178
|
-
* }
|
|
179
|
-
* ```
|
|
180
|
-
*/
|
|
181
|
-
rcOptions: ResourceCreditsOptions;
|
|
182
|
-
|
|
183
154
|
/**
|
|
184
155
|
* The constructor receives de private key as hexstring, bigint or Uint8Array.
|
|
185
156
|
* See also the functions [[Signer.fromWif]] and [[Signer.fromSeed]]
|
|
@@ -189,7 +160,6 @@ export class Signer implements SignerInterface {
|
|
|
189
160
|
* @param compressed - compressed format is true by default
|
|
190
161
|
* @param provider - provider to connect with the blockchain
|
|
191
162
|
* @param sendOptions - Send options
|
|
192
|
-
* @param rcOptions - options for mana estimation
|
|
193
163
|
* @example
|
|
194
164
|
* ```ts
|
|
195
165
|
* const privateKey = "ec8601a24f81decd57f4b611b5ac6eb801cb3780bb02c0f9cdfe9d09daaddf9c";
|
|
@@ -197,46 +167,12 @@ export class Signer implements SignerInterface {
|
|
|
197
167
|
* console.log(signer.getAddress());
|
|
198
168
|
* // 1MbL6mG8ASAvSYdoMnGUfG3ZXkmQ2dpL5b
|
|
199
169
|
* ```
|
|
200
|
-
*
|
|
201
|
-
* By default the mana is estimated as 110% the mana used. This
|
|
202
|
-
* estimation is computed using the "broadcast:false" option.
|
|
203
|
-
* For instance, if the transaction consumes 1 mana, the rc_limit
|
|
204
|
-
* will be set to 1.1 mana.
|
|
205
|
-
*
|
|
206
|
-
* You can also adjust the rc limit.
|
|
207
|
-
* @example
|
|
208
|
-
* ```ts
|
|
209
|
-
* const privateKey = "ec8601a24f81decd57f4b611b5ac6eb801cb3780bb02c0f9cdfe9d09daaddf9c";
|
|
210
|
-
* cons signer = new Signer({
|
|
211
|
-
* privateKey,
|
|
212
|
-
* provider,
|
|
213
|
-
* rcOptions: {
|
|
214
|
-
* estimateRc: true,
|
|
215
|
-
* // use 2 times rc_used as rc_limit
|
|
216
|
-
* adjustRcLimit: (r) => 2 * Number(r.rc_used),
|
|
217
|
-
* },
|
|
218
|
-
* });
|
|
219
|
-
* ```
|
|
220
|
-
*
|
|
221
|
-
* The rpc node must be highly trusted because the transaction
|
|
222
|
-
* is signed during the estimation of the mana. You can also
|
|
223
|
-
* disable the mana estimation:
|
|
224
|
-
* @example
|
|
225
|
-
* ```ts
|
|
226
|
-
* const privateKey = "ec8601a24f81decd57f4b611b5ac6eb801cb3780bb02c0f9cdfe9d09daaddf9c";
|
|
227
|
-
* cons signer = new Signer({
|
|
228
|
-
* privateKey,
|
|
229
|
-
* provider,
|
|
230
|
-
* rcOptions: { estimateRc: false },
|
|
231
|
-
* });
|
|
232
|
-
* ```
|
|
233
170
|
*/
|
|
234
171
|
constructor(c: {
|
|
235
172
|
privateKey: string | number | bigint | Uint8Array;
|
|
236
173
|
compressed?: boolean;
|
|
237
174
|
provider?: Provider;
|
|
238
175
|
sendOptions?: SendTransactionOptions;
|
|
239
|
-
rcOptions?: ResourceCreditsOptions;
|
|
240
176
|
}) {
|
|
241
177
|
this.compressed = typeof c.compressed === "undefined" ? true : c.compressed;
|
|
242
178
|
this.privateKey = c.privateKey;
|
|
@@ -252,16 +188,6 @@ export class Signer implements SignerInterface {
|
|
|
252
188
|
broadcast: true,
|
|
253
189
|
...c.sendOptions,
|
|
254
190
|
};
|
|
255
|
-
this.rcOptions = c.rcOptions ?? {
|
|
256
|
-
estimateRc: true,
|
|
257
|
-
adjustRcLimit: (receipt) =>
|
|
258
|
-
Promise.resolve(
|
|
259
|
-
Math.min(
|
|
260
|
-
Number(receipt.max_payer_rc),
|
|
261
|
-
Math.floor(1.1 * Number(receipt.rc_used))
|
|
262
|
-
)
|
|
263
|
-
),
|
|
264
|
-
};
|
|
265
191
|
}
|
|
266
192
|
|
|
267
193
|
/**
|
|
@@ -392,18 +318,6 @@ export class Signer implements SignerInterface {
|
|
|
392
318
|
): Promise<TransactionJson> {
|
|
393
319
|
if (!tx.id) throw new Error("Missing transaction id");
|
|
394
320
|
|
|
395
|
-
// estimation of rcLimit
|
|
396
|
-
if (
|
|
397
|
-
this.rcOptions.estimateRc &&
|
|
398
|
-
(!tx.signatures || tx.signatures.length === 0)
|
|
399
|
-
) {
|
|
400
|
-
const receipt = await this.estimateReceipt(tx);
|
|
401
|
-
tx.header!.rc_limit = this.rcOptions.adjustRcLimit
|
|
402
|
-
? await this.rcOptions.adjustRcLimit(receipt)
|
|
403
|
-
: receipt.rc_used;
|
|
404
|
-
tx.id = Transaction.computeTransactionId(tx.header!);
|
|
405
|
-
}
|
|
406
|
-
|
|
407
321
|
// multihash 0x1220. 12: code sha2-256. 20: length (32 bytes)
|
|
408
322
|
// tx id is a stringified multihash, need to extract the hash digest only
|
|
409
323
|
const hash = toUint8Array(tx.id.slice(6));
|
|
@@ -466,45 +380,6 @@ export class Signer implements SignerInterface {
|
|
|
466
380
|
return this.provider.sendTransaction(transaction, opts.broadcast);
|
|
467
381
|
}
|
|
468
382
|
|
|
469
|
-
/**
|
|
470
|
-
* Estimate the receipt associated to the transaction if
|
|
471
|
-
* it sent to the blockchain. It is useful to estimate the
|
|
472
|
-
* consumption of mana.
|
|
473
|
-
* The transaction is signed during this process and sent
|
|
474
|
-
* to the rpc node with the "broadcast:false" option to
|
|
475
|
-
* just compute the transaction without broadcasting it to
|
|
476
|
-
* the network.
|
|
477
|
-
* After that, the initial signatures are restored (if any)
|
|
478
|
-
* and the ones used for the estimation will be removed.
|
|
479
|
-
*/
|
|
480
|
-
async estimateReceipt(
|
|
481
|
-
tx: TransactionJson | TransactionJsonWait
|
|
482
|
-
): Promise<TransactionReceipt> {
|
|
483
|
-
if (!tx.id) throw new Error("Missing transaction id");
|
|
484
|
-
if (!tx.signatures) tx.signatures = [];
|
|
485
|
-
const signaturesCopy = [...tx.signatures];
|
|
486
|
-
|
|
487
|
-
// sign if there are no signatures
|
|
488
|
-
if (tx.signatures.length === 0) {
|
|
489
|
-
const hash = toUint8Array(tx.id.slice(6));
|
|
490
|
-
const signature = await this.signHash(hash);
|
|
491
|
-
tx.signatures.push(encodeBase64url(signature));
|
|
492
|
-
}
|
|
493
|
-
|
|
494
|
-
try {
|
|
495
|
-
const { receipt } = await this.sendTransaction(tx, {
|
|
496
|
-
broadcast: false,
|
|
497
|
-
});
|
|
498
|
-
// restore signatures
|
|
499
|
-
tx.signatures = signaturesCopy;
|
|
500
|
-
return receipt;
|
|
501
|
-
} catch (error) {
|
|
502
|
-
// restore signatures
|
|
503
|
-
tx.signatures = signaturesCopy;
|
|
504
|
-
throw error;
|
|
505
|
-
}
|
|
506
|
-
}
|
|
507
|
-
|
|
508
383
|
/**
|
|
509
384
|
* Function to recover the public key from hash and signature
|
|
510
385
|
* @param hash - hash sha256
|
|
@@ -596,7 +471,7 @@ export class Signer implements SignerInterface {
|
|
|
596
471
|
* });
|
|
597
472
|
* ```
|
|
598
473
|
*/
|
|
599
|
-
async recoverPublicKeys(
|
|
474
|
+
static async recoverPublicKeys(
|
|
600
475
|
txOrBlock: TransactionJson | BlockJson,
|
|
601
476
|
opts?: RecoverPublicKeyOptions
|
|
602
477
|
): Promise<string[]> {
|
|
@@ -696,7 +571,7 @@ export class Signer implements SignerInterface {
|
|
|
696
571
|
* });
|
|
697
572
|
* ```
|
|
698
573
|
*/
|
|
699
|
-
async recoverAddresses(
|
|
574
|
+
static async recoverAddresses(
|
|
700
575
|
txOrBlock: TransactionJson | BlockJson,
|
|
701
576
|
opts?: RecoverPublicKeyOptions
|
|
702
577
|
): Promise<string[]> {
|
package/src/Transaction.ts
CHANGED
|
@@ -110,6 +110,7 @@ export class Transaction {
|
|
|
110
110
|
constructor(c?: {
|
|
111
111
|
signer?: SignerInterface;
|
|
112
112
|
provider?: Provider;
|
|
113
|
+
transaction?: TransactionJson;
|
|
113
114
|
options?: TransactionOptions;
|
|
114
115
|
}) {
|
|
115
116
|
this.signer = c?.signer;
|
|
@@ -128,6 +129,7 @@ export class Transaction {
|
|
|
128
129
|
...(c?.options?.payee && { payee: c.options.payee }),
|
|
129
130
|
},
|
|
130
131
|
operations: [],
|
|
132
|
+
...c?.transaction,
|
|
131
133
|
};
|
|
132
134
|
}
|
|
133
135
|
|
|
@@ -347,6 +349,22 @@ export class Transaction {
|
|
|
347
349
|
return this.transaction;
|
|
348
350
|
}
|
|
349
351
|
|
|
352
|
+
/**
|
|
353
|
+
* Update the rc limit with a new value and update the
|
|
354
|
+
* transaction ID accordingly. The signatures will be removed
|
|
355
|
+
* if the transaction ID changed
|
|
356
|
+
*/
|
|
357
|
+
adjustRcLimit(newRcLimit: string | number): void {
|
|
358
|
+
if (!this.transaction.header)
|
|
359
|
+
throw new Error("transaction header not defined");
|
|
360
|
+
this.transaction.header.rc_limit = newRcLimit;
|
|
361
|
+
const newTxId = Transaction.computeTransactionId(this.transaction.header);
|
|
362
|
+
if (this.transaction.id !== newTxId) {
|
|
363
|
+
this.transaction.signatures = [];
|
|
364
|
+
}
|
|
365
|
+
this.transaction.id = newTxId;
|
|
366
|
+
}
|
|
367
|
+
|
|
350
368
|
/**
|
|
351
369
|
* Function to sign the transaction
|
|
352
370
|
*/
|
package/src/interface.ts
CHANGED
|
@@ -231,39 +231,6 @@ export interface DecodedOperationJson {
|
|
|
231
231
|
args?: Record<string, unknown>;
|
|
232
232
|
}
|
|
233
233
|
|
|
234
|
-
export interface ResourceCreditsOptions {
|
|
235
|
-
/**
|
|
236
|
-
* Boolean to define if the mana should be estimated
|
|
237
|
-
* and the rcLimit be updated before signing the transaction.
|
|
238
|
-
* It is true by default
|
|
239
|
-
*/
|
|
240
|
-
estimateRc?: boolean;
|
|
241
|
-
|
|
242
|
-
/**
|
|
243
|
-
* Function to adjust the rc limit after the estimation.
|
|
244
|
-
* It is useful to give an extra margin to the rc limit.
|
|
245
|
-
* By default the Signer will use a function that increases
|
|
246
|
-
* the rcLimit by 10% with respect to the estimation.
|
|
247
|
-
*
|
|
248
|
-
* @example
|
|
249
|
-
* ```ts
|
|
250
|
-
* const signer = Signer.fromWif("Kab...");
|
|
251
|
-
* signer.rcOptions = {
|
|
252
|
-
* estimateRc: true,
|
|
253
|
-
* adjustRcLimit: async (receipt) => {
|
|
254
|
-
* return Math.min(
|
|
255
|
-
* Number(receipt.max_payer_rc),
|
|
256
|
-
* Math.floor(1.1 * Number(receipt.rc_used))
|
|
257
|
-
* );
|
|
258
|
-
* },
|
|
259
|
-
* }
|
|
260
|
-
* ```
|
|
261
|
-
*/
|
|
262
|
-
adjustRcLimit?: (
|
|
263
|
-
receipt: TransactionReceipt
|
|
264
|
-
) => Promise<string | number> | string | number;
|
|
265
|
-
}
|
|
266
|
-
|
|
267
234
|
export interface SendTransactionOptions {
|
|
268
235
|
/**
|
|
269
236
|
* Broadcast
|
|
@@ -821,3 +788,17 @@ export interface TransactionReceipt {
|
|
|
821
788
|
logs: string[];
|
|
822
789
|
rpc_error?: unknown;
|
|
823
790
|
}
|
|
791
|
+
|
|
792
|
+
export interface BlockReceipt {
|
|
793
|
+
id: string;
|
|
794
|
+
height: string;
|
|
795
|
+
disk_storage_used: string;
|
|
796
|
+
network_bandwidth_used: string;
|
|
797
|
+
compute_bandwidth_used: string;
|
|
798
|
+
disk_storage_charged: string;
|
|
799
|
+
network_bandwidth_charged: string;
|
|
800
|
+
compute_bandwidth_charged: string;
|
|
801
|
+
events: EventData[];
|
|
802
|
+
transaction_receipts: TransactionReceipt[];
|
|
803
|
+
state_delta_entries: StateDeltaEntry[];
|
|
804
|
+
}
|