Skip to content
This repository was archived by the owner on Jan 18, 2023. It is now read-only.

Commit 1eb7a43

Browse files
committed
support for metamask privacy mode
1 parent 8a84dcb commit 1eb7a43

File tree

1 file changed

+32
-15
lines changed

1 file changed

+32
-15
lines changed

src/App.js

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import './App.css';
33

44
import SetProtocol from 'setprotocol.js';
55
import BigNumber from 'bignumber.js';
6+
import Web3 from 'web3';
67

78
// Kovan configuration
89
const config = {
@@ -15,27 +16,43 @@ const config = {
1516
class App extends Component {
1617
constructor() {
1718
super();
18-
const injectedWeb3 = window.web3 || undefined;
19-
let setProtocol;
20-
try {
21-
// Use MetaMask/Mist provider
22-
const provider = injectedWeb3.currentProvider;
23-
setProtocol = new SetProtocol(provider, config);
24-
} catch (err) {
25-
// Throws when user doesn't have MetaMask/Mist running
26-
throw new Error(`No injected web3 found when initializing setProtocol: ${err}`);
27-
}
28-
2919
this.state = {
30-
setProtocol,
31-
web3: injectedWeb3,
20+
setProtocol: null,
21+
web3: null,
3222
// Etherscan Links
3323
createdSetLink: '',
3424
};
3525
this.createSet = this.createSet.bind(this);
3626
this.getAccount = this.getAccount.bind(this);
3727
}
3828

29+
componentDidMount() {
30+
if (!this.state.web3) {
31+
this.requestWeb3Access();
32+
}
33+
}
34+
35+
async requestWeb3Access() {
36+
let setProtocol, injectedWeb3;
37+
if (window.ethereum) {
38+
try {
39+
injectedWeb3 = new Web3(window.ethereum);
40+
await window.ethereum.enable();
41+
} catch (err) {
42+
console.error(`Unable to access web3: ${err.message}`);
43+
}
44+
}
45+
else if (window.web3) {
46+
injectedWeb3 = window.web3;
47+
}
48+
if (injectedWeb3) {
49+
setProtocol = new SetProtocol(injectedWeb3.currentProvider, config);
50+
this.setState((prevState) => ({ web3: injectedWeb3, setProtocol }));
51+
} else {
52+
console.error('Web3 provider not found.');
53+
}
54+
}
55+
3956
async createSet() {
4057
const { setProtocol } = this.state;
4158

@@ -99,8 +116,8 @@ class App extends Component {
99116

100117
getAccount() {
101118
const { web3 } = this.state;
102-
if (web3.eth.accounts[0]) return web3.eth.accounts[0];
103-
throw new Error('Your MetaMask is locked. Unlock it to continue.');
119+
if (web3 && web3.eth.accounts[0]) return web3.eth.accounts[0];
120+
throw new Error('Unlock MetaMask or allow account access when prompted to continue.');
104121
}
105122

106123
renderEtherScanLink(link, content) {

0 commit comments

Comments
 (0)