added browser support for bitauth
This commit is contained in:
parent
4009ab9684
commit
75b77019c4
|
@ -1 +1,3 @@
|
||||||
node_modules
|
node_modules
|
||||||
|
dist/bitauth.browser.js
|
||||||
|
dist/bitauth.browser.min.js
|
||||||
|
|
|
@ -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`.
|
|
@ -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;
|
|
@ -1,26 +1,25 @@
|
||||||
var base58 = require('base58-native');
|
|
||||||
var crypto = require('crypto');
|
var crypto = require('crypto');
|
||||||
var bitcore = require('bitcore');
|
var bitcore = require('bitcore');
|
||||||
var Key = bitcore.Key;
|
var Key = bitcore.Key;
|
||||||
var SIN = bitcore.SIN;
|
var SIN = bitcore.SIN;
|
||||||
var SINKey = bitcore.SINKey
|
var SINKey = bitcore.SINKey
|
||||||
var coinUtil = bitcore.util;
|
var util = bitcore.util;
|
||||||
|
|
||||||
var BitAuth = function() {};
|
var BitAuth = {};
|
||||||
|
|
||||||
BitAuth.generateSin = function() {
|
BitAuth.generateSin = function() {
|
||||||
var sk = new SINKey();
|
var sk = new SINKey();
|
||||||
sk.generate();
|
sk.generate();
|
||||||
var obj = sk.storeObj();
|
return sk.storeObj();
|
||||||
|
|
||||||
return obj;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
BitAuth.getPublicKeyFromPrivateKey = function(privkey) {
|
BitAuth.getPublicKeyFromPrivateKey = function(privkey) {
|
||||||
try {
|
try {
|
||||||
var key = new Key();
|
var key = new Key();
|
||||||
|
|
||||||
key.private = new Buffer(privkey, 'hex');
|
key.private = new Buffer(privkey, 'hex');
|
||||||
key.regenerateSync();
|
key.regenerateSync();
|
||||||
|
|
||||||
return key.public.toString('hex');
|
return key.public.toString('hex');
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.log(err);
|
console.log(err);
|
||||||
|
@ -29,13 +28,13 @@ BitAuth.getPublicKeyFromPrivateKey = function(privkey) {
|
||||||
};
|
};
|
||||||
|
|
||||||
BitAuth.getSinFromPublicKey = function(pubkey) {
|
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);
|
var sin = new SIN(SIN.SIN_EPHEM, pubkeyHash);
|
||||||
return sin.toString();
|
return sin.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
BitAuth.sign = function(data, privkey) {
|
BitAuth.sign = function(data, privkey) {
|
||||||
var hash = coinUtil.sha256(data);
|
var hash = util.sha256(data);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
var key = new Key();
|
var key = new Key();
|
||||||
|
@ -49,7 +48,7 @@ BitAuth.sign = function(data, privkey) {
|
||||||
};
|
};
|
||||||
|
|
||||||
BitAuth.verifySignature = function(data, pubkey, signature, callback) {
|
BitAuth.verifySignature = function(data, pubkey, signature, callback) {
|
||||||
var hash = coinUtil.sha256(data);
|
var hash = util.sha256(data);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
var key = new Key();
|
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;
|
module.exports = BitAuth;
|
||||||
|
|
|
@ -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');
|
||||||
|
};
|
|
@ -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);
|
||||||
|
};
|
17
package.json
17
package.json
|
@ -5,12 +5,27 @@
|
||||||
"name": "Patrick Nagurny",
|
"name": "Patrick Nagurny",
|
||||||
"email": "patrick@bitpay.com"
|
"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": {
|
"dependencies": {
|
||||||
"bitcore": ">= 0.1.9",
|
"bitcore": ">= 0.1.9",
|
||||||
"request": "^2.36.0",
|
"request": "^2.36.0",
|
||||||
"express": "^4.3.1",
|
"express": "^4.3.1",
|
||||||
"base58-native": "^0.1.4",
|
"base58-native": "^0.1.4",
|
||||||
"body-parser": "^1.2.0"
|
"body-parser": "^1.2.0"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"uglify-js": "~2.4.14",
|
||||||
|
"browserify": "~4.1.11"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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!"
|
Loading…
Reference in New Issue