Skip to content

Commit

Permalink
Moving core Action types to Zod schema as source of truth (#197)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattschoch authored Mar 28, 2024
1 parent a6ef980 commit c6b9a6a
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 40 deletions.
1 change: 0 additions & 1 deletion packages/policy-engine-shared/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from './lib/schema/action.schema'
export * from './lib/schema/address.schema'
export * from './lib/schema/data-store.schema'
export * from './lib/schema/domain.schema'
Expand Down
91 changes: 71 additions & 20 deletions packages/policy-engine-shared/src/lib/type/action.type.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Hex } from 'viem'
import { Address, JwtString, TransactionRequest } from './domain.type'
import { z } from 'zod'
import { addressSchema } from '../schema/address.schema'
import { hexSchema } from '../schema/hex.schema'
import { Address, JwtString } from './domain.type'
import {
AccountClassification,
AccountType,
Expand Down Expand Up @@ -67,26 +69,75 @@ export type BaseAdminRequest = {
approvals: JwtString[]
}

export type SignTransactionAction = BaseAction & {
action: typeof Action.SIGN_TRANSACTION
resourceId: string
transactionRequest: TransactionRequest
}
export const AccessList = z.array(
z.object({
address: addressSchema,
storageKeys: z.array(hexSchema)
})
)
export type AccessList = z.infer<typeof AccessList>

export const ActionSchema = z.nativeEnum(Action)

export const BaseActionSchema = z.object({
action: ActionSchema,
nonce: z.string()
})
export type BaseActionSchema = z.infer<typeof BaseActionSchema>

export const TransactionRequest = z.object({
chainId: z.number(),
from: addressSchema,
nonce: z.number().optional(),
accessList: AccessList.optional(),
data: hexSchema.optional(),
gas: z.coerce.bigint().optional(),
maxFeePerGas: z.coerce.bigint().optional(),
maxPriorityFeePerGas: z.coerce.bigint().optional(),
to: addressSchema.nullable().optional(),
type: z.literal('2').optional(),
value: hexSchema.optional()
})
export type TransactionRequest = z.infer<typeof TransactionRequest>

export const SignTransactionAction = z.intersection(
BaseActionSchema,
z.object({
action: z.literal(Action.SIGN_TRANSACTION),
resourceId: z.string(),
transactionRequest: TransactionRequest
})
)
export type SignTransactionAction = z.infer<typeof SignTransactionAction>

// Matching viem's SignableMessage options https://viem.sh/docs/actions/wallet/signMessage#message
export type SignableMessage = string | { raw: Hex }

export type SignMessageAction = BaseAction & {
action: typeof Action.SIGN_MESSAGE
resourceId: string
message: SignableMessage
}

export type SignTypedDataAction = BaseAction & {
action: typeof Action.SIGN_TYPED_DATA
resourceId: string
typedData: string
}
export const SignableMessage = z.union([
z.string(),
z.object({
raw: hexSchema
})
])
export type SignableMessage = z.infer<typeof SignableMessage>

export const SignMessageAction = z.intersection(
BaseActionSchema,
z.object({
action: z.literal(Action.SIGN_MESSAGE),
resourceId: z.string(),
message: SignableMessage
})
)
export type SignMessageAction = z.infer<typeof SignMessageAction>

export const SignTypedDataAction = z.intersection(
BaseActionSchema,
z.object({
action: z.literal(Action.SIGN_TYPED_DATA),
resourceId: z.string(),
typedData: z.string()
})
)
export type SignTypedDataAction = z.infer<typeof SignTypedDataAction>

export type CreateOrganizationAction = BaseAction & {
action: typeof Action.CREATE_ORGANIZATION
Expand Down
19 changes: 0 additions & 19 deletions packages/policy-engine-shared/src/lib/type/domain.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,22 +165,3 @@ export type EvaluationResponse = {
export type Hex = `0x${string}` // DOMAIN

export type Address = `0x${string}` // DOMAIN

export type AccessList = {
address: Address
storageKeys: Hex[]
}[] // DOMAIN

export type TransactionRequest = {
chainId: number
from: Address
nonce?: number
accessList?: AccessList
data?: Hex
gas?: bigint
maxFeePerGas?: bigint
maxPriorityFeePerGas?: bigint
to?: Address | null
type?: '2'
value?: Hex
}

0 comments on commit c6b9a6a

Please sign in to comment.