@airgap/aeternity
Version:
The @airgap/aeternity is an Aeternity implementation of the ICoinProtocol interface from @airgap/coinlib-core.
732 lines • 36.8 kB
JavaScript
"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 (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
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 };
}
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AeternityProtocol = void 0;
var index_1 = __importDefault(require("@airgap/coinlib-core/dependencies/src/axios-0.19.0/index"));
var bignumber_1 = __importDefault(require("@airgap/coinlib-core/dependencies/src/bignumber.js-9.0.0/bignumber"));
var index_2 = require("@airgap/coinlib-core/dependencies/src/bip39-2.5.0/index");
var bs58check = __importStar(require("@airgap/coinlib-core/dependencies/src/bs58check-2.1.2/index"));
var index_3 = require("@airgap/coinlib-core/dependencies/src/hd-wallet-js-b216450e56954a6e82ace0aade9474673de5d9d5/src/index");
var rlp = __importStar(require("@airgap/coinlib-core/dependencies/src/rlp-2.2.3/index"));
var errors_1 = require("@airgap/coinlib-core/errors");
var coinlib_error_1 = require("@airgap/coinlib-core/errors/coinlib-error");
var NonExtendedProtocol_1 = require("@airgap/coinlib-core/protocols/NonExtendedProtocol");
var base64Check_1 = __importDefault(require("@airgap/coinlib-core/utils/base64Check"));
var hex_1 = require("@airgap/coinlib-core/utils/hex");
var ProtocolSymbols_1 = require("@airgap/coinlib-core/utils/ProtocolSymbols");
var ed25519_1 = require("@stablelib/ed25519");
var AeternityAddress_1 = require("./AeternityAddress");
var AeternityCryptoClient_1 = require("./AeternityCryptoClient");
var AeternityProtocolOptions_1 = require("./AeternityProtocolOptions");
var AeternityProtocol = /** @class */ (function (_super) {
__extends(AeternityProtocol, _super);
function AeternityProtocol(options) {
if (options === void 0) { options = new AeternityProtocolOptions_1.AeternityProtocolOptions(); }
var _this = _super.call(this) || this;
_this.options = options;
_this.symbol = 'AE';
_this.name = 'æternity';
_this.marketSymbol = 'ae';
_this.feeSymbol = 'ae';
_this.decimals = 18;
_this.feeDecimals = 18;
_this.identifier = ProtocolSymbols_1.MainProtocolSymbols.AE;
_this.feeDefaults = {
low: '0.00021',
medium: '0.000315',
high: '0.00084'
};
_this.units = [
{
unitSymbol: 'AE',
factor: '1'
}
];
_this.supportsHD = false;
_this.standardDerivationPath = "m/44h/457h/0h/0h/0h";
_this.addressIsCaseSensitive = true;
_this.addressValidationPattern = '^ak_+[1-9A-Za-z]{38,50}$';
_this.addressPlaceholder = 'ak_abc...';
// ae specifics
_this.defaultNetworkId = 'ae_mainnet';
_this.feesURL = 'https://api-airgap.gke.papers.tech/fees';
_this.cryptoClient = new AeternityCryptoClient_1.AeternityCryptoClient();
return _this;
}
AeternityProtocol.prototype.getSymbol = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.symbol];
});
});
};
AeternityProtocol.prototype.getName = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.name];
});
});
};
AeternityProtocol.prototype.getMarketSymbol = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.marketSymbol];
});
});
};
AeternityProtocol.prototype.getAssetSymbol = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, undefined];
});
});
};
AeternityProtocol.prototype.getFeeSymbol = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.feeSymbol];
});
});
};
AeternityProtocol.prototype.getDecimals = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.decimals];
});
});
};
AeternityProtocol.prototype.getFeeDecimals = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.feeDecimals];
});
});
};
AeternityProtocol.prototype.getIdentifier = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.identifier];
});
});
};
AeternityProtocol.prototype.getFeeDefaults = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.feeDefaults];
});
});
};
AeternityProtocol.prototype.getUnits = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.units];
});
});
};
AeternityProtocol.prototype.getSupportsHD = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.supportsHD];
});
});
};
AeternityProtocol.prototype.getStandardDerivationPath = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.standardDerivationPath];
});
});
};
AeternityProtocol.prototype.getAddressIsCaseSensitive = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.addressIsCaseSensitive];
});
});
};
AeternityProtocol.prototype.getAddressValidationPattern = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.addressValidationPattern];
});
});
};
AeternityProtocol.prototype.getAddressPlaceholder = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.addressPlaceholder];
});
});
};
AeternityProtocol.prototype.getOptions = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.options];
});
});
};
AeternityProtocol.prototype.getBlockExplorerLinkForAddress = function (address) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.options.network.blockExplorer.getAddressLink(address)];
});
});
};
AeternityProtocol.prototype.getBlockExplorerLinkForTxId = function (txId) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.options.network.blockExplorer.getTransactionLink(txId)];
});
});
};
AeternityProtocol.prototype.getPublicKeyFromMnemonic = function (mnemonic, derivationPath, password) {
return __awaiter(this, void 0, void 0, function () {
var secret;
return __generator(this, function (_a) {
secret = (0, index_2.mnemonicToSeed)(mnemonic, password);
return [2 /*return*/, this.getPublicKeyFromHexSecret(secret, derivationPath)];
});
});
};
AeternityProtocol.prototype.getPrivateKeyFromMnemonic = function (mnemonic, derivationPath, password) {
return __awaiter(this, void 0, void 0, function () {
var secret;
return __generator(this, function (_a) {
secret = (0, index_2.mnemonicToSeed)(mnemonic, password);
return [2 /*return*/, this.getPrivateKeyFromHexSecret(secret, derivationPath)];
});
});
};
/**
* Returns the PublicKey as String, derived from a supplied hex-string
* @param secret HEX-Secret from BIP39
* @param derivationPath DerivationPath for Key
*/
AeternityProtocol.prototype.getPublicKeyFromHexSecret = function (secret, derivationPath) {
return __awaiter(this, void 0, void 0, function () {
var publicKey;
return __generator(this, function (_a) {
publicKey = (0, index_3.generateWalletUsingDerivationPath)(Buffer.from(secret, 'hex'), derivationPath).publicKey;
return [2 /*return*/, Buffer.from(publicKey).toString('hex')];
});
});
};
/**
* Returns the PrivateKey as Buffer, derived from a supplied hex-string
* @param secret HEX-Secret from BIP39
* @param derivationPath DerivationPath for Key
*/
AeternityProtocol.prototype.getPrivateKeyFromHexSecret = function (secret, derivationPath) {
return __awaiter(this, void 0, void 0, function () {
var secretKey;
return __generator(this, function (_a) {
secretKey = (0, index_3.generateWalletUsingDerivationPath)(Buffer.from(secret, 'hex'), derivationPath).secretKey;
return [2 /*return*/, Buffer.from(secretKey).toString('hex')];
});
});
};
AeternityProtocol.prototype.getAddressFromPublicKey = function (publicKey, cursor) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, {
address: AeternityAddress_1.AeternityAddress.from(publicKey).asString(),
cursor: { hasNext: false }
}];
});
});
};
AeternityProtocol.prototype.getAddressesFromPublicKey = function (publicKey, cursor) {
return __awaiter(this, void 0, void 0, function () {
var address;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getAddressFromPublicKey(publicKey, cursor)];
case 1:
address = _a.sent();
return [2 /*return*/, [address]];
}
});
});
};
AeternityProtocol.prototype.getTransactionsFromPublicKey = function (publicKey, limit, cursor) {
return __awaiter(this, void 0, void 0, function () {
var address;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getAddressFromPublicKey(publicKey)];
case 1:
address = _a.sent();
return [2 /*return*/, this.getTransactionsFromAddresses([address.address], limit, cursor)];
}
});
});
};
AeternityProtocol.prototype.getTransactionsFromAddresses = function (addresses, limit, cursor) {
return __awaiter(this, void 0, void 0, function () {
var groupedTransactions, _a, next, allTransactions, transactions;
var _this = this;
return __generator(this, function (_b) {
switch (_b.label) {
case 0: return [4 /*yield*/, Promise.all(addresses.map(function (address) { return __awaiter(_this, void 0, void 0, function () {
var endpoint, url, response, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
endpoint = cursor === undefined ? "/txs/backward?account=".concat(address, "&limit=").concat(limit) : cursor.next[address];
url = endpoint !== undefined ? "".concat(this.options.network.rpcUrl, "/mdw/").concat(endpoint.replace(/^\/+/, '')) : undefined;
if (!(url !== undefined)) return [3 /*break*/, 2];
return [4 /*yield*/, index_1.default.get(url)];
case 1:
_a = _b.sent();
return [3 /*break*/, 3];
case 2:
_a = undefined;
_b.label = 3;
case 3:
response = _a;
return [2 /*return*/, {
address: address,
data: response === null || response === void 0 ? void 0 : response.data
}];
}
});
}); }))];
case 1:
groupedTransactions = _b.sent();
_a = groupedTransactions.reduce(function (acc, curr) {
var _a;
var _b, _c;
var nextAcc = ((_b = curr.data) === null || _b === void 0 ? void 0 : _b.next) ? Object.assign(acc[0], (_a = {}, _a[curr.address] = curr.data.next, _a)) : acc[0];
var transactionsAcc = acc[1].concat(((_c = curr.data) === null || _c === void 0 ? void 0 : _c.data) || []);
return [nextAcc, transactionsAcc];
}, [{}, []]), next = _a[0], allTransactions = _a[1];
transactions = allTransactions.map(function (obj) {
var parsedTimestamp = parseInt(obj.micro_time, 10);
var airGapTx = {
amount: new bignumber_1.default(obj.tx.amount).toString(10),
fee: new bignumber_1.default(obj.tx.fee).toString(10),
from: [obj.tx.sender_id],
isInbound: addresses.indexOf(obj.tx.recipient_id) !== -1,
protocolIdentifier: _this.identifier,
network: _this.options.network,
to: [obj.tx.recipient_id],
hash: obj.hash,
blockHeight: obj.block_height
};
if (obj.tx.payload) {
airGapTx.data = obj.tx.payload;
}
if (!isNaN(parsedTimestamp)) {
airGapTx.timestamp = Math.round(parsedTimestamp / 1000);
}
return airGapTx;
});
return [2 /*return*/, { transactions: transactions, cursor: { next: next } }];
}
});
});
};
AeternityProtocol.prototype.getPageNumber = function (limit, offset) {
if (limit <= 0 || offset < 0) {
return 1;
}
return 1 + Math.floor(offset / limit); // We need +1 here because pages start at 1
};
AeternityProtocol.prototype.signWithPrivateKey = function (privateKey, transaction) {
return __awaiter(this, void 0, void 0, function () {
var rawTx, signature, txObj, txArray, rlpEncodedTx, signedEncodedTx;
return __generator(this, function (_a) {
rawTx = this.decodeTx(transaction.transaction);
signature = (0, ed25519_1.sign)(Buffer.from(privateKey, 'hex'), Buffer.concat([Buffer.from(transaction.networkId), rawTx]));
txObj = {
tag: (0, hex_1.toHexBuffer)(11),
version: (0, hex_1.toHexBuffer)(1),
signatures: [Buffer.from(signature)],
transaction: rawTx
};
txArray = Object.keys(txObj).map(function (a) { return txObj[a]; });
rlpEncodedTx = rlp.encode(txArray);
signedEncodedTx = "tx_".concat(base64Check_1.default.encode(rlpEncodedTx));
return [2 /*return*/, signedEncodedTx];
});
});
};
AeternityProtocol.prototype.decodeTx = function (transaction) {
var rawTx;
try {
rawTx = base64Check_1.default.decode(transaction.replace('tx_', ''));
return rawTx;
}
catch (error) {
//
}
try {
rawTx = bs58check.decode(transaction.replace('tx_', ''));
return rawTx;
}
catch (error) {
//
}
throw new errors_1.InvalidValueError(coinlib_error_1.Domain.AETERNITY, 'invalid TX-encoding');
};
AeternityProtocol.prototype.getTransactionDetails = function (unsignedTx) {
return __awaiter(this, void 0, void 0, function () {
var transaction, rlpEncodedTx, rlpDecodedTx, fromAddress, toAddress, airgapTx;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
transaction = unsignedTx.transaction.transaction;
rlpEncodedTx = this.decodeTx(transaction);
rlpDecodedTx = rlp.decode(rlpEncodedTx, false);
return [4 /*yield*/, this.getAddressFromPublicKey(rlpDecodedTx[2].slice(1).toString('hex'))];
case 1:
fromAddress = _a.sent();
return [4 /*yield*/, this.getAddressFromPublicKey(rlpDecodedTx[3].slice(1).toString('hex'))];
case 2:
toAddress = _a.sent();
airgapTx = {
amount: new bignumber_1.default(parseInt(rlpDecodedTx[4].toString('hex'), 16)).toString(10),
fee: new bignumber_1.default(parseInt(rlpDecodedTx[5].toString('hex'), 16)).toString(10),
from: [fromAddress.address],
isInbound: false,
protocolIdentifier: this.identifier,
network: this.options.network,
to: [toAddress.address],
data: (rlpDecodedTx[8] || '').toString('utf8'),
transactionDetails: unsignedTx.transaction
};
return [2 /*return*/, [airgapTx]];
}
});
});
};
AeternityProtocol.prototype.getTransactionDetailsFromSigned = function (signedTx) {
return __awaiter(this, void 0, void 0, function () {
var rlpEncodedTx, rlpDecodedTx, unsignedAeternityTransaction;
return __generator(this, function (_a) {
rlpEncodedTx = this.decodeTx(signedTx.transaction);
rlpDecodedTx = rlp.decode(rlpEncodedTx, false);
unsignedAeternityTransaction = {
publicKey: '',
transaction: {
networkId: 'ae_mainnet',
transaction: "tx_".concat(base64Check_1.default.encode(rlpDecodedTx[3]))
}
};
return [2 /*return*/, this.getTransactionDetails(unsignedAeternityTransaction)];
});
});
};
AeternityProtocol.prototype.getBalanceOfAddresses = function (addresses) {
return __awaiter(this, void 0, void 0, function () {
var balance, _i, addresses_1, address, data, error_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
balance = new bignumber_1.default(0);
_i = 0, addresses_1 = addresses;
_a.label = 1;
case 1:
if (!(_i < addresses_1.length)) return [3 /*break*/, 6];
address = addresses_1[_i];
_a.label = 2;
case 2:
_a.trys.push([2, 4, , 5]);
return [4 /*yield*/, index_1.default.get("".concat(this.options.network.rpcUrl, "/v2/accounts/").concat(address))];
case 3:
data = (_a.sent()).data;
balance = balance.plus(new bignumber_1.default(data.balance));
return [3 /*break*/, 5];
case 4:
error_1 = _a.sent();
// if node returns 404 (which means 'no account found'), go with 0 balance
if (error_1.response && error_1.response.status !== 404) {
throw new errors_1.NetworkError(coinlib_error_1.Domain.AETERNITY, error_1);
}
return [3 /*break*/, 5];
case 5:
_i++;
return [3 /*break*/, 1];
case 6: return [2 /*return*/, balance.toString(10)];
}
});
});
};
AeternityProtocol.prototype.getBalanceOfPublicKey = function (publicKey) {
return __awaiter(this, void 0, void 0, function () {
var address;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getAddressFromPublicKey(publicKey)];
case 1:
address = _a.sent();
return [2 /*return*/, this.getBalanceOfAddresses([address.address])];
}
});
});
};
AeternityProtocol.prototype.getBalanceOfPublicKeyForSubProtocols = function (publicKey, subProtocols) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
throw Promise.reject('get balance of sub protocols not supported');
});
});
};
AeternityProtocol.prototype.getAvailableBalanceOfAddresses = function (addresses) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.getBalanceOfAddresses(addresses)];
});
});
};
AeternityProtocol.prototype.estimateMaxTransactionValueFromPublicKey = function (publicKey, recipients, fee) {
return __awaiter(this, void 0, void 0, function () {
var balance, balanceWrapper, maxFee, estimatedFeeDefaults, amountWithoutFees;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getBalanceOfPublicKey(publicKey)];
case 1:
balance = _a.sent();
balanceWrapper = new bignumber_1.default(balance);
if (!(fee !== undefined)) return [3 /*break*/, 2];
maxFee = new bignumber_1.default(fee);
return [3 /*break*/, 4];
case 2: return [4 /*yield*/, this.estimateFeeDefaultsFromPublicKey(publicKey, recipients, [balance])];
case 3:
estimatedFeeDefaults = _a.sent();
maxFee = new bignumber_1.default(estimatedFeeDefaults.medium).shiftedBy(this.decimals);
if (maxFee.gte(balanceWrapper)) {
maxFee = new bignumber_1.default(0);
}
_a.label = 4;
case 4:
amountWithoutFees = balanceWrapper.minus(maxFee);
if (amountWithoutFees.isNegative()) {
amountWithoutFees = new bignumber_1.default(0);
}
return [2 /*return*/, amountWithoutFees.toFixed()];
}
});
});
};
AeternityProtocol.prototype.estimateFeeDefaultsFromPublicKey = function (publicKey, recipients, values, data) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, index_1.default.get(this.feesURL)];
case 1: return [2 /*return*/, (_a.sent()).data];
}
});
});
};
AeternityProtocol.prototype.prepareTransactionFromPublicKey = function (publicKey, recipients, values, fee, data) {
return __awaiter(this, void 0, void 0, function () {
var nonce, address, accountResponse, error_2, balance, _a, sender, recipient, txObj, txArray, rlpEncodedTx, preparedTx;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
nonce = 1;
return [4 /*yield*/, this.getAddressFromPublicKey(publicKey)];
case 1:
address = _b.sent();
_b.label = 2;
case 2:
_b.trys.push([2, 4, , 5]);
return [4 /*yield*/, index_1.default.get("".concat(this.options.network.rpcUrl, "/v2/accounts/").concat(address.address))];
case 3:
accountResponse = (_b.sent()).data;
nonce = accountResponse.nonce + 1;
return [3 /*break*/, 5];
case 4:
error_2 = _b.sent();
// if node returns 404 (which means 'no account found'), go with nonce 0
if (error_2.response && error_2.response.status !== 404) {
throw new errors_1.NetworkError(coinlib_error_1.Domain.AETERNITY, error_2);
}
return [3 /*break*/, 5];
case 5:
_a = bignumber_1.default.bind;
return [4 /*yield*/, this.getBalanceOfPublicKey(publicKey)];
case 6:
balance = new (_a.apply(bignumber_1.default, [void 0, _b.sent()]))();
if (balance.isLessThan(fee)) {
throw new errors_1.BalanceError(coinlib_error_1.Domain.AETERNITY, 'not enough balance');
}
sender = publicKey;
recipient = bs58check.decode(recipients[0].replace('ak_', ''));
txObj = {
tag: (0, hex_1.toHexBuffer)(12),
version: (0, hex_1.toHexBuffer)(1),
sender_id: Buffer.concat([(0, hex_1.toHexBuffer)(1), Buffer.from(sender, 'hex')]),
recipient_id: Buffer.concat([(0, hex_1.toHexBuffer)(1), recipient]),
amount: (0, hex_1.toHexBuffer)(new bignumber_1.default(values[0])),
fee: (0, hex_1.toHexBuffer)(new bignumber_1.default(fee)),
ttl: (0, hex_1.toHexBuffer)(0),
nonce: (0, hex_1.toHexBuffer)(nonce),
payload: Buffer.from((data === null || data === void 0 ? void 0 : data.payload) || '')
};
txArray = Object.keys(txObj).map(function (a) { return txObj[a]; });
rlpEncodedTx = rlp.encode(txArray);
preparedTx = "tx_".concat(base64Check_1.default.encode(rlpEncodedTx));
return [2 /*return*/, {
transaction: preparedTx,
networkId: this.defaultNetworkId
}];
}
});
});
};
/**
* This is a function that we only use to fix incompatibilitis with old vault versions that are unable to understand b64 encoded Txs.
*
* @deprecated
* @param preparedTx
*/
AeternityProtocol.prototype.convertTxToBase58 = function (preparedTx) {
return {
transaction: bs58check.encode(base64Check_1.default.decode(preparedTx.transaction)),
networkId: preparedTx.networkId
};
};
AeternityProtocol.prototype.broadcastTransaction = function (rawTransaction) {
return __awaiter(this, void 0, void 0, function () {
var data;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, index_1.default.post("".concat(this.options.network.rpcUrl, "/v2/transactions"), { tx: rawTransaction }, { headers: { 'Content-Type': 'application/json' } })];
case 1:
data = (_a.sent()).data;
return [2 /*return*/, data.tx_hash];
}
});
});
};
AeternityProtocol.prototype.signMessage = function (message, keypair) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.cryptoClient.signMessage(message, keypair)];
});
});
};
AeternityProtocol.prototype.verifyMessage = function (message, signature, publicKey) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.cryptoClient.verifyMessage(message, signature, publicKey)];
});
});
};
AeternityProtocol.prototype.encryptAsymmetric = function (message, publicKey) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.cryptoClient.encryptAsymmetric(message, publicKey)];
});
});
};
AeternityProtocol.prototype.decryptAsymmetric = function (message, keypair) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.cryptoClient.decryptAsymmetric(message, keypair)];
});
});
};
AeternityProtocol.prototype.encryptAES = function (message, privateKey) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.cryptoClient.encryptAES(message, privateKey)];
});
});
};
AeternityProtocol.prototype.decryptAES = function (message, privateKey) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.cryptoClient.decryptAES(message, privateKey)];
});
});
};
AeternityProtocol.prototype.getTransactionStatuses = function (transactionHashes) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, Promise.reject('Transaction status not implemented')];
});
});
};
return AeternityProtocol;
}(NonExtendedProtocol_1.NonExtendedProtocol));
exports.AeternityProtocol = AeternityProtocol;
//# sourceMappingURL=AeternityProtocol.js.map