UNPKG

@biconomy/abstractjs

Version:

SDK for Biconomy integration with support for account abstraction, smart accounts, ERC-4337.

74 lines 3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getNonceWithKeyUtil = exports.getDefaultNonceKey = void 0; const viem_1 = require("viem"); const constants_1 = require("../../constants/index.js"); const abi_1 = require("../../constants/abi/index.js"); const utils_1 = require("../utils/index.js"); class NonceManager { constructor() { Object.defineProperty(this, "isNonceKeyBeingCalculated", { enumerable: true, configurable: true, writable: true, value: new Map() }); } static getInstance() { if (!NonceManager.instance) { NonceManager.instance = new NonceManager(); } return NonceManager.instance; } buildNonceStoreKey(accountAddress, chainId) { return `${accountAddress.toLowerCase()}::${chainId}`; } async getDefaultNonceKey(accountAddress, chainId) { const storeKey = this.buildNonceStoreKey(accountAddress, chainId); while (this.isNonceKeyBeingCalculated.get(storeKey)) { await new Promise((resolve) => setTimeout(resolve, 1)); } this.isNonceKeyBeingCalculated.set(storeKey, true); const key = BigInt(Date.now()); await new Promise((resolve) => setTimeout(resolve, 1)); this.isNonceKeyBeingCalculated.set(storeKey, false); return key; } async getNonceWithKey(client, accountAddress, parameters) { const TIMESTAMP_ADJUSTMENT = 16777215n; const { key: key_, validationMode, moduleAddress } = parameters; try { const adjustedKey = BigInt(key_) % TIMESTAMP_ADJUSTMENT; const key = (0, viem_1.concat)([ (0, viem_1.toHex)(adjustedKey, { size: 3 }), validationMode, moduleAddress ]); const entryPointContract = (0, viem_1.getContract)({ address: constants_1.ENTRY_POINT_ADDRESS, abi: abi_1.EntrypointAbi, client }); const nonce = await entryPointContract.read.getNonce([ accountAddress, BigInt(key) ]); return { nonceKey: BigInt(key), nonce }; } catch (error) { const errorMessage = error.message ?? "RPC issue"; throw new Error(`Failed to fetch nonce due to the error: ${(0, utils_1.sanitizeUrl)(errorMessage)}`); } } } const getDefaultNonceKey = async (accountAddress, chainId) => { const manager = NonceManager.getInstance(); return manager.getDefaultNonceKey(accountAddress, chainId); }; exports.getDefaultNonceKey = getDefaultNonceKey; const getNonceWithKeyUtil = async (client, accountAddress, parameters) => { const manager = NonceManager.getInstance(); return manager.getNonceWithKey(client, accountAddress, parameters); }; exports.getNonceWithKeyUtil = getNonceWithKeyUtil; //# sourceMappingURL=getNonceWithKey.js.map