Compare Substrate account IDs across SS58 display formats
Two address strings can identify the same 32-byte account under different SS58 prefixes. A duplicate detector that compares strings alone can create two contacts for one account.
Decode before comparing
The example takes an SS58 inputAddress and re-encodes its bytes with generic prefix 42. Compare decoded account bytes for equality. Let the decoder validate the input checksum and report malformed addresses separately.
import { decodeAddress, encodeAddress } from '@polkadot/util-crypto'
import { u8aEq } from '@polkadot/util'
const id = decodeAddress(inputAddress)
const reformatted = encodeAddress(id, 42)
console.log(reformatted, u8aEq(id, decodeAddress(reformatted)))
Preserve network context in balances and transfers
Changing a display prefix does not move funds or look up an account on another chain. Two chains may record independent balances for the same identifier. Store the chain identity alongside the account bytes for ledger records, and select the network’s expected display format when presenting a destination.
This comparison assumes the chain uses the expected account-ID representation. Inspect the runtime’s account type before extending it to chains using different address lengths.