Skip to content

Commit

Permalink
wip: categories to be removed, changed interface to take into account…
Browse files Browse the repository at this point in the history
… transaction and contract registries
  • Loading branch information
Ptroger committed Jan 12, 2024
1 parent 86425a6 commit 4045ac5
Show file tree
Hide file tree
Showing 10 changed files with 230 additions and 130 deletions.
44 changes: 2 additions & 42 deletions apps/authz/src/shared/module/persistence/mock_data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@ import {
WalletGroup
} from '@app/authz/shared/types/entities.types'
import { AccountType, BlockchainActions, ResourceActions, UserRoles } from '@app/authz/shared/types/enums'
import { AuthZRequestPayload, TransactionRequest } from '@app/authz/shared/types/http'
import { TransactionRequest } from '@app/authz/shared/types/http'
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 { TransferNative } from 'packages/transaction-request-intent/src/lib/intent.types'
import { Address, toHex } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'

export const ONE_ETH = BigInt('1000000000000000000')

Expand Down Expand Up @@ -269,41 +267,3 @@ 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 (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)
})
const approvalSigAAUser = await privateKeyToAccount(UNSAFE_PRIVATE_KEY_AAUSER).signMessage({
message: JSON.stringify(txRequest)
})
const approvalSigBBUser = await privateKeyToAccount(UNSAFE_PRIVATE_KEY_BBUSER).signMessage({
message: JSON.stringify(txRequest)
})

