Skip to content

Commit

Permalink
Merge branch 'wevm:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
nikola-bozin-txfusion authored May 24, 2024
2 parents 3db392e + 61baec0 commit b909845
Show file tree
Hide file tree
Showing 19 changed files with 122 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/shaggy-onions-check.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"viem": patch
---

Fixed parsing SIWE message containing domain with port
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ coverage
node_modules
tsconfig*.tsbuildinfo
wagmi
src/node/trustedSetups_esm.ts
**/trusted-setups/**/*.txt
.idea

Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
"bench": "vitest bench",
"bench:ci": "CI=true vitest bench",
"build": "bun run clean && bun run build:cjs && bun run build:esm && bun run build:types",
"build:cjs": "tsc --project ./tsconfig.build.json --module commonjs --outDir ./src/_cjs --removeComments --verbatimModuleSyntax false && printf '{\"type\":\"commonjs\"}' > ./src/_cjs/package.json",
"build:cjs": "bun run build:trustedSetups:start && tsc --project ./tsconfig.build.json --module commonjs --outDir ./src/_cjs --removeComments --verbatimModuleSyntax false && printf '{\"type\":\"commonjs\"}' > ./src/_cjs/package.json && bun run build:trustedSetups:end",
"build:esm": "tsc --project ./tsconfig.build.json --module es2015 --outDir ./src/_esm && printf '{\"type\": \"module\",\"sideEffects\":false}' > ./src/_esm/package.json",
"build:trustedSetups:start": "mv src/node/trustedSetups.ts src/node/trustedSetups_esm.ts && mv src/node/trustedSetups_cjs.ts src/node/trustedSetups.ts",
"build:trustedSetups:end": "mv src/node/trustedSetups.ts src/node/trustedSetups_cjs.ts && mv src/node/trustedSetups_esm.ts src/node/trustedSetups.ts",
"build:types": "tsc --project ./tsconfig.build.json --module esnext --declarationDir ./src/_types --emitDeclarationOnly --declaration --declarationMap",
"chaincheck": "bun test test/chains/check.test.ts",
"changeset": "changeset",
Expand Down
14 changes: 14 additions & 0 deletions src/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
# viem

## 2.12.1

### Patch Changes

