1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { Address } from '@wagmi/core'
import { BigNumber } from 'ethers'
import {
hexlify,
isAddress,
isHexString,
toUtf8Bytes,
} from 'ethers/lib/utils.js'
import { WagmiBytes } from '../types/WagmiBytes'
/**
* 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
): WagmiBytes => {
bytes = bytes === '0x' ? '0x0' : bytes
if (BigNumber.isBigNumber(bytes)) {
return bytes.toHexString() as WagmiBytes
}
if (typeof bytes === 'number') {
return BigNumber.from(bytes).toHexString() as WagmiBytes
}
if (typeof bytes === 'boolean') {
return bytes ? '0x1' : '0x0'
}
if (isAddress(bytes)) {
return bytes
}
if (isHexString(bytes)) {
return bytes as WagmiBytes
}
if (typeof bytes === 'string') {
return hexlify(toUtf8Bytes(bytes)) as WagmiBytes
}
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)
}