Skip to content

Instantly share code, notes, and snippets.

@ryandotsmith
Last active August 29, 2015 14:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryandotsmith/7e009f5633f27061936a to your computer and use it in GitHub Desktop.
Save ryandotsmith/7e009f5633f27061936a to your computer and use it in GitHub Desktop.
Chain - Send Transactions API
var chain = require('chain-node');
var bitcoin = require('bitcoinjs-lib');
// The Chain API will never accept your private key.
// Keep the private key stored in a safe place alongside
// your program.
var key = new bitcoin.ECKey.fromWIF("Your private key in WIF format.")
var txn = new bitcoin.Transaction()
// We use a previous transaction where our private key
// can authorize the use of its output at index 1.
txn.addInput("Previous transaction hash.", 1);
// Specify the address hash of where our bitcoin should go.
// We are sending 10,000 Satoshi to a friend.
txn.addOutput("The receiver's address.", 10000)
// Assume we have 100,000 Satoshi to use as a part of our
// Previous transactions has at index 1.
// Then we are making change for ourselves and leaving 10,000
// Satoshi as a mining fee.
txn.addOutput("Your address.", 80000)
// Finally we sign our transaction using a private key that is
// stored safely along side this program. The Chain API will
// never read your private key.
txn.sign(0, key)
// Once we have created the transaction. Sending it to
// the Chain API is as simple as a single function call.
chain.sendTransaction(txn.serializeHex(), function(err, resp) {
console.log('Error: ' + err);
console.log('Resp: ' + resp.message);
});
{
"description": "Chain - Send Transaction API Example",
"dependencies": {
"bitcoinjs-lib": "1.1.1",
"chain-node": "0.0.17"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment