diff --git a/lib/modules/blockchain_connector/index.js b/lib/modules/blockchain_connector/index.js index ab494d5a..233117c3 100644 --- a/lib/modules/blockchain_connector/index.js +++ b/lib/modules/blockchain_connector/index.js @@ -350,8 +350,9 @@ class BlockchainConnector { } let accountConfig = self.config.blockchainConfig.account; let selectedAccount = accountConfig && accountConfig.address; - self.setDefaultAccount(selectedAccount || accounts[0]); - cb(); + const defaultAccount = selectedAccount || accounts[0]; + self.setDefaultAccount(defaultAccount); + cb(null, defaultAccount); }); } diff --git a/lib/modules/storage/storageProcessesLauncher.js b/lib/modules/storage/storageProcessesLauncher.js index adddf945..f0a697e4 100644 --- a/lib/modules/storage/storageProcessesLauncher.js +++ b/lib/modules/storage/storageProcessesLauncher.js @@ -111,13 +111,21 @@ class StorageProcessesLauncher { silent: self.logger.logLevel !== 'trace', exitCallback: self.processExited.bind(this, storageName) }); - self.processes[storageName].send({ - action: constants.blockchain.init, options: { - storageConfig: self.storageConfig, - blockchainConfig: self.blockchainConfig, - cors: self.buildCors() - } + this.events.once("web3Ready", () => { + this.events.request("blockchain:object", (blockchain) => { + blockchain.determineDefaultAccount((err, defaultAccount) => { + self.processes[storageName].send({ + action: constants.storage.init, options: { + storageConfig: self.storageConfig, + blockchainConfig: self.blockchainConfig, + cors: self.buildCors(), + defaultAccount: defaultAccount + } + }); + }); + }); }); + self.processes[storageName].on('result', constants.storage.initiated, (msg) => { if (msg.error) { diff --git a/lib/modules/swarm/process.js b/lib/modules/swarm/process.js index 4376a991..3ebfce89 100644 --- a/lib/modules/swarm/process.js +++ b/lib/modules/swarm/process.js @@ -12,21 +12,43 @@ class SwarmProcess extends ProcessWrapper { this.blockchainConfig = options.blockchainConfig; this.cors = options.cors; this.command = this.storageConfig.swarmPath || 'swarm'; + this.defaultAccount = options.defaultAccount; } startSwarmDaemon() { const self = this; - if (!this.storageConfig.account || !this.storageConfig.account.address || !this.storageConfig.account.password) { + let bzzaccount; + let password; + // use our storage config address/password if we have it + if (this.storageConfig.account && this.storageConfig.account.address && this.storageConfig.account.password) { + bzzaccount = this.storageConfig.account.address; + password = fs.dappPath(this.storageConfig.account.password); + } + // default to our blockchain config account, or our default account + else if (!this.blockchainConfig.isDev && + this.blockchainConfig.mineWhenNeeded && + this.blockchainConfig.account && + (this.blockchainConfig.account.address || this.defaultAccount) && + this.blockchainConfig.account.password + ) { + // defaultAccount is populated from blockchain_connector.determineDefaultAccount which is either + // config/blockchain.js > account > address or the first address returned from web3.eth.getAccounts + // (usually the default account) + bzzaccount = this.defaultAccount; + password = fs.dappPath(this.blockchainConfig.account.password); + console.trace(`Swarm account/password falling back to the blockchain account ${this.defaultAccount}. The account is either specified in config/blockchain.js > account > address or is the first address returned from web3.eth.getAccounts. The password is specified in config/blockchain.js > account > address.`); + } + else { return 'Account address and password are needed in the storage config to start the Swarm process'; } const datadir = this.blockchainConfig.datadir || fs.dappPath(`.embark/development/datadir`); const args = [ '--datadir', datadir, - '--bzzaccount', this.storageConfig.account.address, - '--password', fs.dappPath(this.storageConfig.account.password), + '--bzzaccount', bzzaccount, '--corsdomain', self.cors.join(',') ]; + if (password) args.push('--password', password); console.trace('Starting swarm process with arguments: ' + args.join(' ')); this.child = child_process.spawn(this.command, args);