@crpdo/coins
v0.0.3
Published
A host of methods to manage a variety of cryptocurrencies, such as deriving paths for coin symbols, obtaining coin type IDs, and generating Bitcoin or Ethereum addresses for specific public keys.
Downloads
23
Maintainers
Keywords
Readme
@crpdo/coins
Cha Ching
Coins is a utility class containing methods for working with different types of cryptocurrencies as part of the crpdo project. This module is particularly helpful for tasks like getting coin type IDs, deriving paths for a coin symbol, and getting Bitcoin or Ethereum addresses for given public keys, among other tasks.
Installation
npm install @crpdo/coins
Usage
Below are some examples of how to use the Coins module:
const Coins = require('@crpdo/coins')
const coinTypeId = Coins.getCoinType('BTC') // Get coin type id for Bitcoin
console.log(coinTypeId) // Outputs: 0
const derivePath = Coins.getDerivePath('ETH', 0, 0) // Get derive path for Ethereum
console.log(derivePath) // Outputs: "m/44'/60'/0'/0"
const btcAddress = Coins.getBtcAddress('somePublicKey', 'bitcoin') // Get Bitcoin address for a given public key
console.log(btcAddress) // Outputs: the Bitcoin address
const isValidAddress = Coins.validateAddress('BTC', 'someAddress') // Validate a given address for Bitcoin
console.log(isValidAddress) // Outputs: true or false depending on the address
const coinInfo = Coins.getCoinInfo('ETH') // Get comprehensive information for Ethereum
console.log(coinInfo) // Outputs: the information for Ethereum
const transactionFee = await Coins.getTransactionFee('BTC') // Get the current average transaction fee for Bitcoin
console.log(transactionFee) // Outputs: the current average transaction fee for Bitcoin
const convertedAmount = await Coins.convertAmount('BTC', 1, 'ETH') // Convert 1 Bitcoin to Ethereum
console.log(convertedAmount) // Outputs: the equivalent amount in Ethereum
Please note that in order to use the getTransactionFee
and convertAmount
methods, you need to use the await
keyword because they are asynchronous.
Documentation
API
Coins.getCoinType(coinSymbol)
Get the coin type id for a given coin symbol.
Parameters:
coinSymbol
- String: The symbol for the coin e.g. 'BTC'.
Returns:
- Number: The coin type id for the coin symbol.
Coins.getDerivePath(coinSymbol, account, change, index)
Get the derive path for a coin symbol, account, change, and optionally index.
Parameters:
coinSymbol
- String: The symbol for the coin. Default: 'BTC'.account
- Number: The account number. Default: 0.change
- Number: The change number. Default: 0.index
- Number: The index number. Optional.
Returns:
- String: The derive path.
Coins.getNetwork(coinSymbol)
Set up network parameters for Dogecoin. This method currently only supports 'DOGE'.
Parameters:
coinSymbol
- String: The symbol for the coin e.g. 'DOGE'.
Coins.getBtcAddress(pubkey, network)
Get a Bitcoin address for a given public key.
Parameters:
pubkey
- String|Buffer: The public key as a string or buffer.network
- String: The network name. Default: 'bitcoin'.
Returns:
- String: The Bitcoin address.
Coins.getEthAddress(pubkey)
Get an Ethereum address for a given public key.
Parameters:
pubkey
- String|Buffer: The public key as a string or buffer.
Returns:
- String: The Ethereum address.
Coins.getCoinName(coinSymbol)
Get the full name of a given coin.
Parameters:
coinSymbol
- String: The symbol for the coin e.g. 'BTC'.
Returns:
- String: The full name of the coin.
- Throws an error if the coin symbol is not supported.
Coins.validateAddress(coinSymbol, address)
Validate a given address for a specific coin type.
Parameters:
coinSymbol
- String: The symbol for the coin e.g. 'BTC'.address
- String: The address to validate.
Returns:
- Boolean: True if the address is valid, false otherwise.
- Throws an error if the coin symbol is not supported.
Coins.getCoinInfo(coinSymbol)
Get a comprehensive set of information for a specific coin.
Parameters:
coinSymbol
- String: The symbol for the coin e.g. 'BTC'.
Returns:
- Object: An object containing the coin information.
- Throws an error if the coin symbol is not supported.
Coins.getTransactionFee(coinSymbol)
Get the current average transaction fee for a given coin.
Parameters:
coinSymbol
- String: The symbol for the coin e.g. 'BTC'.
Returns:
- Number: The current average transaction fee.
- Throws an error if the coin symbol is not supported.
Coins.convertAmount(coinSymbol, amount, toCoinSymbol)
Convert an amount of one cryptocurrency to another.
Parameters:
coinSymbol
- String: The symbol for the source coin.amount
- Number: The amount of the source coin to convert.toCoinSymbol
- String: The symbol for the target coin.
Returns:
- Number: The equivalent amount in the target coin.
- Throws an error if either coin symbol is not supported.
Tests
In order to run the test suite, simply clone the repository and install its dependencies:
git clone https://gitlab.com/frenware/core/crpdo/coins.git
cd basd
npm install
To run the tests:
npm test
Contributing
Thank you! Please see our contributing guidelines for details.
Donations
If you find this project useful and want to help support further development, please send us some coin. We greatly appreciate any and all contributions. Thank you!
Bitcoin (BTC):
1JUb1yNFH6wjGekRUW6Dfgyg4J4h6wKKdF
Monero (XMR):
46uV2fMZT3EWkBrGUgszJCcbqFqEvqrB4bZBJwsbx7yA8e2WBakXzJSUK8aqT4GoqERzbg4oKT2SiPeCgjzVH6VpSQ5y7KQ
License
@crpdo/coins is MIT licensed.