@tomo-inc/wallet-adaptor-base 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/project.json +0 -8
- package/src/wallet-api/connect.ts +2 -4
- package/src/wallets/providers/WalletConnectProvider.ts +20 -10
- package/src/wallets/providers/WalletConnectSolanaProvider.ts +32 -23
- package/src/wallets/wallet-walletconnect.ts +18 -9
- package/tsup.config.ts +3 -2
- package/dist/index.cjs +0 -3179
- package/dist/index.d.cts +0 -350
- package/dist/index.d.ts +0 -350
- package/dist/index.js +0 -3094
package/package.json
CHANGED
package/project.json
CHANGED
|
@@ -18,14 +18,6 @@
|
|
|
18
18
|
"cwd": "packages/wallet-adaptor-base"
|
|
19
19
|
}
|
|
20
20
|
},
|
|
21
|
-
"prepare": {
|
|
22
|
-
"executor": "nx:run-commands",
|
|
23
|
-
"dependsOn": ["build"],
|
|
24
|
-
"options": {
|
|
25
|
-
"command": "pnpm build",
|
|
26
|
-
"cwd": "packages/wallet-adaptor-base"
|
|
27
|
-
}
|
|
28
|
-
},
|
|
29
21
|
"lint": {
|
|
30
22
|
"executor": "nx:run-commands",
|
|
31
23
|
"options": {
|
|
@@ -42,14 +42,12 @@ export const connect = async (
|
|
|
42
42
|
if (provider?.connect) {
|
|
43
43
|
res = await provider.connect();
|
|
44
44
|
address = res?.publicKey?.toString() || "";
|
|
45
|
-
}
|
|
46
|
-
if (provider?.request) {
|
|
45
|
+
} else if (provider?.request) {
|
|
47
46
|
res = await provider.request({
|
|
48
47
|
method: "connect",
|
|
49
48
|
});
|
|
50
49
|
address = res?.publicKey?.toString() || "";
|
|
51
|
-
}
|
|
52
|
-
if (provider?.["standard:connect"]) {
|
|
50
|
+
} else if (provider?.["standard:connect"]) {
|
|
53
51
|
res = await provider["standard:connect"]?.connect();
|
|
54
52
|
address = res?.accounts?.[0]?.address || "";
|
|
55
53
|
}
|
|
@@ -20,6 +20,7 @@ export interface WalletConnectProviderConfig {
|
|
|
20
20
|
optionalChains?: string[];
|
|
21
21
|
methods?: string[];
|
|
22
22
|
events?: string[];
|
|
23
|
+
namespaces?: Record<string, NamespaceConfig>;
|
|
23
24
|
}
|
|
24
25
|
|
|
25
26
|
const defaultNamespaces: Record<string, NamespaceConfig> = {
|
|
@@ -47,6 +48,7 @@ export class WalletConnectProvider {
|
|
|
47
48
|
private sessionMap: Map<string, SessionInfo> = new Map();
|
|
48
49
|
public uri: string | null = null;
|
|
49
50
|
public qrCode: string | null = null;
|
|
51
|
+
private namespaces: Record<string, NamespaceConfig> = defaultNamespaces;
|
|
50
52
|
|
|
51
53
|
constructor(config: WalletConnectProviderConfig) {
|
|
52
54
|
this.client = new WalletConnectClient({
|
|
@@ -54,6 +56,8 @@ export class WalletConnectProvider {
|
|
|
54
56
|
metadata: config.metadata,
|
|
55
57
|
});
|
|
56
58
|
|
|
59
|
+
this.namespaces = config.namespaces || defaultNamespaces;
|
|
60
|
+
|
|
57
61
|
this.setupClientListeners();
|
|
58
62
|
}
|
|
59
63
|
|
|
@@ -107,13 +111,20 @@ export class WalletConnectProvider {
|
|
|
107
111
|
}
|
|
108
112
|
}
|
|
109
113
|
|
|
110
|
-
public async getUri(
|
|
114
|
+
public async getUri(): Promise<string> {
|
|
115
|
+
// Return existing URI if available to avoid creating duplicate proposals
|
|
116
|
+
if (this.uri) {
|
|
117
|
+
this.emit("uri_changed", { uri: this.uri });
|
|
118
|
+
return this.uri;
|
|
119
|
+
}
|
|
120
|
+
|
|
111
121
|
// Create connection and get URI
|
|
112
122
|
const uri = await this.client.connect({
|
|
113
|
-
requiredNamespaces:
|
|
123
|
+
requiredNamespaces: this.namespaces,
|
|
114
124
|
});
|
|
115
125
|
|
|
116
126
|
this.uri = uri;
|
|
127
|
+
this.emit("uri_changed", { uri: this.uri });
|
|
117
128
|
return uri;
|
|
118
129
|
}
|
|
119
130
|
/**
|
|
@@ -126,9 +137,11 @@ export class WalletConnectProvider {
|
|
|
126
137
|
sessions.forEach((session: SessionInfo) => {
|
|
127
138
|
this.sessionMap.set(session.topic, session);
|
|
128
139
|
});
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
140
|
+
|
|
141
|
+
// Only create new URI if we don't have an active session
|
|
142
|
+
if (sessions.length === 0) {
|
|
143
|
+
this.uri = await this.getUri();
|
|
144
|
+
}
|
|
132
145
|
}
|
|
133
146
|
|
|
134
147
|
/**
|
|
@@ -139,6 +152,8 @@ export class WalletConnectProvider {
|
|
|
139
152
|
async connect(): Promise<string[] | undefined> {
|
|
140
153
|
// Start waiting for connection in background (don't await)
|
|
141
154
|
// Connection will emit 'connect' event when established
|
|
155
|
+
this.uri = await this.getUri();
|
|
156
|
+
this.emit("uri_changed", { uri: this.uri });
|
|
142
157
|
const connected = await this.waitForConnection().catch((error) => {
|
|
143
158
|
console.error("WalletConnect connection failed:", error);
|
|
144
159
|
this.emit("error", error);
|
|
@@ -199,11 +214,6 @@ export class WalletConnectProvider {
|
|
|
199
214
|
if (this.accounts.length > 0) {
|
|
200
215
|
return this.accounts;
|
|
201
216
|
}
|
|
202
|
-
if (this.uri === null) {
|
|
203
|
-
this.uri = await this.getUri({
|
|
204
|
-
requiredNamespaces: defaultNamespaces,
|
|
205
|
-
});
|
|
206
|
-
}
|
|
207
217
|
return this.connect();
|
|
208
218
|
}
|
|
209
219
|
|
|
@@ -19,6 +19,7 @@ export interface WalletConnectSolanaProviderConfig {
|
|
|
19
19
|
chains?: string[];
|
|
20
20
|
methods?: string[];
|
|
21
21
|
events?: string[];
|
|
22
|
+
namespaces?: Record<string, NamespaceConfig>;
|
|
22
23
|
}
|
|
23
24
|
|
|
24
25
|
const defaultSolanaNamespaces: Record<string, NamespaceConfig> = {
|
|
@@ -39,12 +40,13 @@ const defaultSolanaNamespaces: Record<string, NamespaceConfig> = {
|
|
|
39
40
|
export class WalletConnectSolanaProvider {
|
|
40
41
|
private client: WalletConnectClient;
|
|
41
42
|
private session: SessionInfo | null = null;
|
|
42
|
-
private accounts: string[] = [];
|
|
43
|
+
private accounts: { publicKey: string }[] = [];
|
|
43
44
|
private chainId: string = "solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp"; // Default to Solana mainnet
|
|
44
45
|
private eventListeners: Map<string, Set<(...args: any[]) => void>> = new Map();
|
|
45
46
|
private sessionMap: Map<string, SessionInfo> = new Map();
|
|
46
47
|
public uri: string | null = null;
|
|
47
48
|
public qrCode: string | null = null;
|
|
49
|
+
private namespaces: Record<string, NamespaceConfig> = defaultSolanaNamespaces;
|
|
48
50
|
|
|
49
51
|
constructor(config: WalletConnectSolanaProviderConfig) {
|
|
50
52
|
this.client = new WalletConnectClient({
|
|
@@ -52,6 +54,8 @@ export class WalletConnectSolanaProvider {
|
|
|
52
54
|
metadata: config.metadata,
|
|
53
55
|
});
|
|
54
56
|
|
|
57
|
+
this.namespaces = config.namespaces || defaultSolanaNamespaces;
|
|
58
|
+
|
|
55
59
|
this.setupClientListeners();
|
|
56
60
|
}
|
|
57
61
|
|
|
@@ -94,11 +98,13 @@ export class WalletConnectSolanaProvider {
|
|
|
94
98
|
|
|
95
99
|
const allAccounts = Object.values(this.session.namespaces).flatMap((ns: any) => ns.accounts || []);
|
|
96
100
|
|
|
97
|
-
this.accounts = allAccounts
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
101
|
+
this.accounts = allAccounts
|
|
102
|
+
.filter((account: string) => account.startsWith("solana:"))
|
|
103
|
+
.map((account) => {
|
|
104
|
+
const parts = account.split(":");
|
|
105
|
+
// Solana account format: solana:chainId:pubkey
|
|
106
|
+
return { publicKey: parts[2] };
|
|
107
|
+
});
|
|
102
108
|
|
|
103
109
|
if (allAccounts.length > 0) {
|
|
104
110
|
const chainIdPart = allAccounts[0].split(":")[1];
|
|
@@ -106,13 +112,20 @@ export class WalletConnectSolanaProvider {
|
|
|
106
112
|
}
|
|
107
113
|
}
|
|
108
114
|
|
|
109
|
-
public async getUri(
|
|
115
|
+
public async getUri(): Promise<string> {
|
|
116
|
+
// Return existing URI if available to avoid creating duplicate proposals
|
|
117
|
+
if (this.uri) {
|
|
118
|
+
this.emit("uri_changed", { uri: this.uri });
|
|
119
|
+
return this.uri;
|
|
120
|
+
}
|
|
121
|
+
|
|
110
122
|
// Create connection and get URI
|
|
111
123
|
const uri = await this.client.connect({
|
|
112
|
-
requiredNamespaces:
|
|
124
|
+
requiredNamespaces: this.namespaces,
|
|
113
125
|
});
|
|
114
126
|
|
|
115
127
|
this.uri = uri;
|
|
128
|
+
this.emit("uri_changed", { uri: this.uri });
|
|
116
129
|
return uri;
|
|
117
130
|
}
|
|
118
131
|
|
|
@@ -126,16 +139,17 @@ export class WalletConnectSolanaProvider {
|
|
|
126
139
|
sessions.forEach((session: SessionInfo) => {
|
|
127
140
|
this.sessionMap.set(session.topic, session);
|
|
128
141
|
});
|
|
129
|
-
this.uri = await this.getUri({
|
|
130
|
-
requiredNamespaces: defaultSolanaNamespaces,
|
|
131
|
-
});
|
|
132
142
|
}
|
|
133
143
|
|
|
134
144
|
/**
|
|
135
145
|
* Connect to wallet via WalletConnect
|
|
136
146
|
* Returns accounts when connected
|
|
137
147
|
*/
|
|
138
|
-
async connect(): Promise<string
|
|
148
|
+
async connect(): Promise<{ publicKey: string } | undefined> {
|
|
149
|
+
if (this.accounts.length > 0) {
|
|
150
|
+
return this.accounts[0];
|
|
151
|
+
}
|
|
152
|
+
this.uri = await this.getUri();
|
|
139
153
|
// Start waiting for connection in background (don't await)
|
|
140
154
|
// Connection will emit 'connect' event when established
|
|
141
155
|
const connected = await this.waitForConnection().catch((error) => {
|
|
@@ -145,7 +159,7 @@ export class WalletConnectSolanaProvider {
|
|
|
145
159
|
});
|
|
146
160
|
|
|
147
161
|
if (connected) {
|
|
148
|
-
return this.accounts;
|
|
162
|
+
return this.accounts?.length > 0 ? this.accounts[0] : undefined;
|
|
149
163
|
}
|
|
150
164
|
}
|
|
151
165
|
|
|
@@ -195,20 +209,15 @@ export class WalletConnectSolanaProvider {
|
|
|
195
209
|
// solana_requestAccounts
|
|
196
210
|
if (args.method === "solana_requestAccounts") {
|
|
197
211
|
if (this.accounts.length > 0) {
|
|
198
|
-
return
|
|
199
|
-
}
|
|
200
|
-
if (this.uri === null) {
|
|
201
|
-
this.uri = await this.getUri({
|
|
202
|
-
requiredNamespaces: defaultSolanaNamespaces,
|
|
203
|
-
});
|
|
212
|
+
return { publicKey: this.accounts[0] };
|
|
204
213
|
}
|
|
205
214
|
const accounts = await this.connect();
|
|
206
|
-
return accounts
|
|
215
|
+
return accounts;
|
|
207
216
|
}
|
|
208
217
|
|
|
209
218
|
// solana_getAccounts
|
|
210
219
|
if (args.method === "solana_getAccounts") {
|
|
211
|
-
return this.accounts
|
|
220
|
+
return this.accounts || [];
|
|
212
221
|
}
|
|
213
222
|
|
|
214
223
|
if (!this.session) {
|
|
@@ -234,9 +243,9 @@ export class WalletConnectSolanaProvider {
|
|
|
234
243
|
/**
|
|
235
244
|
* Get current accounts
|
|
236
245
|
*/
|
|
237
|
-
async getAccounts(): Promise<string[]> {
|
|
246
|
+
async getAccounts(): Promise<{ publicKey: string }[]> {
|
|
238
247
|
if (!this.session) {
|
|
239
|
-
return [];
|
|
248
|
+
return [{ publicKey: "" }];
|
|
240
249
|
}
|
|
241
250
|
return this.accounts;
|
|
242
251
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { NamespaceConfig } from "@tomo-inc/wallet-connect-protocol";
|
|
1
2
|
import { Connector, ProviderProtocol } from "../type";
|
|
2
3
|
import { walletConnectWallet } from "./default/walletConnectWallet/walletConnectWallet";
|
|
3
4
|
import { WalletConnectProvider } from "./providers/WalletConnectProvider";
|
|
@@ -15,6 +16,8 @@ let walletConnectConfig: {
|
|
|
15
16
|
url: string;
|
|
16
17
|
icons: string[];
|
|
17
18
|
};
|
|
19
|
+
solanaNamespaces?: Record<string, NamespaceConfig>;
|
|
20
|
+
evmNamespaces?: Record<string, NamespaceConfig>;
|
|
18
21
|
} | null = null;
|
|
19
22
|
|
|
20
23
|
let providerInstance: WalletConnectProvider | null = null;
|
|
@@ -66,13 +69,13 @@ function convertWalletToConnector(wallet: ReturnType<typeof walletConnectWallet>
|
|
|
66
69
|
isInstalled: true, // Always available
|
|
67
70
|
providers: {
|
|
68
71
|
evm: {
|
|
69
|
-
provider: createWalletConnectEVMProvider(),
|
|
72
|
+
provider: createWalletConnectEVMProvider(walletConnectConfig?.evmNamespaces),
|
|
73
|
+
protocol: ProviderProtocol.WALLET_CONNECT,
|
|
74
|
+
},
|
|
75
|
+
solana: {
|
|
76
|
+
provider: createWalletConnectSolanaProvider(walletConnectConfig?.solanaNamespaces),
|
|
70
77
|
protocol: ProviderProtocol.WALLET_CONNECT,
|
|
71
78
|
},
|
|
72
|
-
// solana: {
|
|
73
|
-
// provider: createWalletConnectSolanaProvider(),
|
|
74
|
-
// protocol: ProviderProtocol.WALLET_CONNECT,
|
|
75
|
-
// },
|
|
76
79
|
},
|
|
77
80
|
};
|
|
78
81
|
}
|
|
@@ -81,14 +84,17 @@ function convertWalletToConnector(wallet: ReturnType<typeof walletConnectWallet>
|
|
|
81
84
|
* Create a WalletConnect provider instance
|
|
82
85
|
* Provider will be initialized when connect() is called
|
|
83
86
|
*/
|
|
84
|
-
export function createWalletConnectEVMProvider(): any {
|
|
87
|
+
export function createWalletConnectEVMProvider(namespaces?: Record<string, NamespaceConfig>): any {
|
|
85
88
|
const getProvider = async () => {
|
|
86
89
|
if (!providerInstance) {
|
|
87
90
|
if (!walletConnectConfig) {
|
|
88
91
|
throw new Error("WalletConnect not configured. Please call setWalletConnectConfig() first.");
|
|
89
92
|
}
|
|
90
93
|
|
|
91
|
-
providerInstance = new WalletConnectProvider(
|
|
94
|
+
providerInstance = new WalletConnectProvider({
|
|
95
|
+
...walletConnectConfig,
|
|
96
|
+
namespaces: namespaces || walletConnectConfig?.evmNamespaces,
|
|
97
|
+
});
|
|
92
98
|
await providerInstance.initialize();
|
|
93
99
|
}
|
|
94
100
|
return providerInstance;
|
|
@@ -143,14 +149,17 @@ export function createWalletConnectEVMProvider(): any {
|
|
|
143
149
|
* Create a WalletConnect Solana provider instance
|
|
144
150
|
* Provider will be initialized when connect() is called
|
|
145
151
|
*/
|
|
146
|
-
function createWalletConnectSolanaProvider(): any {
|
|
152
|
+
function createWalletConnectSolanaProvider(namespaces?: Record<string, NamespaceConfig>): any {
|
|
147
153
|
const getProvider = async () => {
|
|
148
154
|
if (!solanaProviderInstance) {
|
|
149
155
|
if (!walletConnectConfig) {
|
|
150
156
|
throw new Error("WalletConnect not configured. Please call setWalletConnectConfig() first.");
|
|
151
157
|
}
|
|
152
158
|
|
|
153
|
-
solanaProviderInstance = new WalletConnectSolanaProvider(
|
|
159
|
+
solanaProviderInstance = new WalletConnectSolanaProvider({
|
|
160
|
+
...walletConnectConfig,
|
|
161
|
+
namespaces: namespaces || walletConnectConfig?.solanaNamespaces || {},
|
|
162
|
+
});
|
|
154
163
|
await solanaProviderInstance.initialize();
|
|
155
164
|
}
|
|
156
165
|
return solanaProviderInstance;
|
package/tsup.config.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { readFileSync, writeFileSync, readdirSync, statSync } from "fs";
|
|
1
|
+
import { readdirSync, readFileSync, statSync, writeFileSync } from "fs";
|
|
3
2
|
import { join } from "path";
|
|
3
|
+
import { defineConfig } from "tsup";
|
|
4
4
|
|
|
5
5
|
// Recursively find all JS files in dist directory
|
|
6
6
|
function findJsFiles(dir: string, fileList: string[] = []): string[] {
|
|
@@ -24,6 +24,7 @@ export default defineConfig({
|
|
|
24
24
|
treeshake: true,
|
|
25
25
|
clean: true,
|
|
26
26
|
platform: "browser",
|
|
27
|
+
tsconfig: "tsconfig.json",
|
|
27
28
|
loader: {
|
|
28
29
|
".svg": "base64",
|
|
29
30
|
},
|