UNPKG

@truenetworkio/sdk

Version:

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

69 lines (68 loc) 3.26 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()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.editIssuer = exports.createIssuer = void 0; const state_1 = require("./state"); const hashing_1 = require("../../utils/hashing"); const createIssuer = (api, account, name, controllers) => __awaiter(void 0, void 0, void 0, function* () { // Check if already exists. // Check if you are the owner, then skip the method. const hash = (0, hashing_1.stringToBlakeTwo256Hash)(name); const checkIfExists = yield (0, state_1.getIssuer)(api, `0x${hash}`); if (checkIfExists) { throw Error("Issuer already exists."); } return yield new Promise((resolve, reject) => { api.tx[state_1.ISSUER_PALLET_NAME] .createIssuer(name, controllers) .signAndSend(account, (result) => { result.events.forEach(({ event: { method } }) => { if (method == 'ExtrinsicFailed') { reject('\nTransaction failed, error creating user.'); } }); if (result.status.isInBlock) { console.log(`\nTransaction finalized at blockHash ${result.status.asInBlock}`); resolve(`0x${hash}`); } }); }); }); exports.createIssuer = createIssuer; const editIssuer = (api, account, hashId, name, controllers) => __awaiter(void 0, void 0, void 0, function* () { // Check if already exists. // Check if you are the owner, then skip the method. const newHash = (0, hashing_1.stringToBlakeTwo256Hash)(name); const checkIfNewNameAvailable = yield (0, state_1.getIssuer)(api, `0x${newHash}`); const checkIfExists = yield (0, state_1.getIssuer)(api, `0x${hashId}`); if (!checkIfExists) { throw Error("Issuer does not exist."); } if (checkIfNewNameAvailable) { throw Error("New name for issuer is not available."); } return yield new Promise((resolve, reject) => { api.tx[state_1.ISSUER_PALLET_NAME] .editIssuer(`0x${hashId}`, name, controllers) .signAndSend(account, (result) => { result.events.forEach(({ event: { method } }) => { if (method == 'ExtrinsicFailed') { reject('\nTransaction failed, error creating user.'); } }); if (result.status.isInBlock) { console.log(`\nTransaction finalized at blockHash ${result.status.asInBlock}`); resolve(`0x${newHash}`); } }); }); }); exports.editIssuer = editIssuer;