pcs-js-eos
Version:
Client Api for PCS protocol
295 lines (239 loc) • 8.6 kB
JavaScript
;
exports.__esModule = true;
exports.default = void 0;
var _eosjsEcc = _interopRequireDefault(require("eosjs-ecc"));
var _config = require("./config");
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); }); }; }
var PCSServer =
/*#__PURE__*/
function () {
function PCSServer() {}
PCSServer.checkByPCSSecurity =
/*#__PURE__*/
function () {
var _checkByPCSSecurity = _asyncToGenerator(
/*#__PURE__*/
regeneratorRuntime.mark(function _callee(symbol, tokenId, sig, contractName, eosApiNode) {
var pcsUrl, chainId, payload, req, res;
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
pcsUrl = "https://pcs-api.toycash.io/verify";
_context.next = 3;
return getInfo(eosApiNode);
case 3:
chainId = _context.sent;
if (chainId) {
_context.next = 6;
break;
}
throw new RangeError("eosApiNode is invalid. " + eosApiNode);
case 6:
payload = {
"api_url": eosApiNode,
"contract_name": contractName,
"symbol_code": symbol,
"token_id": tokenId,
"sig": sig
};
req = {
method: "POST",
mode: "cors",
cache: "no-cache",
headers: {
"Content-Type": "application/json; charset=utf-8"
},
body: JSON.stringify(payload)
};
_context.next = 10;
return fetch(pcsUrl, req);
case 10:
_context.next = 12;
return _context.sent.text();
case 12:
res = _context.sent;
return _context.abrupt("return", res);
case 14:
case "end":
return _context.stop();
}
}
}, _callee);
}));
function checkByPCSSecurity(_x, _x2, _x3, _x4, _x5) {
return _checkByPCSSecurity.apply(this, arguments);
}
return checkByPCSSecurity;
}();
PCSServer.requestSignTx =
/*#__PURE__*/
function () {
var _requestSignTx = _asyncToGenerator(
/*#__PURE__*/
regeneratorRuntime.mark(function _callee2(query, chainId) {
var apiUrl, options, response, data, signedTx;
return regeneratorRuntime.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
// chainIdがメインネットならメインネット用のAPIエンドポイントを、それ以外ならkylin用のAPIエンドポイントを使う
apiUrl = chainId === _config.EOS_MAIN_ID ? _config.AWS_PCS_API_URL + "/eosagent" : _config.AWS_PCS_KYLIN_API_URL + "/eosagent";
options = {
method: "POST",
body: JSON.stringify(query)
};
_context2.next = 4;
return fetch(apiUrl, options);
case 4:
response = _context2.sent;
_context2.next = 7;
return response.json();
case 7:
data = _context2.sent;
if (!data.errorMessage) {
_context2.next = 10;
break;
}
throw new Error(data.errorMessage);
case 10:
_context2.prev = 10;
signedTx = data.signedTransaction.transaction;
return _context2.abrupt("return", signedTx);
case 15:
_context2.prev = 15;
_context2.t0 = _context2["catch"](10);
console.error(_context2.t0);
case 18:
case "end":
return _context2.stop();
}
}
}, _callee2, null, [[10, 15]]);
}));
function requestSignTx(_x6, _x7) {
return _requestSignTx.apply(this, arguments);
}
return requestSignTx;
}();
PCSServer.genSalt =
/*#__PURE__*/
function () {
var _genSalt = _asyncToGenerator(
/*#__PURE__*/
regeneratorRuntime.mark(function _callee3(password, symbol, nftId) {
var seedHash, payload, req, response, salt;
return regeneratorRuntime.wrap(function _callee3$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
seedHash = _eosjsEcc.default.sha256(password);
payload = {
"hash": seedHash,
"tokenId": nftId,
"symbol": symbol
};
req = {
method: "POST",
mode: "cors",
cache: "no-cache",
headers: {
"Content-Type": "application/json; charset=utf-8"
},
body: JSON.stringify(payload)
};
_context3.next = 5;
return fetch(_config.AWS_KEY_API_URL, req);
case 5:
_context3.next = 7;
return _context3.sent.json();
case 7:
response = _context3.sent;
if (!(Number(response.statusCode) === 403)) {
_context3.next = 12;
break;
}
throw new Error(response.statusCode + " Rate exceeded.");
case 12:
if (!(Number(response.statusCode) !== 200)) {
_context3.next = 14;
break;
}
throw new Error(response.statusCode + " Internal server error.");
case 14:
salt = response.body;
return _context3.abrupt("return", salt);
case 16:
case "end":
return _context3.stop();
}
}
}, _callee3);
}));
function genSalt(_x8, _x9, _x10) {
return _genSalt.apply(this, arguments);
}
return genSalt;
}();
return PCSServer;
}();
exports.default = PCSServer;
function getInfo(_x11) {
return _getInfo.apply(this, arguments);
}
function _getInfo() {
_getInfo = _asyncToGenerator(
/*#__PURE__*/
regeneratorRuntime.mark(function _callee4(eosApiNode) {
var url, response, chainId, validChainList;
return regeneratorRuntime.wrap(function _callee4$(_context4) {
while (1) {
switch (_context4.prev = _context4.next) {
case 0:
url = eosApiNode + "/v1/chain/get_info";
_context4.next = 3;
return fetch(url);
case 3:
response = _context4.sent;
if (!response.ok) {
_context4.next = 21;
break;
}
_context4.next = 7;
return response.json();
case 7:
response = _context4.sent;
if (!("chain_id" in response)) {
_context4.next = 18;
break;
}
chainId = response.chain_id;
validChainList = [_config.EOS_MAIN_ID, _config.EOS_KYLIN_ID];
if (!(validChainList.indexOf(chainId) !== -1)) {
_context4.next = 15;
break;
}
return _context4.abrupt("return", chainId);
case 15:
return _context4.abrupt("return", false);
case 16:
_context4.next = 19;
break;
case 18:
return _context4.abrupt("return", false);
case 19:
_context4.next = 22;
break;
case 21:
return _context4.abrupt("return", false);
case 22:
case "end":
return _context4.stop();
}
}
}, _callee4);
}));
return _getInfo.apply(this, arguments);
}