Skip to content

Commit

Permalink
added test for safeDecode and safe function
Browse files Browse the repository at this point in the history
  • Loading branch information
Ptroger committed Jan 12, 2024
1 parent 325346c commit 86425a6
Show file tree
Hide file tree
Showing 11 changed files with 225 additions and 126 deletions.
39 changes: 29 additions & 10 deletions apps/authz/src/shared/module/persistence/mock_data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@ import {
Wallet,
WalletGroup
} from '@app/authz/shared/types/entities.types'
import { AccountType, BlockchainActions, Intents, ResourceActions, UserRoles } from '@app/authz/shared/types/enums'
import { AccountType, BlockchainActions, ResourceActions, UserRoles } from '@app/authz/shared/types/enums'
import { AuthZRequestPayload, TransactionRequest } from '@app/authz/shared/types/http'
import { TransferNative } from '@app/authz/shared/types/intents'
import { RegoInput } from '@app/authz/shared/types/rego'
import { safeDecode } from '@narval/transaction-request-intent'
import { Caip19 } from 'packages/transaction-request-intent/src/lib/caip'
import { Intents } from 'packages/transaction-request-intent/src/lib/domain'
import { Intent, TransferNative } from 'packages/transaction-request-intent/src/lib/intent.types'
import { Address, toHex } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'

export const ONE_ETH = 1000000000000000000n
export const ONE_ETH = BigInt('1000000000000000000')

