From f1157cb6bdf6ec722b6ee89c4af44d76b11b532f Mon Sep 17 00:00:00 2001 From: joepegler Date: Thu, 5 Dec 2024 17:25:11 +0000 Subject: [PATCH] chore: fix getAddress (#142) --- CHANGELOG.md | 6 ++++ package.json | 2 +- src/sdk/account/toNexusAccount.ts | 58 ++++++++++++++++++++++++++++--- 3 files changed, 60 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b002050fb..9ef210fed 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # @biconomy/sdk +## 0.0.13 + +### Patch Changes + +- Fix getAddress() + ## 0.0.12 ### Patch Changes diff --git a/package.json b/package.json index fbc314efb..1ec1523ea 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@biconomy/sdk", - "version": "0.0.12", + "version": "0.0.13", "author": "Biconomy", "repository": "github:bcnmy/sdk", "main": "./dist/_cjs/index.js", diff --git a/src/sdk/account/toNexusAccount.ts b/src/sdk/account/toNexusAccount.ts index e0a7c9d92..3d88b4757 100644 --- a/src/sdk/account/toNexusAccount.ts +++ b/src/sdk/account/toNexusAccount.ts @@ -213,14 +213,13 @@ export const toNexusAccount = async ( args: [signerAddress, index, [], 0] }) - let _accountAddress: Address | undefined = parameters.accountAddress - /** * @description Gets the init code for the account * @returns The init code as a hexadecimal string */ const getInitCode = () => concatHex([factoryAddress, factoryData]) + let _accountAddress: Address | undefined = parameters.accountAddress /** * @description Gets the counterfactual address of the account * @returns The counterfactual address @@ -233,12 +232,61 @@ export const toNexusAccount = async ( // biome-ignore lint/suspicious/noExplicitAny: } catch (e: any) { if (e?.cause?.data?.errorName === "SenderAddressResult") { - _accountAddress = e?.cause.data.args[0] as Address - if (!addressEquals(_accountAddress, zeroAddress)) { - return _accountAddress + const accountAddressFromError = e?.cause.data.args[0] as Address + if (!addressEquals(accountAddressFromError, zeroAddress)) { + _accountAddress = accountAddressFromError + return accountAddressFromError } } } + + const addressFromFactory = (await publicClient.readContract({ + address: factoryAddress, + abi: [ + { + inputs: [ + { + internalType: "address", + name: "eoaOwner", + type: "address" + }, + { + internalType: "uint256", + name: "index", + type: "uint256" + }, + { + internalType: "address[]", + name: "attesters", + type: "address[]" + }, + { + internalType: "uint8", + name: "threshold", + type: "uint8" + } + ], + name: "computeAccountAddress", + outputs: [ + { + internalType: "address payable", + name: "expectedAddress", + type: "address" + } + ], + stateMutability: "view", + type: "function" + } + ], + functionName: "computeAccountAddress", + args: [signerAddress, index, [], 0] + })) as Address + + if (!addressEquals(addressFromFactory, zeroAddress)) { + _accountAddress = addressFromFactory + return addressFromFactory + } + throw new Error("Failed to get counterfactual account address") }