Documentation / @cashconnect-js/core / primitives/address
AddressType
type AddressType = "P2PK" | "P2PKH" | "P2SH20" | "P2SH32";Defined in: cashconnect-js/packages/core/src/primitives/address.ts:19
AddressNetworkPrefix
type AddressNetworkPrefix = "bitcoincash" | "bchtest" | "bchreg" | string & object;Defined in: cashconnect-js/packages/core/src/primitives/address.ts:21
Address
Defined in: cashconnect-js/packages/core/src/primitives/address.ts:57
Address Entity.
Example
// Import our primitives.
import { Address } from '@xocash/primitives';
// Instantiate from a CashAddr (Legacy Addresses also supported).
const address = Address.from('bitcoincash:qpmlpygrtzznznhgsl4c245xgmglpfh0vgcqns0q0r');
// Convert to Locking Bytecode.
console.log(address.toLockscriptBytes());
// Convert to Legacy Address.
console.log(address.toLegacy());
// Convert to Token Address.
console.log(address.toTokenAddress());Methods
from()
static from(address, tryPrefixes): Address;Defined in: cashconnect-js/packages/core/src/primitives/address.ts:73
Decode a Cash Address or Legacy Address.
Parameters
| Parameter | Type | Default value | Description |
|---|---|---|---|
address | string | undefined | {string} The address to attempt to decode. |
tryPrefixes | string[] | DEFAULT_ADDRESS_PREFIXES | - |
Returns
Instance of Address.
Throws
If address cannot be decoded.
fromCashAddr()
static fromCashAddr(
cashAddr,
requireTokenSupport,
tryPrefixes): Address;Defined in: cashconnect-js/packages/core/src/primitives/address.ts:98
Decode a CashAddr into an Address Entity.
Parameters
| Parameter | Type | Default value | Description |
|---|---|---|---|
cashAddr | string | undefined | The CashAddr to decode. |
requireTokenSupport | boolean | false | - |
tryPrefixes | string[] | DEFAULT_ADDRESS_PREFIXES | - |
Returns
Instance of Address.
Throws
If address cannot be decoded.
Example
const address = Address.fromCashAddr('bitcoincash:qpeqv9k9j3l7fepp9du24f9pr5jdu7c2mvn4wwldav')fromLegacy()
static fromLegacy(legacyAddress): Address;Defined in: cashconnect-js/packages/core/src/primitives/address.ts:156
Decode a Legacy Address into an Address Entity.
Parameters
| Parameter | Type | Description |
|---|---|---|
legacyAddress | string | The Legacy (Base58) Address to decode. |
Returns
Instance of Address.
Throws
If address cannot be decoded.
fromRedeemScript()
static fromRedeemScript(bytes, type): Address;Defined in: cashconnect-js/packages/core/src/primitives/address.ts:198
Instantiate an address from Contract Bytecode.
Parameters
| Parameter | Type | Default value | Description |
|---|---|---|---|
bytes | Uint8Array | undefined | The contract bytecode |
type | "P2SH20" | "P2SH32" | 'P2SH32' | - |
Returns
Instance of Address.
Throws
If address cannot be instantiated from Contract Bytecode.
fromRedeemScriptHex()
static fromRedeemScriptHex(hex, type): Address;Defined in: cashconnect-js/packages/core/src/primitives/address.ts:226
Instantiate an address from Contract Bytecode as a hex string.
Parameters
| Parameter | Type | Default value | Description |
|---|---|---|---|
hex | string | undefined | The contract bytecode as hex |
type | "P2SH20" | "P2SH32" | 'P2SH32' | - |
Returns
Instance of Address.
Throws
If address cannot be instantiated from Contract Bytecode.
fromLockscript()
static fromLockscript(lockscript): Address;Defined in: cashconnect-js/packages/core/src/primitives/address.ts:243
Attempt to instantiate Address from Locking Bytecode.
Parameters
| Parameter | Type | Description |
|---|---|---|
lockscript | string | Uint8Array<ArrayBufferLike> | The lockscript as bytes or hex. |
Returns
Instance of Address.
Throws
If lock script cannot be decoded into an address.
fromLockscriptBytes()
static fromLockscriptBytes(lockScript): Address;Defined in: cashconnect-js/packages/core/src/primitives/address.ts:258
Attempt to instantiate Address from Locking Bytecode.
Parameters
| Parameter | Type | Description |
|---|---|---|
lockScript | Uint8Array | {Uint8Array} The lockscript bytes. |
Returns
Instance of Address.
Throws
If lock script cannot be decoded into an address.
fromLockscriptHex()
static fromLockscriptHex(hex): Address;Defined in: cashconnect-js/packages/core/src/primitives/address.ts:278
Attempt to instantiate Address from Locking Bytecode as a hex string.
Parameters
| Parameter | Type | Description |
|---|---|---|
hex | string | {Uint8Array} The lockscript bytes as hex. |
Returns
Instance of Address.
Throws
If lock script cannot be decoded into an address.
fromHash160Bytes()
static fromHash160Bytes(hash160, type): Address;Defined in: cashconnect-js/packages/core/src/primitives/address.ts:292
Instantiate an address from a Ripemd160 hash.
Parameters
| Parameter | Type | Default value | Description |
|---|---|---|---|
hash160 | Uint8Array | undefined | {Uint8Array} The hash to use. |
type | AddressType | 'P2PKH' | {AddressType} The type of the address to instantiate (e.g. P2PKH). |
Returns
Instance of Address.
Throws
If hash160 is not 20 bytes.
fromHash160Hex()
static fromHash160Hex(hash160Hex, type): Address;Defined in: cashconnect-js/packages/core/src/primitives/address.ts:316
Instantiate an address from a Ripemd160 hash (as hex).
Parameters
| Parameter | Type | Default value | Description |
|---|---|---|---|
hash160Hex | string | undefined | {string} The hash to use (as hex). |
type | AddressType | 'P2PKH' | {AddressType} The type of the address to instantiate (e.g. P2PKH). |
Returns
Instance of Address.
Throws
If hash160 is not 20 bytes.
fromRaw()
static fromRaw(addressContents): Address;Defined in: cashconnect-js/packages/core/src/primitives/address.ts:334
Creates an Address instance from a raw object representation (LibAuth's KnownAddressTypeContents).
Parameters
| Parameter | Type |
|---|---|
addressContents | KnownAddressTypeContents |
Returns
A new Address instance
Remarks
This method is UNSAFE and MUST only be used where input is already verified or trusted.
isValid()
static isValid(address, tryPrefixes): boolean;Defined in: cashconnect-js/packages/core/src/primitives/address.ts:345
Checks whether the given address is a valid Legacy or CashAddr.
Parameters
| Parameter | Type | Default value | Description |
|---|---|---|---|
address | string | undefined | The address. |
tryPrefixes | string[] | DEFAULT_ADDRESS_PREFIXES | - |
Returns
boolean
boolean indicating whether the address is a valid Legacy Address or CashAddr.
isValidLegacy()
static isValidLegacy(address): boolean;Defined in: cashconnect-js/packages/core/src/primitives/address.ts:365
Checks whether the given address is a valid Legacy Address.
Parameters
| Parameter | Type | Description |
|---|---|---|
address | string | The address. |
Returns
boolean
boolean indicating whether the address is a valid Legacy Address.
isValidCashAddr()
static isValidCashAddr(address, tryPrefixes): boolean;Defined in: cashconnect-js/packages/core/src/primitives/address.ts:382
Checks whether the given address is a valid CashAddr.
Parameters
| Parameter | Type | Default value | Description |
|---|---|---|---|
address | string | undefined | The address. |
tryPrefixes | string[] | DEFAULT_ADDRESS_PREFIXES | - |
Returns
boolean
boolean indicating whether the address is a valid CashAddr.
isValidTokenAddress()
static isValidTokenAddress(address, tryPrefixes): boolean;Defined in: cashconnect-js/packages/core/src/primitives/address.ts:402
Checks whether the given address is a valid CashAddr with Token Support.
Parameters
| Parameter | Type | Default value | Description |
|---|---|---|---|
address | string | undefined | The address. |
tryPrefixes | string[] | DEFAULT_ADDRESS_PREFIXES | - |
Returns
boolean
boolean indicating whether the address is a valid CashAddr with Token Support.
type()
type(): AddressType;Defined in: cashconnect-js/packages/core/src/primitives/address.ts:420
Gets the type of address (P2PKH, P2SH).
Returns
The type of Address.
toHash160Bytes()
toHash160Bytes(): Uint8Array;Defined in: cashconnect-js/packages/core/src/primitives/address.ts:429
Gets this address' raw Hash160 (Ripemd160) hash as a Uint8Array.
Returns
Uint8Array
The Hash160 of this address.
toHash160Hex()
toHash160Hex(): string;Defined in: cashconnect-js/packages/core/src/primitives/address.ts:438
Gets this address' raw Hash160 (Ripemd16) hash as a hex string.
Returns
string
The Hash160 of this address.
toCashAddr()
toCashAddr(networkPrefix): string;Defined in: cashconnect-js/packages/core/src/primitives/address.ts:451
Converts this address to CashAddr format.
Parameters
| Parameter | Type | Default value | Description |
|---|---|---|---|
networkPrefix | AddressNetworkPrefix | 'bitcoincash' | {'bitcoincash' |
Returns
string
The address in CashAddr format.
Throws
If address cannot be encoded as a CashAddr.
toScriptHashBytes()
toScriptHashBytes(): Uint8Array<ArrayBuffer>;Defined in: cashconnect-js/packages/core/src/primitives/address.ts:483
Returns
Uint8Array<ArrayBuffer>
toScriptHashHex()
toScriptHashHex(): string;Defined in: cashconnect-js/packages/core/src/primitives/address.ts:488
Returns
string
toTokenAddress()
toTokenAddress(networkPrefix): string;Defined in: cashconnect-js/packages/core/src/primitives/address.ts:502
Converts this address to a Tokens Address format.
Parameters
| Parameter | Type | Default value | Description |
|---|---|---|---|
networkPrefix | AddressNetworkPrefix | 'bitcoincash' | {'bitcoincash' |
Returns
string
The address in Token Address format.
Throws
If address cannot be encoded as a CashAddr.
toLegacy()
toLegacy(): string;Defined in: cashconnect-js/packages/core/src/primitives/address.ts:541
Converts this address to Legacy (Base58) Address format.
Returns
string
The address in Legacy Address format.
Throws
If address cannot be encoded as a Legacy Address (Base58).
toLockscriptBytes()
toLockscriptBytes(): Uint8Array;Defined in: cashconnect-js/packages/core/src/primitives/address.ts:568
Get the corresponding lockscript for this address as a Uint8Array.
Returns
Uint8Array
The lockscript as a Uint8Array.
toLockscriptHex()
toLockscriptHex(): string;Defined in: cashconnect-js/packages/core/src/primitives/address.ts:577
Get the corresponding lockscript for this address as a hex string.
Returns
string
The lockscript as a hex string.
toLockscriptAsm()
toLockscriptAsm(): string;Defined in: cashconnect-js/packages/core/src/primitives/address.ts:586
Get the corresponding lockscript for this address as ASM.
Returns
string
The lockscript as ASN.
toRaw()
toRaw(): object;Defined in: cashconnect-js/packages/core/src/primitives/address.ts:599
Converts the address to its raw LibAuth representation (KnownAddressTypeContents).
Returns
object
A LibAuth KnownAddressTypeContents object.
| Name | Type | Defined in |
|---|---|---|
type | "P2PK" | "P2PKH" | "P2SH20" | "P2SH32" | cashconnect-js/node_modules/@bitauth/libauth/build/lib/address/locking-bytecode.d.ts:47 |
payload | Uint8Array | cashconnect-js/node_modules/@bitauth/libauth/build/lib/address/locking-bytecode.d.ts:48 |
DEFAULT_ADDRESS_PREFIXES
const DEFAULT_ADDRESS_PREFIXES: string[];Defined in: cashconnect-js/packages/core/src/primitives/address.ts:27