@helium/transactions
Version:
Construct and serialize Helium blockchain transaction primatives
126 lines • 6.68 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 AssertLocationV2 extends Transaction_1.default {
constructor(opts) {
super();
this.type = 'assert_location_v2';
const { owner, gateway, payer, location, nonce, gain, elevation, stakingFee, fee, ownerSignature, payerSignature, } = opts;
this.owner = owner;
this.gateway = gateway;
this.payer = payer;
this.location = location;
this.nonce = nonce;
this.gain = gain;
this.elevation = elevation;
this.stakingFee = 0;
this.fee = 0;
if (fee !== undefined) {
this.fee = fee;
}
else {
this.fee = this.calculateFee();
}
if (stakingFee !== undefined) {
this.stakingFee = stakingFee;
}
else {
// v2 reuses the v1 staking fee chain var
this.stakingFee = Transaction_1.default.stakingFeeTxnAssertLocationV1;
}
if (ownerSignature)
this.ownerSignature = ownerSignature;
if (payerSignature)
this.payerSignature = payerSignature;
}
serialize() {
const Txn = proto_1.default.helium.blockchain_txn;
const assertLocationV2 = this.toProto();
const txn = Txn.create({ assertLocationV2 });
return Txn.encode(txn).finish();
}
static fromString(serializedTxnString) {
var _a, _b, _c, _d, _e;
const buf = Buffer.from(serializedTxnString, 'base64');
const { assertLocationV2 } = proto_1.default.helium.blockchain_txn.decode(buf);
const owner = ((_a = assertLocationV2 === null || assertLocationV2 === void 0 ? void 0 : assertLocationV2.owner) === null || _a === void 0 ? void 0 : _a.length)
? (0, utils_1.toAddressable)(assertLocationV2 === null || assertLocationV2 === void 0 ? void 0 : assertLocationV2.owner)
: undefined;
const gateway = ((_b = assertLocationV2 === null || assertLocationV2 === void 0 ? void 0 : assertLocationV2.gateway) === null || _b === void 0 ? void 0 : _b.length)
? (0, utils_1.toAddressable)(assertLocationV2 === null || assertLocationV2 === void 0 ? void 0 : assertLocationV2.gateway)
: undefined;
const payer = ((_c = assertLocationV2 === null || assertLocationV2 === void 0 ? void 0 : assertLocationV2.payer) === null || _c === void 0 ? void 0 : _c.length)
? (0, utils_1.toAddressable)(assertLocationV2 === null || assertLocationV2 === void 0 ? void 0 : assertLocationV2.payer)
: undefined;
const location = (assertLocationV2 === null || assertLocationV2 === void 0 ? void 0 : assertLocationV2.location) || undefined;
const nonce = (0, utils_1.toNumber)(assertLocationV2 === null || assertLocationV2 === void 0 ? void 0 : assertLocationV2.nonce);
const gain = (0, utils_1.toNumber)(assertLocationV2 === null || assertLocationV2 === void 0 ? void 0 : assertLocationV2.gain);
const elevation = (0, utils_1.toNumber)(assertLocationV2 === null || assertLocationV2 === void 0 ? void 0 : assertLocationV2.elevation);
const ownerSignature = ((_d = assertLocationV2 === null || assertLocationV2 === void 0 ? void 0 : assertLocationV2.ownerSignature) === null || _d === void 0 ? void 0 : _d.length)
? (0, utils_1.toUint8Array)(assertLocationV2 === null || assertLocationV2 === void 0 ? void 0 : assertLocationV2.ownerSignature)
: undefined;
const payerSignature = ((_e = assertLocationV2 === null || assertLocationV2 === void 0 ? void 0 : assertLocationV2.payerSignature) === null || _e === void 0 ? void 0 : _e.length)
? (0, utils_1.toUint8Array)(assertLocationV2 === null || assertLocationV2 === void 0 ? void 0 : assertLocationV2.payerSignature)
: undefined;
const fee = (0, utils_1.toNumber)(assertLocationV2 === null || assertLocationV2 === void 0 ? void 0 : assertLocationV2.fee);
const stakingFee = (0, utils_1.toNumber)(assertLocationV2 === null || assertLocationV2 === void 0 ? void 0 : assertLocationV2.stakingFee);
return new AssertLocationV2({
owner,
gateway,
payer,
location,
nonce,
gain,
elevation,
fee,
stakingFee,
ownerSignature,
payerSignature,
});
}
async sign(keypairs) {
const AssertLocation = proto_1.default.helium.blockchain_txn_assert_location_v2;
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.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_v2;
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,
location: this.location,
nonce: this.nonce,
gain: this.gain,
elevation: this.elevation && this.elevation > 0 ? this.elevation : null,
fee: this.fee && this.fee > 0 ? this.fee : null,
stakingFee: this.stakingFee && this.stakingFee > 0 ? this.stakingFee : null,
ownerSignature: this.ownerSignature && !forSigning ? (0, utils_1.toUint8Array)(this.ownerSignature) : null,
payerSignature: this.payerSignature && !forSigning ? (0, utils_1.toUint8Array)(this.payerSignature) : null,
});
}
calculateFee() {
this.ownerSignature = 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 = AssertLocationV2;
//# sourceMappingURL=AssertLocationV2.js.map