From 0ebe1f0be697d4d2879f408c230744a1a58ad6aa Mon Sep 17 00:00:00 2001 From: Chad Engler Date: Wed, 27 May 2015 08:29:18 -0700 Subject: [PATCH 1/4] add fallback to browser is secp256k1 --- browser.js | 4 ++-- index.js | 9 ++++++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/browser.js b/browser.js index 045d523..e223c63 100644 --- a/browser.js +++ b/browser.js @@ -7,7 +7,7 @@ var EC = require("elliptic").ec; var ec = new EC("secp256k1"); -var cryptoObj = window.crypto || window.msCrypto || {}; +var cryptoObj = global.crypto || global.msCrypto || {}; var subtle = cryptoObj.subtle || cryptoObj.webkitSubtle; function assert(condition, message) { @@ -18,7 +18,7 @@ function assert(condition, message) { function randomBytes(size) { var arr = new Uint8Array(size); - window.crypto.getRandomValues(arr); + global.crypto.getRandomValues(arr); return new Buffer(arr); } diff --git a/index.js b/index.js index fa0f728..e9f1a4d 100644 --- a/index.js +++ b/index.js @@ -9,9 +9,12 @@ var promise = typeof Promise === "undefined" ? require("es6-promise").Promise : Promise; var crypto = require("crypto"); -// TODO(Kagami): We may fallback to pure JS implementation -// (`browser.js`) if this modules are failed to load. -var secp256k1 = require("secp256k1"); +// try to use secp256k1, fallback to browser implementation +try { + var secp256k1 = require("secp256k1"); +} catch (e) { + return module.exports = require("./browser"); +} var ecdh = require("./build/Release/ecdh"); function assert(condition, message) { From 365674f6226bdd2824b58bc0535b1ff4e9d04104 Mon Sep 17 00:00:00 2001 From: Chad Engler Date: Wed, 27 May 2015 08:39:33 -0700 Subject: [PATCH 2/4] fix jshint issue --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index e9f1a4d..501311c 100644 --- a/index.js +++ b/index.js @@ -13,7 +13,7 @@ var crypto = require("crypto"); try { var secp256k1 = require("secp256k1"); } catch (e) { - return module.exports = require("./browser"); + return (module.exports = require("./browser")); } var ecdh = require("./build/Release/ecdh"); From 3922b1b4ab6aa954603f55ae352107802006b911 Mon Sep 17 00:00:00 2001 From: Chad Engler Date: Thu, 28 May 2015 06:57:06 -0700 Subject: [PATCH 3/4] move ecdh load into try{}; add fallback override env var --- .travis.yml | 2 ++ index.js | 8 ++++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index ee9f5fd..48ab839 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,6 +3,8 @@ node_js: - "0.10" addons: firefox: "34.0" +env: + - ECCRYPTO_NO_FALLBACK=1 notifications: email: on_success: never diff --git a/index.js b/index.js index 501311c..017a646 100644 --- a/index.js +++ b/index.js @@ -12,10 +12,14 @@ var crypto = require("crypto"); // try to use secp256k1, fallback to browser implementation try { var secp256k1 = require("secp256k1"); + var ecdh = require("./build/Release/ecdh"); } catch (e) { - return (module.exports = require("./browser")); + if (process.env.ECCRYPTO_NO_FALLBACK) { + throw e; + } else { + return (module.exports = require("./browser")); + } } -var ecdh = require("./build/Release/ecdh"); function assert(condition, message) { if (!condition) { From d743332ebc2e0d16dc89e109e73255b9ab82d217 Mon Sep 17 00:00:00 2001 From: Chad Engler Date: Thu, 28 May 2015 07:42:43 -0700 Subject: [PATCH 4/4] move no fallback env var to npm test script --- .travis.yml | 2 -- package.json | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 48ab839..ee9f5fd 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,8 +3,6 @@ node_js: - "0.10" addons: firefox: "34.0" -env: - - ECCRYPTO_NO_FALLBACK=1 notifications: email: on_success: never diff --git a/package.json b/package.json index cf334f7..42117e8 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "browser": "browser.js", "scripts": { "install": "node-gyp rebuild || exit 0", - "test": "mocha && xvfb-run -a karma start && jshint .", + "test": "ECCRYPTO_NO_FALLBACK=1 mocha && xvfb-run -a karma start && jshint .", "m": "mocha", "k": "xvfb-run -a karma start", "kc": "xvfb-run -a karma start --browsers Chromium",