---
title: "Working with Addresses"
description: "Turn a public key into an address. Pick a format. Check one you were given."
canonical_url: "https://keys.agntn.dev/guide/addresses"
---
# Working with Addresses

> Turn a public key into an address. Pick a format. Check one you were given.

## From public key to address

```js
import { useBlockchain } from "@agntn/keys";
import Bitcoin from "@agntn/keys/blockchains/bitcoin";

const chain = useBlockchain(new Bitcoin());
const keys = chain.generateKeys();
const address = chain.getAddress(keys.keys.public);

console.log(address); // '1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa'
```

Every chain does the same thing here: hash the public key, then encode the hash the way the chain likes. What differs is the hash, the encoding, and whether there's a checksum.

## Address types

The second argument picks a format on chains that have more than one.

```js
chain.getAddress(publicKey); // legacy, starts with 1
chain.getAddress(publicKey, "segwit"); // bc1q...
chain.getAddress(publicKey, "taproot"); // bc1p...
```

Bitcoin and Litecoin have five formats, Bitcoin Gold four, Cardano three. Sui and the XRP Ledger use it to name the curve. Everything else has one format and throws if you ask for another. Leave the argument out.

## Validating

```js
chain.validateAddress("1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa"); // true
chain.validateAddress("1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNb"); // false, checksum
```

Validation checks the format and the checksum when the format has one. It doesn't touch the network. A valid address with no history is still valid.

<note>

`validateAddress` is optional on the `BlockchainImplementation` type so that structural implementations can skip it, but every class built on `AbstractBlockchain` has one. Every built in chain does.

</note>

## How each chain builds its address

