├── .versions ├── README.md ├── networkInfo.js └── package.js /.versions: -------------------------------------------------------------------------------- 1 | ethereum:networkinfo@0.3.0 2 | jrudio:bluebird@3.3.1_1 3 | meteor@1.1.2 4 | underscore@1.0.1 5 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Meteor package - Ethereum network info 2 | 3 | Waits for a `web3` connection and then extracts network info. 4 | 5 | It fetches the Genesis block information and places it into a 6 | Promise - `NetworkInfoPromise` - for use throughout your application. 7 | 8 | It also provides a proxy wrapper class for Mongo collections 9 | which ensures that network id is stored for each document in the collection, 10 | allowing you store [data by network](https://github.com/ethereum/mist/pull/1049). 11 | 12 | ## Installation 13 | 14 | $ meteor add hiddentao:ethereum-networkinfo 15 | 16 | ## Usage 17 | 18 | Use the extracted network information via the Promise: 19 | 20 | ```js 21 | NetworkInfo.promise.then(function(networkInfo) { 22 | console.log(info); 23 | 24 | /* 25 | type: 'main', 26 | uniqueId: 'fb25ce3f...', 27 | genesis: {...}, 28 | */ 29 | }) 30 | ``` 31 | 32 | To _network_-ify a collection do: 33 | 34 | ```js 35 | MyCollection = new NetworkInfo.ProxyCollection( 36 | new Mongo.Collection('mydata') 37 | ); 38 | ``` 39 | 40 | You can then use all the normal Meteor collection methods on the returned object. 41 | 42 | -------------------------------------------------------------------------------- /networkInfo.js: -------------------------------------------------------------------------------- 1 | NetworkInfo = { 2 | promise: new P(function(resolve, reject) { 3 | new P(function(timerDone) { 4 | // wait for web3 5 | var timer = setInterval(function() { 6 | if (typeof web3 !== 'undefined') { 7 | clearInterval(timer); 8 | 9 | timerDone(); 10 | } 11 | }, 100); 12 | }) 13 | .then(function() { 14 | web3.eth.getBlock(0, function(e, res) { 15 | if (e) { 16 | console.error('Error fetching Genesis block'); 17 | 18 | return reject(e); 19 | } else { 20 | console.info('Genesis block: ' + res.hash); 21 | 22 | var network = 'private'; 23 | 24 | switch(res.hash) { 25 | case '0x0cd786a2425d16f152c658316c423e6ce1181e15c3295826d7c9904cba9ce303': 26 | network = 'test'; 27 | break; 28 | case '0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3': 29 | network = 'main'; 30 | break; 31 | } 32 | 33 | NetworkInfo.type = network; 34 | NetworkInfo.uniqueId = res.hash; 35 | NetworkInfo.genesis = res; 36 | 37 | return resolve(); 38 | } 39 | }); 40 | }); 41 | }), 42 | 43 | /** 44 | * An Ethereum network id aware collection using given collection. 45 | * 46 | * This creates a proxy collection which ensures all queries to the 47 | * underlying original collection take network id into account. 48 | * 49 | * @param {Collectkon} mongoCollection Original collection 50 | */ 51 | ProxyCollection: function(mongoCollection) { 52 | var self = this; 53 | 54 | self._coll = mongoCollection; 55 | self._name = self._coll._name; 56 | self._network = null; 57 | 58 | NetworkInfo.promise.then(function() { 59 | self._network = NetworkInfo.uniqueId; 60 | }); 61 | 62 | self._addToQuery = function(selector) { 63 | var self = this; 64 | 65 | if(_.isObject(selector)) { 66 | selector = _.extend(selector, { 67 | network: self._network, 68 | }); 69 | } 70 | else if(_.isString(selector)) { 71 | selector = { 72 | network: self._network, 73 | _id: selector, 74 | }; 75 | } 76 | else { 77 | selector = { 78 | network: self._network, 79 | }; 80 | } 81 | 82 | return selector; 83 | }; 84 | 85 | ['find', 'findOne', 'insert', 'update', 'upsert', 'remove'].forEach(function(method) { 86 | self[method] = function() { 87 | var selector = arguments[0], 88 | args = Array.prototype.slice.call(arguments, 1); 89 | 90 | return self._coll[method].apply(self._coll, [self._addToQuery(selector)].concat(args)); 91 | } 92 | }); 93 | 94 | 95 | ['allow', 'deny', 'rawCollection', 'rawDatabase'].forEach(function(method) { 96 | self[method] = function() { 97 | return self._coll[method].apply(self._coll, arguments); 98 | } 99 | }); 100 | 101 | 102 | } 103 | }; 104 | -------------------------------------------------------------------------------- /package.js: -------------------------------------------------------------------------------- 1 | Package.describe({ 2 | name: 'ethereum:networkinfo', 3 | summary: 'Ethereum network information package', 4 | version: '0.4.1', 5 | git: 'https://github.com/ethereum/meteor-ethereum-networkinfo.git' 6 | }); 7 | 8 | Package.onUse(function(api) { 9 | api.versionsFrom('1.0'); 10 | api.use('underscore', ['client', 'server']); 11 | api.use('jrudio:bluebird@3.3.1_1', ['client', 'server']); 12 | api.export(['NetworkInfo'], ['client', 'server']); 13 | api.addFiles('networkInfo.js', ['client', 'server']); 14 | }); 15 | --------------------------------------------------------------------------------