UNPKG

@dfinity/agent

Version:

JavaScript and TypeScript library to interact with the Internet Computer

69 lines 2.83 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.fetchCandid = fetchCandid; const principal_1 = require("@dfinity/principal"); const CanisterStatus = __importStar(require("./canisterStatus/index.js")); const index_ts_1 = require("./agent/http/index.js"); const actor_ts_1 = require("./actor.js"); /** * Retrieves the Candid interface for the specified canister. * @param canisterId A string corresponding to the canister ID * @param agent The agent to use for the request (usually an `HttpAgent`) * @returns Candid source code */ async function fetchCandid(canisterId, agent) { if (!agent) { // Create an anonymous `HttpAgent` (adapted from Candid UI) agent = await index_ts_1.HttpAgent.create(); } // Attempt to use canister metadata const status = await CanisterStatus.request({ agent, canisterId: principal_1.Principal.fromText(canisterId), paths: ['candid'], }); const candid = status.get('candid'); if (candid) { return candid; } // Use `__get_candid_interface_tmp_hack` for canisters without Candid metadata const tmpHackInterface = ({ IDL }) => IDL.Service({ __get_candid_interface_tmp_hack: IDL.Func([], [IDL.Text], ['query']), }); const actor = actor_ts_1.Actor.createActor(tmpHackInterface, { agent, canisterId }); return (await actor.__get_candid_interface_tmp_hack()); } //# sourceMappingURL=fetch_candid.js.map