UNPKG

airgap-coin-lib

Version:

The airgap-coin-lib is a protocol agnostic library to prepare, sign and broadcast cryptocurrency transactions.

280 lines 15.1 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); 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 index_1 = require("../../../dependencies/src/axios-0.19.0/index"); var bignumber_1 = require("../../../dependencies/src/bignumber.js-9.0.0/bignumber"); var ProtocolSymbols_1 = require("../../../utils/ProtocolSymbols"); var ICoinSubProtocol_1 = require("../../ICoinSubProtocol"); var TezosProtocol_1 = require("../TezosProtocol"); var TezosOperationType_1 = require("../types/TezosOperationType"); var TezosKtProtocol = /** @class */ (function (_super) { __extends(TezosKtProtocol, _super); function TezosKtProtocol() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.identifier = ProtocolSymbols_1.SubProtocolSymbols.XTZ_KT; _this.isSubProtocol = true; _this.subProtocolType = ICoinSubProtocol_1.SubProtocolType.ACCOUNT; _this.addressValidationPattern = '^(tz1|KT1)[1-9A-Za-z]{33}$'; _this.migrationFee = new bignumber_1.default(5000); return _this; } TezosKtProtocol.prototype.getAddressFromPublicKey = function (publicKey) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.getAddressesFromPublicKey(publicKey)]; case 1: return [2 /*return*/, (_a.sent())[0]]; } }); }); }; TezosKtProtocol.prototype.getAddressesFromPublicKey = function (publicKey) { return __awaiter(this, void 0, void 0, function () { var tz1address, getRequestBody, data, ktAddresses; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, _super.prototype.getAddressFromPublicKey.call(this, publicKey)]; case 1: tz1address = _a.sent(); getRequestBody = function (field, set) { return { fields: ["originated_contracts"], predicates: [ { field: field, operation: 'eq', set: [tz1address], inverse: false }, { field: 'kind', operation: 'eq', set: [set], inverse: false }, { field: 'status', operation: 'eq', set: ['applied'], inverse: false } ] }; }; return [4 /*yield*/, index_1.default.post(this.baseApiUrl + "/v2/data/tezos/mainnet/operations", getRequestBody('manager_pubkey', 'origination'), { headers: this.headers })]; case 2: data = (_a.sent()).data; ktAddresses = data.map(function (origination) { return origination.originated_contracts; }); return [2 /*return*/, ktAddresses.reverse()]; } }); }); }; TezosKtProtocol.prototype.estimateMaxTransactionValueFromPublicKey = function (publicKey, recipients, fee) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, this.getBalanceOfPublicKey(publicKey)]; }); }); }; TezosKtProtocol.prototype.estimateFeeDefaultsFromPublicKey = function (publicKey, recipients, values, data) { return __awaiter(this, void 0, void 0, function () { var fee; return __generator(this, function (_a) { fee = this.migrationFee.shiftedBy(-this.feeDecimals).toFixed(); return [2 /*return*/, { low: fee, medium: fee, high: fee }]; }); }); }; TezosKtProtocol.prototype.prepareTransactionFromPublicKey = function (_publicKey, _recipients, _values, _fee, _data) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { throw new Error('sending funds from KT addresses is not supported. Please use the migration feature.'); }); }); }; TezosKtProtocol.prototype.originate = function (publicKey, delegate, amount) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { throw new Error('Originate operation not supported for KT Addresses'); }); }); }; TezosKtProtocol.prototype.delegate = function (publicKey, delegate) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { throw new Error('Delegate operation not supported for KT Addresses'); }); }); }; TezosKtProtocol.prototype.migrateKtContract = function (publicKey, destinationContract) { return __awaiter(this, void 0, void 0, function () { var counter, branch, operations, address, balanceOfManager, _a, amount, _b, results, accountManager, _c, _d, error_1, hexDestination, spendOperation, tezosWrappedOperation, binaryTx, error_2; return __generator(this, function (_e) { switch (_e.label) { case 0: counter = new bignumber_1.default(1); operations = []; return [4 /*yield*/, _super.prototype.getAddressFromPublicKey.call(this, publicKey)]; case 1: address = _e.sent(); _a = bignumber_1.default.bind; return [4 /*yield*/, _super.prototype.getBalanceOfAddresses.call(this, [address])]; case 2: balanceOfManager = new (_a.apply(bignumber_1.default, [void 0, _e.sent()]))(); if (balanceOfManager.isLessThan(this.migrationFee)) { throw new Error('not enough balance on tz address for fee'); } _b = bignumber_1.default.bind; return [4 /*yield*/, this.getBalanceOfAddresses([destinationContract])]; case 3: amount = new (_b.apply(bignumber_1.default, [void 0, _e.sent()]))(); _e.label = 4; case 4: _e.trys.push([4, 8, , 9]); return [4 /*yield*/, Promise.all([ index_1.default.get(this.jsonRPCAPI + "/chains/main/blocks/head/context/contracts/" + address + "/counter"), index_1.default.get(this.jsonRPCAPI + "/chains/main/blocks/head/hash"), index_1.default.get(this.jsonRPCAPI + "/chains/main/blocks/head/context/contracts/" + address + "/manager_key") ])]; case 5: results = _e.sent(); counter = new bignumber_1.default(results[0].data).plus(1); branch = results[1].data; accountManager = results[2].data; if (!!accountManager) return [3 /*break*/, 7]; _d = (_c = operations).push; return [4 /*yield*/, this.createRevealOperation(counter, publicKey, address)]; case 6: _d.apply(_c, [_e.sent()]); counter = counter.plus(1); _e.label = 7; case 7: return [3 /*break*/, 9]; case 8: error_1 = _e.sent(); throw error_1; case 9: hexDestination = this.checkAndRemovePrefixToHex(address, this.tezosPrefixes.tz1); if (hexDestination.length > 42) { // must be less or equal 21 bytes throw new Error('provided source is invalid'); } while (hexDestination.length !== 42) { // fill up with 0s to match 21 bytes hexDestination = "0" + hexDestination; } spendOperation = { kind: TezosOperationType_1.TezosOperationType.TRANSACTION, fee: this.migrationFee.toFixed(), gas_limit: '26283', storage_limit: '0', amount: '0', counter: counter.toFixed(), destination: destinationContract, source: address, parameters: { entrypoint: 'do', value: [ { prim: 'DROP' }, { prim: 'NIL', args: [{ prim: 'operation' }] }, { prim: 'PUSH', args: [ { prim: 'key_hash' }, { bytes: hexDestination } ] }, { prim: 'IMPLICIT_ACCOUNT' }, { prim: 'PUSH', args: [{ prim: 'mutez' }, { int: amount.toString(10) }] }, { prim: 'UNIT' }, { prim: 'TRANSFER_TOKENS' }, { prim: 'CONS' } ] } }; operations.push(spendOperation); _e.label = 10; case 10: _e.trys.push([10, 12, , 13]); tezosWrappedOperation = { branch: branch, contents: operations }; return [4 /*yield*/, this.forgeTezosOperation(tezosWrappedOperation)]; case 11: binaryTx = _e.sent(); return [2 /*return*/, { binaryTransaction: binaryTx }]; case 12: error_2 = _e.sent(); console.warn(error_2.message); throw new Error('Forging Tezos TX failed.'); case 13: return [2 /*return*/]; } }); }); }; return TezosKtProtocol; }(TezosProtocol_1.TezosProtocol)); exports.TezosKtProtocol = TezosKtProtocol; //# sourceMappingURL=TezosKtProtocol.js.map