Improved instructions, usage considerations

This commit is contained in:
Jon Layton 2018-04-12 00:50:20 -05:00
parent e85af2f478
commit e6ce213d86
2 changed files with 41 additions and 9 deletions

View File

@ -12,12 +12,19 @@ function LemonadeStand(options) {
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.price = 12340000; // (sats)
// Use a HD Private Key and generate a unique address for every invoice
//TODO generate/display once, and from then on, load same xprv ('merchant master private key') from an encrypted db
this.hdPrivateKey = new bitcore.HDPrivateKey(this.node.network);
this.log.info('Using key:', this.hdPrivateKey);
//TODO reload this from db.lastAddressIndex
this.addressIndex = 0;
//TODO implement item id's for multiple items; could use same xprv for all
this.log.info('xprv key:', this.hdPrivateKey);
this.log.info('addressIndex:', this.addressIndex);
}
LemonadeStand.dependencies = ['bitcoind'];
@ -38,6 +45,22 @@ LemonadeStand.prototype.getPublishEvents = function() {
return [];
};
//TODO Implement State Machine: AWAITING_PAYMENT -> FULL_AMOUNT_RECEIVED / TIMED_OUT / PARTIAL_AMOUNT_RECEIVED
//TODO Poll on all not-completed (timed_out, full_amount_received) addresses (between 0 -> lastAddressIndex)
/* we need to also listen, just like invoice.html -
var socket = io('http://localhost:8001');
socket.emit('subscribe', 'bitcoind/addresstxid', ['{{address}}']);
socket.on('bitcoind/addresstxid', function(data) {
var address = bitcore.Address(data.address);
if (address.toString() == '{{address}}') {
//TODO save an entry in db for each confirmed payment, for each relevant addr
//db.save({index: index, txid: txid, fromAddress, amount, time})
...
*/
LemonadeStand.prototype.setupRoutes = function(app, express) {
var self = this;
@ -45,9 +68,17 @@ LemonadeStand.prototype.setupRoutes = function(app, express) {
app.use('/', express.static(__dirname + '/static'));
// This module will be installed as a service of Bitcore, which will be running on 8001.
// TO USE (Generate an invoice) -
// POST localhost:8001/invoice {amount: 100}
// (or visit localhost:8001)
app.post('/invoice', function(req, res, next) {
self.addressIndex++;
self.amount = parseFloat(req.body.amount) * 1e8;
//TODO db.increment(lastAddressIndex)
//convert from btcp to sats (for static/index.html demo):
//self.price = parseFloat(req.body.price) * 1e8;
res.status(200).send(self.filterInvoiceHTML());
});
};
@ -57,12 +88,13 @@ LemonadeStand.prototype.getRoutePrefix = function() {
};
LemonadeStand.prototype.filterInvoiceHTML = function() {
var btc = this.amount / 1e8;
var btcp = this.price / 1e8;
var address = this.hdPrivateKey.derive(this.addressIndex).privateKey.toAddress();
this.log.info('New invoice with address:', address);
var hash = address.hashBuffer.toString('hex');
this.log.info('New invoice, with generated address:', address);
var hash = address.hashBuffer.toString('hex'); //TODO shouldn't need this
var transformed = this.invoiceHtml
.replace(/{{amount}}/g, btc)
.replace(/{{price}}/g, btcp)
.replace(/{{address}}/g, address)
.replace(/{{hash}}/g, hash)
.replace(/{{baseUrl}}/g, '/' + this.getRoutePrefix() + '/');

View File

@ -9,7 +9,7 @@
<h2>Invoice</h2>
<form method="post" action="invoice">
Amount: <input type="text" name="amount"/> BTCP <input type="submit" value="Generate Invoice" />
Amount: <input type="text" name="price"/> BTCP <input type="submit" value="Generate Invoice" />
</form>
</body>