- [#2299](https://github.com/wevm/viem/pull/2299) [`144d90e09231cdbb105c95b0b52332cd1a97bbbb`](https://github.com/wevm/viem/commit/144d90e09231cdbb105c95b0b52332cd1a97bbbb) Thanks [@tmm](https://github.com/tmm)! - Fixed `createSiweMessage` `domain` validation.

- [#2292](https://github.com/wevm/viem/pull/2292) [`176a9e7bae90285401878cfeb54c09a7f7d8881b`](https://github.com/wevm/viem/commit/176a9e7bae90285401878cfeb54c09a7f7d8881b) Thanks [@nidz-the-fact](https://github.com/nidz-the-fact)! - Fixed `thaiChain` RPC URL.

- [#2298](https://github.com/wevm/viem/pull/2298) [`601cb741f7ba526c5441ab3fe6a485d283b85fd1`](https://github.com/wevm/viem/commit/601cb741f7ba526c5441ab3fe6a485d283b85fd1) Thanks [@tmm](https://github.com/tmm)! - Fixed support for ESM for 4337 trusted setups.

- [#2301](https://github.com/wevm/viem/pull/2301) [`36c55da99111714bfda5b55d5e855cc8c4a121d9`](https://github.com/wevm/viem/commit/36c55da99111714bfda5b55d5e855cc8c4a121d9) Thanks [@jxom](https://github.com/jxom)! - Fixed `isAddress` cache.

- [#2296](https://github.com/wevm/viem/pull/2296) [`efb77bf2dd201caad6d538767cd04790f42892a0`](https://github.com/wevm/viem/commit/efb77bf2dd201caad6d538767cd04790f42892a0) Thanks [@JackHamer09](https://github.com/JackHamer09)! - Added `native` explorers for zkSync and zkSync Sepolia Testnet chains.

## 2.12.0

### Minor Changes
Expand Down
2 changes: 1 addition & 1 deletion src/actions/wallet/signTransaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ describe('eip1559', () => {
...baseEip1559,
})
expect(signature).toMatchInlineSnapshot(
`"0x02f855018203118085016b25dda6825208808080c026a0894d2e76b7b356750c81d8912d2f7e18dffa7cc49a3ceaa124f586814f981b59a016ab3f7fb02995af583cc345a7a5f1268da9c016dc1ac203a8565e0e7502af12"`,
`"0x02f855018203118085016b25dda6825208808080c001a0894d2e76b7b356750c81d8912d2f7e18dffa7cc49a3ceaa124f586814f981b59a016ab3f7fb02995af583cc345a7a5f1268da9c016dc1ac203a8565e0e7502af12"`,
)
})

Expand Down
2 changes: 1 addition & 1 deletion src/chains/definitions/thaiChain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const thaiChain = /*#__PURE__*/ defineChain({
nativeCurrency: { name: 'TCH', symbol: 'TCH', decimals: 18 },
rpcUrls: {
default: {
http: ['hhttps://rpc.thaichain.org'],
http: ['https://rpc.thaichain.org'],
},
},
blockExplorers: {
Expand Down
4 changes: 4 additions & 0 deletions src/chains/definitions/zkSync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ export const zkSync = /*#__PURE__*/ defineChain({
url: 'https://era.zksync.network/',
apiUrl: 'https://api-era.zksync.network/api',
},
native: {
name: 'zkSync Explorer',
url: 'https://explorer.zksync.io/',
},
},
contracts: {
multicall3: {
Expand Down
6 changes: 5 additions & 1 deletion src/chains/definitions/zkSyncSepoliaTestnet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ export const zkSyncSepoliaTestnet = /*#__PURE__*/ defineChain({
},
blockExplorers: {
default: {
name: 'zkExplorer',
name: 'Etherscan',
url: 'https://sepolia-era.zksync.network/',
},
native: {
name: 'zkSync Explorer',
url: 'https://sepolia.explorer.zksync.io/',
},
},
Expand Down
2 changes: 1 addition & 1 deletion src/errors/version.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const version = '2.12.0'
export const version = '2.12.1'
2 changes: 1 addition & 1 deletion src/jsr.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@wevm/viem",
"version": "2.12.0",
"version": "2.12.1",
"exports": {
".": "./index.ts",
"./accounts": "./accounts/index.ts",
Expand Down
10 changes: 9 additions & 1 deletion src/node/trustedSetups.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { resolve } from 'node:path'
import { dirname, resolve } from 'node:path'
import { fileURLToPath } from 'node:url'

const __filename = fileURLToPath(
// import.meta' meta-property is only allowed when the '--module' option is 'es2020', 'es2022', 'esnext', 'system', 'node16', or 'nodenext'
// @ts-ignore
import.meta.url,
)
const __dirname = dirname(__filename)

export const mainnetTrustedSetupPath = resolve(
__dirname,
Expand Down
11 changes: 11 additions & 0 deletions src/node/trustedSetups_cjs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { resolve } from 'node:path'

export const mainnetTrustedSetupPath = resolve(
__dirname,
'../trusted-setups/mainnet.json',
)

export const minimalTrustedSetupPath = resolve(
__dirname,
'../trusted-setups/mainnet.json',
)
2 changes: 1 addition & 1 deletion src/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "viem",
"description": "TypeScript Interface for Ethereum",
"version": "2.12.0",
"version": "2.12.1",
"type": "module",
"main": "./_cjs/index.js",
"module": "./_esm/index.js",
Expand Down
5 changes: 3 additions & 2 deletions src/utils/address/isAddress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,16 @@ export function isAddress(
options?: IsAddressOptions | undefined,
): address is Address {
const { strict = true } = options ?? {}
const cacheKey = `${address}.${strict}`

if (isAddressCache.has(address)) return isAddressCache.get(address)!
if (isAddressCache.has(cacheKey)) return isAddressCache.get(cacheKey)!

const result = (() => {
if (!addressRegex.test(address)) return false
if (address.toLowerCase() === address) return true
if (strict) return checksumAddress(address as Address) === address
return true
})()
isAddressCache.set(address, result)
isAddressCache.set(cacheKey, result)
return result
}
34 changes: 34 additions & 0 deletions src/utils/siwe/createSiweMessage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,3 +332,37 @@ test('behavior: invalid resources', () => {
Version: [email protected]]
`)
})

test.each([
'example.com',
'localhost',
'127.0.0.1',
'example.com:3000',
'localhost:3000',
'127.0.0.1:3000',
])('valid domain `%s`', (domain) => {
expect(
createSiweMessage({
...message,
domain,
}),
).toBeTypeOf('string')
})

test.each([
'http://example.com',
'http://localhost',
'http://127.0.0.1',
'http://example.com:3000',
'http://localhost:3000',
'http://127.0.0.1:3000',
'foobarbaz',
'-example.com',
])('invalid domain `%s`', (domain) => {
expect(() =>
createSiweMessage({
...message,
domain,
}),
).toThrowError()
})
14 changes: 12 additions & 2 deletions src/utils/siwe/createSiweMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,13 @@ export function createSiweMessage(
`Provided value: ${chainId}`,
],
})
if (!domainRegex.test(domain))
if (
!(
domainRegex.test(domain) ||
ipRegex.test(domain) ||
localhostRegex.test(domain)
)
)
throw new SiweInvalidMessageFieldError({
field: 'domain',
metaMessages: [
Expand Down Expand Up @@ -163,6 +169,10 @@ export function createSiweMessage(
return `${prefix}\n${suffix}`
}

const domainRegex = /^(?:(?:(?!-)[a-zA-Z0-9-]{1,63}(?<!-)\.)+[a-zA-Z]{2,63})$/
const domainRegex =
/^([a-zA-Z0-9][-a-zA-Z0-9]{0,61}[a-zA-Z0-9])\.[a-zA-Z]{2,}(:[0-9]{1,5})?$/
const ipRegex =
/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(:[0-9]{1,5})?$/
const localhostRegex = /^localhost(:[0-9]{1,5})?$/
const nonceRegex = /^[a-zA-Z0-9]{8,}$/
const schemeRegex = /^([a-zA-Z][a-zA-Z0-9+-.]*)$/
13 changes: 13 additions & 0 deletions src/utils/siwe/parseSiweMessage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,19 @@ Issued At: 2023-02-01T00:00:00.000Z`
expect(parsed.scheme).toMatchInlineSnapshot(`"https"`)
})

test('behavior: domain with port', () => {
const message = `example.com:8080 wants you to sign in with your Ethereum account:
0xA0Cf798816D4b9b9866b5330EEa46a18382f251e
URI: https://example.com/path
Version: 1
Chain ID: 1
Nonce: foobarbaz
Issued At: 2023-02-01T00:00:00.000Z`
const parsed = parseSiweMessage(message)
expect(parsed.domain).toMatchInlineSnapshot('example.com:8080')
})

test('behavior: with statement', () => {
const message = `example.com wants you to sign in with your Ethereum account:
0xA0Cf798816D4b9b9866b5330EEa46a18382f251e
Expand Down
2 changes: 1 addition & 1 deletion src/utils/siwe/parseSiweMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export function parseSiweMessage(

// https://regexr.com/80gdj
const prefixRegex =
/^(?:(?<scheme>[a-zA-Z][a-zA-Z0-9+-.]*):\/\/)?(?<domain>[a-zA-Z0-9+-.]*) (?:wants you to sign in with your Ethereum account:\n)(?<address>0x[a-fA-F0-9]{40})\n\n(?:(?<statement>.*)\n\n)?/
/^(?:(?<scheme>[a-zA-Z][a-zA-Z0-9+-.]*):\/\/)?(?<domain>[a-zA-Z0-9+-.]*(?::[0-9]{1,5})?) (?:wants you to sign in with your Ethereum account:\n)(?<address>0x[a-fA-F0-9]{40})\n\n(?:(?<statement>.*)\n\n)?/

// https://regexr.com/80gf9
const suffixRegex =
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"exclude": [
"src/**/*.test.ts",
"src/**/*.test-d.ts",
"src/**/*.bench.ts"
"src/**/*.bench.ts",
"src/node/trustedSetups_esm.ts"
],
"compilerOptions": {
"moduleResolution": "node",
Expand Down

0 comments on commit b909845

Please sign in to comment.