Skip to content

Commit

Permalink
chore(all): replace ethers with viem
Browse files Browse the repository at this point in the history
  • Loading branch information
szkl committed Jan 8, 2024
1 parent d57011e commit a2ab909
Show file tree
Hide file tree
Showing 98 changed files with 613 additions and 340 deletions.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file removed .yarn/cache/rlp-npm-2.2.7-4cb283b8fe-3db4dfe5c7.zip
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
3 changes: 0 additions & 3 deletions apps/console/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
"lint": "eslint --cache --cache-location ./node_modules/.cache/eslint ."
},
"dependencies": {
"@ethersproject/bytes": "5.7.0",
"@ethersproject/random": "5.7.0",
"@headlessui/react": "1.7.5",
"@heroicons/react": "2.0.13",
"@proofzero/design-system": "workspace:*",
Expand All @@ -39,7 +37,6 @@
"classnames": "2.3.2",
"connectkit": "1.5.3",
"cross-env": "7.0.3",
"ethers": "5.7.2",
"flowbite": "1.6.5",
"flowbite-react": "0.4.3",
"lodash": "4.17.21",
Expand Down
1 change: 0 additions & 1 deletion apps/passport/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
"build-storybook": "npx tailwindcss -o ./app/styles/tailwind.css && build-storybook"
},
"dependencies": {
"@ethersproject/keccak256": "5.7.0",
"@proofzero/design-system": "*",
"@proofzero/platform-clients": "workspace:*",
"@proofzero/platform.account": "workspace:*",
Expand Down
3 changes: 2 additions & 1 deletion packages/urns/idref.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { keccak256 } from '@ethersproject/keccak256'
import { keccak256 } from 'viem'

import { createRollupIdURNSpace, RollupIdURN } from '.'
import { AccountType } from '../types/account'

Expand Down
4 changes: 2 additions & 2 deletions packages/urns/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"typescript": "5.0.4"
},
"dependencies": {
"@ethersproject/keccak256": "5.7.0",
"urns": "0.6.0"
"urns": "0.6.0",
"viem": "1.20.0"
}
}
7 changes: 1 addition & 6 deletions platform/account/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,6 @@
"typescript": "5.0.4"
},
"dependencies": {
"@ethersproject/address": "5.7.0",
"@ethersproject/bytes": "5.7.0",
"@ethersproject/keccak256": "5.7.0",
"@ethersproject/random": "5.7.0",
"@ethersproject/signing-key": "5.7.0",
"@ethersproject/transactions": "5.7.0",
"@ethersproject/wallet": "5.7.0",
"@proofzero/platform-clients": "workspace:*",
"@proofzero/platform.core": "workspace:*",
Expand All @@ -46,6 +40,7 @@
"jose": "4.11.0",
"random-words": "2.0.0",
"remix-auth-google": "1.2.0",
"viem": "1.20.0",
"zod": "3.22.4"
}
}
6 changes: 3 additions & 3 deletions platform/account/src/jsonrpc/methods/resolveIdentity.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { z } from 'zod'
import { hexlify } from '@ethersproject/bytes'
import { randomBytes } from '@ethersproject/random'
import { toHex } from 'viem'

import { router } from '@proofzero/platform.core'
import { IdentityURNInput } from '@proofzero/platform-middleware/inputValidators'
Expand Down Expand Up @@ -54,7 +53,8 @@ export const resolveIdentityMethod = async ({
const decodedJwt = jose.decodeJwt(input.jwt)
urn = decodedJwt.sub as IdentityURN
} else {
const name = hexlify(randomBytes(IDENTITY_OPTIONS.length))
const buffer = new Uint8Array(IDENTITY_OPTIONS.length)
const name = toHex(crypto.getRandomValues(buffer))
urn = IdentityURNSpace.componentizedUrn(name)
eventName = 'account_created_identity'
}
Expand Down
10 changes: 4 additions & 6 deletions platform/account/src/jsonrpc/methods/revokeWalletSessionKey.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { z } from 'zod'
import { toBytes } from 'viem'

import { arrayify } from '@ethersproject/bytes'
import { BadRequestError } from '@proofzero/errors'

import { AccountURNSpace, type AccountURN } from '@proofzero/urns/account'
Expand Down Expand Up @@ -92,22 +92,20 @@ const revokeWalletSessionKey = async ({
}) => {
const owner = (await smartContractWalletNode.storage.get(
'privateKey'
)) as string
)) as '0x${string}'

