jcc-call-utils
Version:
Toolkit of crossing chain from Call chain to SWTC chain
310 lines • 11.8 kB
JavaScript
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 callWallet = require("jcc_wallet/lib/call");
var validator_1 = require("./validator");
var call;
/* istanbul ignore if */
if (global["Window"]) { // tslint:disable-line
call = require("./call-for-browser"); // tslint:disable-line
}
else {
call = require("call-lib"); // tslint:disable-line
}
var CallAPI = call.CallAPI;
/**
* call fingate
*
* @export
* @class CallFingate
*/
var CallFingate = /** @class */ (function () {
function CallFingate(node) {
this._remote = null;
this._remote = new CallAPI({
server: node
});
}
/**
* create call wallet
*
* @static
* @returns {IWallet}
* @memberof CallFingate
*/
CallFingate.createWallet = function () {
return callWallet.createWallet();
};
/**
* check call address is valid or not
*
* @static
* @param {string} address
* @returns {boolean} return true if valid
* @memberof CallFingate
*/
CallFingate.isValidAddress = function (address) {
return callWallet.isValidAddress(address);
};
/**
* check call secret is valid or not
*
* @static
* @param {string} secret
* @returns {boolean} return true if valid
* @memberof CallFingate
*/
CallFingate.isValidSecret = function (secret) {
return callWallet.isValidSecret(secret);
};
/**
* retrive address with secret
*
* @static
* @param {string} secret
* @returns {(string | null)} return address if valid, otherwise return null
* @memberof CallFingate
*/
CallFingate.getAddress = function (secret) {
return callWallet.getAddress(secret);
};
Object.defineProperty(CallFingate.prototype, "remote", {
get: function () {
return this._remote;
},
enumerable: true,
configurable: true
});
/**
* connect to call node server
*
* @returns
* @memberof CallFingate
*/
CallFingate.prototype.connect = function () {
return __awaiter(this, void 0, void 0, function () {
var _this = this;
return __generator(this, function (_a) {
return [2 /*return*/, new Promise(function (resolve, reject) {
_this._remote.connect().then(function () {
return resolve();
}).catch(function (error) {
return reject(error);
});
})];
});
});
};
/**
* check if connected to call node server
*
* @returns {boolean}
* @memberof CallFingate return true if connected
*/
CallFingate.prototype.isConnected = function () {
return this._remote.isConnected();
};
/**
* disconnect from call node server
*
* @memberof CallFingate
*/
CallFingate.prototype.disconnect = function () {
if (this._remote) {
this._remote.disconnect();
}
};
/**
* get call balance
*
* @param {string} address
* @returns {Promise<string>}
* @memberof CallFingate
*/
CallFingate.prototype.getCallBalance = function (address) {
return __awaiter(this, void 0, void 0, function () {
var balances, balance, error_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 2, , 3]);
return [4 /*yield*/, this._remote.getBalances(address)];
case 1:
balances = _a.sent();
balance = balances.find(function (b) { return b.currency.toUpperCase() === "CALL"; });
return [2 /*return*/, balance.value];
case 2:
error_1 = _a.sent();
throw error_1;
case 3: return [2 /*return*/];
}
});
});
};
/**
* sign payment data
*
* @param {string} txJSON
* @param {string} secret
* @returns {ISignature}
* @memberof CallFingate
*/
CallFingate.prototype.sign = function (txJSON, secret) {
try {
return this._remote.sign(txJSON, secret);
}
catch (error) {
throw error;
}
};
CallFingate.prototype.preparePayment = function (address, payment) {
var _this = this;
return new Promise(function (resolve, reject) {
_this._remote.preparePayment(address, payment).then(function (prepared) {
return resolve(prepared);
}).catch(function (error) {
return reject(error);
});
});
};
CallFingate.prototype.submit = function (signedTransaction) {
var _this = this;
return new Promise(function (resolve, reject) {
_this._remote.submit(signedTransaction, true).then(function (result) {
return resolve(result);
}).catch(function (error) {
return reject(error);
});
});
};
/**
* format payment data
*
* @param {string} from call address
* @param {string} destination call address
* @param {number} amount amount
* @param {string} memo memo
* @returns {IPayment}
* @memberof CallFingate
*/
CallFingate.prototype.formatPayment = function (from, destination, amount, memo) {
var payment = {
destination: {
address: destination,
amount: {
currency: "CALL",
value: amount
}
},
memos: [{
data: memo,
format: "plain/text",
type: "payment"
}],
source: {
address: from,
maxAmount: {
currency: "CALL",
value: amount
}
}
};
return payment;
};
/**
* transfer CALL
*
* @param {string} secret call secret
* @param {string} destination call destination address
* @param {string} amount transfer amount
* @param {IMemo} memo transfer memo
* @returns {Promise<string>} return hash if success
* @memberof CallFingate
*/
CallFingate.prototype.transfer = function (secret, destination, amount, memo) {
return __awaiter(this, void 0, void 0, function () {
var from, payment, prepared, signature, response, error_2;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
from = CallFingate.getAddress(secret);
payment = this.formatPayment(from, destination, new bignumber_js_1.default(amount).toString(10), JSON.stringify(memo));
_a.label = 1;
case 1:
_a.trys.push([1, 5, , 6]);
return [4 /*yield*/, this.preparePayment(from, payment)];
case 2:
prepared = _a.sent();
return [4 /*yield*/, this.sign(prepared.tx_json, secret)];
case 3:
signature = _a.sent();
return [4 /*yield*/, this.submit(signature)];
case 4:
response = _a.sent();
if (response.resultCode === "tesSUCCESS") {
return [2 /*return*/, signature.id];
}
else {
throw new Error(response.resultMessage);
}
return [3 /*break*/, 6];
case 5:
error_2 = _a.sent();
throw error_2;
case 6: return [2 /*return*/];
}
});
});
};
__decorate([
validator_1.validate,
__param(0, validator_1.isValidCallSecret), __param(1, validator_1.isValidCallAddress), __param(2, validator_1.isValidAmount), __param(3, validator_1.isValidMemo)
], CallFingate.prototype, "transfer", null);
return CallFingate;
}());
exports.default = CallFingate;
//# sourceMappingURL=callFingate.js.map
;