@astrox/agent 0.0.24 → 0.0.27
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,154 @@
|
|
1
|
+
import { Principal } from '@astrox/principal';
|
2
|
+
export var ProxyMessageKind;
|
3
|
+
(function (ProxyMessageKind) {
|
4
|
+
ProxyMessageKind["Error"] = "err";
|
5
|
+
ProxyMessageKind["GetPrincipal"] = "gp";
|
6
|
+
ProxyMessageKind["GetPrincipalResponse"] = "gpr";
|
7
|
+
ProxyMessageKind["Query"] = "q";
|
8
|
+
ProxyMessageKind["QueryResponse"] = "qr";
|
9
|
+
ProxyMessageKind["Call"] = "c";
|
10
|
+
ProxyMessageKind["CallResponse"] = "cr";
|
11
|
+
ProxyMessageKind["ReadState"] = "rs";
|
12
|
+
ProxyMessageKind["ReadStateResponse"] = "rsr";
|
13
|
+
ProxyMessageKind["Status"] = "s";
|
14
|
+
ProxyMessageKind["StatusResponse"] = "sr";
|
15
|
+
})(ProxyMessageKind || (ProxyMessageKind = {}));
|
16
|
+
// A Stub Agent that forwards calls to another Agent implementation.
|
17
|
+
export class ProxyStubAgent {
|
18
|
+
constructor(_frontend, _agent) {
|
19
|
+
this._frontend = _frontend;
|
20
|
+
this._agent = _agent;
|
21
|
+
}
|
22
|
+
onmessage(msg) {
|
23
|
+
switch (msg.type) {
|
24
|
+
case ProxyMessageKind.GetPrincipal:
|
25
|
+
this._agent.getPrincipal().then(response => {
|
26
|
+
this._frontend({
|
27
|
+
id: msg.id,
|
28
|
+
type: ProxyMessageKind.GetPrincipalResponse,
|
29
|
+
response: response.toText(),
|
30
|
+
});
|
31
|
+
});
|
32
|
+
break;
|
33
|
+
case ProxyMessageKind.Query:
|
34
|
+
this._agent.query(...msg.args).then(response => {
|
35
|
+
this._frontend({
|
36
|
+
id: msg.id,
|
37
|
+
type: ProxyMessageKind.QueryResponse,
|
38
|
+
response,
|
39
|
+
});
|
40
|
+
});
|
41
|
+
break;
|
42
|
+
case ProxyMessageKind.Call:
|
43
|
+
this._agent.call(...msg.args).then(response => {
|
44
|
+
this._frontend({
|
45
|
+
id: msg.id,
|
46
|
+
type: ProxyMessageKind.CallResponse,
|
47
|
+
response,
|
48
|
+
});
|
49
|
+
});
|
50
|
+
break;
|
51
|
+
case ProxyMessageKind.ReadState:
|
52
|
+
this._agent.readState(...msg.args).then(response => {
|
53
|
+
this._frontend({
|
54
|
+
id: msg.id,
|
55
|
+
type: ProxyMessageKind.ReadStateResponse,
|
56
|
+
response,
|
57
|
+
});
|
58
|
+
});
|
59
|
+
break;
|
60
|
+
case ProxyMessageKind.Status:
|
61
|
+
this._agent.status().then(response => {
|
62
|
+
this._frontend({
|
63
|
+
id: msg.id,
|
64
|
+
type: ProxyMessageKind.StatusResponse,
|
65
|
+
response,
|
66
|
+
});
|
67
|
+
});
|
68
|
+
break;
|
69
|
+
default:
|
70
|
+
throw new Error(`Invalid message received: ${JSON.stringify(msg)}`);
|
71
|
+
}
|
72
|
+
}
|
73
|
+
}
|
74
|
+
// An Agent that forwards calls to a backend. The calls are serialized
|
75
|
+
export class ProxyAgent {
|
76
|
+
constructor(_backend) {
|
77
|
+
this._backend = _backend;
|
78
|
+
this._nextId = 0;
|
79
|
+
this._pendingCalls = new Map();
|
80
|
+
this.rootKey = null;
|
81
|
+
}
|
82
|
+
onmessage(msg) {
|
83
|
+
const id = msg.id;
|
84
|
+
const maybePromise = this._pendingCalls.get(id);
|
85
|
+
if (!maybePromise) {
|
86
|
+
throw new Error('A proxy get the same message twice...');
|
87
|
+
}
|
88
|
+
this._pendingCalls.delete(id);
|
89
|
+
const [resolve, reject] = maybePromise;
|
90
|
+
switch (msg.type) {
|
91
|
+
case ProxyMessageKind.Error:
|
92
|
+
return reject(msg.error);
|
93
|
+
case ProxyMessageKind.GetPrincipalResponse:
|
94
|
+
case ProxyMessageKind.CallResponse:
|
95
|
+
case ProxyMessageKind.QueryResponse:
|
96
|
+
case ProxyMessageKind.ReadStateResponse:
|
97
|
+
case ProxyMessageKind.StatusResponse:
|
98
|
+
return resolve(msg.response);
|
99
|
+
default:
|
100
|
+
throw new Error(`Invalid message being sent to ProxyAgent: ${JSON.stringify(msg)}`);
|
101
|
+
}
|
102
|
+
}
|
103
|
+
async getPrincipal() {
|
104
|
+
return this._sendAndWait({
|
105
|
+
id: this._nextId++,
|
106
|
+
type: ProxyMessageKind.GetPrincipal,
|
107
|
+
}).then(principal => {
|
108
|
+
if (typeof principal !== 'string') {
|
109
|
+
throw new Error('Invalid principal received.');
|
110
|
+
}
|
111
|
+
return Principal.fromText(principal);
|
112
|
+
});
|
113
|
+
}
|
114
|
+
readState(canisterId, fields) {
|
115
|
+
return this._sendAndWait({
|
116
|
+
id: this._nextId++,
|
117
|
+
type: ProxyMessageKind.ReadState,
|
118
|
+
args: [canisterId.toString(), fields],
|
119
|
+
});
|
120
|
+
}
|
121
|
+
call(canisterId, fields) {
|
122
|
+
return this._sendAndWait({
|
123
|
+
id: this._nextId++,
|
124
|
+
type: ProxyMessageKind.Call,
|
125
|
+
args: [canisterId.toString(), fields],
|
126
|
+
});
|
127
|
+
}
|
128
|
+
status() {
|
129
|
+
return this._sendAndWait({
|
130
|
+
id: this._nextId++,
|
131
|
+
type: ProxyMessageKind.Status,
|
132
|
+
});
|
133
|
+
}
|
134
|
+
query(canisterId, fields) {
|
135
|
+
return this._sendAndWait({
|
136
|
+
id: this._nextId++,
|
137
|
+
type: ProxyMessageKind.Query,
|
138
|
+
args: [canisterId.toString(), fields],
|
139
|
+
});
|
140
|
+
}
|
141
|
+
async _sendAndWait(msg) {
|
142
|
+
return new Promise((resolve, reject) => {
|
143
|
+
this._pendingCalls.set(msg.id, [resolve, reject]);
|
144
|
+
this._backend(msg);
|
145
|
+
});
|
146
|
+
}
|
147
|
+
async fetchRootKey() {
|
148
|
+
// Hex-encoded version of the replica root key
|
149
|
+
const rootKey = (await this.status()).root_key;
|
150
|
+
this.rootKey = rootKey;
|
151
|
+
return rootKey;
|
152
|
+
}
|
153
|
+
}
|
154
|
+
//# sourceMappingURL=proxy.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"proxy.js","sourceRoot":"","sources":["../../../src/agent/proxy.ts"],"names":[],"mappings":"AAUA,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AAE9C,MAAM,CAAN,IAAY,gBAYX;AAZD,WAAY,gBAAgB;IAC1B,iCAAa,CAAA;IACb,uCAAmB,CAAA;IACnB,gDAA4B,CAAA;IAC5B,+BAAW,CAAA;IACX,wCAAoB,CAAA;IACpB,8BAAU,CAAA;IACV,uCAAmB,CAAA;IACnB,oCAAgB,CAAA;IAChB,6CAAyB,CAAA;IACzB,gCAAY,CAAA;IACZ,yCAAqB,CAAA;AACvB,CAAC,EAZW,gBAAgB,KAAhB,gBAAgB,QAY3B;AAyED,oEAAoE;AACpE,MAAM,OAAO,cAAc;IACzB,YAAoB,SAAsC,EAAU,MAAa;QAA7D,cAAS,GAAT,SAAS,CAA6B;QAAU,WAAM,GAAN,MAAM,CAAO;IAAG,CAAC;IAE9E,SAAS,CAAC,GAAiB;QAChC,QAAQ,GAAG,CAAC,IAAI,EAAE;YAChB,KAAK,gBAAgB,CAAC,YAAY;gBAChC,IAAI,CAAC,MAAM,CAAC,YAAY,EAAE,CAAC,IAAI,CAAC,QAAQ,CAAC,EAAE;oBACzC,IAAI,CAAC,SAAS,CAAC;wBACb,EAAE,EAAE,GAAG,CAAC,EAAE;wBACV,IAAI,EAAE,gBAAgB,CAAC,oBAAoB;wBAC3C,QAAQ,EAAE,QAAQ,CAAC,MAAM,EAAE;qBAC5B,CAAC,CAAC;gBACL,CAAC,CAAC,CAAC;gBACH,MAAM;YACR,KAAK,gBAAgB,CAAC,KAAK;gBACzB,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,GAAG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AAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;YACrC,IAAI,CAAC,aAAa,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,EAAE,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC,CAAC;YAElD,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC;QACrB,CAAC,CAAC,CAAC;IACL,CAAC;IAEM,KAAK,CAAC,YAAY;QACvB,8CAA8C;QAC9C,MAAM,OAAO,GAAI,CAAC,MAAM,IAAI,CAAC,MAAM,EAAE,CAAS,CAAC,QAAQ,CAAC;QACxD,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,OAAO,OAAO,CAAC;IACjB,CAAC;CACF"}
|
@@ -0,0 +1,87 @@
|
|
1
|
+
import { Principal } from '@astrox/principal';
|
2
|
+
import { HttpAgentRequest } from './agent/http/types';
|
3
|
+
/**
|
4
|
+
* A Key Pair, containing a secret and public key.
|
5
|
+
*/
|
6
|
+
export interface KeyPair {
|
7
|
+
secretKey: ArrayBuffer;
|
8
|
+
publicKey: PublicKey;
|
9
|
+
}
|
10
|
+
/**
|
11
|
+
* A public key that is DER encoded. This is a branded ArrayBuffer.
|
12
|
+
*/
|
13
|
+
export declare type DerEncodedPublicKey = ArrayBuffer & {
|
14
|
+
__derEncodedPublicKey__: void;
|
15
|
+
};
|
16
|
+
/**
|
17
|
+
* A signature array buffer.
|
18
|
+
*/
|
19
|
+
export declare type Signature = ArrayBuffer & {
|
20
|
+
__signature__: void;
|
21
|
+
};
|
22
|
+
/**
|
23
|
+
* A Public Key implementation.
|
24
|
+
*/
|
25
|
+
export interface PublicKey {
|
26
|
+
toDer(): DerEncodedPublicKey;
|
27
|
+
}
|
28
|
+
/**
|
29
|
+
* A General Identity object. This does not have to be a private key (for example,
|
30
|
+
* the Anonymous identity), but it must be able to transform request.
|
31
|
+
*/
|
32
|
+
export interface Identity {
|
33
|
+
/**
|
34
|
+
* Get the principal represented by this identity. Normally should be a
|
35
|
+
* `Principal.selfAuthenticating()`.
|
36
|
+
*/
|
37
|
+
getPrincipal(): Principal;
|
38
|
+
/**
|
39
|
+
* Transform a request into a signed version of the request. This is done last
|
40
|
+
* after the transforms on the body of a request. The returned object can be
|
41
|
+
* anything, but must be serializable to CBOR.
|
42
|
+
*/
|
43
|
+
transformRequest(request: HttpAgentRequest): Promise<unknown>;
|
44
|
+
}
|
45
|
+
/**
|
46
|
+
* An Identity that can sign blobs.
|
47
|
+
*/
|
48
|
+
export declare abstract class SignIdentity implements Identity {
|
49
|
+
protected _principal: Principal | undefined;
|
50
|
+
/**
|
51
|
+
* Returns the public key that would match this identity's signature.
|
52
|
+
*/
|
53
|
+
abstract getPublicKey(): PublicKey;
|
54
|
+
/**
|
55
|
+
* Signs a blob of data, with this identity's private key.
|
56
|
+
*/
|
57
|
+
abstract sign(blob: ArrayBuffer): Promise<Signature>;
|
58
|
+
/**
|
59
|
+
* Get the principal represented by this identity. Normally should be a
|
60
|
+
* `Principal.selfAuthenticating()`.
|
61
|
+
*/
|
62
|
+
getPrincipal(): Principal;
|
63
|
+
/**
|
64
|
+
* Transform a request into a signed version of the request. This is done last
|
65
|
+
* after the transforms on the body of a request. The returned object can be
|
66
|
+
* anything, but must be serializable to CBOR.
|
67
|
+
* @param request - internet computer request to transform
|
68
|
+
*/
|
69
|
+
transformRequest(request: HttpAgentRequest): Promise<unknown>;
|
70
|
+
}
|
71
|
+
export declare class AnonymousIdentity implements Identity {
|
72
|
+
getPrincipal(): Principal;
|
73
|
+
transformRequest(request: HttpAgentRequest): Promise<unknown>;
|
74
|
+
}
|
75
|
+
export interface AnonymousIdentityDescriptor {
|
76
|
+
type: 'AnonymousIdentity';
|
77
|
+
}
|
78
|
+
export interface PublicKeyIdentityDescriptor {
|
79
|
+
type: 'PublicKeyIdentity';
|
80
|
+
publicKey: string;
|
81
|
+
}
|
82
|
+
export declare type IdentityDescriptor = AnonymousIdentityDescriptor | PublicKeyIdentityDescriptor;
|
83
|
+
/**
|
84
|
+
* Create an IdentityDescriptor from a @astrox/authentication Identity
|
85
|
+
* @param identity - identity describe in returned descriptor
|
86
|
+
*/
|
87
|
+
export declare function createIdentityDescriptor(identity: SignIdentity | AnonymousIdentity): IdentityDescriptor;
|
package/lib/esm/auth.js
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
2
|
+
var t = {};
|
3
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
4
|
+
t[p] = s[p];
|
5
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
6
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
7
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
8
|
+
t[p[i]] = s[p[i]];
|
9
|
+
}
|
10
|
+
return t;
|
11
|
+
};
|
12
|
+
import { Principal } from '@astrox/principal';
|
13
|
+
import { requestIdOf } from './request_id';
|
14
|
+
import { concat, toHex } from './utils/buffer';
|
15
|
+
const domainSeparator = new TextEncoder().encode('\x0Aic-request');
|
16
|
+
/**
|
17
|
+
* An Identity that can sign blobs.
|
18
|
+
*/
|
19
|
+
export class SignIdentity {
|
20
|
+
/**
|
21
|
+
* Get the principal represented by this identity. Normally should be a
|
22
|
+
* `Principal.selfAuthenticating()`.
|
23
|
+
*/
|
24
|
+
getPrincipal() {
|
25
|
+
if (!this._principal) {
|
26
|
+
this._principal = Principal.selfAuthenticating(new Uint8Array(this.getPublicKey().toDer()));
|
27
|
+
}
|
28
|
+
return this._principal;
|
29
|
+
}
|
30
|
+
/**
|
31
|
+
* Transform a request into a signed version of the request. This is done last
|
32
|
+
* after the transforms on the body of a request. The returned object can be
|
33
|
+
* anything, but must be serializable to CBOR.
|
34
|
+
* @param request - internet computer request to transform
|
35
|
+
*/
|
36
|
+
async transformRequest(request) {
|
37
|
+
const { body } = request, fields = __rest(request, ["body"]);
|
38
|
+
const requestId = await requestIdOf(body);
|
39
|
+
return Object.assign(Object.assign({}, fields), { body: {
|
40
|
+
content: body,
|
41
|
+
sender_pubkey: this.getPublicKey().toDer(),
|
42
|
+
sender_sig: await this.sign(concat(domainSeparator, requestId)),
|
43
|
+
} });
|
44
|
+
}
|
45
|
+
}
|
46
|
+
export class AnonymousIdentity {
|
47
|
+
getPrincipal() {
|
48
|
+
return Principal.anonymous();
|
49
|
+
}
|
50
|
+
async transformRequest(request) {
|
51
|
+
return Object.assign(Object.assign({}, request), { body: { content: request.body } });
|
52
|
+
}
|
53
|
+
}
|
54
|
+
/**
|
55
|
+
* Create an IdentityDescriptor from a @astrox/authentication Identity
|
56
|
+
* @param identity - identity describe in returned descriptor
|
57
|
+
*/
|
58
|
+
export function createIdentityDescriptor(identity) {
|
59
|
+
const identityIndicator = 'getPublicKey' in identity
|
60
|
+
? { type: 'PublicKeyIdentity', publicKey: toHex(identity.getPublicKey().toDer()) }
|
61
|
+
: { type: 'AnonymousIdentity' };
|
62
|
+
return identityIndicator;
|
63
|
+
}
|
64
|
+
//# sourceMappingURL=auth.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"auth.js","sourceRoot":"","sources":["../../src/auth.ts"],"names":[],"mappings":";;;;;;;;;;;AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AAE9C,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAC3C,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAC;AAE/C,MAAM,eAAe,GAAG,IAAI,WAAW,EAAE,CAAC,MAAM,CAAC,gBAAgB,CAAC,CAAC;AA+CnE;;GAEG;AACH,MAAM,OAAgB,YAAY;IAahC;;;OAGG;IACI,YAAY;QACjB,IAAI,CAAC,IAAI,CAAC,UAAU,EAAE;YACpB,IAAI,CAAC,UAAU,GAAG,SAAS,CAAC,kBAAkB,CAAC,IAAI,UAAU,CAAC,IAAI,CAAC,YAAY,EAAE,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC;SAC7F;QACD,OAAO,IAAI,CAAC,UAAU,CAAC;IACzB,CAAC;IAED;;;;;OAKG;IACI,KAAK,CAAC,gBAAgB,CAAC,OAAyB;QACrD,MAAM,EAAE,IAAI,KAAgB,OAAO,EAAlB,MAAM,UAAK,OAAO,EAA7B,QAAmB,CAAU,CAAC;QACpC,MAAM,SAAS,GAAG,MAAM,WAAW,CAAC,IAAI,CAAC,CAAC;QAC1C,uCACK,MAAM,KACT,IAAI,EAAE;gBACJ,OAAO,EAAE,IAAI;gBACb,aAAa,EAAE,IAAI,CAAC,YAAY,EAAE,CAAC,KAAK,EAAE;gBAC1C,UAAU,EAAE,MAAM,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,eAAe,EAAE,SAAS,CAAC,CAAC;aAChE,IACD;IACJ,CAAC;CACF;AAED,MAAM,OAAO,iBAAiB;IACrB,YAAY;QACjB,OAAO,SAAS,CAAC,SAAS,EAAE,CAAC;IAC/B,CAAC;IAEM,KAAK,CAAC,gBAAgB,CAAC,OAAyB;QACrD,uCACK,OAAO,KACV,IAAI,EAAE,EAAE,OAAO,EAAE,OAAO,CAAC,IAAI,EAAE,IAC/B;IACJ,CAAC;CACF;AAuBD;;;GAGG;AACH,MAAM,UAAU,wBAAwB,CACtC,QAA0C;IAE1C,MAAM,iBAAiB,GACrB,cAAc,IAAI,QAAQ;QACxB,CAAC,CAAC,EAAE,IAAI,EAAE,mBAAmB,EAAE,SAAS,EAAE,KAAK,CAAC,QAAQ,CAAC,YAAY,EAAE,CAAC,KAAK,EAAE,CAAC,EAAE;QAClF,CAAC,CAAC,EAAE,IAAI,EAAE,mBAAmB,EAAE,CAAC;IACpC,OAAO,iBAAiB,CAAC;AAC3B,CAAC"}
|
@@ -0,0 +1,10 @@
|
|
1
|
+
import { ActorConfig, ActorSubclass } from '../actor';
|
2
|
+
export interface AssetCanisterRecord {
|
3
|
+
store(path: string, content: number[]): Promise<void>;
|
4
|
+
retrieve(path: string): Promise<number[]>;
|
5
|
+
}
|
6
|
+
/**
|
7
|
+
* Create a management canister actor.
|
8
|
+
* @param config
|
9
|
+
*/
|
10
|
+
export declare function createAssetCanisterActor(config: ActorConfig): ActorSubclass<AssetCanisterRecord>;
|
@@ -0,0 +1,11 @@
|
|
1
|
+
import { Actor } from '../actor';
|
2
|
+
import assetCanister from './asset_idl';
|
3
|
+
/* tslint:enable */
|
4
|
+
/**
|
5
|
+
* Create a management canister actor.
|
6
|
+
* @param config
|
7
|
+
*/
|
8
|
+
export function createAssetCanisterActor(config) {
|
9
|
+
return Actor.createActor(assetCanister, config);
|
10
|
+
}
|
11
|
+
//# sourceMappingURL=asset.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"asset.js","sourceRoot":"","sources":["../../../src/canisters/asset.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAA0C,MAAM,UAAU,CAAC;AACzE,OAAO,aAAa,MAAM,aAAa,CAAC;AAOxC,mBAAmB;AAEnB;;;GAGG;AACH,MAAM,UAAU,wBAAwB,CAAC,MAAmB;IAC1D,OAAO,KAAK,CAAC,WAAW,CAAsB,aAAa,EAAE,MAAM,CAAC,CAAC;AACvE,CAAC"}
|
@@ -0,0 +1,12 @@
|
|
1
|
+
/**
|
2
|
+
* This file is generated from the candid for asset management.
|
3
|
+
*/
|
4
|
+
/* tslint:disable */
|
5
|
+
// @ts-ignore
|
6
|
+
export default ({ IDL }) => {
|
7
|
+
return IDL.Service({
|
8
|
+
retrieve: IDL.Func([IDL.Text], [IDL.Vec(IDL.Nat8)], ['query']),
|
9
|
+
store: IDL.Func([IDL.Text, IDL.Vec(IDL.Nat8)], [], []),
|
10
|
+
});
|
11
|
+
};
|
12
|
+
//# sourceMappingURL=asset_idl.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"asset_idl.js","sourceRoot":"","sources":["../../../src/canisters/asset_idl.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,oBAAoB;AACpB,aAAa;AACb,eAAe,CAAC,EAAE,GAAG,EAAE,EAAE,EAAE;IACzB,OAAO,GAAG,CAAC,OAAO,CAAC;QACjB,QAAQ,EAAE,GAAG,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC,OAAO,CAAC,CAAC;QAC9D,KAAK,EAAE,GAAG,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,IAAI,EAAE,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,EAAE,EAAE,EAAE,EAAE,CAAC;KACvD,CAAC,CAAC;AACL,CAAC,CAAC"}
|
@@ -0,0 +1,37 @@
|
|
1
|
+
import { ActorMethod, ActorSubclass, CallConfig } from '../actor';
|
2
|
+
import { Principal } from '@astrox/principal';
|
3
|
+
export interface CanisterSettings {
|
4
|
+
controller: [] | [Principal];
|
5
|
+
compute_allocation: [] | [bigint];
|
6
|
+
memory_allocation: [] | [bigint];
|
7
|
+
freezing_threshold: [] | [bigint];
|
8
|
+
}
|
9
|
+
export interface ManagementCanisterRecord {
|
10
|
+
provisional_create_canister_with_cycles: ActorMethod<[
|
11
|
+
{
|
12
|
+
amount: [] | [number];
|
13
|
+
settings: [] | [CanisterSettings];
|
14
|
+
}
|
15
|
+
], {
|
16
|
+
canister_id: Principal;
|
17
|
+
}>;
|
18
|
+
install_code: ActorMethod<[
|
19
|
+
{
|
20
|
+
mode: {
|
21
|
+
install: null;
|
22
|
+
} | {
|
23
|
+
reinstall: null;
|
24
|
+
} | {
|
25
|
+
upgrade: null;
|
26
|
+
};
|
27
|
+
canister_id: Principal;
|
28
|
+
wasm_module: number[];
|
29
|
+
arg: number[];
|
30
|
+
}
|
31
|
+
], void>;
|
32
|
+
}
|
33
|
+
/**
|
34
|
+
* Create a management canister actor.
|
35
|
+
* @param config
|
36
|
+
*/
|
37
|
+
export declare function getManagementCanister(config: CallConfig): ActorSubclass<ManagementCanisterRecord>;
|
@@ -0,0 +1,23 @@
|
|
1
|
+
import { Actor } from '../actor';
|
2
|
+
import { Principal } from '@astrox/principal';
|
3
|
+
import managementCanisterIdl from './management_idl';
|
4
|
+
/* tslint:enable */
|
5
|
+
/**
|
6
|
+
* Create a management canister actor.
|
7
|
+
* @param config
|
8
|
+
*/
|
9
|
+
export function getManagementCanister(config) {
|
10
|
+
function transform(methodName, args, callConfig) {
|
11
|
+
const first = args[0];
|
12
|
+
let effectiveCanisterId = Principal.fromHex('');
|
13
|
+
if (first && typeof first === 'object' && first.canister_id) {
|
14
|
+
effectiveCanisterId = Principal.from(first.canister_id);
|
15
|
+
}
|
16
|
+
return { effectiveCanisterId };
|
17
|
+
}
|
18
|
+
return Actor.createActor(managementCanisterIdl, Object.assign(Object.assign(Object.assign({}, config), { canisterId: Principal.fromHex('') }), {
|
19
|
+
callTransform: transform,
|
20
|
+
queryTransform: transform,
|
21
|
+
}));
|
22
|
+
}
|
23
|
+
//# sourceMappingURL=management.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"management.js","sourceRoot":"","sources":["../../../src/canisters/management.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAA0C,MAAM,UAAU,CAAC;AACzE,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AAC9C,OAAO,qBAAqB,MAAM,kBAAkB,CAAC;AAgCrD,mBAAmB;AAEnB;;;GAGG;AACH,MAAM,UAAU,qBAAqB,CAAC,MAAkB;IACtD,SAAS,SAAS,CAAC,UAAkB,EAAE,IAAe,EAAE,UAAsB;QAC5E,MAAM,KAAK,GAAG,IAAI,CAAC,CAAC,CAAQ,CAAC;QAC7B,IAAI,mBAAmB,GAAG,SAAS,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC;QAChD,IAAI,KAAK,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,CAAC,WAAW,EAAE;YAC3D,mBAAmB,GAAG,SAAS,CAAC,IAAI,CAAC,KAAK,CAAC,WAAsB,CAAC,CAAC;SACpE;QACD,OAAO,EAAE,mBAAmB,EAAE,CAAC;IACjC,CAAC;IAED,OAAO,KAAK,CAAC,WAAW,CAA2B,qBAAqB,gDACnE,MAAM,KACT,UAAU,EAAE,SAAS,CAAC,OAAO,CAAC,EAAE,CAAC,KAC9B;QACD,aAAa,EAAE,SAAS;QACxB,cAAc,EAAE,SAAS;KAC1B,EACD,CAAC;AACL,CAAC"}
|
@@ -0,0 +1,27 @@
|
|
1
|
+
/**
|
2
|
+
* This file is generated from the candid for asset management.
|
3
|
+
*/
|
4
|
+
/* tslint:disable */
|
5
|
+
// @ts-ignore
|
6
|
+
export default ({ IDL }) => {
|
7
|
+
const canister_id = IDL.Principal;
|
8
|
+
const wasm_module = IDL.Vec(IDL.Nat8);
|
9
|
+
const CanisterSettings = IDL.Record({
|
10
|
+
compute_allocation: IDL.Opt(IDL.Nat),
|
11
|
+
memory_allocation: IDL.Opt(IDL.Nat),
|
12
|
+
});
|
13
|
+
return IDL.Service({
|
14
|
+
provisional_create_canister_with_cycles: IDL.Func([IDL.Record({ amount: IDL.Opt(IDL.Nat), settings: IDL.Opt(CanisterSettings) })], [IDL.Record({ canister_id: canister_id })], []),
|
15
|
+
create_canister: IDL.Func([], [IDL.Record({ canister_id: canister_id })], []),
|
16
|
+
install_code: IDL.Func([
|
17
|
+
IDL.Record({
|
18
|
+
mode: IDL.Variant({ install: IDL.Null, reinstall: IDL.Null, upgrade: IDL.Null }),
|
19
|
+
canister_id: canister_id,
|
20
|
+
wasm_module: wasm_module,
|
21
|
+
arg: IDL.Vec(IDL.Nat8),
|
22
|
+
}),
|
23
|
+
], [], []),
|
24
|
+
set_controller: IDL.Func([IDL.Record({ canister_id: canister_id, new_controller: IDL.Principal })], [], []),
|
25
|
+
});
|
26
|
+
};
|
27
|
+
//# sourceMappingURL=management_idl.js.map
|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I,EAAE,CAAC;gBAChF,WAAW,EAAE,WAAW;gBACxB,WAAW,EAAE,WAAW;gBACxB,GAAG,EAAE,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC;aACvB,CAAC;SACH,EACD,EAAE,EACF,EAAE,CACH;QACD,cAAc,EAAE,GAAG,CAAC,IAAI,CACtB,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE,WAAW,EAAE,WAAW,EAAE,cAAc,EAAE,GAAG,CAAC,SAAS,EAAE,CAAC,CAAC,EACzE,EAAE,EACF,EAAE,CACH;KACF,CAAC,CAAC;AACL,CAAC,CAAC"}
|
package/lib/esm/cbor.js
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
// tslint:disable:max-classes-per-file
|
2
|
+
// This file is based on:
|
3
|
+
// tslint:disable-next-line: max-line-length
|
4
|
+
// https://github.com/dfinity-lab/dfinity/blob/9bca65f8edd65701ea6bdb00e0752f9186bbc893/docs/spec/public/index.adoc#cbor-encoding-of-requests-and-responses
|
5
|
+
import borc from 'borc';
|
6
|
+
import * as cbor from 'simple-cbor';
|
7
|
+
import { SelfDescribeCborSerializer } from 'simple-cbor';
|
8
|
+
import { concat, fromHex } from './utils/buffer';
|
9
|
+
// We are using hansl/simple-cbor for CBOR serialization, to avoid issues with
|
10
|
+
// encoding the uint64 values that the HTTP handler of the client expects for
|
11
|
+
// canister IDs. However, simple-cbor does not yet provide deserialization so
|
12
|
+
// we are using `Uint8Array` so that we can use the dignifiedquire/borc CBOR
|
13
|
+
// decoder.
|
14
|
+
class PrincipalEncoder {
|
15
|
+
get name() {
|
16
|
+
return 'Principal';
|
17
|
+
}
|
18
|
+
get priority() {
|
19
|
+
return 0;
|
20
|
+
}
|
21
|
+
match(value) {
|
22
|
+
return value && value._isPrincipal === true;
|
23
|
+
}
|
24
|
+
encode(v) {
|
25
|
+
return cbor.value.bytes(v.toUint8Array());
|
26
|
+
}
|
27
|
+
}
|
28
|
+
class BufferEncoder {
|
29
|
+
get name() {
|
30
|
+
return 'Buffer';
|
31
|
+
}
|
32
|
+
get priority() {
|
33
|
+
return 1;
|
34
|
+
}
|
35
|
+
match(value) {
|
36
|
+
return value instanceof ArrayBuffer || ArrayBuffer.isView(value);
|
37
|
+
}
|
38
|
+
encode(v) {
|
39
|
+
return cbor.value.bytes(new Uint8Array(v));
|
40
|
+
}
|
41
|
+
}
|
42
|
+
class BigIntEncoder {
|
43
|
+
get name() {
|
44
|
+
return 'BigInt';
|
45
|
+
}
|
46
|
+
get priority() {
|
47
|
+
return 1;
|
48
|
+
}
|
49
|
+
match(value) {
|
50
|
+
return typeof value === `bigint`;
|
51
|
+
}
|
52
|
+
encode(v) {
|
53
|
+
// Always use a bigint encoding.
|
54
|
+
if (v > BigInt(0)) {
|
55
|
+
return cbor.value.tagged(2, cbor.value.bytes(fromHex(v.toString(16))));
|
56
|
+
}
|
57
|
+
else {
|
58
|
+
return cbor.value.tagged(3, cbor.value.bytes(fromHex((BigInt('-1') * v).toString(16))));
|
59
|
+
}
|
60
|
+
}
|
61
|
+
}
|
62
|
+
const serializer = SelfDescribeCborSerializer.withDefaultEncoders(true);
|
63
|
+
serializer.addEncoder(new PrincipalEncoder());
|
64
|
+
serializer.addEncoder(new BufferEncoder());
|
65
|
+
serializer.addEncoder(new BigIntEncoder());
|
66
|
+
export var CborTag;
|
67
|
+
(function (CborTag) {
|
68
|
+
CborTag[CborTag["Uint64LittleEndian"] = 71] = "Uint64LittleEndian";
|
69
|
+
CborTag[CborTag["Semantic"] = 55799] = "Semantic";
|
70
|
+
})(CborTag || (CborTag = {}));
|
71
|
+
/**
|
72
|
+
* Encode a JavaScript value into CBOR.
|
73
|
+
*/
|
74
|
+
export function encode(value) {
|
75
|
+
return serializer.serialize(value);
|
76
|
+
}
|
77
|
+
function decodePositiveBigInt(buf) {
|
78
|
+
const len = buf.byteLength;
|
79
|
+
let res = BigInt(0);
|
80
|
+
for (let i = 0; i < len; i++) {
|
81
|
+
// tslint:disable-next-line:no-bitwise
|
82
|
+
res = res * BigInt(0x100) + BigInt(buf[i]);
|
83
|
+
}
|
84
|
+
return res;
|
85
|
+
}
|
86
|
+
// A BORC subclass that decodes byte strings to ArrayBuffer instead of the Buffer class.
|
87
|
+
class Uint8ArrayDecoder extends borc.Decoder {
|
88
|
+
createByteString(raw) {
|
89
|
+
return concat(...raw);
|
90
|
+
}
|
91
|
+
createByteStringFromHeap(start, end) {
|
92
|
+
if (start === end) {
|
93
|
+
return new ArrayBuffer(0);
|
94
|
+
}
|
95
|
+
return new Uint8Array(this._heap.slice(start, end));
|
96
|
+
}
|
97
|
+
}
|
98
|
+
export function decode(input) {
|
99
|
+
const buffer = new Uint8Array(input);
|
100
|
+
const decoder = new Uint8ArrayDecoder({
|
101
|
+
size: buffer.byteLength,
|
102
|
+
tags: {
|
103
|
+
// Override tags 2 and 3 for BigInt support (borc supports only BigNumber).
|
104
|
+
2: val => decodePositiveBigInt(val),
|
105
|
+
3: val => -decodePositiveBigInt(val),
|
106
|
+
[CborTag.Semantic]: (value) => value,
|
107
|
+
},
|
108
|
+
});
|
109
|
+
return decoder.decodeFirst(buffer);
|
110
|
+
}
|
111
|
+
//# 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,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,KAAK,IAAI,MAAM,aAAa,CAAC;AACpC,OAAO,EAAe,0BAA0B,EAAE,MAAM,aAAa,CAAC;AAEtE,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AAEjD,8EAA8E;AAC9E,6EAA6E;AAC7E,6EAA6E;AAC7E,4EAA4E;AAC5E,WAAW;AAEX,MAAM,gBAAgB;IACpB,IAAW,IAAI;QACb,OAAO,WAAW,CAAC;IACrB,CAAC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AAE;YACjB,OAAO,IAAI,WAAW,CAAC,CAAC,CAAC,CAAC;SAC3B;QAED,OAAO,IAAI,UAAU,CAAE,IAAY,CAAC,KAAK,CAAC,KAAK,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC,CAAC;IAC/D,CAAC;CACF;AAED,MAAM,UAAU,MAAM,CAAI,KAAkB;IAC1C,MAAM,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"}
|
@@ -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 {};
|