if (!owner) {
throw new BadRequestError({ message: 'missing private key for the user' })
}

const signer = new Wallet(owner)

const address = await signer.getAddress()

const createRevokeSessionKeyUserOpResponse = await fetch(
'https://zerodev-api.zobeir.workers.dev/create-revoke-session-key-user-op',
{
...requestInit,
body: JSON.stringify({
address,
address: await signer.getAddress(),
projectId,
publicSessionKey,
}),
Expand All @@ -120,7 +118,7 @@ const revokeWalletSessionKey = async ({
userOpHash: string
}

const signedMessage = await signer.signMessage(arrayify(userOpHash))
const signedMessage = await signer.signMessage(toBytes(userOpHash))

await fetch('https://zerodev-api.zobeir.workers.dev/send-userop', {
...requestInit,
Expand Down
4 changes: 2 additions & 2 deletions platform/account/src/jsonrpc/methods/verifyNonce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { CryptoAccount, type AccountNode } from '../../nodes'

export const VerifyNonceInput = z.object({
nonce: z.string(),
signature: z.string(),
signature: z.string().startsWith('0x'),
jwt: z.string().optional(),
forceAccountCreation: z.boolean().optional(),
})
Expand All @@ -31,7 +31,7 @@ export const verifyNonceMethod = async ({

const nodeClient = new CryptoAccount(ctx.account as AccountNode)

await nodeClient.verifyNonce(nonce, signature)
await nodeClient.verifyNonce(nonce, signature as '0x${string}')

const caller = router.createCaller(ctx)
const { existing } = await caller.account.resolveIdentity({
Expand Down
3 changes: 2 additions & 1 deletion platform/account/src/jsonrpc/middlewares/checkCryptoNode.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { isAddress as isEthAddress } from 'viem'

import { BaseMiddlewareFunction } from '@proofzero/platform-middleware/types'
import { isAddress as isEthAddress } from '@ethersproject/address'

import { NodeType } from '@proofzero/types/account'

Expand Down
13 changes: 8 additions & 5 deletions platform/account/src/nodes/crypto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { hexlify } from '@ethersproject/bytes'
import { randomBytes } from '@ethersproject/random'
import { toHex } from 'viem'

import type { Scope } from '@proofzero/types/authorization'
import { CryptoAccountType } from '@proofzero/types/account'
Expand Down Expand Up @@ -28,7 +27,8 @@ export default class CryptoAccount {
scope: Scope,
state: string
): Promise<string> {
const nonce = hexlify(randomBytes(NONCE_OPTIONS.length))
const buffer = new Uint8Array(NONCE_OPTIONS.length)
const nonce = toHex(crypto.getRandomValues(buffer))
const timestamp = Date.now()

const challenges =
Expand All @@ -50,7 +50,10 @@ export default class CryptoAccount {
return nonce
}

async verifyNonce(nonce: string, signature: string): Promise<Challenge> {
async verifyNonce(
nonce: string,
signature: '0x${string}'
): Promise<Challenge> {
const challenges: Record<string, Challenge> =
(await this.node.storage.get<Record<string, Challenge>>('challenges')) ||
{}
Expand All @@ -61,7 +64,7 @@ export default class CryptoAccount {
}

const message = challenge.template.slice().replace(/{{nonce}}/, nonce)
const address = recoverEthereumAddress(message, signature)
const address = await recoverEthereumAddress(message, signature)

if (address != challenge.address) {
throw new Error('not matching address')
Expand Down
12 changes: 5 additions & 7 deletions platform/account/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { keccak256 } from '@ethersproject/keccak256'
import { recoverPublicKey } from '@ethersproject/signing-key'
import { computeAddress } from '@ethersproject/transactions'
import { keccak256, recoverAddress } from 'viem'

import {
CryptoAccountType,
Expand Down Expand Up @@ -89,13 +87,13 @@ export const isValidAccountType = (type: string) => {

export const recoverEthereumAddress = (
message: string,
signature: string
): string => {
signature: '0x${string}'
): Promise<string> => {
const prefix = `\u0019Ethereum Signed Message:\n${message.length}`
const encoder = new TextEncoder()
const bytes = encoder.encode(`${prefix}${message}`)
const digest = keccak256(bytes)
return computeAddress(recoverPublicKey(digest, signature))
const hash = keccak256(bytes)
return recoverAddress({ hash, signature })
}

export const generateSmartWalletAccountUrn = (
Expand Down
3 changes: 1 addition & 2 deletions platform/authorization/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@
"wrangler": "3.2.0"
},
"dependencies": {
"@ethersproject/bytes": "5.7.0",
"@ethersproject/random": "5.7.0",
"@proofzero/errors": "workspace:*",
"@proofzero/platform-clients": "workspace:*",
"@proofzero/platform-middleware": "workspace:*",
Expand All @@ -43,6 +41,7 @@
"async": "3.2.4",
"do-proxy": "1.3.3",
"jose": "4.11.0",
"viem": "1.20.0",
"zod": "3.22.4"
}
}
6 changes: 3 additions & 3 deletions platform/authorization/src/jsonrpc/methods/authorize.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { z } from 'zod'
import { toHex } from 'viem'

import { IdentityURNInput } from '@proofzero/platform-middleware/inputValidators'

import { Context } from '../../context'
import { CODE_OPTIONS } from '../../constants'
import { initExchangeCodeNodeByName } from '../../nodes'
import { hexlify } from '@ethersproject/bytes'
import { randomBytes } from '@ethersproject/random'
import { PersonaData } from '@proofzero/types/application'
import { createAnalyticsEvent } from '@proofzero/utils/analytics'

Expand Down Expand Up @@ -44,7 +43,8 @@ export const authorizeMethod = async ({
state,
} = input

const code = hexlify(randomBytes(CODE_OPTIONS.length))
const buffer = new Uint8Array(CODE_OPTIONS.length)
const code = toHex(crypto.getRandomValues(buffer))

// TODO: validate the scopes are legitmate here or when we ask for it back in exchangeToken

Expand Down
14 changes: 9 additions & 5 deletions platform/authorization/src/nodes/authorization.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { DOProxy } from 'do-proxy'

import * as jose from 'jose'

import { hexlify } from '@ethersproject/bytes'
import { randomBytes } from '@ethersproject/random'
import { toHex } from 'viem'

import { InternalServerError, RollupError } from '@proofzero/errors'
import { NodeMethodReturnValue } from '@proofzero/types/node'
Expand Down Expand Up @@ -89,7 +87,10 @@ export default class Authorization extends DOProxy {
options
const { alg, kid } = jwk
if (!alg) throw new InternalServerError({ message: 'missing alg in jwk' })
const jti = hexlify(randomBytes(JWT_OPTIONS.jti.length))

const buffer = new Uint8Array(JWT_OPTIONS.jti.length)
const jti = toHex(crypto.getRandomValues(buffer))

//Need to convert scope array to space-delimited string, per spec
return new jose.SignJWT({ scope: scope.join(' ') })
.setProtectedHeader({ alg, jku, kid, typ: 'JWT' })
Expand All @@ -106,7 +107,10 @@ export default class Authorization extends DOProxy {
const { jku, jwk, identity, clientId, issuer, scope } = options
const { alg, kid } = jwk
if (!alg) throw new InternalServerError({ message: 'missing alg in jwk' })
const jti = hexlify(randomBytes(JWT_OPTIONS.jti.length))

const buffer = new Uint8Array(JWT_OPTIONS.jti.length)
const jti = toHex(crypto.getRandomValues(buffer))

const jwt = await new jose.SignJWT({ scope: scope.join(' ') })
.setProtectedHeader({ alg, jku, kid, typ: 'JWT' })
.setAudience([clientId])
Expand Down
3 changes: 0 additions & 3 deletions platform/billing/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@
"typescript": "5.0.4"
},
"dependencies": {
"@ethersproject/address": "5.7.0",
"@ethersproject/bytes": "5.7.0",
"@ethersproject/random": "5.7.0",
"@proofzero/platform-middleware": "workspace:*",
"@proofzero/platform.core": "workspace:*",
"@proofzero/types": "workspace:*",
Expand Down
1 change: 0 additions & 1 deletion platform/galaxy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
"start": "echo \"No start for galaxy. Please use dev.\""
},
"dependencies": {
"@ethersproject/providers": "5.7.2",
"@graphql-tools/merge": "8.4.1",
"@graphql-tools/resolvers-composition": "6.5.18",
"@graphql-tools/schema": "9.0.19",
Expand Down
6 changes: 2 additions & 4 deletions platform/identity/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@
"typescript": "5.0.4"
},
"dependencies": {
"@ethersproject/address": "5.7.0",
"@ethersproject/bytes": "5.7.0",
"@ethersproject/random": "5.7.0",
"@proofzero/platform-middleware": "workspace:*",
"@proofzero/platform.core": "workspace:*",
"@proofzero/types": "workspace:*",
Expand All @@ -38,6 +35,7 @@
"do-proxy": "1.3.3",
"ts-set-utils": "0.2.0",
"typed-json-rpc": "1.1.0",
"urns": "0.6.0"
"urns": "0.6.0",
"viem": "1.20.0"
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { z } from 'zod'
import { toHex } from 'viem'

import { router } from '@proofzero/platform.core'
import { hexlify } from '@ethersproject/bytes'
import { randomBytes } from '@ethersproject/random'
import { IDENTITY_GROUP_OPTIONS } from '../../../constants'
import { IdentityGroupURNSpace } from '@proofzero/urns/identity-group'
import { EDGE_MEMBER_OF_IDENTITY_GROUP } from '@proofzero/types/graph'
Expand All @@ -27,7 +27,9 @@ export const createIdentityGroup = async ({
input: CreateIdentityGroupInput
ctx: Context
}): Promise<CreateIdentityGroupOutput> => {
const name = hexlify(randomBytes(IDENTITY_GROUP_OPTIONS.length))
const buffer = new Uint8Array(IDENTITY_GROUP_OPTIONS.length)
const name = toHex(crypto.getRandomValues(buffer))

const groupURN = IdentityGroupURNSpace.componentizedUrn(name, undefined, {
name: input.name,
})
Expand Down
4 changes: 2 additions & 2 deletions platform/images/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
"deploy": "wrangler publish"
},
"dependencies": {
"@ethersproject/keccak256": "5.7.0",
"@proofzero/utils": "workspace:*",
"date-fns": "2.29.3",
"image-data-uri": "2.0.1",
"svg2png-wasm": "1.3.4",
"tiny-invariant": "1.3.1"
"tiny-invariant": "1.3.1",
"viem": "1.20.0"
}
}
3 changes: 2 additions & 1 deletion platform/images/src/jsonrpc/methods/getGradient.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { z } from 'zod'
import { keccak256 } from '@ethersproject/keccak256'
import { keccak256 } from 'viem'

import colors from '../../assets/colors.json'
//@ts-ignore
import wasm from '../../assets/svg2png_wasm_bg.wasm'
Expand Down
4 changes: 2 additions & 2 deletions platform/starbase/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
"typescript": "5.0.4"
},
"dependencies": {
"@ethersproject/bytes": "5.7.0",
"@proofzero/platform.authorization": "workspace:*",
"@proofzero/security": "workspace:*",
"@proofzero/urns": "workspace:*",
Expand All @@ -41,6 +40,7 @@
"lodash": "4.17.21",
"multiformats": "10.0.2",
"tiny-invariant": "1.3.1",
"ts-set-utils": "0.2.0"
"ts-set-utils": "0.2.0",
"viem": "1.20.0"
}
}
Loading

0 comments on commit a2ab909

Please sign in to comment.