From 76092fb1a35285ac5c4523febd274008db761d16 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Mon, 18 Aug 2014 14:34:50 -0700 Subject: [PATCH 1/4] paypro: example - allow more modularity concerning options. --- examples/PayPro/server.js | 44 ++++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/examples/PayPro/server.js b/examples/PayPro/server.js index bfcf6d7..7da6807 100755 --- a/examples/PayPro/server.js +++ b/examples/PayPro/server.js @@ -52,8 +52,6 @@ server.setOptions = function(options) { argv = options; }; -var isNode = !argv.b && !argv.browser; - var app = express(); /** @@ -409,22 +407,30 @@ function error() { server.on('request', app); server.app = app; -server.port = +argv.p || +argv.port || 8080; +server.port = 8080; +server.isNode = true; -if (argv.s) { - server.listen(server.port); - return; -} - -if (!module.parent || path.basename(module.parent.filename) === 'index.js') { - server.listen(server.port, function(addr) { - if (!isNode) return; - var customer = require('./customer'); - customer.sendPayment(function(err) { - if (err) return error(err.message); - customer.print('Payment sent successfully.'); +setTimeout(function() { + server.port = +argv.p || +argv.port || 8080; + server.isNode = !argv.b && !argv.browser; + // Arguably the same thing as -b or --browser: + if (argv.s || argv.server) { + server.listen(server.port, function(addr) { + print('Listening on port %s.', server.port); }); - }); -} else { - module.exports = server; -} + return; + } + if (!module.parent || path.basename(module.parent.filename) === 'index.js') { + server.listen(server.port, function(addr) { + print('Listening on port %s.', server.port); + if (!server.isNode) return; + var customer = require('./customer'); + customer.sendPayment(function(err) { + if (err) return error(err.message); + customer.print('Payment sent successfully.'); + }); + }); + } +}, 1); + +module.exports = server; From 774dacae8dc1d941f9554789f285c51769427da4 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Mon, 18 Aug 2014 14:44:06 -0700 Subject: [PATCH 2/4] paypro: example - document modular usage of example. --- examples/PayPro/README.md | 7 +++++++ examples/PayPro/server.js | 9 +++++---- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/examples/PayPro/README.md b/examples/PayPro/README.md index 645d632..0c5487b 100644 --- a/examples/PayPro/README.md +++ b/examples/PayPro/README.md @@ -65,3 +65,10 @@ testnet coins, you can pass `--no-tx` on the server command line. If you don't want the tests to run automatically and simply host the payment server, simply pass `--browser` (`-b`) as mentioned above. + +## Using the example in a modular manner + +``` js +var server = require('bitcore/examples/PayPro'); +server.listen(8080); +``` diff --git a/examples/PayPro/server.js b/examples/PayPro/server.js index 7da6807..5e352f1 100755 --- a/examples/PayPro/server.js +++ b/examples/PayPro/server.js @@ -48,8 +48,10 @@ var server = https.createServer({ cert: fs.readFileSync(__dirname + '/../../test/data/x509.crt') }); +server.options = argv; + server.setOptions = function(options) { - argv = options; + server.options = argv = options; }; var app = express(); @@ -411,10 +413,9 @@ server.port = 8080; server.isNode = true; setTimeout(function() { - server.port = +argv.p || +argv.port || 8080; + server.port = argv.p = argv.port = +argv.p || +argv.port || 8080; server.isNode = !argv.b && !argv.browser; - // Arguably the same thing as -b or --browser: - if (argv.s || argv.server) { + if (argv.s || argv.server || argv.l || argv.listen) { server.listen(server.port, function(addr) { print('Listening on port %s.', server.port); }); From a90e42181808efefdf4dde921f746536d69273f0 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Mon, 18 Aug 2014 15:41:26 -0700 Subject: [PATCH 3/4] paypro: example - fix customer createTX for browser. --- examples/PayPro/customer.js | 149 ++++++++++++++++++++++++++---------- 1 file changed, 109 insertions(+), 40 deletions(-) diff --git a/examples/PayPro/customer.js b/examples/PayPro/customer.js index fd7857e..d72f01a 100644 --- a/examples/PayPro/customer.js +++ b/examples/PayPro/customer.js @@ -62,45 +62,7 @@ var merchant = isNode if (isNode) { var Buffer = global.Buffer; } else { - var Buffer = function Buffer(data) { - var ab = new ArrayBuffer(data.length); - var view = new Uint8Array(ab); - data._size = data.length; - for (var i = 0; i < data._size; i++) { - view[i] = data[i]; - } - if (!view.slice) { - // view.slice = ab.slice.bind(ab); - view.slice = function(start, end) { - if (end < 0) { - end = data._size + end; - } - data._size = end - start; - var ab = new ArrayBuffer(data._size); - var view = new Uint8Array(ab); - for (var i = 0, j = start; j < end; i++, j++) { - view[i] = data[j]; - } - return view; - }; - } - return view; - }; - Buffer.byteLength = function(buf) { - var bytes = 0 - , ch; - - for (var i = 0; i < buf.length; i++) { - ch = buf.charCodeAt(i); - if (ch > 0xff) { - bytes += 2; - } else { - bytes++; - } - } - - return bytes; - }; + var Buffer = bitcore.Buffer; } function request(options, callback) { @@ -281,7 +243,11 @@ function sendPayment(msg, callback) { if (tx.buffer) { tx.buffer = tx.buffer.slice(tx.offset, tx.limit); var ptx = new bitcore.Transaction(); - ptx.parse(tx.buffer); + + var parser = new bitcore.BinaryParser(tx.buffer); + ptx.parse(parser); + // ptx.parse(tx.buffer); + tx = ptx; } var txid = tx.getHash().toString('hex'); @@ -420,6 +386,109 @@ function createTX(outputs) { return tx.serialize(); } +function createTX(outputs) { + // Addresses + var addrs = [ + 'mzTQ66VKcybz9BD1LAqEwMFp9NrBGS82sY', + 'mmu9k3KzsDMEm9JxmJmZaLhovAoRKW3zr4', + 'myqss64GNZuWuFyg5LTaoTCyWEpKH56Fgz' + ]; + + // Private keys in WIF format (see TransactionBuilder.js for other options) + var keys = [ + 'cVvr5YmWVAkVeZWAawd2djwXM4QvNuwMdCw1vFQZBM1SPFrtE8W8', + 'cPyx1hXbe3cGQcHZbW3GNSshCYZCriidQ7afR2EBsV6ReiYhSkNF' + // 'cUB9quDzq1Bj7pocenmofzNQnb1wJNZ5V3cua6pWKzNL1eQtaDqQ' + ]; + + var unspent = [{ + // http://blockexplorer.com/testnet/rawtx/1fcfe898cc2612f8b222bd3b4ac8d68bf95d43df8367b71978c184dea35bde22 + 'txid': '1fcfe898cc2612f8b222bd3b4ac8d68bf95d43df8367b71978c184dea35bde22', + 'vout': 1, + 'address': addrs[0], + 'scriptPubKey': '76a94c14cfbe41f4a518edc25af71bafc72fb61bfcfc4fcd88ac', + 'amount': 1.60000000, + 'confirmations': 9 + }, + + { + // http://blockexplorer.com/testnet/rawtx/0624c0c794447b0d2343ae3d20382983f41b915bb115a834419e679b2b13b804 + 'txid': '0624c0c794447b0d2343ae3d20382983f41b915bb115a834419e679b2b13b804', + 'vout': 1, + 'address': addrs[1], + 'scriptPubKey': '76a94c14460376539c219c5e3274d86f16b40e806b37817688ac', + 'amount': 1.60000000, + 'confirmations': 9 + }]; + + // set change address + var opts = { + remainderOut: { + address: addrs[0] + } + }; + + var outs = []; + outputs.forEach(function(output) { + var amount = output.get('amount'); + var script = { + offset: output.get('script').offset, + limit: output.get('script').limit, + buffer: new Buffer(new Uint8Array( + output.get('script').buffer)) + }; + + // big endian + var v = new Buffer(8); + v[0] = (amount.high >> 24) & 0xff; + v[1] = (amount.high >> 16) & 0xff; + v[2] = (amount.high >> 8) & 0xff; + v[3] = (amount.high >> 0) & 0xff; + v[4] = (amount.low >> 24) & 0xff; + v[5] = (amount.low >> 16) & 0xff; + v[6] = (amount.low >> 8) & 0xff; + v[7] = (amount.low >> 0) & 0xff; + + var s = script.buffer.slice(script.offset, script.limit); + var addr = bitcore.Address.fromScriptPubKey(new bitcore.Script(s), 'testnet'); + + outs.push({ + address: addr.toString(), + amountSatStr: bitcore.Bignum.fromBuffer(v, { + // XXX for some reason, endian is ALWAYS 'big' + // in node (in the browser it behaves correctly) + endian: 'big', + size: 1 + }).toString(10) + }); + }); + + var b = new bitcore.TransactionBuilder(opts) + .setUnspent(unspent) + .setOutputs(outs) + .sign(keys); + + outputs.forEach(function(output, i) { + var script = { + offset: output.get('script').offset, + limit: output.get('script').limit, + buffer: new Buffer(new Uint8Array( + output.get('script').buffer)) + }; + var s = script.buffer.slice(script.offset, script.limit); + b.tx.outs[i].s = s; + }); + + var tx = b.build(); + + print(''); + print('Customer created transaction:'); + print(tx.getStandardizedObject()); + print(''); + + return tx.serialize(); +} + /** * Helpers */ From 786c38834d5dc5cee50177a5ab0467a31eea20a4 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Mon, 18 Aug 2014 15:42:15 -0700 Subject: [PATCH 4/4] paypro: example - remove old createTX. --- examples/PayPro/customer.js | 83 ------------------------------------- 1 file changed, 83 deletions(-) diff --git a/examples/PayPro/customer.js b/examples/PayPro/customer.js index d72f01a..e11b060 100644 --- a/examples/PayPro/customer.js +++ b/examples/PayPro/customer.js @@ -303,89 +303,6 @@ function parseQS(query) { return out; } -function createTX(outputs) { - // Addresses - var addrs = [ - 'mzTQ66VKcybz9BD1LAqEwMFp9NrBGS82sY', - 'mmu9k3KzsDMEm9JxmJmZaLhovAoRKW3zr4', - 'myqss64GNZuWuFyg5LTaoTCyWEpKH56Fgz' - ]; - - // Private keys in WIF format (see TransactionBuilder.js for other options) - var keys = [ - 'cVvr5YmWVAkVeZWAawd2djwXM4QvNuwMdCw1vFQZBM1SPFrtE8W8', - 'cPyx1hXbe3cGQcHZbW3GNSshCYZCriidQ7afR2EBsV6ReiYhSkNF' - // 'cUB9quDzq1Bj7pocenmofzNQnb1wJNZ5V3cua6pWKzNL1eQtaDqQ' - ]; - - var unspent = [{ - // http://blockexplorer.com/testnet/rawtx/1fcfe898cc2612f8b222bd3b4ac8d68bf95d43df8367b71978c184dea35bde22 - 'txid': '1fcfe898cc2612f8b222bd3b4ac8d68bf95d43df8367b71978c184dea35bde22', - 'vout': 1, - 'address': addrs[0], - 'scriptPubKey': '76a94c14cfbe41f4a518edc25af71bafc72fb61bfcfc4fcd88ac', - 'amount': 1.60000000, - 'confirmations': 9 - }, - - { - // http://blockexplorer.com/testnet/rawtx/0624c0c794447b0d2343ae3d20382983f41b915bb115a834419e679b2b13b804 - 'txid': '0624c0c794447b0d2343ae3d20382983f41b915bb115a834419e679b2b13b804', - 'vout': 1, - 'address': addrs[1], - 'scriptPubKey': '76a94c14460376539c219c5e3274d86f16b40e806b37817688ac', - 'amount': 1.60000000, - 'confirmations': 9 - } - ]; - - // define transaction output - var outs = []; - outputs.forEach(function(output) { - outs.push({ - address: addrs[0], // dummy address - amount: 0 // dummy value - }); - }); - - // set change address - var opts = { - remainderOut: { - address: addrs[0] - } - }; - - var tx = new TransactionBuilder(opts) - .setUnspent(unspent) - .setOutputs(outs) - .sign(keys) - .build(); - - outputs.forEach(function(output, i) { - var value = output.get('amount'); - var script = output.get('script'); - var v = new Buffer(8); - v[0] = (value.low >> 0) & 0xff; - v[1] = (value.low >> 8) & 0xff; - v[2] = (value.low >> 16) & 0xff; - v[3] = (value.low >> 24) & 0xff; - v[4] = (value.high >> 0) & 0xff; - v[5] = (value.high >> 8) & 0xff; - v[6] = (value.high >> 16) & 0xff; - v[7] = (value.high >> 24) & 0xff; - var s = script.buffer.slice(script.offset, script.limit); - tx.outs[i].v = v; - tx.outs[i].s = s; - }); - - print(''); - print('Customer created transaction:'); - print(tx.getStandardizedObject()); - print(''); - - return tx.serialize(); -} - function createTX(outputs) { // Addresses var addrs = [