UNPKG

@firmachain/firma-js

Version:

The Official FirmaChain Javascript SDK written in Typescript

133 lines (132 loc) 5.02 kB
"use strict"; var __read = (this && this.__read) || function (o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; }; var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.makeSignDocProtobuf = exports.makeAuthInfoBytesProtobuf = exports.makeSignBytes = exports.makeSignDoc = exports.makeAuthInfoBytes = void 0; /* eslint-disable @typescript-eslint/naming-convention */ var coin_1 = require("cosmjs-types/cosmos/base/v1beta1/coin"); var signing_1 = require("cosmjs-types/cosmos/tx/signing/v1beta1/signing"); var tx_1 = require("cosmjs-types/cosmos/tx/v1beta1/tx"); function makeSignerInfos(signers, signMode) { return signers.map(function (_a) { var pubkey = _a.pubkey, sequence = _a.sequence; return tx_1.SignerInfo.fromPartial({ publicKey: pubkey, modeInfo: { single: { mode: signMode }, }, sequence: BigInt(sequence), }); }); } function makeAuthInfoBytes(signers, feeAmount, gasLimit, granter, payer, signMode) { if (signMode === void 0) { signMode = signing_1.SignMode.SIGN_MODE_DIRECT; } var fee = tx_1.Fee.fromPartial({ amount: __spreadArray([], __read(feeAmount), false), gasLimit: BigInt(gasLimit), granter: granter || "", payer: payer || "", }); var authInfo = tx_1.AuthInfo.fromPartial({ signerInfos: makeSignerInfos(signers, signMode), fee: fee, }); return tx_1.AuthInfo.encode(authInfo).finish(); } exports.makeAuthInfoBytes = makeAuthInfoBytes; function makeSignDoc(bodyBytes, authInfoBytes, chainId, accountNumber) { return tx_1.SignDoc.fromPartial({ bodyBytes: bodyBytes, authInfoBytes: authInfoBytes, chainId: chainId, accountNumber: BigInt(accountNumber), }); } exports.makeSignDoc = makeSignDoc; function makeSignBytes(signDoc) { // Ensure all required fields are present if (!signDoc.bodyBytes || !signDoc.authInfoBytes || !signDoc.chainId || signDoc.accountNumber === undefined) { throw new Error("SignDoc is missing required fields"); } var doc = tx_1.SignDoc.fromPartial({ accountNumber: signDoc.accountNumber, authInfoBytes: signDoc.authInfoBytes, bodyBytes: signDoc.bodyBytes, chainId: signDoc.chainId, }); return tx_1.SignDoc.encode(doc).finish(); } exports.makeSignBytes = makeSignBytes; /** * Creates AuthInfo bytes for protobuf signing with enhanced type safety */ function makeAuthInfoBytesProtobuf(signers, feeAmount, gasLimit, granter, payer) { var normalizedGasLimit = typeof gasLimit === 'bigint' ? gasLimit : BigInt(gasLimit); var fee = tx_1.Fee.fromPartial({ amount: feeAmount.map(function (coin) { return coin_1.Coin.fromPartial(coin); }), gasLimit: normalizedGasLimit, granter: granter || "", payer: payer || "", }); var signerInfos = signers.map(function (_a) { var pubkey = _a.pubkey, sequence = _a.sequence; return tx_1.SignerInfo.fromPartial({ publicKey: pubkey, modeInfo: { single: { mode: signing_1.SignMode.SIGN_MODE_DIRECT }, }, sequence: BigInt(sequence), }); }); var authInfo = tx_1.AuthInfo.fromPartial({ signerInfos: signerInfos, fee: fee, }); return tx_1.AuthInfo.encode(authInfo).finish(); } exports.makeAuthInfoBytesProtobuf = makeAuthInfoBytesProtobuf; /** * Creates a protobuf SignDoc with validation */ function makeSignDocProtobuf(bodyBytes, authInfoBytes, chainId, accountNumber) { if (!bodyBytes || bodyBytes.length === 0) { throw new Error("bodyBytes cannot be empty"); } if (!authInfoBytes || authInfoBytes.length === 0) { throw new Error("authInfoBytes cannot be empty"); } if (!chainId) { throw new Error("chainId cannot be empty"); } var normalizedAccountNumber = typeof accountNumber === 'bigint' ? accountNumber : BigInt(accountNumber); return tx_1.SignDoc.fromPartial({ bodyBytes: bodyBytes, authInfoBytes: authInfoBytes, chainId: chainId, accountNumber: normalizedAccountNumber, }); } exports.makeSignDocProtobuf = makeSignDocProtobuf;