Skip to content

Commit

Permalink
feat: address
Browse files Browse the repository at this point in the history
  • Loading branch information
jxom committed Aug 18, 2024
1 parent 5caded5 commit f2bd2c7
Show file tree
Hide file tree
Showing 36 changed files with 1,007 additions and 20 deletions.
80 changes: 80 additions & 0 deletions site/pages/api/address/assert.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
---
description: Asserts that the given value is a valid address
---

# Address.assert

**Alias:** `assertAddress`

Asserts that the given value is a valid address.

## Imports

```ts twoslash
// @noErrors
// Named Module Import
import { Address } from 'ox'

// Module Imports
import * as Address from 'ox/Address'
import { assertAddress } from 'ox/Address'
```

## Usage

```ts twoslash
// @noErrors
import { Address } from 'ox';

Address.assert('0xa0cf798816d4b9b9866b5330eea46a18382f251e')

Address.assert('0xa5cc3c03994db5b0d9a5eEdD10Cabab0813678ac')
// Address "0xa5cc3c03994db5b0d9a5eEdD10Cabab0813678ac" is invalid.
//
// Details: Address does not match its checksum counterpart.

Address.assert('0xa')
// Address "0xa" is invalid.
//
// Details: Address is not a 20 byte hexadecimal value.
```

## Returns

`void`

## Parameters

### value

- **Type:** `string`

Value to assert if it is a valid address.

```ts twoslash
import { Address } from 'ox';

Address.assert(
'0xa0cf798816d4b9b9866b5330eea46a18382f251e' // [!code focus]
);
```

## Parameters

### options

#### options.strict

- **Type:** `boolean`
- **Default:** `true`

Enables strict mode. Whether or not to compare the address against its checksum.

```ts twoslash
import { Address } from 'ox';

Address.assert(
'0xa0cf798816d4b9b9866b5330eea46a18382f251e',
{ strict: false } // [!code focus]
);
```
31 changes: 31 additions & 0 deletions site/pages/api/address/checksum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
description: Computes the checksum address for the given address
---

# Address.checksum

**Alias:** `checksumAddress`

Computes the checksum address for the given address.

## Imports

```ts twoslash
// @noErrors
// Named Module Import
import { Address } from 'ox'

// Module Imports
import * as Address from 'ox/Address'
import { checksumAddress } from 'ox/Address'
```

## Usage

```ts twoslash
// @noErrors
import { Address } from 'ox';

Address.checksum('0xa0cf798816d4b9b9866b5330eea46a18382f251e')
// '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e'
```
79 changes: 79 additions & 0 deletions site/pages/api/address/from.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
---
description: Converts an address string to a typed Address
---

# Address.from

**Alias:** `toAddress`

Converts an address string to a typed (checksummed) Address.

## Imports

```ts twoslash
// @noErrors
// Named Module Import
import { Address } from 'ox'

// Module Imports
import * as Address from 'ox/Address'
import { toAddress } from 'ox/Address'
```

## Usage

```ts twoslash
// @noErrors
import { Address } from 'ox';

const address = Address.from('0xa0cf798816d4b9b9866b5330eea46a18382f251e')
// '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e'

const address = Address.from(
'0xa0cf798816d4b9b9866b5330eea46a18382f251e',
{ checksum: false }
)
// '0xa0cf798816d4b9b9866b5330eea46a18382f251e'

const address = Address.from('hello')
// InvalidAddressError: Address "0xa" is invalid.
```

## Return Type

`Address`

The checksummed Address.

## Parameters

### address

- **Type:** `string`

An address string to convert to a typed Address.

```ts twoslash
import { Address } from 'ox';

const address = Address.from(
'0xa0cf798816d4b9b9866b5330eea46a18382f251e' // [!code focus]
);
```

### options

#### options.checksum

- **Type:** `boolean`

Whether to checksum the address.

```ts twoslash
import { Address } from 'ox';

const address = Address.from(
'0xa0cf798816d4b9b9866b5330eea46a18382f251e',
{ checksum: false } // [!code focus]
);
```
18 changes: 17 additions & 1 deletion site/pages/api/address/index.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
# Address

TODO
The **Address** Module provides a set of utility functions for working with Ethereum addresses.

## Type

**Address** can be represented via the `Address` type. It is a JavaScript `string` primitive with a `"0x"` prefix.

```ts twoslash
// @noErrors
import { Address } from 'ox'

const address = Address.from('0xa0cf798816d4b9b9866b5330eea46a18382f251e')
satisfies Address.Address
// ^?



```
72 changes: 72 additions & 0 deletions site/pages/api/address/isAddress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
---
description: Checks if the given address is a valid address
---

# Address.isAddress

Checks if the given address is a valid address.

## Imports

```ts twoslash
// @noErrors
// Named Module Import
import { Address } from 'ox'

// Module Imports
import * as Address from 'ox/Address'
import { isAddress } from 'ox/Address'
```

## Usage

```ts twoslash
// @noErrors
import { Address } from 'ox';

const isAddress = Address.isAddress('0xA0Cf798816D4b9b9866b5330EEa46a18382f251e')
// true

const isAddress = Address.isAddress('0xdeadbeef')
// false
```

## Return Type

`boolean`

Whether the address is a valid address.

## Parameters

### value

- **Type:** `string`

Value to check if it is a valid address.

```ts twoslash
import { Address } from 'ox';

const isAddress = Address.isAddress(
'0xa0cf798816d4b9b9866b5330eea46a18382f251e' // [!code focus]
);
```

### options

#### options.strict

- **Type:** `boolean`
- **Default:** `true`

Enables strict mode. Whether or not to compare the address against its checksum.

```ts twoslash
import { Address } from 'ox';

const address = Address.isAddress(
'0xa0cf798816d4b9b9866b5330eea46a18382f251e',
{ strict: false } // [!code focus]
);
```
46 changes: 46 additions & 0 deletions site/pages/api/address/isEqual.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
description: Checks if two addresses are equal
---

# Address.isEqual

**Alias:** `isAddressEqual`

Checks if two addresses are equal.

## Imports

```ts twoslash
// @noErrors
// Named Module Import
import { Address } from 'ox'

// Module Imports
import * as Address from 'ox/Address'
import { isAddressEqual } from 'ox/Address'
```

## Usage

```ts twoslash
// @noErrors
import { Address } from 'ox';

const isEqual = Address.isEqual(
'0xa0cf798816d4b9b9866b5330eea46a18382f251e',
'0xA0Cf798816D4b9b9866b5330EEa46a18382f251e'
)
// true

const isEqual = Address.isEqual(
'0xa0cf798816d4b9b9866b5330eea46a18382f251e',
'0xA0Cf798816D4b9b9866b5330EEa46a18382f251f'
)
// false
```

## Return Type

`boolean`

Whether the addresses are equal.
Empty file.
20 changes: 20 additions & 0 deletions site/pages/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,26 @@ On the other hand, an 8-bit **_signed_** integer can range from `-2 ** (8 - 1)`

Pass a number within the valid signed or unsigned integer range.

## `InvalidAddressError`

### Why?

The provided value is not a valid address. Either the value is not a 20 byte (40 character) hexadecimal value, or the value does not match its checksum counterpart.

### Example

An example of this error may occur when trying to convert an invalid address string to a typed `Address` value (ie. `Address.from('0xdeadbeef')`).

```
Address "0xdeadbeef" is invalid.
Details: Address is not a 20 byte (40 hexadecimal character) value.
```

### Solution

Ensure that the value is a 40 character hexadecimal address, and that it matches its checksum counterpart.

## `InvalidBytesBooleanError`

### Why?
Expand Down
Loading

0 comments on commit f2bd2c7

Please sign in to comment.