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

Merge pull request #5034 from ethereum-optimism/03-01-fix_atst_return_correct_type_for_encodeRawKey

fix(atst): return correct typescript types for bytes
parents 43b47c22 d49910af
---
'@eth-optimism/atst': minor
---
Fix string type that should be `0x${string}`
......@@ -26,6 +26,7 @@ cli
})
.example(
() =>
// note: private key is just the first testing address when running anvil
`atst read --key "optimist.base-uri" --about 0x2335022c740d17c2837f9C884Bfe4fFdbf0A95D5 --creator 0x60c5C9c98bcBd0b0F2fD89B24c16e533BaA8CdA3`
)
.action(async (options: ReadOptions) => {
......@@ -72,6 +73,8 @@ cli
`atst write --key "optimist.base-uri" --about 0x2335022c740d17c2837f9C884Bfe4fFdbf0A95D5 --value "my attestation" --private-key 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80 --rpc-url http://localhost:8545`
)
.action(async (options: WriteOptions) => {
const spinner = logger.spinner()
spinner.start('Writing attestation...')
const { write } = await import('./commands/write')
// TODO use the native api to do this instead of parsing the raw args
......@@ -86,9 +89,16 @@ cli
: options.contract
await write({ ...options, about, privateKey, contract })
.then((res) => {
spinner.succeed('Attestation written!')
logger.info(`Attestation hash: ${res}`)
})
.catch((e) => {
logger.error(e)
spinner.fail('Attestation failed!')
})
})
cli.help()
cli.version(packageJson.version)
void (async () => {
......
......@@ -28,3 +28,5 @@ export type { AttestationCreatedEvent } from './types/AttestationCreatedEvent'
export type { AttestationReadParams } from './types/AttestationReadParams'
export type { DataTypeOption } from './types/DataTypeOption'
export type { WagmiBytes } from './types/WagmiBytes'
// react
export * from './react'
import { ethers } from 'ethers'
export const encodeRawKey = (rawKey: string) => {
import { WagmiBytes } from '../types/WagmiBytes'
export const encodeRawKey = (rawKey: string): WagmiBytes => {
if (rawKey.length < 32) {
return ethers.utils.formatBytes32String(rawKey)
return ethers.utils.formatBytes32String(rawKey) as WagmiBytes
}
const hash = ethers.utils.keccak256(ethers.utils.toUtf8Bytes(rawKey))
return hash.slice(0, 64) + 'ff'
return (hash.slice(0, 64) + 'ff') as WagmiBytes
}
......@@ -41,8 +41,11 @@ describe(parseAttestationBytes.name, () => {
})
it('should work for raw bytes', () => {
const bytes = '0x420'
expect(parseAttestationBytes(bytes, 'bytes')).toBe(bytes)
expect(parseAttestationBytes('0x420', 'bytes')).toMatchInlineSnapshot(
'"0x420"'
)
expect(parseAttestationBytes('0x', 'string')).toMatchInlineSnapshot('""')
expect(parseAttestationBytes('0x0', 'string')).toMatchInlineSnapshot('""')
})
it('should return raw bytes for invalid type', () => {
......@@ -57,12 +60,16 @@ describe('parseFoo', () => {
const str = 'Hello World'
const bytes = BigNumber.from(toUtf8Bytes(str)).toHexString() as WagmiBytes
expect(parseString(bytes)).toBe(str)
expect(parseString('0x')).toMatchInlineSnapshot('""')
expect(parseString('0x0')).toMatchInlineSnapshot('""')
expect(parseString('0x0')).toMatchInlineSnapshot('""')
})
it('works for numbers', () => {
const num = 123
const bytes = BigNumber.from(num).toHexString() as WagmiBytes
expect(parseNumber(bytes)).toEqual(BigNumber.from(num))
expect(parseNumber('0x')).toEqual(BigNumber.from(0))
})
it('works for addresses', () => {
......@@ -74,5 +81,8 @@ describe('parseFoo', () => {
it('works for booleans', () => {
const bytes = BigNumber.from(1).toHexString() as WagmiBytes
expect(parseBool(bytes)).toBe(true)
expect(parseBool('0x')).toBe(false)
expect(parseBool('0x0')).toBe(false)
expect(parseBool('0x00000')).toBe(false)
})
})
......@@ -10,7 +10,7 @@ import { ParseBytesReturn } from '../types/ParseBytesReturn'
* Parses a string attestion
*/
export const parseString = (rawAttestation: WagmiBytes): string => {
rawAttestation = rawAttestation === '0x' ? '0x0' : rawAttestation
rawAttestation = rawAttestation === '0x0' ? '0x' : rawAttestation
return rawAttestation ? toUtf8String(rawAttestation) : ''
}
......
......@@ -39,4 +39,3 @@ describe(readAttestation.name, () => {
)
})
})
import { Address } from '@wagmi/core'
import { BigNumber } from 'ethers'
import { isAddress, isHexString, toUtf8Bytes } from 'ethers/lib/utils.js'
import {
hexlify,
isAddress,
isHexString,
toUtf8Bytes,
} from 'ethers/lib/utils.js'
import { WagmiBytes } from '../types/WagmiBytes'
export const stringifyAttestationBytes = (
bytes: WagmiBytes | string | Address | number | boolean | BigNumber
) => {
): WagmiBytes => {
bytes = bytes === '0x' ? '0x0' : bytes
if (BigNumber.isBigNumber(bytes)) {
return bytes.toHexString()
return bytes.toHexString() as WagmiBytes
}
if (typeof bytes === 'number') {
return BigNumber.from(bytes).toHexString()
return BigNumber.from(bytes).toHexString() as WagmiBytes
}
if (typeof bytes === 'boolean') {
return bytes ? '0x1' : '0x0'
......@@ -21,10 +26,10 @@ export const stringifyAttestationBytes = (
return bytes
}
if (isHexString(bytes)) {
return bytes
return bytes as WagmiBytes
}
if (typeof bytes === 'string') {
return toUtf8Bytes(bytes)
return hexlify(toUtf8Bytes(bytes)) as WagmiBytes
}
throw new Error(`unrecognized bytes type ${bytes satisfies never}`)
}
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