Skip to content

Documentation / @cashconnect-js/core / primitives/address

AddressType

ts
type AddressType = "P2PK" | "P2PKH" | "P2SH20" | "P2SH32";

Defined in: cashconnect-js/packages/core/src/primitives/address.ts:19


AddressNetworkPrefix

ts
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

ts
// 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()

ts
static from(address, tryPrefixes): Address;

Defined in: cashconnect-js/packages/core/src/primitives/address.ts:73

Decode a Cash Address or Legacy Address.

Parameters
ParameterTypeDefault valueDescription
addressstringundefined{string} The address to attempt to decode.
tryPrefixesstring[]DEFAULT_ADDRESS_PREFIXES-
Returns

Address

Instance of Address.

Throws

If address cannot be decoded.

fromCashAddr()

ts
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
ParameterTypeDefault valueDescription
cashAddrstringundefinedThe CashAddr to decode.
requireTokenSupportbooleanfalse-
tryPrefixesstring[]DEFAULT_ADDRESS_PREFIXES-
Returns

Address

Instance of Address.

Throws

If address cannot be decoded.

Example
ts
const address = Address.fromCashAddr('bitcoincash:qpeqv9k9j3l7fepp9du24f9pr5jdu7c2mvn4wwldav')

fromLegacy()

ts
static fromLegacy(legacyAddress): Address;

Defined in: cashconnect-js/packages/core/src/primitives/address.ts:156

Decode a Legacy Address into an Address Entity.

Parameters
ParameterTypeDescription
legacyAddressstringThe Legacy (Base58) Address to decode.
Returns

Address

Instance of Address.

Throws

If address cannot be decoded.

fromRedeemScript()

ts
static fromRedeemScript(bytes, type): Address;

Defined in: cashconnect-js/packages/core/src/primitives/address.ts:198

Instantiate an address from Contract Bytecode.

Parameters
ParameterTypeDefault valueDescription
bytesUint8ArrayundefinedThe contract bytecode
type"P2SH20" | "P2SH32"'P2SH32'-
Returns

Address

Instance of Address.

Throws

If address cannot be instantiated from Contract Bytecode.

fromRedeemScriptHex()

ts
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
ParameterTypeDefault valueDescription
hexstringundefinedThe contract bytecode as hex
type"P2SH20" | "P2SH32"'P2SH32'-
Returns

Address

Instance of Address.

Throws

If address cannot be instantiated from Contract Bytecode.

fromLockscript()

ts
static fromLockscript(lockscript): Address;

Defined in: cashconnect-js/packages/core/src/primitives/address.ts:243

Attempt to instantiate Address from Locking Bytecode.

Parameters
ParameterTypeDescription
lockscriptstring | Uint8Array<ArrayBufferLike>The lockscript as bytes or hex.
Returns

Address

Instance of Address.

Throws

If lock script cannot be decoded into an address.

fromLockscriptBytes()

ts
static fromLockscriptBytes(lockScript): Address;

Defined in: cashconnect-js/packages/core/src/primitives/address.ts:258

Attempt to instantiate Address from Locking Bytecode.

Parameters
ParameterTypeDescription
lockScriptUint8Array{Uint8Array} The lockscript bytes.
Returns

Address

Instance of Address.

Throws

If lock script cannot be decoded into an address.

fromLockscriptHex()

ts
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
ParameterTypeDescription
hexstring{Uint8Array} The lockscript bytes as hex.
Returns

Address

Instance of Address.

Throws

If lock script cannot be decoded into an address.

fromHash160Bytes()

ts
static fromHash160Bytes(hash160, type): Address;

Defined in: cashconnect-js/packages/core/src/primitives/address.ts:292

Instantiate an address from a Ripemd160 hash.

Parameters
ParameterTypeDefault valueDescription
hash160Uint8Arrayundefined{Uint8Array} The hash to use.
typeAddressType'P2PKH'{AddressType} The type of the address to instantiate (e.g. P2PKH).
Returns

Address

Instance of Address.

Throws

If hash160 is not 20 bytes.

fromHash160Hex()

ts
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
ParameterTypeDefault valueDescription
hash160Hexstringundefined{string} The hash to use (as hex).
typeAddressType'P2PKH'{AddressType} The type of the address to instantiate (e.g. P2PKH).
Returns

Address

Instance of Address.

Throws

If hash160 is not 20 bytes.

fromRaw()

ts
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
ParameterType
addressContentsKnownAddressTypeContents
Returns

Address

A new Address instance

Remarks

This method is UNSAFE and MUST only be used where input is already verified or trusted.

isValid()

ts
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
ParameterTypeDefault valueDescription
addressstringundefinedThe address.
tryPrefixesstring[]DEFAULT_ADDRESS_PREFIXES-
Returns

boolean

boolean indicating whether the address is a valid Legacy Address or CashAddr.

isValidLegacy()

ts
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
ParameterTypeDescription
addressstringThe address.
Returns

boolean

boolean indicating whether the address is a valid Legacy Address.

isValidCashAddr()

ts
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
ParameterTypeDefault valueDescription
addressstringundefinedThe address.
tryPrefixesstring[]DEFAULT_ADDRESS_PREFIXES-
Returns

boolean

boolean indicating whether the address is a valid CashAddr.

isValidTokenAddress()

ts
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
ParameterTypeDefault valueDescription
addressstringundefinedThe address.
tryPrefixesstring[]DEFAULT_ADDRESS_PREFIXES-
Returns

boolean

boolean indicating whether the address is a valid CashAddr with Token Support.

type()

ts
type(): AddressType;

Defined in: cashconnect-js/packages/core/src/primitives/address.ts:420

Gets the type of address (P2PKH, P2SH).

Returns

AddressType

The type of Address.

toHash160Bytes()

ts
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()

ts
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()

ts
toCashAddr(networkPrefix): string;

Defined in: cashconnect-js/packages/core/src/primitives/address.ts:451

Converts this address to CashAddr format.

Parameters
ParameterTypeDefault valueDescription
networkPrefixAddressNetworkPrefix'bitcoincash'{'bitcoincash'
Returns

string

The address in CashAddr format.

Throws

If address cannot be encoded as a CashAddr.

toScriptHashBytes()

ts
toScriptHashBytes(): Uint8Array<ArrayBuffer>;

Defined in: cashconnect-js/packages/core/src/primitives/address.ts:483

Returns

Uint8Array<ArrayBuffer>

toScriptHashHex()

ts
toScriptHashHex(): string;

Defined in: cashconnect-js/packages/core/src/primitives/address.ts:488

Returns

string

toTokenAddress()

ts
toTokenAddress(networkPrefix): string;

Defined in: cashconnect-js/packages/core/src/primitives/address.ts:502

Converts this address to a Tokens Address format.

Parameters
ParameterTypeDefault valueDescription
networkPrefixAddressNetworkPrefix'bitcoincash'{'bitcoincash'
Returns

string

The address in Token Address format.

Throws

If address cannot be encoded as a CashAddr.

toLegacy()

ts
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()

ts
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()

ts
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()

ts
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()

ts
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.

NameTypeDefined in
type"P2PK" | "P2PKH" | "P2SH20" | "P2SH32"cashconnect-js/node_modules/@bitauth/libauth/build/lib/address/locking-bytecode.d.ts:47
payloadUint8Arraycashconnect-js/node_modules/@bitauth/libauth/build/lib/address/locking-bytecode.d.ts:48

DEFAULT_ADDRESS_PREFIXES

ts
const DEFAULT_ADDRESS_PREFIXES: string[];

Defined in: cashconnect-js/packages/core/src/primitives/address.ts:27