Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nexus fix: OwnableValidator & OwnableExecutor integration + tests, + add missing features after viem refactor + fix issues & conflicts +++ #576

Merged
merged 18 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified bun.lockb
Binary file not shown.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
"execa": "^9.3.1",
"get-port": "^7.1.0",
"gh-pages": "^6.1.1",
"nexus": "github:bcnmy/nexus#7f8410d6f8037b698b9dd08e6da9008cbd30418a",
"nexus": "github:bcnmy/nexus#16273fdb3baa26e03c8b536ca3a9156197c8410a",
"prool": "^0.0.16",
"rimraf": "^5.0.5",
"simple-git-hooks": "^2.9.0",
Expand All @@ -125,6 +125,7 @@
"commit-msg": "npx --no -- commitlint --edit ${1}"
},
"dependencies": {
"@rhinestone/module-sdk": "^0.1.17",
"merkletreejs": "^0.3.11"
}
}
14 changes: 7 additions & 7 deletions scripts/fetch:deployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type FetchDetails = {
}
const {
nexusDeploymentPath = "../node_modules/nexus/deployments",
chainName = "anvil-51139",
chainName = "anvil-55261",
forSrc = ["K1ValidatorFactory", "Nexus", "K1Validator"]
} = yargs(hideBin(process.argv)).argv as unknown as FetchDetails

Expand Down Expand Up @@ -51,8 +51,8 @@ export const getDeployments = async () => {
)} as const;\n`

const tsAbiPath = isForCore
? `${__dirname}/../src/__contracts/abi/${name}Abi.ts`
: `${__dirname}/../test/__contracts/abi/${name}Abi.ts`
? `${__dirname}/../src/sdk/__contracts/abi/${name}Abi.ts`
: `${__dirname}/../src/test/__contracts/abi/${name}Abi.ts`

fs.writeFileSync(tsAbiPath, tsAbiContent)

Expand All @@ -73,15 +73,15 @@ export const getDeployments = async () => {
.join("\n")}`

// Write the ABIs
const abiIndexPath = `${__dirname}/../src/__contracts/abi/index.ts`
const abiIndexPath = `${__dirname}/../src/sdk/__contracts/abi/index.ts`
fs.writeFileSync(abiIndexPath, abiIndexContent)

const testAbiIndexPath = `${__dirname}/../test/__contracts/abi/index.ts`
const testAbiIndexPath = `${__dirname}/../src/test/__contracts/abi/index.ts`
fs.writeFileSync(testAbiIndexPath, testAbiIndexContent)

// Write addresses to src folder
const writeAddressesPath = `${__dirname}/../src/__contracts/addresses.ts`
const writeAddressesPathTest = `${__dirname}/../test/__contracts/mockAddresses.ts`
const writeAddressesPath = `${__dirname}/../src/sdk/__contracts/addresses.ts`
const writeAddressesPathTest = `${__dirname}/../src/test/__contracts/mockAddresses.ts`

