@helium/transactions
Version:
Construct and serialize Helium blockchain transaction primatives
55 lines • 2.24 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 StakeValidatorV1 extends Transaction_1.default {
constructor(opts) {
super();
this.type = 'stake_validator_v1';
this.address = opts.address;
this.owner = opts.owner;
this.stake = opts.stake;
if (opts.fee !== undefined) {
this.fee = opts.fee;
}
else {
this.fee = 0;
this.fee = this.calculateFee();
}
}
serialize() {
const Txn = proto_1.default.helium.blockchain_txn;
const stakeValidator = this.toProto();
const txn = Txn.create({ stakeValidator });
return Txn.encode(txn).finish();
}
async sign({ owner: ownerKeypair }) {
const StakeValidator = proto_1.default.helium.blockchain_txn_stake_validator_v1;
const stakeValidator = this.toProto(true);
const serialized = StakeValidator.encode(stakeValidator).finish();
const signature = await ownerKeypair.sign(serialized);
this.ownerSignature = signature;
return this;
}
toProto(forSigning = false) {
const StakeValidator = proto_1.default.helium.blockchain_txn_stake_validator_v1;
return StakeValidator.create({
address: this.address ? (0, utils_1.toUint8Array)(this.address.bin) : null,
owner: this.owner ? (0, utils_1.toUint8Array)(this.owner.bin) : null,
stake: this.stake,
fee: this.fee && this.fee > 0 ? this.fee : null,
ownerSignature: this.ownerSignature && !forSigning ? (0, utils_1.toUint8Array)(this.ownerSignature) : null,
});
}
calculateFee() {
this.ownerSignature = utils_1.EMPTY_SIGNATURE;
const payload = this.serialize();
return Transaction_1.default.calculateFee(payload);
}
}
exports.default = StakeValidatorV1;
//# sourceMappingURL=StakeValidatorV1.js.map