@helium/transactions
Version:
Construct and serialize Helium blockchain transaction primatives
67 lines • 3.1 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 TransferValidatorStakeV1 extends Transaction_1.default {
constructor(opts) {
super();
this.type = 'transfer_validator_stake_v1';
this.oldAddress = opts.oldAddress;
this.newAddress = opts.newAddress;
this.oldOwner = opts.oldOwner;
this.newOwner = opts.newOwner;
this.stakeAmount = opts.stakeAmount;
this.paymentAmount = opts.paymentAmount;
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 transferValStake = this.toProto();
const txn = Txn.create({ transferValStake });
return Txn.encode(txn).finish();
}
async sign(keypairs) {
const TransferValStake = proto_1.default.helium.blockchain_txn_transfer_validator_stake_v1;
const transferValStake = this.toProto(true);
const serialized = TransferValStake.encode(transferValStake).finish();
if (keypairs.oldOwner) {
this.oldOwnerSignature = await keypairs.oldOwner.sign(serialized);
}
if (keypairs.newOwner) {
this.newOwnerSignature = await keypairs.newOwner.sign(serialized);
}
return this;
}
toProto(forSigning = false) {
const TransferValStake = proto_1.default.helium.blockchain_txn_transfer_validator_stake_v1;
return TransferValStake.create({
oldAddress: this.oldAddress ? (0, utils_1.toUint8Array)(this.oldAddress.bin) : null,
newAddress: this.newAddress ? (0, utils_1.toUint8Array)(this.newAddress.bin) : null,
oldOwner: this.oldOwner ? (0, utils_1.toUint8Array)(this.oldOwner.bin) : null,
newOwner: this.newOwner ? (0, utils_1.toUint8Array)(this.newOwner.bin) : null,
stakeAmount: this.stakeAmount,
paymentAmount: this.paymentAmount && this.paymentAmount > 0 ? this.paymentAmount : null,
fee: this.fee && this.fee > 0 ? this.fee : null,
oldOwnerSignature: this.oldOwnerSignature && !forSigning ? (0, utils_1.toUint8Array)(this.oldOwnerSignature) : null,
newOwnerSignature: this.newOwnerSignature && !forSigning ? (0, utils_1.toUint8Array)(this.newOwnerSignature) : null,
});
}
calculateFee() {
this.oldOwnerSignature = utils_1.EMPTY_SIGNATURE;
this.newOwnerSignature = utils_1.EMPTY_SIGNATURE;
const payload = this.serialize();
return Transaction_1.default.calculateFee(payload);
}
}
exports.default = TransferValidatorStakeV1;
//# sourceMappingURL=TransferValidatorStakeV1.js.map