@parity/product-sdk-address
Address handling for accounts that live on both Substrate (SS58) and EVM (H160) chains.
Convert between formats, validate, normalize SS58 prefixes, and shorten addresses for display.
npm install @parity/product-sdk-addressExports
Functions
| Name | Summary |
|---|---|
accountIdBytes() | Decode an SS58 address to its 32-byte AccountId using polkadot-api’s AccountId codec. |
accountIdFromBytes() | Encode an SS58 address from a 32-byte public key using polkadot-api’s AccountId codec. |
addressesEqual() | Do two addresses name the same account? |
deriveH160() | Derive the H160 EVM address from a 32-byte Substrate public key. |
h160ToSs58() | Convert an H160 EVM address to its corresponding SS58 address. |
isValidH160() | Validate whether a string is a valid H160 (20-byte hex) address. |
isValidSs58() | Validate whether a string is a valid SS58 address. |
normalizeSs58() | Re-encode an SS58 address with a different network prefix. |
publicKeysEqual() | Do two public keys match? The loop half of addressesEqual: decode the |
ss58Decode() | Decode an SS58 address into its raw public key bytes and network prefix. |
ss58Encode() | Encode raw public key bytes into an SS58 address with the given prefix. |
ss58ToH160() | Convert an SS58 address to its H160 EVM address. |
toGenericSs58() | Convert any SS58 address to generic Substrate format (prefix 42). |
toH160() | Convert any address (SS58 or H160) to an H160 EVM address. |
toPolkadotSs58() | Convert any SS58 address to Polkadot format (prefix 0). |
truncateAddress() | Truncate an address for display. |
Type Aliases
| Name | Summary |
|---|---|
HexString | A 0x-prefixed hex string (e.g. 0xdeadbeef). The SDK uses it for raw byte |
SS58String | An SS58-encoded Substrate address (e.g. 5GrwvaEF...). The brand marks strings |
Functions
accountIdBytes()
Decode an SS58 address to its 32-byte AccountId using polkadot-api’s AccountId codec.
This is the inverse of accountIdFromBytes().
accountIdBytes(address: string): Uint8ArrayaccountIdFromBytes()
Encode an SS58 address from a 32-byte public key using polkadot-api’s AccountId codec.
This is the inverse of accountIdBytes().
accountIdFromBytes(publicKey: Uint8Array, prefix: number = GENERIC_PREFIX): SS58StringaddressesEqual()
Do two addresses name the same account?
On the account, not on the encoding. Two SS58 strings for one key are equal
even at different network prefixes, so this cannot tell you which network an
address is written for; read the prefix from ss58Decode for that. An
SS58 and an H160 are never equal, whatever they encode.
A non-matching pair costs a decode, roughly 20 microseconds, where an exact
match costs nothing. In a loop, use publicKeysEqual instead.
addressesEqual(a: string, b: string): booleanderiveH160()
Derive the H160 EVM address from a 32-byte Substrate public key.
Asset Hub pallet-revive uses these derivation rules:
- If the account was originally EVM-derived (last 12 bytes are all 0xEE): strip the padding to recover the original H160 address.
- If native Substrate account (sr25519/ed25519): keccak256(publicKey), take last 20 bytes. One-way mapping.
deriveH160(publicKey: Uint8Array): `0x${string}`h160ToSs58()
Convert an H160 EVM address to its corresponding SS58 address.
Constructs an “EVM-derived” AccountId32 by padding the H160 with 0xEE bytes. These accounts are implicitly mapped in pallet-revive.
h160ToSs58(evmAddress: string, prefix: number = 42): SS58StringisValidH160()
Validate whether a string is a valid H160 (20-byte hex) address.
isValidH160(address: string): booleanisValidSs58()
Validate whether a string is a valid SS58 address.
isValidSs58(address: string): booleannormalizeSs58()
Re-encode an SS58 address with a different network prefix. Returns null if the input is not a valid SS58 address.
normalizeSs58(address: string, prefix: number = GENERIC_PREFIX): SS58String | nullpublicKeysEqual()
Do two public keys match? The loop half of addressesEqual: decode the
needle once with ss58Decode, then compare it against each candidate.
A byte comparison, so both inputs must already have decoded successfully. Two empty arrays compare equal. Unvalidated on purpose, since this runs per candidate.
publicKeysEqual(a: Uint8Array, b: Uint8Array): booleanss58Decode()
Decode an SS58 address into its raw public key bytes and network prefix.
ss58Decode(address: string): { prefix: number; publicKey: Uint8Array }ss58Encode()
Encode raw public key bytes into an SS58 address with the given prefix. Defaults to prefix 42 (generic Substrate).
ss58Encode(publicKey: Uint8Array, prefix: number = GENERIC_PREFIX): SS58Stringss58ToH160()
Convert an SS58 address to its H160 EVM address.
Handles both native Substrate accounts (keccak256 path) and EVM-derived accounts (0xEE padding strip).
ss58ToH160(address: string): `0x${string}`toGenericSs58()
Convert any SS58 address to generic Substrate format (prefix 42). Returns null if the input is invalid.
toGenericSs58(address: string): SS58String | nulltoH160()
Convert any address (SS58 or H160) to an H160 EVM address. If already H160 format (0x-prefixed, 42 chars), returns as-is preserving original casing.
toH160(address: string): `0x${string}`toPolkadotSs58()
Convert any SS58 address to Polkadot format (prefix 0). Returns null if the input is invalid.
toPolkadotSs58(address: string): SS58String | nulltruncateAddress()
Truncate an address for display.
truncateAddress(address: string, startChars: number = 6, endChars: number = 4): stringParameters
address: Full address (SS58 or H160)startChars: Characters to show at the start (default 6)endChars: Characters to show at the end (default 4)
Returns
Truncated string like “5Grwva…utQY”
Type Aliases
type HexString
A 0x-prefixed hex string (e.g. 0xdeadbeef). The SDK uses it for raw byte
values like public keys and EVM addresses.
type HexString = string & { __hexString?: unknown }type SS58String
An SS58-encoded Substrate address (e.g. 5GrwvaEF...). The brand marks strings
the SDK has validated, so APIs accepting SS58String can skip re-checking.
Construct one via normalizeSs58 or ss58Encode.
type SS58String = string & { __SS58String?: unknown }