diff --git a/Gruntfile.js b/Gruntfile.js index b58d64b46..3afa41486 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -4,34 +4,29 @@ var through = require('through'); var undef = "module.exports = undefined;"; var empty = "module.exports = {};"; +// The elliptic package.json is only used for its version +var ellipticPackage = require('elliptic/package.json'); +ellipticPackage = JSON.stringify({ version: ellipticPackage.version }); + +// We already have a random Uint8Array browser/node safe source +var brorand = "var randomBytes = require('ethers-utils').randomBytes; module.exports = function(length) { return randomBytes(length); };"; + var transforms = { - "ripemd.js": "module.exports = {ripemd160: null}", - "precomputed/secp256k1.js": undef, + "elliptic/lib/elliptic/precomputed/secp256k1.js": undef, "elliptic/curve/edwards.js": empty, "elliptic/curve/mont.js": empty, "elliptic/lib/elliptic/eddsa/.*": empty, - "elliptic/lib/elliptic/eddsa/.*": empty, - "elliptic/lib/elliptic/hmac-drbg.js": empty, -// "elliptic/package.json" : + "elliptic/package.json" : ellipticPackage, - "base64-js/.*": undef, - "browser-resolve:.*": undef, - "buffer/.*": undef, - "buffer-shims/.*": undef, - "cipher-base/.*": undef, - "core-util-is/.*": undef, - "create-hash/.*": undef, - "create-hmac/.*": undef, - "events/.*": undef, - "hmac-drbg/lib/hmac-drbg.js": undef, - "ieee754/.*": undef, - "isarray/.*": undef, - "pbkdf2/.*": undef, + "hash.js/lib/hash/ripemd.js": "module.exports = {ripemd160: null}", + + // brorand (Maybe swap out brorand with out getRandomBytes from utils? + "brorand/index.js": brorand, +// "browser-resolve/.*": undef, // If we use the actual brorand, we need this + + // Used by sha3 "process/.*": undef, - "readable-stream/.*": undef, - "sha.js/.*": undef, - "stream-browserify/.*": undef, - "string_decoder/.*": undef, + }; var modified = {}; @@ -39,7 +34,7 @@ var unmodified = {}; function transformFile(path) { for (var pattern in transforms) { - if (path.match(new RegExp(pattern + '$'))) { + if (path.match(new RegExp('/' + pattern + '$'))) { modified[pattern] = true; return transforms[pattern]; } @@ -55,38 +50,74 @@ function transform(path, options) { }, function () { var transformed = transformFile(path); if (transformed != null) { - //console.log('Transform: ' + path + ' => ' + transformed); data = transformed; } else { unmodified[path] = true; - //console.log('Not transformed:', path); } + //data = data.replace(/__ETHERS_EXPORT__/g, '__MASKED_ETHERS_EXPORT__'); //console.log(data.length, 'FOOBAR', path); this.queue(data); this.queue(null); }); } -function checkBundle(error, source, next) { - var passed = Object.keys(unmodified); - passed.sort(); - console.log('Unmodified:'); - passed.forEach(function(path) { - console.log(' ' + path); - }); - /* - var skipped = Object.keys(transforms); - Object.keys(modified).forEach(function(key) { - delete skipped[key]; - }); - skipped.sort(); - if (skipped.length) { - console.log('Unused Patterns:'); - skipped.forEach(function(pattern) { - console.log(' ' + pattern); - }); +var inflight = 0; + +function preBundle(bundle) { + inflight++; +} + +function postBundle(error, source, next) { + if (error) { + console.log(error); + + } else { + // We setup the utils instance to be able to create a stand-alone package + source = source.toString(); + var lengthBefore = source.length; + var source = source.replace(/"__STAND_ALONE_FALSE__"/g, '"__STAND_ALONE_TRUE__"'); + if (lengthBefore - source.length !== 1) { + next(new Error('multiple stand-alone variables changed')); + return; + } } - */ + + inflight-- + if (inflight === 0) { + + // List all files that passed though unchanged + var preserved = {}; + Object.keys(unmodified).forEach(function(filename) { + var match = filename.match(/(node_modules.*)$/); + if (!match) { + match = filename.match(/(ethers\.js.*)$/); + } + if (!match) { + match = [null, filename]; + } + preserved[match[1]] = true; + }); + preserved = Object.keys(preserved); + preserved.sort(); + console.log('Preserved:'); + preserved.forEach(function(path) { + console.log(' ' + path); + }); + + // Make sure there were no replacement patterns that went unused + var skipped = []; + for (var key in transforms) { + if (!modified[key]) { skipped.push(key); } + } + skipped.sort(); + if (skipped.length) { + console.log('Unused Patterns:'); + skipped.forEach(function(pattern) { + console.log(' ' + pattern); + }); + } + } + next(error, source); } @@ -101,16 +132,16 @@ module.exports = function(grunt) { 'dist/ethers-providers.js': './providers/index.js', 'dist/ethers-utils.js': './utils/index.js', 'dist/ethers-wallet.js': './wallet/index.js', - 'dist/ethers-tests.js': './tests/browser.js', }, options: { transform: [ [ transform, { global: true } ], ], browserifyOptions: { - standalone: 'ethers', + //standalone: '_ethers', }, - postBundleCB: checkBundle + preBundleCB: preBundle, + postBundleCB: postBundle }, }, }, diff --git a/contracts/index.js b/contracts/index.js index b1cf02f02..fe0e35d99 100644 --- a/contracts/index.js +++ b/contracts/index.js @@ -1,3 +1,5 @@ +'use strict'; + var Contract = require('./contract.js'); var Interface = require('./interface.js'); @@ -5,3 +7,6 @@ module.exports = { Constract: Contract, Interface: Interface, } + +require('ethers-utils/standalone.js')(module.exports); + diff --git a/hdnode/index.js b/hdnode/index.js index b4bd58a8d..19d5d3e74 100644 --- a/hdnode/index.js +++ b/hdnode/index.js @@ -257,3 +257,6 @@ module.exports = { validMnemonic: validMnemonic, }; +require('ethers-utils/standalone.js')({ + HDNode: module.exports +}); diff --git a/index.js b/index.js index 4cce6992e..36a5fab1b 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,5 @@ 'use strict'; - /* var contracts = require('ethers-contracts'); var HDNode = require('ethers-hdnode'); @@ -46,3 +45,4 @@ module.exports = { _SigningKey: Wallet._SigningKey, }; +require('ethers-utils/standalone.js')(module.exports); diff --git a/package.json b/package.json index 2f36881ec..abd68d31d 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,7 @@ "version": "grunt dist" }, "dependencies": { + "ethers-utils": "2.0.0" }, "devDependencies": { "grunt": "^0.4.5", diff --git a/providers/index.js b/providers/index.js index 513197928..403484b6f 100644 --- a/providers/index.js +++ b/providers/index.js @@ -7,6 +7,13 @@ var FallbackProvider = require('./fallback-provider.js'); var InfuraProvider = require('./infura-provider.js'); var JsonRpcProvider = require('./json-rpc-provider.js'); +function getDefaultProvider(testnet) { + return new FallbackProvider([ + new InfuraProvider(testnet), + new EtherscanProvider(testnet), + ]); +} + module.exports = { EtherscanProvider: EtherscanProvider, FallbackProvider: FallbackProvider, @@ -15,5 +22,12 @@ module.exports = { isProvder: Provider.isProvider, - _Provider: Provider, + getDefaultProvider:getDefaultProvider, + + Provider: Provider, } + +require('ethers-utils/standalone.js')({ + providers: module.exports +}); + diff --git a/providers/package.json b/providers/package.json index 951cf78bd..d069d4f7c 100644 --- a/providers/package.json +++ b/providers/package.json @@ -7,6 +7,7 @@ "xmlhttprequest": "./browser-xmlhttprequest.js" }, "dependencies": { + "ethers-utils": "2.0.0", "inherits": "2.0.1", "xmlhttprequest": "1.8.0" }, diff --git a/utils/hmac.js b/utils/hmac.js index 427b55312..a789904a7 100644 --- a/utils/hmac.js +++ b/utils/hmac.js @@ -2,18 +2,20 @@ var hash = require('hash.js'); +var sha2 = require('./sha2.js'); + var convert = require('./convert.js'); // @TODO: Make this use create-hmac in node function createSha256Hmac(key) { if (!key.buffer) { key = convert.arrayify(key); } - return new hash.hmac(hash.sha256, key); + return new hash.hmac(sha2.createSha256, key); } function createSha512Hmac(key) { if (!key.buffer) { key = convert.arrayify(key); } - return new hash.hmac(hash.sha512, key); + return new hash.hmac(sha2.createSha512, key); } module.exports = { diff --git a/utils/index.js b/utils/index.js index 9f25a3c4e..057ce1bd9 100644 --- a/utils/index.js +++ b/utils/index.js @@ -17,15 +17,6 @@ var rlp = require('./rlp.js'); var utf8 = require('./utf8.js'); var units = require('./units.js'); -////var xmlhttprequest = require('./xmlhttprequest.js'); - -/* -function cloneObject(object) { - var clone = {}; - for (var key in object) { clone[key] = object[key]; } - return clone; -} -*/ module.exports = { rlp: rlp, @@ -68,3 +59,8 @@ module.exports = { randomBytes: randomBytes, } + +require('./standalone.js')({ + utils: module.exports +}); + diff --git a/utils/package.json b/utils/package.json index 8cb1c0a77..f886c1192 100644 --- a/utils/package.json +++ b/utils/package.json @@ -13,8 +13,7 @@ "xmlhttprequest": "1.8.0" }, "browser": { - "./random-bytes.js": "./browser-random-bytes.js", - "xmlhttprequest": "./browser-xmlhttprequest.js" + "./random-bytes.js": "./browser-random-bytes.js" }, "keywords": [ "ethereum", diff --git a/utils/standalone.js b/utils/standalone.js new file mode 100644 index 000000000..fe2ba1fa5 --- /dev/null +++ b/utils/standalone.js @@ -0,0 +1,23 @@ +var defineProperty = require('./properties.js').defineProperty; + +function Ethers() { } + +function defineEthersValues(values) { + + // This is modified in the Gruntfile.js + if ("__STAND_ALONE_FALSE__" !== ("__STAND_ALONE_" + "TRUE__")) { + return; + } + + if (global.ethers == null) { + defineProperty(global, 'ethers', new Ethers()); + } + + for (var key in values) { + if (global.ethers[key] == null) { + defineProperty(global.ethers, key, values[key]); + } + } +} + +module.exports = defineEthersValues; diff --git a/wallet/index.js b/wallet/index.js index f6c2322bb..d85528139 100644 --- a/wallet/index.js +++ b/wallet/index.js @@ -354,3 +354,8 @@ utils.defineProperty(Wallet, 'summonBrainWallet', function(username, password, p utils.defineProperty(Wallet, '_SigningKey', SigningKey); module.exports = Wallet; + +require('ethers-utils/standalone.js')({ + Wallet: module.exports +}); + diff --git a/wallet/secret-storage.js b/wallet/secret-storage.js index 288e7a365..4648ab2b5 100644 --- a/wallet/secret-storage.js +++ b/wallet/secret-storage.js @@ -1,7 +1,6 @@ 'use strict'; var aes = require('aes-js'); -var pbkdf2 = require('pbkdf2'); var scrypt = require('scrypt-js'); var uuid = require('uuid');