Skip to content

Commit

Permalink
Use secp256k1-node v3.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
fanatid committed Jan 25, 2016
1 parent 4a6e15d commit 920ac8b
Show file tree
Hide file tree
Showing 18 changed files with 327 additions and 562 deletions.
105 changes: 2 additions & 103 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,109 +89,8 @@ identity.

## Examples

Example server

```javascript
var express = require('express');
var bodyParser = require('body-parser');
var rawBody = require('../lib/middleware/rawbody');
var bitauth = require('../lib/middleware/bitauth');

var users = {
'Tf7UNQnxB8SccfoyZScQmb34V2GdEtQkzDz': {name: 'Alice'},
'Tf22EUFxHWh4wmA3sDuw151W5C5g32jgph2': {name: 'Bob'}
};

var pizzas = [];

var app = express();
app.use(rawBody);
app.use(bodyParser());


app.get('/user', bitauth, function(req, res) {
if(!req.sin || !users[req.sin]) return res.send(401, {error: 'Unauthorized'});
res.send(200, users[req.sin]);
});

app.post('/pizzas', bitauth, function(req, res) {
if(!req.sin || !users[req.sin]) return res.send(401, {error: 'Unauthorized'});
var pizza = req.body;
pizza.owner = users[req.sin].name;
pizzas.push(pizza);
res.send(200, req.body);
});

app.get('/pizzas', function(req, res) {
res.send(200, pizzas);
});

app.listen(3000);
```

Example client

