@astrox/agent 0.0.24 → 0.0.30
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/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
package/lib/cjs/cbor.js
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
+
}) : (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
o[k2] = m[k];
|
|
8
|
+
}));
|
|
9
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
10
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
11
|
+
}) : function(o, v) {
|
|
12
|
+
o["default"] = v;
|
|
13
|
+
});
|
|
14
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
15
|
+
if (mod && mod.__esModule) return mod;
|
|
16
|
+
var result = {};
|
|
17
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
18
|
+
__setModuleDefault(result, mod);
|
|
19
|
+
return result;
|
|
20
|
+
};
|
|
21
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
22
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
23
|
+
};
|
|
24
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
25
|
+
exports.decode = exports.encode = exports.CborTag = void 0;
|
|
26
|
+
// tslint:disable:max-classes-per-file
|
|
27
|
+
// This file is based on:
|
|
28
|
+
// tslint:disable-next-line: max-line-length
|
|
29
|
+
// https://github.com/dfinity-lab/dfinity/blob/9bca65f8edd65701ea6bdb00e0752f9186bbc893/docs/spec/public/index.adoc#cbor-encoding-of-requests-and-responses
|
|
30
|
+
const borc_1 = __importDefault(require("borc"));
|
|
31
|
+
const cbor = __importStar(require("simple-cbor"));
|
|
32
|
+
const simple_cbor_1 = require("simple-cbor");
|
|
33
|
+
const buffer_1 = require("./utils/buffer");
|
|
34
|
+
// We are using hansl/simple-cbor for CBOR serialization, to avoid issues with
|
|
35
|
+
// encoding the uint64 values that the HTTP handler of the client expects for
|
|
36
|
+
// canister IDs. However, simple-cbor does not yet provide deserialization so
|
|
37
|
+
// we are using `Uint8Array` so that we can use the dignifiedquire/borc CBOR
|
|
38
|
+
// decoder.
|
|
39
|
+
class PrincipalEncoder {
|
|
40
|
+
get name() {
|
|
41
|
+
return 'Principal';
|
|
42
|
+
}
|
|
43
|
+
get priority() {
|
|
44
|
+
return 0;
|
|
45
|
+
}
|
|
46
|
+
match(value) {
|
|
47
|
+
return value && value._isPrincipal === true;
|
|
48
|
+
}
|
|
49
|
+
encode(v) {
|
|
50
|
+
return cbor.value.bytes(v.toUint8Array());
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
class BufferEncoder {
|
|
54
|
+
get name() {
|
|
55
|
+
return 'Buffer';
|
|
56
|
+
}
|
|
57
|
+
get priority() {
|
|
58
|
+
return 1;
|
|
59
|
+
}
|
|
60
|
+
match(value) {
|
|
61
|
+
return value instanceof ArrayBuffer || ArrayBuffer.isView(value);
|
|
62
|
+
}
|
|
63
|
+
encode(v) {
|
|
64
|
+
return cbor.value.bytes(new Uint8Array(v));
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
class BigIntEncoder {
|
|
68
|
+
get name() {
|
|
69
|
+
return 'BigInt';
|
|
70
|
+
}
|
|
71
|
+
get priority() {
|
|
72
|
+
return 1;
|
|
73
|
+
}
|
|
74
|
+
match(value) {
|
|
75
|
+
return typeof value === `bigint`;
|
|
76
|
+
}
|
|
77
|
+
encode(v) {
|
|
78
|
+
// Always use a bigint encoding.
|
|
79
|
+
if (v > BigInt(0)) {
|
|
80
|
+
return cbor.value.tagged(2, cbor.value.bytes(buffer_1.fromHex(v.toString(16))));
|
|
81
|
+
}
|
|
82
|
+
else {
|
|
83
|
+
return cbor.value.tagged(3, cbor.value.bytes(buffer_1.fromHex((BigInt('-1') * v).toString(16))));
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
const serializer = simple_cbor_1.SelfDescribeCborSerializer.withDefaultEncoders(true);
|
|
88
|
+
serializer.addEncoder(new PrincipalEncoder());
|
|
89
|
+
serializer.addEncoder(new BufferEncoder());
|
|
90
|
+
serializer.addEncoder(new BigIntEncoder());
|
|
91
|
+
var CborTag;
|
|
92
|
+
(function (CborTag) {
|
|
93
|
+
CborTag[CborTag["Uint64LittleEndian"] = 71] = "Uint64LittleEndian";
|
|
94
|
+
CborTag[CborTag["Semantic"] = 55799] = "Semantic";
|
|
95
|
+
})(CborTag = exports.CborTag || (exports.CborTag = {}));
|
|
96
|
+
/**
|
|
97
|
+
* Encode a JavaScript value into CBOR.
|
|
98
|
+
*/
|
|
99
|
+
function encode(value) {
|
|
100
|
+
return serializer.serialize(value);
|
|
101
|
+
}
|
|
102
|
+
exports.encode = encode;
|
|
103
|
+
function decodePositiveBigInt(buf) {
|
|
104
|
+
const len = buf.byteLength;
|
|
105
|
+
let res = BigInt(0);
|
|
106
|
+
for (let i = 0; i < len; i++) {
|
|
107
|
+
// tslint:disable-next-line:no-bitwise
|
|
108
|
+
res = res * BigInt(0x100) + BigInt(buf[i]);
|
|
109
|
+
}
|
|
110
|
+
return res;
|
|
111
|
+
}
|
|
112
|
+
// A BORC subclass that decodes byte strings to ArrayBuffer instead of the Buffer class.
|
|
113
|
+
class Uint8ArrayDecoder extends borc_1.default.Decoder {
|
|
114
|
+
createByteString(raw) {
|
|
115
|
+
return buffer_1.concat(...raw);
|
|
116
|
+
}
|
|
117
|
+
createByteStringFromHeap(start, end) {
|
|
118
|
+
if (start === end) {
|
|
119
|
+
return new ArrayBuffer(0);
|
|
120
|
+
}
|
|
121
|
+
return new Uint8Array(this._heap.slice(start, end));
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
function decode(input) {
|
|
125
|
+
const buffer = new Uint8Array(input);
|
|
126
|
+
const decoder = new Uint8ArrayDecoder({
|
|
127
|
+
size: buffer.byteLength,
|
|
128
|
+
tags: {
|
|
129
|
+
// Override tags 2 and 3 for BigInt support (borc supports only BigNumber).
|
|
130
|
+
2: val => decodePositiveBigInt(val),
|
|
131
|
+
3: val => -decodePositiveBigInt(val),
|
|
132
|
+
[CborTag.Semantic]: (value) => value,
|
|
133
|
+
},
|
|
134
|
+
});
|
|
135
|
+
return decoder.decodeFirst(buffer);
|
|
136
|
+
}
|
|
137
|
+
exports.decode = decode;
|
|
138
|
+
//# sourceMappingURL=cbor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cbor.js","sourceRoot":"","sources":["../../src/cbor.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAAA,sCAAsC;AACtC,yBAAyB;AACzB,4CAA4C;AAC5C,2JAA2J;AAC3J,gDAAwB;AACxB,kDAAoC;AACpC,6CAAsE;AAEtE,2CAAiD;AAEjD,8EAA8E;AAC9E,6EAA6E;AAC7E,6EAA6E;AAC7E,4EAA4E;AAC5E,WAAW;AAEX,MAAM,gBAAgB;IACpB,IAAW,IAAI;QACb,OAAO,WAAW,CAAC;IACrB,CAAC;IAED,IAAW,QAAQ;QACjB,OAAO,CAAC,CAAC;IACX,CAAC;IAEM,KAAK,CAAC,KAAU;QACrB,OAAO,KAAK,IAAI,KAAK,CAAC,YAAY,KAAK,IAAI,CAAC;IAC9C,CAAC;IAEM,MAAM,CAAC,CAAY;QACxB,OAAO,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC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MAAM,GAAG,IAAI,UAAU,CAAC,KAAK,CAAC,CAAC;IACrC,MAAM,OAAO,GAAG,IAAI,iBAAiB,CAAC;QACpC,IAAI,EAAE,MAAM,CAAC,UAAU;QACvB,IAAI,EAAE;YACJ,2EAA2E;YAC3E,CAAC,EAAE,GAAG,CAAC,EAAE,CAAC,oBAAoB,CAAC,GAAG,CAAC;YACnC,CAAC,EAAE,GAAG,CAAC,EAAE,CAAC,CAAC,oBAAoB,CAAC,GAAG,CAAC;YACpC,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE,CAAC,KAAQ,EAAK,EAAE,CAAC,KAAK;SAC3C;KACF,CAAC,CAAC;IAEH,OAAO,OAAO,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC;AACrC,CAAC;AAbD,wBAaC"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { Agent, ReadStateResponse } from './agent';
|
|
2
|
+
import { AgentError } from './errors';
|
|
3
|
+
/**
|
|
4
|
+
* A certificate needs to be verified (using {@link Certificate.prototype.verify})
|
|
5
|
+
* before it can be used.
|
|
6
|
+
*/
|
|
7
|
+
export declare class UnverifiedCertificateError extends AgentError {
|
|
8
|
+
constructor();
|
|
9
|
+
}
|
|
10
|
+
declare const enum NodeId {
|
|
11
|
+
Empty = 0,
|
|
12
|
+
Fork = 1,
|
|
13
|
+
Labeled = 2,
|
|
14
|
+
Leaf = 3,
|
|
15
|
+
Pruned = 4
|
|
16
|
+
}
|
|
17
|
+
export declare type HashTree = [NodeId.Empty] | [NodeId.Fork, HashTree, HashTree] | [NodeId.Labeled, ArrayBuffer, HashTree] | [NodeId.Leaf, ArrayBuffer] | [NodeId.Pruned, ArrayBuffer];
|
|
18
|
+
/**
|
|
19
|
+
* Make a human readable string out of a hash tree.
|
|
20
|
+
* @param tree
|
|
21
|
+
*/
|
|
22
|
+
export declare function hashTreeToString(tree: HashTree): string;
|
|
23
|
+
export declare class Certificate {
|
|
24
|
+
private _agent;
|
|
25
|
+
private readonly cert;
|
|
26
|
+
private verified;
|
|
27
|
+
private _rootKey;
|
|
28
|
+
constructor(response: ReadStateResponse, _agent?: Agent);
|
|
29
|
+
lookup(path: Array<ArrayBuffer | string>): ArrayBuffer | undefined;
|
|
30
|
+
verify(): Promise<boolean>;
|
|
31
|
+
protected checkState(): void;
|
|
32
|
+
private _checkDelegation;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* @param t
|
|
36
|
+
*/
|
|
37
|
+
export declare function reconstruct(t: HashTree): Promise<ArrayBuffer>;
|
|
38
|
+
/**
|
|
39
|
+
* @param path
|
|
40
|
+
* @param tree
|
|
41
|
+
*/
|
|
42
|
+
export declare function lookup_path(path: Array<ArrayBuffer | string>, tree: HashTree): ArrayBuffer | undefined;
|
|
43
|
+
export {};
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
+
}) : (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
o[k2] = m[k];
|
|
8
|
+
}));
|
|
9
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
10
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
11
|
+
}) : function(o, v) {
|
|
12
|
+
o["default"] = v;
|
|
13
|
+
});
|
|
14
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
15
|
+
if (mod && mod.__esModule) return mod;
|
|
16
|
+
var result = {};
|
|
17
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
18
|
+
__setModuleDefault(result, mod);
|
|
19
|
+
return result;
|
|
20
|
+
};
|
|
21
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
22
|
+
exports.lookup_path = exports.reconstruct = exports.Certificate = exports.hashTreeToString = exports.UnverifiedCertificateError = void 0;
|
|
23
|
+
const agent_1 = require("./agent");
|
|
24
|
+
const cbor = __importStar(require("./cbor"));
|
|
25
|
+
const errors_1 = require("./errors");
|
|
26
|
+
const request_id_1 = require("./request_id");
|
|
27
|
+
const bls_1 = require("./utils/bls");
|
|
28
|
+
const buffer_1 = require("./utils/buffer");
|
|
29
|
+
/**
|
|
30
|
+
* A certificate needs to be verified (using {@link Certificate.prototype.verify})
|
|
31
|
+
* before it can be used.
|
|
32
|
+
*/
|
|
33
|
+
class UnverifiedCertificateError extends errors_1.AgentError {
|
|
34
|
+
constructor() {
|
|
35
|
+
super(`Cannot lookup unverified certificate. Call 'verify()' first.`);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
exports.UnverifiedCertificateError = UnverifiedCertificateError;
|
|
39
|
+
/**
|
|
40
|
+
* Make a human readable string out of a hash tree.
|
|
41
|
+
* @param tree
|
|
42
|
+
*/
|
|
43
|
+
function hashTreeToString(tree) {
|
|
44
|
+
const indent = (s) => s
|
|
45
|
+
.split('\n')
|
|
46
|
+
.map(x => ' ' + x)
|
|
47
|
+
.join('\n');
|
|
48
|
+
function labelToString(label) {
|
|
49
|
+
const decoder = new TextDecoder(undefined, { fatal: true });
|
|
50
|
+
try {
|
|
51
|
+
return JSON.stringify(decoder.decode(new Uint8Array(label)));
|
|
52
|
+
}
|
|
53
|
+
catch (e) {
|
|
54
|
+
return `data(...${label.byteLength} bytes)`;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
switch (tree[0]) {
|
|
58
|
+
case 0 /* Empty */:
|
|
59
|
+
return '()';
|
|
60
|
+
case 1 /* Fork */: {
|
|
61
|
+
const left = hashTreeToString(tree[1]);
|
|
62
|
+
const right = hashTreeToString(tree[2]);
|
|
63
|
+
return `sub(\n left:\n${indent(left)}\n---\n right:\n${indent(right)}\n)`;
|
|
64
|
+
}
|
|
65
|
+
case 2 /* Labeled */: {
|
|
66
|
+
const label = labelToString(tree[1]);
|
|
67
|
+
const sub = hashTreeToString(tree[2]);
|
|
68
|
+
return `label(\n label:\n${indent(label)}\n sub:\n${indent(sub)}\n)`;
|
|
69
|
+
}
|
|
70
|
+
case 3 /* Leaf */: {
|
|
71
|
+
return `leaf(...${tree[1].byteLength} bytes)`;
|
|
72
|
+
}
|
|
73
|
+
case 4 /* Pruned */: {
|
|
74
|
+
return `pruned(${buffer_1.toHex(new Uint8Array(tree[1]))}`;
|
|
75
|
+
}
|
|
76
|
+
default: {
|
|
77
|
+
return `unknown(${JSON.stringify(tree[0])})`;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
exports.hashTreeToString = hashTreeToString;
|
|
82
|
+
function isBufferEqual(a, b) {
|
|
83
|
+
if (a.byteLength !== b.byteLength) {
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
const a8 = new Uint8Array(a);
|
|
87
|
+
const b8 = new Uint8Array(b);
|
|
88
|
+
for (let i = 0; i < a8.length; i++) {
|
|
89
|
+
if (a8[i] !== b8[i]) {
|
|
90
|
+
return false;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return true;
|
|
94
|
+
}
|
|
95
|
+
class Certificate {
|
|
96
|
+
constructor(response, _agent = agent_1.getDefaultAgent()) {
|
|
97
|
+
this._agent = _agent;
|
|
98
|
+
this.verified = false;
|
|
99
|
+
this._rootKey = null;
|
|
100
|
+
this.cert = cbor.decode(new Uint8Array(response.certificate));
|
|
101
|
+
}
|
|
102
|
+
lookup(path) {
|
|
103
|
+
this.checkState();
|
|
104
|
+
return lookup_path(path, this.cert.tree);
|
|
105
|
+
}
|
|
106
|
+
async verify() {
|
|
107
|
+
const rootHash = await reconstruct(this.cert.tree);
|
|
108
|
+
const derKey = await this._checkDelegation(this.cert.delegation);
|
|
109
|
+
const sig = this.cert.signature;
|
|
110
|
+
const key = extractDER(derKey);
|
|
111
|
+
const msg = buffer_1.concat(domain_sep('ic-state-root'), rootHash);
|
|
112
|
+
const res = await bls_1.blsVerify(new Uint8Array(key), new Uint8Array(sig), new Uint8Array(msg));
|
|
113
|
+
this.verified = res;
|
|
114
|
+
return res;
|
|
115
|
+
}
|
|
116
|
+
checkState() {
|
|
117
|
+
if (!this.verified) {
|
|
118
|
+
throw new UnverifiedCertificateError();
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
async _checkDelegation(d) {
|
|
122
|
+
if (!d) {
|
|
123
|
+
if (!this._rootKey) {
|
|
124
|
+
if (this._agent.rootKey) {
|
|
125
|
+
this._rootKey = this._agent.rootKey;
|
|
126
|
+
return this._rootKey;
|
|
127
|
+
}
|
|
128
|
+
throw new Error(`Agent does not have a rootKey. Do you need to call 'fetchRootKey'?`);
|
|
129
|
+
}
|
|
130
|
+
return this._rootKey;
|
|
131
|
+
}
|
|
132
|
+
const cert = new Certificate(d, this._agent);
|
|
133
|
+
if (!(await cert.verify())) {
|
|
134
|
+
throw new Error('fail to verify delegation certificate');
|
|
135
|
+
}
|
|
136
|
+
const lookup = cert.lookup(['subnet', d.subnet_id, 'public_key']);
|
|
137
|
+
if (!lookup) {
|
|
138
|
+
throw new Error(`Could not find subnet key for subnet 0x${buffer_1.toHex(d.subnet_id)}`);
|
|
139
|
+
}
|
|
140
|
+
return lookup;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
exports.Certificate = Certificate;
|
|
144
|
+
const DER_PREFIX = buffer_1.fromHex('308182301d060d2b0601040182dc7c0503010201060c2b0601040182dc7c05030201036100');
|
|
145
|
+
const KEY_LENGTH = 96;
|
|
146
|
+
function extractDER(buf) {
|
|
147
|
+
const expectedLength = DER_PREFIX.byteLength + KEY_LENGTH;
|
|
148
|
+
if (buf.byteLength !== expectedLength) {
|
|
149
|
+
throw new TypeError(`BLS DER-encoded public key must be ${expectedLength} bytes long`);
|
|
150
|
+
}
|
|
151
|
+
const prefix = buf.slice(0, DER_PREFIX.byteLength);
|
|
152
|
+
if (!isBufferEqual(prefix, DER_PREFIX)) {
|
|
153
|
+
throw new TypeError(`BLS DER-encoded public key is invalid. Expect the following prefix: ${DER_PREFIX}, but get ${prefix}`);
|
|
154
|
+
}
|
|
155
|
+
return buf.slice(DER_PREFIX.byteLength);
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* @param t
|
|
159
|
+
*/
|
|
160
|
+
async function reconstruct(t) {
|
|
161
|
+
switch (t[0]) {
|
|
162
|
+
case 0 /* Empty */:
|
|
163
|
+
return request_id_1.hash(domain_sep('ic-hashtree-empty'));
|
|
164
|
+
case 4 /* Pruned */:
|
|
165
|
+
return t[1];
|
|
166
|
+
case 3 /* Leaf */:
|
|
167
|
+
return request_id_1.hash(buffer_1.concat(domain_sep('ic-hashtree-leaf'), t[1]));
|
|
168
|
+
case 2 /* Labeled */:
|
|
169
|
+
return request_id_1.hash(buffer_1.concat(domain_sep('ic-hashtree-labeled'), t[1], await reconstruct(t[2])));
|
|
170
|
+
case 1 /* Fork */:
|
|
171
|
+
return request_id_1.hash(buffer_1.concat(domain_sep('ic-hashtree-fork'), await reconstruct(t[1]), await reconstruct(t[2])));
|
|
172
|
+
default:
|
|
173
|
+
throw new Error('unreachable');
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
exports.reconstruct = reconstruct;
|
|
177
|
+
function domain_sep(s) {
|
|
178
|
+
const len = new Uint8Array([s.length]);
|
|
179
|
+
const str = new TextEncoder().encode(s);
|
|
180
|
+
return buffer_1.concat(len.buffer, str.buffer);
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* @param path
|
|
184
|
+
* @param tree
|
|
185
|
+
*/
|
|
186
|
+
function lookup_path(path, tree) {
|
|
187
|
+
if (path.length === 0) {
|
|
188
|
+
switch (tree[0]) {
|
|
189
|
+
case 3 /* Leaf */: {
|
|
190
|
+
return new Uint8Array(tree[1]).buffer;
|
|
191
|
+
}
|
|
192
|
+
default: {
|
|
193
|
+
return undefined;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
const label = typeof path[0] === 'string' ? new TextEncoder().encode(path[0]) : path[0];
|
|
198
|
+
const t = find_label(label, flatten_forks(tree));
|
|
199
|
+
if (t) {
|
|
200
|
+
return lookup_path(path.slice(1), t);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
exports.lookup_path = lookup_path;
|
|
204
|
+
function flatten_forks(t) {
|
|
205
|
+
switch (t[0]) {
|
|
206
|
+
case 0 /* Empty */:
|
|
207
|
+
return [];
|
|
208
|
+
case 1 /* Fork */:
|
|
209
|
+
return flatten_forks(t[1]).concat(flatten_forks(t[2]));
|
|
210
|
+
default:
|
|
211
|
+
return [t];
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
function find_label(l, trees) {
|
|
215
|
+
if (trees.length === 0) {
|
|
216
|
+
return undefined;
|
|
217
|
+
}
|
|
218
|
+
for (const t of trees) {
|
|
219
|
+
if (t[0] === 2 /* Labeled */) {
|
|
220
|
+
const p = t[1];
|
|
221
|
+
if (isBufferEqual(l, p)) {
|
|
222
|
+
return t[2];
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
//# sourceMappingURL=certificate.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"certificate.js","sourceRoot":"","sources":["../../src/certificate.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAAA,mCAAoE;AACpE,6CAA+B;AAC/B,qCAAsC;AACtC,6CAAoC;AACpC,qCAAwC;AACxC,2CAAwD;AAExD;;;GAGG;AACH,MAAa,0BAA2B,SAAQ,mBAAU;IACxD;QACE,KAAK,CAAC,8DAA8D,CAAC,CAAC;IACxE,CAAC;CACF;AAJD,gEAIC;AAuBD;;;GAGG;AACH,SAAgB,gBAAgB,CAAC,IAAc;IAC7C,MAAM,MAAM,GAAG,CAAC,CAAS,EAAE,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AC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IACxF,MAAM,CAAC,GAAG,UAAU,CAAC,KAAK,EAAE,aAAa,CAAC,IAAI,CAAC,CAAC,CAAC;IACjD,IAAI,CAAC,EAAE;QACL,OAAO,WAAW,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;KACtC;AACH,CAAC;AApBD,kCAoBC;AACD,SAAS,aAAa,CAAC,CAAW;IAChC,QAAQ,CAAC,CAAC,CAAC,CAAC,EAAE;QACZ;YACE,OAAO,EAAE,CAAC;QACZ;YACE,OAAO,aAAa,CAAC,CAAC,CAAC,CAAC,CAAa,CAAC,CAAC,MAAM,CAAC,aAAa,CAAC,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,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AgentError = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* An error that happens in the Agent. This is the root of all errors and should be used
|
|
6
|
+
* everywhere in the Agent code (this package).
|
|
7
|
+
*
|
|
8
|
+
* @todo https://github.com/dfinity/agent-js/issues/420
|
|
9
|
+
*/
|
|
10
|
+
class AgentError extends Error {
|
|
11
|
+
}
|
|
12
|
+
exports.AgentError = AgentError;
|
|
13
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../../src/errors.ts"],"names":[],"mappings":";;;AAAA;;;;;GAKG;AACH,MAAa,UAAW,SAAQ,KAAK;CAAG;AAAxC,gCAAwC"}
|
|
@@ -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/cjs/index.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
+
}) : (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
o[k2] = m[k];
|
|
8
|
+
}));
|
|
9
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
10
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
11
|
+
}) : function(o, v) {
|
|
12
|
+
o["default"] = v;
|
|
13
|
+
});
|
|
14
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
15
|
+
for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p);
|
|
16
|
+
};
|
|
17
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
18
|
+
if (mod && mod.__esModule) return mod;
|
|
19
|
+
var result = {};
|
|
20
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
21
|
+
__setModuleDefault(result, mod);
|
|
22
|
+
return result;
|
|
23
|
+
};
|
|
24
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
25
|
+
__exportStar(require("./actor"), exports);
|
|
26
|
+
__exportStar(require("./agent"), exports);
|
|
27
|
+
__exportStar(require("./auth"), exports);
|
|
28
|
+
__exportStar(require("./certificate"), exports);
|
|
29
|
+
__exportStar(require("./agent/http/transforms"), exports);
|
|
30
|
+
__exportStar(require("./agent/http/types"), exports);
|
|
31
|
+
__exportStar(require("./canisters/asset"), exports);
|
|
32
|
+
__exportStar(require("./canisters/management"), exports);
|
|
33
|
+
__exportStar(require("./request_id"), exports);
|
|
34
|
+
__exportStar(require("./utils/bls"), exports);
|
|
35
|
+
__exportStar(require("./utils/buffer"), exports);
|
|
36
|
+
exports.polling = __importStar(require("./polling"));
|
|
37
|
+
exports.Cbor = __importStar(require("./cbor"));
|
|
38
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAEA,0CAAwB;AACxB,0CAAwB;AACxB,yCAAuB;AACvB,gDAA8B;AAC9B,0DAAwC;AACxC,qDAAmC;AACnC,oDAAkC;AAClC,yDAAuC;AACvC,+CAA6B;AAC7B,8CAA4B;AAC5B,iDAA+B;AAC/B,qDAAqC;AAKrC,+CAA+B"}
|
|
@@ -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,81 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
+
}) : (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
o[k2] = m[k];
|
|
8
|
+
}));
|
|
9
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
10
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
11
|
+
}) : function(o, v) {
|
|
12
|
+
o["default"] = v;
|
|
13
|
+
});
|
|
14
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
15
|
+
if (mod && mod.__esModule) return mod;
|
|
16
|
+
var result = {};
|
|
17
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
18
|
+
__setModuleDefault(result, mod);
|
|
19
|
+
return result;
|
|
20
|
+
};
|
|
21
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
22
|
+
exports.pollForResponse = void 0;
|
|
23
|
+
const agent_1 = require("../agent");
|
|
24
|
+
const certificate_1 = require("../certificate");
|
|
25
|
+
const buffer_1 = require("../utils/buffer");
|
|
26
|
+
exports.strategy = __importStar(require("./strategy"));
|
|
27
|
+
var strategy_1 = require("./strategy");
|
|
28
|
+
Object.defineProperty(exports, "defaultStrategy", { enumerable: true, get: function () { return strategy_1.defaultStrategy; } });
|
|
29
|
+
/**
|
|
30
|
+
* Polls the IC to check the status of the given request then
|
|
31
|
+
* returns the response bytes once the request has been processed.
|
|
32
|
+
* @param agent The agent to use to poll read_state.
|
|
33
|
+
* @param canisterId The effective canister ID.
|
|
34
|
+
* @param requestId The Request ID to poll status for.
|
|
35
|
+
* @param strategy A polling strategy.
|
|
36
|
+
*/
|
|
37
|
+
async function pollForResponse(agent, canisterId, requestId, strategy) {
|
|
38
|
+
const path = [new TextEncoder().encode('request_status'), requestId];
|
|
39
|
+
const state = await agent.readState(canisterId, { paths: [path] });
|
|
40
|
+
const cert = new certificate_1.Certificate(state, agent);
|
|
41
|
+
const verified = await cert.verify();
|
|
42
|
+
if (!verified) {
|
|
43
|
+
throw new Error('Fail to verify certificate');
|
|
44
|
+
}
|
|
45
|
+
const maybeBuf = cert.lookup([...path, new TextEncoder().encode('status').buffer]);
|
|
46
|
+
let status;
|
|
47
|
+
if (typeof maybeBuf === 'undefined') {
|
|
48
|
+
// Missing requestId means we need to wait
|
|
49
|
+
status = agent_1.RequestStatusResponseStatus.Unknown;
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
status = new TextDecoder().decode(new Uint8Array(maybeBuf));
|
|
53
|
+
}
|
|
54
|
+
switch (status) {
|
|
55
|
+
case agent_1.RequestStatusResponseStatus.Replied: {
|
|
56
|
+
return cert.lookup([...path, 'reply']);
|
|
57
|
+
}
|
|
58
|
+
case agent_1.RequestStatusResponseStatus.Received:
|
|
59
|
+
case agent_1.RequestStatusResponseStatus.Unknown:
|
|
60
|
+
case agent_1.RequestStatusResponseStatus.Processing:
|
|
61
|
+
// Execute the polling strategy, then retry.
|
|
62
|
+
await strategy(canisterId, requestId, status);
|
|
63
|
+
return pollForResponse(agent, canisterId, requestId, strategy);
|
|
64
|
+
case agent_1.RequestStatusResponseStatus.Rejected: {
|
|
65
|
+
const rejectCode = new Uint8Array(cert.lookup([...path, 'reject_code']))[0];
|
|
66
|
+
const rejectMessage = new TextDecoder().decode(new Uint8Array(cert.lookup([...path, 'reject_message'])));
|
|
67
|
+
throw new Error(`Call was rejected:\n` +
|
|
68
|
+
` Request ID: ${buffer_1.toHex(requestId)}\n` +
|
|
69
|
+
` Reject code: ${rejectCode}\n` +
|
|
70
|
+
` Reject text: ${rejectMessage}\n`);
|
|
71
|
+
}
|
|
72
|
+
case agent_1.RequestStatusResponseStatus.Done:
|
|
73
|
+
// This is _technically_ not an error, but we still didn't see the `Replied` status so
|
|
74
|
+
// we don't know the result and cannot decode it.
|
|
75
|
+
throw new Error(`Call was marked as done but we never saw the reply:\n` +
|
|
76
|
+
` Request ID: ${buffer_1.toHex(requestId)}\n`);
|
|
77
|
+
}
|
|
78
|
+
throw new Error('unreachable');
|
|
79
|
+
}
|
|
80
|
+
exports.pollForResponse = pollForResponse;
|
|
81
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/polling/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;AACA,oCAA8D;AAC9D,gDAA6C;AAE7C,4CAAwC;AAExC,uDAAuC;AACvC,uCAA6C;AAApC,2GAAA,eAAe,OAAA;AAQxB;;;;;;;GAOG;AACI,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,yBAAW,CAAC,KAAK,EA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