const addressesContent = `// The contents of this folder is auto-generated. Please do not edit as your changes are likely to be overwritten\n
export const addresses = ${JSON.stringify(
Expand Down
2 changes: 1 addition & 1 deletion scripts/send:userOp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const accountTwo = privateKeyToAccount(`0x${privateKeyTwo}`)
const recipient = accountTwo.address

const nexusClient = await createNexusClient({
holder: account,
signer: account,
chain,
transport: http(),
bundlerTransport: http(bundlerUrl),
Expand Down
2 changes: 1 addition & 1 deletion scripts/viem:bundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ const publicClient = createPublicClient({

const main = async () => {
const nexusAccount = await toNexusAccount({
holder: account,
signer: account,
chain,
transport: http(),
k1ValidatorAddress,
Expand Down
2 changes: 1 addition & 1 deletion src/sdk/__contracts/abi/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export * from "./EIP1271Abi"
export * from "./UniActionPolicyAbi"
export * from "./EntryPointABI"
export * from "./EIP1271Abi"
export * from "./NexusAbi"
export * from "./K1ValidatorAbi"
export * from "./K1ValidatorFactoryAbi"
7 changes: 3 additions & 4 deletions src/sdk/__contracts/addresses.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
// The contents of this folder is auto-generated. Please do not edit as your changes are likely to be overwritten

export const addresses = {
Nexus: "0x21f4C007C9f091B93B7C1C6911E13ACcd3DAd403",
K1Validator: "0x6854688d3D9A87a33Addd5f4deB5cea1B97fa5b7",
K1ValidatorFactory: "0x976869CF9c5Dd5046b41963EF1bBcE62b5366869",
UniActionPolicy: "0x28120dC008C36d95DE5fa0603526f219c1Ba80f6"
Nexus: "0xcb56273F55Fde691f92976B976cf79f79a5B4c22",
K1Validator: "0x08FCa672878aD598FBDaDdEBFbB71Dd9BA90b75a",
K1ValidatorFactory: "0xEC6596Ca8ea4DBFb8Ca1B365490Dce8dAe2CCDcA"
} as const
export default addresses
2 changes: 1 addition & 1 deletion src/sdk/account/toNexusAccount.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ describe("nexus.account", async () => {
})

nexusClient = await createNexusClient({
holder: account,
signer: account,
chain,
transport: http(),
bundlerTransport: http(bundlerUrl)
Expand Down
44 changes: 27 additions & 17 deletions src/sdk/account/toNexusAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ import {
packUserOp,
typeToString
} from "./utils/Utils"
import { type UnknownHolder, toHolder } from "./utils/toHolder"
import { type UnknownSigner, toSigner } from "./utils/toSigner"

/**
* Parameters for creating a Nexus Smart Account
Expand All @@ -70,12 +70,12 @@ export type ToNexusSmartAccountParameters = {
chain: Chain
/** The transport configuration */
transport: ClientConfig["transport"]
/** The holder account or address */
holder: UnknownHolder
/** The signer account or address */
signer: UnknownSigner
/** Optional index for the account */
index?: bigint | undefined
/** Optional active validation module */
activeModule?: BaseValidationModule
activeValidationModule?: BaseValidationModule
/** Optional factory address */
factoryAddress?: Address
/** Optional K1 validator address */
Expand Down Expand Up @@ -113,6 +113,8 @@ export type NexusSmartAccountImplementation = SmartAccountImplementation<
encodeExecute: (call: Call) => Promise<Hex>
encodeExecuteBatch: (calls: readonly Call[]) => Promise<Hex>
getUserOpHash: (userOp: Partial<UserOperationStruct>) => Promise<Hex>
setActiveValidationModule: (validationModule: BaseValidationModule) => void
getActiveValidationModule: () => BaseValidationModule,
factoryData: Hex
factoryAddress: Address
}
Expand All @@ -132,7 +134,7 @@ export type NexusSmartAccountImplementation = SmartAccountImplementation<
* const account = await toNexusAccount({
* chain: mainnet,
* transport: http(),
* holder: '0x...',
* signer: '0x...',
* })
*/
export const toNexusAccount = async (
Expand All @@ -141,19 +143,19 @@ export const toNexusAccount = async (
const {
chain,
transport,
holder: holder_,
signer: _signer,
index = 0n,
activeModule,
activeValidationModule,
factoryAddress = contracts.k1ValidatorFactory.address,
k1ValidatorAddress = contracts.k1Validator.address,
key = "nexus account",
name = "Nexus Account"
} = parameters

const holder = await toHolder({ holder: holder_ })
const signer = await toSigner({ signer: _signer })

const masterClient = createWalletClient({
account: holder,
account: signer,
chain,
transport,
key,
Expand All @@ -178,16 +180,16 @@ export const toNexusAccount = async (
args: [signerAddress, index, [], 0]
})

const defaultedActiveModule =
activeModule ??
let defaultedActiveModule =
activeValidationModule ??
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can also call this activeValidator
but can keep this as note for now and still merge PR.

new K1ValidatorModule(
{
address: k1ValidatorAddress,
type: "validator",
context: signerAddress,
data: signerAddress,
additionalContext: "0x"
},
holder
signer
)

let _accountAddress: Address
Expand Down Expand Up @@ -215,7 +217,6 @@ export const toNexusAccount = async (
_accountAddress = e?.cause.data.args[0] as Address
return _accountAddress
}
console.log("Im in here", e)
}
throw new Error("Failed to get counterfactual account address")
}
Expand Down Expand Up @@ -345,6 +346,15 @@ export const toNexusAccount = async (
}
}

/**
* @description Changes the active module for the account
* @param newModule - The new module to set as active
* @returns void
*/
const setActiveValidationModule = (validationModule: BaseValidationModule): void => {
defaultedActiveModule = validationModule;
}

/**
* @description Signs a message
* @param params - The parameters for signing
Expand All @@ -354,9 +364,7 @@ export const toNexusAccount = async (
const signMessage = async ({
message
}: { message: SignableMessage }): Promise<Hex> => {
const tempSignature = await defaultedActiveModule
.getHolder()
.signMessage({ message })
const tempSignature = await defaultedActiveModule.getSigner().signMessage({ message })

const signature = encodePacked(
["address", "bytes"],
Expand Down Expand Up @@ -510,6 +518,8 @@ export const toNexusAccount = async (
encodeExecute,
encodeExecuteBatch,
getUserOpHash,
setActiveValidationModule,
getActiveValidationModule: () => defaultedActiveModule,
factoryData,
factoryAddress
}
Expand Down
2 changes: 1 addition & 1 deletion src/sdk/account/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ export * from "./Utils.js"
export * from "./Constants.js"
export * from "./getChain.js"
export * from "./Logger.js"
export * from "./toHolder.js"
export * from "./toSigner.js"
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,38 @@ import { toAccount } from "viem/accounts"
import { signTypedData } from "viem/actions"
import { getAction } from "viem/utils"

export type Holder = LocalAccount
export type UnknownHolder = OneOf<
export type Signer = LocalAccount
export type UnknownSigner = OneOf<
| EIP1193Provider
| WalletClient<Transport, Chain | undefined, Account>
| LocalAccount
| Account
>
export async function toHolder({
holder,
export async function toSigner({
signer,
address
}: {
holder: UnknownHolder
signer: UnknownSigner
address?: Address
}): Promise<LocalAccount> {
if ("type" in holder && holder.type === "local") {
return holder as LocalAccount
if ("type" in signer && signer.type === "local") {
return signer as LocalAccount
}

let walletClient:
| WalletClient<Transport, Chain | undefined, Account>
| undefined = undefined

if ("request" in holder) {
if ("request" in signer) {
if (!address) {
try {
;[address] = await (holder.request as EIP1193RequestFn<EIP1474Methods>)(
;[address] = await (signer.request as EIP1193RequestFn<EIP1474Methods>)(
{
method: "eth_requestAccounts"
}
)
} catch {
;[address] = await (holder.request as EIP1193RequestFn<EIP1474Methods>)(
;[address] = await (signer.request as EIP1193RequestFn<EIP1474Methods>)(
{
method: "eth_accounts"
}
Expand All @@ -58,12 +59,12 @@ export async function toHolder({

walletClient = createWalletClient({
account: address,
transport: custom(holder as EIP1193Provider)
transport: custom(signer as EIP1193Provider)
})
}

if (!walletClient) {
walletClient = holder as WalletClient<Transport, Chain | undefined, Account>
walletClient = signer as WalletClient<Transport, Chain | undefined, Account>
}

return toAccount({
Expand Down
2 changes: 1 addition & 1 deletion src/sdk/clients/createBicoBundlerClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe("bico.bundler", async () => {
testClient = toTestClient(chain, getTestAccount(5))

nexusAccount = await toNexusAccount({
holder: account,
signer: account,
chain,
transport: http()
})
Expand Down
Loading