diff --git a/lib/chain_manager.js b/lib/chain_manager.js index 2e50f35f..41dd211a 100644 --- a/lib/chain_manager.js +++ b/lib/chain_manager.js @@ -1,7 +1,10 @@ var fs = require('fs'); var web3 = require('web3'); +var sha3_256 = require('js-sha3').sha3_256; -ChainManager = function() { } +ChainManager = function() { + this.currentChain = {}; +} ChainManager.prototype.loadConfigFile = function(filename) { try { @@ -26,7 +29,16 @@ ChainManager.prototype.init = function(env, blockchainConfig) { var chainId = web3.eth.getBlock(0).hash; if (this.chainManagerConfig[chainId] === undefined) { - this.chainManagerConfig[chainId] = {contracts: []}; + this.chainManagerConfig[chainId] = {contracts: {}}; + } + + this.currentChain = this.chainManagerConfig[chainId]; +} + +ChainManager.prototype.addContract = function(contractName, code, address) { + this.currentChain.contracts[sha3_256(code)] = { + name: contractName, + address: address } } diff --git a/package.json b/package.json index cbb9f03f..32ce6053 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ "grunt": "^0.4.5", "hashmerge": "^1.0.2", "jasmine": "^2.3.1", + "js-sha3": "^0.3.1", "meteor-build-client": "^0.1.6", "methodmissing": "^0.0.3", "mkdirp": "^0.5.1", diff --git a/test/chain_manager.js b/test/chain_manager.js index b589f9fb..c1426612 100644 --- a/test/chain_manager.js +++ b/test/chain_manager.js @@ -11,9 +11,23 @@ describe('embark.chain_manager', function() { chainManager.init('development', blockchainConfig); it('should initialize chain', function() { - assert.equal(chainManager.chainManagerConfig['0x629e768beb87dc8c54a475d310a7196e86c97d0006e5a6d34a8217726c90223f'] != undefined, true); - assert.equal(chainManager.chainManagerConfig['0x629e768beb87dc8c54a475d310a7196e86c97d0006e5a6d34a8217726c90223f'].contracts.length, 0); + var chain = chainManager.chainManagerConfig['0x629e768beb87dc8c54a475d310a7196e86c97d0006e5a6d34a8217726c90223f'] + assert.equal(chain != undefined, true); + }); + }); + + describe('#addContract', function() { + + it('should register a contract in the chain', function() { + chainManager.addContract("Foo", "123456", "0x123"); + + var chain = chainManager.chainManagerConfig['0x629e768beb87dc8c54a475d310a7196e86c97d0006e5a6d34a8217726c90223f'] + var contract = chain.contracts["d7190eb194ff9494625514b6d178c87f99c5973e28c398969d2233f2960a573e"] + + assert.equal(contract.name, "Foo"); + assert.equal(contract.address, "0x123"); }); }); + });