UNPKG

@helium/transactions

Version:

Construct and serialize Helium blockchain transaction primatives

102 lines 4.36 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const proto_1 = __importDefault(require("@helium/proto")); const long_1 = __importDefault(require("long")); const Transaction_1 = __importDefault(require("./Transaction")); const utils_1 = require("./utils"); class PaymentV2 extends Transaction_1.default { constructor(opts) { super(); this.type = 'payment_v2'; this.payer = opts.payer; this.payments = opts.payments || []; this.nonce = opts.nonce; if (opts.fee !== undefined) { this.fee = opts.fee; } else { this.fee = 0; this.fee = this.calculateFee(); } this.signature = opts.signature; } serialize() { const BlockchainTxn = proto_1.default.helium.blockchain_txn; const paymentV2 = this.toProto(); const txn = BlockchainTxn.create({ paymentV2 }); return BlockchainTxn.encode(txn).finish(); } static fromString(serializedTxnString) { var _a, _b, _c, _d, _e; const buf = Buffer.from(serializedTxnString, 'base64'); const decoded = proto_1.default.helium.blockchain_txn.decode(buf); const payer = (0, utils_1.toAddressable)((_a = decoded.paymentV2) === null || _a === void 0 ? void 0 : _a.payer); const payments = (((_b = decoded.paymentV2) === null || _b === void 0 ? void 0 : _b.payments) || []).map((p) => { var _a; return ({ payee: (0, utils_1.toAddressable)(p.payee), amount: (0, utils_1.toNumber)(p.amount), memo: (0, utils_1.toString)(p.memo), tokenType: (0, utils_1.toTicker)((0, utils_1.toNumber)(p.tokenType)), max: (_a = p.max) !== null && _a !== void 0 ? _a : undefined, }); }); const fee = (0, utils_1.toNumber)((_c = decoded.paymentV2) === null || _c === void 0 ? void 0 : _c.fee); const nonce = (0, utils_1.toNumber)((_d = decoded.paymentV2) === null || _d === void 0 ? void 0 : _d.nonce); const signature = (0, utils_1.toUint8Array)((_e = decoded.paymentV2) === null || _e === void 0 ? void 0 : _e.signature); return new PaymentV2({ payer, payments, fee, nonce, signature, }); } async sign({ payer: payerKeypair }) { const PaymentTxn = proto_1.default.helium.blockchain_txn_payment_v2; const payment = this.toProto(true); const serialized = PaymentTxn.encode(payment).finish(); this.signature = await payerKeypair.sign(serialized); return this; } toProto(forSigning = false) { const PaymentTxn = proto_1.default.helium.blockchain_txn_payment_v2; const Payment = proto_1.default.helium.payment; const payments = this.payments.map(({ payee, amount, memo, tokenType, max }) => { const memoBuffer = memo ? Buffer.from(memo, 'base64') : undefined; let memoValue; if (memoBuffer) { memoValue = long_1.default.fromBytes(Array.from(memoBuffer), true, true); } const payment = { payee: (0, utils_1.toUint8Array)(payee.bin), memo: memoValue, tokenType: (0, utils_1.toTokenType)({ ticker: tokenType, defaultToUndefined: true }), }; if (max) { payment.max = true; } else { payment.amount = amount; } return Payment.create(payment); }); return PaymentTxn.create({ payer: this.payer ? (0, utils_1.toUint8Array)(this.payer.bin) : null, payments, fee: this.fee && this.fee > 0 ? this.fee : null, nonce: this.nonce, signature: this.signature && !forSigning ? (0, utils_1.toUint8Array)(this.signature) : null, }); } calculateFee() { this.signature = utils_1.EMPTY_SIGNATURE; const payload = this.serialize(); return Transaction_1.default.calculateFee(payload); } } exports.default = PaymentV2; //# sourceMappingURL=PaymentV2.js.map