|
1 | | -import * as Address from '../core/Address.js' |
| 1 | +import * as core_Address from '../core/Address.js' |
2 | 2 | import * as Bech32m from '../core/Bech32m.js' |
3 | 3 | import * as Bytes from '../core/Bytes.js' |
4 | 4 | import * as CompactSize from '../core/CompactSize.js' |
5 | 5 | import * as Errors from '../core/Errors.js' |
6 | 6 | import * as Hex from '../core/Hex.js' |
| 7 | +import type { Compute } from '../core/internal/types.js' |
| 8 | + |
| 9 | +/** An address that can be either an Ethereum hex address or a Tempo bech32m address. */ |
| 10 | +export type Address = core_Address.Address | Tempo |
7 | 11 |
|
8 | 12 | /** Root type for a Tempo Address. */ |
9 | | -export type TempoAddress = `tempo1${string}` | `tempoz1${string}` |
| 13 | +export type Tempo = Compute<`tempo1${string}` | `tempoz1${string}`> |
| 14 | + |
| 15 | +/** |
| 16 | + * Resolves an address input (either an Ethereum hex address or a Tempo bech32m address) |
| 17 | + * to an Ethereum hex address. |
| 18 | + * |
| 19 | + * @example |
| 20 | + * ```ts twoslash |
| 21 | + * import { TempoAddress } from 'ox/tempo' |
| 22 | + * |
| 23 | + * const address = TempoAddress.resolve('tempo1qp6z6dwvvc6vq5efyk3ms39une6etu4a9qtj2kk0') |
| 24 | + * // @log: '0x742d35CC6634c0532925a3B844bc9e7595F2Bd28' |
| 25 | + * ``` |
| 26 | + * |
| 27 | + * @example |
| 28 | + * ### Hex Address Passthrough |
| 29 | + * ```ts twoslash |
| 30 | + * import { TempoAddress } from 'ox/tempo' |
| 31 | + * |
| 32 | + * const address = TempoAddress.resolve('0x742d35Cc6634C0532925a3b844Bc9e7595f2bD28') |
| 33 | + * // @log: '0x742d35CC6634c0532925a3B844bc9e7595F2Bd28' |
| 34 | + * ``` |
| 35 | + * |
| 36 | + * @param address - An Ethereum hex address or Tempo bech32m address. |
| 37 | + * @returns The resolved Ethereum hex address. |
| 38 | + */ |
| 39 | +export function resolve(address: Address): core_Address.Address { |
| 40 | + if (address.startsWith('tempo')) return parse(address).address |
| 41 | + return address as core_Address.Address |
| 42 | +} |
10 | 43 |
|
11 | 44 | /** |
12 | 45 | * Formats a raw Ethereum address (and optional zone ID) into a Tempo address string. |
@@ -35,18 +68,16 @@ export type TempoAddress = `tempo1${string}` | `tempoz1${string}` |
35 | 68 | * @param options - Options. |
36 | 69 | * @returns The encoded Tempo address string. |
37 | 70 | */ |
38 | | -export function format( |
39 | | - address: Address.Address, |
40 | | - options: format.Options = {}, |
41 | | -): TempoAddress { |
| 71 | +export function format(address: Address, options: format.Options = {}): Tempo { |
42 | 72 | const { zoneId } = options |
43 | 73 |
|
| 74 | + const resolved = resolve(address) |
44 | 75 | const hrp = zoneId != null ? 'tempoz' : 'tempo' |
45 | 76 | const version = new Uint8Array([0x00]) |
46 | 77 | const zone = zoneId != null ? CompactSize.toBytes(zoneId) : new Uint8Array() |
47 | | - const data = Bytes.concat(version, zone, Bytes.fromHex(address)) |
| 78 | + const data = Bytes.concat(version, zone, Bytes.fromHex(resolved)) |
48 | 79 |
|
49 | | - return Bech32m.encode(hrp, data) as TempoAddress |
| 80 | + return Bech32m.encode(hrp, data) as Tempo |
50 | 81 | } |
51 | 82 |
|
52 | 83 | export declare namespace format { |
@@ -126,15 +157,15 @@ export function parse(tempoAddress: string): parse.ReturnType { |
126 | 157 | actual: rawAddress.length, |
127 | 158 | }) |
128 | 159 |
|
129 | | - const address = Address.checksum(Hex.fromBytes(rawAddress) as Address.Address) |
| 160 | + const address = core_Address.checksum(Hex.fromBytes(rawAddress) as never) |
130 | 161 |
|
131 | 162 | return { address, zoneId } |
132 | 163 | } |
133 | 164 |
|
134 | 165 | export declare namespace parse { |
135 | 166 | type ReturnType = { |
136 | 167 | /** The raw 20-byte Ethereum address. */ |
137 | | - address: Address.Address |
| 168 | + address: core_Address.Address |
138 | 169 | /** The zone ID, or `undefined` for mainnet addresses. */ |
139 | 170 | zoneId: number | bigint | undefined |
140 | 171 | } |
|
0 commit comments