|
4 | 4 | .module('multiSigWeb')
|
5 | 5 | .service("Web3Service", function ($window, $q, Utils, $uibModal, Connection, Config, $http) {
|
6 | 6 |
|
7 |
| - var factory = {}; |
| 7 | + var factory = { |
| 8 | + coinbase: null, |
| 9 | + accounts: [] |
| 10 | + }; |
8 | 11 |
|
9 | 12 | factory.webInitialized = $q(function (resolve, reject) {
|
10 | 13 | window.addEventListener('load', function () {
|
|
13 | 16 | });
|
14 | 17 | });
|
15 | 18 |
|
| 19 | + /** |
| 20 | + * Asks Metamask to open its widget. |
| 21 | + * Returns a callback call with the list of accounts or null in case the |
| 22 | + * user rejects the approval request. |
| 23 | + * @param callback, function (error, accounts) |
| 24 | + */ |
| 25 | + factory.enableMetamask = function (callback) { |
| 26 | + $window.ethereum.enable().then(function (accounts) { |
| 27 | + factory.reloadWeb3Provider(); |
| 28 | + // Set accounts and coinbase |
| 29 | + factory.accounts = accounts; |
| 30 | + factory.coinbase = accounts[0]; |
| 31 | + callback(null, accounts) |
| 32 | + }).catch(function (error) { |
| 33 | + callback(error, null) |
| 34 | + }); |
| 35 | + }; |
| 36 | + |
| 37 | + /** |
| 38 | + * Returns true if metamask is injected, false otherwise |
| 39 | + **/ |
| 40 | + factory.isMetamaskInjected = function () { |
| 41 | + return window && (typeof window.web3 !== 'undefined' && |
| 42 | + (window.web3.currentProvider.constructor.name === 'MetamaskInpageProvider' || window.web3.currentProvider.isMetaMask) |
| 43 | + ); |
| 44 | + }; |
| 45 | + |
16 | 46 | /**
|
17 | 47 | * Reloads web3 provider
|
18 | 48 | * @param resolve, function (optional)
|
|
22 | 52 |
|
23 | 53 | factory.accounts = [];
|
24 | 54 | factory.coinbase = null;
|
| 55 | + var web3 = null; |
| 56 | + |
| 57 | + // Legacy dapp browsers... |
| 58 | + if ($window.web3 && !$window.ethereum) { |
| 59 | + web3 = $window.web3; |
| 60 | + } |
| 61 | + // TODO: figure out whether Metamask standardize isEnabled() or find out |
| 62 | + // another way to manage it |
| 63 | + else if ($window.ethereum && window.ethereum._metamask.isEnabled()) { |
| 64 | + web3 = $window.ethereum; |
| 65 | + } |
25 | 66 |
|
26 | 67 | // Ledger wallet
|
27 | 68 | if (txDefault.wallet == "ledger") {
|
28 | 69 | if (isElectron) {
|
29 | 70 | factory.ledgerElectronSetup();
|
30 |
| - if (resolve) { |
31 |
| - resolve(); |
32 |
| - } |
| 71 | + factory.web3.eth.getAccounts(function (e, accounts) { |
| 72 | + if (e) { |
| 73 | + if (reject) { |
| 74 | + reject(e); |
| 75 | + } |
| 76 | + } else { |
| 77 | + factory.accounts = accounts; |
| 78 | + factory.coinbase = factory.accounts[0]; |
| 79 | + if (resolve) { |
| 80 | + resolve(); |
| 81 | + } |
| 82 | + } |
| 83 | + }); |
33 | 84 | }
|
34 | 85 | else {
|
35 | 86 | factory.ledgerSetup();
|
|
45 | 96 | }
|
46 | 97 | }
|
47 | 98 | // injected web3 provider (Metamask, mist, etc)
|
48 |
| - else if (txDefault.wallet == "injected" && $window && $window.web3 && !isElectron) { |
49 |
| - factory.web3 = new MultisigWeb3($window.web3.currentProvider); |
| 99 | + else if (txDefault.wallet == "injected" && web3 && !isElectron) { |
| 100 | + factory.web3 = web3.currentProvider !== undefined ? new MultisigWeb3(web3.currentProvider) : new MultisigWeb3(web3); |
| 101 | + // Set accounts |
| 102 | + factory.accounts = factory.web3.eth.accounts; |
| 103 | + factory.coinbase = factory.accounts[0]; |
| 104 | + |
50 | 105 | if (resolve) {
|
51 | 106 | resolve();
|
52 | 107 | }
|
|
57 | 112 | resolve();
|
58 | 113 | }
|
59 | 114 | }
|
60 |
| - else { |
| 115 | + else if (txDefault.wallet == 'remotenode') { |
61 | 116 | // Connect to Ethereum Node
|
| 117 | + // factory.web3 = new MultisigWeb3(new RpcSubprovider({ |
| 118 | + // rpcUrl: txDefault.ethereumNode |
| 119 | + // })); |
62 | 120 | factory.web3 = new MultisigWeb3(new MultisigWeb3.providers.HttpProvider(txDefault.ethereumNode));
|
63 | 121 | // Check connection
|
64 |
| - factory.web3.net.getListening(function(e){ |
| 122 | + factory.web3.net.getListening(function(e) { |
65 | 123 | if (e) {
|
66 | 124 | Utils.dangerAlert("You are not connected to any node.");
|
67 | 125 | if (reject) {
|
68 | 126 | reject();
|
69 | 127 | }
|
70 | 128 | }
|
71 | 129 | else {
|
72 |
| - if (resolve) { |
73 |
| - resolve(); |
74 |
| - } |
| 130 | + // Get accounts from remote node |
| 131 | + factory.web3.eth.getAccounts(function(e, accounts) { |
| 132 | + if (e) { |
| 133 | + if (reject) { |
| 134 | + reject(e); |
| 135 | + } |
| 136 | + } |
| 137 | + else { |
| 138 | + // Set accounts |
| 139 | + factory.accounts = accounts; |
| 140 | + factory.coinbase = accounts[0]; |
| 141 | + |
| 142 | + if (resolve) { |
| 143 | + resolve(); |
| 144 | + } |
| 145 | + } |
| 146 | + }); |
75 | 147 | }
|
76 | 148 | });
|
77 | 149 | }
|
| 150 | + else if (resolve) { |
| 151 | + resolve(); |
| 152 | + } |
78 | 153 | };
|
79 | 154 |
|
80 | 155 | /**
|
|
169 | 244 | * Get ethereum accounts and update account list.
|
170 | 245 | */
|
171 | 246 | factory.updateAccounts = function (cb) {
|
172 |
| - return factory.web3.eth.getAccounts( |
173 |
| - function (e, accounts) { |
174 |
| - if (e) { |
175 |
| - cb(e); |
176 |
| - } |
177 |
| - else { |
178 |
| - factory.accounts = accounts; |
179 |
| - |
180 |
| - if (factory.coinbase && accounts && accounts.length && accounts.indexOf(factory.coinbase) != -1) { |
181 |
| - // same coinbase |
182 |
| - } |
183 |
| - else if (accounts) { |
184 |
| - factory.coinbase = accounts[0]; |
| 247 | + if (!isElectron && factory.coinbase) { |
| 248 | + return factory.web3.eth.getAccounts( |
| 249 | + function (e, accounts) { |
| 250 | + if (e) { |
| 251 | + cb(e); |
185 | 252 | }
|
186 | 253 | else {
|
187 |
| - factory.coinbase = null; |
188 |
| - } |
| 254 | + factory.accounts = accounts; |
189 | 255 |
|
190 |
| - cb(null, accounts); |
| 256 | + if (factory.coinbase && accounts && accounts.length && accounts.indexOf(factory.coinbase) != -1) { |
| 257 | + // same coinbase |
| 258 | + } |
| 259 | + else if (accounts) { |
| 260 | + factory.coinbase = accounts[0]; |
| 261 | + } |
| 262 | + else { |
| 263 | + factory.coinbase = null; |
| 264 | + } |
| 265 | + |
| 266 | + cb(null, accounts); |
| 267 | + } |
191 | 268 | }
|
192 |
| - } |
193 |
| - ); |
| 269 | + ); |
| 270 | + } |
| 271 | + else { |
| 272 | + cb(null, null); |
| 273 | + } |
194 | 274 | };
|
195 | 275 |
|
196 | 276 | /* Ledger setup on browser*/
|
|
0 commit comments