Skip to content

Commit

Permalink
used decoderErros
Browse files Browse the repository at this point in the history
  • Loading branch information
Ptroger committed Feb 7, 2024
1 parent 0948a99 commit 744b769
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ const decode = ({ input, config = defaultConfig }: { input: DecodeInput; config?
payload: input.raw.rawData
}
default:
throw new Error('Invalid input type')
throw new DecoderError({ message: 'Invalid input type', status: 400 })
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ContractCallInput, Intents } from '../../../domain'
import { DecoderError } from '../../../error'
import { ApproveAllowanceParams } from '../../../extraction/types'
import { ApproveTokenAllowance } from '../../../intent.types'
import { MethodsMapping } from '../../../supported-methods'
Expand All @@ -13,12 +14,12 @@ export const decodeApproveTokenAllowance = (
const { from, to, chainId, data, methodId } = input

if (!isSupportedMethodId(methodId)) {
throw new Error('Unsupported methodId')
throw new DecoderError({ message: 'Unsupported methodId', status: 400 })
}

const params = extract(supportedMethods, data, methodId) as ApproveAllowanceParams
if (!params) {
throw new Error('Params do not match ERC20 transfer methodId')
throw new DecoderError({ message: 'Params do not match ERC20 transfer methodId', status: 400 })
}

const { amount, spender } = params
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { AssetType, Hex, toAccountId, toAssetId } from '@narval/authz-shared'
import { Address } from 'viem'
import { ContractCallInput, Intents } from '../../../domain'
import { DecoderError } from '../../../error'
import { Erc1155SafeTransferFromParams, SafeBatchTransferFromParams } from '../../../extraction/types'
import { ERC1155Transfer, TransferErc1155 } from '../../../intent.types'
import { MethodsMapping, SupportedMethodId } from '../../../supported-methods'
Expand All @@ -10,7 +11,7 @@ import { extract } from '../../utils'
export const decodeERC1155Transfer = (input: ContractCallInput, supportedMethods: MethodsMapping): TransferErc1155 => {
const { to: contract, from, data, chainId, methodId } = input
if (!isSupportedMethodId(methodId)) {
throw new Error('Unsupported methodId')
throw new DecoderError({ message: 'Unsupported methodId', status: 400 })
}

const params = extract(supportedMethods, data, methodId)
Expand All @@ -31,8 +32,7 @@ export const decodeERC1155Transfer = (input: ContractCallInput, supportedMethods

return constructTransferErc1155Intent({ to, from, contract, transfers, chainId })
}

throw new Error('Params do not match ERC1155 transfer methodId')
throw new DecoderError({ message: 'Params do not match ERC1155 transfer methodId', status: 400 })
}

function createERC1155Transfer({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ContractCallInput, Intents } from '../../../domain'
import { DecoderError } from '../../../error'
import { TransferParams } from '../../../extraction/types'
import { TransferErc20 } from '../../../intent.types'
import { MethodsMapping } from '../../../supported-methods'
Expand All @@ -9,7 +10,7 @@ import { extract } from '../../utils'
export const decodeErc20Transfer = (input: ContractCallInput, supportedMethods: MethodsMapping): TransferErc20 => {
const { from, to, chainId, data, methodId } = input
if (!isSupportedMethodId(methodId)) {
throw new Error('Unsupported methodId')
throw new DecoderError({ message: 'Unsupported methodId', status: 400 })
}

const params = extract(supportedMethods, data, methodId) as TransferParams
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { AssetType } from '@narval/authz-shared'
import { ContractCallInput, Intents } from '../../../domain'
import { DecoderError } from '../../../error'
import { Erc721SafeTransferFromParams } from '../../../extraction/types'
import { TransferErc721 } from '../../../intent.types'
import { MethodsMapping } from '../../../supported-methods'
Expand All @@ -10,7 +11,7 @@ import { extract } from '../../utils'
export const decodeErc721Transfer = (input: ContractCallInput, supportedMethods: MethodsMapping): TransferErc721 => {
const { to: contract, from, chainId, data, methodId } = input
if (!isSupportedMethodId(methodId)) {
throw new Error('Unsupported methodId')
throw new DecoderError({ message: 'Unsupported methodId', status: 400 })
}

const params = extract(supportedMethods, data, methodId) as Erc721SafeTransferFromParams
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Address } from '@narval/authz-shared'
import { Hex, toHex } from 'viem'
import { ContractCallInput, InputType, Intents } from '../../../domain'
import { DecoderError } from '../../../error'
import { ExecuteAndRevertParams, ExecuteParams, HandleOpsParams } from '../../../extraction/types'
import { Intent, UserOperation } from '../../../intent.types'
import { MethodsMapping, SupportedMethodId } from '../../../supported-methods'
Expand Down Expand Up @@ -69,7 +70,7 @@ const decodeExecuteAndRevert = (
export const decodeUserOperation = (input: ContractCallInput, supportedMethods: MethodsMapping): UserOperation => {
const { from, chainId, data, to, methodId } = input
if (!isSupportedMethodId(methodId)) {
throw new Error('Unsupported methodId')
throw new DecoderError({ message: 'Unsupported methodId', status: 400 })
}

const params = extract(supportedMethods, data, methodId) as HandleOpsParams
Expand Down
7 changes: 5 additions & 2 deletions packages/transaction-request-intent/src/lib/decoders/utils.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { Hex, decodeAbiParameters } from 'viem'
import { DecoderError } from '../error'
import { ExtractedParams } from '../extraction/types'
import { MethodsMapping, SupportedMethodId } from '../supported-methods'

export const getMethod = (supportedMethods: MethodsMapping, methodId: SupportedMethodId) => {
const method = supportedMethods[methodId]
if (!method) throw new Error('Unsupported methodId')
if (!method) {
throw new DecoderError({ message: 'Unsupported methodId', status: 400 })
}
return method
}

Expand All @@ -14,6 +17,6 @@ export const extract = (supportedMethods: MethodsMapping, data: Hex, methodId: S
const params = decodeAbiParameters(method.abi, data)
return method.transformer(params)
} catch (error) {
throw new Error(`Failed to decode abi parameters: ${error}`)
throw new DecoderError({ message: 'Failed to decode abi parameters', status: 400, context: { error } })
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { DecoderError } from '../error'
import { assertAddress, assertArray, assertBigInt, assertHexString, assertLowerHexString } from '../typeguards'
import {
ApproveAllowanceParams,
Expand Down Expand Up @@ -83,7 +84,7 @@ export const ExecuteAndRevertParamsTransform = (params: unknown[]): ExecuteAndRe

export const transformUserOperation = (op: unknown[]): UserOp => {
if (typeof op !== 'object' || op === null) {
throw new Error('UserOperation is not an object')
throw new DecoderError({ message: 'UserOperation is not an object', status: 400 })
}

return {
Expand All @@ -103,7 +104,7 @@ export const transformUserOperation = (op: unknown[]): UserOp => {

export const HandleOpsParamsTransform = (params: unknown[]): HandleOpsParams => {
if (!Array.isArray(params[0]) || typeof params[1] !== 'string') {
throw new Error('Invalid input format')
throw new DecoderError({ message: 'Invalid input format', status: 400 })
}
return {
userOps: params[0].map(transformUserOperation),
Expand Down
15 changes: 8 additions & 7 deletions packages/transaction-request-intent/src/lib/typeguards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Address, AssetType, Hex } from '@narval/authz-shared'
// eslint-disable-next-line no-restricted-imports
import { isAddress } from 'viem'
import { AssetTypeAndUnknown, Misc, Permit2Message, PermitMessage } from './domain'
import { DecoderError } from './error'
import { SupportedMethodId } from './supported-methods'

export const isString = (value: unknown): value is string => {
Expand All @@ -16,7 +17,7 @@ export const assertBigInt = (value: unknown): bigint => {
if (isBigInt(value)) {
return value
}
throw new Error('Value is not a bigint')
throw new DecoderError({ message: 'Value is not a bigint', status: 400 })
}

export function isHexString(value: unknown): value is Hex {
Expand All @@ -27,7 +28,7 @@ export const assertHexString = (value: unknown): Hex => {
if (isHexString(value)) {
return value
}
throw new Error('Value is not a hex string')
throw new DecoderError({ message: 'Value is not a hex string', status: 400 })
}

export const assertLowerHexString = (value: unknown): Hex => {
Expand All @@ -38,7 +39,7 @@ export function assertString(value: unknown): string {
if (isString(value)) {
return value
}
throw new Error('Value is not a string')
throw new DecoderError({ message: 'Value is not a string', status: 400 })
}

// Checks if a value is a number
Expand All @@ -50,7 +51,7 @@ export const assertNumber = (value: unknown): number => {
if (isNumber(value)) {
return value
}
throw new Error('Value is not a number')
throw new DecoderError({ message: 'Value is not a number', status: 400 })
}

// Checks if a value is a boolean
Expand All @@ -62,7 +63,7 @@ export const assertBoolean = (value: unknown): boolean => {
if (isBoolean(value)) {
return value
}
throw new Error('Value is not a boolean')
throw new DecoderError({ message: 'Value is not a boolean', status: 400 })
}

// Checks if a value is an array
Expand All @@ -76,7 +77,7 @@ export const isSupportedMethodId = (value: Hex): value is SupportedMethodId => {

export const assertAddress = (value: unknown): Address => {
if (!isString(value) || !isAddress(value)) {
throw new Error('Value is not an address')
throw new DecoderError({ message: 'Value is not an address', status: 400 })
}
return value.toLowerCase() as Address
}
Expand All @@ -92,7 +93,7 @@ export const isAssetType = (value: unknown): value is AssetTypeAndUnknown => {

export const assertArray = <T>(value: unknown, type: AssertType): T[] => {
if (!Array.isArray(value)) {
throw new Error('Value is not an array')
throw new DecoderError({ message: 'Value is not an array', status: 400 })
}
switch (type) {
case 'string': {
Expand Down
30 changes: 23 additions & 7 deletions packages/transaction-request-intent/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export const buildContractRegistryEntry = ({
}): { [key: AccountId]: AssetTypeAndUnknown } => {
const registry: { [key: AccountId]: AssetTypeAndUnknown } = {}
if (!isAddress(contractAddress) || !isAssetType(assetType)) {
throw new Error('Invalid contract registry entry')
throw new DecoderError({ message: 'Invalid contract registry entry', status: 400 })
}
const key = buildContractKey(chainId, contractAddress as Address)
registry[key] = assetType
Expand All @@ -79,7 +79,11 @@ export const buildContractRegistry = (input: ContractRegistryInput): ContractReg
}
if (isString(contract)) {
if (!isAccountId(contract)) {
throw new Error(`Contract registry key is not a valid Caip10: ${contract}`)
throw new DecoderError({
message: 'Contract registry key is not a valid Caip10',
status: 400,
context: { contract, input }
})
}
registry.set(contract.toLowerCase(), information)
} else {
Expand All @@ -99,10 +103,18 @@ export const buildContractKey = (
export const checkContractRegistry = (registry: Record<string, string>) => {
Object.keys(registry).forEach((key) => {
if (!isAccountId(key)) {
throw new Error(`Invalid contract registry key: ${key}: ${registry[key]}`)
throw new DecoderError({
message: 'Invalid contract registry key',
status: 400,
context: { key, value: registry[key] }
})
}
if (!isAssetType(registry[key])) {
throw new Error(`Invalid contract registry value: ${key}: ${registry[key]}`)
throw new DecoderError({
message: 'Invalid contract registry value',
status: 400,
context: { key, value: registry[key] }
})
}
})
return true
Expand All @@ -122,7 +134,9 @@ export const contractTypeLookup = (
}

export const buildTransactionKey = (txRequest: TransactionRequest): TransactionKey => {
if (!txRequest.nonce) throw new Error('nonce needed to build transaction key')
if (!txRequest.nonce) {
throw new DecoderError({ message: 'nonce needed to build transaction key', status: 400 })
}
const account = toAccountId({
chainId: txRequest.chainId,
address: txRequest.from,
Expand Down Expand Up @@ -152,7 +166,7 @@ export const decodeTypedData = (typedData: TypedData): SignTypedData => ({

export const decodeMessage = (message: MessageInput): SignMessage => {
if (!message.payload.startsWith(presignMessagePrefix)) {
throw new Error('Invalid message prefix')
throw new DecoderError({ message: 'Invalid message prefix', status: 400 })
}
return {
type: Intents.SIGN_MESSAGE,
Expand Down Expand Up @@ -316,6 +330,8 @@ export const nativeCaip19 = (chainId: number): AssetId => {

export const getMethod = (methodId: SupportedMethodId, supportedMethods: MethodsMapping) => {
const method = supportedMethods[methodId]
if (!method) throw new Error('Unsupported methodId')
if (!method) {
throw new DecoderError({ message: 'Unsupported methodId', status: 400 })
}
return method
}

0 comments on commit 744b769

Please sign in to comment.