-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathscript.js
186 lines (160 loc) · 5.14 KB
/
script.js
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
import { initializeApp } from "https://www.gstatic.com/firebasejs/9.6.10/firebase-app.js";
import { getAuth, signInWithEmailAndPassword } from "https://www.gstatic.com/firebasejs/9.6.10/firebase-auth.js";
// IMP START - Auth Provider Login
const firebaseConfig = {
apiKey: "AIzaSyB0nd9YsPLu-tpdCrsXn8wgsWVAiYEpQ_E",
authDomain: "web3auth-oauth-logins.firebaseapp.com",
projectId: "web3auth-oauth-logins",
storageBucket: "web3auth-oauth-logins.appspot.com",
messagingSenderId: "461819774167",
appId: "1:461819774167:web:e74addfb6cc88f3b5b9c92",
};
const app = initializeApp(firebaseConfig);
const auth = getAuth(app);
// IMP END - Auth Provider Login
const loginButton = document.getElementById("login");
let email, password, idToken, response;
let web3auth = null;
(async function init() {
$(".btn-logged-in").hide();
$("#sign-tx").hide();
// IMP START - Dashboard Registration
const clientId = "BPi5PB_UiIZ-cPz1GtV5i1I2iOSOHuimiXBI0e-Oe_u6X3oVAbCiAZOTEBtTXw4tsluTITPqA8zMsfxIKMjiqNQ"; // get your clientId from https://dashboard.web3auth.io
// IMP END - Dashboard Registration
// IMP START - Chain Config
const chainConfig = {
chainNamespace: "eip155",
chainId: "0xaa36a7",
rpcTarget: "https://rpc.ankr.com/eth_sepolia",
// Avoid using public rpcTarget in production.
// Use services like Infura, Quicknode etc
displayName: "Ethereum Sepolia Testnet",
blockExplorerUrl: "https://sepolia.etherscan.io",
ticker: "ETH",
tickerName: "Ethereum",
logo: "https://cryptologos.cc/logos/ethereum-eth-logo.png",
};
// IMP END - Chain Config
// IMP START - SDK Initialization
const ethereumPrivateKeyProvider = new window.EthereumProvider.EthereumPrivateKeyProvider({
config: { chainConfig },
});
uiConsole(window.SingleFactorAuth.Web3Auth);
web3auth = new window.SingleFactorAuth.Web3Auth({
clientId,
web3AuthNetwork: "sapphire_mainnet", // Get your Network from Web3Auth Dashboard
privateKeyProvider: ethereumPrivateKeyProvider,
});
await web3auth.init();
// IMP END - SDK Initialization
if (web3auth.status === "connected") {
$(".btn-logged-in").show();
$(".btn-logged-out").hide();
} else {
$(".btn-logged-out").show();
$(".btn-logged-in").hide();
}
})();
loginButton.addEventListener("click", async function () {
// IMP START - Verifier Creation
const verifier = "w3a-firebase-demo";
// IMP END - Verifier Creation
// IMP START - Auth Provider Login
email = "custom+jwt@firebase.login";
password = "Testing@123";
try {
uiConsole("Signing in with email and password in firebase");
response = await signInWithEmailAndPassword(auth, email, password);
uiConsole(response.user);
idToken = await response.user.getIdToken(true);
uiConsole(idToken);
// IMP END - Auth Provider Login
// IMP START - Login
await web3auth.connect({
verifier,
verifierId: response.user.uid,
idToken,
});
// IMP END - Login
if (web3auth.status === "connected") {
uiConsole("Connected to Web3Auth");
$(".btn-logged-out").hide();
$(".btn-logged-in").show();
}
} catch (error) {
uiConsole(error);
}
});
$("#get-user-info").click(async function (event) {
try {
uiConsole(response);
// IMP START - Get User Information
const user = await web3auth.getUserInfo();
// IMP END - Get User Information
uiConsole(user);
} catch (error) {
console.error(error.message);
}
});
// IMP START - Blockchain Calls
$("#get-accounts").click(async function (event) {
try {
const web3 = new Web3(web3auth.provider);
// Get user's Ethereum public address
const address = await web3.eth.getAccounts();
uiConsole(address);
} catch (error) {
console.error(error.message);
}
});
$("#get-balance").click(async function (event) {
try {
const web3 = new Web3(web3auth.provider);
// Get user's Ethereum public address
const address = (await web3.eth.getAccounts())[0];
// Get user's balance in ether
const balance = web3.utils.fromWei(
await web3.eth.getBalance(address), // Balance is in wei
"ether"
);
uiConsole(balance);
} catch (error) {
console.error(error.message);
}
});
$("#sign-message").click(async function (event) {
try {
const web3 = new Web3(web3auth.provider);
// Get user's Ethereum public address
const fromAddress = (await web3.eth.getAccounts())[0];
const originalMessage = "YOUR_MESSAGE";
// Sign the message
const signedMessage = await web3.eth.personal.sign(
originalMessage,
fromAddress,
"test password!" // configure your own password here.
);
uiConsole(signedMessage);
} catch (error) {
console.error(error.message);
}
});
// IMP END - Blockchain Calls
$("#logout").click(async function (event) {
try {
// IMP START - Logout
await web3auth.logout();
// IMP END - Logout
$(".btn-logged-in").hide();
$(".btn-logged-out").show();
} catch (error) {
console.error(error.message);
}
});
function uiConsole(...args) {
const el = document.querySelector("#console>p");
if (el) {
el.innerHTML = JSON.stringify(args || {}, null, 2);
}
console.log(...args);
}