@truenetworkio/sdk
Version:
True Network SDK is the abstracted interface for interacting with True Network nodes.
86 lines (85 loc) • 4.2 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.getAttestationsFromSchemaHash = exports.getAttestationForSchema = exports.getSchemaFromHash = exports.checkIfSchemaExist = exports.CREDENTIALS_PALLET_NAME = void 0;
const utils_1 = require("../../utils");
exports.CREDENTIALS_PALLET_NAME = 'credentialsModule';
const checkIfSchemaExist = (api, schemaHash) => __awaiter(void 0, void 0, void 0, function* () {
const response = yield api.query[exports.CREDENTIALS_PALLET_NAME].schemas(schemaHash);
const data = response.toJSON();
if (!data || data.length == 0)
return false;
return true;
});
exports.checkIfSchemaExist = checkIfSchemaExist;
const getSchemaFromHash = (api, schemaHash) => __awaiter(void 0, void 0, void 0, function* () {
const response = yield api.query[exports.CREDENTIALS_PALLET_NAME].schemas(schemaHash);
const data = response.toHuman();
if (!data || data.length == 0)
return;
return data;
});
exports.getSchemaFromHash = getSchemaFromHash;
const getAttestationForSchema = (api, account, issuerHash, schema) => __awaiter(void 0, void 0, void 0, function* () {
const walletWithType = (0, utils_1.getWalletWithType)(account);
const response = yield api.query[exports.CREDENTIALS_PALLET_NAME].attestations(walletWithType, issuerHash, schema.getSchemaHash());
const data = response.toHuman();
if (!data || data.length == 0)
return [];
return data;
});
exports.getAttestationForSchema = getAttestationForSchema;
const getAttestationsFromSchemaHash = (api, account, issuerHash, schemaHash) => __awaiter(void 0, void 0, void 0, function* () {
const walletWithType = (0, utils_1.getWalletWithType)(account);
const response = yield api.query[exports.CREDENTIALS_PALLET_NAME].attestations(walletWithType, issuerHash, schemaHash);
const data = response.toJSON();
if (!data || data.length == 0)
return [];
return data;
});
exports.getAttestationsFromSchemaHash = getAttestationsFromSchemaHash;
// export const getAttestation = async (api: ApiPromise, account: any, issuerHash: string, schema: Schema<any>): Promise<(string | number)[] | undefined> => {
// const response = await api.query[CREDENTIALS_PALLET_NAME].attestations(account, issuerHash, schema.getSchemaHash());
// const data = (response.toJSON() as any)
// if (!data || data.length == 0) return;
// const cred: (string | number)[] = []
// console.log('data values', data)
// data.forEach((i: string, index: number) => {
// schema.getSortedEntries()
// })
// // Get
// data.forEach((i: string) => {
// const value = parseFloat(convertHexToString(i))
// if (Number.isNaN(value)) {
// cred.push(convertHexToString(i))
// } else {
// cred.push(value)
// }
// })
// return cred
// }
// TODO: need to implement this later.
// export const getAllAttestations = async (api: ApiPromise, account: string): Promise<SchemaObject | undefined> => {
// const response = await api.query[CREDENTIALS_PALLET_NAME].attestations(account);
// const data = (response.toJSON() as any)
// if (!data || data.length == 0) return;
// const schema: SchemaObject = []
// data.forEach((i: any[]) => {
// const key = convertHexToString(i[0])
// const schemaType = stringToSchemaType(i[1])
// if (!schemaType) throw Error("Invalid schema type coming from nodes.")
// schema.push({
// key: key,
// type: schemaType
// })
// })
// return schema
// }