@helium/transactions
Version:
Construct and serialize Helium blockchain transaction primatives
57 lines • 2.17 kB
JavaScript
"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 Transaction_1 = __importDefault(require("./Transaction"));
const utils_1 = require("./utils");
class PaymentV1 extends Transaction_1.default {
constructor(opts) {
super();
this.type = 'payment_v1';
this.payer = opts.payer;
this.payee = opts.payee;
this.amount = opts.amount;
this.nonce = opts.nonce;
if (opts.fee !== undefined) {
this.fee = opts.fee;
}
else {
this.fee = 0;
this.fee = this.calculateFee();
}
}
serialize() {
const Txn = proto_1.default.helium.blockchain_txn;
const payment = this.toProto();
const txn = Txn.create({ payment });
return Txn.encode(txn).finish();
}
async sign({ payer: payerKeypair }) {
const Payment = proto_1.default.helium.blockchain_txn_payment_v1;
const payment = this.toProto(true);
const serialized = Payment.encode(payment).finish();
const signature = await payerKeypair.sign(serialized);
this.signature = signature;
return this;
}
toProto(forSigning = false) {
const Payment = proto_1.default.helium.blockchain_txn_payment_v1;
return Payment.create({
payer: this.payer ? (0, utils_1.toUint8Array)(this.payer.bin) : null,
payee: this.payee ? (0, utils_1.toUint8Array)(this.payee.bin) : null,
amount: this.amount,
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 = PaymentV1;
//# sourceMappingURL=PaymentV1.js.map