Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Humanizer ascii module #985

Draft
wants to merge 3 commits into
base: humanizer-remove-async-ops
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/libs/humanizer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
import OneInchModule from './modules/1Inch'
import { aaveHumanizer } from './modules/Aave'
import AcrossModule from './modules/Across'
import asciiModule from './modules/AsciiModule'
import curveModule from './modules/Curve'
import fallbackHumanizer from './modules/FallbackHumanizer'
import gasTankModule from './modules/GasTankModule'
Expand Down Expand Up @@ -48,6 +49,7 @@ export const humanizerCallModules: HumanizerCallModule[] = [
WALLETModule,
privilegeHumanizer,
sushiSwapModule,
asciiModule,
fallbackHumanizer,
postProcessing
]
Expand Down
3 changes: 2 additions & 1 deletion src/libs/humanizer/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Call } from '../accountOp/types'
// @TODO remove property humanizerMeta
export type HumanizerVisualization = (
| {
type: 'address' | 'label' | 'action' | 'danger' | 'deadline' | 'chain' | 'message'
type: 'address' | 'danger' | 'deadline' | 'chain' | 'message'
address?: string
content?: string
value?: bigint
Expand All @@ -22,6 +22,7 @@ export type HumanizerVisualization = (
value: bigint
chainId?: bigint
}
| {type:'text' | 'label' | 'action', content:string }
) & { isHidden?: boolean; id: number; content?: string }
export interface IrCall extends Call {
fullVisualization?: HumanizerVisualization[]
Expand Down
49 changes: 49 additions & 0 deletions src/libs/humanizer/modules/AsciiModule/asciiModule.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { describe, expect } from '@jest/globals'

import humanizerInfo from '../../../../consts/humanizer/humanizerInfo.json'
import { ErrorRef } from '../../../../controllers/eventEmitter/eventEmitter'
import { AccountOp } from '../../../accountOp/accountOp'
import { HumanizerMeta } from '../../interfaces'
import { compareHumanizerVisualizations, compareVisualizations } from '../../testHelpers'
import { getAction, getAddressVisualization, getLabel, getText } from '../../utils'
import { asciiModule } from './asciiModule'

const accountOp: AccountOp = {
accountAddr: '0xB674F3fd5F43464dB0448a57529eAF37F04cceA5',
networkId: 'ethereum',
// this may not be defined, in case the user has not picked a key yet
signingKeyAddr: null,
signingKeyType: null,
// this may not be set in case we haven't set it yet
nonce: null,
calls: [],
gasLimit: null,
signature: null,
gasFeePayment: null,
// This is used when we have an account recovery to finalize before executing the AccountOp,
// And we set this to the recovery finalization AccountOp; could be used in other scenarios too in the future,
// for example account migration (from v1 QuickAcc to v2)
accountOpToExecuteBefore: null
// This is fed into the humanizer to help visualize the accountOp
// This can contain info like the value of specific share tokens at the time of signing,
// or any other data that needs to otherwise be retrieved in an async manner and/or needs to be
// "remembered" at the time of signing in order to visualize history properly
// humanizerMeta: {}
}
const transactions = [
{ to: '0x77777777789A8BBEE6C64381e5E89E501fb0e4c8', value: 0n, data: '0x68656c6c6f' },
{ to: '0x77777777789A8BBEE6C64381e5E89E501fb0e4c8', value: 1n, data: '0x68656c6c6f' },
{ to: '0x77777777789A8BBEE6C64381e5E89E501fb0e4c8', value: 0n, data: '0x536F6D65206578616D706C65206F6E636861696E2074657874206D657373616765' },
]
describe('asciiHumanizer', () => {
test('basic functionality', async () => {
const humanizationPrefix = [getAction('Send this message'),getLabel('to') ,getAddressVisualization('0x77777777789A8BBEE6C64381e5E89E501fb0e4c8')]
accountOp.calls = transactions

let irCalls = asciiModule(accountOp, accountOp.calls, humanizerInfo as HumanizerMeta)

expect(irCalls[1].fullVisualization).toBeFalsy()
compareVisualizations(irCalls[0].fullVisualization!, [...humanizationPrefix,getText('hello')])
compareVisualizations(irCalls[2].fullVisualization!, [...humanizationPrefix,getText('Some example onchain text message')])
})
})
31 changes: 31 additions & 0 deletions src/libs/humanizer/modules/AsciiModule/asciiModule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* eslint-disable no-await-in-loop */
import { toUtf8String } from 'ethers'

import { AccountOp } from '../../../accountOp/accountOp'
import { HumanizerCallModule, IrCall } from '../../interfaces'
import {
checkIfUnknownAction,
getAction,
getAddressVisualization,
getLabel,
getText
} from '../../utils'

export const asciiModule: HumanizerCallModule = (
accountOp: AccountOp,
currentIrCalls: IrCall[],
) => {
const newCalls = currentIrCalls.map((call) => {
if (call.fullVisualization && !checkIfUnknownAction(call?.fullVisualization)) return call
if(call.value) return call
try {
return {
...call,
fullVisualization: [getAction('Send this message'), getLabel('to'), getAddressVisualization(call.to), getText(toUtf8String(call.data))]
}
} catch (_) {
return call
}
})
return newCalls
}
3 changes: 3 additions & 0 deletions src/libs/humanizer/modules/AsciiModule/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { asciiModule } from './asciiModule'

export default asciiModule
5 changes: 5 additions & 0 deletions src/libs/humanizer/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ export function getChain(chainId: bigint): HumanizerVisualization {
return { type: 'chain', id: randomId(), chainId }
}

export function getText(text:string): HumanizerVisualization {
return { type:'text', content: text, id: randomId()}
}

export function getOnBehalfOf(onBehalfOf: string, sender: string): HumanizerVisualization[] {
return onBehalfOf.toLowerCase() !== sender.toLowerCase()
? [getLabel('on behalf of'), getAddressVisualization(onBehalfOf)]
Expand Down Expand Up @@ -145,6 +149,7 @@ export function getKnownName(
return humanizerMeta?.knownAddresses?.[address.toLowerCase()]?.name
}


export const EMPTY_HUMANIZER_META = { abis: { NO_ABI: {} }, knownAddresses: {} }

export const uintToAddress = (uint: bigint): string =>
Expand Down