This project is designed to make it very easy to interact with various RPC APIs available within the BLOC Project. This entire project uses Javascript Promises to make things fast, easy, and safe.
constBLOCd=require('bloc-rpc').BLOCdconstdaemon=newBLOCd({
host:'127.0.0.1', // ip address or hostname of the BLOCd host
port:2086, // what port is the RPC server running on
timeout:2000, // request timeout
ssl:false// whether we need to connect using SSL/TLS
})
BlocService
constBlocService=require('bloc-rpc').BlocServiceconstservice=newBlocService({
host:'127.0.0.1', // ip address or hostname of the bloc-service host
port:8070, // what port is bloc-service running on
timeout:2000, // request timeout
ssl:false, // whether we need to connect using SSL/TLS
rpcPassword:'inblocwetrust', // must be set to the password used to run bloc-service// RPC API default values
defaultMixin:false, // the default mixin to use for transactions, the default setting is false which means we don't have a default value
defaultFee:1, // the default transaction fee for transactions
defaultBlockCount:1, // the default number of blocks when blockCount is required
decimalDivisor:1000, // Currency has many decimal places?
defaultFirstBlockIndex:1, // the default first block index we will use when it is required
defaultUnlockTime:0, // the default unlockTime for transactions
defaultFusionThreshold:1, // the default fusionThreshold for fusion transactions
})
Client
constClient=require('bloc-rpc').Clientconstclient=newClient({
host:'127.0.0.1', // ip address or hostname of the BLOCd host
port:2086, // what port is the RPC server running on
timeout:2000, // request timeout
ssl:false// whether we need to connect using SSL/TLS
})
BLOCd RPC API Interface
We expose all of the BLOCd RPC API commands via the BLOCd interface. Each of the below methods are Javascript Promises. For safety sake, always handle your promise catches as we do use them properly.
Methods noted having options have parameters that may be optional or required as documented.
daemon.getBlocks(options)
Returns information on the last 30 blocks before height (inclusive).
Input
Argument
Mandatory
Description
Format
height
Yes
height of the blockchain to be included in the result.
integer
Example Code
daemon.getBlocks({
height:104326
}).then((blocks) => {
// do something
})
| cumul_size | size of the block | int
| difficulty | difficulty of the block | int
| hash | hash of the block | string
| height | height of the block | int
| timestamp | the time at which the block is occured on the chain since Unix epoch | int
| tx_count | number of transactions in the block | int
daemon.getBlock(options)
Returns information on a single block
Input
Argument
Mandatory
Description
Format
hash
Yes
Block hash you wish to retreive
string
Example Code
daemon.getBlock({
hash:'d8a24f43719a1088c9311dbbef17cef5141fa02bf29986a38dae59e20297c768'
}).then((block) => {
// do something
})
We expose all of the bloc-service RPC API commands via the BlocService interface. Each of the below methods are Javascript Promises. For safety sake, always handle your promise catches as we do use them properly.
Special Note: Any and all amounts/fees will already be in HUMAN readable units. DO NOT DIVIDE THEM AGAIN unless you've specified decimalDivisor as 1 in the options. You have been warned.
Unless otherwise noted, all methods will resolve the promise upon success and sample return data is supplied below. Any errors will reject the promise with an error condition.
Methods noted having options have parameters that may be optional or required as documented.
service.reset(options)
If the viewSecretKey argument is not provided, the reset() method resets the wallet and re-syncs it. If the viewSecretKey argument is provided, the reset() method substitutes the existing wallet with a new one with the specified key.
Input
Argument
Mandatory
Description
Format
viewSecretKey
No
The secret Private view key to reset
string
newAddress
No
Is this a new address being created? If so, blocks before the creation timestamp will not be scanned. Only one of newAddress and scanHeight can be specified, as if a new address is being created, there is no need to scan from a certain height.
bool
scanHeight
No
The height to begin scanning for transactions at. This can greatly speed up wallet syncing time.
int
Example Code
service.reset({
viewSecretKey:'12345678901234567890'
}).then(() => {
// do something
})
Output
No output in case of success.
If the viewSecretKey argument is not provided, the reset() method resets the wallet and
re-syncs it. If the viewSecretKey argument is provided, the reset() method substitutes the
existing wallet with a new one with the specified key.
service.save()
save() method allows you to save your wallet by request.
No input.
No output in case of success.
Example Code
service.save().then(() => {
// do something
})
service.getFeeInfo()
getFeeInfo() method retrieves the fee and address (if any) that that BLOCd walletd is connecting to is using. This fee will automatically be added to any transactions sent by sendTransaction() or sendDelayedTransaction(). Note it does not apply to sendFusionTransaction()
No input
Example Code
service.getFeeInfo().then((result) => {
// do something
})
getMnemonicSeed() method returns the mnemonic seed for the given deterministic address. A mnemonic seed is a list of words which can be used to recover a wallet.
Input
Argument
Mandatory
Description
Format
address
Yes
Valid deterministic address that exists in this container
string
Example Code
service.getMnemonicSeed({
address:'abLocAR4dJi14yKV7ixsMMhW77HZt8c6HCYM5qdJDnwkDQByQwbBbPAYZuE4QGJVYfRc1TPmysMAuioPFuanAumEQoKCgBaK6xg'
}).then((result) => {
// do something
})
Example Data
dads vehicle fiat fountain repent radar aspire orbit awesome trolling guide drinks kickoff heron husband tutor onward legion nail yahoo arena were melting necklace vehicle
Output
Argument
Description
Format
mnemonicSeed
Mnemonic seed
string
The first wallet address that is generated when the container is created is the deterministic address. Only one wallet from a multi-wallet container can be deterministic. If a non-deterministic address is given, the RPC response will be an error with the message: "Keys not deterministic."
service.getStatus()
service.getStatus() method returns information about the current RPC Wallet state: block count, known block count, last block hash and peer count.
No input
Example Code
service.getStatus().then((result) => {
// do something
})
createAddress() method creates an additional address in your wallet.
Input
Argument
Mandatory
Description
Format
secretSpendKey
No
Private spend key. If secretSpendKey was specified, RPC Wallet creates spend address
string
publicSpendKey
No
Public spend key. If publicSpendKey was specified, RPC Wallet creates view address
string
newAddress
No
Is this a new address being created? If so, blocks before the creation timestamp will not be scanned. Defaults to true if neither keys are given, as it is guaranteed to be a new address.
bool
scanHeight
No
The height to begin scanning for transactions at. Only applies if a public/secret key is supplied. This can greatly speed up wallet syncing time.
int
Note: Both secretSpendKey and publicSpendKey are optional; however, you can only supply one or the other. Both are given below as examples.
Example Code
service.createAddress({
secretSpendKey:'f4abd8dfc3ffea2c96b131c22340d4b437b9722407769c9449f24d1a8bad0c0e',
publicSpendKey:'db20e68b00e69ba85d161af5378ab04551e0c10f23d1df6ea29da58ddc527b2e'
}).then((result) => {
// do something
})
deleteAddress() method deletes a specified address.
Input
Argument
Mandatory
Description
Format
address
Yes
An address to be deleted
string
Example Code
service.deleteAddress({
address:'abLocv1pacKFJk9QgSmzk2LJWn14JGmTKzReFLz1RgY3K9Ryn7783RDT2TretzfYdck5GMCGzXTuwKfePWQYViNs4avKpnUbrwfQ'
}).then((result) => {
// do something
})
Output
In case of success returns an empty JSON object.
service.getBalance(options)
getBalance() method returns a balance for a specified address.
Input
Argument
Mandatory
Description
Format
address
No
Valid address that exists in this container
string
Example Code
service.getBalance({
address:'abLocAR4dJi14yKV7ixsMMhW77HZt8c6HCYM5qdJDnwkDQByQwbBbPAYZuE4QGJVYfRc1TPmysMAuioPFuanAumEQoKCgBaK6xg'
}).then((result) => {
// do something
})
Example Data
{
"availableBalance":1.0000,
"lockedAmount":0
}
Output
Argument
Description
Format
availableBalance
Available balance of the specified address in shells
integer
lockedAmount
Locked amount of the specified address in shells
integer
If address is not specified, getBalance() returns a cumulative balance of all RPC Wallet's addresses.
Also note, balances are expressed in shells, so a balance of 10000 is equal to 100.00 abLoc.
service.getBlockHashes(options)
getBlockHashes() method returns an array of block hashes for a specified block range.
Input
Argument
Mandatory
Description
Format
firstBlockIndex
Yes
Starting height
integer
blockCount
Yes
Number of blocks to process
integer
Example Code
service.getBlockHashes({
firstBlockIndex:100000,
blockCount:10
}).then((result) => {
// do something
})
Array of strings, where each element is a block hash
array
service.getTransactionHashes(options)
getTransactionHashes() method returns an array of block and transaction hashes.
A transaction consists of transfers. A transfer is an amount-address pair.
There could be several transfers in a single transaction.
Input
Argument
Mandatory
Description
Format
addresses
No
Array of strings, where each string is an address
array
blockHash
No. Only one of these parameters (blockHash or firstBlockIndex) is allowed.
Hash of the starting block
string
firstBlockIndex
No. Only one of these parameters (blockHash or firstBlockIndex) is allowed.
Starting height >0 (1,2,3...)
integer
blockCount
Yes
Number of blocks to return transaction hashes from
integer
paymentId
No. 64 characters
Valid payment ID
string
If paymentId parameter is set, getTransactionHashes() method returns transaction hashes of transactions that contain specified payment ID in the given block range.
If addresses parameter is set, getTransactionHashes() method returns transaction hashes of transactions that contain transfer from at least one of specified addresses.
If both above mentioned parameters are set, getTransactionHashes() method returns transaction hashes of transactions that contain both specified payment ID and transfer from at least one of specified addresses.
Only one of either blockHash or firstBlockIndex may be supplied, but not both.
| **Attribute** | **Description** | **Format** |
| blockHash | Hash of the block which contains transaction hashes | string |
| transactionHashes | Array of strings, where each string is a transaction hash | array |
service.getTransactions(options)
getTransactions() method returns an array of block and transaction hashes.
A transaction consists of transfers. A transfer is an amount-address pair.
There could be several transfers in a single transaction.
Input
Argument
Mandatory
Description
Format
addresses
No
Array of strings, where each string is an address
array
blockHash
No. Only one of these parameters (blockHash or firstBlockIndex) is allowed.
Hash of the starting block
string
firstBlockIndex
No. Only one of these parameters (blockHash or firstBlockIndex) is allowed.
Starting height >0 (1,2,3...)
integer
blockCount
Yes
Number of blocks to return transaction hashes from
integer
paymentId
No. 64 characters
Valid payment ID
string
If paymentId parameter is set, getTransactions() method returns transactions that contain specified payment ID in the given block range.
If addresses parameter is set, getTransactions() method returns transactions that contain transfer from at least one of specified addresses.
If both above mentioned parameters are set, getTransactions() method returns transactions that contain both specified payment ID and transfer from at least one of specified addresses.
Only one of either blockHash or firstBlockIndex may be supplied, but not both.
Array of strings, where each string is a hash of an unconfirmed transaction
array
service.getTransaction(options)
getTransaction() method returns information about a particular transaction.
Transaction consists of transfers. Transfer is an amount-address pair.
There could be several transfers in a single transaction.
Special Note: Any and all amounts/fees will already be in HUMAN readable units. DO NOT DIVIDE AMOUNTS AGAIN unless you've specified decimalDivisor as 1 in the options. You have been warned.
Input
Argument
Mandatory
Description
Format
transactionHash
Yes
Hash of the requested transaction
string
Example Code
service.getTransaction({
transactionHash:'f731190b013ac313d6cf2edba86e1e1fe1f0220c2bb59b80d817b0957f0d0e4d'
}).then((result) => {
// do something
})
Shows if the transaction is a CoinBase transaction or not
boolean
unlockTime
Height of the block when transaction is going to be available for spending
int
amount
Amount of the transaction
int
fee
Transaction fee
int
extra
Hash of the transaction
string
paymentId
Payment ID of the transaction (optional)
string
transfers
Array of addresses (string), amount (int)
array
service.newTransfer(address, amount)
This method creates a transfer object designed to be used with service.sendTransaction
Note: This method does NOT return a promise.
Special Note: Any and all amounts/fees will already be in HUMAN readable units. DO NOT SUPPLY NATIVE CURRENCY AMOUNTS unless you've specified decimalDivisor as 1 in the options. You have been warned.
Example Code
var transfer =service.newTransfer('abLocv1pacKFJk9QgSmzk2LJWn14JGmTKzReFLz1RgY3K9Ryn7783RDT2TretzfYdck5GMCGzXTuwKfePWQYViNs4avKpnUbrwfQ', 1000000)
service.sendTransaction(options)
sendTransaction() method allows you to send transaction(s) to one or several addresses. Also, it allows you to use a payment ID for a transaction to a single address.
Special Note: Any and all amounts/fees will already be in HUMAN readable units. DO NOT SUPPLY NATIVE CURRENCY AMOUNTS unless you've specified decimalDivisor as 1 in the options. You have been warned.
Input
Argument
Mandatory
Description
Format
addresses
No
Array of strings, where each string is an address to take the funds from
array
transfers
Yes
Array of objects, address: (string address), amount: (int amount)
array
fee
Yes
Transaction fee. Minimal fee in BLOC network is 0.0001 BLOC. As with other amounts use whole units, 1 BLOC = 1000 units, so 0.0001 BLOC = 1 unit
integer
unlockTime
No
The block height at which the transaction will be unlocked for spending.
integer
anonymity
Yes
Privacy (mixin) (level from 0 to 10)
integer
extra
No
String of variable length. Can contain A-Z, 0-9 characters.
string
paymentId
No
Payment ID (64 hex characters)
string
changeAddress
No
Where to send any change from the transaction. If not specified, the first address in the wallet container is used.
string
If container contains only 1 address, changeAddress field can be left empty and the change is going to be sent to this address.
If addresses field contains only 1 address, changeAddress can be left empty and the change is going to be sent to this address.
In the rest of the cases, changeAddress field is mandatory and must contain an address.
createDelayedTransaction() method creates a delayed transaction. Such transactions are not sent into the network automatically and should be pushed using sendDelayedTransaction method.
Special Note: Any and all amounts/fees will already be in HUMAN readable units. DO NOT SUPPLY NATIVE CURRENCY AMOUNTS unless you've specified decimalDivisor as 1 in the options. You have been warned.
Input
Argument
Mandatory
Description
Format
addresses
No
Array of strings, where each string is an address to take the funds from
array
transfers
Yes
Array of objects, address: (string address), amount: (int amount)
array
fee
Yes
Transaction fee. Minimal fee in BLOC network is 0.0001 BLOC. As with other amounts use whole units, 1 BLOC = 1000 units, so 0.0001 BLOC = 1 unit
integer
unlockTime
No
The block height at which the transaction will be unlocked for spending.
integer
anonymity
Yes
Privacy (mixin) (level from 0 to 10)
integer
extra
No
String of variable length. Can contain A-Z, 0-9 characters.
string
paymentId
No
Payment ID (64 hex characters)
string
changeAddress
No
Where to send any change from the transaction. If not specified, the first address in the wallet container is used.
string
If container contains only 1 address, changeAddress field can be left empty and the change is going to be sent to this address
If addresses field contains only 1 address, changeAddress can be left empty and the change is going to be sent to this address
In the rest of the cases, changeAddress field is mandatory and must contain an address.
Outputs that were used for this transactions will be locked until the transaction is sent or cancelled
service.deleteDelayedTransaction({
transactionHash:'d01e448f7b631cebd989e3a150258b0da59c66f96adecec392bbf61814310751'
}).then((result) => {
// do something
})
service.sendDelayedTransaction()
Method Parameters
Argument
Mandatory
Description
Format
transactionHash
Yes
The hash of the transaction
string
Example Code
service.sendDelayedTransaction({
transactionHash:'d01e448f7b631cebd989e3a150258b0da59c66f96adecec392bbf61814310751'
}).then((result) => {
// do something
})
service.sendFusionTransaction(options)
Method Parameters
Argument
Mandatory
Description
Format
threshold
No
The minimum fusion threshold amount
integer
mixin
No
The number of mixins to use
integer
addresses
No
Array of public wallet addresses
strings
destinationAddress
No
The address to send the fusion transaction to
string
Note: If the container has only one address or addressess consists of one address, then destinationAddress need not be supplied. Otherwise, destinationAddress is required.
Example Code
service.sendFusionTransaction({
mixin:7,
destinationAddress:'abLocv1pacKFJk9QgSmzk2LJWn14JGmTKzReFLz1RgY3K9Ryn7783RDT2TretzfYdck5GMCGzXTuwKfePWQYViNs4avKpnUbrwfQ'
}).then((result) => {
// do something
})
We expose all of the BLOCd Client RPC API commands via the Client interface. Each of the below methods are Javascript Promises. For safety sake, always handle your promise catches as we do use them properly.
Methods noted having options have parameters that may be optional or required as documented.
client.getBlocks(options)
Not implemented
client.queryBlocks(options)
Not implemented
client.queryBlocksLite(options)
Retrieves the last 100 (as defined in ) blocks from the first block hash supplied in the requested array.
The Tidelift Subscription provides access to a continuously curated stream of human-researched and maintainer-verified data on open source packages and their licenses, releases, vulnerabilities, and development practices.