UNPKG

@truenetworkio/sdk

Version:

True Network SDK is the abstracted interface for interacting with True Network nodes.

108 lines (107 loc) 4.8 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 __exportStar = (this && this.__exportStar) || function(m, exports) { for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); }; 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()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.TrueApi = void 0; const api_1 = require("@polkadot/api"); const util_1 = require("@polkadot/util"); const network_1 = require("./network"); const extrinsic_1 = require("./pallets/issuer/extrinsic"); const address_1 = require("./utils/address"); const extrinsic_2 = require("./pallets/algorithms/extrinsic"); const state_1 = require("./pallets/credentials/state"); const state_2 = require("./pallets/issuer/state"); const utils_1 = require("./utils"); // Create a keyring instance const keyring = new api_1.Keyring({ type: 'sr25519' }); keyring.setSS58Format(7); class TrueApi { constructor(api, accountKey, issuerHash) { this.network = api; this.account = keyring.addFromUri(accountKey); if (issuerHash) this.issuerHash = issuerHash; } close() { return __awaiter(this, void 0, void 0, function* () { yield this.network.disconnect(); }); } static create(accountKey, nodeUrl) { return __awaiter(this, void 0, void 0, function* () { if (!this.instance || this.instance.account.address !== keyring.addFromUri(accountKey).address) { const api = yield (0, network_1.connect)(nodeUrl); this.instance = new TrueApi(api, accountKey); } return this.instance; }); } setIssuer(hash) { return __awaiter(this, void 0, void 0, function* () { this.issuerHash = hash; }); } // Abstracted extrinsics of the pallets. registerIssuer(name, controllers) { return __awaiter(this, void 0, void 0, function* () { const trueAddresses = (0, address_1.checkAndConvertAddresses)(controllers); const issuer = yield (0, extrinsic_1.createIssuer)(this.network, this.account, name, trueAddresses); this.issuerHash = issuer; return issuer; }); } getReputationScore(algoId, user) { return __awaiter(this, void 0, void 0, function* () { if (!this.issuerHash) throw Error("Issuer not found."); return yield (0, extrinsic_2.runAlgo)(this.network, this.issuerHash, this.account, user, algoId); }); } getSchemaFromHash(schemaHash) { return __awaiter(this, void 0, void 0, function* () { return yield (0, state_1.getSchemaFromHash)(this.network, schemaHash); }); } getIssuerFromHash(issuerHash) { return __awaiter(this, void 0, void 0, function* () { return yield (0, state_2.getIssuer)(this.network, issuerHash); }); } getBalance(address) { return __awaiter(this, void 0, void 0, function* () { const account = (0, address_1.toTrueNetworkAddress)(address); const freeBalance = yield (0, utils_1.getFreeBalance)(this.network, account); const decimals = this.network.registry.chainDecimals; return { text: (0, util_1.formatBalance)(freeBalance.toString(), { withSiFull: true, withUnit: 'TRUE', decimals: decimals[0] }).toString(), numeric: freeBalance, decimals: decimals[0] }; }); } } exports.TrueApi = TrueApi; __exportStar(require("./schemas"), exports); __exportStar(require("./utils"), exports); __exportStar(require("./network"), exports);