return {
authn: {
signature: signatureMatt
},
request: {
activityType: BlockchainActions.SIGN_TRANSACTION,
intent,
transactionRequest: txRequest,
resourceId: NATIVE_TRANSFER_TX_REQUEST.from
},
approvalSignatures: [approvalSigAAUser, approvalSigBBUser]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ describe('decodeIntent', () => {

describe('decode', () => {
it('decodes ERC20 transaction request correctly', () => {
const dec = decode(ERC20_TRANSFER_TX_REQUEST)
const dec = decode({
txRequest: ERC20_TRANSFER_TX_REQUEST
})
expect(dec).toEqual({
type: Intents.TRANSFER_ERC20,
amount: '428406414311469998210669',
Expand All @@ -46,7 +48,9 @@ describe('decode', () => {

describe('safeDecode', () => {
it('decodes ERC20 transaction request correctly', () => {
const dec = safeDecode(ERC20_TRANSFER_TX_REQUEST)
const dec = safeDecode({
txRequest: ERC20_TRANSFER_TX_REQUEST
})
expect(dec).toEqual({
success: true,
intent: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,52 @@
import { AssetTypeEnum, Intents } from '../../domain'
import { validateIntent } from '../../intent'
import { Erc20Methods } from '../../methodId'
import { AssetTypeEnum, Intents, NULL_METHOD_ID } from '../../domain'
import { getAssetType, validateIntent } from '../../intent'
import { Erc1155Methods, Erc20Methods, Erc721Methods } from '../../methodId'
import { ERC20_TRANSFER_TX_REQUEST } from './mocks'

describe('getAssetType', () => {
it('identify ERC20', () => {
const res = getAssetType(Erc20Methods.TRANSFER)
expect(res).toEqual(AssetTypeEnum.ERC20)
})
it('identify ERC721 safeTransferFrom', () => {
const res = getAssetType(Erc721Methods.SAFE_TRANSFER_FROM)
expect(res).toEqual(AssetTypeEnum.ERC721)
})
it('identify ERC721 safeTransferFrom overflow', () => {
const res = getAssetType(Erc721Methods.SAFE_TRANSFER_FROM_WITH_BYTES)
expect(res).toEqual(AssetTypeEnum.ERC721)
})
it('identify ERC1155 safeBatchTransferFrom', () => {
const res = getAssetType(Erc1155Methods.SAFE_BATCH_TRANSFER_FROM)
expect(res).toEqual(AssetTypeEnum.ERC1155)
})
it('identify ERC1155 safeTransferFrom', () => {
const res = getAssetType(Erc1155Methods.SAFE_TRANSFER_FROM)
expect(res).toEqual(AssetTypeEnum.ERC1155)
})
it('identify ambiguous erc721 transferFrom', () => {
const res = getAssetType(Erc721Methods.TRANSFER_FROM)
expect(res).toEqual(AssetTypeEnum.AMBIGUOUS_TRANSFER)
})
it('identify ambiguous erc20 transferFrom', () => {
const res = getAssetType(Erc20Methods.TRANSFER_FROM)
expect(res).toEqual(AssetTypeEnum.AMBIGUOUS_TRANSFER)
})
it('identify native transfer', () => {
const value = '0x111'
const res = getAssetType(NULL_METHOD_ID, value)
expect(res).toEqual(AssetTypeEnum.NATIVE)
})
it('defaults to unknown', () => {
const res = getAssetType('invalid asset type')
expect(res).toEqual(AssetTypeEnum.UNKNOWN)
})
it('unknown when NULL_METHOD_ID is passed without value', () => {
const res = getAssetType(NULL_METHOD_ID)
expect(res).toEqual(AssetTypeEnum.UNKNOWN)
})
})

describe('validateIntent', () => {
it('validate good data', () => {
expect(validateIntent(ERC20_TRANSFER_TX_REQUEST)).toEqual({
Expand Down
112 changes: 50 additions & 62 deletions packages/transaction-request-intent/src/lib/abis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,65 +37,53 @@ export const Erc721TransferAbi = {
'0xb88d4fde': Erc721SafeTransferFromBytesAbiParameters
}

// export const Erc1155TransferSignatures: {[key: string]: AbiParameter} = {
// "0xa22cb465": {
// "constant": false,
// "inputs": [
// {
// "name": "from",
// "type": "address"
// },
// {
// "name": "to",
// "type": "address"
// },
// {
// "name": "id",
// "type": "uint256"
// },
// {
// "name": "amount",
// "type": "uint256"
// },
// {
// "name": "data",
// "type": "bytes"
// }
// ],
// "name": "safeTransferFrom",
// "outputs": [],
// "payable": false,
// "stateMutability": "nonpayable",
// "type": "function"
// },
// "0xf242432a": {
// "constant": false,
// "inputs": [
// {
// "name": "from",
// "type": "address"
// },
// {
// "name": "to",
// "type": "address"
// },
// {
// "name": "ids",
// "type": "uint256[]"
// },
// {
// "name": "amounts",
// "type": "uint256[]"
// },
// {
// "name": "data",
// "type": "bytes"
// }
// ],
// "name": "safeBatchTransferFrom",
// "outputs": [],
// "payable": false,
// "stateMutability": "nonpayable",
// "type": "function"
// }
// };
export const Erc1155SafeTransferFromAbiParameters: AbiParameter[] = [
{
name: 'from',
type: 'address'
},
{
name: 'to',
type: 'address'
},
{
name: 'id',
type: 'uint256'
},
{
name: 'amount',
type: 'uint256'
},
{
name: 'data',
type: 'bytes'
}
]

export const Erc1155SafeBatchTransferFromAbiParameters: AbiParameter[] = [
{
name: 'from',
type: 'address'
},
{
name: 'to',
type: 'address'
},
{
name: 'ids',
type: 'uint256[]'
},
{
name: 'amounts',
type: 'uint256[]'
},
{
name: 'data',
type: 'bytes'
}
]

export const Erc1155TransferAbi = {
'0xa22cb465': Erc1155SafeTransferFromAbiParameters,
'0xf242432a': Erc1155SafeBatchTransferFromAbiParameters
}
26 changes: 21 additions & 5 deletions packages/transaction-request-intent/src/lib/decoders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { validateIntent } from './intent'
import { Intent, TransferErc20, TransferErc721 } from './intent.types'
import { extractErc20Amount, extractErc721AssetId } from './param-extractors'
import { TransactionRequest } from './transaction.type'
import { Decode, IntentRequest } from './types'
import { Decode, IntentRequest, TransactionRegistry } from './types'

export const decodeErc721 = ({
data,
Expand Down Expand Up @@ -90,13 +90,29 @@ export const decodeIntent = (request: IntentRequest): Intent => {
}
}

export const decode = (txRequest: TransactionRequest): Intent => {
const request = validateIntent(txRequest)
export const decode = ({
txRequest,
// contractRegistry,
transactionRegistry
}: {
txRequest: TransactionRequest
// contractRegistry?: ContractRegistry
transactionRegistry?: TransactionRegistry
}): Intent => {
const request = validateIntent(txRequest, transactionRegistry)
return decodeIntent(request)
}

export const safeDecode = (txRequest: TransactionRequest): Decode => {
const request = validateIntent(txRequest)
export const safeDecode = ({
txRequest,
// contractRegistry,
transactionRegistry
}: {
txRequest: TransactionRequest
// contractRegistry?: ContractRegistry
transactionRegistry?: TransactionRegistry
}): Decode => {
const request = validateIntent(txRequest, transactionRegistry)
try {
const intent = decodeIntent(request)
return {
Expand Down
14 changes: 10 additions & 4 deletions packages/transaction-request-intent/src/lib/domain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,26 @@ export enum PolicyManagementActions {
export type Actions = BlockchainActions | PolicyManagementActions

export enum Category {
ASSET_OPERATIONS = 'AssetOperations',
TRANSFER = 'transfer',
CONTRACT_LIFECYCLE = 'ContractLifecycle',
TRANSACTION_MANAGEMENT = 'TransactionManagement',
GENERIC_CONTRACT_CALLS = 'GenericContractCalls',
AUTHORIZATION_SIGNATURES = 'AuthorizationSignatures',
UNKNOWN = 'Unknown'
AUTHORIZATION_SIGNATURES = 'AuthorizationSignatures'
}

export enum TransactionStatus {
PENDING = 'pending',
FAILED = 'failed'
}

export enum Intents {
TRANSFER_NATIVE = 'transferNative',
TRANSFER_ERC20 = 'transferErc20',
TRANSFER_ERC721 = 'transferErc721',
TRANSFER_ERC1155 = 'transferErc1155',
CALL_CONTRACT = 'callContract'
CALL_CONTRACT = 'callContract',
RETRY_TRANSACTION = 'retryTransaction',
CANCEL_TRANSACTION = 'cancelTransaction'
}

export const NULL_METHOD_ID = '0x00000000'
Expand Down
Loading

0 comments on commit 4045ac5

Please sign in to comment.