Updates for Bitcore v4

This commit is contained in:
Braydon Fuller 2016-05-19 13:34:33 -04:00
parent 201b194eb6
commit 21d916dff1
11 changed files with 189 additions and 79 deletions

3
.bowerrc Normal file
View File

@ -0,0 +1,3 @@
{
"directory": "static/js"
}

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
static/js
node_modules

View File

@ -1 +1 @@
# lemonade-stand
# Lemonade Stand

28
bower.json Normal file
View File

@ -0,0 +1,28 @@
{
"name": "lemonade-stand",
"main": "index.js",
"version": "1.0.0",
"homepage": "https://github.com/bitpay/lemonade-stand",
"authors": [
"BitPay"
],
"description": "Tutorial service for Bitcore",
"keywords": [
"bitcore"
],
"license": "MIT",
"private": true,
"ignore": [
"**/.*",
"node_modules",
"bower_components",
"static/js",
"test",
"tests"
],
"dependencies": {
"bitcore-lib": "~0.13.15",
"jquery": "~2.2.3",
"jquery-qrcode": "*"
}
}

72
index.js Normal file
View File

@ -0,0 +1,72 @@
'use strict';
var EventEmitter = require('events').EventEmitter;
var fs = require('fs');
var bitcore = require('bitcore-lib');
var bodyParser = require('body-parser');
function LemonadeStand(options) {
EventEmitter.call(this);
this.node = options.node;
this.log = this.node.log;
this.invoiceHtml = fs.readFileSync(__dirname + '/invoice.html', 'utf8');
this.amount = 12340000;
// Use 1 HD Private Key and generate a unique address for every invoice
this.hdPrivateKey = new bitcore.HDPrivateKey(this.node.network);
this.log.info('Using key:', this.hdPrivateKey);
this.addressIndex = 0;
}
LemonadeStand.dependencies = ['bitcoind'];
LemonadeStand.prototype.start = function(callback) {
setImmediate(callback);
};
LemonadeStand.prototype.stop = function(callback) {
setImmediate(callback);
};
LemonadeStand.prototype.getAPIMethods = function() {
return [];
};
LemonadeStand.prototype.getPublishEvents = function() {
return [];
};
LemonadeStand.prototype.setupRoutes = function(app, express) {
var self = this;
app.use(bodyParser.urlencoded({extended: true}));
app.use('/', express.static(__dirname + '/static'));
app.post('/invoice', function(req, res, next) {
self.addressIndex++;
self.amount = parseFloat(req.body.amount) * 1e8;
res.status(200).send(self.filterInvoiceHTML());
});
};
LemonadeStand.prototype.getRoutePrefix = function() {
return 'lemonade-stand';
};
LemonadeStand.prototype.filterInvoiceHTML = function() {
var btc = this.amount / 1e8;
var address = this.hdPrivateKey.derive(this.addressIndex).privateKey.toAddress();
this.log.info('New invoice with address:', address);
var hash = address.hashBuffer.toString('hex');
var transformed = this.invoiceHtml
.replace(/{{amount}}/g, btc)
.replace(/{{address}}/g, address)
.replace(/{{hash}}/g, hash)
.replace(/{{baseUrl}}/g, '/' + this.getRoutePrefix() + '/');
return transformed;
};
module.exports = LemonadeStand;

40
invoice.html Normal file
View File

@ -0,0 +1,40 @@
<!DOCTYPE html>
<html>
<head>
<title>Lemonade Stand</title>
<base href="{{baseUrl}}" />
<script src="/socket.io/socket.io.js"></script>
<script src="js/jquery/dist/jquery.js"></script>
<script src="js/jquery-qrcode/jquery.qrcode.min.js"></script>
<script src="js/bitcore-lib/bitcore-lib.js"></script>
</head>
<body>
<h1>Lemonade Stand</h1>
<h2>Invoice</h2>
<div id="qrcode"></div>
<p>Please send {{amount}} BTC to {{address}}</p>
<h2>Transactions Received</h2>
<ul id="txids">
</ul>
<script type="text/javascript">
$('#qrcode').qrcode("bitcoin:{{address}}?amount={{amount}}");
</script>
<script language="javascript">
var bitcore = require('bitcore-lib');
var socket = io('http://localhost:4001');
socket.on('bitcoind/addresstxid', function(data) {
var address = bitcore.Address(data.address);
if (address.toString() == '{{address}}') {
var txidsElm = document.getElementById('txids');
var elm = document.createTextNode('txid: ' + data.txid);
txidsElm.appendChild(elm);
}
});
socket.emit('subscribe', 'bitcoind/addresstxid', ['{{address}}']);
</script>
</body>
</html>

