> ## Documentation Index
> Fetch the complete documentation index at: https://ckbfans.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Address

> Parse and construct CKB addresses, which encode a lock script and network prefix.

A CKB address is a bech32m-encoded string that packages two things:

1. **A lock script** — the on-chain predicate that controls who can spend the cells at this address (code hash, hash type, and args).
2. **An address prefix** — `ckb` for mainnet, `ckt` for testnet.

Because the lock script is embedded in the address, you can always recover the exact script needed to send funds to an address, even without any additional metadata.

## Parse an address string

Use `Address.fromString` to decode an address into a structured object:

```ts theme={null}
import { ccc } from "@ckb-ccc/ccc";

const client = new ccc.ClientPublicMainnet();
const address = await ccc.Address.fromString(
  "ckb1qzda0cr08m85hc8jlnfp3sdrlalyatuqvqdveld...",
  client,
);

console.log(address.script.codeHash); // lock script code hash
console.log(address.script.hashType); // "type" or "data"
console.log(address.script.args);     // lock script args
console.log(address.prefix);          // "ckb"
```

`fromString` validates that the address prefix matches the client's network. It throws if there is a mismatch (e.g. passing a `ckt` address to a mainnet client).

You can also pass a record of clients when you need to handle both networks in the same code path:

```ts theme={null}
const address = await ccc.Address.fromString(someAddress, {
  ckb: mainnetClient,
  ckt: testnetClient,
});
```

## Construct an address from a script

Build an `Address` directly from a known lock script and a client:

```ts theme={null}
const address = ccc.Address.fromScript(lockScript, client);
console.log(address.toString()); // "ckb1q..."
```

Use `fromKnownScript` to derive an address from one of the built-in `KnownScript` entries:

```ts theme={null}
const address = await ccc.Address.fromKnownScript(
  client,
  ccc.KnownScript.Secp256k1Blake160,
  "0xpublicKeyHash...",
);
```

## Get an address from a signer

The most common way to obtain an address in a dApp is to ask the connected signer:

```ts theme={null}
// Primary address as a string
const addressStr = await signer.getRecommendedAddress();

// Primary address as an Address object (includes the lock script)
const addressObj = await signer.getRecommendedAddressObj();
const { script: lock } = addressObj;

// All addresses controlled by this signer
const allAddresses = await signer.getAddresses(); // string[]
```

<Tip>
  Use `getRecommendedAddressObj` when you need the lock script — for example, to set it as the recipient of a transaction output — instead of parsing the address string again with `Address.fromString`.
</Tip>

## Convert an address back to a string

Call `toString()` on any `Address` instance to produce the bech32m string:

```ts theme={null}
const str = address.toString(); // "ckb1q..." or "ckt1q..."
```

## Address prefix and network

The prefix tells you which network an address belongs to:

| Prefix | Network |
| ------ | ------- |
| `ckb`  | Mainnet |
| `ckt`  | Testnet |

The `Client` exposes `addressPrefix` so you can read it at runtime:

```ts theme={null}
const client = new ccc.ClientPublicTestnet();
console.log(client.addressPrefix); // "ckt"
```

<Warning>
  Never mix mainnet and testnet addresses. A `ckt` address decoded against a mainnet client will throw an error. Always pair your `Address.fromString` call with the correct client instance.
</Warning>