<table>
<thead>
  <tr>
    <th>
      Chain
    </th>
    
    <th>
      Recipe
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      Bitcoin
    </td>
    
    <td>
      <code>
        RIPEMD160(SHA256(pubkey))
      </code>
      
      , then base58check for legacy and p2sh, bech32 for segwit, bech32m with a BIP341 tweak for taproot
    </td>
  </tr>
  
  <tr>
    <td>
      Bitcoin Cash
    </td>
    
    <td>
      Bitcoin's P2PKH hash behind a version byte, then CashAddr with the <code>
        bitcoincash
      </code>
      
       prefix and a 40-bit checksum
    </td>
  </tr>
  
  <tr>
    <td>
      Bitcoin Gold
    </td>
    
    <td>
      the same hash and encodings, version <code>
        0x26
      </code>
      
       for legacy (<code>
        G
      </code>
      
      ), <code>
        0x17
      </code>
      
       for p2sh (<code>
        A
      </code>
      
      ), bech32 with <code>
        btg
      </code>
      
      , no taproot
    </td>
  </tr>
  
  <tr>
    <td>
      Bitcoin SV
    </td>
    
    <td>
      Bitcoin's legacy recipe, version <code>
        0x00
      </code>
      
      , base58check, so it starts with <code>
        1
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      Litecoin
    </td>
    
    <td>
      the same hash and encodings, version <code>
        0x30
      </code>
      
       for legacy (<code>
        L
      </code>
      
      ), <code>
        0x32
      </code>
      
       for p2sh (<code>
        M
      </code>
      
      ), bech32 with <code>
        ltc
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      Dash
    </td>
    
    <td>
      Bitcoin's legacy recipe, version <code>
        0x4c
      </code>
      
      , base58check, so it starts with <code>
        X
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      Decred
    </td>
    
    <td>
      <code>
        RIPEMD160(BLAKE256(pubkey))
      </code>
      
       behind the two byte prefix <code>
        07 3f
      </code>
      
      , base58check with a double BLAKE-256 checksum, so it starts with <code>
        Ds
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      Dogecoin
    </td>
    
    <td>
      Bitcoin's legacy recipe, version <code>
        0x1e
      </code>
      
      , base58check, so it starts with <code>
        D
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      Zcash
    </td>
    
    <td>
      Bitcoin's legacy recipe behind the two byte version <code>
        1c b8
      </code>
      
      , base58check, so it starts with <code>
        t1
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      eCash
    </td>
    
    <td>
      Bitcoin Cash's recipe under the <code>
        ecash
      </code>
      
       prefix, so the same key hash starts with <code>
        ecash:q
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      Ethereum, Base
    </td>
    
    <td>
      last 20 bytes of <code>
        Keccak-256(uncompressed pubkey without 04)
      </code>
      
      , <code>
        0x
      </code>
      
       prefix, EIP-55 mixed case checksum
    </td>
  </tr>
  
  <tr>
    <td>
      TRON
    </td>
    
    <td>
      same 20 bytes as Ethereum, version byte <code>
        0x41
      </code>
      
      , base58check, so it starts with <code>
        T
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      Solana
    </td>
    
    <td>
      the ed25519 public key itself, base58
    </td>
  </tr>
  
  <tr>
    <td>
      Stellar
    </td>
    
    <td>
      version byte <code>
        0x30
      </code>
      
      , the ed25519 public key, a CRC16-XModem checksum, all in base32, so it starts with <code>
        G
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      Aptos
    </td>
    
    <td>
      <code>
        SHA3-256(pubkey + 0x00)
      </code>
      
      , <code>
        0x
      </code>
      
       prefix
    </td>
  </tr>
  
  <tr>
    <td>
      Sui
    </td>
    
    <td>
      <code>
        Blake2b-256(flag + pubkey)
      </code>
      
      , flag <code>
        0x00
      </code>
      
       for ed25519 and <code>
        0x01
      </code>
      
       for secp256k1, <code>
        0x
      </code>
      
       prefix
    </td>
  </tr>
  
  <tr>
    <td>
      Cardano
    </td>
    
    <td>
      <code>
        Blake2b-224(pubkey)
      </code>
      
       behind a header byte, bech32 with <code>
        addr
      </code>
      
       or <code>
        stake
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      XRP Ledger
    </td>
    
    <td>
      Bitcoin's <code>
        RIPEMD160(SHA256(pubkey))
      </code>
      
       behind version <code>
        0x00
      </code>
      
      , base58check in XRPL's alphabet, so it starts with <code>
        r
      </code>
      
      ; an ed25519 key gets hashed with its <code>
        ED
      </code>
      
       byte
    </td>
  </tr>
  
  <tr>
    <td>
      NEAR
    </td>
    
    <td>
      the ed25519 public key itself, lowercase hex, no prefix; named accounts like <code>
        alice.near
      </code>
      
       come back invalid
    </td>
  </tr>
  
  <tr>
    <td>
      Cosmos
    </td>
    
    <td>
      Bitcoin's <code>
        RIPEMD160(SHA256(compressed pubkey))
      </code>
      
      , bech32 under the chain's prefix, <code>
        cosmos1...
      </code>
      
       on the Hub and <code>
        osmo1...
      </code>
      
       on Osmosis
    </td>
  </tr>
  
  <tr>
    <td>
      Polkadot
    </td>
    
    <td>
      the sr25519 or ed25519 public key itself behind the network prefix, two bytes of blake2b-512 as checksum, base58; <code>
        1...
      </code>
      
       on Polkadot, a capital letter on Kusama
    </td>
  </tr>
  
  <tr>
    <td>
      Monero
    </td>
    
    <td>
      a network byte, the public spend key, the public view key and four bytes of <code>
        Keccak-256
      </code>
      
      , block base58, 95 characters or 106 with a payment ID; <code>
        4...
      </code>
      
       on mainnet, <code>
        8...
      </code>
      
       for a subaddress
    </td>
  </tr>
  
  <tr>
    <td>
      TON
    </td>
    
    <td>
      <code>
        SHA256
      </code>
      
       of the wallet contract's state init, so the code and the key; a flag byte, workchain, that hash and CRC16-XModem in base64url, <code>
        UQ...
      </code>
      
       as wallets show it, <code>
        EQ...
      </code>
      
       bounceable
    </td>
  </tr>
</tbody>
</table>

The chain pages have the code for each, plus the testnet variants where they exist.

## A quick tour

```js
import Ethereum from "@agntn/keys/blockchains/ethereum";
import Solana from "@agntn/keys/blockchains/solana";
import Sui from "@agntn/keys/blockchains/sui";

const ethereumChain = useBlockchain(new Ethereum());
ethereumChain.getAddress(ethereumChain.getKeyPublic(privateKey)); // '0x...' with checksum

const solanaChain = useBlockchain(new Solana());
solanaChain.getAddress(solanaChain.getKeyPublic(privateKey)); // base58, no hashing at all

const suiChain = useBlockchain(new Sui());
const secpKey = suiChain.getKeyPublic(privateKey, { scheme: "secp256k1" });
suiChain.getAddress(secpKey, "secp256k1"); // '0x...' from the secp256k1 flag
```

The Sui line is the one people get wrong: a secp256k1 public key hashed with the ed25519 flag produces a perfectly valid looking address that no key can spend from. Keep the scheme on both calls.


## Sitemap

See the full [sitemap](https://keys.agntn.dev/sitemap.md) for all pages.
