πŸ”Œ
Connecting to the wallet through JS and identifying the wallet's owner
Contributors: @rajkaria, @kb24x7​
  1. 1.
    User visits the website, at this point the wallet extension injects code into the window object so that the website's code can connect and interact with the wallet to get the wallet's details, perform actions, etc.
  2. 2.
    As soon as the user clicks the connect button, the JavaScript code in the front-end prompts the wallet to get an approval from the user to connect to that website.
  3. 3.
    If approved, the wallet returns the public key of that user for identification.

Identifying the wallet

While this example only includes code for Phantom and SolFlare, the process is pretty similar for other wallets as well.
1
// exportWallet.ts
2
export const getWallet = () => window.solana || window.solflare
Copied!
Now that we have the wallet exported, we can import it into our other files for us to utilize it.
1
// wallet.ts
2
import { getWallet } from './exportWallet.ts'
3
​
4
export const connectWallet = async () => {
5
const wallet = getWallet()
6
if (!wallet) {
7
window.open("https://phantom.app", "_blank");
8
return
9
}
10
const publicKey = await wallet.connect()
11
return publicKey
12
}
Copied!
See? It was that simple, now that we have the connect to wallet function ready, we can use the user's public key to save it to the DB and identify them as it will always be unique.
If there are no supported wallets detected, it automatically redirects the user to phantom's website.

FAQs

If you have any doubts related to this topic or you have a suggestion, feel free to create an issue here.
If you want to make edits to this topic or update the topic, please click on the "Edit on Github" link you see on the right section, make the changes and create a pull request to the master branch. One of the moderators will look at your changes & merge your changes if all looks good.
And yes, if you update anything in this topic, don't forget to add your GitHub username in the contributors' list!
Have a happy learning! ✌️