@funded-labs/plug-controller
Version:
Internet Computer Plug wallet's controller
146 lines (145 loc) • 8.67 kB
JavaScript
"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 __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
if (kind === "m") throw new TypeError("Private method is not writable");
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
var _ICNSAdapter_resolver, _ICNSAdapter_registry, _ICNSAdapter_reverseRegistrar, _ICNSAdapter_agent;
Object.defineProperty(exports, "__esModule", { value: true });
const agent_1 = require("@dfinity/agent");
const icns_resolver_did_1 = __importDefault(require("../../../idls/icns_resolver.did"));
const icns_registry_did_1 = __importDefault(require("../../../idls/icns_registry.did"));
const icns_reverse_registrar_did_1 = __importDefault(require("../../../idls/icns_reverse_registrar.did"));
const principal_1 = require("@dfinity/principal");
const dab_js_1 = require("@funded-labs/dab-js");
const errors_1 = require("../../../errors");
const ICNS_REGISTRY_ID = 'e5kvl-zyaaa-aaaan-qabaq-cai';
const ICNS_RESOLVER_ID = 'euj6x-pqaaa-aaaan-qabba-cai';
const ICNS_REVERSE_REGISTRAR_ID = 'etiyd-ciaaa-aaaan-qabbq-cai';
const ICNS_IMG = 'https://icns.id/Rectangle.jpg';
const ICNS_LOGO = 'https://icns.id/ICNS-logo.png';
class ICNSAdapter {
constructor(agent) {
_ICNSAdapter_resolver.set(this, void 0);
_ICNSAdapter_registry.set(this, void 0);
_ICNSAdapter_reverseRegistrar.set(this, void 0);
_ICNSAdapter_agent.set(this, void 0);
this.resolveName = (name, isICP) => __awaiter(this, void 0, void 0, function* () {
var _a, _b;
let record = yield __classPrivateFieldGet(this, _ICNSAdapter_resolver, "f").getUserDefaultInfo(name);
const { icp, pid: principal } = (record === null || record === void 0 ? void 0 : record[0]) || {};
const accountId = icp === null || icp === void 0 ? void 0 : icp[0];
if (isICP && accountId) {
return accountId;
}
if (!principal) {
record = yield __classPrivateFieldGet(this, _ICNSAdapter_registry, "f").getRecord(name);
const { owner } = (record === null || record === void 0 ? void 0 : record[0]) || {};
return (_a = owner === null || owner === void 0 ? void 0 : owner.toString) === null || _a === void 0 ? void 0 : _a.call(owner);
}
return (_b = principal === null || principal === void 0 ? void 0 : principal.toString) === null || _b === void 0 ? void 0 : _b.call(principal);
});
this.getICNSNames = () => __awaiter(this, void 0, void 0, function* () {
const _names = yield __classPrivateFieldGet(this, _ICNSAdapter_registry, "f").getUserDomains(yield __classPrivateFieldGet(this, _ICNSAdapter_agent, "f").getPrincipal());
const names = (_names === null || _names === void 0 ? void 0 : _names[0]) || [];
return names;
});
this.getICNSCollection = () => __awaiter(this, void 0, void 0, function* () {
let icnsNames = yield this.getICNSNames();
const formattedNames = icnsNames === null || icnsNames === void 0 ? void 0 : icnsNames.map((name) => {
var _a, _b;
return ({
name: ((_a = name === null || name === void 0 ? void 0 : name.name) === null || _a === void 0 ? void 0 : _a.toString()) || '',
index: (_b = name === null || name === void 0 ? void 0 : name.id) === null || _b === void 0 ? void 0 : _b.toString(),
url: ICNS_IMG,
collection: 'ICNS',
desc: 'ICNS Name Record',
canister: ICNS_REGISTRY_ID,
metadata: {},
standard: dab_js_1.standards.NFT.dip721,
});
});
return {
canisterId: ICNS_REGISTRY_ID,
description: 'ICNS .icp names',
icon: ICNS_LOGO,
name: 'ICNS',
standard: dab_js_1.standards.NFT.dip721,
tokens: formattedNames || [],
};
});
this.getICNSReverseResolvedName = (principalId) => __awaiter(this, void 0, void 0, function* () {
const ownPrincipal = yield __classPrivateFieldGet(this, _ICNSAdapter_agent, "f").getPrincipal();
const principal = principalId ? principal_1.Principal.from(principalId) : ownPrincipal;
const name = yield __classPrivateFieldGet(this, _ICNSAdapter_reverseRegistrar, "f").getName(principal);
return name;
});
this.setICNSReverseResolvedName = (name) => __awaiter(this, void 0, void 0, function* () {
yield this.resetNameRecordData(name);
const result = yield __classPrivateFieldGet(this, _ICNSAdapter_reverseRegistrar, "f").setName(name);
if ('ok' in result) {
return result.ok;
}
throw (errors_1.ERRORS.ICNS_REVERSE_RESOLVER_ERROR);
});
this.getICNSMappings = (principalIds) => __awaiter(this, void 0, void 0, function* () {
const mappings = {};
yield Promise.all(principalIds.map((pid) => __awaiter(this, void 0, void 0, function* () {
try {
const name = yield this.getICNSReverseResolvedName(pid);
if (name) {
mappings[name] = pid;
mappings[pid] = name;
}
}
catch (e) {
console.log('error getting ICNS mapping', pid, e);
}
})));
return mappings;
});
this.resetNameRecordData = (name) => __awaiter(this, void 0, void 0, function* () {
try {
const principal = yield __classPrivateFieldGet(this, _ICNSAdapter_agent, "f").getPrincipal();
yield __classPrivateFieldGet(this, _ICNSAdapter_resolver, "f").setAddr(name, 'icp.principal', [principal.toString()]);
}
catch (e) {
console.log('Error when reseting your name data', e);
}
});
__classPrivateFieldSet(this, _ICNSAdapter_agent, agent, "f");
__classPrivateFieldSet(this, _ICNSAdapter_resolver, agent_1.Actor.createActor(icns_resolver_did_1.default, {
canisterId: ICNS_RESOLVER_ID,
agent,
}), "f");
__classPrivateFieldSet(this, _ICNSAdapter_registry, agent_1.Actor.createActor(icns_registry_did_1.default, {
canisterId: ICNS_REGISTRY_ID,
agent,
}), "f");
__classPrivateFieldSet(this, _ICNSAdapter_reverseRegistrar, agent_1.Actor.createActor(icns_reverse_registrar_did_1.default, {
canisterId: ICNS_REVERSE_REGISTRAR_ID,
agent,
}), "f");
}
}
exports.default = ICNSAdapter;
_ICNSAdapter_resolver = new WeakMap(), _ICNSAdapter_registry = new WeakMap(), _ICNSAdapter_reverseRegistrar = new WeakMap(), _ICNSAdapter_agent = new WeakMap();
;