export const USDC_TOKEN = {
uid: 'eip155:137/erc20:0x2791bca1f2de4661ed88a30c99a7a9449aa84174',
Expand Down Expand Up @@ -154,10 +157,17 @@ export const ACCOUNT_INTERNAL_WXZ_137: AddressBookAccount = {

export const NATIVE_TRANSFER_INTENT: TransferNative = {
type: Intents.TRANSFER_NATIVE,
from: TREASURY_WALLET_X,
to: ACCOUNT_Q_137,
amount: toHex(ONE_ETH),
native: TREASURY_WALLET_X.uid // Assuming NativeId is a Caip10 string // TODO: This doesn't make sense, what is this field supposed to be?
native: 'eip155:1/slip44:60' as Caip19 // Caip19 for ETH
}

export const ERC20_TRANSFER_TX_REQUEST: TransactionRequest = {
from: TREASURY_WALLET_X.address as Address,
to: '0x031d8C0cA142921c459bCB28104c0FF37928F9eD' as Address,
chainId: ACCOUNT_Q_137.chainId,
data: '0xa9059cbb000000000000000000000000031d8c0ca142921c459bcb28104c0ff37928f9ed000000000000000000000000000000000000000000005ab7f55035d1e7b4fe6d',
nonce: 192,
type: '2'
}

export const NATIVE_TRANSFER_TX_REQUEST: TransactionRequest = {
Expand Down Expand Up @@ -260,10 +270,19 @@ export const mockEntityData: RegoData = {
}
}

export const generateIntent = (request: TransactionRequest): Intent => {
const intent = safeDecode(request)
if (!intent.success) {
throw new Error('Failed to decode intent')
}
return intent.intent
}

// stub out the actual tx request & signature
// This is what would be the initial input from the external service
export const generateInboundRequest = async (): Promise<AuthZRequestPayload> => {
const txRequest = NATIVE_TRANSFER_TX_REQUEST
export const generateInboundRequest = async (generate: boolean = false): Promise<AuthZRequestPayload> => {
const txRequest = ERC20_TRANSFER_TX_REQUEST
const intent = generate ? generateIntent(txRequest) : NATIVE_TRANSFER_INTENT

const signatureMatt = await privateKeyToAccount(UNSAFE_PRIVATE_KEY_MATT).signMessage({
message: JSON.stringify(txRequest)
Expand All @@ -281,9 +300,9 @@ export const generateInboundRequest = async (): Promise<AuthZRequestPayload> =>
},
request: {
activityType: BlockchainActions.SIGN_TRANSACTION,
intent: NATIVE_TRANSFER_INTENT,
intent,
transactionRequest: txRequest,
resourceId: NATIVE_TRANSFER_INTENT.from.uid
resourceId: NATIVE_TRANSFER_TX_REQUEST.from
},
approvalSignatures: [approvalSigAAUser, approvalSigBBUser]
}
Expand Down
7 changes: 0 additions & 7 deletions apps/authz/src/shared/types/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,6 @@ export enum ResourceActions {

export type Actions = BlockchainActions | PolicyManagementActions | ResourceActions

export enum Intents {
TRANSFER_NATIVE = 'transferNative',
TRANSFER_TOKEN = 'transferToken',
TRANSFER_NFT = 'transferNft',
CALL_CONTRACT = 'callContract'
}

export enum AccountType {
EOA = 'eoa',
AA = '4337'
Expand Down
2 changes: 1 addition & 1 deletion apps/authz/src/shared/types/http.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Intent } from 'packages/transaction-request-intent/src/lib/intent.types'
import { Actions } from './enums'
import { Intent } from './intents'

// Types ripped from viem; combining a few though because they don't have chainId on txRequest
export type Hex = `0x${string}`
Expand Down
50 changes: 0 additions & 50 deletions apps/authz/src/shared/types/intents.ts

This file was deleted.

2 changes: 1 addition & 1 deletion apps/authz/src/shared/types/rego.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Intent } from 'packages/transaction-request-intent/src/lib/intent.types'
import { Actions } from './enums'
import { TransactionRequest } from './http'
import { Intent } from './intents'

export type RegoInput = {
activityType: Actions
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { decodeIntent } from '../../decoders'
import { decode, decodeIntent, safeDecode } from '../../decoders'
import { AssetTypeEnum, Intents } from '../../domain'
import { Erc20Methods } from '../../methodId'
import { IntentRequest } from '../../types'
import { ERC20_TRANSFER_TX_REQUEST } from './mocks'

jest.mock('viem', () => ({
decodeAbiParameters: jest
.fn()
.mockReturnValueOnce(['0x031d8C0cA142921c459bCB28104c0FF37928F9eD', BigInt('428406414311469998210669')])
.mockReturnValue(['0x031d8C0cA142921c459bCB28104c0FF37928F9eD', BigInt('428406414311469998210669')])
}))

describe('decodeIntent', () => {
Expand All @@ -31,3 +32,28 @@ describe('decodeIntent', () => {
})
})
})

describe('decode', () => {
it('decodes ERC20 transaction request correctly', () => {
const dec = decode(ERC20_TRANSFER_TX_REQUEST)
expect(dec).toEqual({
type: Intents.TRANSFER_ERC20,
amount: '428406414311469998210669',
token: 'eip155:137:0x031d8c0ca142921c459bcb28104c0ff37928f9ed'
})
})
})

describe('safeDecode', () => {
it('decodes ERC20 transaction request correctly', () => {
const dec = safeDecode(ERC20_TRANSFER_TX_REQUEST)
expect(dec).toEqual({
success: true,
intent: {
type: 'transferErc20',
amount: '428406414311469998210669',
token: 'eip155:137:0x031d8c0ca142921c459bcb28104c0ff37928f9ed'
}
})
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { AssetTypeEnum, Intents } from '../../domain'
import { validateIntent } from '../../intent'
import { Erc20Methods } from '../../methodId'
import { ERC20_TRANSFER_TX_REQUEST } from './mocks'

describe('validateIntent', () => {
it('validate good data', () => {
expect(validateIntent(ERC20_TRANSFER_TX_REQUEST)).toEqual({
methodId: Erc20Methods.TRANSFER,
assetType: AssetTypeEnum.ERC20,
type: Intents.TRANSFER_ERC20,
validatedFields: {
data: '0xa9059cbb000000000000000000000000031d8c0ca142921c459bcb28104c0ff37928f9ed000000000000000000000000000000000000000000005ab7f55035d1e7b4fe6d',
to: '0x031d8C0cA142921c459bCB28104c0FF37928F9eD',
chainId: '137'
}
})
})
})
107 changes: 95 additions & 12 deletions packages/transaction-request-intent/src/lib/__test__/unit/mocks.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
import {
AddressBookAccount,
RegoData,
RolePermission,
User,
UserGroup,
Wallet,
WalletGroup
} from '@app/authz/shared/types/entities.types'
import { AccountType, BlockchainActions, ResourceActions, UserRoles } from '@app/authz/shared/types/enums'
import { TransactionRequest } from '@app/authz/shared/types/http'
import { Address } from 'viem'
import { Caip19 } from '../../caip'
import { Intents } from '../../domain'
import { TransferNative } from '../../intent.types'
import { TransactionRequest } from '../../transaction.type'

export const ONE_ETH = BigInt('1000000000000000000')

Expand All @@ -24,11 +14,104 @@ export const USDC_TOKEN = {
decimals: 6
}

type UUID = string

// ENTITIES: user, user group, wallet, wallet group, and address book.
export type User = {
uid: string // Pubkey
role: UserRoles
}

export type UserGroup = {
uid: string
name: string
users: string[] // userIds
}

export type Wallet = {
uid: string
address: string
accountType: AccountType
chainId?: string
assignees?: string[] // userIds
}

export type WalletGroup = {
uid: string
name: string
wallets: string[] // walletIds
}

export type AddressBookAccount = {
uid: string
address: string
chainId: string
classification: string
}

export type AddressBook = {
orgId: UUID
name: string
accounts: AddressBookAccount[]
}

export type RolePermission = {
permit: boolean
admin_quorum_threshold?: number
}

export type RegoData = {
entities: {
users: Record<string, User>
user_groups: Record<string, UserGroup>
wallets: Record<string, Wallet>
wallet_groups: Record<string, WalletGroup>
address_book: Record<string, AddressBookAccount>
}
permissions: Record<string, Record<string, RolePermission>>
}

export enum AccountType {
EOA = 'eoa',
AA = '4337'
}

export enum BlockchainActions {
SIGN_TRANSACTION = 'signTransaction',
SIGN_RAW = 'signRaw',
SIGN_MESSAGE = 'signMessage',
SIGN_TYPED_DATA = 'signTypedData'
}

export enum UserRoles {
ROOT = 'root',
ADMIN = 'admin',
MEMBER = 'member',
MANAGER = 'manager'
}

export enum ResourceActions {
CREATE_USER = 'user:create',
EDIT_USER = 'user:edit',
DELETE_USER = 'user:delete',
CHANGE_USER_ROLE = 'user:change-role',
CREATE_WALLET = 'wallet:create',
EDIT_WALLET = 'wallet:edit',
ASSIGN_WALLET = 'wallet:assign',
UNASSIGN_WALLET = 'wallet:unassign',
CREATE_USER_GROUP = 'user-group:create',
EDIT_USER_GROUP = 'user-group:edit',
DELETE_USER_GROUP = 'user-group:delete',
CREATE_WALLET_GROUP = 'wallet-group:create',
EDIT_WALLET_GROUP = 'wallet-group:edit',
DELETE_WALLET_GROUP = 'wallet-group:delete'
}

/**
* User & User Groups
*/

export const ROOT_USER: User = {
export const ROOT_USER = {
uid: 'u:root_user',
role: UserRoles.ROOT
}
Expand Down
Loading

0 comments on commit 86425a6

Please sign in to comment.