@helium/transactions
Version:
Construct and serialize Helium blockchain transaction primatives
92 lines • 4.52 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 TransferHotspotV1 extends Transaction_1.default {
constructor(opts) {
super();
this.type = 'transfer_hotspot_v1';
this.gateway = opts.gateway;
this.seller = opts.seller;
this.buyer = opts.buyer;
this.buyerNonce = opts.buyerNonce;
this.amountToSeller = opts.amountToSeller;
if (opts.fee !== undefined) {
this.fee = opts.fee;
}
else {
this.fee = 0;
this.fee = this.calculateFee();
}
this.sellerSignature = opts.sellerSignature;
this.buyerSignature = opts.buyerSignature;
}
serialize() {
const BlockchainTxn = proto_1.default.helium.blockchain_txn;
const transferHotspot = this.toProto();
const txn = BlockchainTxn.create({ transferHotspot });
return BlockchainTxn.encode(txn).finish();
}
static fromString(serializedTxnString) {
var _a, _b, _c, _d, _e, _f, _g, _h;
const buf = Buffer.from(serializedTxnString, 'base64');
const decoded = proto_1.default.helium.blockchain_txn.decode(buf);
const gateway = (0, utils_1.toAddressable)((_a = decoded.transferHotspot) === null || _a === void 0 ? void 0 : _a.gateway);
const seller = (0, utils_1.toAddressable)((_b = decoded.transferHotspot) === null || _b === void 0 ? void 0 : _b.seller);
const buyer = (0, utils_1.toAddressable)((_c = decoded.transferHotspot) === null || _c === void 0 ? void 0 : _c.buyer);
const sellerSignature = (0, utils_1.toUint8Array)((_d = decoded.transferHotspot) === null || _d === void 0 ? void 0 : _d.sellerSignature);
const buyerSignature = (0, utils_1.toUint8Array)((_e = decoded.transferHotspot) === null || _e === void 0 ? void 0 : _e.buyerSignature);
const buyerNonce = (0, utils_1.toNumber)((_f = decoded.transferHotspot) === null || _f === void 0 ? void 0 : _f.buyerNonce);
const amountToSeller = (0, utils_1.toNumber)((_g = decoded.transferHotspot) === null || _g === void 0 ? void 0 : _g.amountToSeller);
const fee = (0, utils_1.toNumber)((_h = decoded.transferHotspot) === null || _h === void 0 ? void 0 : _h.fee);
return new TransferHotspotV1({
gateway,
seller,
buyer,
sellerSignature,
buyerSignature,
buyerNonce,
amountToSeller,
fee,
});
}
async sign(keypairs) {
const TransferHotspotTxn = proto_1.default.helium.blockchain_txn_transfer_hotspot_v1;
const transferHotspot = this.toProto(true);
const serialized = TransferHotspotTxn.encode(transferHotspot).finish();
if (keypairs.seller) {
this.sellerSignature = await keypairs.seller.sign(serialized);
}
if (keypairs.buyer) {
this.buyerSignature = await keypairs.buyer.sign(serialized);
}
return this;
}
toProto(forSigning = false) {
const TransferHotspotTxn = proto_1.default.helium.blockchain_txn_transfer_hotspot_v1;
return TransferHotspotTxn.create({
gateway: this.gateway ? (0, utils_1.toUint8Array)(this.gateway.bin) : null,
seller: this.seller ? (0, utils_1.toUint8Array)(this.seller.bin) : null,
buyer: this.buyer ? (0, utils_1.toUint8Array)(this.buyer.bin) : null,
sellerSignature: this.sellerSignature && !forSigning
? (0, utils_1.toUint8Array)(this.sellerSignature) : null,
buyerSignature: this.buyerSignature && !forSigning
? (0, utils_1.toUint8Array)(this.buyerSignature) : null,
buyerNonce: this.buyerNonce,
amountToSeller: this.amountToSeller ? this.amountToSeller : null,
fee: this.fee ? this.fee : null,
});
}
calculateFee() {
this.buyerSignature = utils_1.EMPTY_SIGNATURE;
this.sellerSignature = utils_1.EMPTY_SIGNATURE;
const payload = this.serialize();
return Transaction_1.default.calculateFee(payload);
}
}
exports.default = TransferHotspotV1;
//# sourceMappingURL=TransferHotspotV1.js.map