Skip to content

Commit

Permalink
chore: update package
Browse files Browse the repository at this point in the history
  • Loading branch information
joepegler committed Dec 4, 2024
1 parent eba0270 commit 855f575
Show file tree
Hide file tree
Showing 15 changed files with 470 additions and 231 deletions.
30 changes: 28 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
[![Biconomy](https://img.shields.io/badge/Made_with_%F0%9F%8D%8A_by-Biconomy-ff4e17?style=flat)](https://biconomy.io) [![License MIT](https://img.shields.io/badge/License-MIT-blue?&style=flat)](./LICENSE) [![codecov](https://codecov.io/github/bcnmy/sdk/graph/badge.svg?token=DTdIR5aBDA)](https://codecov.io/github/bcnmy/sdk)



# SDK 🚀

[![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/bcnmy/sdk)
Expand Down Expand Up @@ -55,6 +53,34 @@ const { status, transactionHash } = await nexusClient.waitForTransactionReceipt(

```

3. Testing

To run the tests, ensure you have the following prerequisites installed:

- Node.js v22 or higher
- [Bun](https://bun.sh/) package manager
- [Foundry](https://book.getfoundry.sh/getting-started/installation)

Install the dependencies:

```bash
bun install --frozen-lockfile
```

### Run all tests

```bash
bun run test
```

### Run tests for a specific module

```bash
bun run test -t=smartSessions
```

For detailed information about the testing framework, network configurations, and debugging guidelines, please refer to our [Testing Documentation](./src/test/README.md).

## Documentation and Resources

For a comprehensive understanding of our project and to contribute effectively, please refer to the following resources:
Expand Down
Binary file modified bun.lockb
Binary file not shown.
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@
"typedoc": "^0.25.9",
"viem": "2.21.6",
"vitest": "^1.3.1",
"yargs": "^17.7.2"
"yargs": "^17.7.2",
"@rhinestone/module-sdk": "^0.1.28"

},
"peerDependencies": {
"typescript": "^5",
"viem": "^2.20.0",
"@rhinestone/module-sdk": "^0.1.25"
"@rhinestone/module-sdk": "^0.1.28"
},
"exports": {
".": {
Expand Down
66 changes: 56 additions & 10 deletions src/sdk/constants/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import type { Hex } from "viem"
import {
getSpendingLimitsPolicy,
getTimeFramePolicy,
getUniversalActionPolicy,
getUsageLimitPolicy,
getValueLimitPolicy
} from "@rhinestone/module-sdk"
import { type Hex, toBytes, toHex } from "viem"
import { isTesting } from "../account"
import { ParamCondition } from "../modules/smartSessionsValidator/Types"

export * from "./abi"
export {
SMART_SESSIONS_ADDRESS,
OWNABLE_VALIDATOR_ADDRESS,
OWNABLE_EXECUTOR_ADDRESS,
MOCK_ATTESTER_ADDRESS,
REGISTRY_ADDRESS
} from "@rhinestone/module-sdk"

// export const SIMPLE_SESSION_VALIDATOR_ADDRESS: Hex = OWNABLE_VALIDATOR_ADDRESS
export const SIMPLE_SESSION_VALIDATOR_ADDRESS: Hex =
"0x41f143f4B5f19AfCd2602F6ADE18E75e9b5E37d3"
export const ENTRY_POINT_ADDRESS: Hex =
Expand All @@ -19,8 +22,6 @@ export const TIMEFRAME_POLICY_ADDRESS: Hex =
"0x0B7BB9bD65858593D97f12001FaDa94828307805"
export const NEXUS_BOOTSTRAP_ADDRESS: Hex =
"0x00000008c901d8871b6F6942De0B5D9cCf3873d3"
export const UNIVERSAL_ACTION_POLICY_ADDRESS: Hex =
"0x148CD6c24F4dd23C396E081bBc1aB1D92eeDe2BF"

export const TEST_ADDRESS_NEXUS_IMPLEMENTATION_ADDRESS: Hex =
"0x3AdEa1898eb7d9FbD49242618782717A1f86DA14"
Expand All @@ -45,3 +46,48 @@ export const k1ValidatorFactoryAddress: Hex = isTesting()
export const k1ValidatorAddress: Hex = isTesting()
? TEST_ADDRESS_K1_VALIDATOR_ADDRESS
: MAINNET_ADDRESS_K1_VALIDATOR_ADDRESS

// Rhinestone constants
export {
SMART_SESSIONS_ADDRESS,
OWNABLE_VALIDATOR_ADDRESS,
OWNABLE_EXECUTOR_ADDRESS,
MOCK_ATTESTER_ADDRESS,
RHINESTONE_ATTESTER_ADDRESS,
REGISTRY_ADDRESS
} from "@rhinestone/module-sdk"

// Rhinestone doesn't export the universal action policy address, so we need to get it from the policies
export const UNIVERSAL_ACTION_POLICY_ADDRESS: Hex = getUniversalActionPolicy({
valueLimitPerUse: 0n,
paramRules: {
length: 16,
rules: new Array(16).fill({
condition: ParamCondition.EQUAL,
isLimited: false,
offset: 0,
ref: toHex(toBytes("0x", { size: 32 })),
usage: { limit: BigInt(0), used: BigInt(0) }
})
}
}).address

export const TIME_FRAME_POLICY_ADDRESS: Hex = getTimeFramePolicy({
validUntil: 0,
validAfter: 0
}).address

export const VALUE_LIMIT_POLICY_ADDRESS: Hex = getValueLimitPolicy({
limit: 0n
}).address

export const USAGE_LIMIT_POLICY_ADDRESS: Hex = getUsageLimitPolicy({
limit: 0n
}).address

export const SPENDING_LIMITS_POLICY_ADDRESS: Hex = getSpendingLimitsPolicy([
{
token: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
limit: 0n
}
]).address
119 changes: 33 additions & 86 deletions src/sdk/modules/smartSessionsValidator/Helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,12 @@ import { parseReferenceValue } from "../utils/Helpers"
import type { AnyData } from "../utils/Types"
import type {
ActionConfig,
ActionPolicyData,
ActionPolicyInfo,
CreateSessionDataParams,
FullCreateSessionDataParams,
RawActionConfig,
Rule,
SpendingLimitsParams,
SudoPolicyData
ResolvedActionPolicyInfo,
Rule
} from "./Types"

export const MAX_RULES = 16
Expand Down Expand Up @@ -94,23 +93,6 @@ export const applyDefaults = (
sessionInfo.sessionValidatorAddress ?? SIMPLE_SESSION_VALIDATOR_ADDRESS
}
}
/**
* Creates an ActionData object for a sudo policy.
*
* @param contractAddress - The address of the contract.
* @param functionSelector - The function selector or AbiFunction.
* @returns An ActionData object.
*/
export const createSudoData = (
contractAddress: Address,
functionSelector: string | AbiFunction
): ActionData => ({
actionTargetSelector: (typeof functionSelector === "string"
? functionSelector
: functionSelector.name) as Hex,
actionTarget: contractAddress,
actionPolicies: [getSudoPolicy()]
})

/**
* Creates an ActionData object.
Expand Down Expand Up @@ -206,13 +188,14 @@ export const isPermissionEnabled = async ({
client: PublicClient
accountAddress: Address
permissionId: Hex
}) =>
client.readContract({
}) => {
return client.readContract({
address: SMART_SESSIONS_ADDRESS,
abi: SmartSessionAbi,
functionName: "isPermissionEnabled",
args: [permissionId, accountAddress]
})
}

/**
* Converts an ActionConfig to a UniversalActionPolicy.
Expand Down Expand Up @@ -260,34 +243,6 @@ export const toTimeRangePolicy = (
return timeFramePolicyData
}

/**
* Converts SpendingLimitsParams to a SpendingLimitsPolicy.
*
* @param params - An array of SpendingLimitsParams.
* @returns A PolicyData object representing the SpendingLimitsPolicy.
*/
export const toSpendingLimitsPolicy = (
params: SpendingLimitsParams
): PolicyData => {
return {
policy: "0x8e58f4945e6ba2a11b184a9c20b6c765a0891b95",
initData: encodeAbiParameters(
[{ type: "address[]" }, { type: "uint256[]" }],
[params.map(({ token }) => token), params.map(({ limit }) => limit)]
)
}
}

/**
* An object containing policy conversion functions.
*/
export const policies = {
to: {
universalAction: toUniversalActionPolicy,
spendingLimits: toSpendingLimitsPolicy
}
} as const

/**
* Stringifies an object, explicitly tagging BigInt values.
*
Expand Down Expand Up @@ -357,47 +312,39 @@ export const getTrustedAttesters = async ({
}

/**
* Converts an ABI to a list of ActionPolicyData objects.
* Converts an ABI to a list of ActionPolicyInfo objects.
*
* @param params - The parameters object
* @param params.abi - The ABI to convert
* @param params.actionPolicyData - The ActionPolicyData object to apply to each function in the ABI
* @returns An array of ActionPolicyData objects
*/
export const abi2ActionPolicy = ({
abi,
actionPolicyData
}: {
abi: Abi
actionPolicyData: Omit<ActionPolicyData, "functionSelector"> & {
rules?: never // Rules should not be available here because they should be custom per method, not used in a loop
}
}): ActionPolicyData[] =>
abi
.filter((item): item is AbiFunction => item.type === "function")
.map((func) => ({
...actionPolicyData,
functionSelector: toFunctionSelector(func)
}))

/**
* Converts an ABI to a list of SudoPolicyData objects.
* @param params.actionPolicyInfo - The ActionPolicyInfo object to apply to each function in the ABI
*
* @param params - The parameters object
* @param params.abi - The ABI to convert
* @param params.contractAddress - The address of the contract
* @returns An array of SudoPolicyData objects
* @example
* const actionPoliciesInfo = abiToPoliciesInfo({
* abi: CounterAbi,
* actionPolicyInfo: {
* contractAddress: testAddresses.Counter,
* sudo: false,
* tokenLimits: [],
* usageLimit: 1000n,
* valueLimit: 1000n
* }
* })
* @returns An array of ActionPolicyInfo objects
*/
export const abi2SudoPolicy = ({

export type AbiToPoliciesInfoParams = Omit<
ActionPolicyInfo,
"functionSelector" | "rules"
> & { abi: Abi }

export const abiToPoliciesInfo = ({
abi,
contractAddress
}: {
abi: Abi
contractAddress: Address
}): SudoPolicyData[] =>
abi
...actionPolicyInfo
}: AbiToPoliciesInfoParams): ResolvedActionPolicyInfo[] =>
(abi ?? [])
.filter((item): item is AbiFunction => item.type === "function")
.map((func) => ({
contractAddress,
functionSelector: toFunctionSelector(func)
...actionPolicyInfo,
functionSelector: toFunctionSelector(func),
rules: [] // Rules should not be available here because they should be custom per method, not used in a loop
}))
Loading

0 comments on commit 855f575

Please sign in to comment.