Skip to content

Commit a995c11

Browse files
committed
feat: signature
1 parent af7c1bb commit a995c11

26 files changed

+1395
-25
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
---
2+
description: Deserializes a Bytes or Hex signature into a structured Signature.
3+
---
4+
5+
# Signature.deserialize
6+
7+
**Alias:** `deserializeSignature`
8+
9+
Deserializes a [Bytes](/api/bytes) or [Hex](/api/hex) signature into a structured [Signature](/api/signature#signature-1).
10+
11+
## Imports
12+
13+
```ts twoslash
14+
// @noErrors
15+
// Named Import
16+
import { Signature } from 'ox'
17+
18+
// Entrypoint Imports
19+
import * as Signature from 'ox/Signature'
20+
import { deserializeSignature } from 'ox/Signature'
21+
```
22+
23+
## Usage
24+
25+
```ts twoslash
26+
// @noErrors
27+
import { Signature } from 'ox'
28+
29+
const signature = Signature.deserialize('0x6e100a352ec6ad1b70802290e18aeed190704973570f3b8ed42cb9808e2ea6bf4a90a229a244495b41890987806fcbd2d5d23fc0dbe5f5256c2613c039d76db81c')
30+
// @log: {
31+
// @log: r: 49782753348462494199823712700004552394425719014458918871452329774910450607807n,
32+
// @log: s: 33726695977844476214676913201140481102225469284307016937915595756355928419768n,
33+
// @log: yParity: 1,
34+
// @log: }
35+
```
36+
37+
## Returns
38+
39+
[`Signature`](/api/signature#signature-1).
40+
41+
Signature object.
42+
43+
## Parameters
44+
45+
### serializedSignature
46+
47+
- **Type:** `Bytes | Hex`
48+
49+
Signature to deserialize.

site/pages/api/signature/from.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
---
2+
description: Instantiates a typed Signature object from a compatible signature value.
3+
---
4+
5+
# Signature.from
6+
7+
**Alias:** `toSignature`
8+
9+
Instantiates a typed [Signature](/api/signature#signature-1) object from a [Signature](/api/signature#signature-1), [CompactSignature](/api/signature#compactsignature), [LegacySignature](#legacysignature), [Bytes](/api/bytes), or [Hex](/api/hex).
10+
11+
## Imports
12+
13+
```ts twoslash
14+
// @noErrors
15+
// Named Import
16+
import { Signature } from 'ox'
17+
18+
// Entrypoint Imports
19+
import * as Signature from 'ox/Signature'
20+
import { toSignature } from 'ox/Signature'
21+
```
22+
23+
## Usage
24+
25+
```ts twoslash
26+
// @noErrors
27+
import { Signature } from 'ox'
28+
29+
// from Serialized Hex Signature
30+
const signature = Signature.from('0x6e100a352ec6ad1b70802290e18aeed190704973570f3b8ed42cb9808e2ea6bf4a90a229a244495b41890987806fcbd2d5d23fc0dbe5f5256c2613c039d76db801')
31+
// @log: {
32+
// @log: r: 49782753348462494199823712700004552394425719014458918871452329774910450607807n,
33+
// @log: s: 33726695977844476214676913201140481102225469284307016937915595756355928419768n,
34+
// @log: yParity: 1,
35+
// @log: }
36+
37+
// from Compact Signature
38+
const signature = Signature.from({
39+
r: 49782753348462494199823712700004552394425719014458918871452329774910450607807n,
40+
yParityAndS: 57228803202727131502949358313456071280488184270258293674242124340113824882788n,
41+
})
42+
43+
// from Legacy Signature
44+
const signature = Signature.from({
45+
r: 49782753348462494199823712700004552394425719014458918871452329774910450607807n,
46+
s: 33726695977844476214676913201140481102225469284307016937915595756355928419768n,
47+
v: 28,
48+
})
49+
```
50+
51+
## Returns
52+
53+
[`Signature`](/api/signature#signature-1)
54+
55+
Signature object.
56+
57+
## Parameters
58+
59+
### value
60+
61+
- **Type:** `Signature | CompactSignature | LegacySignature | Bytes | Hex`
62+
63+
A signature value represented as a [Signature](/api/signature#signature-1), [CompactSignature](/api/signature#compactsignature), [LegacySignature](/api/signature#legacysignature), [Bytes](/api/bytes), or [Hex](/api/hex).
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
---
2+
description: Converts an EIP-2098 Compact Signature into a Signature.
3+
---
4+
5+
# Signature.fromCompact
6+
7+
**Alias:** `compactSignatureToSignature`
8+
9+
Converts an [EIP-2098](https://eips.ethereum.org/EIPS/eip-2098) [CompactSignature](/api/signature#compactsignature) into a [Signature](/api/signature#signature).
10+
11+
## Imports
12+
13+
```ts twoslash
14+
// @noErrors
15+
// Named Import
16+
import { Signature } from 'ox'
17+
18+
// Entrypoint Imports
19+
import * as Signature from 'ox/Signature'
20+
import { compactSignatureToSignature } from 'ox/Signature'
21+
```
22+
23+
## Usage
24+
25+
```ts twoslash
26+
import { Signature } from 'ox'
27+
28+
const signature = Signature.fromCompact({
29+
r: 47323457007453657207889730243826965761922296599680473886588287015755652701072n,
30+
yParityAndS: 57228803202727131502949358313456071280488184270258293674242124340113824882788n,
31+
})
32+
// @log: {
33+
// @log: r: 47323457007453657207889730243826965761922296599680473886588287015755652701072n,
34+
// @log: s: 57228803202727131502949358313456071280488184270258293674242124340113824882788n,
35+
// @log: yParity: 0
36+
// @log: }
37+
```
38+
39+
## Returns
40+
41+
[`Signature`](/api/signature#signature-1)
42+
43+
Signature object.
44+
45+
## Parameters
46+
47+
### compactSignature
48+
49+
- **Type:** [`CompactSignature`](/api/signature#compactsignature)
50+
51+
Compact signature value.

site/pages/api/signature/index.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# Signature
2+
3+
The **Signature** Module provides a set of utility functions for working with ECDSA signatures.
4+
5+
## Functions
6+
7+
| Module | Description |
8+
| ----------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
9+
| [`Signature.from`](/api/signature/from) | Instantiates a typed [Signature](#signature) object from a [Signature](#signature), [CompactSignature](#compactsignature), [LegacySignature](#legacysignature), [Bytes](/api/bytes), or [Hex](/api/hex). |
10+
| [`Signature.fromCompact`](/api/signature/fromCompact) | Converts an [EIP-2098](https://eips.ethereum.org/EIPS/eip-2098) [Compact Signature](#compactsignature) into a [Signature](#signature). |
11+
| [`Signature.deserialize`](/api/signature/deserialize) | Deserializes a {@link Bytes} or {@link Hex} signature into a structured {@link Signature}. |
12+
| [`Signature.serialize`](/api/signature/serialize) | Serializes a [Signature](#signature) (or an [EIP-2098](https://eips.ethereum.org/EIPS/eip-2098) [CompactSignature](#compactsignature)) to [Bytes](/api/bytes) or [Hex](/api/hex). |
13+
| [`Signature.toCompact`](/api/signature/toCompact) | Converts a [Signature](#signature) into an [EIP-2098](https://eips.ethereum.org/EIPS/eip-2098) [CompactSignature](#compactsignature). |
14+
15+
## Types
16+
17+
### `Signature`
18+
19+
An ECDSA **Signature** can be represented via the `Signature` type. It is a JavaScript `object` with `r`, `s`, and `yParity` properties.
20+
21+
```ts twoslash
22+
// @noErrors
23+
import { Signature } from 'ox'
24+
25+
const Signature = Signature.from({
26+
r: 49782753348462494199823712700004552394425719014458918871452329774910450607807n,
27+
s: 33726695977844476214676913201140481102225469284307016937915595756355928419768n,
28+
yParity: 1,
29+
}) satisfies Signature.Signature
30+
// ^?
31+
32+
33+
34+
35+
36+
37+
38+
39+
```
40+
41+
### `CompactSignature`
42+
43+
Ox also has support for [EIP-2098](https://eips.ethereum.org/EIPS/eip-2098) Compact Signatures. A Compact Signature is represented via the `CompactSignature` type, and is a JavaScript `object` with `r` and `yParityAndS` properties.
44+
45+
```ts twoslash
46+
// @noErrors
47+
import { Signature } from 'ox'
48+
49+
const signature = {
50+
r: 49782753348462494199823712700004552394425719014458918871452329774910450607807n,
51+
yParityAndS: 33726695977844476214676913201140481102225469284307016937915595756355928419768n,
52+
} satisfies Signature.Compact
53+
// ^?
54+
55+
56+
57+
58+
59+
60+
61+
```
62+
63+
### `LegacySignature`
64+
65+
Ox supports ECDSA signatures that consist of a legacy `v` value. A Legacy Signature is represented via the `LegacySignature` type, and is a JavaScript `object` with `r`, `s`, and `v` properties.
66+
67+
```ts twoslash
68+
// @noErrors
69+
import { Signature } from 'ox'
70+
71+
const signature = {
72+
r: 49782753348462494199823712700004552394425719014458918871452329774910450607807n,
73+
s: 33726695977844476214676913201140481102225469284307016937915595756355928419768n,
74+
v: 27,
75+
} satisfies Signature.Legacy
76+
// ^?
77+
78+
79+
80+
81+
82+
83+
84+
85+
```

site/pages/api/signature/serialize.md

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
---
2+
description: Deserializes a Bytes or Hex signature into a structured Signature.
3+
---
4+
5+
# Signature.serialize
6+
7+
**Alias:** `serializeSignature`
8+
9+
Serializes a [Signature](/api/signature#signature-1) (or an [EIP-2098](https://eips.ethereum.org/EIPS/eip-2098) [CompactSignature](/api/signature#compactsignature)) to [Hex](/api/hex) or [Bytes](/api/bytes).
10+
11+
## Imports
12+
13+
```ts twoslash
14+
// @noErrors
15+
// Named Import
16+
import { Signature } from 'ox'
17+
18+
// Entrypoint Imports
19+
import * as Signature from 'ox/Signature'
20+
import { serializeSignature } from 'ox/Signature'
21+
```
22+
23+
## Usage
24+
25+
```ts twoslash
26+
// @noErrors
27+
import { Signature } from 'ox'
28+
29+
const signature = Signature.serialize({
30+
r: 49782753348462494199823712700004552394425719014458918871452329774910450607807n,
31+
s: 33726695977844476214676913201140481102225469284307016937915595756355928419768n,
32+
yParity: 1
33+
})
34+
// @log: '0x6e100a352ec6ad1b70802290e18aeed190704973570f3b8ed42cb9808e2ea6bf4a90a229a244495b41890987806fcbd2d5d23fc0dbe5f5256c2613c039d76db81c'
35+
36+
// Serialize into a compact signature.
37+
const signature = Signature.serialize({
38+
r: 47323457007453657207889730243826965761922296599680473886588287015755652701072n,
39+
s: 57228803202727131502949358313456071280488184270258293674242124340113824882788n,
40+
yParity: 0
41+
}, { compact: true })
42+
// @log: '0x68a020a209d3d56c46f38cc50a33f704f4a9a10a59377f8dd762ac66910e9b907e865ad05c4035ab5792787d4a0297a43617ae897930a6fe4d822b8faea52064'
43+
44+
// Serialize a Compact Signature.
45+
const signature = Signature.serialize({
46+
r: 47323457007453657207889730243826965761922296599680473886588287015755652701072n,
47+
yParityAndS: 57228803202727131502949358313456071280488184270258293674242124340113824882788n,
48+
})
49+
// @log: '0x68a020a209d3d56c46f38cc50a33f704f4a9a10a59377f8dd762ac66910e9b907e865ad05c4035ab5792787d4a0297a43617ae897930a6fe4d822b8faea52064'
50+
```
51+
52+
## Returns
53+
54+
`Hex | Bytes`
55+
56+
The serialized signature.
57+
58+
## Parameters
59+
60+
### signature
61+
62+
- **Type:** `Signature | LegacySignature | CompactSignature`
63+
64+
Signature to serialize.
65+
66+
```ts twoslash
67+
import { Signature } from 'ox'
68+
69+
const signature = Signature.from({
70+
r: 49782753348462494199823712700004552394425719014458918871452329774910450607807n,
71+
s: 33726695977844476214676913201140481102225469284307016937915595756355928419768n,
72+
yParity: 1
73+
})
74+
75+
const serialized = Signature.serialize(
76+
signature // [!code focus]
77+
)
78+
```
79+
80+
### options.compact
81+
82+
- **Type:** `boolean`
83+
84+
Whether or not to serialize the signature into a compact signature.
85+
86+
```ts twoslash
87+
import { Signature } from 'ox'
88+
89+
const signature = Signature.from({
90+
r: 49782753348462494199823712700004552394425719014458918871452329774910450607807n,
91+
s: 33726695977844476214676913201140481102225469284307016937915595756355928419768n,
92+
yParity: 1
93+
})
94+
95+
const serialized = Signature.serialize(
96+
signature,
97+
{ compact: true } // [!code focus]
98+
)
99+
// @log: '0x68a020a209d3d56c46f38cc50a33f704f4a9a10a59377f8dd762ac66910e9b907e865ad05c4035ab5792787d4a0297a43617ae897930a6fe4d822b8faea52064'
100+
```
101+
102+
### options.to
103+
104+
- **Type:** `'bytes' | 'hex'`
105+
106+
Format to serialize the signature into.
107+
108+
```ts twoslash
109+
import { Signature } from 'ox'
110+
111+
const signature = Signature.from({
112+
r: 49782753348462494199823712700004552394425719014458918871452329774910450607807n,
113+
s: 33726695977844476214676913201140481102225469284307016937915595756355928419768n,
114+
yParity: 1
115+
})
116+
117+
const serialized = Signature.serialize(
118+
signature,
119+
{ to: 'bytes' } // [!code focus]
120+
)
121+
// @log: Uint8Array(65) [ ... ]
122+
```

0 commit comments

Comments
 (0)