```javascript
var request = require('request');
var bitauth = require('../lib/bitauth');

// These can be generated with bitauth.generateSin()
var keys = {
alice: '38f93bdda21a5c4a7bae4eb75bb7811cbc3eb627176805c1009ff2099263c6ad',
bob: '09880c962437080d72f72c8c63a69efd65d086c9e7851a87b76373eb6ce9aab5'
};

// GET

for(k in keys) {
var url = 'http://localhost:3000/user';
var dataToSign = url;
var options = {
url: url,
headers: {
'x-identity': bitauth.getPublicKeyFromPrivateKey(keys[k]),
'x-signature': bitauth.sign(dataToSign, keys[k])
}
};

request.get(options, function(err, response, body) {
if(err) {
console.log(err);
}
if(body) {
console.log(body);
}
});
}

var pizzas = ['pepperoni', 'sausage', 'veggie', 'hawaiian'];

// POST

for(k in keys) {
var url = 'http://localhost:3000/pizzas';
var data = {type: pizzas[Math.floor(Math.random() * pizzas.length)]};
var dataToSign = url + JSON.stringify(data);
var options = {
url: url,
headers: {
'x-identity': bitauth.getPublicKeyFromPrivateKey(keys[k]),
'x-signature': bitauth.sign(dataToSign, keys[k])
},
json: data
};

request.post(options, function(err, response, body) {
if(err) {
console.log(err);
}
if(body) {
console.log(body);
}
});
}

```
* [server](https://github.com/bitpay/bitauth/blob/master/examples/server.js)
* [client](https://github.com/bitpay/bitauth/blob/master/examples/client.js)

## Middleware
BitAuth exposes a connect middleware for use in connect or ExpressJS applications. Use:
Expand Down
29 changes: 13 additions & 16 deletions benchmarks/index.js
Original file line number Diff line number Diff line change
@@ -1,63 +1,60 @@
'use strict';

var assert = require('assert');
var benchmark = require('benchmark');
var bitauth = require('../lib/bitauth-node');
var bitauth = require('../lib/bitauth');
var async = require('async');

var maxTime = 10;

async.series([
function(next) {

var privkey = '9b3bdba1c7910017dae5d6cbfb2e86aafdccfbcbea518d1b984c45817b6c655b';
var privkeyBuffer = new Buffer(privkey, 'hex');
function (next) {
// var privkey = '9b3bdba1c7910017dae5d6cbfb2e86aafdccfbcbea518d1b984c45817b6c655b';
// var privkeyBuffer = new Buffer(privkey, 'hex');
var pubkey = '03ff368ca67364d1df4c0f131b6a454d4fa14c00538357f03235917feabc1a9cb6';
var pubkeyBuffer = new Buffer(pubkey, 'hex');
var contract = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer vestibulum nibh neque, ac fermentum nunc pharetra in. Aenean orci velit, facilisis a gravida eu, ullamcorper feugiat dui. Sed quis eros sed sem egestas sagittis non sit amet arcu. Nulla feugiat purus et sem tempus convallis. Ut a odio consequat, vulputate nisl a, venenatis lectus. Aenean mi diam, pulvinar sed vehicula pulvinar, commodo quis justo. Pellentesque quis elementum eros. Sed ligula tellus, interdum non interdum eget, ultricies in ipsum. Maecenas vitae lectus sit amet ante volutpat malesuada. Nulla condimentum iaculis sem sit amet rhoncus. Mauris at vestibulum felis, a porttitor elit. Pellentesque rhoncus faucibus condimentum. Praesent auctor auctor magna, nec consectetur mi suscipit eget. Nulla sit amet ligula enim. Ut odio augue, auctor ac quam vel, aliquet mattis nisi. Curabitur orci lectus, viverra at hendrerit at, feugiat at magna. Morbi rhoncus bibendum erat, quis dapibus felis eleifend vitae. Etiam vel sapien consequat, tempor libero non, lobortis purus. Maecenas finibus pretium augue a ullamcorper. Donec consectetur sed nunc sed convallis. Phasellus eu magna a nisl lobortis finibus. Quisque hendrerit at arcu tempus gravida. Donec fringilla pulvinar sapien at porta. Interdum et malesuada fames ac ante ipsum primis in faucibus. Sed dui metus, rhoncus at iaculis nec, porta at nunc. Donec in purus pellentesque, lacinia erat eget, congue massa. In a magna molestie tellus convallis dictum. Etiam id magna laoreet, suscipit leo non, egestas turpis. Sed dolor orci, pellentesque eget tempor ut, tincidunt at magna. Duis quis imperdiet sapien.';
var contractBuffer = new Buffer(contract);
var signature = '3045022100db71942a5a6dd1443cbf7519b2bc16a041aff8d4830bd42599f03ce503b8bf700220281989345617548d2512391a4b04450761df9add920d83043f9e21cb5baeb703';
var signatureBuffer = new Buffer(signature, 'hex');

function nodebitauthVerify() {
function nodebitauthVerify () {
bitauth.verifySignature(contractBuffer, pubkeyBuffer, signatureBuffer);
}

// #verifySignature
var suite = new benchmark.Suite();
suite.add('bitauth#verifySignature', nodebitauthVerify, { maxTime: maxTime });
suite
.on('cycle', function(event) {
.on('cycle', function (event) {
console.log(String(event.target));
})
.on('complete', function() {
.on('complete', function () {
console.log('---------------------------------------');
next();
})
.run();
},
function(next) {

function (next) {
// invalid checksum
var sinbad = 'Tf1Jc1xSbqasm5QLwwSQc5umddx2h7mAMhX';

function nodebitauthValidateSin() {
function nodebitauthValidateSin () {
bitauth.validateSin(sinbad);
}

// #validateSin
var suite = new benchmark.Suite();
suite.add('bitauth#validateSin', nodebitauthValidateSin, { maxTime: maxTime });
suite
.on('cycle', function(event) {
.on('cycle', function (event) {
console.log(String(event.target));
})
.on('complete', function() {
.on('complete', function () {
console.log('---------------------------------------');
next();
})
.run();
}
], function(err) {
console.log('Finished');
], function (err) {
console.log('Finished', err);
});
3 changes: 2 additions & 1 deletion browser.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use strict';

module.exports = require('./lib/bitauth-browserify');
var bitauth = require('./lib/bitauth');
module.exports = bitauth;
28 changes: 9 additions & 19 deletions examples/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var keys = {

// GET

for(k in keys) {
for (var k in keys) {
var url = 'http://localhost:3000/user';
var dataToSign = url;
var options = {
Expand All @@ -20,25 +20,20 @@ for(k in keys) {
}
};

request.get(options, function(err, response, body) {
if(err) {
console.log(err);
}
if(body) {
console.log(body);
}
request.get(options, function (err, response, body) {
console.log(err !== null ? err : body);
});
}

var pizzas = ['pepperoni', 'sausage', 'veggie', 'hawaiian'];

// POST

for(k in keys) {
var url = 'http://localhost:3000/pizzas';
for (k in keys) {
url = 'http://localhost:3000/pizzas';
var data = {type: pizzas[Math.floor(Math.random() * pizzas.length)]};
var dataToSign = url + JSON.stringify(data);
var options = {
dataToSign = url + JSON.stringify(data);
options = {
url: url,
headers: {
'x-identity': bitauth.getPublicKeyFromPrivateKey(keys[k]),
Expand All @@ -47,12 +42,7 @@ for(k in keys) {
json: data
};

request.post(options, function(err, response, body) {
if(err) {
console.log(err);
}
if(body) {
console.log(body);
}
request.post(options, function (err, response, body) {
console.log(err !== null ? err : body);
});
}
15 changes: 10 additions & 5 deletions examples/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,26 @@ var app = express();
app.use(rawBody);
app.use(bodyParser());

app.get('/user', bitauthMiddleware, function (req, res) {
if (!req.sin || !users[req.sin]) {
return res.send(401, {error: 'Unauthorized'});
}

app.get('/user', bitauthMiddleware, function(req, res) {
if(!req.sin || !users[req.sin]) return res.send(401, {error: 'Unauthorized'});
res.send(200, users[req.sin]);
});

app.post('/pizzas', bitauthMiddleware, function(req, res) {
if(!req.sin || !users[req.sin]) return res.send(401, {error: 'Unauthorized'});
app.post('/pizzas', bitauthMiddleware, function (req, res) {
if (!req.sin || !users[req.sin]) {
return res.send(401, {error: 'Unauthorized'});
}

var pizza = req.body;
pizza.owner = users[req.sin].name;
pizzas.push(pizza);
res.send(200, req.body);
});

app.get('/pizzas', function(req, res) {
app.get('/pizzas', function (req, res) {
res.send(200, pizzas);
});

Expand Down
Loading

0 comments on commit 920ac8b

Please sign in to comment.