Skip to content

Commit

Permalink
feat: counterfactual address helper
Browse files Browse the repository at this point in the history
  • Loading branch information
joepegler committed Dec 23, 2024
1 parent 3e10439 commit 59d69f5
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/sdk/account/toNexusAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ export const toNexusAccount = async (
}
],
functionName: "computeAccountAddress",
args: [signerAddress, index, [], 0]
args: [signerAddress, index, attesters_, attesterThreshold]
})) as Address

if (!addressEquals(addressFromFactory, zeroAddress)) {
Expand Down
80 changes: 80 additions & 0 deletions src/sdk/account/utils/getCounterFactualAddress.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import {
MOCK_ATTESTER_ADDRESS,
RHINESTONE_ATTESTER_ADDRESS
} from "@rhinestone/module-sdk/module"
import type { Address } from "viem"
import type { PublicClient } from "viem"
import { k1ValidatorFactoryAddress } from "../../constants"

/**
* Get the counterfactual address of a signer
*
* @param publicClient - The public client to use for the read contract
* @param signerAddress - The address of the signer
* @param index - The index of the account
* @param isTestnet - Whether the network is testnet
* @param attesters - The attesters to use
* @param threshold - The threshold of the attesters
* @param factoryAddress - The factory address to use
* @returns The counterfactual address
*
* @example
* ```ts
* const counterFactualAddress = await getCounterFactualAddress(publicClient, signerAddress)
* ```
*/
export const getCounterFactualAddress = async (
publicClient: PublicClient,
signerAddress: Address,
isTestnet = false,
index = 0n,
attesters = [RHINESTONE_ATTESTER_ADDRESS],
threshold = 1,
factoryAddress = k1ValidatorFactoryAddress
) => {
if (isTestnet) {
attesters.push(MOCK_ATTESTER_ADDRESS)
}

return 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, attesters, threshold]
})
}
1 change: 1 addition & 0 deletions src/sdk/account/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export * from "./Constants.js"
export * from "./getChain.js"
export * from "./Logger.js"
export * from "./toSigner.js"
export * from "./getCounterFactualAddress.js"

0 comments on commit 59d69f5

Please sign in to comment.