credo-near-module
Version:
NEAR Module for Credo SSI Agent
717 lines (716 loc) • 38.1 kB
JavaScript
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
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 __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (g && (g = 0, op[0] && (_ = 0)), _) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.NearLedgerService = void 0;
var core_1 = require("@credo-ts/core");
var NearModuleConfig_1 = require("../NearModuleConfig");
var near_api_js_1 = require("near-api-js");
var NearLedgerService = /** @class */ (function () {
// private abi: any[] = [
// ];
function NearLedgerService(config) {
this.contractAddress = "ajnainc.testnet";
this.networks = { privateKey: config.privateKey };
}
NearLedgerService.prototype.getProviderAndSigner = function (networkName) {
return __awaiter(this, void 0, void 0, function () {
var keyStore, keyPair, connectionConfig, nearConnection, account, contractId, contract;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
keyStore = new near_api_js_1.keyStores.InMemoryKeyStore();
keyPair = near_api_js_1.KeyPair.fromString(this.networks.privateKey);
return [4 /*yield*/, keyStore.setKey("testnet", this.contractAddress, keyPair)];
case 1:
_a.sent();
connectionConfig = {
networkId: networkName,
keyStore: keyStore, // first create a key store
nodeUrl: "https://rpc.testnet.near.org",
walletUrl: "https://testnet.mynearwallet.com/",
helperUrl: "https://helper.testnet.near.org",
explorerUrl: "https://testnet.nearblocks.io",
};
return [4 /*yield*/, (0, near_api_js_1.connect)(connectionConfig)];
case 2:
nearConnection = _a.sent();
return [4 /*yield*/, nearConnection.account("ajnainc.testnet")];
case 3:
account = _a.sent();
contractId = "ajnainc.testnet";
contract = new near_api_js_1.Contract(account, contractId, {
changeMethods: [],
useLocalViewExecution: false,
viewMethods: [
"getSchema",
"registerSchema",
"registerDID",
"getDID",
"updateDID",
"registerCredentialDefinition",
"getCredentialDefinition",
"addApprovedIssuer",
],
});
return [2 /*return*/, { Account: account, SSIContract: contract }];
}
});
});
};
NearLedgerService.prototype.executeDIDOperation = function (operation, identifier, networkName, didDoc, metadata) {
return __awaiter(this, void 0, void 0, function () {
var keyStore, keyPair, connectionConfig, nearConnection, account, transactionResponse, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
keyStore = new near_api_js_1.keyStores.InMemoryKeyStore();
console.log(this.networks.privateKey, "this.networks.privateKey");
keyPair = near_api_js_1.KeyPair.fromString(this.networks.privateKey);
console.log(keyPair, "keyPair");
return [4 /*yield*/, keyStore.setKey("testnet", this.contractAddress, keyPair)];
case 1:
_b.sent();
connectionConfig = {
networkId: "testnet",
keyStore: keyStore, // first create a key store
nodeUrl: "https://rpc.testnet.near.org",
walletUrl: "https://testnet.mynearwallet.com/",
helperUrl: "https://helper.testnet.near.org",
explorerUrl: "https://testnet.nearblocks.io",
};
return [4 /*yield*/, (0, near_api_js_1.connect)(connectionConfig)];
case 2:
nearConnection = _b.sent();
return [4 /*yield*/, nearConnection.account("ajnainc.testnet")];
case 3:
account = _b.sent();
// const contractId = "ajnainc.testnet";
console.log(account);
_a = operation;
switch (_a) {
case "create": return [3 /*break*/, 4];
case "update": return [3 /*break*/, 6];
case "deactivate": return [3 /*break*/, 8];
}
return [3 /*break*/, 9];
case 4:
console.log("Creating DID", identifier, didDoc, metadata);
return [4 /*yield*/, account.functionCall({
contractId: "ajnainc.testnet",
methodName: "registerDID",
args: {
did: identifier,
context: didDoc,
metadata: metadata || "",
},
})];
case 5:
// const data = await account.functionCall({
// contractId,
// methodName: "registerSchema",
// args: {
// schemaId: "schema:test123",
// details: "Test Context",
// },
// });
// console.log(data);
// const view = await account.viewFunction({
// contractId,
// methodName: "getSchema",
// args: {
// schemaId: "schema:test123",
// },
// });
// transactionResponse = await contract.registerDID(
// identifier,
// didDoc,
// metadata || ""
// );
transactionResponse = _b.sent();
return [3 /*break*/, 10];
case 6: return [4 /*yield*/, account.functionCall({
contractId: this.contractAddress,
methodName: "updateDID",
args: {
did: identifier,
context: didDoc,
metadata: metadata || "",
},
})];
case 7:
transactionResponse = _b.sent();
// transactionResponse = await contract.updateDID(
// identifier,
// didDoc,
// metadata || ""
// );
return [3 /*break*/, 10];
case 8:
//@TODO
// transactionResponse = await contract.deactivateDID(identifier);
return [3 /*break*/, 10];
case 9: throw new Error("Invalid operation: ".concat(operation));
case 10: return [2 /*return*/, transactionResponse === null || transactionResponse === void 0 ? void 0 : transactionResponse.receipts_outcome];
}
});
});
};
NearLedgerService.prototype.getDIDDocument = function (did_1) {
return __awaiter(this, arguments, void 0, function (did, networkName) {
var keyStore, keyPair, connectionConfig, nearConnection, account, contractId, contract, document, didDoc, metadata, didDocument, json;
if (networkName === void 0) { networkName = "testnet"; }
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
keyStore = new near_api_js_1.keyStores.InMemoryKeyStore();
keyPair = near_api_js_1.KeyPair.fromString(this.networks.privateKey);
return [4 /*yield*/, keyStore.setKey("testnet", this.contractAddress, keyPair)];
case 1:
_a.sent();
connectionConfig = {
networkId: networkName,
keyStore: keyStore, // first create a key store
nodeUrl: "https://rpc.testnet.near.org",
walletUrl: "https://testnet.mynearwallet.com/",
helperUrl: "https://helper.testnet.near.org",
explorerUrl: "https://testnet.nearblocks.io",
};
return [4 /*yield*/, (0, near_api_js_1.connect)(connectionConfig)];
case 2:
nearConnection = _a.sent();
return [4 /*yield*/, nearConnection.account("ajnainc.testnet")];
case 3:
account = _a.sent();
contractId = "ajnainc.testnet";
contract = new near_api_js_1.Contract(account, contractId, {
changeMethods: [],
useLocalViewExecution: false,
viewMethods: [
"getSchema",
"registerSchema",
"registerDID",
"getDID",
"updateDID",
"registerCredentialDefinition",
"getCredentialDefinition",
"addApprovedIssuer",
],
});
return [4 /*yield*/, account.viewFunction({
contractId: this.contractAddress,
methodName: "getDID",
args: {
did: did,
},
})];
case 4:
document = _a.sent();
console.log(document, "document");
if (!document) {
return [2 /*return*/, undefined];
}
console.log(document, "document");
didDoc = document[0];
metadata = document[1];
if (didDoc) {
json = JSON.parse(didDoc);
didDocument = new core_1.DidDocument({
id: did,
context: json.context,
service: json.service,
});
}
else {
didDocument = new core_1.DidDocument({
id: did,
context: "",
service: [],
});
}
return [2 /*return*/];
}
});
});
};
NearLedgerService.prototype.getSchema = function (schemaId_1) {
return __awaiter(this, arguments, void 0, function (schemaId, networkName) {
var keyStore, keyPair, connectionConfig, nearConnection, account, contractId, contract, response, Json;
if (networkName === void 0) { networkName = "testnet"; }
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
keyStore = new near_api_js_1.keyStores.InMemoryKeyStore();
keyPair = near_api_js_1.KeyPair.fromString(this.networks.privateKey);
return [4 /*yield*/, keyStore.setKey("testnet", this.contractAddress, keyPair)];
case 1:
_a.sent();
connectionConfig = {
networkId: networkName,
keyStore: keyStore, // first create a key store
nodeUrl: "https://rpc.testnet.near.org",
walletUrl: "https://testnet.mynearwallet.com/",
helperUrl: "https://helper.testnet.near.org",
explorerUrl: "https://testnet.nearblocks.io",
};
return [4 /*yield*/, (0, near_api_js_1.connect)(connectionConfig)];
case 2:
nearConnection = _a.sent();
return [4 /*yield*/, nearConnection.account("ajnainc.testnet")];
case 3:
account = _a.sent();
contractId = "ajnainc.testnet";
contract = new near_api_js_1.Contract(account, contractId, {
changeMethods: [],
useLocalViewExecution: false,
viewMethods: [
"getSchema",
"registerSchema",
"registerDID",
"getDID",
"updateDID",
"registerCredentialDefinition",
"getCredentialDefinition",
"addApprovedIssuer",
],
});
return [4 /*yield*/, account.viewFunction({
contractId: this.contractAddress,
methodName: "getSchema",
args: {
schemaId: schemaId,
},
})];
case 4:
response = _a.sent();
console.log(response, "response");
console.log(JSON.parse(response['schemaDetails']));
try {
Json = JSON.parse(response['schemaDetails']);
return [2 /*return*/, {
schema: {
attrNames: Json.data.attrNames,
name: Json.name,
version: Json.data.version,
issuerId: Json.data.issuerId,
},
schemaId: schemaId,
resolutionMetadata: {},
schemaMetadata: {},
}];
}
catch (e) {
return [2 /*return*/, {
schema: {
attrNames: [],
name: "",
version: "",
issuerId: "",
},
schemaId: schemaId,
resolutionMetadata: {},
schemaMetadata: {},
}];
}
return [2 /*return*/];
}
});
});
};
NearLedgerService.prototype.registerSchema = function (schemaId_1, details_1) {
return __awaiter(this, arguments, void 0, function (schemaId, details, networkName) {
var keyStore, keyPair, connectionConfig, nearConnection, account, contractId, contract, data;
if (networkName === void 0) { networkName = "testnet"; }
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
keyStore = new near_api_js_1.keyStores.InMemoryKeyStore();
keyPair = near_api_js_1.KeyPair.fromString(this.networks.privateKey);
return [4 /*yield*/, keyStore.setKey("testnet", this.contractAddress, keyPair)];
case 1:
_a.sent();
connectionConfig = {
networkId: networkName,
keyStore: keyStore, // first create a key store
nodeUrl: "https://rpc.testnet.near.org",
walletUrl: "https://testnet.mynearwallet.com/",
helperUrl: "https://helper.testnet.near.org",
explorerUrl: "https://testnet.nearblocks.io",
};
return [4 /*yield*/, (0, near_api_js_1.connect)(connectionConfig)];
case 2:
nearConnection = _a.sent();
return [4 /*yield*/, nearConnection.account("ajnainc.testnet")];
case 3:
account = _a.sent();
contractId = "ajnainc.testnet";
contract = new near_api_js_1.Contract(account, contractId, {
changeMethods: [],
useLocalViewExecution: false,
viewMethods: [
"getSchema",
"registerSchema",
"registerDID",
"getDID",
"updateDID",
"registerCredentialDefinition",
"getCredentialDefinition",
"addApprovedIssuer",
],
});
return [4 /*yield*/, account.functionCall({
contractId: this.contractAddress,
methodName: "registerSchema",
args: {
schemaId: schemaId,
details: details,
},
})];
case 4:
data = _a.sent();
return [2 /*return*/, data];
}
});
});
};
NearLedgerService.prototype.addApprovedIssuer = function (schemaId_1, issuer_1) {
return __awaiter(this, arguments, void 0, function (schemaId, issuer, networkName) {
var keyStore, keyPair, connectionConfig, nearConnection, account, contractId, contract, data;
if (networkName === void 0) { networkName = "testnet"; }
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
keyStore = new near_api_js_1.keyStores.InMemoryKeyStore();
keyPair = near_api_js_1.KeyPair.fromString(this.networks.privateKey);
return [4 /*yield*/, keyStore.setKey("testnet", this.contractAddress, keyPair)];
case 1:
_a.sent();
connectionConfig = {
networkId: networkName,
keyStore: keyStore, // first create a key store
nodeUrl: "https://rpc.testnet.near.org",
walletUrl: "https://testnet.mynearwallet.com/",
helperUrl: "https://helper.testnet.near.org",
explorerUrl: "https://testnet.nearblocks.io",
};
return [4 /*yield*/, (0, near_api_js_1.connect)(connectionConfig)];
case 2:
nearConnection = _a.sent();
return [4 /*yield*/, nearConnection.account("ajnainc.testnet")];
case 3:
account = _a.sent();
contractId = "ajnainc.testnet";
contract = new near_api_js_1.Contract(account, contractId, {
changeMethods: [],
useLocalViewExecution: false,
viewMethods: [
"getSchema",
"registerSchema",
"registerDID",
"getDID",
"updateDID",
"registerCredentialDefinition",
"getCredentialDefinition",
"addApprovedIssuer",
],
});
return [4 /*yield*/, account.functionCall({
contractId: this.contractAddress,
methodName: "addApprovedIssuer",
args: {
schemaId: schemaId,
issuer: issuer,
},
})];
case 4:
data = _a.sent();
return [2 /*return*/, data];
}
});
});
};
NearLedgerService.prototype.registerCredentialDefinition = function (credDefId_1, schemaId_1, issuer_1) {
return __awaiter(this, arguments, void 0, function (credDefId, schemaId, issuer, networkName) {
var keyStore, keyPair, connectionConfig, nearConnection, account, contractId, contract, data;
if (networkName === void 0) { networkName = "testnet"; }
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
keyStore = new near_api_js_1.keyStores.InMemoryKeyStore();
keyPair = near_api_js_1.KeyPair.fromString(this.networks.privateKey);
return [4 /*yield*/, keyStore.setKey("testnet", this.contractAddress, keyPair)];
case 1:
_a.sent();
connectionConfig = {
networkId: networkName,
keyStore: keyStore, // first create a key store
nodeUrl: "https://rpc.testnet.near.org",
walletUrl: "https://testnet.mynearwallet.com/",
helperUrl: "https://helper.testnet.near.org",
explorerUrl: "https://testnet.nearblocks.io",
};
return [4 /*yield*/, (0, near_api_js_1.connect)(connectionConfig)];
case 2:
nearConnection = _a.sent();
return [4 /*yield*/, nearConnection.account("ajnainc.testnet")];
case 3:
account = _a.sent();
contractId = "ajnainc.testnet";
contract = new near_api_js_1.Contract(account, contractId, {
changeMethods: [],
useLocalViewExecution: false,
viewMethods: [
"getSchema",
"registerSchema",
"registerDID",
"getDID",
"updateDID",
"registerCredentialDefinition",
"getCredentialDefinition",
"addApprovedIssuer",
],
});
return [4 /*yield*/, account.functionCall({
contractId: this.contractAddress,
methodName: "registerCredentialDefinition",
args: {
credDefId: credDefId,
schemaId: schemaId,
issuer: issuer,
},
})];
case 4:
data = _a.sent();
return [2 /*return*/, data];
}
});
});
};
NearLedgerService.prototype.getCredentialDefinition = function (credDefId_1) {
return __awaiter(this, arguments, void 0, function (credDefId, networkName) {
var keyStore, keyPair, connectionConfig, nearConnection, account, contractId, contract, response, Json;
if (networkName === void 0) { networkName = "testnet"; }
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
keyStore = new near_api_js_1.keyStores.InMemoryKeyStore();
keyPair = near_api_js_1.KeyPair.fromString(this.networks.privateKey);
return [4 /*yield*/, keyStore.setKey("testnet", this.contractAddress, keyPair)];
case 1:
_a.sent();
connectionConfig = {
networkId: networkName,
keyStore: keyStore, // first create a key store
nodeUrl: "https://rpc.testnet.near.org",
walletUrl: "https://testnet.mynearwallet.com/",
helperUrl: "https://helper.testnet.near.org",
explorerUrl: "https://testnet.nearblocks.io",
};
return [4 /*yield*/, (0, near_api_js_1.connect)(connectionConfig)];
case 2:
nearConnection = _a.sent();
return [4 /*yield*/, nearConnection.account("ajnainc.testnet")];
case 3:
account = _a.sent();
contractId = "ajnainc.testnet";
contract = new near_api_js_1.Contract(account, contractId, {
changeMethods: [],
useLocalViewExecution: false,
viewMethods: [
"getSchema",
"registerSchema",
"registerDID",
"getDID",
"updateDID",
"registerCredentialDefinition",
"getCredentialDefinition",
"addApprovedIssuer",
],
});
return [4 /*yield*/, account.viewFunction({
contractId: this.contractAddress,
methodName: "getCredentialDefinition",
args: {
credDefId: credDefId,
},
})];
case 4:
response = _a.sent();
try {
Json = JSON.parse(response[0]);
return [2 /*return*/, {
schemaId: Json.schemaId,
issuer: Json.issuer,
credDefId: credDefId,
}];
}
catch (e) {
return [2 /*return*/, {
schemaId: "",
issuer: "",
credDefId: credDefId,
}];
}
return [2 /*return*/];
}
});
});
};
NearLedgerService.prototype.issueCredential = function (credId_1, credDefId_1, issuer_1, subject_1, issuanceDate_1, expiryDate_1, metadata_1) {
return __awaiter(this, arguments, void 0, function (credId, credDefId, issuer, subject, issuanceDate, expiryDate, metadata, networkName) {
if (networkName === void 0) { networkName = "testnet"; }
return __generator(this, function (_a) {
return [2 /*return*/];
});
});
};
NearLedgerService.prototype.revokeCredential = function (credId_1) {
return __awaiter(this, arguments, void 0, function (credId, networkName) {
if (networkName === void 0) { networkName = "testnet"; }
return __generator(this, function (_a) {
return [2 /*return*/];
});
});
};
NearLedgerService.prototype.isCredentialRevoked = function (credId_1) {
return __awaiter(this, arguments, void 0, function (credId, networkName) {
if (networkName === void 0) { networkName = "testnet"; }
return __generator(this, function (_a) {
return [2 /*return*/];
});
});
};
NearLedgerService.prototype.getDID = function (did_1) {
return __awaiter(this, arguments, void 0, function (did, networkName) {
var _a, Account, SSIContract, document;
if (networkName === void 0) { networkName = "testnet"; }
return __generator(this, function (_b) {
switch (_b.label) {
case 0: return [4 /*yield*/, this.getProviderAndSigner(networkName)];
case 1:
_a = _b.sent(), Account = _a.Account, SSIContract = _a.SSIContract;
return [4 /*yield*/, Account.viewFunction({
contractId: this.contractAddress,
methodName: "getDID",
args: {
did: did,
},
})];
case 2:
document = _b.sent();
console.log(document, "document");
if (!document) {
return [2 /*return*/, undefined];
}
return [2 /*return*/];
}
});
});
};
NearLedgerService.prototype.registerDID = function (did_1, context_1, metadata_1) {
return __awaiter(this, arguments, void 0, function (did, context, metadata, networkName) {
var keyStore, keyPair, connectionConfig, nearConnection, account, contractId, contract, data;
if (networkName === void 0) { networkName = "testnet"; }
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
keyStore = new near_api_js_1.keyStores.InMemoryKeyStore();
keyPair = near_api_js_1.KeyPair.fromString(this.networks.privateKey);
return [4 /*yield*/, keyStore.setKey("testnet", this.contractAddress, keyPair)];
case 1:
_a.sent();
connectionConfig = {
networkId: networkName,
keyStore: keyStore, // first create a key store
nodeUrl: "https://rpc.testnet.near.org",
walletUrl: "https://testnet.mynearwallet.com/",
helperUrl: "https://helper.testnet.near.org",
explorerUrl: "https://testnet.nearblocks.io",
};
return [4 /*yield*/, (0, near_api_js_1.connect)(connectionConfig)];
case 2:
nearConnection = _a.sent();
return [4 /*yield*/, nearConnection.account("ajnainc.testnet")];
case 3:
account = _a.sent();
contractId = "ajnainc.testnet";
contract = new near_api_js_1.Contract(account, contractId, {
changeMethods: [],
useLocalViewExecution: false,
viewMethods: [
"getSchema",
"registerSchema",
"registerDID",
"getDID",
"updateDID",
"registerCredentialDefinition",
"getCredentialDefinition",
"addApprovedIssuer",
],
});
return [4 /*yield*/, account.functionCall({
contractId: this.contractAddress,
methodName: "registerDID",
args: {
did: did,
context: context,
metadata: metadata,
},
})];
case 4:
data = _a.sent();
return [2 /*return*/, data];
}
});
});
};
NearLedgerService = __decorate([
(0, core_1.injectable)(),
__metadata("design:paramtypes", [NearModuleConfig_1.NearModuleConfig])
], NearLedgerService);
return NearLedgerService;
}());
exports.NearLedgerService = NearLedgerService;