UNPKG

@helium/transactions

Version:

Construct and serialize Helium blockchain transaction primatives

125 lines 6.8 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 AssertLocationV1 extends Transaction_1.default { constructor(opts) { super(); this.type = 'assert_location_v1'; const { owner, gateway, payer, location, nonce, stakingFee, fee, ownerSignature, gatewaySignature, payerSignature, } = opts; this.owner = owner; this.gateway = gateway; this.payer = payer; this.location = location; this.nonce = nonce; this.stakingFee = 0; this.fee = 0; if (fee !== undefined) { this.fee = fee; } else { this.fee = this.calculateFee(); } if (stakingFee !== undefined) { this.stakingFee = stakingFee; } else { this.stakingFee = Transaction_1.default.stakingFeeTxnAssertLocationV1; } if (ownerSignature) this.ownerSignature = ownerSignature; if (gatewaySignature) this.gatewaySignature = gatewaySignature; if (payerSignature) this.payerSignature = payerSignature; } serialize() { const Txn = proto_1.default.helium.blockchain_txn; const assertLocation = this.toProto(); const txn = Txn.create({ assertLocation }); return Txn.encode(txn).finish(); } static fromString(serializedTxnString) { var _a, _b, _c, _d, _e, _f; const buf = Buffer.from(serializedTxnString, 'base64'); const { assertLocation } = proto_1.default.helium.blockchain_txn.decode(buf); const owner = ((_a = assertLocation === null || assertLocation === void 0 ? void 0 : assertLocation.owner) === null || _a === void 0 ? void 0 : _a.length) ? (0, utils_1.toAddressable)(assertLocation === null || assertLocation === void 0 ? void 0 : assertLocation.owner) : undefined; const gateway = ((_b = assertLocation === null || assertLocation === void 0 ? void 0 : assertLocation.gateway) === null || _b === void 0 ? void 0 : _b.length) ? (0, utils_1.toAddressable)(assertLocation === null || assertLocation === void 0 ? void 0 : assertLocation.gateway) : undefined; const payer = ((_c = assertLocation === null || assertLocation === void 0 ? void 0 : assertLocation.payer) === null || _c === void 0 ? void 0 : _c.length) ? (0, utils_1.toAddressable)(assertLocation === null || assertLocation === void 0 ? void 0 : assertLocation.payer) : undefined; const location = (assertLocation === null || assertLocation === void 0 ? void 0 : assertLocation.location) || undefined; const nonce = (0, utils_1.toNumber)(assertLocation === null || assertLocation === void 0 ? void 0 : assertLocation.nonce); const ownerSignature = ((_d = assertLocation === null || assertLocation === void 0 ? void 0 : assertLocation.ownerSignature) === null || _d === void 0 ? void 0 : _d.length) ? (0, utils_1.toUint8Array)(assertLocation === null || assertLocation === void 0 ? void 0 : assertLocation.ownerSignature) : undefined; const gatewaySignature = ((_e = assertLocation === null || assertLocation === void 0 ? void 0 : assertLocation.gatewaySignature) === null || _e === void 0 ? void 0 : _e.length) ? (0, utils_1.toUint8Array)(assertLocation === null || assertLocation === void 0 ? void 0 : assertLocation.gatewaySignature) : undefined; const payerSignature = ((_f = assertLocation === null || assertLocation === void 0 ? void 0 : assertLocation.payerSignature) === null || _f === void 0 ? void 0 : _f.length) ? (0, utils_1.toUint8Array)(assertLocation === null || assertLocation === void 0 ? void 0 : assertLocation.payerSignature) : undefined; const fee = (0, utils_1.toNumber)(assertLocation === null || assertLocation === void 0 ? void 0 : assertLocation.fee); const stakingFee = (0, utils_1.toNumber)(assertLocation === null || assertLocation === void 0 ? void 0 : assertLocation.stakingFee); return new AssertLocationV1({ owner, gateway, payer, location, nonce, fee, stakingFee, ownerSignature, gatewaySignature, payerSignature, }); } async sign(keypairs) { const AssertLocation = proto_1.default.helium.blockchain_txn_assert_location_v1; const assertLocation = this.toProto(true); const serialized = AssertLocation.encode(assertLocation).finish(); if (keypairs.owner) { const signature = await keypairs.owner.sign(serialized); this.ownerSignature = signature; } if (keypairs.gateway) { const signature = await keypairs.gateway.sign(serialized); this.gatewaySignature = signature; } if (keypairs.payer) { const signature = await keypairs.payer.sign(serialized); this.payerSignature = signature; } return this; } toProto(forSigning = false) { const AssertLocation = proto_1.default.helium.blockchain_txn_assert_location_v1; return AssertLocation.create({ owner: this.owner ? (0, utils_1.toUint8Array)(this.owner.bin) : null, gateway: this.gateway ? (0, utils_1.toUint8Array)(this.gateway.bin) : null, payer: this.payer ? (0, utils_1.toUint8Array)(this.payer.bin) : null, ownerSignature: this.ownerSignature && !forSigning ? (0, utils_1.toUint8Array)(this.ownerSignature) : null, gatewaySignature: this.gatewaySignature && !forSigning ? (0, utils_1.toUint8Array)(this.gatewaySignature) : null, payerSignature: this.payerSignature && !forSigning ? (0, utils_1.toUint8Array)(this.payerSignature) : null, stakingFee: this.stakingFee && this.stakingFee > 0 ? this.stakingFee : null, fee: this.fee && this.fee > 0 ? this.fee : null, location: this.location, nonce: this.nonce, }); } calculateFee() { this.ownerSignature = utils_1.EMPTY_SIGNATURE; this.gatewaySignature = utils_1.EMPTY_SIGNATURE; if (this.payer) { this.payerSignature = utils_1.EMPTY_SIGNATURE; } const payload = this.serialize(); return Transaction_1.default.calculateFee(payload); } } exports.default = AssertLocationV1; //# sourceMappingURL=AssertLocationV1.js.map