Skip to content

Commit

Permalink
chore: continued
Browse files Browse the repository at this point in the history
  • Loading branch information
joepegler committed Dec 6, 2024
1 parent b7cdba1 commit 13e3184
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/sdk/account/toNexusAccount.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ describe("nexus.account", async () => {
expect(isValidEthSigned).toBe(true)
})

test.skip("should verify signatures from prepared UserOperation", async () => {
test("should verify signatures from prepared UserOperation", async () => {
const mockSigVerifierContract = getContract({
address: testAddresses.MockSignatureValidator,
abi: MockSignatureValidatorAbi,
Expand Down Expand Up @@ -365,7 +365,7 @@ describe("nexus.account", async () => {
expect(contractResponse).toBe(eip1271MagicValue)
})

test.skip("should sign using signTypedData SDK method", async () => {
test("should sign using signTypedData SDK method", async () => {
const appDomain = {
chainId: chain.id,
name: "TokenWithPermit",
Expand Down
126 changes: 124 additions & 2 deletions src/test/playground.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import {
type PublicClient,
type WalletClient,
createPublicClient,
createWalletClient
createWalletClient,
type Hex,
encodeFunctionData
} from "viem"
import { beforeAll, describe, expect, test } from "vitest"
import { playgroundTrue } from "../sdk/account/utils/Utils"
Expand All @@ -21,6 +23,18 @@ import {
type TestnetParams,
getTestParamsForTestnet
} from "./testUtils"
import { toSmartSessionsValidator } from "../sdk/modules/smartSessionsValidator/toSmartSessionsValidator"
import { testAddresses } from "./callDatas"
import type {
CreateSessionDataParams,
SessionData
} from "../sdk/modules/smartSessionsValidator/Types"
import {
smartSessionCreateActions,
smartSessionUseActions
} from "../sdk/modules/smartSessionsValidator/decorators"
import { SmartSessionMode } from "@rhinestone/module-sdk"
import { CounterAbi } from "./__contracts/abi/CounterAbi"

describe.skipIf(!playgroundTrue())("playground", () => {
let network: NetworkConfig
Expand All @@ -38,6 +52,8 @@ describe.skipIf(!playgroundTrue())("playground", () => {
let nexusClient: NexusClient
let nexusAccountAddress: Address

const index = 2n

beforeAll(async () => {
network = await toNetwork("PUBLIC_TESTNET")

Expand Down Expand Up @@ -72,6 +88,7 @@ describe.skipIf(!playgroundTrue())("playground", () => {
transport: http(network.paymasterUrl)
})
: undefined,
index,
...testParams
})
})
Expand All @@ -91,6 +108,8 @@ describe.skipIf(!playgroundTrue())("playground", () => {
})
])

console.log({ ownerBalance, smartAccountBalance })

const balancesAreOfCorrectType = [ownerBalance, smartAccountBalance].every(
(balance) => typeof balance === "bigint"
)
Expand All @@ -99,7 +118,7 @@ describe.skipIf(!playgroundTrue())("playground", () => {
chain,
account: eoaAccount,
to: nexusAccountAddress,
value: 1000000000000000000n
value: 100000000000000000n
})
const receipt = await publicClient.waitForTransactionReceipt({ hash })
}
Expand Down Expand Up @@ -142,4 +161,107 @@ describe.skipIf(!playgroundTrue())("playground", () => {
expect(success).toBe("true")
expect(balanceAfter - balanceBefore).toBe(1n)
})

test("should test creating and using a session", async () => {
const sessionsModule = toSmartSessionsValidator({
account: nexusClient.account,
signer: eoaAccount
})

const isInstalledBefore = await nexusClient.isModuleInstalled({
module: sessionsModule
})

if (!isInstalledBefore) {
const hash = await nexusClient.installModule({
module: sessionsModule.moduleInitData
})

const { success: installSuccess } =
await nexusClient.waitForUserOperationReceipt({ hash })
expect(installSuccess).toBe("true")
}

const isInstalledAfter = await nexusClient.isModuleInstalled({
module: sessionsModule
})
expect(isInstalledAfter).toBe(true)

const sessionRequestedInfo: CreateSessionDataParams[] = [
{
sessionPublicKey: eoaAccount.address, // session key signer
actionPoliciesInfo: [
{
contractAddress: testAddresses.Counter, // counter address
functionSelector: "0x273ea3e3" as Hex // function selector for increment count
}
]
}
]

const nexusSessionClient = nexusClient.extend(
smartSessionCreateActions(sessionsModule)
)

const createSessionsResponse = await nexusSessionClient.grantPermission({
sessionRequestedInfo
})

expect(createSessionsResponse.userOpHash).toBeDefined()
expect(createSessionsResponse.permissionIds).toBeDefined()

const receiptTwo = await nexusClient.waitForUserOperationReceipt({
hash: createSessionsResponse.userOpHash
})

expect(receiptTwo.success).toBe("true")

const sessionData: SessionData = {
granter: nexusClient.account.address,
sessionPublicKey: eoaAccount.address,
moduleData: {
permissionIds: createSessionsResponse.permissionIds,
mode: SmartSessionMode.USE
}
}

const smartSessionNexusClient = await createNexusClient({
chain,
accountAddress: nexusClient.account.address,
signer: eoaAccount,
transport: http(),
bundlerTransport: http(bundlerUrl),
index,
...testParams
})

const usePermissionsModule = toSmartSessionsValidator({
account: smartSessionNexusClient.account,
signer: eoaAccount,
moduleData: sessionData.moduleData
})

const useSmartSessionNexusClient = smartSessionNexusClient.extend(
smartSessionUseActions(usePermissionsModule)
)

const userOpHash = await useSmartSessionNexusClient.usePermission({
calls: [
{
to: testAddresses.Counter,
data: encodeFunctionData({
abi: CounterAbi,
functionName: "incrementNumber",
args: []
})
}
]
})

expect(userOpHash).toBeDefined()
const receiptThree =
await useSmartSessionNexusClient.waitForUserOperationReceipt({
hash: userOpHash
})
})
})

0 comments on commit 13e3184

Please sign in to comment.