From 076dab2a4782d1bd8049e450fc576ed6d6a40096 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Wed, 13 Aug 2014 17:11:23 -0400 Subject: [PATCH 1/9] paypro: example - use strict paypro headers. --- examples/PayPro/server.js | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/examples/PayPro/server.js b/examples/PayPro/server.js index 86486bd..e47b143 100755 --- a/examples/PayPro/server.js +++ b/examples/PayPro/server.js @@ -71,12 +71,18 @@ app.use(function(req, res, next) { }; res.setHeader('Access-Control-Allow-Origin', '*'); - - if (req.method === 'OPTIONS') { - res.setHeader('Access-Control-Allow-Methods', 'GET,POST'); - res.setHeader('Access-Control-Allow-Headers', req.headers['access-control-request-headers']); - return res.send(200); - } + res.setHeader('Access-Control-Allow-Methods', 'GET,POST'); + res.setHeader('Access-Control-Allow-Headers', [ + 'Host', + 'Connection', + 'Content-Length', + 'Accept', + 'Origin', + 'User-Agent', + 'Content-Type', + 'Accept-Encoding', + 'Accept-Language' + ].join(',')); res.setHeader('Accept', PayPro.PAYMENT_CONTENT_TYPE); From f972baf42c9e49e940c1270517fb2a35b2d1b1d0 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Mon, 18 Aug 2014 13:05:58 -0700 Subject: [PATCH 2/9] paypro: fix readme file for better explanation. --- examples/PayPro/README.md | 41 ++++++++++++++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/examples/PayPro/README.md b/examples/PayPro/README.md index 802ae93..889e990 100644 --- a/examples/PayPro/README.md +++ b/examples/PayPro/README.md @@ -1,11 +1,42 @@ # Running the Payment Protocol Demo -This is an example of Bitcore's Payment Protocol implementation, including a -mocked server (`server.js`) and client (`customer.js`). -1. Start the server: `node server.js` -2. Start the customer: `node customer.js` +## Node -At this point, you should see an acknowledgement from your local server: +The node payment protocol demonstration will run automatically via: + +``` bash +$ node examples/PayPro` +``` + +You will see the server and customer logs output in the terminal. + +## Browser + +To run our payment protocol demonstration in the browser, you may run: + +``` bash +$ node examples/PayPro/server.js -b -p 8080 +``` + +This will start the payment protocol demonstration server which serves outputs +in the payment protocol request (which don't ask for *too* many testnet coins). + +Once the server is started, you can visit it in your browser: + +``` bash +$ chromium https://localhost:8080/ +``` + +You will see a simple checkout page to buy some imaginary products. Once you +press checkout, you will see all the server and client logs in the browser as +well as the terminal. + +If you're connected to enough peers, your transaction will be broadcast +throughout the bitcoin testnet network and hopefully ACKed by your peers. + +## Logs + +Your logs may ultimately look something like this: ``` Customer: Our payment was acknowledged! From bd227930e196ee5d7e680ebbb871e94d283780d7 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Mon, 18 Aug 2014 13:06:33 -0700 Subject: [PATCH 3/9] paypro: example - add extra arguments for experimentation. --- examples/PayPro/server.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/examples/PayPro/server.js b/examples/PayPro/server.js index e47b143..befdeb3 100755 --- a/examples/PayPro/server.js +++ b/examples/PayPro/server.js @@ -288,7 +288,10 @@ app.post('/-/pay', function(req, res, next) { }); print('Broadcasting transaction...'); - conn.sendTx(tx); + + if (!argv['no-tx']) { + conn.sendTx(tx); + } }); } else { print('No BTC network connection. Retrying...'); @@ -311,7 +314,7 @@ var peerman = new bitcore.PeerManager({ network: 'testnet' }); -peerman.peerDiscovery = false; +peerman.peerDiscovery = argv.d || argv.discovery || false; peerman.addPeer(new bitcore.Peer('testnet-seed.alexykot.me', 18333)); peerman.addPeer(new bitcore.Peer('testnet-seed.bitcoin.petertodd.org', 18333)); From cc3ca4e571ff24f9eccea0da1bafd9024b04ad47 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Mon, 18 Aug 2014 13:36:04 -0700 Subject: [PATCH 4/9] paypro: example - explain extra arguments in readme file. --- examples/PayPro/README.md | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/examples/PayPro/README.md b/examples/PayPro/README.md index 889e990..f73e66c 100644 --- a/examples/PayPro/README.md +++ b/examples/PayPro/README.md @@ -18,8 +18,9 @@ To run our payment protocol demonstration in the browser, you may run: $ node examples/PayPro/server.js -b -p 8080 ``` -This will start the payment protocol demonstration server which serves outputs -in the payment protocol request (which don't ask for *too* many testnet coins). +This will start the payment protocol demonstration server in browser mode, +which serves outputs in the payment protocol request (don't worry, it doesn't +ask for *too* many testnet coins). Once the server is started, you can visit it in your browser: @@ -43,3 +44,18 @@ Customer: Our payment was acknowledged! Customer: Message from Merchant: Thank you for your payment! Customer: Payment sent successfully. ``` + +## Changing the server address in outputs + +## Other Options + +If you you're not connected to enough peers to broadcast your transaction (by +default, this example only connects to the core seed peers), you can enable +peer discovery in bitcore by passing the `--discovery` (`-d`) argument onto the +server command line. + +If you don't want to actually broadcast your transaction and want to keep your +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. From e93e3d83f4883b7661eacee2fdcb731780117a94 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Mon, 18 Aug 2014 13:39:57 -0700 Subject: [PATCH 5/9] paypro: example - add --address and --pubkey options. document in readme. --- examples/PayPro/README.md | 7 +++- examples/PayPro/server.js | 69 +++++++++++++++++++++++---------------- 2 files changed, 47 insertions(+), 29 deletions(-) diff --git a/examples/PayPro/README.md b/examples/PayPro/README.md index f73e66c..3c38f8c 100644 --- a/examples/PayPro/README.md +++ b/examples/PayPro/README.md @@ -45,7 +45,12 @@ Customer: Message from Merchant: Thank you for your payment! Customer: Payment sent successfully. ``` -## Changing the server address in outputs +## Changing the server address contained in outputs + +If you want to alter the address or public key the testnet coins get sent to by +the payment server, you can pass in the `--pubkey` or `--address` options. +`address` has to be a testnet address, whereas `pubkey` is a hex encoded public +key. ## Other Options diff --git a/examples/PayPro/server.js b/examples/PayPro/server.js index befdeb3..52aa658 100755 --- a/examples/PayPro/server.js +++ b/examples/PayPro/server.js @@ -141,34 +141,47 @@ app.get('/-/request', function(req, res, next) { // number of satoshis to be paid po.set('amount', value); // a TxOut script where the payment should be sent. similar to OP_CHECKSIG - po.set('script', new Buffer([ - 118, // OP_DUP - 169, // OP_HASH160 - 76, // OP_PUSHDATA1 - 20, // number of bytes - 55, - 48, - 254, - 188, - 186, - 4, - 186, - 208, - 205, - 71, - 108, - 251, - 130, - 15, - 156, - 55, - 215, - 70, - 111, - 217, - 136, // OP_EQUALVERIFY - 172 // OP_CHECKSIG - ])); + if (argv.pubkey || argv.address) { + var pubKey; + if (argv.address) { + pubKey = bitcore.Base58Check.decode(new Buffer(argv.address)); + } else { + pubKey = new Buffer(argv.pubkey, 'hex'); + } + var pubKeyHash = bitcore.util.sha256ripe160(pubKey); + var address = new bitcore.Address(pubKeyHash, 'testnet'); + var scriptPubKey = addr.getScriptPubKey(); + po.set('script', scriptPubKey.getBuffer()); + } else { + po.set('script', new Buffer([ + 118, // OP_DUP + 169, // OP_HASH160 + 76, // OP_PUSHDATA1 + 20, // number of bytes + 55, + 48, + 254, + 188, + 186, + 4, + 186, + 208, + 205, + 71, + 108, + 251, + 130, + 15, + 156, + 55, + 215, + 70, + 111, + 217, + 136, // OP_EQUALVERIFY + 172 // OP_CHECKSIG + ])); + } outputs.push(po.message); }); From 253d66994cdc43de5558fd12a68aab5c8865909b Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Mon, 18 Aug 2014 14:00:43 -0700 Subject: [PATCH 6/9] paypro: example - add privkey option. refactor. --- examples/PayPro/README.md | 3 ++- examples/PayPro/server.js | 33 +++++++++++++++++++++++++-------- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/examples/PayPro/README.md b/examples/PayPro/README.md index 3c38f8c..006185f 100644 --- a/examples/PayPro/README.md +++ b/examples/PayPro/README.md @@ -50,7 +50,8 @@ Customer: Payment sent successfully. If you want to alter the address or public key the testnet coins get sent to by the payment server, you can pass in the `--pubkey` or `--address` options. `address` has to be a testnet address, whereas `pubkey` is a hex encoded public -key. +key. The `--privkey` option is also available in the standard bitcoind privkey +format. ## Other Options diff --git a/examples/PayPro/server.js b/examples/PayPro/server.js index 52aa658..054c4f4 100755 --- a/examples/PayPro/server.js +++ b/examples/PayPro/server.js @@ -15,6 +15,7 @@ var fs = require('fs'); var path = require('path'); var qs = require('querystring'); var crypto = require('crypto'); +var assert = require('assert'); // Disable strictSSL process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0'; @@ -71,7 +72,7 @@ app.use(function(req, res, next) { }; res.setHeader('Access-Control-Allow-Origin', '*'); - res.setHeader('Access-Control-Allow-Methods', 'GET,POST'); + res.setHeader('Access-Control-Allow-Methods', 'GET,POST,OPTIONS'); res.setHeader('Access-Control-Allow-Headers', [ 'Host', 'Connection', @@ -138,19 +139,34 @@ app.get('/-/request', function(req, res, next) { [2000, 1000, 10000].forEach(function(value) { var po = new PayPro(); po = po.makeOutput(); + // number of satoshis to be paid po.set('amount', value); + // a TxOut script where the payment should be sent. similar to OP_CHECKSIG - if (argv.pubkey || argv.address) { + + // Instead of creating it ourselves: + // if (!argv.pubkey && !argv.privkey && !argv.address) { + // argv.pubkey = '3730febcba04bad0cd476cfb820f9c37d7466fd9'; + // } + + if (argv.pubkey || argv.privkey || argv.address) { var pubKey; - if (argv.address) { - pubKey = bitcore.Base58Check.decode(new Buffer(argv.address)); - } else { + if (argv.pubkey) { pubKey = new Buffer(argv.pubkey, 'hex'); + // If it were possible: + // pubKey = bitcore.Script.fromCompressedPubKey(new Buffer(argv.pubkey)).toCompressedPubKey(); + } else if (argv.privkey) { + pubKey = bitcore.Key.recoverPubKey(new Buffer(argv.privkey)).toCompressedPubKey(); + } else if (argv.address) { + pubKey = bitcore.Base58Check.decode(new Buffer(argv.address)); + // pubKey = bitcore.Script.fromUncompressedPubKey(pubKey).toCompressedPubKey(); } - var pubKeyHash = bitcore.util.sha256ripe160(pubKey); - var address = new bitcore.Address(pubKeyHash, 'testnet'); - var scriptPubKey = addr.getScriptPubKey(); + // var pubKeyHash = bitcore.util.sha256ripe160(pubKey); + // var address = new bitcore.Address(111, pubKeyHash); + var address = bitcore.Address.fromPubKey(pubKey, 'testnet'); + var scriptPubKey = address.getScriptPubKey(); + assert.equal(scriptPubKey.isPubkeyHash(), true); po.set('script', scriptPubKey.getBuffer()); } else { po.set('script', new Buffer([ @@ -182,6 +198,7 @@ app.get('/-/request', function(req, res, next) { 172 // OP_CHECKSIG ])); } + outputs.push(po.message); }); From 1e57a6d8742531383c13b23951e446c7f194edfb Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Mon, 18 Aug 2014 14:08:04 -0700 Subject: [PATCH 7/9] paypro: example - remove some comments. --- examples/PayPro/README.md | 2 +- examples/PayPro/server.js | 8 ++------ 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/examples/PayPro/README.md b/examples/PayPro/README.md index 006185f..645d632 100644 --- a/examples/PayPro/README.md +++ b/examples/PayPro/README.md @@ -5,7 +5,7 @@ The node payment protocol demonstration will run automatically via: ``` bash -$ node examples/PayPro` +$ node examples/PayPro ``` You will see the server and customer logs output in the terminal. diff --git a/examples/PayPro/server.js b/examples/PayPro/server.js index 054c4f4..83b5d9e 100755 --- a/examples/PayPro/server.js +++ b/examples/PayPro/server.js @@ -147,23 +147,19 @@ app.get('/-/request', function(req, res, next) { // Instead of creating it ourselves: // if (!argv.pubkey && !argv.privkey && !argv.address) { - // argv.pubkey = '3730febcba04bad0cd476cfb820f9c37d7466fd9'; + // //argv.pubkey = '3730febcba04bad0cd476cfb820f9c37d7466fd9'; + // argv.pubkey = 'd96f46d7379c0f82fb6c47cdd0ba04babcfe3037' // } if (argv.pubkey || argv.privkey || argv.address) { var pubKey; if (argv.pubkey) { pubKey = new Buffer(argv.pubkey, 'hex'); - // If it were possible: - // pubKey = bitcore.Script.fromCompressedPubKey(new Buffer(argv.pubkey)).toCompressedPubKey(); } else if (argv.privkey) { pubKey = bitcore.Key.recoverPubKey(new Buffer(argv.privkey)).toCompressedPubKey(); } else if (argv.address) { pubKey = bitcore.Base58Check.decode(new Buffer(argv.address)); - // pubKey = bitcore.Script.fromUncompressedPubKey(pubKey).toCompressedPubKey(); } - // var pubKeyHash = bitcore.util.sha256ripe160(pubKey); - // var address = new bitcore.Address(111, pubKeyHash); var address = bitcore.Address.fromPubKey(pubKey, 'testnet'); var scriptPubKey = address.getScriptPubKey(); assert.equal(scriptPubKey.isPubkeyHash(), true); From bf77041f1de5fbdd1e8605bbd546cada3ff37bc1 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Mon, 18 Aug 2014 14:22:09 -0700 Subject: [PATCH 8/9] paypro: example - fix request method. --- examples/PayPro/customer.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/PayPro/customer.js b/examples/PayPro/customer.js index dfb256b..fd7857e 100644 --- a/examples/PayPro/customer.js +++ b/examples/PayPro/customer.js @@ -153,7 +153,7 @@ function sendPayment(msg, callback) { } return request({ - method: 'POST', + method: 'GET', uri: 'https://localhost:' + port + '/-/request', headers: { 'Accept': PayPro.PAYMENT_REQUEST_CONTENT_TYPE From da36abcc1e88fd39ccc70c10c31584df67bf797d Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Mon, 18 Aug 2014 14:25:50 -0700 Subject: [PATCH 9/9] paypro: example - allow user to set options when using example as a module. --- examples/PayPro/server.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/examples/PayPro/server.js b/examples/PayPro/server.js index 83b5d9e..bfcf6d7 100755 --- a/examples/PayPro/server.js +++ b/examples/PayPro/server.js @@ -36,10 +36,6 @@ var TransactionBuilder = bitcore.TransactionBuilder; * Variables */ -var isNode = !argv.b && !argv.browser; - -var app = express(); - var x509 = { priv: fs.readFileSync(__dirname + '/../../test/data/x509.key'), pub: fs.readFileSync(__dirname + '/../../test/data/x509.pub'), @@ -52,6 +48,14 @@ var server = https.createServer({ cert: fs.readFileSync(__dirname + '/../../test/data/x509.crt') }); +server.setOptions = function(options) { + argv = options; +}; + +var isNode = !argv.b && !argv.browser; + +var app = express(); + /** * Ignore Cache Headers * Allow CORS