@helium/transactions
Version:
Construct and serialize Helium blockchain transaction primatives
115 lines • 6.09 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 AddGatewayV1 extends Transaction_1.default {
constructor(opts) {
super();
this.type = 'add_gateway_v1';
const { owner, gateway, payer, stakingFee, fee, ownerSignature, gatewaySignature, payerSignature, } = opts;
this.owner = owner;
this.gateway = gateway;
this.payer = payer;
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.stakingFeeTxnAddGatewayV1;
}
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 addGateway = this.toProto();
const txn = Txn.create({ addGateway });
return Txn.encode(txn).finish();
}
static fromString(serializedTxnString) {
var _a, _b, _c, _d, _e, _f;
const buf = Buffer.from(serializedTxnString, 'base64');
const { addGateway } = proto_1.default.helium.blockchain_txn.decode(buf);
const owner = ((_a = addGateway === null || addGateway === void 0 ? void 0 : addGateway.owner) === null || _a === void 0 ? void 0 : _a.length) ? (0, utils_1.toAddressable)(addGateway === null || addGateway === void 0 ? void 0 : addGateway.owner) : undefined;
const gateway = ((_b = addGateway === null || addGateway === void 0 ? void 0 : addGateway.gateway) === null || _b === void 0 ? void 0 : _b.length) ? (0, utils_1.toAddressable)(addGateway === null || addGateway === void 0 ? void 0 : addGateway.gateway) : undefined;
const payer = ((_c = addGateway === null || addGateway === void 0 ? void 0 : addGateway.payer) === null || _c === void 0 ? void 0 : _c.length) ? (0, utils_1.toAddressable)(addGateway === null || addGateway === void 0 ? void 0 : addGateway.payer) : undefined;
const ownerSignature = ((_d = addGateway === null || addGateway === void 0 ? void 0 : addGateway.ownerSignature) === null || _d === void 0 ? void 0 : _d.length)
? (0, utils_1.toUint8Array)(addGateway === null || addGateway === void 0 ? void 0 : addGateway.ownerSignature)
: undefined;
const gatewaySignature = ((_e = addGateway === null || addGateway === void 0 ? void 0 : addGateway.gatewaySignature) === null || _e === void 0 ? void 0 : _e.length)
? (0, utils_1.toUint8Array)(addGateway === null || addGateway === void 0 ? void 0 : addGateway.gatewaySignature)
: undefined;
const payerSignature = ((_f = addGateway === null || addGateway === void 0 ? void 0 : addGateway.payerSignature) === null || _f === void 0 ? void 0 : _f.length)
? (0, utils_1.toUint8Array)(addGateway === null || addGateway === void 0 ? void 0 : addGateway.payerSignature)
: undefined;
const fee = (0, utils_1.toNumber)(addGateway === null || addGateway === void 0 ? void 0 : addGateway.fee);
const stakingFee = (0, utils_1.toNumber)(addGateway === null || addGateway === void 0 ? void 0 : addGateway.stakingFee);
return new AddGatewayV1({
owner,
gateway,
payer,
fee,
stakingFee,
ownerSignature,
gatewaySignature,
payerSignature,
});
}
async sign(keypairs) {
const AddGateway = proto_1.default.helium.blockchain_txn_add_gateway_v1;
const addGateway = this.toProto(true);
const serialized = AddGateway.encode(addGateway).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 AddGateway = proto_1.default.helium.blockchain_txn_add_gateway_v1;
return AddGateway.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,
});
}
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 = AddGatewayV1;
//# sourceMappingURL=AddGatewayV1.js.map