@pharosnames/address-encoder
Version:
Encodes and decodes address formats for various cryptocurrencies with Pharos network support
101 lines (70 loc) • 2.96 kB
Markdown
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 .phrs domains to resolve to cryptocurrency addresses across multiple blockchain networks.
```bash
npm install @pharosnames/address-encoder
```
```ts
import { getCoderByCoinName } from "@pharosnames/address-encoder";
// Get the coder for the address you want to encode/decode
const coder = getCoderByCoinName("phrs");
// 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.
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.
```ts
import { getCoderByCoinNameAsync } from "@pharosnames/address-encoder/async";
const phrsCoder = await getCoderByCoinNameAsync("phrs");
```
```ts
import { phrs } from "@pharosnames/address-encoder/coins";
// Use phrs.decode() and phrs.encode() methods
```
```ts
import {
decodePHRSAddress,
encodePHRSAddress,
} from "@pharosnames/address-encoder/coders";
```
```ts
import { http } from "viem";
import { createEnsPublicClient } from "@pharosnames/pnsjs";
import { pharosTestnetChain } from "@pharosnames/pnsjs/contracts";
import { evmCoinNameToTypeMap } from "@pharosnames/address-encoder";
const PHAROS_COIN_TYPE = evmCoinNameToTypeMap["phrs"];
// continue to use createEnsPublicClient for better compatibility with ensjs
const client = createEnsPublicClient({
chain: pharosTestnetChain,
transport: http(),
});
// Get a Pharos address for a .phrs domain
const address = await client.getAddressRecord({
name: "example.phrs",
coin: PHAROS_COIN_TYPE,
});
// Retrieve the address
const retrievedAddress = await client.getAddressRecord({
name: "example.phrs",
coinType: 2148172336,
});
const decodedAddress = phrsCoder.encode(retrievedAddress);
console.log(decodedAddress); // 0x1234567890123456789012345678901234567890
```
MIT License - see the LICENSE file for details.