Commit 480fe38f authored by mergify[bot]'s avatar mergify[bot] Committed by GitHub

Merge pull request #5043 from ethereum-optimism/willc/rename-createKey

feat(atst): rename createValue createKey
parents c591b584 28499887
---
'@eth-optimism/atst': minor
---
Deprecate parseAttestationBytes and createRawKey in favor for createKey, createValue
...@@ -44,7 +44,9 @@ TODO put a gif here of using it ...@@ -44,7 +44,9 @@ TODO put a gif here of using it
For react hooks we recomend using the [wagmi cli](https://wagmi.sh/cli/getting-started) with the [etherscan plugin](https://wagmi.sh/cli/plugins/etherscan) and [react plugin](https://wagmi.sh/cli/plugins/react) to automatically generate react hooks around the attestation station. For react hooks we recomend using the [wagmi cli](https://wagmi.sh/cli/getting-started) with the [etherscan plugin](https://wagmi.sh/cli/plugins/etherscan) and [react plugin](https://wagmi.sh/cli/plugins/react) to automatically generate react hooks around the attestation station.
Use `parseAttestationBytes` and `stringifyAttestationBytes` to parse and stringify attestations before passing them into wagmi hooks. Use `createKey` and `createValue` to convert your raw keys and values into bytes that can be used in the attestation station contract calls
Use `parseString`, `parseBool`, `parseAddress` and `parseNumber` to convert values returned by attestation station to their correct data type.
For convenience we also export the hooks here. For convenience we also export the hooks here.
......
...@@ -207,10 +207,10 @@ const attestation = parseAttestationBytes( ...@@ -207,10 +207,10 @@ const attestation = parseAttestationBytes(
### attestation keys ### attestation keys
Attestation keys are limited to 32 bytes. To support keys longer than 32 bytes, you can use the `encodeRawKey` function Attestation keys are limited to 32 bytes. To support keys longer than 32 bytes, you can use the `createKey` function
```typescript ```typescript
const key = await encodeRawKey( const key = await createKey(
about, about,
key, key,
'i.am.a.key.much.longer.than.32.bytes.long' 'i.am.a.key.much.longer.than.32.bytes.long'
...@@ -218,7 +218,7 @@ const key = await encodeRawKey( ...@@ -218,7 +218,7 @@ const key = await encodeRawKey(
await writeAttestation(preparedTx) await writeAttestation(preparedTx)
``` ```
encodeRawKey will keep the key as is if it is shorter than 32 bytes and otherwise run it through kekkak256 createKey will keep the key as is if it is shorter than 32 bytes and otherwise run it through kekkak256
### prepareWriteAttestation ### prepareWriteAttestation
......
// constants // constants
export { ATTESTATION_STATION_ADDRESS } from './constants/attestationStationAddress' export { ATTESTATION_STATION_ADDRESS } from './constants/attestationStationAddress'
// lib // lib
export { encodeRawKey } from './lib/encodeRawKey' export { encodeRawKey, createKey } from './lib/createKey'
export { createValue, stringifyAttestationBytes } from './lib/createValue'
export { export {
readAttestation, readAttestation,
readAttestationAddress, readAttestationAddress,
...@@ -15,7 +16,6 @@ export { prepareWriteAttestation } from './lib/prepareWriteAttestation' ...@@ -15,7 +16,6 @@ export { prepareWriteAttestation } from './lib/prepareWriteAttestation'
export { prepareWriteAttestations } from './lib/prepareWriteAttestations' export { prepareWriteAttestations } from './lib/prepareWriteAttestations'
export { writeAttestation } from './lib/writeAttestation' export { writeAttestation } from './lib/writeAttestation'
export { abi } from './lib/abi' export { abi } from './lib/abi'
export { stringifyAttestationBytes } from './lib/stringifyAttestationBytes'
export { export {
parseAttestationBytes, parseAttestationBytes,
parseAddress, parseAddress,
......
import { describe, expect, it } from 'vitest' import { describe, expect, it } from 'vitest'
import { encodeRawKey } from './encodeRawKey' import { encodeRawKey } from './createKey'
describe(encodeRawKey.name, () => { describe(encodeRawKey.name, () => {
it('should return just the raw key if it is less than 32 bytes', () => { it('should return just the raw key if it is less than 32 bytes', () => {
......
...@@ -2,10 +2,24 @@ import { ethers } from 'ethers' ...@@ -2,10 +2,24 @@ import { ethers } from 'ethers'
import { WagmiBytes } from '../types/WagmiBytes' import { WagmiBytes } from '../types/WagmiBytes'
export const encodeRawKey = (rawKey: string): WagmiBytes => { /**
* Creates an attesation key from a raw string
* Converts to bytes32 if key is less than 32 bytes
* Hashes key if key is greater than 32 bytes
*/
export const createKey = (rawKey: string): WagmiBytes => {
if (rawKey.length < 32) { if (rawKey.length < 32) {
return ethers.utils.formatBytes32String(rawKey) as WagmiBytes return ethers.utils.formatBytes32String(rawKey) as WagmiBytes
} }
const hash = ethers.utils.keccak256(ethers.utils.toUtf8Bytes(rawKey)) const hash = ethers.utils.keccak256(ethers.utils.toUtf8Bytes(rawKey))
return (hash.slice(0, 64) + 'ff') as WagmiBytes return (hash.slice(0, 64) + 'ff') as WagmiBytes
} }
/**
* @deprecated use createKey instead
* Will be removed in v1.0.0
*/
export const encodeRawKey: typeof createKey = (rawKey) => {
console.warn('encodeRawKey is deprecated, use createKey instead')
return createKey(rawKey)
}
...@@ -9,7 +9,16 @@ import { ...@@ -9,7 +9,16 @@ import {
import { WagmiBytes } from '../types/WagmiBytes' import { WagmiBytes } from '../types/WagmiBytes'
export const stringifyAttestationBytes = ( /**
* Turns a value into bytes to make an attestation
*
* @example
* createValue('hello world') // '0x68656c6c6f20776f726c64'
* createValue(123) // '0x7b'
* createValue(true) // '0x1'
* createValue(BigNumber.from(10)) // '0xa'
*/
export const createValue = (
bytes: WagmiBytes | string | Address | number | boolean | BigNumber bytes: WagmiBytes | string | Address | number | boolean | BigNumber
): WagmiBytes => { ): WagmiBytes => {
bytes = bytes === '0x' ? '0x0' : bytes bytes = bytes === '0x' ? '0x0' : bytes
...@@ -33,3 +42,14 @@ export const stringifyAttestationBytes = ( ...@@ -33,3 +42,14 @@ export const stringifyAttestationBytes = (
} }
throw new Error(`unrecognized bytes type ${bytes satisfies never}`) throw new Error(`unrecognized bytes type ${bytes satisfies never}`)
} }
/**
* @deprecated use createValue instead
* Will be removed in v1.0.0
*/
export const stringifyAttestationBytes: typeof createValue = (bytes) => {
console.warn(
'stringifyAttestationBytes is deprecated, use createValue instead'
)
return createValue(bytes)
}
...@@ -4,7 +4,7 @@ import { Address } from 'wagmi' ...@@ -4,7 +4,7 @@ import { Address } from 'wagmi'
import { ATTESTATION_STATION_ADDRESS } from '../constants/attestationStationAddress' import { ATTESTATION_STATION_ADDRESS } from '../constants/attestationStationAddress'
import { abi } from '../lib/abi' import { abi } from '../lib/abi'
import { AttestationCreatedEvent } from '../types/AttestationCreatedEvent' import { AttestationCreatedEvent } from '../types/AttestationCreatedEvent'
import { encodeRawKey } from './encodeRawKey' import { encodeRawKey } from './createKey'
export const getEvents = async ({ export const getEvents = async ({
creator = null, creator = null,
......
...@@ -41,6 +41,9 @@ export const parseAddress = (rawAttestation: WagmiBytes): Address => { ...@@ -41,6 +41,9 @@ export const parseAddress = (rawAttestation: WagmiBytes): Address => {
} }
/** /**
* @deprecated use parseString, parseBool, parseNumber, or parseAddress instead
* Will be removed in v1.0.0
* @internal
* Parses a raw attestation * Parses a raw attestation
*/ */
export const parseAttestationBytes = <TDataType extends DataTypeOption>( export const parseAttestationBytes = <TDataType extends DataTypeOption>(
......
...@@ -4,7 +4,7 @@ import { formatBytes32String } from 'ethers/lib/utils.js' ...@@ -4,7 +4,7 @@ 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 { stringifyAttestationBytes } from './stringifyAttestationBytes' import { createValue } from './createValue'
export const prepareWriteAttestation = async ( export const prepareWriteAttestation = async (
about: Address, about: Address,
...@@ -27,6 +27,6 @@ export const prepareWriteAttestation = async ( ...@@ -27,6 +27,6 @@ export const prepareWriteAttestation = async (
abi, abi,
functionName: 'attest', functionName: 'attest',
chainId, chainId,
args: [about, formattedKey, stringifyAttestationBytes(value) as WagmiBytes], args: [about, formattedKey, createValue(value) as WagmiBytes],
}) })
} }
...@@ -4,7 +4,7 @@ import { formatBytes32String } from 'ethers/lib/utils.js' ...@@ -4,7 +4,7 @@ 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 { stringifyAttestationBytes } from './stringifyAttestationBytes' import { createValue } from './createValue'
type Attestation = { type Attestation = {
about: Address about: Address
...@@ -27,7 +27,7 @@ export const prepareWriteAttestations = async ( ...@@ -27,7 +27,7 @@ export const prepareWriteAttestations = async (
`key is longer than 32 bytes: ${attestation.key}. Try using a shorter key or using 'encodeRawKey' to encode the key into 32 bytes first` `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( const formattedValue = createValue(
attestation.value attestation.value
) as WagmiBytes ) as WagmiBytes
return { return {
......
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