@helium/transactions
Version:
Construct and serialize Helium blockchain transaction primatives
84 lines • 3.95 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
/* eslint-disable consistent-return */
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 TokenBurnV1 extends Transaction_1.default {
constructor(opts) {
super();
this.type = 'token_burn_v1';
this.payer = opts.payer;
this.payee = opts.payee;
this.amount = opts.amount;
this.nonce = opts.nonce;
this.memo = opts.memo;
this.fee = opts.fee === undefined ? this.calculateFee() : opts.fee;
this.signature = opts.signature;
}
serialize() {
const BlockchainTxn = proto_1.default.helium.blockchain_txn;
const tokenBurn = this.toProto();
const txn = BlockchainTxn.create({ tokenBurn });
return BlockchainTxn.encode(txn).finish();
}
message() {
const TokenBurnTxn = proto_1.default.helium.blockchain_txn_token_burn_v1;
const tokenBurn = this.toProto(true);
return TokenBurnTxn.encode(tokenBurn).finish();
}
async sign({ payer: payerKeypair }) {
const serialized = this.message();
this.signature = await payerKeypair.sign(serialized);
return this;
}
toProto(forSigning = false) {
const TokenBurnTxn = proto_1.default.helium.blockchain_txn_token_burn_v1;
const memoBuffer = Buffer.from(this.memo, 'base64');
const memoLong = long_1.default.fromBytes(Array.from(memoBuffer), true, true);
const memoValue = !memoLong.isZero() ? memoLong : undefined;
return TokenBurnTxn.create({
payer: (0, utils_1.toUint8Array)(this.payer.bin),
payee: (0, utils_1.toUint8Array)(this.payee.bin),
amount: this.amount,
nonce: this.nonce,
signature: this.signature && !forSigning ? (0, utils_1.toUint8Array)(this.signature) : null,
fee: this.fee,
memo: memoValue,
});
}
static fromString(serializedTxnString) {
var _a;
const buf = Buffer.from(serializedTxnString, 'base64');
const { tokenBurn } = proto_1.default.helium.blockchain_txn.decode(buf);
const payer = (0, utils_1.toAddressable)(tokenBurn === null || tokenBurn === void 0 ? void 0 : tokenBurn.payer);
const payee = (0, utils_1.toAddressable)(tokenBurn === null || tokenBurn === void 0 ? void 0 : tokenBurn.payee);
const amount = (0, utils_1.toNumber)(tokenBurn === null || tokenBurn === void 0 ? void 0 : tokenBurn.amount) || 0;
const nonce = (0, utils_1.toNumber)(tokenBurn === null || tokenBurn === void 0 ? void 0 : tokenBurn.nonce);
const memo = (0, utils_1.toString)(tokenBurn === null || tokenBurn === void 0 ? void 0 : tokenBurn.memo) || '';
const fee = (0, utils_1.toNumber)(tokenBurn === null || tokenBurn === void 0 ? void 0 : tokenBurn.fee);
const signature = ((_a = tokenBurn === null || tokenBurn === void 0 ? void 0 : tokenBurn.signature) === null || _a === void 0 ? void 0 : _a.length) ? (0, utils_1.toUint8Array)(tokenBurn === null || tokenBurn === void 0 ? void 0 : tokenBurn.signature) : undefined;
if (!payee || !payer || !amount || nonce === undefined)
return;
return new TokenBurnV1({
payer,
payee,
amount,
nonce,
memo,
fee,
signature,
});
}
calculateFee() {
this.signature = utils_1.EMPTY_SIGNATURE;
const payload = this.serialize();
return Transaction_1.default.calculateFee(payload);
}
}
exports.default = TokenBurnV1;
//# sourceMappingURL=TokenBurnV1.js.map