UNPKG

jcc-stream-utils

Version:

Toolkit of crossing chain from Stream chain to SWTC chain

256 lines 10.3 kB
"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 __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { 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) : new P(function (resolve) { resolve(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 bignumber_js_1 = require("bignumber.js"); var stmWallet = require("jcc_wallet/lib/stm"); var stm_lib_1 = require("stm-lib"); var validator_1 = require("./validator"); /** * Toolkit of Stream * * @class StreamFingate */ var StreamFingate = /** @class */ (function () { function StreamFingate(server) { this.version = "0.1.1"; this._remote = null; this._trace = false; this._trusted = true; this._localSign = true; this._ping = 10; this._server = null; this._currency = "STM"; this._native = "STM"; this._decimals = 6; this._server = server; } Object.defineProperty(StreamFingate.prototype, "trace", { set: function (v) { this._trace = v; }, enumerable: true, configurable: true }); Object.defineProperty(StreamFingate.prototype, "trusted", { set: function (v) { this._trusted = v; }, enumerable: true, configurable: true }); Object.defineProperty(StreamFingate.prototype, "localSign", { set: function (v) { this._localSign = v; }, enumerable: true, configurable: true }); Object.defineProperty(StreamFingate.prototype, "ping", { set: function (v) { this._ping = v; }, enumerable: true, configurable: true }); Object.defineProperty(StreamFingate.prototype, "currency", { set: function (v) { this._currency = v; }, enumerable: true, configurable: true }); Object.defineProperty(StreamFingate.prototype, "remote", { get: function () { return this._remote; }, enumerable: true, configurable: true }); /** * validate stream address is valid or not. * * @static * @param {string} address stream address * @returns {boolean} return true if valid * @memberof StreamFingate */ StreamFingate.isValidAddress = function (address) { return stmWallet.isValidAddress(address); }; /** * validate stream secret is valid or not * * @static * @param {string} secret stream secret * @returns {boolean} return true if valid * @memberof StreamFingate */ StreamFingate.isValidSecret = function (secret) { return stmWallet.isValidSecret(secret); }; /** * retrive address with secret * * @static * @param {string} secret stream secret * @returns {(string | null)} return stream address if secret is valid, otherwise return null * @memberof StreamFingate */ StreamFingate.getAddress = function (secret) { return stmWallet.getAddress(secret); }; /** * init instance of _remote * * @returns {StreamFingate} * @memberof StreamFingate */ StreamFingate.prototype.init = function () { var _server = { local_signing: this._localSign, ping: this._ping, servers: [this._server], trace: this._trace, trusted: this._trusted }; this._remote = new stm_lib_1.Remote(_server, true); return this; }; /** * connect to stream network * * @returns {StreamFingate} * @memberof StreamFingate */ StreamFingate.prototype.connect = function () { this._remote.connect(); return this; }; /** * disconnect from stream network * * @returns {StreamFingate} * @memberof StreamFingate */ StreamFingate.prototype.disconnect = function () { this._remote.disconnect(); return this; }; /** * request balance of STM * * @param {string} address * @returns {Promise<string>} * @memberof StreamFingate */ StreamFingate.prototype.getBalance = function (address) { var _this = this; return new Promise(function (resolve, reject) { _this._remote.requestAccountInfo({ account: address }, function (err, res) { if (err) { return reject(err); } var bn = new bignumber_js_1.default(res.account_data.Balance); var balance = bn.dividedBy(Math.pow(10, _this._decimals)); return resolve(balance.toString(10)); }); }); }; /** * transfer token to address of stream fingate * * @param {string} secret stream secret * @param {string} destination address of stream fingate * @param {string} value transfer amount * @param {IMemo} memo transfer memo * @returns {Promise<string>} resolve hash if success * @memberof StreamFingate */ StreamFingate.prototype.transfer = function (secret, destination, value, memo) { return __awaiter(this, void 0, void 0, function () { var _this = this; return __generator(this, function (_a) { return [2 /*return*/, new Promise(function (resolve, reject) { var address = StreamFingate.getAddress(secret); var amount; if (_this._currency === _this._native) { amount = new bignumber_js_1.default(value).multipliedBy(Math.pow(10, _this._decimals)); amount = amount.toString(10); } else { return reject(new Error("Only support transfer STM for now.")); } _this._remote.setSecret(address, secret); var transaction = _this._remote.createTransaction("Payment", { account: address, amount: amount, destination: destination }); transaction.addMemo(encodeURI("memo"), encodeURI(JSON.stringify(memo))); transaction.lastLedger(_this._remote._ledger_current_index + 3); transaction.submit(function (error, raw) { if (error) { return reject(error); } if (raw.engine_result === "tesSUCCESS") { return resolve(raw.tx_json.hash); } return reject(new Error(raw.engine_result_message)); }); })]; }); }); }; __decorate([ validator_1.validate, __param(0, validator_1.isValidStreamSecret), __param(1, validator_1.isValidStreamAddress), __param(2, validator_1.isValidAmount), __param(3, validator_1.isValidMemo) ], StreamFingate.prototype, "transfer", null); return StreamFingate; }()); exports.default = StreamFingate; //# sourceMappingURL=streamFingate.js.map