Skip to content

Instantly share code, notes, and snippets.

@runeksvendsen
Last active July 1, 2016 09:22
Show Gist options
  • Save runeksvendsen/d395a687d5f7da47b5df065a932e1e11 to your computer and use it in GitHub Desktop.
Save runeksvendsen/d395a687d5f7da47b5df065a932e1e11 to your computer and use it in GitHub Desktop.
var bitcoin = require('bitcoinjs-lib');
var keyPair = bitcoin.ECPair.makeRandom( { compressed: true } ); // create new key pair to be used for signing payment channel payments
var chanState = new PaymentChannel("https://paychan.runeks.me", keyPair, expTime);
chanState.getFundingInfo(function (fundingAddress, openPrice, fundingTxMinConf) {
// pay to 'fundingAddress' here
// wait for 'fundingTxMinConf' confirmations before proceeding to open step
});
var fi = blockchain_GetTxInfoFromAddress(fundingAddress) // get information about the transaction paying to the funding address
chanState.setFundingSource( fi.txid, fi.vout, fi.value ); // register this information with the state object
var refundAddress = "1MmWtRnvdayQqVaRdpnsGNesCch7eptZnP";
var txHexStr = chanState.getRefundTx(refundAddress, fee).toHex(); //create a refund transaction, if desired
chanState.openChannel(refundAddress, function (payConn, payInfo) {
var amount = 10; // satoshi
function paymentLoop (numPaymentsLeft, res) {
if ((res.channel_status === "open") && (numPaymentsLeft > 0)) {
payConn.makePayment(amount, paymentLoop.bind(null, numPaymentsLeft - 1));
}
}
// make first payment, then go into loop
payConn.makePayment(amount, paymentLoop.bind(null, 1000));
payConn.deleteChannel(callback);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment