Skip to content

Commit

Permalink
Merge pull request #47 from rhinestonewtf/sudo-policy
Browse files Browse the repository at this point in the history
Integrate smart session policies
  • Loading branch information
kopy-kat authored Aug 28, 2024
2 parents ea62632 + 3cda70a commit e2dad4d
Show file tree
Hide file tree
Showing 13 changed files with 244 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const SPENDING_LIMITS_POLICY_ADDRESS =
'0xcc8d69271fa62045af45fdcc62cdc9c317a9e745'
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './constants'
export * from './installation'
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { SPENDING_LIMITS_POLICY_ADDRESS } from './constants'
import { Policy } from '../../../types'
import { Address, encodeAbiParameters } from 'viem'

type Params = {
token: Address
limit: bigint
}[]

export const getSpendingLimitsPolicy = (params: Params): Policy => {
return {
address: SPENDING_LIMITS_POLICY_ADDRESS,
initData: encodeAbiParameters(
[{ type: 'address[]' }, { type: 'uint256[]' }],
[params.map(({ token }) => token), params.map(({ limit }) => limit)],
),
deInitData: '0x',
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const SUDO_POLICY_ADDRESS = '0x40235179bdd60beb6730e03d1aa91eae9b85928e'
2 changes: 2 additions & 0 deletions src/module/smart-sessions/policies/sudo-policy/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './installation'
export * from './constants'
10 changes: 10 additions & 0 deletions src/module/smart-sessions/policies/sudo-policy/installation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { SUDO_POLICY_ADDRESS } from './constants'
import { Policy } from '../../../types'

export const getSudoPolicy = (): Policy => {
return {
address: SUDO_POLICY_ADDRESS,
initData: '0x',
deInitData: '0x',
}
}
58 changes: 58 additions & 0 deletions src/module/smart-sessions/policies/universal-action-policy/abi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
export const abi = [
{
components: [
{
name: 'valueLimitPerUse',
type: 'uint256',
},
{
components: [
{
name: 'length',
type: 'uint256',
},
{
components: [
{
name: 'condition',
type: 'uint8',
},
{
name: 'offset',
type: 'uint64',
},
{
name: 'isLimited',
type: 'bool',
},
{
name: 'ref',
type: 'bytes32',
},
{
components: [
{
name: 'limit',
type: 'uint256',
},
{
name: 'used',
type: 'uint256',
},
],
name: 'usage',
type: 'tuple',
},
],
name: 'rules',
type: 'tuple[]',
},
],
name: 'paramRules',
type: 'tuple',
},
],
name: 'ActionConfig',
type: 'tuple',
},
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const UNIVERSAL_ACTION_POLICY_ADDRESS =
'0xf87db2a1b6ac548ad5352bcc550ce373ba826bb6'

export const MAX_RULES = 16
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './constants'
export * from './installation'
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { MAX_RULES, UNIVERSAL_ACTION_POLICY_ADDRESS } from './constants'
import { Policy } from '../../../types'
import { encodeAbiParameters } from 'viem'
import { abi } from './abi'
import { ActionConfig } from './types'

export const getUniversalActionPolicy = (
actionConfig: ActionConfig,
): Policy => {
if (actionConfig.paramRules.rules.length > MAX_RULES) {
throw new Error(`Max number of rules is ${MAX_RULES}`)
}

return {
address: UNIVERSAL_ACTION_POLICY_ADDRESS,
initData: encodeAbiParameters(abi, [
{
valueLimitPerUse: actionConfig.valueLimitPerUse,
paramRules: {
length: actionConfig.paramRules.length,
rules: actionConfig.paramRules.rules,
},
},
]),
deInitData: '0x',
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Hex } from 'viem'

export type ActionConfig = {
valueLimitPerUse: bigint
paramRules: ParamRules
}

export type ParamRules = {
length: number
rules: ParamRule[]
}

export type ParamRule = {
condition: ParamCondition
offset: number
isLimited: boolean
ref: Hex
usage: LimitUsage
}

export type LimitUsage = {
limit: bigint
used: bigint
}

export enum ParamCondition {
EQUAL,
GREATER_THAN,
LESS_THAN,
GREATER_THAN_OR_EQUAL,
LESS_THAN_OR_EQUAL,
NOT_EQUAL,
}
6 changes: 6 additions & 0 deletions src/module/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,9 @@ export type {
export type { SigHookInit } from './hook-multi-plexer'

export type { Validator } from './multi-factor-validator'

export type Policy = {
address: Address
initData: Hex
deInitData: Hex
}
78 changes: 78 additions & 0 deletions test/unit/module/smartSessions/smartSessionPolicies.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { getSpendingLimitsPolicy } from 'src/module/smart-sessions/policies/spending-limits-policy'
import { getSudoPolicy } from 'src/module/smart-sessions/policies/sudo-policy'
import {
getUniversalActionPolicy,
UNIVERSAL_ACTION_POLICY_ADDRESS,
} from 'src/module/smart-sessions/policies/universal-action-policy'
import {
ActionConfig,
ParamCondition,
} from 'src/module/smart-sessions/policies/universal-action-policy/types'
import { zeroAddress } from 'viem'

describe('Smart Sessions Polices', () => {
// -----------------------
// Universal Action Policy
// -----------------------
it('should get install universal action policy', async () => {
// Setup
const actionConfigData: ActionConfig = {
valueLimitPerUse: BigInt(1000),
paramRules: {
length: 2,
rules: [
{
condition: ParamCondition.EQUAL,
offset: 0,
isLimited: true,
ref: '0x00000000000000000000000000000000000000000000000000000000000003e8', // 1000 in bytes32
usage: {
limit: BigInt(1000),
used: BigInt(10),
},
},
{
condition: ParamCondition.LESS_THAN,
offset: 32,
isLimited: false,
ref: '0x00000000000000000000000000000000000000000000000000000000000007d0', // 2000 in bytes32
usage: {
limit: BigInt(2000),
used: BigInt(100),
},
},
],
},
}
const installUniversalPolicy = getUniversalActionPolicy(actionConfigData)

expect(installUniversalPolicy.address).toEqual(
UNIVERSAL_ACTION_POLICY_ADDRESS,
)
expect(installUniversalPolicy.initData).toBeDefined()
expect(installUniversalPolicy.deInitData).toEqual('0x')
})

// -----------------------
// Sudo Action Policy
// -----------------------
it('should get install sudo action policy', async () => {
const installSudoActionPolicy = getSudoPolicy()
expect(installSudoActionPolicy.address).toBeDefined()
expect(installSudoActionPolicy.initData).toEqual('0x')
expect(installSudoActionPolicy.deInitData).toEqual('0x')
})

// -----------------------
// Spending Limit Policy
// -----------------------
it('should get install spending limit policy', async () => {
const installSpendingLimitPolicy = getSpendingLimitsPolicy([
{ limit: BigInt(1000), token: zeroAddress },
])

expect(installSpendingLimitPolicy.address).toBeDefined()
expect(installSpendingLimitPolicy.initData).toBeDefined()
expect(installSpendingLimitPolicy.deInitData).toEqual('0x')
})
})

0 comments on commit e2dad4d

Please sign in to comment.