Counterparty JSON RPC: how to use

Alex Kakunov
AmiLabs
Published in
3 min readJan 12, 2015

Counterparty’s API documentation is not comprehensive enough. You may spend a lot of time researching how to make a command work.

For example, to send any transaction into the blockchain network you have to execute a sequence of step-by-step commands: a command you need, then sign the transaction and then broadcast the data into blockhain network. You may use different ways to implement that: counterpartyd, bitcoind, cp’s wallet, third-party javascript libraries.

Below see explained samples of several commands that we used.

Create asset, additional issuance

The command required all three steps to push data into blockchain.

STEP 1
command: create_issuance
daemon: counterpartyd
parameters:

source: bitcoin address of asset owner
asset: asset name (there are some requirements for naming the asset)
pubkey: key of owner’s address
quantity: count of issued specified in satoshis
allow_unconfirmed_inputs: true
description: public description, may be changed later
divisible: true (set to false if you don’t need decimal points)
encoding: “multisig”
transfer_destination: null

Result: hex-encoded raw transaction’s data.

STEP 2: command sign_tx with parameters unsigned_tx_hash (previous command result) and privkey — private key of the owner’s address.
Result: signed raw transaction data
This operation may be done at the browser side (using bitcoinjs-lib for example) if you don’t want to send private key via network.

STEP 3: command broadcast_tx with parameter signed_tx_hash (previous command result)
Result: hash of the broadcasted transaction.

Check asset existing

This command doesn’t send any data into blockchain, so it’s simple to implement.

command: get_issuances
daemon: counterpartyd
parameters:
(JSON sample) {filters: {field: “asset”, op: “==”, value: “asset_name”}, status: “valid”}

Get address info

Get information about one or several addressess.

command: get_chain_address_info
daemon: counterblockd
parameters:
addresses: one or more requested addresses
with_block_height: true
with_last_txn_hashes: count of last confirmed transactions
with_uxtos: true (if you need unspent outputs data)

Result:
addr: bitcoin address
block_height: 314302
info: {
addrStr: address
balance: actual balance
balanceSat: actual balance in satoshis
unconfirmedBalance: unconfirmed balance
unconfirmedBalanceSat: unconfirmed balance in satoshis
}
last_txns: hashes of last transactions
uxtos: unspent outputs, for example:
{ address: “n3aCGc8C5ythCKW1XokwLTDveMxyL6nJth”
amount: 0.000078
confirmations: 1272
confirmationsFromCache: false
scriptPubKey: “76a914f1efae4fcec7a26204b8480acc5330a597b0c02688ac”
ts: 0
txid: “a67d7a9cb3580f5af9d7df9bb120ced34c506b32e8e6fd52c1da7ec1d40adba1" vout: 0
}, … ]

Send coins

Yes, this is the command you may looked at the first of all.

STEP 1
command: create_send
daemon: counterpartyd
parameters:
source: bitcoin address of sender
asset: asset name, there is also special asset names available: “BTC”, “XCP”
destination: bitcoin address of receiver
pubkey: sender’s public key
quantity: amount to send in satoshis
allow_unconfirmed_inputs: true

Result: hex-encoded raw transaction’s data.

STEP 2: command sign_tx with parameters unsigned_tx_hash (previous command result) and privkey — private key of the owner’s address.
Result: signed raw transaction data
This operation may be done at the browser side (using bitcoinjs-lib for example) if you don’t want to send private key via network.

STEP 3: command broadcast_tx with parameter signed_tx_hash (previous command result)
Result: hash of the broadcasted transaction.

Get transactions by address

Use the command to get history of transactions for address you need.

command: get_raw_transactions
daemon: counterblockd
parameters:

address: address you looking history for
start_ts: start date & time (unix timestamp)
end_ts: end date & time (unix timestamp)
limit: maximum number of transactions to return (default 10,000)

--

--