This module provides conversion and validation functions for units (Satoshis, BTC) and hex strings.
Methods
(static) bitcoinsToSatoshis(btc) → {BigNumber}
Convert a value in BTC to Satoshis.
- Accepts both positive and negative input values.
- Rounds down output value to the nearest Satoshi.
Parameters:
Name | Type | Description |
---|---|---|
btc |
BigNumber | string | number | value in BTC |
Returns:
value in satoshis
- Type
- BigNumber
Example
import {bitcoinsToSatoshis} from "unchained-bitcoin";
console.log(bitcoinsToSatoshis(1.2345)); // 123450000
console.log(bitcoinsToSatoshis(-1.2345)); // -123450000
(static) hash160(buf) → {Buffer}
Given a buffer as a digest, pass through sha256 and ripemd160 hash functions. Returns the result
Parameters:
Name | Type | Description |
---|---|---|
buf |
Buffer | buffer to get hash160 of |
Returns:
hash160 of the given buffer
- Type
- Buffer
(static) satoshisToBitcoins(satoshis) → {BigNumber}
Convert a value in Satoshis to BTC.
- Accepts both positive and negative input values.
- Rounds down (towards zero) input value to the nearest Satoshi.
Parameters:
Name | Type | Description |
---|---|---|
satoshis |
BigNumber | string | number | value in Satoshis |
Returns:
value in BTC
- Type
- BigNumber
Example
import {satoshisToBitcoins} from "unchained-bitcoin";
console.log(satoshisToBitcoins(123450000)); // 1.2345
console.log(satoshisToBitcoins('0.5')); // 0
console.log(satoshisToBitcoins('-100000000.5')); // -1.0
(static) toHexString(byteArray) → {string}
Converts a byte array to its hex representation.
Parameters:
Name | Type | Description |
---|---|---|
byteArray |
Array.<number> | Buffer | input byte array |
Returns:
hex representation of input array
- Type
- string
Example
import {toHexString} from "unchained-bitcoin";
const hex = toHexString([255, 0, 15, 16, 31, 32]);
console.log(hex) // ff000f101f20
(static) validBase64(inputString) → {boolean}
Validate whether the given string is base64.
- Valid base64 consists of whole groups of 4 characters containing
a-z
,A-Z
, 0-9,+
, or/
. The end of the string may be padded with==
or=
to complete the four character group.
Parameters:
Name | Type | Description |
---|---|---|
inputString |
string | string to validate |
Returns:
true if base64, false otherwise.
- Type
- boolean
(static) validateHex(inputString) → {string}
Validate whether the given string is hex.
-
Valid hex consists of an even number of characters 'a-f
,
A-F, or
0-9`. This is case-insensitive. -
The presence of the common prefix
0x
will make the input be considered invalid (because of the
Parameters:
Name | Type | Description |
---|---|---|
inputString |
string | string to validate |
Returns:
empty if valid or corresponding validation message if not
- Type
- string
Example
import {validateHex} from "unchained-bitcoin";
console.log(validateHex('00112233gg')) // "Invalid hex: ..."
console.log(validateHex('0xdeadbeef')) // "Invalid hex: ..."
console.log(validateHex('deadbeef')) // ""
console.log(validateHex('DEADbeef')) // ""