did-sdk-dev
Version:
SAN did sdk
293 lines • 15.5 kB
JavaScript
"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());
});
};
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 (_) 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 });
var error_1 = require("../error");
var constant_1 = require("../common/constant");
var index_1 = require("../chain/index");
var jsonrpc_1 = require("../rpc/jsonrpc");
var issuer = (function () {
function issuer(SANClient) {
var _this = this;
this.defaultIssuer = function (issuer) {
issuer.id = issuer.id ? issuer.id : '';
issuer.uuid = issuer.uuid ? issuer.uuid : '';
issuer.website = issuer.website ? issuer.website : '';
issuer.endpoint = issuer.endpoint ? issuer.endpoint : '';
issuer.shortDescription = issuer.shortDescription ? issuer.shortDescription : '';
issuer.longDescription = issuer.longDescription ? issuer.longDescription : '';
issuer.serviceType = issuer.serviceType ? issuer.serviceType : '';
issuer.requestData = issuer.requestData ? issuer.requestData : {
basicData: [],
otherData: []
};
issuer.requestData.basicData = issuer.requestData.basicData ? issuer.requestData.basicData : [];
issuer.requestData.otherData = issuer.requestData.otherData ? issuer.requestData.otherData : [];
issuer.createTime = issuer.createTime ? issuer.createTime : '';
issuer.updateTime = issuer.updateTime ? issuer.updateTime : '';
return issuer;
};
this.basicDataToStringArray = function (basicData) {
var res = [];
var item;
for (var i = 0; i < basicData.length; i++) {
if (typeof basicData[i] != 'string') {
item = JSON.stringify(basicData[i]);
}
else {
item = basicData[i].toString();
}
res.push(item);
}
return res;
};
this.basicDataToObjectArray = function (basicData) {
var res = [];
var item;
for (var i = 0; i < basicData.length; i++) {
if (typeof basicData[i] == 'string') {
try {
item = JSON.parse(basicData[i].toString());
}
catch (e) {
item = basicData[i];
}
}
else {
item = basicData[i];
}
res.push(item);
}
return res;
};
this.create = function (issuer, hexPriKey) { return __awaiter(_this, void 0, void 0, function () {
var path, operation, now, timestamp, actions, resp, pushTransactionArgs, rpc, resp2;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!hexPriKey) {
return [2, error_1.build_error(-10001, 'hexPriKey invalid')];
}
path = "/v1/issuers";
operation = "create";
issuer = this.defaultIssuer(issuer);
now = new Date();
timestamp = now.getTime().toString().slice(0, -3);
issuer.createTime = now.toJSON();
issuer.updateTime = now.toJSON();
if (issuer.requestData && issuer.requestData.basicData) {
issuer.requestData.basicData = this.basicDataToStringArray(issuer.requestData.basicData);
}
actions = [];
actions.push({
account: constant_1.constant.chain.contract,
name: 'addissuer',
authorization: [{
actor: this.SANClient.utils.getAccount(issuer.did),
permission: 'active',
}],
data: {
issuer: issuer,
},
});
return [4, this.chain.serialized_transaction(actions)];
case 1:
resp = _a.sent();
if (resp.code != 0) {
return [2, resp];
}
return [4, this.chain.sign(resp.content.chainId, hexPriKey, new Uint8Array(resp.content.serializedTransaction))];
case 2:
pushTransactionArgs = _a.sent();
pushTransactionArgs.serializedTransaction = Array.from(pushTransactionArgs.serializedTransaction);
rpc = new jsonrpc_1.JsonRpc(this.SANClient.serviceEndpoint, { fetch: this.SANClient.fetch });
return [4, rpc.fetch(path, { operation: operation, timestamp: timestamp, pushTransactionArgs: pushTransactionArgs })];
case 3:
resp2 = _a.sent();
if (resp2.code != 0) {
return [2, resp2];
}
delete resp.content.chainId;
delete resp.content.serializedTransaction;
return [2, resp];
}
});
}); };
this.resolve = function (uuid) {
if (uuid === void 0) { uuid = ""; }
return __awaiter(_this, void 0, void 0, function () {
var path, rpc, res, issuers, issuer_1, i;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
path = '/v1/issuers/resolve/' + uuid;
rpc = new jsonrpc_1.JsonRpc(this.SANClient.serviceEndpoint, { fetch: this.SANClient.fetch });
return [4, rpc.fetch(path, null)];
case 1:
res = _a.sent();
if (!res.code && res.content && res.content.issuers) {
issuers = [];
for (i = 0; i < res.content.issuers.length; i++) {
issuer_1 = res.content.issuers[i];
if (issuer_1.requestData && issuer_1.requestData.basicData) {
issuer_1.requestData.basicData = this.basicDataToObjectArray(issuer_1.requestData.basicData);
}
issuers.push(issuer_1);
}
res.content.issuers = issuers;
}
return [2, res];
}
});
});
};
this.update = function (issuer, hexPriKey) { return __awaiter(_this, void 0, void 0, function () {
var path, operation, now, timestamp, actions, resp, pushTransactionArgs, rpc, resp2;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!hexPriKey) {
return [2, error_1.build_error(-10001, 'hexPriKey invalid')];
}
path = "/v1/issuers";
operation = "update";
issuer = this.defaultIssuer(issuer);
now = new Date();
timestamp = now.getTime().toString().slice(0, -3);
issuer.updateTime = now.toJSON();
if (issuer.requestData && issuer.requestData.basicData) {
issuer.requestData.basicData = this.basicDataToStringArray(issuer.requestData.basicData);
}
actions = [];
actions.push({
account: constant_1.constant.chain.contract,
name: 'updateissuer',
authorization: [{
actor: this.SANClient.utils.getAccount(issuer.did),
permission: 'active',
}],
data: {
issuer: issuer,
}
});
return [4, this.chain.serialized_transaction(actions)];
case 1:
resp = _a.sent();
if (resp.code != 0) {
return [2, resp];
}
return [4, this.chain.sign(resp.content.chainId, hexPriKey, new Uint8Array(resp.content.serializedTransaction))];
case 2:
pushTransactionArgs = _a.sent();
pushTransactionArgs.serializedTransaction = Array.from(pushTransactionArgs.serializedTransaction);
rpc = new jsonrpc_1.JsonRpc(this.SANClient.serviceEndpoint, { fetch: this.SANClient.fetch });
return [4, rpc.fetch(path, { operation: operation, timestamp: timestamp, pushTransactionArgs: pushTransactionArgs })];
case 3:
resp2 = _a.sent();
if (resp2.code == 0) {
resp2.content = {
issuer: issuer,
};
}
return [2, resp2];
}
});
}); };
this.revoke = function (did, uuid, hexPriKey) { return __awaiter(_this, void 0, void 0, function () {
var path, operation, now, timestamp, actions, resp, pushTransactionArgs, rpc;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!did) {
return [2, error_1.build_error(-10001, 'did invalid')];
}
if (!uuid) {
return [2, error_1.build_error(-10001, 'uuid invalid')];
}
if (!hexPriKey) {
return [2, error_1.build_error(-10001, 'hexPriKey invalid')];
}
path = '/v1/issuers';
operation = "revoke";
now = new Date();
timestamp = now.getTime().toString().slice(0, -3);
actions = [];
actions.push({
account: constant_1.constant.chain.contract,
name: 'deleteissuer',
authorization: [{
actor: this.SANClient.utils.getAccount(did),
permission: 'owner',
}],
data: {
uuid: uuid,
}
});
return [4, this.chain.serialized_transaction(actions)];
case 1:
resp = _a.sent();
if (resp.code != 0) {
return [2, resp];
}
return [4, this.chain.sign(resp.content.chainId, hexPriKey, new Uint8Array(resp.content.serializedTransaction))];
case 2:
pushTransactionArgs = _a.sent();
pushTransactionArgs.serializedTransaction = Array.from(pushTransactionArgs.serializedTransaction);
rpc = new jsonrpc_1.JsonRpc(this.SANClient.serviceEndpoint, { fetch: this.SANClient.fetch });
return [4, rpc.fetch(path, { operation: operation, timestamp: timestamp, pushTransactionArgs: pushTransactionArgs })];
case 3: return [2, _a.sent()];
}
});
}); };
this.claim = function (rawClaim, creator, hexPriKey) {
if (creator === void 0) { creator = ''; }
if (hexPriKey === void 0) { hexPriKey = ''; }
return __awaiter(_this, void 0, void 0, function () {
return __generator(this, function (_a) {
rawClaim.proof = this.SANClient.utils.proof(rawClaim, creator, hexPriKey);
return [2, { code: 0, message: 'ok', content: { claim: rawClaim } }];
});
});
};
this.SANClient = SANClient;
this.chain = new index_1.chain(SANClient);
}
;
return issuer;
}());
exports.issuer = issuer;
//# sourceMappingURL=index.js.map