27
package.json Normal file
View File

@ -0,0 +1,27 @@
{
"dependencies": {
"bitcore-lib": "^0.13.15",
"body-parser": "^1.15.1"
},
"name": "lemonade-stand",
"description": "Tutorial service for Bitcore",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/bitpay/lemonade-stand.git"
},
"keywords": [
"bitcore"
],
"author": "BitPay",
"license": "MIT",
"bugs": {
"url": "https://github.com/bitpay/lemonade-stand/issues"
},
"homepage": "https://github.com/bitpay/lemonade-stand#readme",
"private": true
}

View File

@ -1,31 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<title>Payment Processor</title>
<script src="/socket.io/socket.io.js"></script>
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.qrcode/1.0/jquery.qrcode.min.js"></script>
</head>
<body>
<h1>Payment Processor</h1>
<div id="qrcode"></div>
<p>Please send {{amount}} BTC to {{address}}</p>
<h2>Total Received</h2>
<p><span id="total-{{hash}}">0</span> BTC</p>
<script type="text/javascript">
$('#qrcode').qrcode("bitcoin:{{address}}?amount={{amount}}");
</script>
<script language="javascript">
var socket = io('http://localhost:3001');
socket.on('address/balance', function(addressObj, balance) {
// bitcore's address.toObject() doesn't include the actual address, so we have to use the hash
document.getElementById('total-' + addressObj.hash).innerHTML = (balance / 1e8);
});
socket.emit('subscribe', 'address/balance', ['{{address}}']);
</script>
</body>
</html>

View File

@ -1,43 +0,0 @@
var inherits = require('util').inherits;
var Service = require('../../node_modules/bitcore-node/lib/service');
var fs = require('fs');
var bitcore = require('bitcore');
var log;
function PaymentProcessor(options) {
Service.call(this, options);
log = this.node.log;
this.html = fs.readFileSync(__dirname + '/index.html', 'utf8');
this.amount = 12340000;
// Use 1 HD Private Key and generate a unique address for every invoice
this.hdPrivateKey = new bitcore.HDPrivateKey(this.node.network);
this.addressIndex = 0;
}
inherits(PaymentProcessor, Service);
PaymentProcessor.dependencies = ['bitcoind', 'db', 'address'];
PaymentProcessor.prototype.setupRoutes = function(app, express) {
var self = this;
app.get('/', function(req, res, next) {
res.status(200).send(self.filterHTML());
self.addressIndex++;
});
};
PaymentProcessor.prototype.filterHTML = function() {
var btc = this.amount / 1e8;
var address = this.hdPrivateKey.derive(this.addressIndex).privateKey.toAddress();
var hash = address.hashBuffer.toString('hex');
var transformed = this.html
.replace(/{{amount}}/g, btc)
.replace(/{{address}}/g, address)
.replace(/{{hash}}/g, hash);
return transformed;
};
module.exports = PaymentProcessor;

View File

@ -1,4 +0,0 @@
{
"dependencies": {
}
}

16
static/index.html Normal file
View File

@ -0,0 +1,16 @@
<!DOCTYPE html>
<html>
<head>
<title>Lemonade Stand</title>
</head>
<body>
<h1>Lemonade Stand</h1>
<h2>Invoice</h2>
<form method="post" action="invoice">
Amount: <input type="text" name="amount"/> BTC <input type="submit" value="Generate Invoice" />
</form>
</body>
</html>