Skip to content

Instantly share code, notes, and snippets.

@xavierlepretre
Last active March 16, 2018 09:25
Show Gist options
  • Save xavierlepretre/ed02e9569ff7645bdcfc3b528f233715 to your computer and use it in GitHub Desktop.
Save xavierlepretre/ed02e9569ff7645bdcfc3b528f233715 to your computer and use it in GitHub Desktop.
Get the Promise of an account on Web3 or Mist.
web3.eth.getFirstAccountPromise = function () {
// https://gist.github.com/xavierlepretre/ed82f210df0f9300493d5ca79893806a
return web3.eth.getAccountsPromise()
.then(function (accounts) {
if (accounts.length > 0) {
return accounts[0];
} else if (typeof(mist) !== "undefined") {
// https://gist.github.com/xavierlepretre/ee456323b2544dd4da22cd5fa6d2894c
return mist.requestAccountPromise();
} else {
throw "No account found";
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment