Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ERC-6492 sig utils #2209

Merged
merged 4 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .changeset/funny-cars-allow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"viem": minor
---

Added [ERC-6492](https://eips.ethereum.org/EIPS/eip-6492) signature utilities:

- `isErc6492Signature`
- `parseErc6492Signature`
- `serializeErc6492Signature`
10 changes: 10 additions & 0 deletions .changeset/three-windows-collect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
"viem": minor
---

Deprecated utilities (will be removed in v3):

- `hexToSignature` – use `parseSignature` instead.
- `hexToCompactSignature` – use `parseCompactSignature` instead.
- `compactSignatureToHex` – use `serializeCompactSignature` instead.
- `signatureToHex` – use `serializeSignature` instead.
35 changes: 35 additions & 0 deletions site/pages/docs/utilities/isErc6492Signature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
description: Checks whether the signature is in ERC-6492 format.
---

# isErc6492Signature

Checks whether the signature is in [ERC-6492](https://eips.ethereum.org/EIPS/eip-6492) format.

## Import

```ts
import { isErc6492Signature } from 'viem'
```

## Usage

```ts twoslash
import { isErc6492Signature } from 'viem'

const result = isErc6492Signature('0x000000000000000000000000cafebabecafebabecafebabecafebabecafebabe000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000004deadbeef000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000041a461f509887bd19e312c0c58467ce8ff8e300d3c1a90b608a760c5b80318eaf15fe57c96f9175d6cd4daad4663763baa7e78836e067d0163e9a2ccf2ff753f5b1b000000000000000000000000000000000000000000000000000000000000006492649264926492649264926492649264926492649264926492649264926492')
```

## Returns

`boolean`

Whether the signature is in ERC-6492 format.

## Parameters

### signature

- **Type:** [`Hex`](/docs/glossary/types#hex)

The signature to check.
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@
description: Parses a hex formatted compact signature into a structured compact signature.
---

# hexToCompactSignature
# parseCompactSignature

Parses a hex formatted compact signature into a structured ("split") compact signature.

## Import

```ts
import { hexToCompactSignature } from 'viem'
import { parseCompactSignature } from 'viem'
```

## Usage

```ts
import { hexToCompactSignature } from 'viem'
import { parseCompactSignature } from 'viem'

hexToCompactSignature('0x9328da16089fcba9bececa81663203989f2df5fe1faa6291a45381c81bd17f76939c6d6b623b42da56557e5e734a43dc83345ddfadec52cbe24d0cc64f550793') // [!code focus:7]
parseCompactSignature('0x9328da16089fcba9bececa81663203989f2df5fe1faa6291a45381c81bd17f76939c6d6b623b42da56557e5e734a43dc83345ddfadec52cbe24d0cc64f550793') // [!code focus:7]
/**
* {
* r: '0x9328da16089fcba9bececa81663203989f2df5fe1faa6291a45381c81bd17f76',
Expand Down
48 changes: 48 additions & 0 deletions site/pages/docs/utilities/parseErc6492Signature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
description: Parses a hex-formatted ERC-6492 flavoured signature.
---

# parseErc6492Signature

Parses a hex-formatted [ERC-6492](https://eips.ethereum.org/EIPS/eip-6492) flavoured signature.

If the signature is not in ERC-6492 format, then the underlying (original) signature is returned.

## Import

```ts
import { parseErc6492Signature } from 'viem'
```

## Usage

```ts twoslash
import { parseErc6492Signature } from 'viem'

const { // [!code focus:99]
factoryAddress,
factoryData,
signature,
} = parseErc6492Signature('0x000000000000000000000000cafebabecafebabecafebabecafebabecafebabe000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000004deadbeef000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000041a461f509887bd19e312c0c58467ce8ff8e300d3c1a90b608a760c5b80318eaf15fe57c96f9175d6cd4daad4663763baa7e78836e067d0163e9a2ccf2ff753f5b1b000000000000000000000000000000000000000000000000000000000000006492649264926492649264926492649264926492649264926492649264926492')
/**
* {
* factoryAddress: '0xCafEBAbECAFEbAbEcaFEbabECAfebAbEcAFEBaBe',
* factoryData: '0xdeadbeef',
* signature: '0xa461f509887bd19e312c0c58467ce8ff8e300d3c1a90b608a760c5b80318eaf15fe57c96f9175d6cd4daad4663763baa7e78836e067d0163e9a2ccf2ff753f5b1b'
* }
*/
```

## Returns

`ParseErc6492SignatureReturnType`

The ERC-6492 signature components.

## Parameters

### signature

- **Type:** [`Hex`](/docs/glossary/types#hex)

The ERC-6492 signature in hex format.
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@
description: Parses a hex formatted signature into a structured signature.
---

# hexToSignature
# parseSignature

Parses a hex formatted signature into a structured ("split") signature.

## Import

```ts
import { hexToSignature } from 'viem'
import { parseSignature } from 'viem'
```

## Usage

```ts
import { hexToSignature } from 'viem'
import { parseSignature } from 'viem'

hexToSignature('0x6e100a352ec6ad1b70802290e18aeed190704973570f3b8ed42cb9808e2ea6bf4a90a229a244495b41890987806fcbd2d5d23fc0dbe5f5256c2613c039d76db81c') // [!code focus:8]
parseSignature('0x6e100a352ec6ad1b70802290e18aeed190704973570f3b8ed42cb9808e2ea6bf4a90a229a244495b41890987806fcbd2d5d23fc0dbe5f5256c2613c039d76db81c') // [!code focus:8]
/**
* {
* r: '0x6e100a352ec6ad1b70802290e18aeed190704973570f3b8ed42cb9808e2ea6bf',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
---
description: Parses a compact signature into hex format.
description: Serializes a compact signature into hex format.
---

# compactSignatureToHex
# serializeCompactSignature

Parses a [EIP-2098](https://eips.ethereum.org/EIPS/eip-2098) compact signature into hex format.
Serializes a [EIP-2098](https://eips.ethereum.org/EIPS/eip-2098) compact signature into hex format.

## Import

```ts
import { compactSignatureToHex } from 'viem'
import { serializeCompactSignature } from 'viem'
```

## Usage

```ts
import { compactSignatureToHex } from 'viem'
import { serializeCompactSignature } from 'viem'

compactSignatureToHex({ // [!code focus:8]
serializeCompactSignature({ // [!code focus:8]
r: '0x68a020a209d3d56c46f38cc50a33f704f4a9a10a59377f8dd762ac66910e9b90',
yParityAndS:
'0x7e865ad05c4035ab5792787d4a0297a43617ae897930a6fe4d822b8faea52064',
Expand Down
52 changes: 52 additions & 0 deletions site/pages/docs/utilities/serializeErc6492Signature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
---
description: Serializes a ERC-6492 flavoured signature into hex format.
---

# serializeErc6492Signature

Serializes a [ERC-6492](https://eips.ethereum.org/EIPS/eip-6492) flavoured signature into hex format.

## Import

```ts
import { serializeErc6492Signature } from 'viem'
```

## Usage

```ts twoslash
import { serializeErc6492Signature } from 'viem'

serializeErc6492Signature({ // [!code focus:99]
factoryAddress: '0xcafebabecafebabecafebabecafebabecafebabe',
factoryData: '0xdeadbeef',
signature: '0x41a461f509887bd19e312c0c58467ce8ff8e300d3c1a90b608a760c5b80318eaf15fe57c96f9175d6cd4daad4663763baa7e78836e067d0163e9a2ccf2ff753f5b1b',
})
// "0x000000000000000000000000cafebabecafebabecafebabecafebabecafebabe000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000004deadbeef000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000041a461f509887bd19e312c0c58467ce8ff8e300d3c1a90b608a760c5b80318eaf15fe57c96f9175d6cd4daad4663763baa7e78836e067d0163e9a2ccf2ff753f5b1b000000000000000000000000000000000000000000000000000000000000006492649264926492649264926492649264926492649264926492649264926492"
```

## Returns

[`Hex`](/docs/glossary/types#hex)

The hex formatted signature.

## Parameters

### factoryAddress

- **Type:** `Address`

The ERC-4337 Account Factory address to use for counterfactual verification.

### factoryData

- **Type:** `Hex`

Calldata to pass to deploy the ERC-4337 Account (if not deployed) for counterfactual verification.

### signature

- **Type:** `Hex`

The original signature.
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
---
description: Parses a hex formatted signature into a structured signature.
description: Serializes a structured signature into hex format.
---

# signatureToHex
# serializeSignature

Parses a hex formatted signature into a structured ("split") signature.
Serializes a structured signature into hex format.

## Import

```ts
import { signatureToHex } from 'viem'
import { serializeSignature } from 'viem'
```

## Usage

```ts
import { signatureToHex } from 'viem'
import { serializeSignature } from 'viem'

signatureToHex({
serializeSignature({
r: '0x6e100a352ec6ad1b70802290e18aeed190704973570f3b8ed42cb9808e2ea6bf',
s: '0x4a90a229a244495b41890987806fcbd2d5d23fc0dbe5f5256c2613c039d76db8',
yParity: 1
Expand Down
36 changes: 24 additions & 12 deletions site/sidebar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -866,10 +866,6 @@ export const sidebar = {
{
text: 'Signature',
items: [
{
text: 'compactSignatureToHex',
link: '/docs/utilities/compactSignatureToHex',
},
{
text: 'compactSignatureToSignature',
link: '/docs/utilities/compactSignatureToSignature',
Expand All @@ -883,12 +879,20 @@ export const sidebar = {
link: '/docs/utilities/hashTypedData',
},
{
text: 'hexToCompactSignature',
link: '/docs/utilities/hexToCompactSignature',
text: 'isErc6492Signature',
link: '/docs/utilities/isErc6492Signature',
},
{
text: 'parseCompactSignature',
link: '/docs/utilities/parseCompactSignature',
},
{
text: 'hexToSignature',
link: '/docs/utilities/hexToSignature',
text: 'parseErc6492Signature',
link: '/docs/utilities/parseErc6492Signature',
},
{
text: 'parseSignature',
link: '/docs/utilities/parseSignature',
},
{
text: 'recoverAddress',
Expand All @@ -911,12 +915,20 @@ export const sidebar = {
link: '/docs/utilities/recoverTypedDataAddress',
},
{
text: 'signatureToCompactSignature',
link: '/docs/utilities/signatureToCompactSignature',
text: 'serializeCompactSignature',
link: '/docs/utilities/serializeCompactSignature',
},
{
text: 'serializeErc6492Signature',
link: '/docs/utilities/serializeErc6492Signature',
},
{
text: 'signatureToHex',
link: '/docs/utilities/signatureToHex',
text: 'serializeSignature',
link: '/docs/utilities/serializeSignature',
},
{
text: 'signatureToCompactSignature',
link: '/docs/utilities/signatureToCompactSignature',
},
{
text: 'verifyMessage',
Expand Down
16 changes: 16 additions & 0 deletions site/vercel.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,22 @@
{
"source": "/:match*.html",
"destination": "/:match"
},
{
"source": "/:match/hexToSignature",
"destination": "/:match/parseSignature"
},
{
"source": "/:match/hexToCompactSignature",
"destination": "/:match/parseCompactSignature"
},
{
"source": "/:match/signatureToHex",
"destination": "/:match/serializeSignature"
},
{
"source": "/:match/compactSignatureToHex",
"destination": "/:match/serializeCompactSignature"
}
]
}
10 changes: 7 additions & 3 deletions src/accounts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,13 @@ export {
sign,
} from './utils/sign.js'
export {
type SignatureToHexErrorType,
signatureToHex,
} from '../utils/signature/signatureToHex.js'
/** @deprecated Use `SignatureToHexErrorType` instead. */
type SerializeSignatureErrorType as SignatureToHexErrorType,
/** @deprecated Use `serializeSignature` instead. */
serializeSignature as signatureToHex,
type SerializeSignatureErrorType,
serializeSignature,
} from '../utils/signature/serializeSignature.js'
export {
type SignMessageErrorType,
type SignMessageParameters,
Expand Down
Loading
Loading