BECH32
- Description:
Provides encoding/decoding for native SegWit (bech32) and Taproot (bech32m) Bitcoin addresses.
- Source:
Methods
(static) decode(address) → {Object|number|Buffer|string|string}
- Description:
Decode a Bech32/Bech32m address
- Source:
Example
const decoded = BECH32.decode('bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq');
// Returns: { version: 0, program: Buffer, network: 'main', type: 'p2wpkh' }
Parameters:
| Name | Type | Description |
|---|---|---|
address |
string | Bech32-encoded Bitcoin address |
Throws:
-
If address prefix is invalid
- Type
- Error
Returns:
-
Decoded address information
- Type
- Object
-
returns.version - Witness version (0-16)
- Type
- number
-
returns.program - Witness program
- Type
- Buffer
-
returns.network - Network type ('main' or 'test')
- Type
- string
-
returns.type - Address type ('p2wpkh', 'p2wsh', 'p2tr', 'unknown')
- Type
- string
(static) getAddressType(version, programLength) → {string}
- Description:
Determine address type from witness version and program length
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
version |
number | Witness version (0-16) |
programLength |
number | Length of witness program in bytes |
Returns:
Address type ('p2wpkh', 'p2wsh', 'p2tr', 'unknown')
- Type
- string
(static) getPrefix(networkopt) → {string}
- Description:
Get the Bech32 prefix for a network
- Source:
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
network |
string |
<optional> |
'main'
|
Network type |
Returns:
Human-readable part prefix ('bc' or 'tb')
- Type
- string
(static) to_P2TR(xOnlyPublicKey, networkopt) → {string}
- Description:
Encode an x-only public key to a P2TR (Pay-to-Taproot) address
- Source:
Example
const address = BECH32.to_P2TR(xOnlyPubKey, 'main');
// Returns: 'bc1p...'
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
xOnlyPublicKey |
string | Buffer | 32-byte x-only public key |
||
network |
string |
<optional> |
'main'
|
Network type ('main' or 'test') |
Throws:
-
If public key is not 32 bytes or network is unknown
- Type
- Error
Returns:
Bech32m-encoded P2TR address
- Type
- string
(static) to_P2WPKH(publicKeyHex, networkopt) → {string}
- Description:
Encode a public key to a P2WPKH (Pay-to-Witness-Public-Key-Hash) address
- Source:
Example
const address = BECH32.to_P2WPKH(compressedPubKeyHex, 'main');
// Returns: 'bc1q...'
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
publicKeyHex |
string | Compressed or uncompressed public key as hex |
||
network |
string |
<optional> |
'main'
|
Network type ('main' or 'test') |
Throws:
-
If public key length is invalid or network is unknown
- Type
- Error
Returns:
Bech32-encoded P2WPKH address
- Type
- string
(static) to_P2WSH(scriptHash, networkopt) → {string}
- Description:
Encode a script hash to a P2WSH (Pay-to-Witness-Script-Hash) address
- Source:
Example
const address = BECH32.to_P2WSH(sha256Hash, 'main');
// Returns: 'bc1q...' (62 characters)
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
scriptHash |
string | Buffer | 32-byte SHA256 hash of the witness script |
||
network |
string |
<optional> |
'main'
|
Network type ('main' or 'test') |
Throws:
-
If hash length is not 32 bytes or network is unknown
- Type
- Error
Returns:
Bech32-encoded P2WSH address
- Type
- string
(static) validate(address) → {boolean}
- Description:
Validate a Bech32/Bech32m address
- Source:
Example
BECH32.validate('bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq'); // true
Parameters:
| Name | Type | Description |
|---|---|---|
address |
string | Address to validate |
Returns:
True if address is valid
- Type
- boolean