Skip to content

Commit

Permalink
switch to [ox](https://oxlib.sh)
Browse files Browse the repository at this point in the history
  • Loading branch information
m1guelpf committed Nov 8, 2024
1 parent 3cdc1fe commit cf3a820
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 98 deletions.
2 changes: 1 addition & 1 deletion cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@
"keccak",
"merkle",
"NextJS",
"ox",
"QRCode",
"tsup",
"TTFB",
"viem",
"webp",
"worldid",
"zustand"
Expand Down
4 changes: 1 addition & 3 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,12 @@
"dependencies": {
"browser-or-node": "3.0.0-pre.0",
"buffer": "^6.0.3",
"viem": "^2.17.0",
"ox": "^0.1.0",
"zustand": "^4.5"
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^6.13.1",
"@typescript-eslint/parser": "^6.13.1",
"prettier": "^2.7.1",
"prettier-plugin-sort-imports-desc": "^1.0.0",
"tsup": "^8.1.0",
"typescript": "^5.3.2"
}
Expand Down
12 changes: 6 additions & 6 deletions packages/core/src/lib/hashing.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Buffer } from 'buffer/index.js'
import type { IDKitConfig } from '@/types/config'
import type { AbiEncodedValue } from '@/types/config'
import { encodePacked, isBytes, isHex, keccak256 } from 'viem'
import { AbiParameters, Bytes, Hex, Hash } from 'ox'

export interface HashFunctionOutput {
hash: bigint
Expand All @@ -15,8 +15,8 @@ export interface HashFunctionOutput {
* @param input Any string, hex-like string, bytes represented as a hex string.
* @returns
*/
export function hashToField(input: Uint8Array | string): HashFunctionOutput {
if (isBytes(input) || isHex(input)) return hashEncodedBytes(input)
export function hashToField(input: Bytes.Bytes | string): HashFunctionOutput {
if (Bytes.validate(input) || Hex.validate(input)) return hashEncodedBytes(input)

return hashString(input)
}
Expand All @@ -32,7 +32,7 @@ export function packAndEncode(input: [string, unknown][]): HashFunctionOutput {
[[], []]
)

return hashEncodedBytes(encodePacked(types, values))
return hashEncodedBytes(AbiParameters.encodePacked(types, values))
}

/**
Expand All @@ -52,8 +52,8 @@ function hashString(input: string): HashFunctionOutput {
* @param input - Bytes represented as a hex string.
* @returns
*/
function hashEncodedBytes(input: `0x${string}` | Uint8Array): HashFunctionOutput {
const hash = BigInt(keccak256(input)) >> 8n
function hashEncodedBytes(input: Hex.Hex | Bytes.Bytes): HashFunctionOutput {
const hash = BigInt(Hash.keccak256(input, { as: 'Hex' })) >> 8n
const rawDigest = hash.toString(16)

return { hash, digest: `0x${rawDigest.padStart(64, '0')}` }
Expand Down
2 changes: 1 addition & 1 deletion packages/core/tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default defineConfig({
clean: true,
outDir: 'build',
format: ['esm', 'cjs'],
external: ['zustand', 'viem'],
external: ['zustand', 'ox'],
entry: ['src/index.ts', 'src/lib/hashing.ts', 'src/lib/backend.ts'],
define: { 'process.env.NODE_ENV': '"production"' },
})
Loading

0 comments on commit cf3a820

Please sign in to comment.