Skip to content

Commit e2fc564

Browse files
authored
Merge pull request #21 from cometh-hq/develop
v1.1.6
2 parents 028bd62 + 1bc810a commit e2fc564

File tree

3 files changed

+106
-1615
lines changed

3 files changed

+106
-1615
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@cometh/connect-sdk-viem",
3-
"version": "1.1.5",
3+
"version": "1.1.6",
44
"main": "dist/index.js",
55
"types": "dist/index.d.ts",
66
"description": "Viem SDK Cometh Connect",
@@ -55,7 +55,7 @@
5555
},
5656
"dependencies": {
5757
"@babel/traverse": "^7.23.4",
58-
"@cometh/connect-sdk": "^1.2.10",
58+
"@cometh/connect-sdk": "^1.2.11",
5959
"@rainbow-me/rainbowkit": "^1.3.0",
6060
"@types/babel__core": "^7.20.0",
6161
"viem": "^1.16.2",

src/wagmi/connector.ts

Lines changed: 73 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
ComethWallet,
33
ConnectAdaptor,
4+
SupportedNetworks,
45
UIConfig,
56
webAuthnOptions
67
} from '@cometh/connect-sdk'
@@ -36,16 +37,20 @@ export type ComethConnectorOptions = WagmiConfigConnectorParams & {
3637
shimDisconnect?: boolean
3738
}
3839

40+
type ConnectFunctionConfig = {
41+
/** Target chain to connect to. */
42+
chainId?: number
43+
}
44+
3945
export class ComethConnectConnector extends Connector<
4046
undefined,
4147
ComethConnectorOptions
4248
> {
4349
id = 'cometh-connect'
4450
name = 'Cometh Connect'
45-
ready = true
46-
wallet: ComethWallet
47-
client: ConnectClient
48-
walletAddress?: string
51+
ready = false
52+
wallet?: ComethWallet
53+
client?: ConnectClient
4954

5055
protected shimDisconnectKey = `${this.id}.shimDisconnect`
5156

@@ -56,24 +61,21 @@ export class ComethConnectConnector extends Connector<
5661
chains: Chain[]
5762
options: WagmiConfigConnectorParams
5863
}) {
59-
if (chains.length !== 1)
60-
throw new Error('Cometh Connect does not support multi network in config')
61-
6264
const options = {
6365
shimDisconnect: true,
6466
...options_
6567
}
6668

67-
chains.forEach((chain) => {
68-
if (!isSupportedNetwork(toHex(chain.id)))
69-
console.warn(`${chain.name} not yet supported by cometh connect`)
70-
})
71-
7269
super({
73-
chains: chains.filter((chain) => isSupportedNetwork(toHex(chain.id))),
70+
chains,
7471
options
7572
})
7673

74+
this.ready = true
75+
}
76+
77+
/* eslint-disable */
78+
async connect({ chainId }: ConnectFunctionConfig = {}) {
7779
const {
7880
apiKey,
7981
walletAddress,
@@ -86,39 +88,30 @@ export class ComethConnectConnector extends Connector<
8688
rpcUrl
8789
} = this.options
8890

89-
const chainId = toHex(this.chains[0].id)
90-
91-
if (isSupportedNetwork(chainId)) {
92-
this.wallet = new ComethWallet({
93-
authAdapter: new ConnectAdaptor({
94-
chainId,
95-
apiKey,
96-
disableEoaFallback,
97-
encryptionSalt,
98-
webAuthnOptions,
99-
passKeyName,
100-
rpcUrl,
101-
baseUrl
102-
}),
91+
const selectedChainId = this._getSelectedChainId(chainId)
92+
93+
this.wallet = new ComethWallet({
94+
authAdapter: new ConnectAdaptor({
95+
chainId: selectedChainId,
10396
apiKey,
104-
uiConfig,
97+
disableEoaFallback,
98+
encryptionSalt,
99+
webAuthnOptions,
100+
passKeyName,
105101
rpcUrl,
106102
baseUrl
107-
})
103+
}),
104+
apiKey,
105+
uiConfig,
106+
rpcUrl,
107+
baseUrl
108+
})
108109

109-
this.client = getConnectViemClient({ wallet: this.wallet, apiKey })
110-
this.walletAddress = walletAddress
111-
this.ready = true
112-
} else {
113-
throw new Error('Network not supported')
114-
}
115-
}
110+
this.client = getConnectViemClient({ wallet: this.wallet, apiKey })
116111

117-
/* eslint-disable */
118-
async connect() {
119-
if (this.walletAddress) {
120-
await this.wallet.connect(this.walletAddress)
121-
window.localStorage.setItem('walletAddress', this.walletAddress)
112+
if (walletAddress) {
113+
await this.wallet.connect(walletAddress)
114+
window.localStorage.setItem('walletAddress', walletAddress)
122115
} else {
123116
const localStorageAddress = window.localStorage.getItem('walletAddress')
124117

@@ -133,10 +126,7 @@ export class ComethConnectConnector extends Connector<
133126
if (this.options.shimDisconnect)
134127
this.storage?.setItem(this.shimDisconnectKey, true)
135128

136-
console.warn(`Connected to ${this.chains[0].name}`)
137-
138-
if (this.chains.length > 1)
139-
console.warn(`Cometh connect does not support multichain`)
129+
console.warn(`Connected to ${selectedChainId}`)
140130

141131
return {
142132
account: this.wallet.getAddress() as `0x${string}`,
@@ -146,18 +136,55 @@ export class ComethConnectConnector extends Connector<
146136
}
147137
}
148138
}
139+
140+
private _getSelectedChainId(chainId?: number) {
141+
if (chainId) {
142+
const providedChainId = toHex(chainId)
143+
144+
if (!isSupportedNetwork(providedChainId))
145+
throw new Error('Network not supported')
146+
147+
return providedChainId
148+
} else {
149+
const supportedChains = this.chains.reduce(
150+
(chains: SupportedNetworks[], chain: Chain) => {
151+
const chainId = toHex(chain.id)
152+
if (!isSupportedNetwork(chainId)) {
153+
console.warn(`${chain.name} not yet supported by cometh connect`)
154+
} else {
155+
chains.push(chainId)
156+
}
157+
return chains
158+
},
159+
[]
160+
)
161+
if (supportedChains.length === 0)
162+
throw new Error('Cometh Connect does not support the provided chains')
163+
164+
if (supportedChains.length > 1)
165+
console.warn(`Cometh connect does not support multichain`)
166+
167+
return supportedChains[0]
168+
}
169+
}
149170
disconnect(): Promise<void> {
171+
if (!this.wallet) throw new Error('no')
150172
// Remove shim signalling wallet is disconnected
151173
if (this.options.shimDisconnect)
152174
this.storage?.removeItem(this.shimDisconnectKey)
153175

154176
return this.wallet.logout()
155177
}
156178
async getAccount(): Promise<`0x${string}`> {
179+
if (!this.wallet) throw new Error('no')
157180
return this.wallet.getAddress() as `0x${string}`
158181
}
159182
async getChainId(): Promise<number> {
160-
return await this.wallet.chainId
183+
if (!this.wallet) {
184+
return this.chains[0].id
185+
} else {
186+
return await this.wallet.chainId
187+
}
161188
}
162189
/* eslint-disable */
163190
async getProvider(): Promise<any> {

0 commit comments

Comments
 (0)