Skip to content

Commit faee8bd

Browse files
Feature/web3auth (#1056)
* Adds Web3Auth * Complete web3auth module * Update logo - remove yarn.lock * Update yarn lock
1 parent 0d0e9df commit faee8bd

File tree

7 files changed

+1119
-31
lines changed

7 files changed

+1119
-31
lines changed

.circleci/config.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,12 @@ jobs:
267267
working_directory: ~/web3-onboard-monorepo/packages/coinbase
268268
steps:
269269
- node-build-steps
270+
build-web3auth:
271+
docker:
272+
- image: cimg/node:16.13.1
273+
working_directory: ~/web3-onboard-monorepo/packages/web3auth
274+
steps:
275+
- node-build-steps
270276
build-dcent:
271277
docker:
272278
- image: cimg/node:16.13.1
@@ -383,6 +389,12 @@ jobs:
383389
working_directory: ~/web3-onboard-monorepo/packages/coinbase
384390
steps:
385391
- node-staging-build-steps
392+
build-staging-web3auth:
393+
docker:
394+
- image: cimg/node:16.13.1
395+
working_directory: ~/web3-onboard-monorepo/packages/web3auth
396+
steps:
397+
- node-build-steps
386398
build-staging-dcent:
387399
docker:
388400
- image: cimg/node:16.13.1
@@ -501,6 +513,12 @@ workflows:
501513
<<: *deploy_production_filters
502514
- build-staging-coinbase:
503515
<<: *deploy_staging_filters
516+
web3auth:
517+
jobs:
518+
- build-web3auth:
519+
<<: *deploy_production_filters
520+
- build-staging-web3auth:
521+
<<: *deploy_staging_filters
504522
dcent:
505523
jobs:
506524
- build-dcent:

packages/demo/src/App.svelte

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import walletConnectModule from '@web3-onboard/walletconnect'
1313
import coinbaseModule from '@web3-onboard/coinbase'
1414
import magicModule from '@web3-onboard/magic'
15+
import web3authModule from '@web3-onboard/web3auth'
16+
1517
import dcentModule from '@web3-onboard/dcent'
1618
import {
1719
recoverAddress,
@@ -69,6 +71,10 @@
6971
apiKey: 'pk_test_886ADCAB855632AA'
7072
})
7173
74+
const web3auth = web3authModule({
75+
clientId: 'BJuUOKvmNnlzy6ruVgeWYWIMKLRyYtjYa9Y10VCeJzWZcygDlrYLyXsBQjpJ2hxlBO9dnl8t9GmAC2rOP5vnIGo'
76+
})
77+
7278
const torus = torusModule()
7379
const ledger = ledgerModule()
7480
const keepkey = keepkeyModule()
@@ -92,6 +98,7 @@
9298
9399
const onboard = Onboard({
94100
wallets: [
101+
web3auth,
95102
ledger,
96103
trezor,
97104
walletConnect,

packages/web3auth/package.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "@web3-onboard/web3auth",
3+
"version": "2.0.0",
4+
"description": "Web3Auth module for web3-onboard",
5+
"module": "dist/index.js",
6+
"browser": "dist/index.js",
7+
"main": "dist/index.js",
8+
"type": "module",
9+
"typings": "dist/index.d.ts",
10+
"files": [
11+
"dist"
12+
],
13+
"scripts": {
14+
"build": "tsc",
15+
"dev": "tsc -w",
16+
"type-check": "tsc --noEmit"
17+
},
18+
"license": "MIT",
19+
"devDependencies": {
20+
"typescript": "^4.5.5"
21+
},
22+
"dependencies": {
23+
"@web3-onboard/common": "^2.0.7",
24+
"@web3auth/web3auth": "^1.0.0"
25+
}
26+
}

packages/web3auth/src/icon.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export default `
2+
<svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="0 0 128 128" fill="none"><script xmlns="" type="text/javascript"/>
3+
<circle cx="64.6887" cy="64.6882" r="59.1828" fill="#0364FF"/>
4+
<path fill-rule="evenodd" clip-rule="evenodd" d="M88.8746 85.5612C88.474 87.056 86.353 87.056 85.9524 85.5612L81.2873 68.1506C81.1971 67.8139 81.1971 67.4595 81.2873 67.1229L85.4214 51.694C85.7869 50.3301 87.0229 49.3817 88.4349 49.3817H94.5031C96.5544 49.3817 98.0475 51.3275 97.5165 53.309L88.8746 85.5612ZM48.8481 68.1215C48.9383 67.7849 48.9383 67.4305 48.8481 67.0938L44.7217 51.694C44.3562 50.3301 43.1203 49.3817 41.7082 49.3817H35.6401C33.5887 49.3817 32.0956 51.3275 32.6266 53.309L41.2607 85.5321C41.6613 87.0269 43.7824 87.0269 44.1829 85.5321L48.8481 68.1215Z" fill="white"/>
5+
<path fill-rule="evenodd" clip-rule="evenodd" d="M61.9907 89.069C61.854 89.5793 62.2385 90.0805 62.7669 90.0805H67.3947C67.9231 90.0805 68.3077 89.5793 68.1709 89.069L65.857 80.4334C65.6442 79.6392 64.5174 79.6392 64.3046 80.4334L61.9907 89.069Z" fill="white"/>
6+
<path fill-rule="evenodd" clip-rule="evenodd" d="M57.3829 94.5979C57.0285 95.9205 55.83 96.8402 54.4607 96.8402H48.1968C46.2076 96.8402 44.7598 94.9533 45.2747 93.0319L58.1248 45.0747C58.5124 43.6281 59.8233 42.6222 61.3209 42.6222H68.7757C70.2733 42.6222 71.5842 43.6281 71.9718 45.0747L84.822 93.0319C85.3368 94.9533 83.889 96.8402 81.8998 96.8402H75.6359C74.2666 96.8402 73.0681 95.9205 72.7137 94.5979L66.4637 71.2726C66.0757 69.8245 64.0209 69.8245 63.6329 71.2726L57.3829 94.5979Z" fill="white"/>
7+
<path fill-rule="evenodd" clip-rule="evenodd" d="M88.8909 85.5612C88.4903 87.056 86.3692 87.056 85.9687 85.5612L81.3035 68.1506C81.2133 67.8139 81.2133 67.4595 81.3035 67.1229L87.2489 44.9345C87.6144 43.5706 88.8503 42.6222 90.2624 42.6222H96.3305C98.3819 42.6222 99.8749 44.568 99.344 46.5494L88.8909 85.5612ZM48.8643 68.1215C48.9545 67.7849 48.9545 67.4305 48.8643 67.0938L42.9268 44.9345C42.5613 43.5706 41.3253 42.6222 39.9133 42.6222H33.8451C31.7938 42.6222 30.3007 44.568 30.8317 46.5494L41.277 85.5321C41.6776 87.0269 43.7986 87.0269 44.1992 85.5321L48.8643 68.1215Z" fill="white"/>
8+
</svg>
9+
`

packages/web3auth/src/index.ts

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
import type {
2+
Chain,
3+
EIP1193Provider,
4+
ProviderAccounts,
5+
WalletInit
6+
} from '@web3-onboard/common'
7+
8+
import type { ModalConfig, Web3AuthOptions } from '@web3auth/web3auth'
9+
import type { CustomChainConfig, CONNECTED_EVENT_DATA } from '@web3auth/base'
10+
11+
type Web3AuthModuleOptions = Omit<Web3AuthOptions, 'chainConfig'> & {
12+
chainConfig?: Partial<CustomChainConfig> &
13+
Pick<CustomChainConfig, 'chainNamespace'>
14+
modalConfig?: Record<string, ModalConfig> | undefined
15+
}
16+
17+
function web3auth(options: Web3AuthModuleOptions): WalletInit {
18+
return () => ({
19+
label: 'Web3Auth',
20+
getIcon: async () => (await import('./icon.js')).default,
21+
getInterface: async ({ EventEmitter, chains }) => {
22+
const { Web3Auth } = await import('@web3auth/web3auth')
23+
const { CHAIN_NAMESPACES, ADAPTER_EVENTS } = await import(
24+
'@web3auth/base'
25+
)
26+
const { createEIP1193Provider, ProviderRpcError, ProviderRpcErrorCode } =
27+
await import('@web3-onboard/common')
28+
29+
const emitter = new EventEmitter()
30+
31+
let [currentChain] = chains
32+
33+
const getChainConfig = ({
34+
rpcUrl,
35+
namespace,
36+
id,
37+
token,
38+
label
39+
}: Chain) => ({
40+
chainConfig: {
41+
ticker: token,
42+
tickerName: label,
43+
chainId: id,
44+
rpcTarget: rpcUrl,
45+
chainNamespace:
46+
namespace === 'evm'
47+
? CHAIN_NAMESPACES.EIP155
48+
: CHAIN_NAMESPACES.OTHER
49+
}
50+
})
51+
52+
const web3authOptions = {
53+
...options,
54+
...getChainConfig(currentChain)
55+
}
56+
57+
let web3auth = new Web3Auth(web3authOptions)
58+
59+
const { modalConfig } = options || {}
60+
await web3auth.initModal(modalConfig)
61+
62+
let provider: EIP1193Provider
63+
64+
let web3AuthProvider = await web3auth.connect()
65+
66+
function patchProvider(): EIP1193Provider {
67+
const patchedProvider = createEIP1193Provider(web3AuthProvider, {
68+
eth_selectAccounts: null,
69+
eth_requestAccounts: async ({ baseRequest }) => {
70+
try {
71+
const accounts = await baseRequest({
72+
method: 'eth_accounts'
73+
})
74+
return accounts as ProviderAccounts
75+
} catch (error) {
76+
console.error(error)
77+
throw new ProviderRpcError({
78+
code: ProviderRpcErrorCode.ACCOUNT_ACCESS_REJECTED,
79+
message: 'Account access rejected'
80+
})
81+
}
82+
},
83+
84+
wallet_switchEthereumChain: async ({ params }) => {
85+
const chain = chains.find(({ id }) => id === params[0].chainId)
86+
if (!chain) throw new Error('Chain must be set before switching')
87+
currentChain = chain
88+
89+
// re-instantiate instance with new network
90+
web3auth = new Web3Auth({
91+
...web3authOptions,
92+
...getChainConfig(currentChain)
93+
})
94+
95+
await web3auth.initModal(modalConfig)
96+
97+
web3AuthProvider = await web3auth.connect()
98+
99+
emitter.emit('chainChanged', currentChain.id)
100+
101+
patchProvider()
102+
103+
return null
104+
}
105+
})
106+
107+
if (!provider) {
108+
patchedProvider.on = emitter.on.bind(emitter)
109+
patchedProvider.disconnect = () => web3auth.logout()
110+
111+
return patchedProvider
112+
} else {
113+
provider.request = patchedProvider.request.bind(patchedProvider)
114+
115+
// @ts-ignore - bind old methods for backwards compat
116+
provider.send = patchedProvider.send.bind(patchedProvider)
117+
118+
// @ts-ignore - bind old methods for backwards compat
119+
provider.sendAsync = patchedProvider.sendAsync.bind(patchedProvider)
120+
121+
return provider
122+
}
123+
}
124+
125+
provider = patchProvider()
126+
127+
return {
128+
provider,
129+
instance: web3auth
130+
}
131+
}
132+
})
133+
}
134+
135+
export default web3auth

packages/web3auth/tsconfig.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"extends": "../../tsconfig.json",
3+
"include": ["src/**/*"],
4+
5+
"compilerOptions": {
6+
"outDir": "dist",
7+
"rootDir": "src",
8+
"declarationDir": "dist",
9+
"paths": {
10+
"*": ["./src/*", "./node_modules/*"]
11+
},
12+
"typeRoots": ["node_modules/@types"]
13+
}
14+
}

0 commit comments

Comments
 (0)