Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
195 lines (172 loc) · 6.62 KB
/
Copy pathapp.js
File metadata and controls
195 lines (172 loc) · 6.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
let web3;
let accounts;
const wweb3 = new Web3();
// Connect to MetaMask
const connectButton = document.getElementById('connect-button');
const connectStatus = document.getElementById('connect-status');
let session_id;
let user_address;
document.addEventListener('DOMContentLoaded', function() {
const params = new URLSearchParams(window.location.search);
const sessionId = params.get('session_id');
const userAddress = params.get('user_address');
if (sessionId) {
session_id = sessionId;
}
if (userAddress && wweb3.utils.isAddress(userAddress)) {
console.log("heysdsd");
user_address = userAddress;
}
});
connectButton.addEventListener('click', async () => {
if (window.ethereum) {
try {
await window.ethereum.request({ method: 'eth_requestAccounts' });
web3 = new Web3(window.ethereum);
accounts = await web3.eth.getAccounts();
connectStatus.classList.remove('disconnected');
connectStatus.classList.add('connected');
searchButton.disabled = false;
} catch (error) {
console.error('User denied account access');
}
} else {
console.error('MetaMask is not installed');
}
});
// Search for ENS domain
const ensInput = document.getElementById('ens-input');
const searchButton = document.getElementById('search-button');
const resultDiv = document.getElementById('result');
const signButton = document.getElementById('sign-button');
const signatureDiv = document.getElementById('signature');
const passwordInput = document.getElementById('password-input');
const verifyButton = document.getElementById('verify-button');
const verifyResultDiv = document.getElementById('verify-result');
searchButton.addEventListener('click', async () => {
const ensDomain = ensInput.value;
if (web3 && ensDomain) {
try {
// Check if the current network is Ethereum
const networkId = await web3.eth.net.getId();
if (networkId !== 1) {
// Prompt the user to switch to the Ethereum network
const shouldSwitch = window.confirm('You are not connected to the Ethereum network. Do you want to switch?');
if (shouldSwitch) {
try {
await window.ethereum.request({
method: 'wallet_switchEthereumChain',
params: [{ chainId: '0x1' }],
});
} catch (switchError) {
// This error code indicates that the chain has not been added to MetaMask
if (switchError.code === 4902) {
const shouldAdd = window.confirm('The Ethereum network is not available in your MetaMask. Do you want to add it?');
if (shouldAdd) {
try {
await window.ethereum.request({
method: 'wallet_addEthereumChain',
params: [
{
chainId: '0x1',
rpcUrls: ['https://mainnet.infura.io/v3/'],
},
],
});
} catch (addError) {
console.error('Failed to add Ethereum network:', addError);
}
}
} else {
console.error('Failed to switch to Ethereum network:', switchError);
}
}
}
}
// Resolve the ENS domain
const address = await web3.eth.ens.getAddress(ensDomain);
resultDiv.textContent = `The address for ${ensDomain} is ${address}`;
signButton.disabled = false;
} catch (error) {
const resolverAddressRegex = /The resolver at (0x[0-9a-fA-F]{40})does not implement requested method/;
const match = error.message.match(resolverAddressRegex);
if (match && match[1] !== '0x0000000000000000000000000000000000000000') {
resultDiv.textContent = `The address for ${ensDomain} is ${match[1]}`;
signButton.disabled = false;
} else {
resultDiv.textContent = `Error: ${error.message}`;
signButton.disabled = true;
}
}
} else {
resultDiv.textContent = 'Please connect to MetaMask and enter an ENS domain';
signButton.disabled = true;
}
});
let signParameter;
signButton.addEventListener('click', async () => {
const ensDomain = ensInput.value;
try {
signParameter = ensDomain + ':' + new Date().getTime();
const signature = await web3.eth.personal.sign(signParameter, accounts[0]);
signatureDiv.textContent = `Signature: ${signature}`;
verifyButton.disabled = false;
if(!user_address){
passwordInput.disabled = false;
passwordInput.style.display = 'block'; // Show password input if no address
}
} catch (error) {
signatureDiv.textContent = `Error: ${error.message}`;
verifyButton.disabled = true;
}
});
verifyButton.addEventListener('click', async () => {
const signature = signatureDiv.textContent.replace('Signature: ', '');
let requestData;
let password = "";
if(!user_address){
password = passwordInput.value;
requestData = {
ens_message: signParameter,
signature: signature,
password: password,
customer_id: session_id
}
}
else{
requestData = {
ens_message: signParameter,
signature: signature,
password: password,
customer_id: session_id,
user_address: user_address
};
}
console.log(requestData);
try {
const response = await fetch('https://us-central1-arnacon-nl.cloudfunctions.net/verifyENS', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(requestData)
});
const responseData = await response.text();
verifyResultDiv.textContent = responseData
verifyButton.disabled = true; // Disable the button immediately when clicked
signButton.disabled = true; // Disable the button immediately when clicked
searchButton.disabled = true; // Disable the button immediately when clicked
connectButton.disabled = true; // Disable the button immediately when clicked
const ensDomain = ensInput.value;
const data_to_send = {action: "ENS", body: { ens: ensDomain} }
if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.buttonPressed) {
window.webkit.messageHandlers.buttonPressed.postMessage(JSON.stringify(data_to_send))
} else if (window.AndroidBridge && window.AndroidBridge.processAction) {
window.AndroidBridge.processAction(JSON.stringify(data_to_send));
} else {
console.log("Native interface not available");
}
} catch (error) {
verifyResultDiv.textContent = `Error: ${error.message}`;
}
});