Sample of using bitcoinjs-lib with Counterparty

For any operation, which requires push data into blockchain, you need full set of cryptographic tools. Here we’ll show how to use bitcoinjs-lib to sign transactions and some other commands at browser’s side.


Create wallet

Keys may be generated and sent to user or user could provide his own key.

var key = Bitcoin.ECKey.makeRandom();
var pk = key.toWIF(); // Private Key
var address = key.pub.getAddress();
address.version = ADDR_VERSION; // 0x00 — mainnet, 0x6F — testnet
// address.toString() — User’s address

Get existing address

Private key is not required for authorization, but should be provided by the user for transaction sign.

try{ var addr = Bitcoin.Address.fromBase58Check(address);
}catch(e){ // Invalid address
}

Create asset

Three steps required:

Step 1: create asset with counterpartyd’s RPC

method: create_issuance
params: { “asset”: “asset name”, “source”: “issuer address”, “quantity”: issue in satoshis, “allow_unconfirmed_inputs”: true, “encoding”: “multisig”, “pubkey”: “public key for issuer address”
}
Result: hex-data of unsigned transaction.

Step 2: sign the transaction with bitcoinjs-lib

Private key provided by user.

var key = Bitcoin.ECKey.fromWIF(‘private key’);
var tx=Bitcoin.Transaction.fromHex(‘HEX-data from step 1’);
tx.sign(0, key);
tx.toHex(); // HEX-data of signed transation
Result: hex-data of signed transaction.

Step 3: push transaction into blockchain

Send command using RPC of counterpartyd.

method: broadcast_tx
params:{ “signed_tx_hex”:”HEX представление подписаной транзакции”
}
Result: hash of pushed transaction.

Make sure that issuer address have at least 0.5XCP at balance. This is the fee for asset issuance. You may also issue asset with names like AXXXXXXX (where XXX are numbers) without fee.