UNPKG

@helium/transactions

Version:

Construct and serialize Helium blockchain transaction primatives

77 lines 3.64 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 TransferHotspotV2 extends Transaction_1.default { constructor(opts) { super(); this.type = 'transfer_hotspot_v2'; this.gateway = opts.gateway; this.owner = opts.owner; this.newOwner = opts.newOwner; this.nonce = opts.nonce; if (opts.fee !== undefined) { this.fee = opts.fee; } else { this.fee = 0; this.fee = this.calculateFee(); } this.ownerSignature = opts.ownerSignature; } serialize() { const BlockchainTxn = proto_1.default.helium.blockchain_txn; const transferHotspotV2 = this.toProto(); const txn = BlockchainTxn.create({ transferHotspotV2 }); return BlockchainTxn.encode(txn).finish(); } static fromString(serializedTxnString) { var _a, _b, _c, _d, _e, _f; const buf = Buffer.from(serializedTxnString, 'base64'); const decoded = proto_1.default.helium.blockchain_txn.decode(buf); const gateway = (0, utils_1.toAddressable)((_a = decoded.transferHotspotV2) === null || _a === void 0 ? void 0 : _a.gateway); const owner = (0, utils_1.toAddressable)((_b = decoded.transferHotspotV2) === null || _b === void 0 ? void 0 : _b.owner); const ownerSignature = (0, utils_1.toUint8Array)((_c = decoded.transferHotspotV2) === null || _c === void 0 ? void 0 : _c.ownerSignature); const newOwner = (0, utils_1.toAddressable)((_d = decoded.transferHotspotV2) === null || _d === void 0 ? void 0 : _d.newOwner); const fee = (0, utils_1.toNumber)((_e = decoded.transferHotspotV2) === null || _e === void 0 ? void 0 : _e.fee); const nonce = (0, utils_1.toNumber)((_f = decoded.transferHotspotV2) === null || _f === void 0 ? void 0 : _f.nonce); return new TransferHotspotV2({ gateway, owner, ownerSignature, newOwner, fee, nonce, }); } async sign({ owner: ownerKeypair }) { const TransferHotspotTxn = proto_1.default.helium.blockchain_txn_transfer_hotspot_v2; const transferHotspot = this.toProto(true); const serialized = TransferHotspotTxn.encode(transferHotspot).finish(); this.ownerSignature = await ownerKeypair.sign(serialized); return this; } toProto(forSigning = false) { const TransferHotspotTxn = proto_1.default.helium.blockchain_txn_transfer_hotspot_v2; return TransferHotspotTxn.create({ gateway: this.gateway ? (0, utils_1.toUint8Array)(this.gateway.bin) : null, owner: this.owner ? (0, utils_1.toUint8Array)(this.owner.bin) : null, ownerSignature: this.ownerSignature && !forSigning ? (0, utils_1.toUint8Array)(this.ownerSignature) : null, newOwner: this.newOwner ? (0, utils_1.toUint8Array)(this.newOwner.bin) : null, fee: this.fee ? this.fee : null, nonce: this.nonce, }); } calculateFee() { this.ownerSignature = utils_1.EMPTY_SIGNATURE; const payload = this.serialize(); return Transaction_1.default.calculateFee(payload); } } exports.default = TransferHotspotV2; //# sourceMappingURL=TransferHotspotV2.js.map