added browser support for bitauth

This commit is contained in:
Gordon Hall 2014-06-25 13:56:24 -04:00
parent 4009ab9684
commit 75b77019c4
8 changed files with 91 additions and 36 deletions

2
.gitignore vendored
View File

@ -1 +1,3 @@
node_modules
dist/bitauth.browser.js
dist/bitauth.browser.min.js

11
dist/README.md vendored Normal file
View File

@ -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`.

9
index.js Normal file
View File

@ -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;

View File

@ -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 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();
}
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;

14
lib/decrypt.js Normal file
View File

@ -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');
};

14
lib/encrypt.js Normal file
View File

@ -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);
};

View File

@ -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"
}
}

11
scripts/make-dist.sh Normal file
View File

@ -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!"