Skip to content

Commit

Permalink
fixed conflicts + added missing functionality + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
VGabriel45 committed Sep 20, 2024
1 parent f97a5ef commit dbc30b6
Show file tree
Hide file tree
Showing 50 changed files with 1,420 additions and 822 deletions.
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
8 changes: 4 additions & 4 deletions src/sdk/account/toNexusAccount.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { toNetwork } from "../../test/testSetup"
import {
fundAndDeployClients,
getTestAccount,
getTestSmartAccountClient,
killNetwork,
toTestClient
} from "../../test/testUtils"
Expand Down Expand Up @@ -77,11 +78,10 @@ describe("nexus.account", async () => {
transport: http()
})

nexusClient = await createNexusClient({
holder: account,
nexusClient = await getTestSmartAccountClient({
account,
chain,
transport: http(),
bundlerTransport: http(bundlerUrl)
bundlerUrl
})

nexusAccount = nexusClient.account
Expand Down
56 changes: 45 additions & 11 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 { Signer, type UnknownSigner, toSigner } from "./utils/toSigner"

/**
* Parameters for creating a Nexus Smart Account
Expand All @@ -70,8 +70,8 @@ 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 */
Expand Down Expand Up @@ -113,6 +113,9 @@ 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
getSigner: () => Promise<Signer>
factoryData: Hex
factoryAddress: Address
}
Expand All @@ -132,7 +135,7 @@ export type NexusSmartAccountImplementation = SmartAccountImplementation<
* const account = await toNexusAccount({
* chain: mainnet,
* transport: http(),
* holder: '0x...',
* signer: '0x...',
* })
*/
export const toNexusAccount = async (
Expand All @@ -141,7 +144,7 @@ export const toNexusAccount = async (
const {
chain,
transport,
holder: holder_,
signer: _signer,
index = 0n,
activeModule,
factoryAddress = contracts.k1ValidatorFactory.address,
Expand All @@ -150,10 +153,10 @@ export const toNexusAccount = async (
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 +181,16 @@ export const toNexusAccount = async (
args: [signerAddress, index, [], 0]
})

const defaultedActiveModule =
let defaultedActiveModule =
activeModule ??
new K1ValidatorModule(
{
address: k1ValidatorAddress,
type: "validator",
context: signerAddress,
data: signerAddress,
additionalContext: "0x"
},
holder
signer
)

let _accountAddress: Address
Expand All @@ -202,6 +205,14 @@ export const toNexusAccount = async (
return _accountAddress
}

/**
* @description Gets the signer associated with this account
* @returns A Promise that resolves to the signer
*/
const getSigner = async (): Promise<Signer> => {
return signer
}

/**
* @description Gets the counterfactual address of the account
* @returns The counterfactual address
Expand Down Expand Up @@ -345,6 +356,24 @@ 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 Gets the active validation module for the account
* @returns The active validation module
*/
const getActiveValidationModule = (): BaseValidationModule => {

Check failure on line 372 in src/sdk/account/toNexusAccount.ts

View workflow job for this annotation

GitHub Actions / size report

'getActiveValidationModule' is declared but its value is never read.
return defaultedActiveModule;
}


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

const signature = encodePacked(
Expand Down Expand Up @@ -494,12 +523,14 @@ export const toNexusAccount = async (
parameters
const address = await getCounterFactualAddress()
const userOperation = { ...userOpWithoutSender, sender: address }
console.log(userOperation, "userOperation");
const hash = getUserOperationHash({
chainId,
entryPointAddress: entryPoint07Address,
entryPointVersion: "0.7",
userOperation
})
console.log('calling signUserOpHash');
return await defaultedActiveModule.signUserOpHash(hash)
},
getNonce,
Expand All @@ -510,6 +541,9 @@ export const toNexusAccount = async (
encodeExecute,
encodeExecuteBatch,
getUserOpHash,
setActiveValidationModule,
getActiveValidationModule: () => defaultedActiveModule,
getSigner,
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,37 @@ 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
>
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 +58,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

0 comments on commit dbc30b6

Please sign in to comment.