UNPKG

@helium/transactions

Version:

Construct and serialize Helium blockchain transaction primatives

81 lines 3.63 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 Transaction_1 = __importDefault(require("./Transaction")); const utils_1 = require("./utils"); class TokenRedeemV1 extends Transaction_1.default { constructor(opts) { super(); this.type = 'token_redeem_v1'; this.account = opts.account; this.amount = opts.amount; this.nonce = opts.nonce; this.tokenType = opts.tokenType; if (opts.fee !== undefined) { this.fee = opts.fee; } else { this.fee = 0; this.fee = this.calculateFee(); } this.signature = opts.signature; } serialize() { const Txn = proto_1.default.helium.blockchain_txn; const tokenRedeem = this.toProto(); const txn = Txn.create({ tokenRedeem }); return Txn.encode(txn).finish(); } message() { const TokenRedeem = proto_1.default.helium.blockchain_txn_token_redeem_v1; const tokenRedeem = this.toProto(true); return TokenRedeem.encode(tokenRedeem).finish(); } async sign({ keypair }) { const serialized = this.message(); this.signature = await keypair.sign(serialized); return this; } toProto(forSigning = false) { const TokenRedeem = proto_1.default.helium.blockchain_txn_token_redeem_v1; return TokenRedeem.create({ account: this.account ? (0, utils_1.toUint8Array)(this.account.bin) : null, amount: this.amount, fee: this.fee && this.fee > 0 ? this.fee : null, nonce: this.nonce, tokenType: (0, utils_1.toTokenType)({ ticker: this.tokenType }), signature: this.signature && !forSigning ? (0, utils_1.toUint8Array)(this.signature) : null, }); } static fromString(serializedTxnString) { var _a; const buf = Buffer.from(serializedTxnString, 'base64'); const { tokenRedeem } = proto_1.default.helium.blockchain_txn.decode(buf); const account = (0, utils_1.toAddressable)(tokenRedeem === null || tokenRedeem === void 0 ? void 0 : tokenRedeem.account); const amount = (0, utils_1.toNumber)(tokenRedeem === null || tokenRedeem === void 0 ? void 0 : tokenRedeem.amount) || 0; const tokenType = (0, utils_1.toTicker)((0, utils_1.toNumber)(tokenRedeem === null || tokenRedeem === void 0 ? void 0 : tokenRedeem.tokenType)); const fee = (0, utils_1.toNumber)(tokenRedeem === null || tokenRedeem === void 0 ? void 0 : tokenRedeem.fee); const nonce = (0, utils_1.toNumber)(tokenRedeem === null || tokenRedeem === void 0 ? void 0 : tokenRedeem.nonce); const signature = ((_a = tokenRedeem === null || tokenRedeem === void 0 ? void 0 : tokenRedeem.signature) === null || _a === void 0 ? void 0 : _a.length) ? (0, utils_1.toUint8Array)(tokenRedeem === null || tokenRedeem === void 0 ? void 0 : tokenRedeem.signature) : undefined; return new TokenRedeemV1({ account, amount, tokenType, fee, nonce, signature, }); } calculateFee() { this.signature = utils_1.EMPTY_SIGNATURE; const payload = this.serialize(); return Transaction_1.default.calculateFee(payload); } } exports.default = TokenRedeemV1; //# sourceMappingURL=TokenRedeemV1.js.map