Skip to content

Commit 71aae19

Browse files
Merge pull request #358 from Web3Auth/feat/external-wallets-auth
authenticate user function added to return id token
2 parents 0d6a58d + 7e97396 commit 71aae19

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+2844
-2283
lines changed

demo/vue-app/package-lock.json

Lines changed: 451 additions & 491 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

demo/vue-app/package.json

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,24 @@
1414
"@solana/web3.js": "^1.50.1",
1515
"@toruslabs/openlogin-ed25519": "^2.0.0",
1616
"@walletconnect/qrcode-modal": "~1.7.8",
17-
"@web3auth/base": "^1.1.2",
18-
"@web3auth/core": "^1.1.2",
19-
"@web3auth/torus-wallet-connector-plugin": "^1.1.2",
17+
"@web3auth/base": "file:../../packages/base",
18+
"@web3auth/base-solana-adapter": "file:../../packages/base-solana-adapter",
19+
"@web3auth/base-evm-adapter": "file:../../packages/base-evm-adapter",
20+
"@web3auth/coinbase-adapter": "^1.1.2",
21+
"@web3auth/core": "file:../../packages/core",
22+
"@web3auth/metamask-adapter": "file:../../packages/adapters/metamask-adapter",
23+
"@web3auth/openlogin-adapter": "file:../../packages/adapters/openlogin-adapter",
24+
"@web3auth/phantom-adapter": "file:../../packages/adapters/phantom-adapter",
25+
"@web3auth/slope-adapter": "file:../../packages/adapters/slope-adapter",
26+
"@web3auth/solana-provider": "file:../../packages/providers/solana-provider",
2027
"@web3auth/solana-wallet-connector-plugin": "^1.1.2",
21-
"@web3auth/metamask-adapter": "^1.1.2",
22-
"@web3auth/openlogin-adapter": "^1.1.2",
23-
"@web3auth/phantom-adapter": "^1.1.2",
24-
"@web3auth/slope-adapter": "^1.1.2",
2528
"@web3auth/solflare-adapter": "^1.1.2",
26-
"@web3auth/solana-provider": "^1.1.2",
27-
"@web3auth/sollet-adapter": "^1.1.2",
28-
"@web3auth/torus-evm-adapter": "^1.1.2",
29-
"@web3auth/torus-solana-adapter": "^1.1.2",
30-
"@web3auth/coinbase-adapter": "^1.1.2",
31-
"@web3auth/wallet-connect-v1-adapter": "^1.1.2",
32-
"@web3auth/web3auth": "^1.1.2",
29+
"@web3auth/sollet-adapter":"file:../../packages/adapters/sollet-adapter",
30+
"@web3auth/torus-evm-adapter": "^1.1.4",
31+
"@web3auth/torus-solana-adapter": "file:../../packages/adapters/torus-solana-adapter",
32+
"@web3auth/torus-wallet-connector-plugin": "^1.1.2",
33+
"@web3auth/wallet-connect-v1-adapter": "^1.1.4",
34+
"@web3auth/web3auth": "file:../../packages/modal",
3335
"lodash.merge": "^4.6.2",
3436
"ts-custom-error": "^3.2.0",
3537
"vue": "^2.6.14",

demo/vue-app/src/chains/ethereum.vue

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<button class="rpcBtn" v-if="!provider" @click="connect" style="cursor: pointer">Connect</button>
1212
<button class="rpcBtn" v-if="provider" @click="logout" style="cursor: pointer">Logout</button>
1313
<button class="rpcBtn" v-if="provider" @click="getUserInfo" style="cursor: pointer">Get User Info</button>
14+
<button class="rpcBtn" v-if="provider" @click="authenticateUser" style="cursor: pointer">Get Auth Id token</button>
1415
<EthRpc :connectedAdapter="web3auth.connectedAdapterName" v-if="provider" :provider="provider" :console="console"></EthRpc>
1516
<span>{{ connecting }}</span>
1617

