commit
d5277cce83
48
index.js
48
index.js
|
@ -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() + '/');
|
||||
|
|
|
@ -0,0 +1,232 @@
|
|||
{
|
||||
"name": "store-demo",
|
||||
"version": "1.0.0",
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
"dependencies": {
|
||||
"bitcore-lib-btcp": {
|
||||
"version": "github:BTCPrivate/bitcore-lib-btcp#3743dc0df5d67b32d45d3d41c213e45c1d6c301a",
|
||||
"requires": {
|
||||
"bn.js": "2.0.4",
|
||||
"bs58": "2.0.0",
|
||||
"buffer-compare": "1.0.0",
|
||||
"elliptic": "3.0.3",
|
||||
"inherits": "2.0.1",
|
||||
"lodash": "3.10.1"
|
||||
},
|
||||
"dependencies": {
|
||||
"bn.js": {
|
||||
"version": "2.0.4",
|
||||
"resolved": "https://registry.npmjs.org/bn.js/-/bn.js-2.0.4.tgz",
|
||||
"integrity": "sha1-Igp81nf38b+pNif/QZN3b+eBlIA="
|
||||
},
|
||||
"bs58": {
|
||||
"version": "2.0.0",
|
||||
"resolved": "https://registry.npmjs.org/bs58/-/bs58-2.0.0.tgz",
|
||||
"integrity": "sha1-crcTvtIjoKxRi72g484/SBfznrU="
|
||||
},
|
||||
"buffer-compare": {
|
||||
"version": "1.0.0",
|
||||
"resolved": "https://registry.npmjs.org/buffer-compare/-/buffer-compare-1.0.0.tgz",
|
||||
"integrity": "sha1-rKp6lm6Y7un64Usxw5pfFY+zxKI="
|
||||
},
|
||||
"elliptic": {
|
||||
"version": "3.0.3",
|
||||
"resolved": "http://registry.npmjs.org/elliptic/-/elliptic-3.0.3.tgz",
|
||||
"integrity": "sha1-hlybQgv75VAGuflp+XoNLESWZZU=",
|
||||
"requires": {
|
||||
"bn.js": "2.0.4",
|
||||
"brorand": "1.0.5",
|
||||
"hash.js": "1.0.3",
|
||||
"inherits": "2.0.1"
|
||||
},
|
||||
"dependencies": {
|
||||
"brorand": {
|
||||
"version": "1.0.5",
|
||||
"resolved": "https://registry.npmjs.org/brorand/-/brorand-1.0.5.tgz",
|
||||
"integrity": "sha1-B7VMowKGq9Fxig4qgwgD79yb+gQ="
|
||||
},
|
||||
"hash.js": {
|
||||
"version": "1.0.3",
|
||||
"resolved": "https://registry.npmjs.org/hash.js/-/hash.js-1.0.3.tgz",
|
||||
"integrity": "sha1-EzL/ABVsCg/92CNgE9B7d6BFFXM=",
|
||||
"requires": {
|
||||
"inherits": "2.0.1"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"inherits": {
|
||||
"version": "2.0.1",
|
||||
"resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.1.tgz",
|
||||
"integrity": "sha1-sX0I0ya0Qj5Wjv9xn5GwscvfafE="
|
||||
},
|
||||
"lodash": {
|
||||
"version": "3.10.1",
|
||||
"resolved": "https://registry.npmjs.org/lodash/-/lodash-3.10.1.tgz",
|
||||
"integrity": "sha1-W/Rejkm6QYnhfUgnid/RW9FAt7Y="
|
||||
}
|
||||
}
|
||||
},
|
||||
"body-parser": {
|
||||
"version": "1.18.2",
|
||||
"resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.18.2.tgz",
|
||||
"integrity": "sha1-h2eKGdhLR9hZuDGZvVm84iKxBFQ=",
|
||||
"requires": {
|
||||
"bytes": "3.0.0",
|
||||
"content-type": "1.0.4",
|
||||
"debug": "2.6.9",
|
||||
"depd": "1.1.2",
|
||||
"http-errors": "1.6.3",
|
||||
"iconv-lite": "0.4.19",
|
||||
"on-finished": "2.3.0",
|
||||
"qs": "6.5.1",
|
||||
"raw-body": "2.3.2",
|
||||
"type-is": "1.6.16"
|
||||
}
|
||||
},
|
||||
"bytes": {
|
||||
"version": "3.0.0",
|
||||
"resolved": "https://registry.npmjs.org/bytes/-/bytes-3.0.0.tgz",
|
||||
"integrity": "sha1-0ygVQE1olpn4Wk6k+odV3ROpYEg="
|
||||
},
|
||||
"content-type": {
|
||||
"version": "1.0.4",
|
||||
"resolved": "https://registry.npmjs.org/content-type/-/content-type-1.0.4.tgz",
|
||||
"integrity": "sha512-hIP3EEPs8tB9AT1L+NUqtwOAps4mk2Zob89MWXMHjHWg9milF/j4osnnQLXBCBFBk/tvIG/tUc9mOUJiPBhPXA=="
|
||||
},
|
||||
"debug": {
|
||||
"version": "2.6.9",
|
||||
"resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz",
|
||||
"integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==",
|
||||
"requires": {
|
||||
"ms": "2.0.0"
|
||||
}
|
||||
},
|
||||
"depd": {
|
||||
"version": "1.1.2",
|
||||
"resolved": "https://registry.npmjs.org/depd/-/depd-1.1.2.tgz",
|
||||
"integrity": "sha1-m81S4UwJd2PnSbJ0xDRu0uVgtak="
|
||||
},
|
||||
"ee-first": {
|
||||
"version": "1.1.1",
|
||||
"resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz",
|
||||
"integrity": "sha1-WQxhFWsK4vTwJVcyoViyZrxWsh0="
|
||||
},
|
||||
"http-errors": {
|
||||
"version": "1.6.3",
|
||||
"resolved": "https://registry.npmjs.org/http-errors/-/http-errors-1.6.3.tgz",
|
||||
"integrity": "sha1-i1VoC7S+KDoLW/TqLjhYC+HZMg0=",
|
||||
"requires": {
|
||||
"depd": "1.1.2",
|
||||
"inherits": "2.0.3",
|
||||
"setprototypeof": "1.1.0",
|
||||
"statuses": "1.5.0"
|
||||
}
|
||||
},
|
||||
"iconv-lite": {
|
||||
"version": "0.4.19",
|
||||
"resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.19.tgz",
|
||||
"integrity": "sha512-oTZqweIP51xaGPI4uPa56/Pri/480R+mo7SeU+YETByQNhDG55ycFyNLIgta9vXhILrxXDmF7ZGhqZIcuN0gJQ=="
|
||||
},
|
||||
"inherits": {
|
||||
"version": "2.0.3",
|
||||
"resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz",
|
||||
"integrity": "sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4="
|
||||
},
|
||||
"media-typer": {
|
||||
"version": "0.3.0",
|
||||
"resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz",
|
||||
"integrity": "sha1-hxDXrwqmJvj/+hzgAWhUUmMlV0g="
|
||||
},
|
||||
"mime-db": {
|
||||
"version": "1.33.0",
|
||||
"resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.33.0.tgz",
|
||||
"integrity": "sha512-BHJ/EKruNIqJf/QahvxwQZXKygOQ256myeN/Ew+THcAa5q+PjyTTMMeNQC4DZw5AwfvelsUrA6B67NKMqXDbzQ=="
|
||||
},
|
||||
"mime-types": {
|
||||
"version": "2.1.18",
|
||||
"resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.18.tgz",
|
||||
"integrity": "sha512-lc/aahn+t4/SWV/qcmumYjymLsWfN3ELhpmVuUFjgsORruuZPVSwAQryq+HHGvO/SI2KVX26bx+En+zhM8g8hQ==",
|
||||
"requires": {
|
||||
"mime-db": "1.33.0"
|
||||
}
|
||||
},
|
||||
"ms": {
|
||||
"version": "2.0.0",
|
||||
"resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
|
||||
"integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g="
|
||||
},
|
||||
"on-finished": {
|
||||
"version": "2.3.0",
|
||||
"resolved": "https://registry.npmjs.org/on-finished/-/on-finished-2.3.0.tgz",
|
||||
"integrity": "sha1-IPEzZIGwg811M3mSoWlxqi2QaUc=",
|
||||
"requires": {
|
||||
"ee-first": "1.1.1"
|
||||
}
|
||||
},
|
||||
"qs": {
|
||||
"version": "6.5.1",
|
||||
"resolved": "https://registry.npmjs.org/qs/-/qs-6.5.1.tgz",
|
||||
"integrity": "sha512-eRzhrN1WSINYCDCbrz796z37LOe3m5tmW7RQf6oBntukAG1nmovJvhnwHHRMAfeoItc1m2Hk02WER2aQ/iqs+A=="
|
||||
},
|
||||
"raw-body": {
|
||||
"version": "2.3.2",
|
||||
"resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.3.2.tgz",
|
||||
"integrity": "sha1-vNYMd9Prk83gBQKVw/N5OJvIj4k=",
|
||||
"requires": {
|
||||
"bytes": "3.0.0",
|
||||
"http-errors": "1.6.2",
|
||||
"iconv-lite": "0.4.19",
|
||||
"unpipe": "1.0.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"depd": {
|
||||
"version": "1.1.1",
|
||||
"resolved": "https://registry.npmjs.org/depd/-/depd-1.1.1.tgz",
|
||||
"integrity": "sha1-V4O04cRZ8G+lyif5kfPQbnoxA1k="
|
||||
},
|
||||
"http-errors": {
|
||||
"version": "1.6.2",
|
||||
"resolved": "https://registry.npmjs.org/http-errors/-/http-errors-1.6.2.tgz",
|
||||
"integrity": "sha1-CgAsyFcHGSp+eUbO7cERVfYOxzY=",
|
||||
"requires": {
|
||||
"depd": "1.1.1",
|
||||
"inherits": "2.0.3",
|
||||
"setprototypeof": "1.0.3",
|
||||
"statuses": "1.5.0"
|
||||
}
|
||||
},
|
||||
"setprototypeof": {
|
||||
"version": "1.0.3",
|
||||
"resolved": "https://registry.npmjs.org/setprototypeof/-/setprototypeof-1.0.3.tgz",
|
||||
"integrity": "sha1-ZlZ+NwQ+608E2RvWWMDL77VbjgQ="
|
||||
}
|
||||
}
|
||||
},
|
||||
"setprototypeof": {
|
||||
"version": "1.1.0",
|
||||
"resolved": "https://registry.npmjs.org/setprototypeof/-/setprototypeof-1.1.0.tgz",
|
||||
"integrity": "sha512-BvE/TwpZX4FXExxOxZyRGQQv651MSwmWKZGqvmPcRIjDqWub67kTKuIMx43cZZrS/cBBzwBcNDWoFxt2XEFIpQ=="
|
||||
},
|
||||
"statuses": {
|
||||
"version": "1.5.0",
|
||||
"resolved": "https://registry.npmjs.org/statuses/-/statuses-1.5.0.tgz",
|
||||
"integrity": "sha1-Fhx9rBd2Wf2YEfQ3cfqZOBR4Yow="
|
||||
},
|
||||
"type-is": {
|
||||
"version": "1.6.16",
|
||||
"resolved": "https://registry.npmjs.org/type-is/-/type-is-1.6.16.tgz",
|
||||
"integrity": "sha512-HRkVv/5qY2G6I8iab9cI7v1bOIdhm94dVjQCPFElW9W+3GeDOSHmy2EBYe4VTApuzolPcmgFTN3ftVJRKR2J9Q==",
|
||||
"requires": {
|
||||
"media-typer": "0.3.0",
|
||||
"mime-types": "2.1.18"
|
||||
}
|
||||
},
|
||||
"unpipe": {
|
||||
"version": "1.0.0",
|
||||
"resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz",
|
||||
"integrity": "sha1-sr9O6FFKrmFltIF4KdIbLvSZBOw="
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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>
|
||||
|
||||
|
|
Loading…
Reference in New Issue