Skip to content
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
350 changes: 350 additions & 0 deletions packages/test/v0.7/permissionValidator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ import {
getAbiItem,
hashMessage,
hashTypedData,
hexToBytes,
pad,
parseAbi,
parseEther,
toFunctionSelector,
zeroAddress
Expand Down Expand Up @@ -1229,6 +1232,353 @@ describe("Permission kernel Account", () => {
TEST_TIMEOUT
)

test(
"Smart account client send transaction with CallPolicy V0.0.5",
async () => {
const callPolicy = toCallPolicy({
policyVersion: CallPolicyVersion.V0_0_5,
permissions: [
{
abi: TEST_ERC20Abi,
target: Test_ERC20Address,
functionName: "transfer",
args: [
{
condition: ParamCondition.EQUAL,
value: owner.address
},
null
]
}
]
})

const permissionSmartAccountClient = await getKernelAccountClient({
account: await getSignerToPermissionKernelAccount([callPolicy]),
paymaster: zeroDevPaymaster
})

await mintToAccount(
permissionSmartAccountClient.account.address,
100000000n
)

const amountToTransfer = 10000n
const transferData = encodeFunctionData({
abi: TEST_ERC20Abi,
functionName: "transfer",
args: [owner.address, amountToTransfer]
})

const balanceOfReceipientBefore = await publicClient.readContract({
abi: TEST_ERC20Abi,
address: Test_ERC20Address,
functionName: "balanceOf",
args: [owner.address]
})

console.log("balanceOfReceipientBefore", balanceOfReceipientBefore)

const response = await permissionSmartAccountClient.sendTransaction(
{
to: Test_ERC20Address,
data: transferData
}
)

console.log("Transaction hash:", response)

const balanceOfReceipientAfter = await publicClient.readContract({
abi: TEST_ERC20Abi,
address: Test_ERC20Address,
functionName: "balanceOf",
args: [owner.address]
})

console.log("balanceOfReceipientAfter", balanceOfReceipientAfter)

expect(balanceOfReceipientAfter).toBe(
balanceOfReceipientBefore + amountToTransfer
)
},
TEST_TIMEOUT
)

test(
"Smart account client send transaction with SLICE_EQUAL condition with bytes",
async () => {
const testAbi = parseAbi(["function test(bytes data) public"])

const callPolicy = toCallPolicy({
policyVersion: CallPolicyVersion.V0_0_5,
permissions: [
{
abi: testAbi,
target: zeroAddress,
functionName: "test",
args: [
{
condition: ParamCondition.SLICE_EQUAL,
value: "0xff",
start: 1,
length: 1
}
]
}
]
})

const permissionSmartAccountClient = await getKernelAccountClient({
account: await getSignerToPermissionKernelAccount([callPolicy]),
paymaster: zeroDevPaymaster
})

const callData = encodeFunctionData({
abi: testAbi,
functionName: "test",
args: ["0x00ff"]
})

const response = await permissionSmartAccountClient.sendTransaction(
{
to: zeroAddress,
data: callData
}
)

console.log("Transaction hash:", response)
},
TEST_TIMEOUT
)

test(
"Smart account client send transaction with SLICE_EQUAL condition with string",
async () => {
const testAbi = parseAbi([
"function test(string calldata data) public"
])

const callPolicy = toCallPolicy({
policyVersion: CallPolicyVersion.V0_0_5,
permissions: [
{
abi: testAbi,
target: zeroAddress,
functionName: "test",
args: [
{
condition: ParamCondition.SLICE_EQUAL,
value: "kernel",
start: 2,
length: 6
}
]
}
]
})

const permissionSmartAccountClient = await getKernelAccountClient({
account: await getSignerToPermissionKernelAccount([callPolicy]),
paymaster: zeroDevPaymaster
})

const callData = encodeFunctionData({
abi: testAbi,
functionName: "test",
args: ["0xkernel"]
})

const response = await permissionSmartAccountClient.sendTransaction(
{
to: zeroAddress,
data: callData
}
)

console.log("Transaction hash:", response)
},
TEST_TIMEOUT
)

test(
"Smart account client send transaction with SLICE_EQUAL condition with hex string",
async () => {
const testAbi = parseAbi([
"function test(string calldata data) public"
])

const callPolicy = toCallPolicy({
policyVersion: CallPolicyVersion.V0_0_5,
permissions: [
{
abi: testAbi,
target: zeroAddress,
functionName: "test",
args: [
{
condition: ParamCondition.SLICE_EQUAL,
value: "0xffff",
start: 0,
length: 6
}
]
}
]
})

const permissionSmartAccountClient = await getKernelAccountClient({
account: await getSignerToPermissionKernelAccount([callPolicy]),
paymaster: zeroDevPaymaster
})

const callData = encodeFunctionData({
abi: testAbi,
functionName: "test",
args: ["0xffff00"]
})

const response = await permissionSmartAccountClient.sendTransaction(
{
to: zeroAddress,
data: callData
}
)

console.log("Transaction hash:", response)
},
TEST_TIMEOUT
)

test(
"should fail with Smart account client send transaction with SLICE_EQUAL condition",
async () => {
const testAbi = parseAbi(["function test(bytes data) public"])

const callPolicy = toCallPolicy({
policyVersion: CallPolicyVersion.V0_0_5,
permissions: [
{
abi: testAbi,
target: zeroAddress,
functionName: "test",
args: [
{
condition: ParamCondition.SLICE_EQUAL,
value: "0xff",
start: 1,
length: 1
}
]
}
]
})

const permissionSmartAccountClient = await getKernelAccountClient({
account: await getSignerToPermissionKernelAccount([callPolicy]),
paymaster: zeroDevPaymaster
})

const invalidCallData = encodeFunctionData({
abi: testAbi,
functionName: "test",
args: ["0x00ee"]
})

await expect(
permissionSmartAccountClient.sendTransaction({
to: zeroAddress,
data: invalidCallData
})
).rejects.toThrow()
},
TEST_TIMEOUT
)

test(
"should fail with SLICE_EQUAL condition, start and length not provided",
async () => {
const testAbi = parseAbi(["function test(bytes data) public"])

expect(() =>
toCallPolicy({
policyVersion: CallPolicyVersion.V0_0_5,
permissions: [
{
abi: testAbi,
target: zeroAddress,
functionName: "test",
args: [
{
condition: ParamCondition.SLICE_EQUAL,
value: "0xff"
}
]
}
]
})
).toThrow("start and length are required for SLICE_EQUAL condition")
},
TEST_TIMEOUT
)

test(
"should fail with SLICE_EQUAL condition, value is not equal to the given length",
async () => {
const testAbi = parseAbi(["function test(bytes data) public"])

expect(() =>
toCallPolicy({
policyVersion: CallPolicyVersion.V0_0_5,
permissions: [
{
abi: testAbi,
target: zeroAddress,
functionName: "test",
args: [
{
condition: ParamCondition.SLICE_EQUAL,
value: "0x00ff",
start: 1,
length: 1
}
]
}
]
})
).toThrow("Value length is not equal to the given length")
},
TEST_TIMEOUT
)

test(
"should fail with SLICE_EQUAL condition, value is too short",
async () => {
const testAbi = parseAbi(["function test(bytes data) public"])

expect(() =>
toCallPolicy({
policyVersion: CallPolicyVersion.V0_0_5,
permissions: [
{
abi: testAbi,
target: zeroAddress,
functionName: "test",
args: [
{
condition: ParamCondition.SLICE_EQUAL,
value: "0xffff",
start: 1,
length: 2
}
]
}
]
})
).toThrow("Value is too short for the given start and length")
},
TEST_TIMEOUT
)

test(
"Smart account client send transaction with serialization/deserialization",
async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/test/v0.7/weightedValidatorWithPermission.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ describe("weightedValidator", () => {
)

const callPolicy = toCallPolicy({
policyVersion: CallPolicyVersion.V0_0_4,
policyVersion: CallPolicyVersion.V0_0_5,
permissions: [
{
target: zeroAddress,
Expand Down
7 changes: 7 additions & 0 deletions plugins/permission/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ export const CALL_POLICY_CONTRACT_V0_0_3 =
export const CALL_POLICY_CONTRACT_V0_0_4 =
"0x9a52283276A0ec8740DF50bF01B28A80D880eaf2"

/**
* @dev CALL_POLICY_CONTRACT_V0_0_5 updates
* - Added SLICE_EQUAL condition
*/
export const CALL_POLICY_CONTRACT_V0_0_5 =
"0x85770b902D1e503D5f5141d9eaC16d0d08eEaDd2"

export const GAS_POLICY_CONTRACT = "0xaeFC5AbC67FfD258abD0A3E54f65E70326F84b23"
export const RATE_LIMIT_POLICY_CONTRACT =
"0xf63d4139B25c836334edD76641356c6b74C86873"
Expand Down
Loading
Loading