@@ -205,6 +206,10 @@ export default Vue.extend({
205206
const userInfo = await this.web3auth.getUserInfo();
206207
this.console(userInfo);
207208
},
209+
async authenticateUser() {
210+
const idTokenDetails = await this.web3auth.authenticateUser();
211+
this.console(idTokenDetails);
212+
},
208213
console(...args: unknown[]): void {
209214
const el = document.querySelector("#console>p");
210215
if (el) {

demo/vue-app/src/chains/solana.vue

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<button class="rpcBtn" v-if="!provider" @click="connect" style="cursor: pointer">Connect</button>
1111
<button class="rpcBtn" v-if="provider" @click="logout" style="cursor: pointer">Logout</button>
1212
<button class="rpcBtn" v-if="provider" @click="getUserInfo" style="cursor: pointer">Get User Info</button>
13+
<button class="rpcBtn" v-if="provider" @click="authenticateUser" style="cursor: pointer">Get Auth Id token</button>
1314
<SolanaRpc v-if="provider" :provider="provider" :console="console"></SolanaRpc>
1415
<!-- <button @click="showError" style="cursor: pointer">Show Error</button> -->
1516
</section>
@@ -212,6 +213,10 @@ export default Vue.extend({
212213
const userInfo = await this.web3auth.getUserInfo();
213214
this.console(userInfo);
214215
},
216+
async authenticateUser() {
217+
const idTokenDetails = await this.web3auth.authenticateUser();
218+
this.console(idTokenDetails);
219+
},
215220
console(...args: unknown[]): void {
216221
const el = document.querySelector("#console>p");
217222
if (el) {

demo/vue-app/src/lib/eth.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export const signEthMessage = async (provider: SafeEventEmitterProvider, uiConso
6767
}
6868
};
6969

70-
export const getAccounts = async (provider: SafeEventEmitterProvider, uiConsole: any): Promise<string[]> => {
70+
export const getAccounts = async (provider: SafeEventEmitterProvider, uiConsole: any): Promise<string[] | undefined> => {
7171
try {
7272
const web3 = new Web3(provider as any);
7373
const accounts = await web3.eth.getAccounts();
@@ -78,7 +78,7 @@ export const getAccounts = async (provider: SafeEventEmitterProvider, uiConsole:
7878
uiConsole("error", error);
7979
}
8080
};
81-
export const getChainId = async (provider: SafeEventEmitterProvider, uiConsole: any): Promise<string> => {
81+
export const getChainId = async (provider: SafeEventEmitterProvider, uiConsole: any): Promise<string | undefined> => {
8282
try {
8383
const web3 = new Web3(provider as any);
8484
const chainId = await web3.eth.getChainId();

demo/vue-app/src/lib/sol.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function getNewTx(publicKeys, blockhash) {
1818
return new Transaction({ recentBlockhash: blockhash, feePayer: new PublicKey(publicKeys![0]) }).add(inst);
1919
}
2020

21-
export const getAccounts = async (provider: SafeEventEmitterProvider, uiConsole: any): Promise<string[]> => {
21+
export const getAccounts = async (provider: SafeEventEmitterProvider, uiConsole: any): Promise<string[] | undefined> => {
2222
try {
2323
const solWeb3 = new SolanaWallet(provider);
2424
const acc = await solWeb3.requestAccounts();

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,4 @@
8282
"resolutions": {
8383
"@typescript-eslint/eslint-plugin": "^5.32.0"
8484
}
85-
}
85+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.d.ts
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Web3Auth Base Evm Adapter (Basic Types and Interface for Web3Auth)
2+
3+
[![npm version](https://img.shields.io/npm/v/@web3auth/base-evm-adapter?label=%22%22)](https://www.npmjs.com/package/@web3auth/base-evm-adapter/v/latest)
4+
[![minzip](https://img.shields.io/bundlephobia/minzip/@web3auth/base?label=%22%22)](https://bundlephobia.com/result?p=@web3auth/base-evm-adapter@latest)
5+
6+
> Web3Auth is where passwordless auth meets non-custodial key infrastructure for Web3 apps and wallets. By aggregating OAuth (Google, Twitter, Discord) logins, different wallets and innovative Multi Party Computation (MPC) - Web3Auth provides a seamless login experience to every user on your application.
7+
8+
Web3Auth Base Evm Adapter contains a base class for EVM adapters.
9+
10+
## 📖 Documentation
11+
12+
Read more about the Web3Auth in the [official Web3Auth Documentation](https://web3auth.io/docs/sdk/web/).
13+
14+
## 🔗 Installation
15+
16+
```shell
17+
npm install --save @web3auth/base-evm-adapter
18+
```
19+
20+
## 🩹 Example
21+
22+
```ts
23+
import { BaseEvmAdapter } from "@web3auth/base-evm-adapter";
24+
25+
26+
export class EvmAdapter extends BaseEvmAdapter<void> {
27+
}
28+
```
29+
30+
Checkout the examples for your preferred blockchain and platform in our [examples repository](https://github.com/Web3Auth/examples/)
31+
32+
## 🌐 Demo
33+
Check out the [Web3Auth](https://demo-app.web3auth.io/) Demo](https://demo-app.web3auth.io/) to see how Web3Auth can be used in your application.
34+
35+
## 💬 Troubleshooting and Discussions
36+
37+
- Have a look at our [GitHub Discussions](https://github.com/Web3Auth/Web3Auth/discussions?discussions_q=sort%3Atop) to see if anyone has any questions or issues you might be having.
38+
- Checkout our [Troubleshooting Documentation Page](https://web3auth.io/docs/troubleshooting) to know the common issues and solutions
39+
- Join our [Discord](https://discord.gg/web3auth) to join our community and get private integration support or help with your integration.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
"name": "@web3auth/base-evm-adapter",
3+
"version": "1.1.4",
4+
"homepage": "https://github.com/Web3Auth/Web3Auth#readme",
5+
"license": "ISC",
6+
"main": "dist/baseEvmAdapter.cjs.js",
7+
"module": "dist/baseEvmAdapter.esm.js",
8+
"unpkg": "dist/baseEvmAdapter.umd.min.js",
9+
"jsdelivr": "dist/baseEvmAdapter.umd.min.js",
10+
"types": "dist/types/index.d.ts",
11+
"author": "Torus Labs",
12+
"scripts": {
13+
"test": "mocha --config ../../.mocharc.json test/**.ts",
14+
"test-debugger": "mocha --config ../../.mocharc.json --inspect-brk test/**.ts",
15+
"dev": "torus-scripts start",
16+
"build": "torus-scripts build",
17+
"lint": "eslint --fix 'src/**/*.ts'",
18+
"prepack": "yarn run build",
19+
"pre-commit": "lint-staged --cwd ."
20+
},
21+
"dependencies": {
22+
"@web3auth/base": "^1.1.3"
23+
},
24+
"files": [
25+
"dist",
26+
"src"
27+
],
28+
"peerDependencies": {
29+
"@babel/runtime": "^7.x"
30+
},
31+
"lint-staged": {
32+
"!(*d).ts": [
33+
"eslint --cache --fix",
34+
"prettier --write"
35+
]
36+
},
37+
"repository": {
38+
"type": "git",
39+
"url": "git+https://github.com/Web3Auth/Web3Auth.git"
40+
},
41+
"bugs": {
42+
"url": "https://github.com/Web3Auth/Web3Auth/issues"
43+
},
44+
"keywords": [],
45+
"publishConfig": {
46+
"access": "public"
47+
}
48+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import {
2+
ADAPTER_STATUS,
3+
BaseAdapter,
4+
checkIfTokenIsExpired,
5+
clearToken,
6+
getSavedToken,
7+
saveToken,
8+
signChallenge,
9+
UserAuthInfo,
10+
verifySignedChallenge,
11+
WalletLoginError,
12+
} from "@web3auth/base";
13+
14+
export abstract class BaseEvmAdapter<T> extends BaseAdapter<T> {
15+
async authenticateUser(): Promise<UserAuthInfo> {
16+
if (!this.provider || !this.chainConfig?.chainId) throw WalletLoginError.notConnectedError();
17+
18+
const { chainNamespace, chainId } = this.chainConfig;
19+
20+
if (this.status !== ADAPTER_STATUS.CONNECTED) throw WalletLoginError.notConnectedError("Not connected with wallet, Please login/connect first");
21+
const accounts = await this.provider.request<string[]>({
22+
method: "eth_accounts",
23+
});
24+
if (accounts && accounts.length > 0) {
25+
const existingToken = getSavedToken(accounts[0] as string, this.name);
26+
if (existingToken) {
27+
const isExpired = checkIfTokenIsExpired(existingToken);
28+
if (!isExpired) {
29+
return { idToken: existingToken };
30+
}
31+
}
32+
33+
const payload = {
34+
domain: window.location.origin,
35+
uri: window.location.href,
36+
address: accounts[0],
37+
chainId: parseInt(chainId, 16),
38+
version: "1",
39+
nonce: Math.random().toString(36).slice(2),
40+
issuedAt: new Date().toISOString(),
41+
};
42+
43+
const challenge = await signChallenge(payload, chainNamespace);
44+
45+
const signedMessage = await this.provider.request<string>({
46+
method: "personal_sign",
47+
params: [challenge, accounts[0]],
48+
});
49+
50+
const idToken = await verifySignedChallenge(chainNamespace, signedMessage as string, challenge, this.name, this.sessionTime);
51+
saveToken(accounts[0] as string, this.name, idToken);
52+
return {
53+
idToken,
54+
};
55+
}
56+
throw WalletLoginError.notConnectedError("Not connected with wallet, Please login/connect first");
57+
}
58+
59+
async disconnect(): Promise<void> {
60+
if (this.status !== ADAPTER_STATUS.CONNECTED) throw WalletLoginError.disconnectionError("Not connected with wallet");
61+
const accounts = await this.provider.request<string[]>({
62+
method: "eth_accounts",
63+
});
64+
if (accounts && accounts.length > 0) {
65+
clearToken(accounts[0], this.name);
66+
}
67+
}
68+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "./baseEvmAdapter";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require("../../../torus.config");
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"include": ["src"]
3+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"extends": "../../../tsconfig.json",
3+
"include": ["src","test"]
4+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/* eslint-disable @typescript-eslint/no-var-requires */
2+
const path = require("path");
3+
const generateWebpackConfig = require("../../../webpack.config");
4+
5+
const pkg = require("./package.json");
6+
7+
const currentPath = path.resolve(".");
8+
9+
const config = generateWebpackConfig({ currentPath, pkg });
10+
11+
module.exports = config;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.d.ts
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Web3Auth Base solana Adapter (Basic Types and Interface for Web3Auth)
2+
3+
[![npm version](https://img.shields.io/npm/v/@web3auth/base-solana-adapter?label=%22%22)](https://www.npmjs.com/package/@web3auth/base-solana-adapter/v/latest)
4+
[![minzip](https://img.shields.io/bundlephobia/minzip/@web3auth/base?label=%22%22)](https://bundlephobia.com/result?p=@web3auth/base-solana-adapter@latest)
5+
6+
> Web3Auth is where passwordless auth meets non-custodial key infrastructure for Web3 apps and wallets. By aggregating OAuth (Google, Twitter, Discord) logins, different wallets and innovative Multi Party Computation (MPC) - Web3Auth provides a seamless login experience to every user on your application.
7+
8+
Web3Auth Base solana Adapter contains a base class for solana adapters.
9+
10+
## 📖 Documentation
11+
12+
Read more about the Web3Auth in the [official Web3Auth Documentation](https://web3auth.io/docs/sdk/web/).
13+
14+
## 🔗 Installation
15+
16+
```shell
17+
npm install --save @web3auth/base-solana-adapter
18+
```
19+
20+
## 🩹 Example
21+
22+
```ts
23+
import { BaseSolanaAdapter } from "@web3auth/base-solana-adapter";
24+
25+
26+
export class solanaAdapter extends BaseSolanaAdapter<void> {
27+
}
28+
```
29+
30+
Checkout the examples for your preferred blockchain and platform in our [examples repository](https://github.com/Web3Auth/examples/)
31+
32+
## 🌐 Demo
33+
34+
Checkout the [Web3Auth Demo](https://demo-app.web3auth.io/) to see how Web3Auth can be used in your application.
35+
36+
## 💬 Troubleshooting and Discussions
37+
38+
- Have a look at our [GitHub Discussions](https://github.com/Web3Auth/Web3Auth/discussions?discussions_q=sort%3Atop) to see if anyone has any questions or issues you might be having.
39+
- Checkout our [Troubleshooting Documentation Page](https://web3auth.io/docs/troubleshooting) to know the common issues and solutions
40+
- Join our [Discord](https://discord.gg/web3auth) to join our community and get private integration support or help with your integration.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
{
2+
"name": "@web3auth/base-solana-adapter",
3+
"version": "1.1.4",
4+
"homepage": "https://github.com/Web3Auth/Web3Auth#readme",
5+
"license": "ISC",
6+
"main": "dist/baseSolanaAdapter.cjs.js",
7+
"module": "dist/baseSolanaAdapter.esm.js",
8+
"unpkg": "dist/baseSolanaAdapter.umd.min.js",
9+
"jsdelivr": "dist/baseSolanaAdapter.umd.min.js",
10+
"types": "dist/types/index.d.ts",
11+
"author": "Torus Labs",
12+
"scripts": {
13+
"test": "mocha --config ../../.mocharc.json test/**.ts",
14+
"test-debugger": "mocha --config ../../.mocharc.json --inspect-brk test/**.ts",
15+
"dev": "torus-scripts start",
16+
"build": "torus-scripts build",
17+
"lint": "eslint --fix 'src/**/*.ts'",
18+
"prepack": "yarn run build",
19+
"pre-commit": "lint-staged --cwd ."
20+
},
21+
"dependencies": {
22+
"@web3auth/base": "^1.1.3",
23+
"bs58": "^5.0.0"
24+
},
25+
"files": [
26+
"dist",
27+
"src"
28+
],
29+
"peerDependencies": {
30+
"@babel/runtime": "^7.x"
31+
},
32+
"lint-staged": {
33+
"!(*d).ts": [
34+
"eslint --cache --fix",
35+
"prettier --write"
36+
]
37+
},
38+
"repository": {
39+
"type": "git",
40+
"url": "git+https://github.com/Web3Auth/Web3Auth.git"
41+
},
42+
"bugs": {
43+
"url": "https://github.com/Web3Auth/Web3Auth/issues"
44+
},
45+
"keywords": [],
46+
"publishConfig": {
47+
"access": "public"
48+
}
49+
}

0 commit comments

Comments
 (0)