From 75b77019c425c0bc55f6d7803b51089e99e6c8e7 Mon Sep 17 00:00:00 2001 From: Gordon Hall Date: Wed, 25 Jun 2014 13:56:24 -0400 Subject: [PATCH] added browser support for bitauth --- .gitignore | 2 ++ dist/README.md | 11 ++++++++++ index.js | 9 ++++++++ lib/bitauth.js | 49 +++++++++++++------------------------------- lib/decrypt.js | 14 +++++++++++++ lib/encrypt.js | 14 +++++++++++++ package.json | 17 ++++++++++++++- scripts/make-dist.sh | 11 ++++++++++ 8 files changed, 91 insertions(+), 36 deletions(-) create mode 100644 dist/README.md create mode 100644 index.js create mode 100644 lib/decrypt.js create mode 100644 lib/encrypt.js create mode 100644 scripts/make-dist.sh diff --git a/.gitignore b/.gitignore index 3c3629e..ed4b243 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ node_modules +dist/bitauth.browser.js +dist/bitauth.browser.min.js diff --git a/dist/README.md b/dist/README.md new file mode 100644 index 0000000..f503e49 --- /dev/null +++ b/dist/README.md @@ -0,0 +1,11 @@ +# BitAuth Browser Bundle + +To build a browser compatible version of BitAuth, run the following command from +the project's root directory: + +``` +npm run make-dist +``` + +This will output `bitauth.browser.js` to this directory. The script introduces a +global variable at `window.bitauth`. diff --git a/index.js b/index.js new file mode 100644 index 0000000..14c9f38 --- /dev/null +++ b/index.js @@ -0,0 +1,9 @@ +// get base functionality +var bitauth = require('./lib/bitauth'); + +// add node-specific encrypt/decrypt +bitauth.encrypt = require('./lib/encrypt'); +bitauth.decrypt = require('./lib/decrypt'); + + +module.exports = bitauth; diff --git a/lib/bitauth.js b/lib/bitauth.js index b936069..98fc875 100644 --- a/lib/bitauth.js +++ b/lib/bitauth.js @@ -1,26 +1,25 @@ -var base58 = require('base58-native'); -var crypto = require('crypto'); -var bitcore = require('bitcore'); -var Key = bitcore.Key; -var SIN = bitcore.SIN; -var SINKey = bitcore.SINKey -var coinUtil = bitcore.util; +var crypto = require('crypto'); +var bitcore = require('bitcore'); +var Key = bitcore.Key; +var SIN = bitcore.SIN; +var SINKey = bitcore.SINKey +var util = bitcore.util; -var BitAuth = function() {}; +var BitAuth = {}; BitAuth.generateSin = function() { var sk = new SINKey(); sk.generate(); - var obj = sk.storeObj(); - - return obj; + return sk.storeObj(); }; BitAuth.getPublicKeyFromPrivateKey = function(privkey) { try { var key = new Key(); + key.private = new Buffer(privkey, 'hex'); key.regenerateSync(); + return key.public.toString('hex'); } catch (err) { console.log(err); @@ -29,13 +28,13 @@ BitAuth.getPublicKeyFromPrivateKey = function(privkey) { }; BitAuth.getSinFromPublicKey = function(pubkey) { - var pubkeyHash = coinUtil.sha256ripe160(new Buffer(pubkey, 'hex')); + var pubkeyHash = util.sha256ripe160(new Buffer(pubkey, 'hex')); var sin = new SIN(SIN.SIN_EPHEM, pubkeyHash); - return sin.toString(); + return sin.toString(); } BitAuth.sign = function(data, privkey) { - var hash = coinUtil.sha256(data); + var hash = util.sha256(data); try { var key = new Key(); @@ -49,7 +48,7 @@ BitAuth.sign = function(data, privkey) { }; BitAuth.verifySignature = function(data, pubkey, signature, callback) { - var hash = coinUtil.sha256(data); + var hash = util.sha256(data); try { var key = new Key(); @@ -60,24 +59,4 @@ BitAuth.verifySignature = function(data, pubkey, signature, callback) { } }; -BitAuth.encrypt = function(password, str) { - var aes256 = crypto.createCipher('aes-256-cbc', password); - var a = aes256.update(str, 'utf8'); - var b = aes256.final(); - var buf = new Buffer(a.length + b.length); - a.copy(buf, 0); - b.copy(buf, a.length); - return base58.encode(buf); -}; - -BitAuth.decrypt = function(password, str) { - var aes256 = crypto.createDecipher('aes-256-cbc', password); - var a = aes256.update(base58.decode(str)); - var b = aes256.final(); - var buf = new Buffer(a.length + b.length); - a.copy(buf, 0); - b.copy(buf, a.length); - return buf.toString('utf8'); -}; - module.exports = BitAuth; diff --git a/lib/decrypt.js b/lib/decrypt.js new file mode 100644 index 0000000..87fe9c9 --- /dev/null +++ b/lib/decrypt.js @@ -0,0 +1,14 @@ +var base58 = require('base58-native'); +var crypto = require('crypto'); + +module.exports = function decrypt(password, str) { + var aes256 = crypto.createDecipher('aes-256-cbc', password); + var a = aes256.update(base58.decode(str)); + var b = aes256.final(); + var buf = new Buffer(a.length + b.length); + + a.copy(buf, 0); + b.copy(buf, a.length); + + return buf.toString('utf8'); +}; diff --git a/lib/encrypt.js b/lib/encrypt.js new file mode 100644 index 0000000..ec40873 --- /dev/null +++ b/lib/encrypt.js @@ -0,0 +1,14 @@ +var base58 = require('base58-native'); +var crypto = require('crypto'); + +module.exports = function encrypt(password, str) { + var aes256 = crypto.createCipher('aes-256-cbc', password); + var a = aes256.update(str, 'utf8'); + var b = aes256.final(); + var buf = new Buffer(a.length + b.length); + + a.copy(buf, 0); + b.copy(buf, a.length); + + return base58.encode(buf); +}; diff --git a/package.json b/package.json index 1b5ad81..45bb6ad 100644 --- a/package.json +++ b/package.json @@ -5,12 +5,27 @@ "name": "Patrick Nagurny", "email": "patrick@bitpay.com" }, - "version": "0.1.0", + "contributors": [ + { + "name": "Gordon Hall", + "email": "gordon@bitpay.com" + } + ], + "scripts": { + "make-dist": "sh scripts/make-dist.sh", + "postinstall": "npm run make-dist" + }, + "main": "index.js", + "version": "0.1.1", "dependencies": { "bitcore": ">= 0.1.9", "request": "^2.36.0", "express": "^4.3.1", "base58-native": "^0.1.4", "body-parser": "^1.2.0" + }, + "devDependencies": { + "uglify-js": "~2.4.14", + "browserify": "~4.1.11" } } diff --git a/scripts/make-dist.sh b/scripts/make-dist.sh new file mode 100644 index 0000000..4a2650e --- /dev/null +++ b/scripts/make-dist.sh @@ -0,0 +1,11 @@ +cd node_modules/bitcore +echo "Building browser bundle for bitcore..." +node browser/build -s lib/Key,lib/SINKey,lib/SIN,util/util +echo "Building browser bundle for bitauth..." +cd ../../ +node_modules/.bin/browserify lib/bitauth.js -s bitauth -x buffertools -i bitcore -o dist/bitauth.browser.js +echo "Compiling bitcore and bitauth..." +node_modules/.bin/uglifyjs node_modules/bitcore/browser/bundle.js dist/bitauth.browser.js -b -o dist/bitauth.browser.js +echo "Minifying bundle..." +node_modules/.bin/uglifyjs dist/bitauth.browser.js -o dist/bitauth.browser.min.js +echo "Done!"