UNPKG

@funded-labs/dab-js

Version:
105 lines (104 loc) 5.07 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const agent_1 = require("@dfinity/agent"); const principal_1 = require("@dfinity/principal"); const ext_did_1 = __importDefault(require("../../idls/ext.did")); const default_1 = __importDefault(require("./default")); const account_1 = require("../../utils/account"); const number_1 = require("../../utils/number"); const canisters_1 = require("../../constants/canisters"); const standards_1 = require("../../constants/standards"); const getTokenIdentifier = (canister, index) => { const padding = Buffer.from('\x0Atid'); const array = new Uint8Array([ ...padding, ...principal_1.Principal.fromText(canister).toUint8Array(), ...(0, number_1.to32bits)(index), ]); return principal_1.Principal.fromUint8Array(array).toText(); }; const extImageUrl = (canisterId, index, tokenIdentifier) => ({ [canisters_1.NFT_CANISTERS.WRAPPED_PUNKS]: `https://${canisters_1.NFT_CANISTERS.IC_PUNKS}.raw.icp0.io/Token/${index}`, [canisters_1.NFT_CANISTERS.WRAPPED_DRIP]: `https://${canisters_1.NFT_CANISTERS.IC_DRIP}.raw.icp0.io?tokenId=${index}`, }[canisterId] || `https://${canisterId}.raw.icp0.io/?type=thumbnail&tokenid=${tokenIdentifier}`); class EXT extends default_1.default { constructor(canisterId, agent, blsVerify) { super(canisterId, agent); this.standard = standards_1.NFT.ext; this.actor = agent_1.Actor.createActor(ext_did_1.default, { agent, canisterId, blsVerify, }); } getUserTokens(principal) { return __awaiter(this, void 0, void 0, function* () { const accountId = (0, account_1.getAccountId)(principal); const userTokensResult = yield this.actor.tokens_ext(accountId); if ('err' in userTokensResult) throw new Error(`${Object.keys(userTokensResult.err)[0]}: ${Object.values(userTokensResult.err)[0]}`); const tokens = userTokensResult.ok || []; return tokens.map((token) => { const metadata = token[2]; const tokenIndex = token[0]; return this.serializeTokenData(metadata, getTokenIdentifier(this.canisterId, tokenIndex), tokenIndex); }); }); } getMetadata() { throw new Error('Method not implemented.'); } transfer(to, tokenIndex) { return __awaiter(this, void 0, void 0, function* () { const tokenIdentifier = getTokenIdentifier(this.canisterId, tokenIndex); const from = yield this.agent.getPrincipal(); const dummyMemmo = new Array(32).fill(0); const transferResult = yield this.actor.transfer({ to: { principal: to }, from: { principal: from }, token: tokenIdentifier, amount: BigInt(1), memo: dummyMemmo, notify: false, subaccount: [], fee: BigInt(0), }); if ('err' in transferResult) throw new Error(`${Object.keys(transferResult.err)[0]}: ${Object.values(transferResult.err)[0]}`); }); } details(tokenIndex) { return __awaiter(this, void 0, void 0, function* () { const tokenIdentifier = getTokenIdentifier(this.canisterId, tokenIndex); const metadataResult = yield this.actor.metadata(tokenIdentifier); if ('err' in metadataResult) throw new Error(`${Object.keys(metadataResult.err)[0]}: ${Object.values(metadataResult.err)[0]}`); const { metadata = {} } = 'nonfungible' in metadataResult.ok ? metadataResult.ok.nonfungible : {}; return this.serializeTokenData(metadata, tokenIdentifier, tokenIndex); }); } serializeTokenData(metadata, tokenIdentifier, tokenIndex) { return { id: tokenIdentifier, index: BigInt(tokenIndex), canister: this.canisterId, metadata: metadata.length ? metadata[0] : undefined, url: extImageUrl(this.canisterId, tokenIndex, tokenIdentifier), standard: this.standard, }; } } exports.default = EXT;