Commit b27483f9 authored by Will Cory's avatar Will Cory

add encodeRawKey

encode key in reads too
parent 69f9e28d
......@@ -193,6 +193,21 @@ const attestation = parseAttestationBytes(
)
```
### attestation keys
Attestation keys are limited to 32 bytes. To support keys longer than 32 bytes, you can use the `encodeRawKey` function
```typescript
const key = await encodeRawKey(
about,
key,
'i.am.a.key.much.longer.than.32.bytes.long'
)
await writeAttestation(preparedTx)
```
encodeRawKey will keep the key as is if it is shorter than 32 bytes and otherwise run it through kekkak256
### prepareWriteAttestation
[Prepares](https://wagmi.sh/core/actions/prepareWriteContract) an attestation to be written.
......@@ -225,3 +240,7 @@ const bigNumberAttestation = stringifyAttestationBytes(
const preparedTx = await prepareWriteAttestation(about, key, 'hello world')
await writeAttestation(preparedTx)
```
## Tutorial
For a tutorial on using the attestation station in general, see out tutorial as well as other Optimism related tutorials in our [optimism-tutorial](https://github.com/ethereum-optimism/optimism-tutorial/tree/main/ecosystem/attestation-station#key-values) repo
// constants
export { ATTESTATION_STATION_ADDRESS } from './constants/attestationStationAddress'
// lib
export { encodeRawKey } from './lib/encodeRawKey'
export {
readAttestation,
readAttestationAddress,
......@@ -23,5 +24,5 @@ export {
} from './lib/parseAttestationBytes'
// types
export type { AttestationReadParams } from './types/AttestationReadParams'
export type { WagmiBytes } from './types/WagmiBytes'
export type { DataTypeOption } from './types/DataTypeOption'
export type { WagmiBytes } from './types/WagmiBytes'
import { describe, expect, it } from 'vitest'
import { encodeRawKey } from './encodeRawKey'
describe(encodeRawKey.name, () => {
it('should return just the raw key if it is less than 32 bytes', () => {
const rawKey = 'I am 32'
const encodedKey = encodeRawKey(rawKey)
expect(encodedKey).toMatchInlineSnapshot(
'"0x4920616d20333200000000000000000000000000000000000000000000000000"'
)
})
it('should return the keccak256 hash of the raw key if it is more than 32 bytes', () => {
const rawKey = 'I am way more than 32 bytes long I should be hashed'
const encodedKey = encodeRawKey(rawKey)
expect(encodedKey).toMatchInlineSnapshot(
'"0xc9d5d767710cc45f74c3a9a0c53dc44391a7951604c7ea3bd9116ccff406daff"'
)
})
})
import { ethers } from 'ethers'
export const encodeRawKey = (rawKey: string) => {
if (rawKey.length < 32) {
return ethers.utils.formatBytes32String(rawKey)
}
const hash = ethers.utils.keccak256(ethers.utils.toUtf8Bytes(rawKey))
return hash.slice(0, 64) + 'ff'
}
......@@ -13,7 +13,15 @@ export const prepareWriteAttestation = async (
chainId = 10,
contractAddress: Address = ATTESTATION_STATION_ADDRESS
) => {
const formattedKey = formatBytes32String(key) as WagmiBytes
let formattedKey: 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({
address: contractAddress,
abi,
......
......@@ -18,7 +18,15 @@ export const prepareWriteAttestations = async (
contractAddress: Address = ATTESTATION_STATION_ADDRESS
) => {
const formattedAttestations = attestations.map((attestation) => {
const formattedKey = formatBytes32String(attestation.key) as WagmiBytes
let formattedKey: WagmiBytes
try {
formattedKey = formatBytes32String(attestation.key) as WagmiBytes
} catch (e) {
console.error(e)
throw new Error(
`key is longer than 32 bytes: ${attestation.key}. Try using a shorter key or using 'encodeRawKey' to encode the key into 32 bytes first`
)
}
const formattedValue = stringifyAttestationBytes(
attestation.value
) as WagmiBytes
......
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