pcs-js-eos
Version:
Client Api for PCS protocol
295 lines (247 loc) • 8.61 kB
JavaScript
;
exports.__esModule = true;
exports.default = void 0;
var _eosjsEcc = _interopRequireDefault(require("eosjs-ecc"));
var _scatterx = require("scatterx");
var _config = require("./util/config");
var _server = _interopRequireDefault(require("./util/server"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
var PcsSignature =
/*#__PURE__*/
function (_ScatterEOSX) {
_inheritsLoose(PcsSignature, _ScatterEOSX);
function PcsSignature(_ref, symbol, contractName) {
var _this;
var protocol = _ref.protocol,
host = _ref.host,
port = _ref.port,
chainId = _ref.chainId;
if (contractName === void 0) {
contractName = _config.CONTRACT_ACCOUNT;
}
_this = _ScatterEOSX.call(this, {
protocol: protocol,
host: host,
port: port,
chainId: chainId
}) || this;
_this.symbol = symbol;
_this.contractName = contractName;
return _this;
}
/**
* Generate Subsig key pair from nftId & password
* @param {number} nftId nft id
* @param {string} passWord password
*/
var _proto = PcsSignature.prototype;
_proto.genKeyPair =
/*#__PURE__*/
function () {
var _genKeyPair = _asyncToGenerator(
/*#__PURE__*/
regeneratorRuntime.mark(function _callee(nftId, passWord) {
var symbol, salt, privateKey, publicKey, response;
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
symbol = this.symbol;
_context.next = 3;
return _server.default.genSalt(passWord, symbol, nftId);
case 3:
salt = _context.sent;
// generate secure salt using our server
privateKey = _eosjsEcc.default.seedPrivate(passWord + salt); // e.g. 5K2YUVmWfxbmvsNxCsfvArXdGXm7d5DC9pn4yD75k2UaSYgkXTh
publicKey = _eosjsEcc.default.privateToPublic(privateKey); // e.g. EOS5cYvx6NBYNdcJUym9WydRRs6329UTzJgzKii8dESmw2ZaA4fEH
response = {
"privateKey": privateKey,
"publicKey": publicKey
};
return _context.abrupt("return", response);
case 8:
case "end":
return _context.stop();
}
}
}, _callee, this);
}));
function genKeyPair(_x, _x2) {
return _genKeyPair.apply(this, arguments);
}
return genKeyPair;
}()
/**
* Load ID and Subsig Private Key in Session Storage
*/
;
_proto.getSessionAuth = function getSessionAuth() {
var session = JSON.parse(sessionStorage.getItem(this.symbol));
try {
var id = session.id;
var privateKey = session.privateKey;
return {
id: id,
privateKey: privateKey
};
} catch (error) {
throw new ReferenceError(this.symbol + " authority is not found in sessionStorage.");
}
}
/**
* Load ID and Subsig Signature in Local Storage
*/
;
_proto.getLocalAuth = function getLocalAuth() {
var local = JSON.parse(localStorage.getItem(this.symbol));
try {
var id = local.id;
var signature = local.signature;
return {
id: id,
signature: signature
};
} catch (error) {
throw new ReferenceError(this.symbol + " authority is not found in localStorage.");
}
}
/**
* store ID and Subsig Private Key in Session Storage
* @param {number} id
* @param {string} privateKey
*/
;
_proto.setSessionAuth = function setSessionAuth(id, privateKey) {
var authority = {
id: Number(id),
privateKey: privateKey
};
sessionStorage.setItem(this.symbol, JSON.stringify(authority));
}
/**
* store ID and Subsig Signature in Local Storage
* @param {number} id
* @param {string} signature
*/
;
_proto.setLocalAuth = function setLocalAuth(id, signature) {
var authority = {
id: Number(id),
signature: signature
};
localStorage.setItem(this.symbol, JSON.stringify(authority));
}
/**
* fetch token's subsig public key and owner account name from EOS contract table
* @param {number} nftId nft id
*/
;
_proto.getEOSAuth =
/*#__PURE__*/
function () {
var _getEOSAuth = _asyncToGenerator(
/*#__PURE__*/
regeneratorRuntime.mark(function _callee2(nftId) {
var query, response, rows, owner, subkey, result;
return regeneratorRuntime.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
query = {
"code": this.contractName,
"scope": this.symbol,
"table": "token",
"lower_bound": nftId,
"limit": 1
};
_context2.next = 3;
return this.fetchTable(query);
case 3:
response = _context2.sent;
rows = response.rows;
if (rows.length) {
_context2.next = 7;
break;
}
throw new ReferenceError("token with id not found");
case 7:
owner = rows[0].owner;
subkey = rows[0].subkey;
result = {
"account": owner,
"subkey": subkey
};
return _context2.abrupt("return", result);
case 11:
case "end":
return _context2.stop();
}
}
}, _callee2, this);
}));
function getEOSAuth(_x3) {
return _getEOSAuth.apply(this, arguments);
}
return getEOSAuth;
}()
/**
* check authority is valid by our server
* @param {number} tokenId nft id
* @param {string} signature subsig signature valid only on the day
*/
;
_proto.verifyAuth =
/*#__PURE__*/
function () {
var _verifyAuth = _asyncToGenerator(
/*#__PURE__*/
regeneratorRuntime.mark(function _callee3(tokenId, signature) {
var eosApiNode, verified;
return regeneratorRuntime.wrap(function _callee3$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
// verify signature by our server
eosApiNode = this.network.protocol + "://" + this.network.host + ":" + this.network.port;
_context3.next = 3;
return _server.default.checkByPCSSecurity(this.symbol, tokenId, signature, this.contractName, eosApiNode);
case 3:
verified = _context3.sent;
return _context3.abrupt("return", verified === "verified");
case 5:
case "end":
return _context3.stop();
}
}
}, _callee3, this);
}));
function verifyAuth(_x4, _x5) {
return _verifyAuth.apply(this, arguments);
}
return verifyAuth;
}()
/**
* generate signature with private key and message from getSubSigMessage()
* @param {string} privateKey private key e.g. 5JVCJaKMoarMBYWXZFamajznjfkDSARGAx1fSWbPmBDGTM82z6A
*/
;
PcsSignature.genSig = function genSig(privateKey) {
var message = PcsSignature.getSubSigMessage();
var signature = _eosjsEcc.default.sign(message, privateKey);
return signature;
}
/**
* generate plain text to sign by subsig private key
*/
;
PcsSignature.getSubSigMessage = function getSubSigMessage() {
var a_day = 24 * 60 * 60 * 1000;
var message = String(Math.floor(Number(new Date()) / a_day) * a_day);
return message;
};
return PcsSignature;
}(_scatterx.ScatterEOSX);
exports.default = PcsSignature;