@dfinity/pic
Version:
An Internet Computer Protocol canister testing library for TypeScript and JavaScript.
392 lines • 14.3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.EncodedCanisterHttpMethod = exports.CanisterHttpMethod = exports.SubnetType = exports.SubnetStateType = void 0;
exports.encodeCreateInstanceRequest = encodeCreateInstanceRequest;
exports.encodeGetPubKeyRequest = encodeGetPubKeyRequest;
exports.decodeGetTopologyResponse = decodeGetTopologyResponse;
exports.decodeSubnetTopology = decodeSubnetTopology;
exports.decodeSubnetKind = decodeSubnetKind;
exports.encodeGetControllersRequest = encodeGetControllersRequest;
exports.decodeGetControllersResponse = decodeGetControllersResponse;
exports.decodeGetTimeResponse = decodeGetTimeResponse;
exports.encodeSetTimeRequest = encodeSetTimeRequest;
exports.encodeGetSubnetIdRequest = encodeGetSubnetIdRequest;
exports.decodeGetSubnetIdResponse = decodeGetSubnetIdResponse;
exports.encodeGetCyclesBalanceRequest = encodeGetCyclesBalanceRequest;
exports.decodeGetCyclesBalanceResponse = decodeGetCyclesBalanceResponse;
exports.encodeAddCyclesRequest = encodeAddCyclesRequest;
exports.decodeAddCyclesResponse = decodeAddCyclesResponse;
exports.encodeUploadBlobRequest = encodeUploadBlobRequest;
exports.decodeUploadBlobResponse = decodeUploadBlobResponse;
exports.encodeSetStableMemoryRequest = encodeSetStableMemoryRequest;
exports.encodeGetStableMemoryRequest = encodeGetStableMemoryRequest;
exports.decodeGetStableMemoryResponse = decodeGetStableMemoryResponse;
exports.decodeGetPendingHttpsOutcallsResponse = decodeGetPendingHttpsOutcallsResponse;
exports.encodeMockPendingHttpsOutcallRequest = encodeMockPendingHttpsOutcallRequest;
exports.encodeEffectivePrincipal = encodeEffectivePrincipal;
exports.decodeEffectivePrincipal = decodeEffectivePrincipal;
exports.encodeCanisterCallRequest = encodeCanisterCallRequest;
exports.decodeCanisterCallResponse = decodeCanisterCallResponse;
exports.encodeSubmitCanisterCallRequest = encodeSubmitCanisterCallRequest;
exports.decodeSubmitCanisterCallResponse = decodeSubmitCanisterCallResponse;
exports.encodeAwaitCanisterCallRequest = encodeAwaitCanisterCallRequest;
exports.decodeAwaitCanisterCallResponse = decodeAwaitCanisterCallResponse;
const principal_1 = require("@dfinity/principal");
const util_1 = require("./util");
const error_1 = require("./error");
var SubnetStateType;
(function (SubnetStateType) {
SubnetStateType["New"] = "new";
SubnetStateType["FromPath"] = "fromPath";
})(SubnetStateType || (exports.SubnetStateType = SubnetStateType = {}));
function encodeManySubnetConfigs(configs = []) {
return configs.map(encodeSubnetConfig).filter(util_1.isNotNil);
}
function encodeSubnetConfig(config) {
if ((0, util_1.isNil)(config)) {
return undefined;
}
switch (config.state.type) {
default: {
throw new Error(`Unknown subnet state type: ${config.state}`);
}
case SubnetStateType.New: {
return {
dts_flag: encodeDtsFlag(config.enableDeterministicTimeSlicing),
instruction_config: encodeInstructionConfig(config.enableBenchmarkingInstructionLimits),
state_config: 'New',
};
}
case SubnetStateType.FromPath: {
return {
dts_flag: encodeDtsFlag(config.enableDeterministicTimeSlicing),
instruction_config: encodeInstructionConfig(config.enableBenchmarkingInstructionLimits),
state_config: {
FromPath: config.state.path,
},
};
}
}
}
function encodeDtsFlag(enableDeterministicTimeSlicing) {
return enableDeterministicTimeSlicing === false ? 'Disabled' : 'Enabled';
}
function encodeInstructionConfig(enableBenchmarkingInstructionLimits) {
return enableBenchmarkingInstructionLimits === true
? 'Benchmarking'
: 'Production';
}
function encodeCreateInstanceRequest(req) {
const defaultApplicationSubnet = {
state: { type: SubnetStateType.New },
};
const defaultOptions = req ?? {
application: [defaultApplicationSubnet],
};
const options = {
subnet_config_set: {
nns: encodeSubnetConfig(defaultOptions.nns),
sns: encodeSubnetConfig(defaultOptions.sns),
ii: encodeSubnetConfig(defaultOptions.ii),
fiduciary: encodeSubnetConfig(defaultOptions.fiduciary),
bitcoin: encodeSubnetConfig(defaultOptions.bitcoin),
system: encodeManySubnetConfigs(defaultOptions.system),
application: encodeManySubnetConfigs(defaultOptions.application ?? [defaultApplicationSubnet]),
verified_application: encodeManySubnetConfigs(defaultOptions.verifiedApplication),
},
nonmainnet_features: defaultOptions.nonmainnetFeatures ?? false,
};
if (((0, util_1.isNil)(options.subnet_config_set.nns) &&
(0, util_1.isNil)(options.subnet_config_set.sns) &&
(0, util_1.isNil)(options.subnet_config_set.ii) &&
(0, util_1.isNil)(options.subnet_config_set.fiduciary) &&
(0, util_1.isNil)(options.subnet_config_set.bitcoin) &&
options.subnet_config_set.system.length === 0 &&
options.subnet_config_set.application.length === 0) ||
options.subnet_config_set.system.length < 0 ||
options.subnet_config_set.application.length < 0) {
throw new error_1.TopologyValidationError();
}
return options;
}
function encodeGetPubKeyRequest(req) {
return {
subnet_id: (0, util_1.base64EncodePrincipal)(req.subnetId),
};
}
var SubnetType;
(function (SubnetType) {
SubnetType["Application"] = "Application";
SubnetType["Bitcoin"] = "Bitcoin";
SubnetType["Fiduciary"] = "Fiduciary";
SubnetType["InternetIdentity"] = "II";
SubnetType["NNS"] = "NNS";
SubnetType["SNS"] = "SNS";
SubnetType["System"] = "System";
})(SubnetType || (exports.SubnetType = SubnetType = {}));
function decodeGetTopologyResponse(encoded) {
return Object.fromEntries(Object.entries(encoded.subnet_configs).map(([subnetId, subnetTopology]) => [
subnetId,
decodeSubnetTopology(subnetId, subnetTopology),
]));
}
function decodeSubnetTopology(subnetId, encoded) {
return {
id: principal_1.Principal.fromText(subnetId),
type: decodeSubnetKind(encoded.subnet_kind),
size: encoded.size,
canisterRanges: encoded.canister_ranges.map(range => ({
start: (0, util_1.base64DecodePrincipal)(range.start.canister_id),
end: (0, util_1.base64DecodePrincipal)(range.end.canister_id),
})),
};
}
function decodeSubnetKind(kind) {
switch (kind) {
case 'Application':
return SubnetType.Application;
case 'Bitcoin':
return SubnetType.Bitcoin;
case 'Fiduciary':
return SubnetType.Fiduciary;
case 'II':
return SubnetType.InternetIdentity;
case 'NNS':
return SubnetType.NNS;
case 'SNS':
return SubnetType.SNS;
case 'System':
return SubnetType.System;
default:
throw new Error(`Unknown subnet kind: ${kind}`);
}
}
function encodeGetControllersRequest(req) {
return {
canister_id: (0, util_1.base64EncodePrincipal)(req.canisterId),
};
}
function decodeGetControllersResponse(res) {
return res.map(({ principal_id }) => (0, util_1.base64DecodePrincipal)(principal_id));
}
function decodeGetTimeResponse(res) {
return {
millisSinceEpoch: res.nanos_since_epoch / 1_000_000,
};
}
function encodeSetTimeRequest(req) {
return {
nanos_since_epoch: req.millisSinceEpoch * 1_000_000,
};
}
function encodeGetSubnetIdRequest(req) {
return {
canister_id: (0, util_1.base64EncodePrincipal)(req.canisterId),
};
}
function decodeGetSubnetIdResponse(res) {
if ((0, util_1.isNil)(res)) {
return { subnetId: null };
}
if ('subnet_id' in res) {
return { subnetId: (0, util_1.base64DecodePrincipal)(res.subnet_id) };
}
return { subnetId: null };
}
function encodeGetCyclesBalanceRequest(req) {
return {
canister_id: (0, util_1.base64EncodePrincipal)(req.canisterId),
};
}
function decodeGetCyclesBalanceResponse(res) {
return {
cycles: res.cycles,
};
}
function encodeAddCyclesRequest(req) {
return {
canister_id: (0, util_1.base64EncodePrincipal)(req.canisterId),
amount: req.amount,
};
}
function decodeAddCyclesResponse(res) {
return {
cycles: res.cycles,
};
}
function encodeUploadBlobRequest(req) {
return req.blob;
}
function decodeUploadBlobResponse(res) {
return {
blobId: new Uint8Array((0, util_1.hexDecode)(res)),
};
}
function encodeSetStableMemoryRequest(req) {
return {
canister_id: (0, util_1.base64EncodePrincipal)(req.canisterId),
blob_id: (0, util_1.base64Encode)(req.blobId),
};
}
function encodeGetStableMemoryRequest(req) {
return {
canister_id: (0, util_1.base64EncodePrincipal)(req.canisterId),
};
}
function decodeGetStableMemoryResponse(res) {
return {
blob: (0, util_1.base64Decode)(res.blob),
};
}
var CanisterHttpMethod;
(function (CanisterHttpMethod) {
CanisterHttpMethod["GET"] = "GET";
CanisterHttpMethod["POST"] = "POST";
CanisterHttpMethod["HEAD"] = "HEAD";
})(CanisterHttpMethod || (exports.CanisterHttpMethod = CanisterHttpMethod = {}));
var EncodedCanisterHttpMethod;
(function (EncodedCanisterHttpMethod) {
EncodedCanisterHttpMethod["GET"] = "GET";
EncodedCanisterHttpMethod["POST"] = "POST";
EncodedCanisterHttpMethod["HEAD"] = "HEAD";
})(EncodedCanisterHttpMethod || (exports.EncodedCanisterHttpMethod = EncodedCanisterHttpMethod = {}));
function decodeGetPendingHttpsOutcallsResponse(res) {
return res.map(decodeHttpOutcall);
}
function decodeHttpOutcall(res) {
return {
subnetId: (0, util_1.base64DecodePrincipal)(res.subnet_id.subnet_id),
requestId: res.request_id,
httpMethod: decodeCanisterHttpMethod(res.http_method),
url: res.url,
headers: res.headers.map(decodeHttpHeader),
body: (0, util_1.base64Decode)(res.body),
maxResponseBytes: res.max_response_bytes,
};
}
function decodeCanisterHttpMethod(method) {
switch (method) {
default:
throw new Error(`Unknown canister HTTP method: ${method}`);
case EncodedCanisterHttpMethod.GET:
return CanisterHttpMethod.GET;
case EncodedCanisterHttpMethod.POST:
return CanisterHttpMethod.POST;
case EncodedCanisterHttpMethod.HEAD:
return CanisterHttpMethod.HEAD;
}
}
function decodeHttpHeader(header) {
return [header.name, header.value];
}
function encodeMockPendingHttpsOutcallRequest(req) {
return {
subnet_id: {
subnet_id: (0, util_1.base64EncodePrincipal)(req.subnetId),
},
request_id: req.requestId,
response: encodeHttpsOutcallResponse(req.response),
additional_responses: req.additionalResponses.map(encodeHttpsOutcallResponse),
};
}
function encodeHttpsOutcallResponse(res) {
switch (res.type) {
default:
throw new Error(`Unknown response type: ${res}`);
case 'success': {
return {
CanisterHttpReply: {
status: res.statusCode,
headers: res.headers.map(encodeHttpHeader),
body: (0, util_1.base64Encode)(res.body),
},
};
}
case 'reject': {
return {
CanisterHttpReject: {
reject_code: res.statusCode,
message: res.message,
},
};
}
}
}
function encodeHttpHeader(header) {
return {
name: header[0],
value: header[1],
};
}
function encodeEffectivePrincipal(effectivePrincipal) {
if ((0, util_1.isNil)(effectivePrincipal)) {
return 'None';
}
if ('subnetId' in effectivePrincipal) {
return {
SubnetId: (0, util_1.base64EncodePrincipal)(effectivePrincipal.subnetId),
};
}
else {
return {
CanisterId: (0, util_1.base64EncodePrincipal)(effectivePrincipal.canisterId),
};
}
}
function decodeEffectivePrincipal(effectivePrincipal) {
if (effectivePrincipal === 'None') {
return null;
}
else if ('SubnetId' in effectivePrincipal) {
return {
subnetId: (0, util_1.base64DecodePrincipal)(effectivePrincipal.SubnetId),
};
}
else {
return {
canisterId: (0, util_1.base64DecodePrincipal)(effectivePrincipal.CanisterId),
};
}
}
function encodeCanisterCallRequest(req) {
return {
sender: (0, util_1.base64EncodePrincipal)(req.sender),
canister_id: (0, util_1.base64EncodePrincipal)(req.canisterId),
method: req.method,
payload: (0, util_1.base64Encode)(req.payload),
effective_principal: encodeEffectivePrincipal(req.effectivePrincipal),
};
}
function decodeCanisterCallResponse(res) {
const okRes = decodeResultResponse(res);
return {
body: (0, util_1.base64Decode)(okRes),
};
}
function decodeResultResponse(res) {
if ('Err' in res) {
throw new Error(`Canister call failed: ${res.Err.reject_message}. Reject code: ${res.Err.reject_code}. Error code: ${res.Err.error_code}. Certified: ${res.Err.certified}`);
}
return res.Ok;
}
function encodeSubmitCanisterCallRequest(req) {
return encodeCanisterCallRequest(req);
}
function decodeSubmitCanisterCallResponse(res) {
const okRes = decodeResultResponse(res);
return {
effectivePrincipal: decodeEffectivePrincipal(okRes.effective_principal),
messageId: okRes.message_id,
};
}
function encodeAwaitCanisterCallRequest(req) {
return {
effective_principal: encodeEffectivePrincipal(req.effectivePrincipal),
message_id: req.messageId,
};
}
function decodeAwaitCanisterCallResponse(res) {
return decodeCanisterCallResponse(res);
}
//#endregion AwaitCanisterCall
//# sourceMappingURL=pocket-ic-client-types.js.map