# @pharosnames/address-encoder

A cryptocurrency address encoder/decoder in TypeScript for the Pharos Name Service (PNS).

Text-format addresses are decoded into their native binary representations, and vice-versa. In the case of Bitcoin-derived chains, this means their scriptPubKey; for Ethereum-derived chains this is their hash.

This library was written for use with the Pharos Name Service, enabling `.pros` domains to resolve to cryptocurrency addresses across multiple blockchain networks.

## Installation

```bash
npm install @pharosnames/address-encoder
```

## Getting Started

```ts
import { getCoderByCoinName } from "@pharosnames/address-encoder";

// Get the coder for the address you want to encode/decode
const coder = getCoderByCoinName("pros");

// Decode the address
const decodedAddress = coder.decode(
  "0x1234567890123456789012345678901234567890"
);
// Uint8Array(25) [ 118, 169, 20, 98, 233, 7, 177, 92, 191, 39, 213, 66, 83, 153, 235, 246, 240, 251, 80, 235, 184, 143, 24, 136, 172 ]

// Encode the address
const encodedAddress = coder.encode(decodedAddress);
// 0x1234567890123456789012345678901234567890
```

**Note**: If your data is a hex string, use `hexToBytes()` from `@pharosnames/address-encoder/utils` to convert it to `Uint8Array` before encoding.

## Supported Cryptocurrencies

The library supports 100+ cryptocurrencies including Bitcoin, Litecoin, Dogecoin, Monero, and many others. For a complete list, check the supported coins in the source code.

## Alternative Import Methods

### Async Coder Getter

```ts
import { getCoderByCoinNameAsync } from "@pharosnames/address-encoder/async";

const prosCoder = await getCoderByCoinNameAsync("pros");
```

## Using with Pharos Name Service

```ts
import { http } from "viem";
import { createEnsPublicClient } from "@pharosnames/pnsjs";
import { pharosMainnetChain } from "@pharosnames/pnsjs/contracts";
import {
  evmCoinNameToTypeMap,
  getCoderByCoinName,
} from "@pharosnames/address-encoder";

const PHAROS_COIN_TYPE = evmCoinNameToTypeMap["pros"];

// continue to use createEnsPublicClient for better compatibility with ensjs
const client = createEnsPublicClient({
  chain: pharosMainnetChain,
  transport: http(),
});

// Get a Pharos address for a .pros domain
const address = await client.getAddressRecord({
  name: "example.pros",
  coin: PHAROS_COIN_TYPE,
});

// Retrieve the address
const retrievedAddress = await client.getAddressRecord({
  name: "example.pros",
  coinType: 2147485320,
});

const prosCoder = getCoderByCoinName("pros");
const decodedAddress = prosCoder.encode(retrievedAddress);
console.log(decodedAddress); // 0x1234567890123456789012345678901234567890
```

## License

MIT License - see the LICENSE file for details.
