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

Feat: public transaction notes #4693

Merged
merged 20 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { useSigner } from '@/hooks/wallets/useWallet'
import { trackTxEvents } from './tracking'
import TxNoteForm from './TxNoteForm'
import TxNote from '@/components/transactions/TxDetails/TxNote'
import { encodeTxNote } from '@/utils/transactions'

export type SubmitCallback = (txId: string, isExecuted?: boolean) => void

Expand Down Expand Up @@ -129,13 +130,7 @@ export const SignOrExecuteForm = ({

const onNoteSubmit = useCallback(
(note: string) => {
const originalOrigin = props.origin ? JSON.parse(props.origin) : { url: location.origin }
setCustomOrigin(
JSON.stringify({
...originalOrigin,
name: JSON.stringify({ note }),
}),
)
setCustomOrigin(encodeTxNote(note, props.origin))
},
[setCustomOrigin, props.origin],
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useCallback, useState } from 'react'
import { InputAdornment, Stack, TextField, Typography } from '@mui/material'
import InfoIcon from '@/public/images/notifications/info.svg'

const MAX_NOTE_LENGTH = 120
const MAX_NOTE_LENGTH = 100

const TxNoteForm = ({ onSubmit }: { onSubmit: (note: string) => void }) => {
const [note, setNote] = useState('')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ exports[`SignOrExecute should display a confirmation screen 1`] = `
aria-invalid="false"
class="MuiInputBase-input MuiOutlinedInput-input MuiInputBase-inputAdornedEnd css-f423sj-MuiInputBase-input-MuiOutlinedInput-input"
id=":r1:"
maxlength="120"
maxlength="100"
name="note"
type="text"
/>
Expand All @@ -142,7 +142,7 @@ exports[`SignOrExecute should display a confirmation screen 1`] = `
>
0
/
120
100
</span>
</div>
<fieldset
Expand Down Expand Up @@ -543,7 +543,7 @@ exports[`SignOrExecute should display an error screen 1`] = `
aria-invalid="false"
class="MuiInputBase-input MuiOutlinedInput-input MuiInputBase-inputAdornedEnd css-f423sj-MuiInputBase-input-MuiOutlinedInput-input"
id=":r6:"
maxlength="120"
maxlength="100"
name="note"
type="text"
/>
Expand All @@ -555,7 +555,7 @@ exports[`SignOrExecute should display an error screen 1`] = `
>
0
/
120
100
</span>
</div>
<fieldset
Expand Down
19 changes: 18 additions & 1 deletion apps/web/src/utils/__tests__/transactions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type {
} from '@safe-global/safe-gateway-typescript-sdk'
import { TransactionInfoType } from '@safe-global/safe-gateway-typescript-sdk'
import { isMultiSendTxInfo } from '../transaction-guards'
import { getQueuedTransactionCount, getTxOrigin } from '../transactions'
import { getQueuedTransactionCount, getTxOrigin, encodeTxNote } from '../transactions'

jest.mock('@/services/tx/tx-sender/sdk')

Expand Down Expand Up @@ -196,4 +196,21 @@ describe('transactions', () => {
).toBe(false)
})
})

describe('transactions utils', () => {
it('should encode tx note with an existing origin', () => {
// Test goes here
const note = 'test note'
const origin = JSON.stringify({ url: 'http://some.dapp' })
const result = encodeTxNote(note, origin)
expect(result).toEqual(JSON.stringify({ url: 'http://some.dapp', name: JSON.stringify({ note }) }))
})

it('should encode tx note with an empty origin', () => {
// Test goes here
const note = 'test note'
const result = encodeTxNote(note)
expect(result).toEqual(JSON.stringify({ url: 'http://localhost', name: JSON.stringify({ note }) }))
})
})
})
16 changes: 16 additions & 0 deletions apps/web/src/utils/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,3 +240,19 @@ export const getSafeTransaction = async (safeTxHash: string, chainId: string, sa
return undefined
}
}

export const encodeTxNote = (note: string, origin?: string): string => {
let originalOrigin = { url: '' }
if (origin) {
try {
originalOrigin = JSON.parse(origin)
} catch {}
katspaugh marked this conversation as resolved.
Show resolved Hide resolved
}
if (!originalOrigin.url) {
originalOrigin.url = location.origin
}
return JSON.stringify({
...originalOrigin,
name: JSON.stringify({ note }),
})
}
Loading