@astrox/agent 0.0.24 → 0.0.30
Sign up to get free protection for your applications and to get access to all the features.
- package/lib/cjs/actor.d.ts +126 -0
- package/lib/cjs/actor.js +207 -0
- package/lib/cjs/actor.js.map +1 -0
- package/lib/cjs/agent/api.d.ts +140 -0
- package/lib/cjs/agent/api.js +16 -0
- package/lib/cjs/agent/api.js.map +1 -0
- package/lib/cjs/agent/http/index.d.ts +49 -0
- package/lib/cjs/agent/http/index.js +355 -0
- package/lib/cjs/agent/http/index.js.map +1 -0
- package/lib/cjs/agent/http/transforms.d.ts +20 -0
- package/lib/cjs/agent/http/transforms.js +70 -0
- package/lib/cjs/agent/http/transforms.js.map +1 -0
- package/lib/cjs/agent/http/types.d.ts +77 -0
- package/lib/cjs/agent/http/types.js +24 -0
- package/lib/cjs/agent/http/types.js.map +1 -0
- package/lib/cjs/agent/index.d.ts +7 -0
- package/lib/cjs/agent/index.js +42 -0
- package/lib/cjs/agent/index.js.map +1 -0
- package/lib/cjs/agent/proxy.d.ts +85 -0
- package/lib/cjs/agent/proxy.js +159 -0
- package/lib/cjs/agent/proxy.js.map +1 -0
- package/lib/cjs/auth.d.ts +87 -0
- package/lib/cjs/auth.js +70 -0
- package/lib/cjs/auth.js.map +1 -0
- package/lib/cjs/canisters/asset.d.ts +10 -0
- package/lib/cjs/canisters/asset.js +18 -0
- package/lib/cjs/canisters/asset.js.map +1 -0
- package/lib/cjs/canisters/asset_idl.d.ts +7 -0
- package/lib/cjs/canisters/asset_idl.js +14 -0
- package/lib/cjs/canisters/asset_idl.js.map +1 -0
- package/lib/cjs/canisters/management.d.ts +33 -0
- package/lib/cjs/canisters/management.js +30 -0
- package/lib/cjs/canisters/management.js.map +1 -0
- package/lib/cjs/canisters/management_idl.d.ts +7 -0
- package/lib/cjs/canisters/management_idl.js +29 -0
- package/lib/cjs/canisters/management_idl.js.map +1 -0
- package/lib/cjs/cbor.d.ts +9 -0
- package/lib/cjs/cbor.js +138 -0
- package/lib/cjs/cbor.js.map +1 -0
- package/lib/cjs/certificate.d.ts +43 -0
- package/lib/cjs/certificate.js +227 -0
- package/lib/cjs/certificate.js.map +1 -0
- package/lib/cjs/errors.d.ts +8 -0
- package/lib/cjs/errors.js +13 -0
- package/lib/cjs/errors.js.map +1 -0
- package/lib/cjs/index.d.ts +52 -0
- package/lib/cjs/index.js +38 -0
- package/lib/cjs/index.js.map +1 -0
- package/lib/cjs/polling/index.d.ts +16 -0
- package/lib/cjs/polling/index.js +81 -0
- package/lib/cjs/polling/index.js.map +1 -0
- package/lib/cjs/polling/strategy.d.ts +47 -0
- package/lib/cjs/polling/strategy.js +106 -0
- package/lib/cjs/polling/strategy.js.map +1 -0
- package/lib/cjs/request_id.d.ts +15 -0
- package/lib/cjs/request_id.js +89 -0
- package/lib/cjs/request_id.js.map +1 -0
- package/lib/cjs/utils/bls.d.ts +9 -0
- package/lib/cjs/utils/bls.js +65 -0
- package/lib/cjs/utils/bls.js.map +1 -0
- package/lib/cjs/utils/buffer.d.ts +16 -0
- package/lib/cjs/utils/buffer.js +59 -0
- package/lib/cjs/utils/buffer.js.map +1 -0
- package/lib/cjs/vendor/bls/bls.d.ts +28 -0
- package/lib/cjs/vendor/bls/bls.js +894 -0
- package/lib/cjs/vendor/bls/bls.js.map +1 -0
- package/lib/esm/actor.d.ts +126 -0
- package/lib/esm/actor.js +200 -0
- package/lib/esm/actor.js.map +1 -0
- package/lib/esm/agent/api.d.ts +140 -0
- package/lib/esm/agent/api.js +13 -0
- package/lib/esm/agent/api.js.map +1 -0
- package/lib/esm/agent/http/index.d.ts +49 -0
- package/lib/esm/agent/http/index.js +328 -0
- package/lib/esm/agent/http/index.js.map +1 -0
- package/lib/esm/agent/http/transforms.d.ts +20 -0
- package/lib/esm/agent/http/transforms.js +45 -0
- package/lib/esm/agent/http/transforms.js.map +1 -0
- package/lib/esm/agent/http/types.d.ts +77 -0
- package/lib/esm/agent/http/types.js +20 -0
- package/lib/esm/agent/http/types.js.map +1 -0
- package/lib/esm/agent/index.d.ts +7 -0
- package/lib/esm/agent/index.js +27 -0
- package/lib/esm/agent/index.js.map +1 -0
- package/lib/esm/agent/proxy.d.ts +85 -0
- package/lib/esm/agent/proxy.js +154 -0
- package/lib/esm/agent/proxy.js.map +1 -0
- package/lib/esm/auth.d.ts +87 -0
- package/lib/esm/auth.js +64 -0
- package/lib/esm/auth.js.map +1 -0
- package/lib/esm/canisters/asset.d.ts +10 -0
- package/lib/esm/canisters/asset.js +11 -0
- package/lib/esm/canisters/asset.js.map +1 -0
- package/lib/esm/canisters/asset_idl.d.ts +7 -0
- package/lib/esm/canisters/asset_idl.js +12 -0
- package/lib/esm/canisters/asset_idl.js.map +1 -0
- package/lib/esm/canisters/management.d.ts +37 -0
- package/lib/esm/canisters/management.js +23 -0
- package/lib/esm/canisters/management.js.map +1 -0
- package/lib/esm/canisters/management_idl.d.ts +7 -0
- package/lib/esm/canisters/management_idl.js +27 -0
- package/lib/esm/canisters/management_idl.js.map +1 -0
- package/lib/esm/cbor.d.ts +9 -0
- package/lib/esm/cbor.js +111 -0
- package/lib/esm/cbor.js.map +1 -0
- package/lib/esm/certificate.d.ts +43 -0
- package/lib/esm/certificate.js +200 -0
- package/lib/esm/certificate.js.map +1 -0
- package/lib/esm/errors.d.ts +8 -0
- package/lib/esm/errors.js +9 -0
- package/lib/esm/errors.js.map +1 -0
- package/lib/esm/index.d.ts +52 -0
- package/lib/esm/index.js +14 -0
- package/lib/esm/index.js.map +1 -0
- package/lib/esm/polling/index.d.ts +16 -0
- package/lib/esm/polling/index.js +57 -0
- package/lib/esm/polling/index.js.map +1 -0
- package/lib/esm/polling/strategy.d.ts +47 -0
- package/lib/esm/polling/strategy.js +95 -0
- package/lib/esm/polling/strategy.js.map +1 -0
- package/lib/esm/request_id.d.ts +15 -0
- package/lib/esm/request_id.js +81 -0
- package/lib/esm/request_id.js.map +1 -0
- package/lib/esm/utils/bls.d.ts +9 -0
- package/lib/esm/utils/bls.js +43 -0
- package/lib/esm/utils/bls.js.map +1 -0
- package/lib/esm/utils/buffer.d.ts +16 -0
- package/lib/esm/utils/buffer.js +52 -0
- package/lib/esm/utils/buffer.js.map +1 -0
- package/lib/esm/vendor/bls/bls.d.ts +28 -0
- package/lib/esm/vendor/bls/bls.js +870 -0
- package/lib/esm/vendor/bls/bls.js.map +1 -0
- package/package.json +4 -4
@@ -0,0 +1,200 @@
|
|
1
|
+
import { getDefaultAgent } from './agent';
|
2
|
+
import * as cbor from './cbor';
|
3
|
+
import { AgentError } from './errors';
|
4
|
+
import { hash } from './request_id';
|
5
|
+
import { blsVerify } from './utils/bls';
|
6
|
+
import { concat, fromHex, toHex } from './utils/buffer';
|
7
|
+
/**
|
8
|
+
* A certificate needs to be verified (using {@link Certificate.prototype.verify})
|
9
|
+
* before it can be used.
|
10
|
+
*/
|
11
|
+
export class UnverifiedCertificateError extends AgentError {
|
12
|
+
constructor() {
|
13
|
+
super(`Cannot lookup unverified certificate. Call 'verify()' first.`);
|
14
|
+
}
|
15
|
+
}
|
16
|
+
/**
|
17
|
+
* Make a human readable string out of a hash tree.
|
18
|
+
* @param tree
|
19
|
+
*/
|
20
|
+
export function hashTreeToString(tree) {
|
21
|
+
const indent = (s) => s
|
22
|
+
.split('\n')
|
23
|
+
.map(x => ' ' + x)
|
24
|
+
.join('\n');
|
25
|
+
function labelToString(label) {
|
26
|
+
const decoder = new TextDecoder(undefined, { fatal: true });
|
27
|
+
try {
|
28
|
+
return JSON.stringify(decoder.decode(new Uint8Array(label)));
|
29
|
+
}
|
30
|
+
catch (e) {
|
31
|
+
return `data(...${label.byteLength} bytes)`;
|
32
|
+
}
|
33
|
+
}
|
34
|
+
switch (tree[0]) {
|
35
|
+
case 0 /* Empty */:
|
36
|
+
return '()';
|
37
|
+
case 1 /* Fork */: {
|
38
|
+
const left = hashTreeToString(tree[1]);
|
39
|
+
const right = hashTreeToString(tree[2]);
|
40
|
+
return `sub(\n left:\n${indent(left)}\n---\n right:\n${indent(right)}\n)`;
|
41
|
+
}
|
42
|
+
case 2 /* Labeled */: {
|
43
|
+
const label = labelToString(tree[1]);
|
44
|
+
const sub = hashTreeToString(tree[2]);
|
45
|
+
return `label(\n label:\n${indent(label)}\n sub:\n${indent(sub)}\n)`;
|
46
|
+
}
|
47
|
+
case 3 /* Leaf */: {
|
48
|
+
return `leaf(...${tree[1].byteLength} bytes)`;
|
49
|
+
}
|
50
|
+
case 4 /* Pruned */: {
|
51
|
+
return `pruned(${toHex(new Uint8Array(tree[1]))}`;
|
52
|
+
}
|
53
|
+
default: {
|
54
|
+
return `unknown(${JSON.stringify(tree[0])})`;
|
55
|
+
}
|
56
|
+
}
|
57
|
+
}
|
58
|
+
function isBufferEqual(a, b) {
|
59
|
+
if (a.byteLength !== b.byteLength) {
|
60
|
+
return false;
|
61
|
+
}
|
62
|
+
const a8 = new Uint8Array(a);
|
63
|
+
const b8 = new Uint8Array(b);
|
64
|
+
for (let i = 0; i < a8.length; i++) {
|
65
|
+
if (a8[i] !== b8[i]) {
|
66
|
+
return false;
|
67
|
+
}
|
68
|
+
}
|
69
|
+
return true;
|
70
|
+
}
|
71
|
+
export class Certificate {
|
72
|
+
constructor(response, _agent = getDefaultAgent()) {
|
73
|
+
this._agent = _agent;
|
74
|
+
this.verified = false;
|
75
|
+
this._rootKey = null;
|
76
|
+
this.cert = cbor.decode(new Uint8Array(response.certificate));
|
77
|
+
}
|
78
|
+
lookup(path) {
|
79
|
+
this.checkState();
|
80
|
+
return lookup_path(path, this.cert.tree);
|
81
|
+
}
|
82
|
+
async verify() {
|
83
|
+
const rootHash = await reconstruct(this.cert.tree);
|
84
|
+
const derKey = await this._checkDelegation(this.cert.delegation);
|
85
|
+
const sig = this.cert.signature;
|
86
|
+
const key = extractDER(derKey);
|
87
|
+
const msg = concat(domain_sep('ic-state-root'), rootHash);
|
88
|
+
const res = await blsVerify(new Uint8Array(key), new Uint8Array(sig), new Uint8Array(msg));
|
89
|
+
this.verified = res;
|
90
|
+
return res;
|
91
|
+
}
|
92
|
+
checkState() {
|
93
|
+
if (!this.verified) {
|
94
|
+
throw new UnverifiedCertificateError();
|
95
|
+
}
|
96
|
+
}
|
97
|
+
async _checkDelegation(d) {
|
98
|
+
if (!d) {
|
99
|
+
if (!this._rootKey) {
|
100
|
+
if (this._agent.rootKey) {
|
101
|
+
this._rootKey = this._agent.rootKey;
|
102
|
+
return this._rootKey;
|
103
|
+
}
|
104
|
+
throw new Error(`Agent does not have a rootKey. Do you need to call 'fetchRootKey'?`);
|
105
|
+
}
|
106
|
+
return this._rootKey;
|
107
|
+
}
|
108
|
+
const cert = new Certificate(d, this._agent);
|
109
|
+
if (!(await cert.verify())) {
|
110
|
+
throw new Error('fail to verify delegation certificate');
|
111
|
+
}
|
112
|
+
const lookup = cert.lookup(['subnet', d.subnet_id, 'public_key']);
|
113
|
+
if (!lookup) {
|
114
|
+
throw new Error(`Could not find subnet key for subnet 0x${toHex(d.subnet_id)}`);
|
115
|
+
}
|
116
|
+
return lookup;
|
117
|
+
}
|
118
|
+
}
|
119
|
+
const DER_PREFIX = fromHex('308182301d060d2b0601040182dc7c0503010201060c2b0601040182dc7c05030201036100');
|
120
|
+
const KEY_LENGTH = 96;
|
121
|
+
function extractDER(buf) {
|
122
|
+
const expectedLength = DER_PREFIX.byteLength + KEY_LENGTH;
|
123
|
+
if (buf.byteLength !== expectedLength) {
|
124
|
+
throw new TypeError(`BLS DER-encoded public key must be ${expectedLength} bytes long`);
|
125
|
+
}
|
126
|
+
const prefix = buf.slice(0, DER_PREFIX.byteLength);
|
127
|
+
if (!isBufferEqual(prefix, DER_PREFIX)) {
|
128
|
+
throw new TypeError(`BLS DER-encoded public key is invalid. Expect the following prefix: ${DER_PREFIX}, but get ${prefix}`);
|
129
|
+
}
|
130
|
+
return buf.slice(DER_PREFIX.byteLength);
|
131
|
+
}
|
132
|
+
/**
|
133
|
+
* @param t
|
134
|
+
*/
|
135
|
+
export async function reconstruct(t) {
|
136
|
+
switch (t[0]) {
|
137
|
+
case 0 /* Empty */:
|
138
|
+
return hash(domain_sep('ic-hashtree-empty'));
|
139
|
+
case 4 /* Pruned */:
|
140
|
+
return t[1];
|
141
|
+
case 3 /* Leaf */:
|
142
|
+
return hash(concat(domain_sep('ic-hashtree-leaf'), t[1]));
|
143
|
+
case 2 /* Labeled */:
|
144
|
+
return hash(concat(domain_sep('ic-hashtree-labeled'), t[1], await reconstruct(t[2])));
|
145
|
+
case 1 /* Fork */:
|
146
|
+
return hash(concat(domain_sep('ic-hashtree-fork'), await reconstruct(t[1]), await reconstruct(t[2])));
|
147
|
+
default:
|
148
|
+
throw new Error('unreachable');
|
149
|
+
}
|
150
|
+
}
|
151
|
+
function domain_sep(s) {
|
152
|
+
const len = new Uint8Array([s.length]);
|
153
|
+
const str = new TextEncoder().encode(s);
|
154
|
+
return concat(len.buffer, str.buffer);
|
155
|
+
}
|
156
|
+
/**
|
157
|
+
* @param path
|
158
|
+
* @param tree
|
159
|
+
*/
|
160
|
+
export function lookup_path(path, tree) {
|
161
|
+
if (path.length === 0) {
|
162
|
+
switch (tree[0]) {
|
163
|
+
case 3 /* Leaf */: {
|
164
|
+
return new Uint8Array(tree[1]).buffer;
|
165
|
+
}
|
166
|
+
default: {
|
167
|
+
return undefined;
|
168
|
+
}
|
169
|
+
}
|
170
|
+
}
|
171
|
+
const label = typeof path[0] === 'string' ? new TextEncoder().encode(path[0]) : path[0];
|
172
|
+
const t = find_label(label, flatten_forks(tree));
|
173
|
+
if (t) {
|
174
|
+
return lookup_path(path.slice(1), t);
|
175
|
+
}
|
176
|
+
}
|
177
|
+
function flatten_forks(t) {
|
178
|
+
switch (t[0]) {
|
179
|
+
case 0 /* Empty */:
|
180
|
+
return [];
|
181
|
+
case 1 /* Fork */:
|
182
|
+
return flatten_forks(t[1]).concat(flatten_forks(t[2]));
|
183
|
+
default:
|
184
|
+
return [t];
|
185
|
+
}
|
186
|
+
}
|
187
|
+
function find_label(l, trees) {
|
188
|
+
if (trees.length === 0) {
|
189
|
+
return undefined;
|
190
|
+
}
|
191
|
+
for (const t of trees) {
|
192
|
+
if (t[0] === 2 /* Labeled */) {
|
193
|
+
const p = t[1];
|
194
|
+
if (isBufferEqual(l, p)) {
|
195
|
+
return t[2];
|
196
|
+
}
|
197
|
+
}
|
198
|
+
}
|
199
|
+
}
|
200
|
+
//# sourceMappingURL=certificate.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"certificate.js","sourceRoot":"","sources":["../../src/certificate.ts"],"names":[],"mappings":"AAAA,OAAO,EAAS,eAAe,EAAqB,MAAM,SAAS,CAAC;AACpE,OAAO,KAAK,IAAI,MAAM,QAAQ,CAAC;AAC/B,OAAO,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AACtC,OAAO,EAAE,IAAI,EAAE,MAAM,cAAc,CAAC;AACpC,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACxC,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAC;AAExD;;;GAGG;AACH,MAAM,OAAO,0BAA2B,SAAQ,UAAU;IACxD;QACE,KAAK,CAAC,8DAA8D,CAAC,CAAC;IACxE,CAAC;CACF;AAuBD;;;GAGG;AACH,MAAM,UAAU,gBAAgB,CAAC,IAAc;IAC7C,MAAM,MAAM,GAAG,CAAC,CAAS,EAAE,EAAE,CAC3B,CAAC;SACE,KAAK,CAAC,IAAI,CAAC;SACX,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,IAAI,GAAG,CAAC,CAAC;SAClB,IAAI,CAAC,IAAI,CAAC,CAAC;IAChB,SAAS,aAAa,CAAC,KAAkB;QACvC,MAAM,OAAO,GAAG,IAAI,WAAW,CAAC,SAAS,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;QAC5D,IAAI;YACF,OAAO,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,UAAU,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;SAC9D;QAAC,OAAO,CAAC,EAAE;YAC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CAAC,CAAC,CAAC,CAAa,CAAC,CAAC,CAAC;QACjF;YACE,OAAO,CAAC,CAAC,CAAC,CAAC;KACd;AACH,CAAC;AACD,SAAS,UAAU,CAAC,CAAc,EAAE,KAAiB;IACnD,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC,EAAE;QACtB,OAAO,SAAS,CAAC;KAClB;IACD,KAAK,MAAM,CAAC,IAAI,KAAK,EAAE;QACrB,IAAI,CAAC,CAAC,CAAC,CAAC,oBAAmB,EAAE;YAC3B,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAgB,CAAC;YAC9B,IAAI,aAAa,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE;gBACvB,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC;aACb;SACF;KACF;AACH,CAAC"}
|
@@ -0,0 +1,9 @@
|
|
1
|
+
/**
|
2
|
+
* An error that happens in the Agent. This is the root of all errors and should be used
|
3
|
+
* everywhere in the Agent code (this package).
|
4
|
+
*
|
5
|
+
* @todo https://github.com/dfinity/agent-js/issues/420
|
6
|
+
*/
|
7
|
+
export class AgentError extends Error {
|
8
|
+
}
|
9
|
+
//# sourceMappingURL=errors.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../../src/errors.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,MAAM,OAAO,UAAW,SAAQ,KAAK;CAAG"}
|
@@ -0,0 +1,52 @@
|
|
1
|
+
import { ActorSubclass } from './actor';
|
2
|
+
export * from './actor';
|
3
|
+
export * from './agent';
|
4
|
+
export * from './auth';
|
5
|
+
export * from './certificate';
|
6
|
+
export * from './agent/http/transforms';
|
7
|
+
export * from './agent/http/types';
|
8
|
+
export * from './canisters/asset';
|
9
|
+
export * from './canisters/management';
|
10
|
+
export * from './request_id';
|
11
|
+
export * from './utils/bls';
|
12
|
+
export * from './utils/buffer';
|
13
|
+
export * as polling from './polling';
|
14
|
+
import { IDL } from '@astrox/candid';
|
15
|
+
import { Agent, HttpAgent } from './agent';
|
16
|
+
export * as Cbor from './cbor';
|
17
|
+
export interface Kraken {
|
18
|
+
methodChannel: {
|
19
|
+
invokeMethod: (method: string, ...args: any[]) => Promise<any>;
|
20
|
+
};
|
21
|
+
}
|
22
|
+
export interface GlobalInternetComputer {
|
23
|
+
kraken?: Kraken;
|
24
|
+
ic: {
|
25
|
+
agent: Agent;
|
26
|
+
HttpAgent: typeof HttpAgent;
|
27
|
+
IDL: typeof IDL;
|
28
|
+
/**
|
29
|
+
* Simple advertisement of features in whoever is managing this `globalThis.ic`.
|
30
|
+
* Use Case
|
31
|
+
* * Scripts that know they need an ic feature can detect using this and, if not present
|
32
|
+
* (e.g. old bootstrap version), they can dynamically include their own and continue
|
33
|
+
* operating (e.g. polyfill).
|
34
|
+
* This is useful when adding features to bootstrap. You can still deploy your canister to
|
35
|
+
* an ic with old bootstrap, then just dynamically reload your own new-version bootstrap if
|
36
|
+
* needed.
|
37
|
+
*/
|
38
|
+
features?: {
|
39
|
+
/** This is falsy if authn isn't supported at all */
|
40
|
+
authentication?: boolean;
|
41
|
+
};
|
42
|
+
/**
|
43
|
+
* The Actor for the canister being used for the frontend. Normally should correspond to the
|
44
|
+
* canister represented by the canister id in the URL.
|
45
|
+
*
|
46
|
+
* It does not have any functions configured.
|
47
|
+
*
|
48
|
+
* If a canister ID could not be found, no actor were created and this is undefined.
|
49
|
+
*/
|
50
|
+
canister: ActorSubclass | undefined;
|
51
|
+
};
|
52
|
+
}
|
package/lib/esm/index.js
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
export * from './actor';
|
2
|
+
export * from './agent';
|
3
|
+
export * from './auth';
|
4
|
+
export * from './certificate';
|
5
|
+
export * from './agent/http/transforms';
|
6
|
+
export * from './agent/http/types';
|
7
|
+
export * from './canisters/asset';
|
8
|
+
export * from './canisters/management';
|
9
|
+
export * from './request_id';
|
10
|
+
export * from './utils/bls';
|
11
|
+
export * from './utils/buffer';
|
12
|
+
export * as polling from './polling';
|
13
|
+
export * as Cbor from './cbor';
|
14
|
+
//# sourceMappingURL=index.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAEA,cAAc,SAAS,CAAC;AACxB,cAAc,SAAS,CAAC;AACxB,cAAc,QAAQ,CAAC;AACvB,cAAc,eAAe,CAAC;AAC9B,cAAc,yBAAyB,CAAC;AACxC,cAAc,oBAAoB,CAAC;AACnC,cAAc,mBAAmB,CAAC;AAClC,cAAc,wBAAwB,CAAC;AACvC,cAAc,cAAc,CAAC;AAC7B,cAAc,aAAa,CAAC;AAC5B,cAAc,gBAAgB,CAAC;AAC/B,OAAO,KAAK,OAAO,MAAM,WAAW,CAAC;AAKrC,OAAO,KAAK,IAAI,MAAM,QAAQ,CAAC"}
|
@@ -0,0 +1,16 @@
|
|
1
|
+
import { Principal } from '@astrox/principal';
|
2
|
+
import { Agent, RequestStatusResponseStatus } from '../agent';
|
3
|
+
import { RequestId } from '../request_id';
|
4
|
+
export * as strategy from './strategy';
|
5
|
+
export { defaultStrategy } from './strategy';
|
6
|
+
export declare type PollStrategy = (canisterId: Principal, requestId: RequestId, status: RequestStatusResponseStatus) => Promise<void>;
|
7
|
+
export declare type PollStrategyFactory = () => PollStrategy;
|
8
|
+
/**
|
9
|
+
* Polls the IC to check the status of the given request then
|
10
|
+
* returns the response bytes once the request has been processed.
|
11
|
+
* @param agent The agent to use to poll read_state.
|
12
|
+
* @param canisterId The effective canister ID.
|
13
|
+
* @param requestId The Request ID to poll status for.
|
14
|
+
* @param strategy A polling strategy.
|
15
|
+
*/
|
16
|
+
export declare function pollForResponse(agent: Agent, canisterId: Principal, requestId: RequestId, strategy: PollStrategy): Promise<ArrayBuffer>;
|
@@ -0,0 +1,57 @@
|
|
1
|
+
import { RequestStatusResponseStatus } from '../agent';
|
2
|
+
import { Certificate } from '../certificate';
|
3
|
+
import { toHex } from '../utils/buffer';
|
4
|
+
export * as strategy from './strategy';
|
5
|
+
export { defaultStrategy } from './strategy';
|
6
|
+
/**
|
7
|
+
* Polls the IC to check the status of the given request then
|
8
|
+
* returns the response bytes once the request has been processed.
|
9
|
+
* @param agent The agent to use to poll read_state.
|
10
|
+
* @param canisterId The effective canister ID.
|
11
|
+
* @param requestId The Request ID to poll status for.
|
12
|
+
* @param strategy A polling strategy.
|
13
|
+
*/
|
14
|
+
export async function pollForResponse(agent, canisterId, requestId, strategy) {
|
15
|
+
const path = [new TextEncoder().encode('request_status'), requestId];
|
16
|
+
const state = await agent.readState(canisterId, { paths: [path] });
|
17
|
+
const cert = new Certificate(state, agent);
|
18
|
+
const verified = await cert.verify();
|
19
|
+
if (!verified) {
|
20
|
+
throw new Error('Fail to verify certificate');
|
21
|
+
}
|
22
|
+
const maybeBuf = cert.lookup([...path, new TextEncoder().encode('status').buffer]);
|
23
|
+
let status;
|
24
|
+
if (typeof maybeBuf === 'undefined') {
|
25
|
+
// Missing requestId means we need to wait
|
26
|
+
status = RequestStatusResponseStatus.Unknown;
|
27
|
+
}
|
28
|
+
else {
|
29
|
+
status = new TextDecoder().decode(new Uint8Array(maybeBuf));
|
30
|
+
}
|
31
|
+
switch (status) {
|
32
|
+
case RequestStatusResponseStatus.Replied: {
|
33
|
+
return cert.lookup([...path, 'reply']);
|
34
|
+
}
|
35
|
+
case RequestStatusResponseStatus.Received:
|
36
|
+
case RequestStatusResponseStatus.Unknown:
|
37
|
+
case RequestStatusResponseStatus.Processing:
|
38
|
+
// Execute the polling strategy, then retry.
|
39
|
+
await strategy(canisterId, requestId, status);
|
40
|
+
return pollForResponse(agent, canisterId, requestId, strategy);
|
41
|
+
case RequestStatusResponseStatus.Rejected: {
|
42
|
+
const rejectCode = new Uint8Array(cert.lookup([...path, 'reject_code']))[0];
|
43
|
+
const rejectMessage = new TextDecoder().decode(new Uint8Array(cert.lookup([...path, 'reject_message'])));
|
44
|
+
throw new Error(`Call was rejected:\n` +
|
45
|
+
` Request ID: ${toHex(requestId)}\n` +
|
46
|
+
` Reject code: ${rejectCode}\n` +
|
47
|
+
` Reject text: ${rejectMessage}\n`);
|
48
|
+
}
|
49
|
+
case RequestStatusResponseStatus.Done:
|
50
|
+
// This is _technically_ not an error, but we still didn't see the `Replied` status so
|
51
|
+
// we don't know the result and cannot decode it.
|
52
|
+
throw new Error(`Call was marked as done but we never saw the reply:\n` +
|
53
|
+
` Request ID: ${toHex(requestId)}\n`);
|
54
|
+
}
|
55
|
+
throw new Error('unreachable');
|
56
|
+
}
|
57
|
+
//# sourceMappingURL=index.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/polling/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAS,2BAA2B,EAAE,MAAM,UAAU,CAAC;AAC9D,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAE7C,OAAO,EAAE,KAAK,EAAE,MAAM,iBAAiB,CAAC;AAExC,OAAO,KAAK,QAAQ,MAAM,YAAY,CAAC;AACvC,OAAO,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAQ7C;;;;;;;GAOG;AACH,MAAM,CAAC,KAAK,UAAU,eAAe,CACnC,KAAY,EACZ,UAAqB,EACrB,SAAoB,EACpB,QAAsB;IAEtB,MAAM,IAAI,GAAG,CAAC,IAAI,WAAW,EAAE,CAAC,MAAM,CAAC,gBAAgB,CAAC,EAAE,SAAS,CAAC,CAAC;IACrE,MAAM,KAAK,GAAG,MAAM,KAAK,CAAC,SAAS,CAAC,UAAU,EAAE,EAAE,KAAK,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IACnE,MAAM,IAAI,GAAG,IAAI,WAAW,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;IAC3C,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,MAAM,EAAE,CAAC;IACrC,IAAI,CAAC,QAAQ,EAAE;QACb,MAAM,IAAI,KAAK,CAAC,4BAA4B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AsB;gBACpB,iBAAiB,KAAK,CAAC,SAAS,CAAC,IAAI;gBACrC,kBAAkB,UAAU,IAAI;gBAChC,kBAAkB,aAAa,IAAI,CACtC,CAAC;SACH;QAED,KAAK,2BAA2B,CAAC,IAAI;YACnC,sFAAsF;YACtF,iDAAiD;YACjD,MAAM,IAAI,KAAK,CACb,uDAAuD;gBACrD,iBAAiB,KAAK,CAAC,SAAS,CAAC,IAAI,CACxC,CAAC;KACL;IACD,MAAM,IAAI,KAAK,CAAC,aAAa,CAAC,CAAC;AACjC,CAAC"}
|
@@ -0,0 +1,47 @@
|
|
1
|
+
import { Principal } from '@astrox/principal';
|
2
|
+
import { RequestId, RequestStatusResponseStatus } from '..';
|
3
|
+
import { PollStrategy } from './index';
|
4
|
+
export declare type Predicate<T> = (canisterId: Principal, requestId: RequestId, status: RequestStatusResponseStatus) => Promise<T>;
|
5
|
+
/**
|
6
|
+
* A best practices polling strategy: wait 2 seconds before the first poll, then 1 second
|
7
|
+
* with an exponential backoff factor of 1.2. Timeout after 5 minutes.
|
8
|
+
*/
|
9
|
+
export declare function defaultStrategy(): PollStrategy;
|
10
|
+
/**
|
11
|
+
* Predicate that returns true once.
|
12
|
+
*/
|
13
|
+
export declare function once(): Predicate<boolean>;
|
14
|
+
/**
|
15
|
+
* Delay the polling once.
|
16
|
+
* @param condition A predicate that indicates when to delay.
|
17
|
+
* @param timeInMsec The amount of time to delay.
|
18
|
+
*/
|
19
|
+
export declare function conditionalDelay(condition: Predicate<boolean>, timeInMsec: number): PollStrategy;
|
20
|
+
/**
|
21
|
+
* Error out after a maximum number of polling has been done.
|
22
|
+
* @param count The maximum attempts to poll.
|
23
|
+
*/
|
24
|
+
export declare function maxAttempts(count: number): PollStrategy;
|
25
|
+
/**
|
26
|
+
* Throttle polling.
|
27
|
+
* @param throttleInMsec Amount in millisecond to wait between each polling.
|
28
|
+
*/
|
29
|
+
export declare function throttle(throttleInMsec: number): PollStrategy;
|
30
|
+
/**
|
31
|
+
* Reject a call after a certain amount of time.
|
32
|
+
* @param timeInMsec Time in milliseconds before the polling should be rejected.
|
33
|
+
*/
|
34
|
+
export declare function timeout(timeInMsec: number): PollStrategy;
|
35
|
+
/**
|
36
|
+
* A strategy that throttle, but using an exponential backoff strategy.
|
37
|
+
* @param startingThrottleInMsec The throttle in milliseconds to start with.
|
38
|
+
* @param backoffFactor The factor to multiple the throttle time between every poll. For
|
39
|
+
* example if using 2, the throttle will double between every run.
|
40
|
+
*/
|
41
|
+
export declare function backoff(startingThrottleInMsec: number, backoffFactor: number): PollStrategy;
|
42
|
+
/**
|
43
|
+
* Chain multiple polling strategy. This _chains_ the strategies, so if you pass in,
|
44
|
+
* say, two throttling strategy of 1 second, it will result in a throttle of 2 seconds.
|
45
|
+
* @param strategies A strategy list to chain.
|
46
|
+
*/
|
47
|
+
export declare function chain(...strategies: PollStrategy[]): PollStrategy;
|
@@ -0,0 +1,95 @@
|
|
1
|
+
import { toHex } from '../utils/buffer';
|
2
|
+
const FIVE_MINUTES_IN_MSEC = 5 * 60 * 1000;
|
3
|
+
/**
|
4
|
+
* A best practices polling strategy: wait 2 seconds before the first poll, then 1 second
|
5
|
+
* with an exponential backoff factor of 1.2. Timeout after 5 minutes.
|
6
|
+
*/
|
7
|
+
export function defaultStrategy() {
|
8
|
+
return chain(conditionalDelay(once(), 1000), backoff(1000, 1.2), timeout(FIVE_MINUTES_IN_MSEC));
|
9
|
+
}
|
10
|
+
/**
|
11
|
+
* Predicate that returns true once.
|
12
|
+
*/
|
13
|
+
export function once() {
|
14
|
+
let first = true;
|
15
|
+
return async () => {
|
16
|
+
if (first) {
|
17
|
+
first = false;
|
18
|
+
return true;
|
19
|
+
}
|
20
|
+
return false;
|
21
|
+
};
|
22
|
+
}
|
23
|
+
/**
|
24
|
+
* Delay the polling once.
|
25
|
+
* @param condition A predicate that indicates when to delay.
|
26
|
+
* @param timeInMsec The amount of time to delay.
|
27
|
+
*/
|
28
|
+
export function conditionalDelay(condition, timeInMsec) {
|
29
|
+
return async (canisterId, requestId, status) => {
|
30
|
+
if (await condition(canisterId, requestId, status)) {
|
31
|
+
return new Promise(resolve => setTimeout(resolve, timeInMsec));
|
32
|
+
}
|
33
|
+
};
|
34
|
+
}
|
35
|
+
/**
|
36
|
+
* Error out after a maximum number of polling has been done.
|
37
|
+
* @param count The maximum attempts to poll.
|
38
|
+
*/
|
39
|
+
export function maxAttempts(count) {
|
40
|
+
let attempts = count;
|
41
|
+
return async (canisterId, requestId, status) => {
|
42
|
+
if (--attempts <= 0) {
|
43
|
+
throw new Error(`Failed to retrieve a reply for request after ${count} attempts:\n` +
|
44
|
+
` Request ID: ${toHex(requestId)}\n` +
|
45
|
+
` Request status: ${status}\n`);
|
46
|
+
}
|
47
|
+
};
|
48
|
+
}
|
49
|
+
/**
|
50
|
+
* Throttle polling.
|
51
|
+
* @param throttleInMsec Amount in millisecond to wait between each polling.
|
52
|
+
*/
|
53
|
+
export function throttle(throttleInMsec) {
|
54
|
+
return () => new Promise(resolve => setTimeout(resolve, throttleInMsec));
|
55
|
+
}
|
56
|
+
/**
|
57
|
+
* Reject a call after a certain amount of time.
|
58
|
+
* @param timeInMsec Time in milliseconds before the polling should be rejected.
|
59
|
+
*/
|
60
|
+
export function timeout(timeInMsec) {
|
61
|
+
const end = Date.now() + timeInMsec;
|
62
|
+
return async (canisterId, requestId, status) => {
|
63
|
+
if (Date.now() > end) {
|
64
|
+
throw new Error(`Request timed out after ${timeInMsec} msec:\n` +
|
65
|
+
` Request ID: ${toHex(requestId)}\n` +
|
66
|
+
` Request status: ${status}\n`);
|
67
|
+
}
|
68
|
+
};
|
69
|
+
}
|
70
|
+
/**
|
71
|
+
* A strategy that throttle, but using an exponential backoff strategy.
|
72
|
+
* @param startingThrottleInMsec The throttle in milliseconds to start with.
|
73
|
+
* @param backoffFactor The factor to multiple the throttle time between every poll. For
|
74
|
+
* example if using 2, the throttle will double between every run.
|
75
|
+
*/
|
76
|
+
export function backoff(startingThrottleInMsec, backoffFactor) {
|
77
|
+
let currentThrottling = startingThrottleInMsec;
|
78
|
+
return () => new Promise(resolve => setTimeout(() => {
|
79
|
+
currentThrottling *= backoffFactor;
|
80
|
+
resolve();
|
81
|
+
}, currentThrottling));
|
82
|
+
}
|
83
|
+
/**
|
84
|
+
* Chain multiple polling strategy. This _chains_ the strategies, so if you pass in,
|
85
|
+
* say, two throttling strategy of 1 second, it will result in a throttle of 2 seconds.
|
86
|
+
* @param strategies A strategy list to chain.
|
87
|
+
*/
|
88
|
+
export function chain(...strategies) {
|
89
|
+
return async (canisterId, requestId, status) => {
|
90
|
+
for (const a of strategies) {
|
91
|
+
await a(canisterId, requestId, status);
|
92
|
+
}
|
93
|
+
};
|
94
|
+
}
|
95
|
+
//# sourceMappingURL=strategy.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"strategy.js","sourceRoot":"","sources":["../../../src/polling/strategy.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,KAAK,EAAE,MAAM,iBAAiB,CAAC;AASxC,MAAM,oBAAoB,GAAG,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC;AAE3C;;;GAGG;AACH,MAAM,UAAU,eAAe;IAC7B,OAAO,KAAK,CAAC,gBAAgB,CAAC,IAAI,EAAE,EAAE,IAAI,CAAC,EAAE,OAAO,CAAC,IAAI,EAAE,GAAG,CAAC,EAAE,OAAO,CAAC,oBAAoB,CAAC,CAAC,CAAC;AAClG,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,IAAI;IAClB,IAAI,KAAK,GAAG,IAAI,CAAC;IACjB,OAAO,KAAK,IAAI,EAAE;QAChB,IAAI,KAAK,EAAE;YACT,KAAK,GAAG,KAAK,CAAC;YACd,OAAO,IAAI,CAAC;SACb;QACD,OAAO,KAAK,CAAC;IACf,CAAC,CAAC;AACJ,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,gBAAgB,CAAC,SAA6B,EAAE,UAAkB;IAChF,OAAO,KAAK,EACV,UAAqB,EACrB,SAAoB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AC,sBAA8B,EAAE,aAAqB;IAC3E,IAAI,iBAAiB,GAAG,sBAAsB,CAAC;IAE/C,OAAO,GAAG,EAAE,CACV,IAAI,OAAO,CAAC,OAAO,CAAC,EAAE,CACpB,UAAU,CAAC,GAAG,EAAE;QACd,iBAAiB,IAAI,aAAa,CAAC;QACnC,OAAO,EAAE,CAAC;IACZ,CAAC,EAAE,iBAAiB,CAAC,CACtB,CAAC;AACN,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,KAAK,CAAC,GAAG,UAA0B;IACjD,OAAO,KAAK,EACV,UAAqB,EACrB,SAAoB,EACpB,MAAmC,EACnC,EAAE;QACF,KAAK,MAAM,CAAC,IAAI,UAAU,EAAE;YAC1B,MAAM,CAAC,CAAC,UAAU,EAAE,SAAS,EAAE,MAAM,CAAC,CAAC;SACxC;IACH,CAAC,CAAC;AACJ,CAAC"}
|
@@ -0,0 +1,15 @@
|
|
1
|
+
export declare type RequestId = ArrayBuffer & {
|
2
|
+
__requestId__: void;
|
3
|
+
};
|
4
|
+
/**
|
5
|
+
* sha256 hash the provided Buffer
|
6
|
+
* @param data - input to hash function
|
7
|
+
*/
|
8
|
+
export declare function hash(data: ArrayBuffer): ArrayBuffer;
|
9
|
+
/**
|
10
|
+
* Get the RequestId of the provided ic-ref request.
|
11
|
+
* RequestId is the result of the representation-independent-hash function.
|
12
|
+
* https://sdk.dfinity.org/docs/interface-spec/index.html#hash-of-map
|
13
|
+
* @param request - ic-ref request to hash into RequestId
|
14
|
+
*/
|
15
|
+
export declare function requestIdOf(request: Record<string, any>): RequestId;
|
@@ -0,0 +1,81 @@
|
|
1
|
+
import { lebEncode } from '@astrox/candid';
|
2
|
+
import { Principal } from '@astrox/principal';
|
3
|
+
import borc from 'borc';
|
4
|
+
import { sha256 as jsSha256 } from 'js-sha256';
|
5
|
+
import { compare, concat } from './utils/buffer';
|
6
|
+
/**
|
7
|
+
* sha256 hash the provided Buffer
|
8
|
+
* @param data - input to hash function
|
9
|
+
*/
|
10
|
+
export function hash(data) {
|
11
|
+
return jsSha256.create().update(new Uint8Array(data)).arrayBuffer();
|
12
|
+
}
|
13
|
+
function hashValue(value) {
|
14
|
+
if (value instanceof borc.Tagged) {
|
15
|
+
return hashValue(value.value);
|
16
|
+
}
|
17
|
+
else if (typeof value === 'string') {
|
18
|
+
return hashString(value);
|
19
|
+
}
|
20
|
+
else if (typeof value === 'number') {
|
21
|
+
return hash(lebEncode(value));
|
22
|
+
}
|
23
|
+
else if (value instanceof ArrayBuffer || ArrayBuffer.isView(value)) {
|
24
|
+
return hash(value);
|
25
|
+
}
|
26
|
+
else if (Array.isArray(value)) {
|
27
|
+
const vals = value.map(hashValue);
|
28
|
+
return hash(concat(...vals));
|
29
|
+
}
|
30
|
+
else if (value instanceof Principal) {
|
31
|
+
return hash(value.toUint8Array());
|
32
|
+
}
|
33
|
+
else if (typeof value === 'object' &&
|
34
|
+
value !== null &&
|
35
|
+
typeof value.toHash === 'function') {
|
36
|
+
return hashValue(value.toHash());
|
37
|
+
// TODO This should be move to a specific async method as the webauthn flow required
|
38
|
+
// the flow to be synchronous to ensure Safari touch id works.
|
39
|
+
// } else if (value instanceof Promise) {
|
40
|
+
// return value.then(x => hashValue(x));
|
41
|
+
}
|
42
|
+
else if (typeof value === 'bigint') {
|
43
|
+
// Do this check much later than the other bigint check because this one is much less
|
44
|
+
// type-safe.
|
45
|
+
// So we want to try all the high-assurance type guards before this 'probable' one.
|
46
|
+
return hash(lebEncode(value));
|
47
|
+
}
|
48
|
+
throw Object.assign(new Error(`Attempt to hash a value of unsupported type: ${value}`), {
|
49
|
+
// include so logs/callers can understand the confusing value.
|
50
|
+
// (when stringified in error message, prototype info is lost)
|
51
|
+
value,
|
52
|
+
});
|
53
|
+
}
|
54
|
+
const hashString = (value) => {
|
55
|
+
const encoded = new TextEncoder().encode(value);
|
56
|
+
return hash(encoded);
|
57
|
+
};
|
58
|
+
/**
|
59
|
+
* Get the RequestId of the provided ic-ref request.
|
60
|
+
* RequestId is the result of the representation-independent-hash function.
|
61
|
+
* https://sdk.dfinity.org/docs/interface-spec/index.html#hash-of-map
|
62
|
+
* @param request - ic-ref request to hash into RequestId
|
63
|
+
*/
|
64
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
65
|
+
export function requestIdOf(request) {
|
66
|
+
const hashed = Object.entries(request)
|
67
|
+
.filter(([, value]) => value !== undefined)
|
68
|
+
.map(([key, value]) => {
|
69
|
+
const hashedKey = hashString(key);
|
70
|
+
const hashedValue = hashValue(value);
|
71
|
+
return [hashedKey, hashedValue];
|
72
|
+
});
|
73
|
+
const traversed = hashed;
|
74
|
+
const sorted = traversed.sort(([k1], [k2]) => {
|
75
|
+
return compare(k1, k2);
|
76
|
+
});
|
77
|
+
const concatenated = concat(...sorted.map(x => concat(...x)));
|
78
|
+
const requestId = hash(concatenated);
|
79
|
+
return requestId;
|
80
|
+
}
|
81
|
+
//# sourceMappingURL=request_id.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"request_id.js","sourceRoot":"","sources":["../../src/request_id.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAC3C,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AAC9C,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,EAAE,MAAM,IAAI,QAAQ,EAAE,MAAM,WAAW,CAAC;AAC/C,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AAIjD;;;GAGG;AACH,MAAM,UAAU,IAAI,CAAC,IAAiB;IACpC,OAAO,QAAQ,CAAC,MAAM,EAAE,CAAC,MAAM,CAAC,IAAI,UAAU,CAAC,IAAI,CAAC,CAAC,CAAC,WAAW,EAAE,CAAC;AACtE,CAAC;AAMD,SAAS,SAAS,CAAC,KAAc;IAC/B,IAAI,KAAK,YAAY,IAAI,CAAC,MAAM,EAAE;QAChC,OAAO,SAAS,CAAC,KAAK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array
|
5
|
+
* @param sig signature: Uint8Array
|
6
|
+
* @param msg message: Uint8Array
|
7
|
+
* @returns Promise resolving a boolean
|
8
|
+
*/
|
9
|
+
export declare function blsVerify(pk: Uint8Array, sig: Uint8Array, msg: Uint8Array): Promise<boolean>;
|