@truenetworkio/sdk
Version:
True Network SDK is the abstracted interface for interacting with True Network nodes.
197 lines (196 loc) • 9.91 kB
JavaScript
;
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.updateAttestation = exports.createAttestation = exports.createAttestationTx = exports.createSchema = exports.createSchemaTx = void 0;
const state_1 = require("./state");
const state_2 = require("../issuer/state");
const network_1 = require("../../network");
const createSchemaTx = (api, account, issuerHash, schema) => __awaiter(void 0, void 0, void 0, function* () {
// Check if issuer exists or not.
// Check if you are the owner, then skip the method.
const issuer = yield (0, state_2.getIssuer)(api, issuerHash);
if (!issuer) {
throw Error("Issuer does not exists.");
}
// Check if the user is a controller.
if (!issuer.controllers.includes(account.address)) {
throw Error("Cannot create schema, account is not a controller.");
}
return api.tx[state_1.CREDENTIALS_PALLET_NAME]
.createSchema(issuerHash, schema.getSchemaObject());
});
exports.createSchemaTx = createSchemaTx;
const createSchema = (api, account, issuerHash, schema) => __awaiter(void 0, void 0, void 0, function* () {
// Check if issuer exists or not.
// Check if you are the owner, then skip the method.
const issuer = yield (0, state_2.getIssuer)(api, issuerHash);
if (!issuer) {
throw Error("Issuer does not exists.");
}
// Check if the user is a controller.
if (!issuer.controllers.includes(account.address)) {
throw Error("Cannot create schema, account is not a controller.");
}
return yield new Promise((resolve, reject) => {
api.tx[state_1.CREDENTIALS_PALLET_NAME]
.createSchema(issuerHash, schema.getSchemaObject())
.signAndSend(account, (result) => {
let schemaHash;
result.events.forEach(({ event: { method, data } }) => {
if (method == 'SchemaCreated') {
const jsonData = data.toJSON();
if (jsonData) {
schemaHash = jsonData[0];
}
}
if (method == 'ExtrinsicFailed') {
reject(Error('\nTransaction failed, error creating user.'));
}
});
if (result.status.isInBlock) {
console.log(`\nTransaction finalized at blockHash ${result.status.asInBlock}`);
if (!schemaHash)
throw Error(`Error registering the schema, tx: ${result.status.asInBlock}`);
resolve(`${result.status.asInBlock}`);
}
});
});
});
exports.createSchema = createSchema;
const createAttestationTx = (api, account, issuerHash, schema, attestedTo, values) => __awaiter(void 0, void 0, void 0, function* () {
// Check if issuer exists or not.
// Check if you are the owner, then skip the method.
const issuer = yield (0, state_2.getIssuer)(api, issuerHash);
if (!issuer) {
throw Error("Issuer does not exists.");
}
// Check if the user is a controller.
if (!issuer.controllers.includes(account.address)) {
throw Error("Cannot create attestation, account is not a controller.");
}
return api.tx[state_1.CREDENTIALS_PALLET_NAME]
.attest(issuerHash, schema.getSchemaHash(), attestedTo, values);
});
exports.createAttestationTx = createAttestationTx;
const createAttestation = (api, account, issuerHash, schema, attestedTo, values) => __awaiter(void 0, void 0, void 0, function* () {
// Check if issuer exists or not.
// Check if you are the owner, then skip the method.
const issuer = yield (0, state_2.getIssuer)(api, issuerHash);
if (!issuer) {
throw Error("Issuer does not exists.");
}
// Check if the user is a controller.
if (!issuer.controllers.includes(account.address)) {
throw Error("Cannot create attestation, account is not a controller.");
}
return yield new Promise((resolve, reject) => {
api.tx[state_1.CREDENTIALS_PALLET_NAME]
.attest(issuerHash, schema.getSchemaHash(), attestedTo, values)
.signAndSend(account, (result) => {
if (result.dispatchError) {
if (result.dispatchError.isModule) {
// for module errors, we have the section indexed, lookup
const decoded = api.registry.findMetaError(result.dispatchError.asModule);
const { docs, name, section } = decoded;
console.log(`Dispatch Error: ${section}.${name}: ${docs.join(' ')}`);
reject(Error(`Dispatch Error: ${section}.${name}: ${docs.join(' ')}`));
}
else {
// Other, CannotLookup, BadOrigin, no extra info
console.log('Extras: ', result.dispatchError.toString());
}
}
let attestationId = -1;
if (result.status.isInBlock) {
result.events.forEach(({ event: { method, data } }) => {
if (method == 'AttestationCreated') {
console.log('Attestation Created: InBlock');
const jsonData = data.toJSON();
if (jsonData) {
attestationId = jsonData[3];
}
}
if (method == 'ExtrinsicFailed') {
reject(Error(`Transaction failed, error attesting on-chain for the user. \ntx: ${result.status.hash}`));
}
});
console.log(`\nTransaction finalized at blockHash ${result.status.asInBlock}`);
resolve({
attestationId,
prismUrl: `${network_1.prismUrl}/query/${result.status.asInBlock.toString()}`,
transaction: {
hash: result.status.asInBlock.toString(),
explorerUrl: `https://polkadot.js.org/apps/?rpc=wss%3A%2F%2Framan.truenetwork.io%2Fws#/explorer/query/${result.status.asInBlock.toString()}`,
events: result.events
}
});
}
});
});
});
exports.createAttestation = createAttestation;
const updateAttestation = (api, account, issuerHash, schema, attestedTo, attestationId, newValues) => __awaiter(void 0, void 0, void 0, function* () {
// Check if issuer exists or not.
// Check if you are the owner, then skip the method.
const issuer = yield (0, state_2.getIssuer)(api, issuerHash);
if (!issuer) {
throw Error("Issuer does not exists.");
}
// Check if the user is a controller.
if (!issuer.controllers.includes(account.address)) {
throw Error("Cannot update attestation, account is not a controller.");
}
return yield new Promise((resolve, reject) => {
api.tx[state_1.CREDENTIALS_PALLET_NAME]
.updateAttestation(issuerHash, schema.getSchemaHash(), attestedTo, attestationId, newValues)
.signAndSend(account, (result) => {
if (result.dispatchError) {
if (result.dispatchError.isModule) {
// for module errors, we have the section indexed, lookup
const decoded = api.registry.findMetaError(result.dispatchError.asModule);
const { docs, name, section } = decoded;
console.log(`Dispatch Error: ${section}.${name}: ${docs.join(' ')}`);
reject(Error(`Dispatch Error: ${section}.${name}: ${docs.join(' ')}`));
}
else {
// Other, CannotLookup, BadOrigin, no extra info
console.log('Extras: ', result.dispatchError.toString());
}
}
let attestationId = -1;
if (result.status.isInBlock) {
result.events.forEach(({ event: { method, data } }) => {
if (method == 'AttestationUpdated') {
console.log('Attestation Updated: InBlock');
const jsonData = data.toJSON();
if (jsonData) {
attestationId = jsonData[3];
}
}
if (method == 'ExtrinsicFailed') {
reject(Error(`Transaction failed, error attesting on-chain for the user. \ntx: ${result.status.hash}`));
}
});
console.log(`\nTransaction finalized at blockHash ${result.status.asInBlock}`);
resolve({
attestationId,
prismUrl: `${network_1.prismUrl}/query/${result.status.hash.toString()}`,
transaction: {
hash: result.status.asInBlock.toString(),
explorerUrl: `https://polkadot.js.org/apps/?rpc=wss%3A%2F%2Framan.truenetwork.io%2Fws#/explorer/query/${result.status.hash.toString()}`,
events: result.events
}
});
}
});
});
});
exports.updateAttestation = updateAttestation;