Commit dcd13eec authored by Will Cory's avatar Will Cory

Use createKey internally in functions

parent c35b6e39
---
'@eth-optimism/atst': minor
---
Update readAttestations and prepareWriteAttestation to handle keys longer than 32 bytes
...@@ -76,6 +76,35 @@ describe(getEvents.name, () => { ...@@ -76,6 +76,35 @@ describe(getEvents.name, () => {
"transactionHash": "0x61f59bd4dfe54272d9369effe3ae57a0ef2584161fcf2bbd55f5596002e759bd", "transactionHash": "0x61f59bd4dfe54272d9369effe3ae57a0ef2584161fcf2bbd55f5596002e759bd",
"transactionIndex": 1, "transactionIndex": 1,
}, },
{
"address": "0xEE36eaaD94d1Cc1d0eccaDb55C38bFfB6Be06C77",
"args": [
"0xBCf86Fd70a0183433763ab0c14E7a760194f3a9F",
"0x00000000000000000000000000000000000060A7",
"0x616e696d616c6661726d2e7363686f6f6c2e617474656e646564000000000000",
"0x01",
],
"blockHash": "0x4870baaac6d7195952dc25e5dc0109ea324f819f8152d2889c7b4ad64040a9bf",
"blockNumber": 6278428,
"data": "0x000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000010100000000000000000000000000000000000000000000000000000000000000",
"decode": [Function],
"event": "AttestationCreated",
"eventSignature": "AttestationCreated(address,address,bytes32,bytes)",
"getBlock": [Function],
"getTransaction": [Function],
"getTransactionReceipt": [Function],
"logIndex": 0,
"removeListener": [Function],
"removed": false,
"topics": [
"0x28710dfecab43d1e29e02aa56b2e1e610c0bae19135c9cf7a83a1adb6df96d85",
"0x000000000000000000000000bcf86fd70a0183433763ab0c14e7a760194f3a9f",
"0x00000000000000000000000000000000000000000000000000000000000060a7",
"0x616e696d616c6661726d2e7363686f6f6c2e617474656e646564000000000000",
],
"transactionHash": "0x4e836b74c51a370375efa374297524d9b0f6eacdd699c30556680ae7dc9a14ea",
"transactionIndex": 1,
},
] ]
`) `)
}) })
......
...@@ -43,7 +43,7 @@ describe(prepareWriteAttestation.name, () => { ...@@ -43,7 +43,7 @@ describe(prepareWriteAttestation.name, () => {
expect(result.address).toMatchInlineSnapshot( expect(result.address).toMatchInlineSnapshot(
'"0xEE36eaaD94d1Cc1d0eccaDb55C38bFfB6Be06C77"' '"0xEE36eaaD94d1Cc1d0eccaDb55C38bFfB6Be06C77"'
) )
expect(result.chainId).toMatchInlineSnapshot('10') expect(result.chainId).toMatchInlineSnapshot('undefined')
expect(result.functionName).toMatchInlineSnapshot('"attest"') expect(result.functionName).toMatchInlineSnapshot('"attest"')
expect(result.mode).toMatchInlineSnapshot('"prepared"') expect(result.mode).toMatchInlineSnapshot('"prepared"')
expect(result.request.gasLimit).toMatchInlineSnapshot(` expect(result.request.gasLimit).toMatchInlineSnapshot(`
...@@ -54,18 +54,16 @@ describe(prepareWriteAttestation.name, () => { ...@@ -54,18 +54,16 @@ describe(prepareWriteAttestation.name, () => {
`) `)
}) })
it('should throw an error if key is longer than 32 bytes', async () => { it('should work for key longer than 32 bytes', async () => {
const dataType = 'string' const dataType = 'string'
await expect( expect(
readAttestation( await readAttestation(
creator, creator,
about, about,
'this is a key that is way longer than 32 bytes so this key should throw an error matching the inline snapshot', 'this is a key that is way longer than 32 bytes so this key should throw an error matching the inline snapshot',
dataType dataType
) )
).rejects.toThrowErrorMatchingInlineSnapshot( ).toMatchInlineSnapshot('""')
'"Key is longer than the max length of 32 for attestation keys"'
)
}) })
}) })
import { Address, prepareWriteContract } from '@wagmi/core' import { Address, prepareWriteContract } from '@wagmi/core'
import { formatBytes32String } from 'ethers/lib/utils.js'
import { ATTESTATION_STATION_ADDRESS } from '../constants/attestationStationAddress' import { ATTESTATION_STATION_ADDRESS } from '../constants/attestationStationAddress'
import { WagmiBytes } from '../types/WagmiBytes' import { WagmiBytes } from '../types/WagmiBytes'
import { abi } from './abi' import { abi } from './abi'
import { createKey } from './createKey'
import { createValue } from './createValue' import { createValue } from './createValue'
export const prepareWriteAttestation = async ( export const prepareWriteAttestation = async (
...@@ -13,15 +13,7 @@ export const prepareWriteAttestation = async ( ...@@ -13,15 +13,7 @@ export const prepareWriteAttestation = async (
chainId: number | undefined = undefined, chainId: number | undefined = undefined,
contractAddress: Address = ATTESTATION_STATION_ADDRESS contractAddress: Address = ATTESTATION_STATION_ADDRESS
) => { ) => {
let formattedKey: WagmiBytes const formattedKey = createKey(key) as WagmiBytes
try {
formattedKey = formatBytes32String(key) as WagmiBytes
} catch (e) {
console.error(e)
throw new Error(
`key is longer than 32 bytes: ${key}. Try using a shorter key or using 'encodeRawKey' to encode the key into 32 bytes first`
)
}
return prepareWriteContract({ return prepareWriteContract({
address: contractAddress, address: contractAddress,
abi, abi,
......
...@@ -49,7 +49,7 @@ describe(prepareWriteAttestations.name, () => { ...@@ -49,7 +49,7 @@ describe(prepareWriteAttestations.name, () => {
expect(result.address).toMatchInlineSnapshot( expect(result.address).toMatchInlineSnapshot(
'"0xEE36eaaD94d1Cc1d0eccaDb55C38bFfB6Be06C77"' '"0xEE36eaaD94d1Cc1d0eccaDb55C38bFfB6Be06C77"'
) )
expect(result.chainId).toMatchInlineSnapshot('10') expect(result.chainId).toMatchInlineSnapshot('undefined')
expect(result.functionName).toMatchInlineSnapshot('"attest"') expect(result.functionName).toMatchInlineSnapshot('"attest"')
expect(result.mode).toMatchInlineSnapshot('"prepared"') expect(result.mode).toMatchInlineSnapshot('"prepared"')
expect(result.request.gasLimit).toMatchInlineSnapshot(` expect(result.request.gasLimit).toMatchInlineSnapshot(`
...@@ -60,18 +60,16 @@ describe(prepareWriteAttestations.name, () => { ...@@ -60,18 +60,16 @@ describe(prepareWriteAttestations.name, () => {
`) `)
}) })
it('should throw an error if key is longer than 32 bytes', async () => { it('should work if key is longer than 32 bytes', async () => {
const dataType = 'string' const dataType = 'string'
await expect( expect(
readAttestation( await readAttestation(
creator, creator,
about, about,
'this is a key that is way longer than 32 bytes so this key should throw an error matching the inline snapshot', 'this is a key that is way longer than 32 bytes so this key should throw an error matching the inline snapshot',
dataType dataType
) )
).rejects.toThrowErrorMatchingInlineSnapshot( ).toMatchInlineSnapshot('""')
'"Key is longer than the max length of 32 for attestation keys"'
)
}) })
}) })
...@@ -26,16 +26,14 @@ describe(readAttestation.name, () => { ...@@ -26,16 +26,14 @@ describe(readAttestation.name, () => {
) )
}) })
it('should throw an error if key is longer than 32 bytes', async () => { it('should work if key is longer than 32 bytes', async () => {
await expect( expect(
readAttestation( await readAttestation(
creator, creator,
about, about,
'this is a key that is way longer than 32 bytes so this key should throw an error matching the inline snapshot', 'this is a key that is way longer than 32 bytes so this key should throw an error matching the inline snapshot',
dataType dataType
) )
).rejects.toThrowErrorMatchingInlineSnapshot( ).toMatchInlineSnapshot('""')
'"Key is longer than the max length of 32 for attestation keys"'
)
}) })
}) })
import { readContracts } from '@wagmi/core' import { readContracts } from '@wagmi/core'
import { formatBytes32String } from 'ethers/lib/utils.js'
import { ATTESTATION_STATION_ADDRESS } from '../constants/attestationStationAddress' import { ATTESTATION_STATION_ADDRESS } from '../constants/attestationStationAddress'
import type { AttestationReadParams } from '../types/AttestationReadParams' import type { AttestationReadParams } from '../types/AttestationReadParams'
import { DEFAULT_DATA_TYPE } from '../types/DataTypeOption' import { DEFAULT_DATA_TYPE } from '../types/DataTypeOption'
import type { WagmiBytes } from '../types/WagmiBytes' import type { WagmiBytes } from '../types/WagmiBytes'
import { abi } from './abi' import { abi } from './abi'
import { createKey } from './createKey'
import { parseAttestationBytes } from './parseAttestationBytes' import { parseAttestationBytes } from './parseAttestationBytes'
/** /**
...@@ -39,16 +39,11 @@ export const readAttestations = async ( ...@@ -39,16 +39,11 @@ export const readAttestations = async (
key, key,
contractAddress = ATTESTATION_STATION_ADDRESS, contractAddress = ATTESTATION_STATION_ADDRESS,
} = attestation } = attestation
if (key.length > 32) {
throw new Error(
'Key is longer than the max length of 32 for attestation keys'
)
}
return { return {
address: contractAddress, address: contractAddress,
abi, abi,
functionName: 'attestations', functionName: 'attestations',
args: [creator, about, formatBytes32String(key) as WagmiBytes], args: [creator, about, createKey(key) as WagmiBytes],
} as const } as const
}) })
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment