@pokt-network/pocket-js
Version:
Pocket-js core package with the main functionalities to interact with the Pocket Network.
124 lines • 5.28 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ProtoTxEncoder = void 0;
var coin_denom_1 = require("../models/coin-denom");
var base_tx_encoder_1 = require("./base-tx-encoder");
var tx_signer_1 = require("../models/proto/generated/tx-signer");
var varint = __importStar(require("varint"));
var ProtoTxEncoder = /** @class */ (function (_super) {
__extends(ProtoTxEncoder, _super);
function ProtoTxEncoder() {
return _super !== null && _super.apply(this, arguments) || this;
}
ProtoTxEncoder.prototype.getFeeObj = function () {
return [{
amount: this.fee,
denom: this.feeDenom !== undefined ? coin_denom_1.CoinDenom[this.feeDenom].toLowerCase() : 'upokt'
}];
};
// Returns the bytes to be signed by the account sending the transaction
ProtoTxEncoder.prototype.marshalStdSignDoc = function () {
var stdSignDoc = {
chain_id: this.chainID,
entropy: this.entropy,
fee: this.getFeeObj(),
memo: this.memo,
msg: this.msg.toStdSignDocMsgObj()
};
return Buffer.from(JSON.stringify(stdSignDoc), "utf-8");
};
// Returns the bytes to be signed by the account sending the transaction
// The key difference from the above is that this can be used to obtain
// the unsigned tx bytes without the use of a signer
ProtoTxEncoder.marshalStdSignDoc = function (chainID, entropy, fee, msg, memo, feeDenom) {
var stdSignDoc = {
chain_id: chainID,
entropy: entropy,
fee: [{
amount: fee,
denom: feeDenom !== undefined ? coin_denom_1.CoinDenom[feeDenom].toLowerCase() : 'upokt'
}],
memo: memo,
msg: msg
};
return Buffer.from(JSON.stringify(stdSignDoc), "utf-8");
};
// Returns the signed encoded transaction
ProtoTxEncoder.prototype.marshalStdTx = function (signature) {
var txSig = {
publicKey: signature.pubKey,
Signature: signature.signature
};
var stdTx = {
msg: this.msg.toStdTxMsgObj(),
fee: this.getFeeObj(),
signature: txSig,
memo: this.memo ? this.memo : "",
entropy: parseInt(this.entropy, 10)
};
// Create the Proto Std Tx bytes
var protoStdTxBytes = Buffer.from(tx_signer_1.ProtoStdTx.encode(stdTx).finish());
// Create the prefix
var prefixBytes = varint.encode(protoStdTxBytes.length);
var prefix = Buffer.from(prefixBytes);
// Concatenate for the result
return Buffer.concat([prefix, protoStdTxBytes]);
};
// Returns the signed encoded transaction,
// The key difference from the above is that this can be called with an external stdTxMsg object
ProtoTxEncoder.marshalStdTx = function (stdTxMsgObj, stdSignDoc, signature) {
var txSig = {
publicKey: signature.pubKey,
Signature: signature.signature
};
var stdTx = {
msg: stdTxMsgObj,
fee: stdSignDoc.fee,
signature: txSig,
memo: stdSignDoc.memo,
entropy: parseInt(stdSignDoc.entropy, 10),
};
// Create the Proto Std Tx bytes
var protoStdTxBytes = Buffer.from(tx_signer_1.ProtoStdTx.encode(stdTx).finish());
// Create the prefix
var prefixBytes = varint.encode(protoStdTxBytes.length);
var prefix = Buffer.from(prefixBytes);
// Concatenate for the result
return Buffer.concat([prefix, protoStdTxBytes]);
};
return ProtoTxEncoder;
}(base_tx_encoder_1.BaseTxEncoder));
exports.ProtoTxEncoder = ProtoTxEncoder;
//# sourceMappingURL=proto-tx-encoder.js.map