@icp-sdk/core 5.0.0-beta.0 → 5.0.0-beta.1
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/agent/actor.js +2 -2
- package/lib/cjs/agent/actor.js.map +1 -1
- package/lib/cjs/agent/agent/api.d.ts +6 -6
- package/lib/cjs/agent/agent/api.js +4 -4
- package/lib/cjs/agent/agent/http/__certificates__/goldenCertificates.d.ts +30 -0
- package/lib/cjs/agent/agent/http/__certificates__/goldenCertificates.d.ts.map +1 -1
- package/lib/cjs/agent/agent/http/__certificates__/goldenCertificates.js +33 -1
- package/lib/cjs/agent/agent/http/__certificates__/goldenCertificates.js.map +1 -1
- package/lib/cjs/agent/agent/http/index.d.ts +22 -3
- package/lib/cjs/agent/agent/http/index.d.ts.map +1 -1
- package/lib/cjs/agent/agent/http/index.js +131 -51
- package/lib/cjs/agent/agent/http/index.js.map +1 -1
- package/lib/cjs/agent/canisterStatus/index.d.ts +18 -50
- package/lib/cjs/agent/canisterStatus/index.d.ts.map +1 -1
- package/lib/cjs/agent/canisterStatus/index.js +30 -92
- package/lib/cjs/agent/canisterStatus/index.js.map +1 -1
- package/lib/cjs/agent/certificate.d.ts +68 -10
- package/lib/cjs/agent/certificate.d.ts.map +1 -1
- package/lib/cjs/agent/certificate.js +198 -27
- package/lib/cjs/agent/certificate.js.map +1 -1
- package/lib/cjs/agent/constants.d.ts +4 -0
- package/lib/cjs/agent/constants.d.ts.map +1 -1
- package/lib/cjs/agent/constants.js +5 -1
- package/lib/cjs/agent/constants.js.map +1 -1
- package/lib/cjs/agent/errors.d.ts +7 -1
- package/lib/cjs/agent/errors.d.ts.map +1 -1
- package/lib/cjs/agent/errors.js +19 -7
- package/lib/cjs/agent/errors.js.map +1 -1
- package/lib/cjs/agent/index.d.ts +97 -3
- package/lib/cjs/agent/index.d.ts.map +1 -1
- package/lib/cjs/agent/index.js +92 -9
- package/lib/cjs/agent/index.js.map +1 -1
- package/lib/cjs/agent/polling/index.js +1 -1
- package/lib/cjs/agent/polling/index.js.map +1 -1
- package/lib/cjs/agent/subnetStatus/index.d.ts +70 -0
- package/lib/cjs/agent/subnetStatus/index.d.ts.map +1 -0
- package/lib/cjs/agent/subnetStatus/index.js +224 -0
- package/lib/cjs/agent/subnetStatus/index.js.map +1 -0
- package/lib/cjs/agent/utils/readState.d.ts +84 -0
- package/lib/cjs/agent/utils/readState.d.ts.map +1 -0
- package/lib/cjs/agent/utils/readState.js +149 -0
- package/lib/cjs/agent/utils/readState.js.map +1 -0
- package/lib/cjs/candid/index.d.ts +20 -1
- package/lib/cjs/candid/index.d.ts.map +1 -1
- package/lib/cjs/candid/index.js +20 -1
- package/lib/cjs/candid/index.js.map +1 -1
- package/lib/cjs/identity/index.d.ts +30 -7
- package/lib/cjs/identity/index.d.ts.map +1 -1
- package/lib/cjs/identity/index.js +31 -11
- package/lib/cjs/identity/index.js.map +1 -1
- package/lib/cjs/identity/secp256k1/index.d.ts +48 -1
- package/lib/cjs/identity/secp256k1/index.d.ts.map +1 -1
- package/lib/cjs/identity/secp256k1/index.js +48 -1
- package/lib/cjs/identity/secp256k1/index.js.map +1 -1
- package/lib/cjs/principal/index.d.ts +15 -0
- package/lib/cjs/principal/index.d.ts.map +1 -1
- package/lib/cjs/principal/index.js +15 -0
- package/lib/cjs/principal/index.js.map +1 -1
- package/lib/esm/agent/actor.js +3 -3
- package/lib/esm/agent/actor.js.map +1 -1
- package/lib/esm/agent/agent/api.d.ts +6 -6
- package/lib/esm/agent/agent/api.js +3 -3
- package/lib/esm/agent/agent/http/__certificates__/goldenCertificates.d.ts +30 -0
- package/lib/esm/agent/agent/http/__certificates__/goldenCertificates.d.ts.map +1 -1
- package/lib/esm/agent/agent/http/__certificates__/goldenCertificates.js +33 -1
- package/lib/esm/agent/agent/http/__certificates__/goldenCertificates.js.map +1 -1
- package/lib/esm/agent/agent/http/index.d.ts +22 -3
- package/lib/esm/agent/agent/http/index.d.ts.map +1 -1
- package/lib/esm/agent/agent/http/index.js +134 -54
- package/lib/esm/agent/agent/http/index.js.map +1 -1
- package/lib/esm/agent/canister-env/index.d.ts +2 -1
- package/lib/esm/agent/canister-env/index.d.ts.map +1 -1
- package/lib/esm/agent/canister-env/index.js +2 -1
- package/lib/esm/agent/canister-env/index.js.map +1 -1
- package/lib/esm/agent/canisterStatus/index.d.ts +18 -50
- package/lib/esm/agent/canisterStatus/index.d.ts.map +1 -1
- package/lib/esm/agent/canisterStatus/index.js +31 -93
- package/lib/esm/agent/canisterStatus/index.js.map +1 -1
- package/lib/esm/agent/certificate.d.ts +68 -10
- package/lib/esm/agent/certificate.d.ts.map +1 -1
- package/lib/esm/agent/certificate.js +196 -29
- package/lib/esm/agent/certificate.js.map +1 -1
- package/lib/esm/agent/constants.d.ts +4 -0
- package/lib/esm/agent/constants.d.ts.map +1 -1
- package/lib/esm/agent/constants.js +4 -0
- package/lib/esm/agent/constants.js.map +1 -1
- package/lib/esm/agent/errors.d.ts +7 -1
- package/lib/esm/agent/errors.d.ts.map +1 -1
- package/lib/esm/agent/errors.js +15 -4
- package/lib/esm/agent/errors.js.map +1 -1
- package/lib/esm/agent/index.d.ts +97 -3
- package/lib/esm/agent/index.d.ts.map +1 -1
- package/lib/esm/agent/index.js +97 -3
- package/lib/esm/agent/index.js.map +1 -1
- package/lib/esm/agent/polling/index.js +1 -1
- package/lib/esm/agent/polling/index.js.map +1 -1
- package/lib/esm/agent/subnetStatus/index.d.ts +70 -0
- package/lib/esm/agent/subnetStatus/index.d.ts.map +1 -0
- package/lib/esm/agent/subnetStatus/index.js +183 -0
- package/lib/esm/agent/subnetStatus/index.js.map +1 -0
- package/lib/esm/agent/utils/readState.d.ts +84 -0
- package/lib/esm/agent/utils/readState.d.ts.map +1 -0
- package/lib/esm/agent/utils/readState.js +107 -0
- package/lib/esm/agent/utils/readState.js.map +1 -0
- package/lib/esm/candid/index.d.ts +20 -1
- package/lib/esm/candid/index.d.ts.map +1 -1
- package/lib/esm/candid/index.js +20 -1
- package/lib/esm/candid/index.js.map +1 -1
- package/lib/esm/identity/index.d.ts +30 -7
- package/lib/esm/identity/index.d.ts.map +1 -1
- package/lib/esm/identity/index.js +30 -9
- package/lib/esm/identity/index.js.map +1 -1
- package/lib/esm/identity/secp256k1/index.d.ts +48 -1
- package/lib/esm/identity/secp256k1/index.d.ts.map +1 -1
- package/lib/esm/identity/secp256k1/index.js +48 -1
- package/lib/esm/identity/secp256k1/index.js.map +1 -1
- package/lib/esm/principal/index.d.ts +15 -0
- package/lib/esm/principal/index.d.ts.map +1 -1
- package/lib/esm/principal/index.js +15 -0
- package/lib/esm/principal/index.js.map +1 -1
- package/package.json +1 -1
- package/src/agent/actor.ts +3 -3
- package/src/agent/agent/api.ts +8 -8
- package/src/agent/agent/http/index.ts +171 -58
- package/src/agent/canister-env/index.ts +2 -1
- package/src/agent/canisterStatus/index.ts +46 -191
- package/src/agent/certificate.ts +271 -52
- package/src/agent/constants.ts +5 -0
- package/src/agent/errors.ts +17 -4
- package/src/agent/index.ts +97 -3
- package/src/agent/polling/index.ts +1 -1
- package/src/agent/subnetStatus/index.ts +270 -0
- package/src/agent/utils/readState.ts +180 -0
- package/src/candid/index.ts +20 -1
- package/src/identity/index.ts +30 -10
- package/src/identity/secp256k1/index.ts +48 -1
- package/src/principal/index.ts +16 -0
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
import { Principal } from '#principal';
|
|
2
|
+
import {
|
|
3
|
+
CertificateVerificationErrorCode,
|
|
4
|
+
MissingRootKeyErrorCode,
|
|
5
|
+
ExternalError,
|
|
6
|
+
AgentError,
|
|
7
|
+
UnknownError,
|
|
8
|
+
UnexpectedErrorCode,
|
|
9
|
+
CertificateTimeErrorCode,
|
|
10
|
+
ProtocolError,
|
|
11
|
+
LookupErrorCode,
|
|
12
|
+
} from '../errors.ts';
|
|
13
|
+
import { HttpAgent } from '../agent/http/index.ts';
|
|
14
|
+
import {
|
|
15
|
+
type Cert,
|
|
16
|
+
type CanisterRanges,
|
|
17
|
+
Certificate,
|
|
18
|
+
lookupResultToBuffer,
|
|
19
|
+
decodeCanisterRanges,
|
|
20
|
+
lookup_path,
|
|
21
|
+
LookupPathStatus,
|
|
22
|
+
} from '../certificate.ts';
|
|
23
|
+
import * as cbor from '../cbor.ts';
|
|
24
|
+
import { decodeTime } from '../utils/leb.ts';
|
|
25
|
+
import { utf8ToBytes } from '@noble/hashes/utils';
|
|
26
|
+
import {
|
|
27
|
+
type BaseStatus,
|
|
28
|
+
type BaseSubnetStatus,
|
|
29
|
+
type SubnetNodeKeys,
|
|
30
|
+
CustomPath,
|
|
31
|
+
decodeValue,
|
|
32
|
+
isCustomPath,
|
|
33
|
+
lookupNodeKeysFromCertificate,
|
|
34
|
+
IC_ROOT_SUBNET_ID,
|
|
35
|
+
} from '../utils/readState.ts';
|
|
36
|
+
|
|
37
|
+
// Re-export shared types and functions
|
|
38
|
+
export { type DecodeStrategy, CustomPath, IC_ROOT_SUBNET_ID } from '../utils/readState.ts';
|
|
39
|
+
|
|
40
|
+
export type SubnetStatus = BaseSubnetStatus & {
|
|
41
|
+
/**
|
|
42
|
+
* The public key of the subnet
|
|
43
|
+
*/
|
|
44
|
+
publicKey: Uint8Array;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
export type Status = BaseStatus | SubnetNodeKeys | CanisterRanges;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Pre-configured fields for subnet status paths
|
|
51
|
+
*/
|
|
52
|
+
export type Path = 'time' | 'canisterRanges' | 'publicKey' | 'nodeKeys' | CustomPath;
|
|
53
|
+
|
|
54
|
+
export type StatusMap = Map<Path | string, Status>;
|
|
55
|
+
|
|
56
|
+
export type SubnetStatusOptions = {
|
|
57
|
+
/**
|
|
58
|
+
* The subnet ID to query. Use {@link IC_ROOT_SUBNET_ID} for the IC mainnet root subnet.
|
|
59
|
+
* You can use {@link HttpAgent.getSubnetIdFromCanister} to get a subnet ID from a canister.
|
|
60
|
+
*/
|
|
61
|
+
subnetId: Principal;
|
|
62
|
+
/**
|
|
63
|
+
* The agent to use to make the subnet request.
|
|
64
|
+
*/
|
|
65
|
+
agent: HttpAgent;
|
|
66
|
+
/**
|
|
67
|
+
* The paths to request.
|
|
68
|
+
* @default []
|
|
69
|
+
*/
|
|
70
|
+
paths?: Path[] | Set<Path>;
|
|
71
|
+
/**
|
|
72
|
+
* Whether to disable the certificate freshness checks.
|
|
73
|
+
* @default false
|
|
74
|
+
*/
|
|
75
|
+
disableCertificateTimeVerification?: boolean;
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Requests information from a subnet's `read_state` endpoint.
|
|
80
|
+
* Can be used to request information about the subnet's time, canister ranges, public key, node keys, and metrics.
|
|
81
|
+
* @param {SubnetStatusOptions} options The configuration for the subnet status request.
|
|
82
|
+
* @see {@link SubnetStatusOptions} for detailed options.
|
|
83
|
+
* @returns {Promise<StatusMap>} A map populated with data from the requested paths. Each path is a key in the map, and the value is the data obtained from the certificate for that path.
|
|
84
|
+
* @example
|
|
85
|
+
* const status = await subnetStatus.request({
|
|
86
|
+
* subnetId: IC_ROOT_SUBNET_ID,
|
|
87
|
+
* paths: ['time', 'nodeKeys'],
|
|
88
|
+
* agent,
|
|
89
|
+
* });
|
|
90
|
+
*
|
|
91
|
+
* const time = status.get('time');
|
|
92
|
+
* const nodeKeys = status.get('nodeKeys');
|
|
93
|
+
*/
|
|
94
|
+
export async function request(options: SubnetStatusOptions): Promise<StatusMap> {
|
|
95
|
+
const { agent, paths, disableCertificateTimeVerification = false } = options;
|
|
96
|
+
const subnetId = Principal.from(options.subnetId);
|
|
97
|
+
|
|
98
|
+
const uniquePaths = [...new Set(paths)];
|
|
99
|
+
const status = new Map<string | Path, Status>();
|
|
100
|
+
|
|
101
|
+
const promises = uniquePaths.map((path, index) => {
|
|
102
|
+
const encodedPath = encodePath(path, subnetId);
|
|
103
|
+
|
|
104
|
+
return (async () => {
|
|
105
|
+
try {
|
|
106
|
+
if (agent.rootKey === null) {
|
|
107
|
+
throw ExternalError.fromCode(new MissingRootKeyErrorCode());
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const rootKey = agent.rootKey;
|
|
111
|
+
|
|
112
|
+
const response = await agent.readSubnetState(subnetId, {
|
|
113
|
+
paths: [encodedPath],
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
const certificate = await Certificate.create({
|
|
117
|
+
certificate: response.certificate,
|
|
118
|
+
rootKey,
|
|
119
|
+
principal: { subnetId },
|
|
120
|
+
disableTimeVerification: disableCertificateTimeVerification,
|
|
121
|
+
agent,
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
const lookup = (cert: Certificate, lookupPath: Path) => {
|
|
125
|
+
if (lookupPath === 'nodeKeys') {
|
|
126
|
+
// For node keys, we need to parse the certificate directly
|
|
127
|
+
const data = lookupNodeKeysFromCertificate(cert.cert, subnetId);
|
|
128
|
+
return {
|
|
129
|
+
path: lookupPath,
|
|
130
|
+
data,
|
|
131
|
+
};
|
|
132
|
+
} else {
|
|
133
|
+
return {
|
|
134
|
+
path: lookupPath,
|
|
135
|
+
data: lookupResultToBuffer(cert.lookup_path(encodedPath)),
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
const { path, data } = lookup(certificate, uniquePaths[index]);
|
|
141
|
+
if (!data) {
|
|
142
|
+
console.warn(`Expected to find result for path ${path}, but instead found nothing.`);
|
|
143
|
+
if (typeof path === 'string') {
|
|
144
|
+
status.set(path, null);
|
|
145
|
+
} else {
|
|
146
|
+
status.set(path.key, null);
|
|
147
|
+
}
|
|
148
|
+
} else {
|
|
149
|
+
switch (path) {
|
|
150
|
+
case 'time': {
|
|
151
|
+
status.set(path, decodeTime(data));
|
|
152
|
+
break;
|
|
153
|
+
}
|
|
154
|
+
case 'canisterRanges': {
|
|
155
|
+
status.set(path, decodeCanisterRanges(data));
|
|
156
|
+
break;
|
|
157
|
+
}
|
|
158
|
+
case 'publicKey': {
|
|
159
|
+
status.set(path, data);
|
|
160
|
+
break;
|
|
161
|
+
}
|
|
162
|
+
case 'nodeKeys': {
|
|
163
|
+
status.set(path, data);
|
|
164
|
+
break;
|
|
165
|
+
}
|
|
166
|
+
default: {
|
|
167
|
+
// Check for CustomPath signature
|
|
168
|
+
if (isCustomPath(path)) {
|
|
169
|
+
status.set(path.key, decodeValue(data, path.decodeStrategy));
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
} catch (error) {
|
|
175
|
+
// Throw on certificate errors
|
|
176
|
+
if (
|
|
177
|
+
error instanceof AgentError &&
|
|
178
|
+
(error.hasCode(CertificateVerificationErrorCode) ||
|
|
179
|
+
error.hasCode(CertificateTimeErrorCode))
|
|
180
|
+
) {
|
|
181
|
+
throw error;
|
|
182
|
+
}
|
|
183
|
+
if (isCustomPath(path)) {
|
|
184
|
+
status.set(path.key, null);
|
|
185
|
+
} else {
|
|
186
|
+
status.set(path, null);
|
|
187
|
+
}
|
|
188
|
+
console.group();
|
|
189
|
+
console.warn(`Expected to find result for path ${path}, but instead found nothing.`);
|
|
190
|
+
console.warn(error);
|
|
191
|
+
console.groupEnd();
|
|
192
|
+
}
|
|
193
|
+
})();
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
// Fetch all values separately, as each option can fail
|
|
197
|
+
await Promise.all(promises);
|
|
198
|
+
|
|
199
|
+
return status;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Fetch subnet info including node keys from a certificate
|
|
204
|
+
* @param certificate the certificate bytes
|
|
205
|
+
* @param subnetId the subnet ID
|
|
206
|
+
* @returns SubnetStatus with subnet ID and node keys
|
|
207
|
+
*/
|
|
208
|
+
export function lookupSubnetInfo(certificate: Uint8Array, subnetId: Principal): SubnetStatus {
|
|
209
|
+
const cert = cbor.decode<Cert>(certificate);
|
|
210
|
+
const nodeKeys = lookupNodeKeysFromCertificate(cert, subnetId);
|
|
211
|
+
const publicKey = lookupSubnetPublicKey(cert, subnetId);
|
|
212
|
+
|
|
213
|
+
return {
|
|
214
|
+
subnetId: subnetId.toText(),
|
|
215
|
+
nodeKeys,
|
|
216
|
+
publicKey,
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
function lookupSubnetPublicKey(certificate: Cert, subnetId: Principal): Uint8Array {
|
|
221
|
+
const subnetLookupResult = lookup_path(
|
|
222
|
+
['subnet', subnetId.toUint8Array(), 'public_key'],
|
|
223
|
+
certificate.tree,
|
|
224
|
+
);
|
|
225
|
+
if (subnetLookupResult.status !== LookupPathStatus.Found) {
|
|
226
|
+
throw ProtocolError.fromCode(
|
|
227
|
+
new LookupErrorCode('Public key not found', subnetLookupResult.status),
|
|
228
|
+
);
|
|
229
|
+
}
|
|
230
|
+
return subnetLookupResult.value;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Encode a path for subnet state queries
|
|
235
|
+
* @param path the path to encode
|
|
236
|
+
* @param subnetId the subnet ID
|
|
237
|
+
* @returns the encoded path as an array of Uint8Arrays
|
|
238
|
+
*/
|
|
239
|
+
export function encodePath(path: Path, subnetId: Principal): Uint8Array[] {
|
|
240
|
+
const subnetUint8Array = subnetId.toUint8Array();
|
|
241
|
+
switch (path) {
|
|
242
|
+
case 'time':
|
|
243
|
+
return [utf8ToBytes('time')];
|
|
244
|
+
case 'canisterRanges':
|
|
245
|
+
return [utf8ToBytes('canister_ranges'), subnetUint8Array];
|
|
246
|
+
case 'publicKey':
|
|
247
|
+
return [utf8ToBytes('subnet'), subnetUint8Array, utf8ToBytes('public_key')];
|
|
248
|
+
case 'nodeKeys':
|
|
249
|
+
return [utf8ToBytes('subnet'), subnetUint8Array, utf8ToBytes('node')];
|
|
250
|
+
default: {
|
|
251
|
+
// Check for CustomPath signature
|
|
252
|
+
if (isCustomPath(path)) {
|
|
253
|
+
if (typeof path['path'] === 'string' || path['path'] instanceof Uint8Array) {
|
|
254
|
+
// For string paths, treat as a subnet path segment
|
|
255
|
+
const encoded =
|
|
256
|
+
typeof path['path'] === 'string' ? utf8ToBytes(path['path']) : path['path'];
|
|
257
|
+
return [utf8ToBytes('subnet'), subnetUint8Array, encoded];
|
|
258
|
+
} else {
|
|
259
|
+
// For non-simple paths, return the provided custom path
|
|
260
|
+
return path['path'];
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
throw UnknownError.fromCode(
|
|
266
|
+
new UnexpectedErrorCode(
|
|
267
|
+
`Error while encoding your path for subnet status. Please ensure that your path ${path} was formatted correctly.`,
|
|
268
|
+
),
|
|
269
|
+
);
|
|
270
|
+
}
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
import { Principal } from '#principal';
|
|
2
|
+
import * as cbor from '../cbor.ts';
|
|
3
|
+
import { decodeLeb128 } from '../utils/leb.ts';
|
|
4
|
+
import { bytesToHex, utf8ToBytes } from '@noble/hashes/utils';
|
|
5
|
+
import { type DerEncodedPublicKey } from '../auth.ts';
|
|
6
|
+
import {
|
|
7
|
+
type Cert,
|
|
8
|
+
flatten_forks,
|
|
9
|
+
LookupPathStatus,
|
|
10
|
+
lookup_path,
|
|
11
|
+
lookup_subtree,
|
|
12
|
+
type LabeledHashTree,
|
|
13
|
+
LookupSubtreeStatus,
|
|
14
|
+
} from '../certificate.ts';
|
|
15
|
+
import {
|
|
16
|
+
LookupErrorCode,
|
|
17
|
+
DerKeyLengthMismatchErrorCode,
|
|
18
|
+
ProtocolError,
|
|
19
|
+
UnknownError,
|
|
20
|
+
HashTreeDecodeErrorCode,
|
|
21
|
+
} from '../errors.ts';
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* The root subnet ID for IC mainnet
|
|
25
|
+
*/
|
|
26
|
+
export const IC_ROOT_SUBNET_ID = Principal.fromText(
|
|
27
|
+
'tdb26-jop6k-aogll-7ltgs-eruif-6kk7m-qpktf-gdiqx-mxtrf-vb5e6-eqe',
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
export type SubnetNodeKeys = Map<string, DerEncodedPublicKey>;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Represents the useful information about a subnet
|
|
34
|
+
*/
|
|
35
|
+
export type BaseSubnetStatus = {
|
|
36
|
+
/**
|
|
37
|
+
* The subnet ID
|
|
38
|
+
*/
|
|
39
|
+
subnetId: string;
|
|
40
|
+
/**
|
|
41
|
+
* The node keys of the subnet
|
|
42
|
+
*/
|
|
43
|
+
nodeKeys: SubnetNodeKeys;
|
|
44
|
+
/**
|
|
45
|
+
* Not supported
|
|
46
|
+
*/
|
|
47
|
+
metrics?: never;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Base types of an entry on the status map.
|
|
52
|
+
* An entry of null indicates that the request failed, due to lack of permissions or the result being missing.
|
|
53
|
+
*/
|
|
54
|
+
export type BaseStatus = string | Uint8Array | Date | Uint8Array[] | Principal[] | bigint | null;
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Decode strategy for custom paths
|
|
58
|
+
*/
|
|
59
|
+
export type DecodeStrategy = 'cbor' | 'hex' | 'leb128' | 'utf-8' | 'raw';
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Interface to define a custom path. Nested paths will be represented as individual buffers, and can be created from text using TextEncoder.
|
|
63
|
+
* @param {string} key the key to use to access the returned value in the status map
|
|
64
|
+
* @param {Uint8Array[]} path the path to the desired value, represented as an array of buffers
|
|
65
|
+
* @param {string} decodeStrategy the strategy to use to decode the returned value
|
|
66
|
+
*/
|
|
67
|
+
export class CustomPath implements CustomPath {
|
|
68
|
+
public key: string;
|
|
69
|
+
public path: Uint8Array[] | string;
|
|
70
|
+
public decodeStrategy: DecodeStrategy;
|
|
71
|
+
constructor(key: string, path: Uint8Array[] | string, decodeStrategy: DecodeStrategy) {
|
|
72
|
+
this.key = key;
|
|
73
|
+
this.path = path;
|
|
74
|
+
this.decodeStrategy = decodeStrategy;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Decode a value based on the specified strategy
|
|
80
|
+
* @param data the raw data to decode
|
|
81
|
+
* @param strategy the decode strategy to use
|
|
82
|
+
* @returns the decoded value
|
|
83
|
+
*/
|
|
84
|
+
export function decodeValue(data: Uint8Array, strategy: DecodeStrategy): BaseStatus {
|
|
85
|
+
switch (strategy) {
|
|
86
|
+
case 'raw':
|
|
87
|
+
return data;
|
|
88
|
+
case 'leb128':
|
|
89
|
+
return decodeLeb128(data);
|
|
90
|
+
case 'cbor':
|
|
91
|
+
return cbor.decode(data);
|
|
92
|
+
case 'hex':
|
|
93
|
+
return bytesToHex(data);
|
|
94
|
+
case 'utf-8':
|
|
95
|
+
return new TextDecoder().decode(data);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Decode controllers from CBOR-encoded buffer
|
|
101
|
+
* @param buf the CBOR-encoded buffer to decode
|
|
102
|
+
* @returns an array of principal IDs
|
|
103
|
+
*/
|
|
104
|
+
export function decodeControllers(buf: Uint8Array): Principal[] {
|
|
105
|
+
const controllersRaw = cbor.decode<Uint8Array[]>(buf);
|
|
106
|
+
return controllersRaw.map(buf => {
|
|
107
|
+
return Principal.fromUint8Array(buf);
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Encode a metadata path for canister metadata queries
|
|
113
|
+
* @param metaPath the metadata path (string or Uint8Array)
|
|
114
|
+
* @param canisterUint8Array the canister ID as Uint8Array
|
|
115
|
+
* @returns the encoded path
|
|
116
|
+
*/
|
|
117
|
+
export function encodeMetadataPath(
|
|
118
|
+
metaPath: string | Uint8Array,
|
|
119
|
+
canisterUint8Array: Uint8Array,
|
|
120
|
+
): Uint8Array[] {
|
|
121
|
+
const encoded = typeof metaPath === 'string' ? utf8ToBytes(metaPath) : metaPath;
|
|
122
|
+
return [utf8ToBytes('canister'), canisterUint8Array, utf8ToBytes('metadata'), encoded];
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Check if a path object has custom path signature (has 'key' and 'path' properties)
|
|
127
|
+
* @param path the path to check
|
|
128
|
+
* @returns true if the path has custom path signature, false otherwise
|
|
129
|
+
*/
|
|
130
|
+
export function isCustomPath<T>(path: T): path is T & { key: string; path: unknown } {
|
|
131
|
+
return typeof path === 'object' && path !== null && 'key' in path && 'path' in path;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Lookup node keys from a certificate for a given subnet
|
|
136
|
+
* This can be used for both canister and subnet status queries
|
|
137
|
+
* @param certificate the certificate to fetch node keys from
|
|
138
|
+
* @param subnetId the subnet ID to fetch node keys for
|
|
139
|
+
* @returns a map of node IDs to public keys
|
|
140
|
+
*/
|
|
141
|
+
export function lookupNodeKeysFromCertificate(
|
|
142
|
+
certificate: Cert,
|
|
143
|
+
subnetId: Principal,
|
|
144
|
+
): SubnetNodeKeys {
|
|
145
|
+
const subnetLookupResult = lookup_subtree(
|
|
146
|
+
['subnet', subnetId.toUint8Array(), 'node'],
|
|
147
|
+
certificate.tree,
|
|
148
|
+
);
|
|
149
|
+
if (subnetLookupResult.status !== LookupSubtreeStatus.Found) {
|
|
150
|
+
throw ProtocolError.fromCode(new LookupErrorCode('Node not found', subnetLookupResult.status));
|
|
151
|
+
}
|
|
152
|
+
if (subnetLookupResult.value instanceof Uint8Array) {
|
|
153
|
+
throw UnknownError.fromCode(new HashTreeDecodeErrorCode('Invalid node tree'));
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// The forks are all labeled trees with the <node_id> label
|
|
157
|
+
const nodeForks = flatten_forks(subnetLookupResult.value) as Array<LabeledHashTree>;
|
|
158
|
+
const nodeKeys = new Map<string, DerEncodedPublicKey>();
|
|
159
|
+
|
|
160
|
+
nodeForks.forEach(fork => {
|
|
161
|
+
const node_id = Principal.from(fork[1]).toText();
|
|
162
|
+
const publicKeyLookupResult = lookup_path(['public_key'], fork[2]);
|
|
163
|
+
if (publicKeyLookupResult.status !== LookupPathStatus.Found) {
|
|
164
|
+
throw ProtocolError.fromCode(
|
|
165
|
+
new LookupErrorCode('Public key not found', publicKeyLookupResult.status),
|
|
166
|
+
);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
const derEncodedPublicKey = publicKeyLookupResult.value;
|
|
170
|
+
if (derEncodedPublicKey.byteLength !== 44) {
|
|
171
|
+
throw ProtocolError.fromCode(
|
|
172
|
+
new DerKeyLengthMismatchErrorCode(44, derEncodedPublicKey.byteLength),
|
|
173
|
+
);
|
|
174
|
+
} else {
|
|
175
|
+
nodeKeys.set(node_id, derEncodedPublicKey as DerEncodedPublicKey);
|
|
176
|
+
}
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
return nodeKeys;
|
|
180
|
+
}
|
package/src/candid/index.ts
CHANGED
|
@@ -1,5 +1,24 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* JavaScript and TypeScript module to work with Candid interfaces
|
|
3
|
+
*
|
|
4
|
+
* ## Usage
|
|
5
|
+
*
|
|
6
|
+
* ```ts
|
|
7
|
+
* import { IDL } from '@icp-sdk/core/candid';
|
|
8
|
+
*
|
|
9
|
+
* const MyType = IDL.Record({
|
|
10
|
+
* name: IDL.Text,
|
|
11
|
+
* age: IDL.Nat8,
|
|
12
|
+
* });
|
|
13
|
+
*
|
|
14
|
+
* const encoded = IDL.encode([MyType], [{ name: 'John', age: 30 }]);
|
|
15
|
+
* const decoded = IDL.decode([MyType], encoded);
|
|
16
|
+
*
|
|
17
|
+
* console.log(decoded); // [{ name: 'John', age: 30 }]
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* <!-- split here -->
|
|
21
|
+
* @module libs/candid/api
|
|
3
22
|
*/
|
|
4
23
|
|
|
5
24
|
export * from './candid-ui.ts';
|
package/src/identity/index.ts
CHANGED
|
@@ -1,5 +1,34 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* JavaScript and TypeScript module to manage Identities and enable simple Web Authentication flows for applications on the [Internet Computer](https://internetcomputer.org/)
|
|
3
|
+
*
|
|
4
|
+
* ## Usage
|
|
5
|
+
*
|
|
6
|
+
* ```ts
|
|
7
|
+
* import { ECDSAKeyIdentity, Ed25519KeyIdentity, WebAuthnIdentity } from '@icp-sdk/core/identity';
|
|
8
|
+
*
|
|
9
|
+
* const ecdsaIdentity = await ECDSAKeyIdentity.generate();
|
|
10
|
+
* const ed25519Identity = Ed25519KeyIdentity.generate();
|
|
11
|
+
* const webAuthnIdentity = await WebAuthnIdentity.create();
|
|
12
|
+
* ```
|
|
13
|
+
*
|
|
14
|
+
* ### DelegationIdentity
|
|
15
|
+
*
|
|
16
|
+
* The `DelegationIdentity` is typically generated using the [`@icp-sdk/auth`](https://js.icp.build/auth/latest/) package.
|
|
17
|
+
*
|
|
18
|
+
* ### PartialIdentity
|
|
19
|
+
*
|
|
20
|
+
* The `PartialIdentity` is not typically used directly, but is used by the `DelegationIdentity` and `WebAuthnIdentity` classes.
|
|
21
|
+
*
|
|
22
|
+
* ### Secp256k1KeyIdentity
|
|
23
|
+
*
|
|
24
|
+
* See [@icp-sdk/core/identity/secp256k1](https://js.icp.build/core/latest/libs/identity-secp256k1/api/) for more information.
|
|
25
|
+
*
|
|
26
|
+
* ## In Node.js
|
|
27
|
+
*
|
|
28
|
+
* Depending on your version, you may need to use a polyfill and set `global.crypto` in a setup file. If you prefer, you can also pass in a `subtleCrypto` implementation in methods that call for it, either as a direct argument, or in a `cryptoOptions` object.
|
|
29
|
+
*
|
|
30
|
+
* <!-- split here -->
|
|
31
|
+
* @module libs/identity/api
|
|
3
32
|
*/
|
|
4
33
|
|
|
5
34
|
export * from './identity/ed25519.ts';
|
|
@@ -8,12 +37,3 @@ export * from './identity/delegation.ts';
|
|
|
8
37
|
export * from './identity/partial.ts';
|
|
9
38
|
export { WebAuthnIdentity } from './identity/webauthn.ts';
|
|
10
39
|
export { wrapDER, unwrapDER, DER_COSE_OID, ED25519_OID } from '#agent';
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* @deprecated due to size of dependencies. Use `@icp-sdk/identity/secp256k1` instead.
|
|
14
|
-
*/
|
|
15
|
-
export class Secp256k1KeyIdentity {
|
|
16
|
-
constructor() {
|
|
17
|
-
throw new Error('Secp256k1KeyIdentity is available in @icp-sdk/identity/secp256k1');
|
|
18
|
-
}
|
|
19
|
-
}
|
|
@@ -1,5 +1,52 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* The `@icp-sdk/core/identity/secp256k1` module provides an implementation of the [`SignIdentity`](https://js.icp.build/core/latest/libs/agent/api/classes/signidentity/) interface for the secp256k1 elliptic curve. It allows you to create and manage key pairs for signing and verification of messages.
|
|
3
|
+
*
|
|
4
|
+
* ## Usage
|
|
5
|
+
*
|
|
6
|
+
* Here's an example of how to use the `Secp256k1KeyIdentity` class to generate a new key pair and sign and verify a message:
|
|
7
|
+
*
|
|
8
|
+
* ```ts
|
|
9
|
+
* import { Secp256k1KeyIdentity } from '@icp-sdk/core/identity/secp256k1';
|
|
10
|
+
*
|
|
11
|
+
* // Generate a new key pair
|
|
12
|
+
* const identity = Secp256k1KeyIdentity.generate();
|
|
13
|
+
*
|
|
14
|
+
* // Sign a message
|
|
15
|
+
* const message = 'Hello, world!';
|
|
16
|
+
* const signature = identity.sign(message);
|
|
17
|
+
*
|
|
18
|
+
* // Verify the signature
|
|
19
|
+
* const isValid = identity.verify(message, signature);
|
|
20
|
+
*
|
|
21
|
+
* console.log(`Signature is ${isValid ? 'valid' : 'invalid'}`);
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* You can also use a seed to generate deterministic key pairs:
|
|
25
|
+
*
|
|
26
|
+
* ```ts
|
|
27
|
+
* import { Secp256k1KeyIdentity } from '@icp-sdk/core/identity/secp256k1';
|
|
28
|
+
*
|
|
29
|
+
* const seed = Buffer.from('my-secret-seed', 'utf8');
|
|
30
|
+
* const identity = Secp256k1KeyIdentity.generate(seed);
|
|
31
|
+
* ```
|
|
32
|
+
*
|
|
33
|
+
* The Secp256k1KeyIdentity class also provides methods for converting the key pair to and from JSON-serializable objects:
|
|
34
|
+
*
|
|
35
|
+
* ```ts
|
|
36
|
+
* import { Secp256k1KeyIdentity } from '@icp-sdk/core/identity/secp256k1';
|
|
37
|
+
*
|
|
38
|
+
* // Generate a new key pair
|
|
39
|
+
* const identity = Secp256k1KeyIdentity.generate();
|
|
40
|
+
*
|
|
41
|
+
* // Convert the key pair to a JSON-serializable object
|
|
42
|
+
* const json = identity.toJson();
|
|
43
|
+
*
|
|
44
|
+
* // Convert the JSON-serializable object back to a key pair
|
|
45
|
+
* const restoredIdentity = Secp256k1KeyIdentity.fromJson(json);
|
|
46
|
+
* ```
|
|
47
|
+
*
|
|
48
|
+
* <!-- split here -->
|
|
49
|
+
* @module libs/identity/secp256k1/api
|
|
3
50
|
*/
|
|
4
51
|
|
|
5
52
|
export * from './secp256k1.ts';
|
package/src/principal/index.ts
CHANGED
|
@@ -1,3 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* JavaScript and TypeScript module to work with Internet Computer Principals.
|
|
3
|
+
*
|
|
4
|
+
* ## Usage
|
|
5
|
+
*
|
|
6
|
+
* ```ts
|
|
7
|
+
* import { Principal } from '@icp-sdk/core/principal';
|
|
8
|
+
*
|
|
9
|
+
* const canisterId = Principal.fromText('uqqxf-5h777-77774-qaaaa-cai');
|
|
10
|
+
* const anonymousPrincipal = Principal.anonymous();
|
|
11
|
+
* ```
|
|
12
|
+
*
|
|
13
|
+
* <!-- split here -->
|
|
14
|
+
* @module libs/principal/api
|
|
15
|
+
*/
|
|
16
|
+
|
|
1
17
|
export * from './principal.ts';
|
|
2
18
|
export { getCrc32 } from './utils/getCrc.ts';
|
|
3
19
|
export { base32Encode, base32Decode } from './utils/base32.ts';
|