Skip to content

Commit

Permalink
integrated shared types/util libs
Browse files Browse the repository at this point in the history
  • Loading branch information
Ptroger committed Jan 25, 2024
1 parent 806420c commit 9b2c645
Show file tree
Hide file tree
Showing 21 changed files with 263 additions and 267 deletions.
5 changes: 2 additions & 3 deletions apps/authz/src/app/evaluation-request.dto.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { AccessList, Action, Address, Alg, FiatCurrency, Hex } from '@narval/authz-shared'
import { AccessList, Action, Address, Alg, Caip10Id, FiatCurrency, Hex } from '@narval/authz-shared'
import { ApiExtraModels, ApiProperty, getSchemaPath } from '@nestjs/swagger'
import { Transform, Type } from 'class-transformer'
import { IsDefined, IsEnum, IsEthereumAddress, IsOptional, IsString, ValidateNested } from 'class-validator'
import { Caip10 } from 'packages/transaction-request-intent/src/lib/caip'

export class RequestSignatureDto {
@ApiProperty()
Expand Down Expand Up @@ -139,7 +138,7 @@ export class SignMessageRequestDataDto extends BaseRequestDataDto {

export class HistoricalTransferDto {
amount: string
from: Caip10
from: Caip10Id
to: string
chainId: number
token: string
Expand Down
18 changes: 13 additions & 5 deletions apps/authz/src/shared/module/persistence/mock_data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,16 @@ import {
Wallet,
WalletGroup
} from '@app/authz/shared/types/entities.types'
import { Action, Alg, AuthorizationRequest, Request, TransactionRequest, hashRequest } from '@narval/authz-shared'
import { Caip10, Caip19 } from 'packages/transaction-request-intent/src/lib/caip'
import {
Action,
Alg,
AuthorizationRequest,
Caip10Id,
Caip19Id,
Request,
TransactionRequest,
hashRequest
} from '@narval/authz-shared'
import { Intents } from 'packages/transaction-request-intent/src/lib/domain'
import { TransferNative } from 'packages/transaction-request-intent/src/lib/intent.types'
import { Address, toHex } from 'viem'
Expand Down Expand Up @@ -181,11 +189,11 @@ export const ACCOUNT_INTERNAL_WXZ_137: AddressBookAccount = {
}

export const NATIVE_TRANSFER_INTENT: TransferNative = {
from: TREASURY_WALLET_X.uid as Caip10,
to: ACCOUNT_Q_137.uid as Caip10,
from: TREASURY_WALLET_X.uid as Caip10Id,
to: ACCOUNT_Q_137.uid as Caip10Id,
type: Intents.TRANSFER_NATIVE,
amount: toHex(ONE_ETH),
token: 'eip155:1/slip44:60' as Caip19 // Caip19 for ETH
token: 'eip155:1/slip44:60' as Caip19Id // Caip19 for ETH
}

export const ERC20_TRANSFER_TX_REQUEST: TransactionRequest = {
Expand Down
46 changes: 42 additions & 4 deletions packages/authz-shared/src/lib/util/caip.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ export type Caip10 = {
namespace: Namespace
}

type Caip10Input = {
address: Address
chainId: number
namespace?: Namespace
}

/**
* @see https://github.com/ChainAgnostic/CAIPs/blob/main/CAIPs/caip-19.md
*/
Expand All @@ -49,6 +55,19 @@ export type Caip19 = Caip10 & {
assetId?: string
}

export enum Slip44SupportedAddresses {
ETH = '60',
MATIC = '966'
}

type Caip19Input = Caip10Input & {
assetType: AssetType
assetId?: string
chainId: number
namespace?: Namespace
address: Address
}

type ParseResult<Value> =
| {
success: false
Expand Down Expand Up @@ -106,13 +125,16 @@ export const parseCaip10 = (caip10Id: Caip10Id): ParseResult<Caip10> => {
}

/**
* Converts a Caip10 object to a Caip10Id string.
* Converts a Caip10Input object to a Caip10Id string.
* 'namespace' is optional and defaults to 'eip155'.
*
* @param caip10 The Caip10 object to convert.
* @returns The Caip10Id string representation of the Caip10 object.
*/
export const toCaip10 = (caip10: Caip10): Caip10Id => `${caip10.namespace}:${caip10.chainId}/${caip10.address}`

export const toCaip10 = ({ namespace = Namespace.EIP155, chainId, address }: Caip10Input): Caip10Id =>
`${namespace}:${chainId}/${address}`
export const toCaip10Lower = ({ namespace = Namespace.EIP155, chainId, address }: Caip10Input): Caip10Id =>
`${namespace}:${chainId}/${address.toLowerCase()}`
/**
* Parses a CAIP-19 ID and returns the parsed result.
*
Expand Down Expand Up @@ -170,13 +192,29 @@ export const parseCaip19 = (caip19Id: Caip19Id): ParseResult<Caip19> => {
}
}

export const isCaip10Id = (value: string): value is Caip10Id => {
const match = value.match(/^([^:]+):(\d+)\/(.+)$/)
return !!match
}

export const isCaip19Id = (value: string): value is Caip19Id => {
const match = value.match(/^([^:]+):(\d+)\/([^:]+):([^/]+)(?:\/([^/]+))?$/)
return !!match
}

/**
* Converts a Caip19 object to a Caip19Id string representation.
*
* @param caip19 - The Caip19 object to convert.
* @returns The Caip19Id string representation.
*/
export const toCaip19 = ({ namespace, chainId, assetType, address, assetId }: Caip19): Caip19Id =>
export const toCaip19 = ({
namespace = Namespace.EIP155,
chainId,
assetType,
address,
assetId
}: Caip19Input): Caip19Id =>
assetId
? `${namespace}:${chainId}/${assetType}:${address}/${assetId}`
: `${namespace}:${chainId}/${assetType}:${address}`
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ describe('decode', () => {
})
expect(decoded).toEqual({
type: Intents.TRANSFER_NATIVE,
to: 'eip155:137:0x031d8c0ca142921c459bcb28104c0ff37928f9ed',
from: 'eip155:137:0xed123cf8e3ba51c6c15da1eac74b2b5deea31448',
to: 'eip155:137/0x031d8c0ca142921c459bcb28104c0ff37928f9ed',
from: 'eip155:137/0xed123cf8e3ba51c6c15da1eac74b2b5deea31448',
amount: '16676',
token: 'eip155:137/slip44/966'
token: 'eip155:137/slip44:966'
})
})
it('decodes approve token allowance', () => {
Expand All @@ -66,10 +66,10 @@ describe('decode', () => {
})
expect(decoded).toEqual({
type: Intents.APPROVE_TOKEN_ALLOWANCE,
from: 'eip155:137:0xed123cf8e3ba51c6c15da1eac74b2b5deea31448',
token: 'eip155:137:0x031d8c0ca142921c459bcb28104c0ff37928f9ed',
from: 'eip155:137/0xed123cf8e3ba51c6c15da1eac74b2b5deea31448',
token: 'eip155:137/0x031d8c0ca142921c459bcb28104c0ff37928f9ed',
amount: '11541971132511365478906515907109950360107522067033065608472376982619868367719',
spender: 'eip155:137:0x1111111254eeb25477b68fb85ed929f73a960582'
spender: 'eip155:137/0x1111111254eeb25477b68fb85ed929f73a960582'
})
})
it('defaults to contract call intent', () => {
Expand All @@ -85,8 +85,8 @@ describe('decode', () => {
})
expect(decoded).toEqual({
type: Intents.CALL_CONTRACT,
from: 'eip155:137:0xed123cf8e3ba51c6c15da1eac74b2b5deea31448',
contract: 'eip155:137:0x031d8c0ca142921c459bcb28104c0ff37928f9ed',
from: 'eip155:137/0xed123cf8e3ba51c6c15da1eac74b2b5deea31448',
contract: 'eip155:137/0x031d8c0ca142921c459bcb28104c0ff37928f9ed',
hexSignature: '0xf2d12b12'
})
})
Expand Down
55 changes: 28 additions & 27 deletions packages/transaction-request-intent/src/lib/__test__/unit/mocks.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { TransactionRequest } from '@narval/authz-shared'
import { Caip10Id, Caip19Id, TransactionRequest } from '@narval/authz-shared'
import { Address } from 'viem'
import { Caip10, Caip19 } from '../../caip'
import { DecodeInput, InputType, Intents, TransactionInput } from '../../domain'
import { TransferErc1155, TransferErc20, TransferErc721, TransferNative } from '../../intent.types'

Expand Down Expand Up @@ -211,7 +210,7 @@ export const TREASURY_WALLET_GROUP: WalletGroup = {
// Address Book

export const SHY_ACCOUNT_137: AddressBookAccount = {
uid: 'eip155:137:0xddcf208f219a6e6af072f2cfdc615b2c1805f98e',
uid: 'eip155:137/0xddcf208f219a6e6af072f2cfdc615b2c1805f98e',
address: '0xddcf208f219a6e6af072f2cfdc615b2c1805f98e',
chainId: 137,
classification: 'wallet'
Expand All @@ -225,25 +224,25 @@ export const SHY_ACCOUNT_1: AddressBookAccount = {
}

export const ACCOUNT_Q_137: AddressBookAccount = {
uid: 'eip155:137:0x08a08d0504d4f3363a5b7fda1f5fff1c7bca8ad4',
uid: 'eip155:137/0x08a08d0504d4f3363a5b7fda1f5fff1c7bca8ad4',
address: '0x08a08d0504d4f3363a5b7fda1f5fff1c7bca8ad4',
chainId: 137,
classification: 'wallet'
}

export const ACCOUNT_INTERNAL_WXZ_137: AddressBookAccount = {
uid: 'eip155:137:0xa45e21e9370ba031c5e1f47dedca74a7ce2ed7a3',
uid: 'eip155:137/0xa45e21e9370ba031c5e1f47dedca74a7ce2ed7a3',
address: '0xa45e21e9370ba031c5e1f47dedca74a7ce2ed7a3',
chainId: 137,
classification: 'internal'
}

export const NATIVE_TRANSFER_INTENT: TransferNative = {
type: Intents.TRANSFER_NATIVE,
to: 'eip155:137/eoa:0x08a08d0504d4f3363a5b7fda1f5fff1c7bca8ad4' as Caip10,
from: 'eip155:137/eoa:0x90d03a8971a2faa19a9d7ffdcbca28fe826a289b' as Caip10,
to: 'eip155:137/eoa:0x08a08d0504d4f3363a5b7fda1f5fff1c7bca8ad4' as Caip10Id,
from: 'eip155:137/eoa:0x90d03a8971a2faa19a9d7ffdcbca28fe826a289b' as Caip10Id,
amount: '0x8000',
token: 'eip155:1/slip44/60' as Caip19 // Caip19 for ETH
token: 'eip155:1/slip44/60' as Caip19Id // Caip19Id for ETH
}

const ERC20_TRANSFER_TX_REQUEST: TransactionRequest = {
Expand All @@ -257,9 +256,9 @@ const ERC20_TRANSFER_TX_REQUEST: TransactionRequest = {

const ERC20_TRANSFER_INTENT: TransferErc20 = {
type: Intents.TRANSFER_ERC20,
to: `eip155:137:0x031d8c0ca142921c459bcb28104c0ff37928f9ed` as Caip10,
from: `eip155:137:${ERC20_TRANSFER_TX_REQUEST.from.toLowerCase()}` as Caip10,
contract: `eip155:137:${ERC20_TRANSFER_TX_REQUEST.to?.toLowerCase()}` as Caip10,
to: `eip155:137/0x031d8c0ca142921c459bcb28104c0ff37928f9ed` as Caip10Id,
from: `eip155:137/${ERC20_TRANSFER_TX_REQUEST.from.toLowerCase()}` as Caip10Id,
contract: `eip155:137/${ERC20_TRANSFER_TX_REQUEST.to?.toLowerCase()}` as Caip10Id,
amount: '428406414311469998210669'
}

Expand All @@ -274,10 +273,10 @@ const ERC721_SAFE_TRANSFER_FROM_TX_REQUEST: TransactionRequest = {

const ERC721_SAFE_TRANSFER_FROM_INTENT: TransferErc721 = {
type: Intents.TRANSFER_ERC721,
to: `eip155:137:0xb253f6156e64b12ba0dec3974062dbbaee139f0c` as Caip10,
from: `eip155:137:${ERC721_SAFE_TRANSFER_FROM_TX_REQUEST.from.toLowerCase()}` as Caip10,
contract: `eip155:137:${ERC721_SAFE_TRANSFER_FROM_TX_REQUEST.to?.toLowerCase()}` as Caip10,
nftId: `eip155:137/erc721:${ERC721_SAFE_TRANSFER_FROM_TX_REQUEST.to}/41173` as Caip19
to: `eip155:137/0xb253f6156e64b12ba0dec3974062dbbaee139f0c` as Caip10Id,
from: `eip155:137/${ERC721_SAFE_TRANSFER_FROM_TX_REQUEST.from.toLowerCase()}` as Caip10Id,
contract: `eip155:137/${ERC721_SAFE_TRANSFER_FROM_TX_REQUEST.to?.toLowerCase()}` as Caip10Id,
nftId: `eip155:137/erc721:${ERC721_SAFE_TRANSFER_FROM_TX_REQUEST.to}/41173` as Caip19Id
}

export const mockErc721SafeTransferFrom = {
Expand All @@ -304,9 +303,9 @@ export const mockTransferFrom = {
}
} as TransactionInput,
intent: {
to: `eip155:137:0x59895c2cdaa07cc3ac20ef0918d2597a277b276c` as Caip10,
from: `eip155:137:${TREASURY_WALLET_X.address}` as Caip10,
contract: `eip155:137:${ACCOUNT_Q_137.address}` as Caip10,
to: `eip155:137/0x59895c2cdaa07cc3ac20ef0918d2597a277b276c` as Caip10Id,
from: `eip155:137/${TREASURY_WALLET_X.address}` as Caip10Id,
contract: `eip155:137/${ACCOUNT_Q_137.address}` as Caip10Id,
amount: '5532'
}
}
Expand All @@ -322,10 +321,12 @@ const ERC1155_SAFE_TRANSFER_FROM_TX_REQUEST: TransactionRequest = {

const ERC1155_SAFE_TRANSFER_FROM_INTENT: TransferErc1155 = {
type: Intents.TRANSFER_ERC1155,
to: `eip155:137:0x00ca04c45da318d5b7e7b14d5381ca59f09c73f0` as Caip10,
from: `eip155:137:${ERC1155_SAFE_TRANSFER_FROM_TX_REQUEST.from.toLowerCase()}` as Caip10,
contract: `eip155:137:${ERC1155_SAFE_TRANSFER_FROM_TX_REQUEST.to?.toLowerCase()}` as Caip10,
transfers: [{ tokenId: `eip155:137/erc1155:${ERC1155_SAFE_TRANSFER_FROM_TX_REQUEST.to}/175` as Caip19, amount: '1' }]
to: `eip155:137/0x00ca04c45da318d5b7e7b14d5381ca59f09c73f0` as Caip10Id,
from: `eip155:137/${ERC1155_SAFE_TRANSFER_FROM_TX_REQUEST.from.toLowerCase()}` as Caip10Id,
contract: `eip155:137/${ERC1155_SAFE_TRANSFER_FROM_TX_REQUEST.to?.toLowerCase()}` as Caip10Id,
transfers: [
{ tokenId: `eip155:137/erc1155:${ERC1155_SAFE_TRANSFER_FROM_TX_REQUEST.to}/175` as Caip19Id, amount: '1' }
]
}
export const mockErc1155SafeTransferFrom = {
input: {
Expand All @@ -345,16 +346,16 @@ const ERC1155_BATCH_SAFE_TRANSFER_FROM_REQUEST = {

const ERC1155_BATCH_SAFE_TRANSFER_FROM_INTENT = {
type: Intents.TRANSFER_ERC1155,
from: `eip155:137:${ERC1155_BATCH_SAFE_TRANSFER_FROM_REQUEST.from.toLowerCase()}` as Caip10,
to: `eip155:137:0x383370726a5bd619e0d2af8ef37a58013b823a8c` as Caip10,
contract: `eip155:137:${ERC1155_BATCH_SAFE_TRANSFER_FROM_REQUEST.to?.toLowerCase()}` as Caip10,
from: `eip155:137/${ERC1155_BATCH_SAFE_TRANSFER_FROM_REQUEST.from.toLowerCase()}` as Caip10Id,
to: `eip155:137/0x383370726a5bd619e0d2af8ef37a58013b823a8c` as Caip10Id,
contract: `eip155:137/${ERC1155_BATCH_SAFE_TRANSFER_FROM_REQUEST.to?.toLowerCase()}` as Caip10Id,
transfers: [
{
tokenId: `eip155:137/erc1155:${ERC1155_BATCH_SAFE_TRANSFER_FROM_REQUEST.to}/2972` as Caip19,
tokenId: `eip155:137/erc1155:${ERC1155_BATCH_SAFE_TRANSFER_FROM_REQUEST.to}/2972` as Caip19Id,
amount: '1'
},
{
tokenId: `eip155:137/erc1155:${ERC1155_BATCH_SAFE_TRANSFER_FROM_REQUEST.to}/162` as Caip19,
tokenId: `eip155:137/erc1155:${ERC1155_BATCH_SAFE_TRANSFER_FROM_REQUEST.to}/162` as Caip19Id,
amount: '1'
}
]
Expand Down
49 changes: 0 additions & 49 deletions packages/transaction-request-intent/src/lib/caip.ts

This file was deleted.

Loading

0 comments on commit 9b2c645

Please sign in to comment.