UNPKG

airgap-coin-lib

Version:

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

114 lines 7.6 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 __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); var hex_1 = require("../../../../../utils/hex"); var SCALEDecoder_1 = require("../scale/SCALEDecoder"); var SCALEAccountId_1 = require("../scale/type/SCALEAccountId"); var SCALEBytes_1 = require("../scale/type/SCALEBytes"); var SCALEClass_1 = require("../scale/type/SCALEClass"); var SCALECompactInt_1 = require("../scale/type/SCALECompactInt"); var SCALEEra_1 = require("../scale/type/SCALEEra"); var SCALEHash_1 = require("../scale/type/SCALEHash"); var SubstrateTransactionMethod_1 = require("./method/SubstrateTransactionMethod"); var SubstrateSignature_1 = require("./SubstrateSignature"); var VERSION = 4; var BIT_SIGNED = 128; var BIT_UNSIGNED = 128; // TODO: change to 0 if payment_queryInfo recognizes the transaction, at the moment it returns "No such variant in enum Call" error var SubstrateTransactionType; (function (SubstrateTransactionType) { SubstrateTransactionType[SubstrateTransactionType["TRANSFER"] = 0] = "TRANSFER"; SubstrateTransactionType[SubstrateTransactionType["BOND"] = 1] = "BOND"; SubstrateTransactionType[SubstrateTransactionType["UNBOND"] = 2] = "UNBOND"; SubstrateTransactionType[SubstrateTransactionType["REBOND"] = 3] = "REBOND"; SubstrateTransactionType[SubstrateTransactionType["BOND_EXTRA"] = 4] = "BOND_EXTRA"; SubstrateTransactionType[SubstrateTransactionType["WITHDRAW_UNBONDED"] = 5] = "WITHDRAW_UNBONDED"; SubstrateTransactionType[SubstrateTransactionType["NOMINATE"] = 6] = "NOMINATE"; SubstrateTransactionType[SubstrateTransactionType["CANCEL_NOMINATION"] = 7] = "CANCEL_NOMINATION"; SubstrateTransactionType[SubstrateTransactionType["COLLECT_PAYOUT"] = 8] = "COLLECT_PAYOUT"; SubstrateTransactionType[SubstrateTransactionType["SET_PAYEE"] = 9] = "SET_PAYEE"; SubstrateTransactionType[SubstrateTransactionType["SET_CONTROLLER"] = 10] = "SET_CONTROLLER"; SubstrateTransactionType[SubstrateTransactionType["SUBMIT_BATCH"] = 11] = "SUBMIT_BATCH"; })(SubstrateTransactionType = exports.SubstrateTransactionType || (exports.SubstrateTransactionType = {})); var SubstrateTransaction = /** @class */ (function (_super) { __extends(SubstrateTransaction, _super); function SubstrateTransaction(network, type, signer, signature, era, nonce, tip, method) { var _this = _super.call(this) || this; _this.network = network; _this.type = type; _this.signer = signer; _this.signature = signature; _this.era = era; _this.nonce = nonce; _this.tip = tip; _this.method = method; _this.scaleFields = [_this.signer, _this.signature, _this.era, _this.nonce, _this.tip, _this.method]; return _this; } SubstrateTransaction.create = function (network, type, config) { return new SubstrateTransaction(network, type, SCALEAccountId_1.SCALEAccountId.from(config.from, network), config.signature || SubstrateSignature_1.SubstrateSignature.create(SubstrateSignature_1.SubstrateSignatureType.Ed25519, config.signature), config.era ? SCALEEra_1.SCALEEra.Mortal(config.era) : SCALEEra_1.SCALEEra.Immortal(), SCALECompactInt_1.SCALECompactInt.from(config.nonce), SCALECompactInt_1.SCALECompactInt.from(config.tip), SubstrateTransactionMethod_1.SubstrateTransactionMethod.create(network, type, config.methodId.moduleIndex, config.methodId.callIndex, config.args)); }; SubstrateTransaction.fromTransaction = function (transaction, config) { var _a; return new SubstrateTransaction(transaction.network, transaction.type, config && config.from ? SCALEAccountId_1.SCALEAccountId.from(config.from, transaction.network) : transaction.signer, ((_a = config) === null || _a === void 0 ? void 0 : _a.signature) || transaction.signature, config && config.era ? SCALEEra_1.SCALEEra.Mortal(config.era) : transaction.era, config && config.nonce ? SCALECompactInt_1.SCALECompactInt.from(config.nonce) : transaction.nonce, config && config.tip ? SCALECompactInt_1.SCALECompactInt.from(config.tip) : transaction.tip, config && config.args && config.methodId ? SubstrateTransactionMethod_1.SubstrateTransactionMethod.create(transaction.network, transaction.type, config.methodId.moduleIndex, config.methodId.callIndex, config.args) : transaction.method); }; SubstrateTransaction.decode = function (network, type, raw) { var bytes = SCALEBytes_1.SCALEBytes.decode(hex_1.stripHexPrefix(raw)); var decoder = new SCALEDecoder_1.SCALEDecoder(network, bytes.decoded.bytes.toString('hex')); decoder.decodeNextHash(8); // signed byte var signer = decoder.decodeNextAccountId(); var signature = decoder.decodeNextObject(SubstrateSignature_1.SubstrateSignature.decode); var era = decoder.decodeNextEra(); var nonce = decoder.decodeNextCompactInt(); var tip = decoder.decodeNextCompactInt(); var method = decoder.decodeNextObject(function (network, hex) { return SubstrateTransactionMethod_1.SubstrateTransactionMethod.decode(network, type, hex); }); return { bytesDecoded: bytes.bytesDecoded, decoded: new SubstrateTransaction(network, type, signer.decoded, signature.decoded, era.decoded, nonce.decoded, tip.decoded, method.decoded) }; }; SubstrateTransaction.prototype.toString = function () { return JSON.stringify({ type: SubstrateTransactionType[this.type], signer: this.signer.toString(), signature: JSON.parse(this.signature.toString()), era: JSON.parse(this.era.toString()), nonce: this.nonce.toNumber(), tip: this.tip.toNumber(), method: JSON.parse(this.method.toString()) }, null, 2); }; SubstrateTransaction.prototype.toAirGapTransactions = function () { var airGapTransaction = { from: [this.signer.asAddress()], to: [this.signer.asAddress()], extra: this.type !== SubstrateTransactionType.TRANSFER ? { type: SubstrateTransactionType[this.type] } : undefined, transactionDetails: JSON.parse(this.toString()) }; var parts = this.method.toAirGapTransactionParts(); return parts.length > 0 ? parts.map(function (part) { return Object.assign(airGapTransaction, part); }) : [airGapTransaction]; }; SubstrateTransaction.prototype._encode = function () { var typeEncoded = SCALEHash_1.SCALEHash.from(new Uint8Array([VERSION | (this.signature.isSigned ? BIT_SIGNED : BIT_UNSIGNED)])).encode(); var bytes = Buffer.from(typeEncoded + this.scaleFields.reduce(function (encoded, struct) { return encoded + struct.encode(); }, ''), 'hex'); return SCALEBytes_1.SCALEBytes.from(bytes).encode(); }; return SubstrateTransaction; }(SCALEClass_1.SCALEClass)); exports.SubstrateTransaction = SubstrateTransaction; //# sourceMappingURL=SubstrateTransaction.js.map