@etherplay/connect 0.0.1
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/README.md +58 -0
- package/dist/index.d.ts +83 -0
- package/dist/index.js +471 -0
- package/dist/popup.d.ts +23 -0
- package/dist/popup.js +165 -0
- package/dist/utils.d.ts +2 -0
- package/dist/utils.js +13 -0
- package/package.json +69 -0
package/README.md
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# create-svelte
|
|
2
|
+
|
|
3
|
+
Everything you need to build a Svelte library, powered by [`create-svelte`](https://github.com/sveltejs/kit/tree/main/packages/create-svelte).
|
|
4
|
+
|
|
5
|
+
Read more about creating a library [in the docs](https://svelte.dev/docs/kit/packaging).
|
|
6
|
+
|
|
7
|
+
## Creating a project
|
|
8
|
+
|
|
9
|
+
If you're seeing this, you've probably already done this step. Congrats!
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
# create a new project in the current directory
|
|
13
|
+
npx sv create
|
|
14
|
+
|
|
15
|
+
# create a new project in my-app
|
|
16
|
+
npx sv create my-app
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Developing
|
|
20
|
+
|
|
21
|
+
Once you've created a project and installed dependencies with `npm install` (or `pnpm install` or `yarn`), start a development server:
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
npm run dev
|
|
25
|
+
|
|
26
|
+
# or start the server and open the app in a new browser tab
|
|
27
|
+
npm run dev -- --open
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Everything inside `src/lib` is part of your library, everything inside `src/routes` can be used as a showcase or preview app.
|
|
31
|
+
|
|
32
|
+
## Building
|
|
33
|
+
|
|
34
|
+
To build your library:
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
npm run package
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
To create a production version of your showcase app:
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
npm run build
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
You can preview the production build with `npm run preview`.
|
|
47
|
+
|
|
48
|
+
> To deploy your app, you may need to install an [adapter](https://svelte.dev/docs/kit/adapters) for your target environment.
|
|
49
|
+
|
|
50
|
+
## Publishing
|
|
51
|
+
|
|
52
|
+
Go into the `package.json` and give your package the desired name through the `"name"` option. Also consider adding a `"license"` field and point it to a `LICENSE` file which you can create from a template (one popular option is the [MIT license](https://opensource.org/license/mit/)).
|
|
53
|
+
|
|
54
|
+
To publish your library to [npm](https://www.npmjs.com):
|
|
55
|
+
|
|
56
|
+
```bash
|
|
57
|
+
npm publish
|
|
58
|
+
```
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import type { AlchemyMechanism, OriginAccount } from '@etherplay/alchemy';
|
|
2
|
+
import type { EIP1193WindowWalletProvider } from 'eip-1193';
|
|
3
|
+
import { fromEntropyKeyToMnemonic } from '@etherplay/alchemy';
|
|
4
|
+
export { fromEntropyKeyToMnemonic };
|
|
5
|
+
export type PopupSettings = {
|
|
6
|
+
walletHost: string;
|
|
7
|
+
mechanism: AlchemyMechanism;
|
|
8
|
+
};
|
|
9
|
+
export type WalletMechanism<WalletName extends string | undefined, Address extends `0x${string}` | undefined> = {
|
|
10
|
+
type: 'wallet';
|
|
11
|
+
} & (WalletName extends undefined ? {
|
|
12
|
+
name?: undefined;
|
|
13
|
+
} : {
|
|
14
|
+
name: WalletName;
|
|
15
|
+
}) & (Address extends undefined ? {
|
|
16
|
+
address?: undefined;
|
|
17
|
+
} : {
|
|
18
|
+
address: Address;
|
|
19
|
+
});
|
|
20
|
+
export type Mechanism = AlchemyMechanism | WalletMechanism<string | undefined, `0x${string}` | undefined>;
|
|
21
|
+
export type FullfilledMechanism = AlchemyMechanism | WalletMechanism<string, `0x${string}`>;
|
|
22
|
+
export type Connection = {
|
|
23
|
+
error?: {
|
|
24
|
+
message: string;
|
|
25
|
+
cause?: any;
|
|
26
|
+
};
|
|
27
|
+
wallets: EIP6963ProviderDetail[];
|
|
28
|
+
} & ({
|
|
29
|
+
step: 'Idle';
|
|
30
|
+
loading: boolean;
|
|
31
|
+
} | {
|
|
32
|
+
step: 'MechanismToChoose';
|
|
33
|
+
} | {
|
|
34
|
+
step: 'PopupLaunched';
|
|
35
|
+
popupClosed: boolean;
|
|
36
|
+
mechanism: AlchemyMechanism;
|
|
37
|
+
} | {
|
|
38
|
+
step: 'WalletToChoose';
|
|
39
|
+
mechanism: WalletMechanism<undefined, undefined>;
|
|
40
|
+
} | {
|
|
41
|
+
step: 'WaitingForWalletConnection';
|
|
42
|
+
mechanism: WalletMechanism<string, undefined>;
|
|
43
|
+
} | {
|
|
44
|
+
step: 'NeedWalletSignature';
|
|
45
|
+
mechanism: WalletMechanism<string, `0x${string}`>;
|
|
46
|
+
} | {
|
|
47
|
+
step: 'WaitingForSignature';
|
|
48
|
+
mechanism: WalletMechanism<string, `0x${string}`>;
|
|
49
|
+
} | {
|
|
50
|
+
step: 'SignedIn';
|
|
51
|
+
mechanism: FullfilledMechanism;
|
|
52
|
+
account: OriginAccount;
|
|
53
|
+
walletAccountChanged: `0x${string}` | undefined;
|
|
54
|
+
});
|
|
55
|
+
interface EIP6963ProviderInfo {
|
|
56
|
+
uuid: string;
|
|
57
|
+
name: string;
|
|
58
|
+
icon: string;
|
|
59
|
+
rdns: string;
|
|
60
|
+
}
|
|
61
|
+
interface EIP6963ProviderDetail {
|
|
62
|
+
info: EIP6963ProviderInfo;
|
|
63
|
+
provider: EIP1193WindowWalletProvider;
|
|
64
|
+
}
|
|
65
|
+
export interface EIP6963AnnounceProviderEvent extends CustomEvent {
|
|
66
|
+
type: 'eip6963:announceProvider';
|
|
67
|
+
detail: EIP6963ProviderDetail;
|
|
68
|
+
}
|
|
69
|
+
export declare function createConnection(settings: {
|
|
70
|
+
walletHost: string;
|
|
71
|
+
autoConnect?: boolean;
|
|
72
|
+
}): {
|
|
73
|
+
subscribe: (this: void, run: import("svelte/store").Subscriber<Connection>, invalidate?: () => void) => import("svelte/store").Unsubscriber;
|
|
74
|
+
connect: (mechanism?: Mechanism, options?: {
|
|
75
|
+
alwaysRequestSignatureOnlyAfterUserConfirmation?: boolean;
|
|
76
|
+
doNotStoreLocally?: boolean;
|
|
77
|
+
}) => Promise<void>;
|
|
78
|
+
cancel: () => void;
|
|
79
|
+
back: (step: "MechanismToChoose" | "Idle" | "WalletToChoose") => void;
|
|
80
|
+
requestSignature: () => Promise<void>;
|
|
81
|
+
connectOnCurrentWalletAccount: (address: `0x${string}`) => void;
|
|
82
|
+
disconnect: () => void;
|
|
83
|
+
};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,471 @@
|
|
|
1
|
+
import { writable } from 'svelte/store';
|
|
2
|
+
import { createPopupLauncher } from './popup.js';
|
|
3
|
+
import { fromEntropyKeyToMnemonic, fromMnemonicToFirstAccount, fromSignatureToKey, originKeyMessage } from '@etherplay/alchemy';
|
|
4
|
+
import { bytesToHex } from '@noble/hashes/utils';
|
|
5
|
+
export { fromEntropyKeyToMnemonic };
|
|
6
|
+
const encoder = new TextEncoder();
|
|
7
|
+
const storageAccountKey = '__origin_account';
|
|
8
|
+
export function createConnection(settings) {
|
|
9
|
+
let autoConnect = true;
|
|
10
|
+
if (typeof settings.autoConnect !== 'undefined') {
|
|
11
|
+
autoConnect = settings.autoConnect;
|
|
12
|
+
}
|
|
13
|
+
let $connection = { step: 'Idle', loading: true, wallets: [] };
|
|
14
|
+
const _store = writable($connection);
|
|
15
|
+
function set(connection) {
|
|
16
|
+
$connection = connection;
|
|
17
|
+
_store.set($connection);
|
|
18
|
+
return $connection;
|
|
19
|
+
}
|
|
20
|
+
function setError(error) {
|
|
21
|
+
if ($connection) {
|
|
22
|
+
set({
|
|
23
|
+
...$connection,
|
|
24
|
+
error
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
throw new Error(`no connection`);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
let walletProvider;
|
|
32
|
+
let popup;
|
|
33
|
+
function fetchWallets() {
|
|
34
|
+
if (typeof window !== 'undefined') {
|
|
35
|
+
// const defaultProvider = (window as any).ethereum;
|
|
36
|
+
// console.log(defaultProvider);
|
|
37
|
+
// TODO ?
|
|
38
|
+
window.addEventListener('eip6963:announceProvider', (event) => {
|
|
39
|
+
const { detail } = event;
|
|
40
|
+
// const { info, provider } = detail;
|
|
41
|
+
// const { uuid, name, icon, rdns } = info;
|
|
42
|
+
// console.log('provider', provider);
|
|
43
|
+
// console.log(`isDefault: ${provider === defaultProvider}`);
|
|
44
|
+
// console.log('info', info);
|
|
45
|
+
const existingWallets = $connection.wallets;
|
|
46
|
+
existingWallets.push(detail);
|
|
47
|
+
set({
|
|
48
|
+
...$connection,
|
|
49
|
+
wallets: existingWallets
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
window.dispatchEvent(new Event('eip6963:requestProvider'));
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
function waitForWallet(name) {
|
|
56
|
+
return new Promise((resolve, reject) => {
|
|
57
|
+
const timeout = setTimeout(() => {
|
|
58
|
+
clearInterval(interval);
|
|
59
|
+
reject('timeout');
|
|
60
|
+
}, 1000);
|
|
61
|
+
const interval = setInterval(() => {
|
|
62
|
+
const wallet = $connection.wallets.find((v) => v.info.name == name);
|
|
63
|
+
if (wallet) {
|
|
64
|
+
clearTimeout(timeout);
|
|
65
|
+
clearInterval(interval);
|
|
66
|
+
resolve(wallet);
|
|
67
|
+
}
|
|
68
|
+
}, 100);
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
if (autoConnect) {
|
|
72
|
+
if (typeof window !== 'undefined') {
|
|
73
|
+
// set({step: 'Idle', loading: true, wallets: $connection.wallets});
|
|
74
|
+
try {
|
|
75
|
+
const existingAccount = getOriginAccount();
|
|
76
|
+
if (existingAccount) {
|
|
77
|
+
if (existingAccount.signer) {
|
|
78
|
+
if (existingAccount.mechanismUsed.type == 'wallet') {
|
|
79
|
+
const walletMechanism = existingAccount.mechanismUsed;
|
|
80
|
+
waitForWallet(walletMechanism.name)
|
|
81
|
+
.then((walletDetails) => {
|
|
82
|
+
walletProvider = walletDetails.provider;
|
|
83
|
+
set({
|
|
84
|
+
step: 'SignedIn',
|
|
85
|
+
account: existingAccount,
|
|
86
|
+
mechanism: existingAccount.mechanismUsed,
|
|
87
|
+
wallets: $connection.wallets,
|
|
88
|
+
walletAccountChanged: undefined
|
|
89
|
+
});
|
|
90
|
+
walletProvider.request({ method: 'eth_accounts' }).then(onAccountChanged);
|
|
91
|
+
watchForAccountChange(walletProvider);
|
|
92
|
+
})
|
|
93
|
+
.catch((err) => {
|
|
94
|
+
set({ step: 'Idle', loading: false, wallets: $connection.wallets });
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
set({
|
|
99
|
+
step: 'SignedIn',
|
|
100
|
+
account: existingAccount,
|
|
101
|
+
mechanism: existingAccount.mechanismUsed,
|
|
102
|
+
wallets: $connection.wallets,
|
|
103
|
+
walletAccountChanged: undefined
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
else {
|
|
108
|
+
set({ step: 'Idle', loading: false, wallets: $connection.wallets });
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
set({ step: 'Idle', loading: false, wallets: $connection.wallets });
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
catch {
|
|
116
|
+
set({ step: 'Idle', loading: false, wallets: $connection.wallets });
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
else {
|
|
121
|
+
set({ step: 'Idle', loading: false, wallets: $connection.wallets });
|
|
122
|
+
}
|
|
123
|
+
fetchWallets();
|
|
124
|
+
function getOriginAccount() {
|
|
125
|
+
const fromStorage = localStorage.getItem(storageAccountKey);
|
|
126
|
+
if (fromStorage) {
|
|
127
|
+
return JSON.parse(fromStorage);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
function saveOriginAccount(account) {
|
|
131
|
+
const accountSTR = JSON.stringify(account);
|
|
132
|
+
sessionStorage.setItem(storageAccountKey, accountSTR);
|
|
133
|
+
localStorage.setItem(storageAccountKey, accountSTR);
|
|
134
|
+
}
|
|
135
|
+
function deleteOriginAccount() {
|
|
136
|
+
sessionStorage.removeItem(storageAccountKey);
|
|
137
|
+
localStorage.removeItem(storageAccountKey);
|
|
138
|
+
}
|
|
139
|
+
async function requestSignature() {
|
|
140
|
+
if ($connection.step !== 'NeedWalletSignature') {
|
|
141
|
+
throw new Error(`invalid step: ${$connection.step}, needs to be NeedWalletSignature`);
|
|
142
|
+
}
|
|
143
|
+
const provider = walletProvider;
|
|
144
|
+
if (!provider) {
|
|
145
|
+
// TODO error ?
|
|
146
|
+
throw new Error(`no wallet provided initialised`);
|
|
147
|
+
}
|
|
148
|
+
const message = originKeyMessage(origin);
|
|
149
|
+
const messageAsBytes = encoder.encode(message);
|
|
150
|
+
const msg = `0x${bytesToHex(messageAsBytes)}`;
|
|
151
|
+
set({
|
|
152
|
+
...$connection,
|
|
153
|
+
step: 'WaitingForSignature'
|
|
154
|
+
});
|
|
155
|
+
const signature = await provider.request({
|
|
156
|
+
method: 'personal_sign',
|
|
157
|
+
params: [msg, $connection.mechanism.address]
|
|
158
|
+
});
|
|
159
|
+
const originKey = fromSignatureToKey(signature);
|
|
160
|
+
const originMnemonic = fromEntropyKeyToMnemonic(originKey);
|
|
161
|
+
const originAccount = fromMnemonicToFirstAccount(originMnemonic);
|
|
162
|
+
const account = {
|
|
163
|
+
address: $connection.mechanism.address,
|
|
164
|
+
signer: {
|
|
165
|
+
origin,
|
|
166
|
+
address: originAccount.address,
|
|
167
|
+
privateKey: originAccount.privateKey,
|
|
168
|
+
mnemomicKey: originKey
|
|
169
|
+
},
|
|
170
|
+
metadata: {},
|
|
171
|
+
mechanismUsed: $connection.mechanism
|
|
172
|
+
};
|
|
173
|
+
set({
|
|
174
|
+
...$connection,
|
|
175
|
+
step: 'SignedIn',
|
|
176
|
+
mechanism: {
|
|
177
|
+
type: 'wallet',
|
|
178
|
+
name: $connection.mechanism.name,
|
|
179
|
+
address: $connection.mechanism.address
|
|
180
|
+
},
|
|
181
|
+
account,
|
|
182
|
+
walletAccountChanged: undefined // TODO check account list
|
|
183
|
+
});
|
|
184
|
+
if (remember) {
|
|
185
|
+
saveOriginAccount(account);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
function connectOnCurrentWalletAccount(address) {
|
|
189
|
+
if ($connection.step === 'SignedIn' && $connection.mechanism.type === 'wallet') {
|
|
190
|
+
connect({
|
|
191
|
+
type: 'wallet',
|
|
192
|
+
address,
|
|
193
|
+
name: $connection.mechanism.name
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
else {
|
|
197
|
+
throw new Error(`need to be using a mechanism of type wallet and be SignedIN`);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
function onAccountChanged(accounts) {
|
|
201
|
+
if ($connection.step === 'SignedIn' && $connection.mechanism.type === 'wallet') {
|
|
202
|
+
// TODO if auto-connect and saved-signature ?
|
|
203
|
+
// connect(
|
|
204
|
+
// {
|
|
205
|
+
// type: 'wallet',
|
|
206
|
+
// address: accounts[0],
|
|
207
|
+
// name: $connection.mechanism.name
|
|
208
|
+
// },
|
|
209
|
+
// { alwaysRequestSignatureOnlyAfterUserConfirmation: true }
|
|
210
|
+
// );
|
|
211
|
+
if (accounts.length > 0 &&
|
|
212
|
+
accounts[0].toLowerCase() != $connection.account.address.toLowerCase()) {
|
|
213
|
+
set({
|
|
214
|
+
...$connection,
|
|
215
|
+
walletAccountChanged: accounts[0]
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
else if ($connection.walletAccountChanged) {
|
|
219
|
+
set({
|
|
220
|
+
...$connection,
|
|
221
|
+
walletAccountChanged: undefined
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
// if (accounts[0] !== $connection)
|
|
226
|
+
}
|
|
227
|
+
function watchForAccountChange(walletProvider) {
|
|
228
|
+
walletProvider.on('accountsChanged', onAccountChanged);
|
|
229
|
+
}
|
|
230
|
+
function stopatchingForAccountChange(walletProvider) {
|
|
231
|
+
walletProvider.removeListener('accountsChanged', onAccountChanged);
|
|
232
|
+
}
|
|
233
|
+
let remember = false;
|
|
234
|
+
async function connect(mechanism, options) {
|
|
235
|
+
remember = !(options?.doNotStoreLocally || false);
|
|
236
|
+
if (mechanism) {
|
|
237
|
+
if (mechanism.type === 'wallet') {
|
|
238
|
+
const walletName = mechanism.name;
|
|
239
|
+
if (walletName) {
|
|
240
|
+
const wallet = $connection.wallets.find((v) => v.info.name == walletName || v.info.uuid == walletName);
|
|
241
|
+
if (wallet) {
|
|
242
|
+
if (walletProvider) {
|
|
243
|
+
stopatchingForAccountChange(walletProvider);
|
|
244
|
+
}
|
|
245
|
+
walletProvider = wallet.provider;
|
|
246
|
+
const mechanism = {
|
|
247
|
+
type: 'wallet',
|
|
248
|
+
name: walletName
|
|
249
|
+
};
|
|
250
|
+
set({
|
|
251
|
+
step: 'WaitingForWalletConnection', // TODO FetchingAccounts
|
|
252
|
+
mechanism,
|
|
253
|
+
wallets: $connection.wallets
|
|
254
|
+
});
|
|
255
|
+
const provider = wallet.provider;
|
|
256
|
+
let accounts = await provider.request({ method: 'eth_accounts' });
|
|
257
|
+
if (accounts.length === 0) {
|
|
258
|
+
set({
|
|
259
|
+
step: 'WaitingForWalletConnection',
|
|
260
|
+
mechanism,
|
|
261
|
+
wallets: $connection.wallets
|
|
262
|
+
});
|
|
263
|
+
accounts = await provider.request({ method: 'eth_requestAccounts' });
|
|
264
|
+
if (accounts.length > 0) {
|
|
265
|
+
set({
|
|
266
|
+
step: 'NeedWalletSignature',
|
|
267
|
+
mechanism: {
|
|
268
|
+
...mechanism,
|
|
269
|
+
address: accounts[0]
|
|
270
|
+
},
|
|
271
|
+
wallets: $connection.wallets
|
|
272
|
+
});
|
|
273
|
+
watchForAccountChange(walletProvider);
|
|
274
|
+
}
|
|
275
|
+
else {
|
|
276
|
+
set({
|
|
277
|
+
step: 'MechanismToChoose',
|
|
278
|
+
wallets: $connection.wallets,
|
|
279
|
+
error: { message: 'could not get any accounts' }
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
else {
|
|
284
|
+
if (options?.alwaysRequestSignatureOnlyAfterUserConfirmation) {
|
|
285
|
+
set({
|
|
286
|
+
step: 'NeedWalletSignature',
|
|
287
|
+
mechanism: {
|
|
288
|
+
...mechanism,
|
|
289
|
+
address: accounts[0]
|
|
290
|
+
},
|
|
291
|
+
wallets: $connection.wallets
|
|
292
|
+
});
|
|
293
|
+
watchForAccountChange(walletProvider);
|
|
294
|
+
}
|
|
295
|
+
else {
|
|
296
|
+
watchForAccountChange(walletProvider);
|
|
297
|
+
set({
|
|
298
|
+
step: 'NeedWalletSignature',
|
|
299
|
+
mechanism: {
|
|
300
|
+
...mechanism,
|
|
301
|
+
address: accounts[0]
|
|
302
|
+
},
|
|
303
|
+
wallets: $connection.wallets
|
|
304
|
+
});
|
|
305
|
+
await requestSignature();
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
else {
|
|
310
|
+
console.error(`failed to get wallet ${walletName}`, $connection.wallets);
|
|
311
|
+
set({
|
|
312
|
+
step: 'MechanismToChoose',
|
|
313
|
+
wallets: $connection.wallets,
|
|
314
|
+
error: { message: `failed to get wallet ${walletName}` }
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
else {
|
|
319
|
+
// TODO can also be done automatically before hand
|
|
320
|
+
// set({
|
|
321
|
+
// step: 'FetchingWallets',
|
|
322
|
+
// mechanism: { type: 'wallet', wallet: undefined }
|
|
323
|
+
// });
|
|
324
|
+
set({
|
|
325
|
+
step: 'WalletToChoose',
|
|
326
|
+
mechanism: { type: 'wallet' },
|
|
327
|
+
wallets: $connection.wallets
|
|
328
|
+
});
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
else {
|
|
332
|
+
popup = connectViaPopup({
|
|
333
|
+
mechanism,
|
|
334
|
+
walletHost: settings.walletHost
|
|
335
|
+
});
|
|
336
|
+
set({
|
|
337
|
+
step: 'PopupLaunched',
|
|
338
|
+
popupClosed: false,
|
|
339
|
+
mechanism,
|
|
340
|
+
wallets: $connection.wallets
|
|
341
|
+
});
|
|
342
|
+
const unsubscribe = popup.subscribe(($popup) => {
|
|
343
|
+
if ($connection?.step === 'PopupLaunched') {
|
|
344
|
+
if ($popup.closed) {
|
|
345
|
+
set({
|
|
346
|
+
...$connection,
|
|
347
|
+
popupClosed: true
|
|
348
|
+
});
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
});
|
|
352
|
+
try {
|
|
353
|
+
const result = await popup;
|
|
354
|
+
console.log({ result });
|
|
355
|
+
set({
|
|
356
|
+
step: 'SignedIn',
|
|
357
|
+
account: result,
|
|
358
|
+
mechanism,
|
|
359
|
+
wallets: $connection.wallets,
|
|
360
|
+
walletAccountChanged: undefined
|
|
361
|
+
});
|
|
362
|
+
if (remember) {
|
|
363
|
+
saveOriginAccount(result);
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
catch (err) {
|
|
367
|
+
console.log({ error: err });
|
|
368
|
+
set({ step: 'Idle', loading: false, wallets: $connection.wallets });
|
|
369
|
+
}
|
|
370
|
+
finally {
|
|
371
|
+
unsubscribe();
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
else {
|
|
376
|
+
set({
|
|
377
|
+
step: 'MechanismToChoose',
|
|
378
|
+
wallets: $connection.wallets
|
|
379
|
+
});
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
function disconnect() {
|
|
383
|
+
deleteOriginAccount();
|
|
384
|
+
if (walletProvider) {
|
|
385
|
+
stopatchingForAccountChange(walletProvider);
|
|
386
|
+
}
|
|
387
|
+
walletProvider = undefined;
|
|
388
|
+
set({
|
|
389
|
+
step: 'Idle',
|
|
390
|
+
loading: false,
|
|
391
|
+
wallets: $connection.wallets
|
|
392
|
+
});
|
|
393
|
+
}
|
|
394
|
+
function back(step) {
|
|
395
|
+
popup?.cancel();
|
|
396
|
+
if (step === 'MechanismToChoose') {
|
|
397
|
+
set({ step, wallets: $connection.wallets });
|
|
398
|
+
}
|
|
399
|
+
else if (step === 'Idle') {
|
|
400
|
+
set({ step, loading: false, wallets: $connection.wallets });
|
|
401
|
+
}
|
|
402
|
+
else if (step === 'WalletToChoose') {
|
|
403
|
+
set({ step, wallets: $connection.wallets, mechanism: { type: 'wallet' } });
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
const popupLauncher = createPopupLauncher();
|
|
407
|
+
function connectViaPopup(settings) {
|
|
408
|
+
let popupURL = new URL(`${settings.walletHost}/login/`);
|
|
409
|
+
let fullWindow = false;
|
|
410
|
+
if (settings.mechanism.type === 'mnemonic') {
|
|
411
|
+
popupURL.searchParams.append('type', 'mnemonic');
|
|
412
|
+
}
|
|
413
|
+
else if (settings.mechanism.type === 'email') {
|
|
414
|
+
popupURL.searchParams.append('type', 'email');
|
|
415
|
+
if (settings.mechanism.email) {
|
|
416
|
+
popupURL.searchParams.append('email', encodeURIComponent(settings.mechanism.email));
|
|
417
|
+
}
|
|
418
|
+
if (settings.mechanism.mode) {
|
|
419
|
+
popupURL.searchParams.append('emailMode', settings.mechanism.mode);
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
else if (settings.mechanism.type === 'oauth') {
|
|
423
|
+
popupURL.searchParams.append('type', 'oauth');
|
|
424
|
+
if (settings.mechanism.provider.id === 'auth0') {
|
|
425
|
+
popupURL.searchParams.append('oauth-provider', settings.mechanism.provider.id);
|
|
426
|
+
popupURL.searchParams.append('oauth-connection', settings.mechanism.provider.connection);
|
|
427
|
+
}
|
|
428
|
+
else {
|
|
429
|
+
popupURL.searchParams.append('oauth-provider', settings.mechanism.provider.id);
|
|
430
|
+
}
|
|
431
|
+
if (!settings.mechanism.usePopup) {
|
|
432
|
+
popupURL.searchParams.append('oauth-redirection', 'true');
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
else {
|
|
436
|
+
throw new Error(`mechanism ${settings.mechanism.type} not supported`);
|
|
437
|
+
}
|
|
438
|
+
// if (settings.extraParams) {
|
|
439
|
+
// for (const [key, value] of Object.entries(settings.extraParams)) {
|
|
440
|
+
// popupURL.searchParams.append(`${key}`, value);
|
|
441
|
+
// }
|
|
442
|
+
// }
|
|
443
|
+
const currentURL = new URL(location.href);
|
|
444
|
+
const entriesToAdd = [];
|
|
445
|
+
for (const entry of currentURL.searchParams.entries()) {
|
|
446
|
+
if (entry[0].startsWith('renraku_')) {
|
|
447
|
+
entriesToAdd.push([entry[0].slice(`renraku_`.length), entry[1]]);
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
if (currentURL.searchParams.has('_d_eruda')) {
|
|
451
|
+
entriesToAdd.push(['_d_eruda', currentURL.searchParams.get('_d_eruda') || '']);
|
|
452
|
+
}
|
|
453
|
+
for (const entryToAdd of entriesToAdd) {
|
|
454
|
+
popupURL.searchParams.append(entryToAdd[0], entryToAdd[1]);
|
|
455
|
+
}
|
|
456
|
+
return popupLauncher.launchPopup(popupURL.toString(), { fullWindow });
|
|
457
|
+
}
|
|
458
|
+
function cancel() {
|
|
459
|
+
popup?.cancel();
|
|
460
|
+
set({ step: 'Idle', loading: false, wallets: $connection.wallets });
|
|
461
|
+
}
|
|
462
|
+
return {
|
|
463
|
+
subscribe: _store.subscribe,
|
|
464
|
+
connect,
|
|
465
|
+
cancel,
|
|
466
|
+
back,
|
|
467
|
+
requestSignature,
|
|
468
|
+
connectOnCurrentWalletAccount,
|
|
469
|
+
disconnect
|
|
470
|
+
};
|
|
471
|
+
}
|
package/dist/popup.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { type Readable } from 'svelte/store';
|
|
2
|
+
import { createStorePromise } from './utils.js';
|
|
3
|
+
export type Error = {
|
|
4
|
+
message: string;
|
|
5
|
+
type?: string;
|
|
6
|
+
cause?: any;
|
|
7
|
+
};
|
|
8
|
+
export type Popup = {
|
|
9
|
+
launched: boolean;
|
|
10
|
+
closed: boolean;
|
|
11
|
+
resolved: boolean;
|
|
12
|
+
error?: Error;
|
|
13
|
+
};
|
|
14
|
+
export type PopupPromise<T> = ReturnType<typeof createStorePromise<T, Popup, Readable<Popup> & {
|
|
15
|
+
cancel: () => void;
|
|
16
|
+
}>>;
|
|
17
|
+
export declare function createPopupLauncher<T>(): {
|
|
18
|
+
launchPopup: (url: string, options?: {
|
|
19
|
+
fullWindow?: boolean;
|
|
20
|
+
}) => Promise<T> & Readable<Popup> & {
|
|
21
|
+
cancel: () => void;
|
|
22
|
+
};
|
|
23
|
+
};
|
package/dist/popup.js
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import { writable } from 'svelte/store';
|
|
2
|
+
import { createStorePromise } from './utils.js';
|
|
3
|
+
export function createPopupLauncher() {
|
|
4
|
+
let id = 1;
|
|
5
|
+
let currentPopup = { popup: undefined };
|
|
6
|
+
function launchPopup(url, options) {
|
|
7
|
+
const urlObject = new URL(url);
|
|
8
|
+
const expectedOrigin = `${urlObject.protocol}//${urlObject.host}`;
|
|
9
|
+
const pathname = urlObject.pathname;
|
|
10
|
+
let $popup = {
|
|
11
|
+
closed: false,
|
|
12
|
+
launched: false,
|
|
13
|
+
resolved: false
|
|
14
|
+
};
|
|
15
|
+
const _store = writable($popup);
|
|
16
|
+
function set(state) {
|
|
17
|
+
$popup = state;
|
|
18
|
+
_store.set(state);
|
|
19
|
+
}
|
|
20
|
+
if (currentPopup.popup) {
|
|
21
|
+
console.log(`stop listening to message from old popup`);
|
|
22
|
+
window.removeEventListener('message', currentPopup.onMessage);
|
|
23
|
+
const tmpRejectRecovery = currentPopup.rejectRecovery;
|
|
24
|
+
let couldCloseExistingPopup = false;
|
|
25
|
+
try {
|
|
26
|
+
currentPopup.popup.close();
|
|
27
|
+
couldCloseExistingPopup = true;
|
|
28
|
+
}
|
|
29
|
+
catch (err) {
|
|
30
|
+
console.error(err);
|
|
31
|
+
}
|
|
32
|
+
currentPopup = { popup: undefined };
|
|
33
|
+
if (couldCloseExistingPopup) {
|
|
34
|
+
tmpRejectRecovery({ message: 'popup closed so new one can take over' });
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
tmpRejectRecovery({ message: 'popup replaced' });
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
let _resolveRecovery;
|
|
41
|
+
let _rejectRecovery;
|
|
42
|
+
function resolveRecovery(state) {
|
|
43
|
+
currentPopup = { popup: undefined };
|
|
44
|
+
console.log(`stop listening to message as we resolved it`);
|
|
45
|
+
window.removeEventListener('message', onMessage);
|
|
46
|
+
if (_resolveRecovery) {
|
|
47
|
+
set({
|
|
48
|
+
closed: true,
|
|
49
|
+
launched: true,
|
|
50
|
+
resolved: true
|
|
51
|
+
});
|
|
52
|
+
_resolveRecovery(state);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
function rejectRecovery(error) {
|
|
56
|
+
currentPopup = { popup: undefined };
|
|
57
|
+
console.log(`stop listening to message as we rejected it`, error);
|
|
58
|
+
window.removeEventListener('message', onMessage);
|
|
59
|
+
if (_rejectRecovery) {
|
|
60
|
+
set({
|
|
61
|
+
closed: true,
|
|
62
|
+
launched: true,
|
|
63
|
+
resolved: true,
|
|
64
|
+
error: {
|
|
65
|
+
message: 'errored',
|
|
66
|
+
cause: error
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
_rejectRecovery(error);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
const onMessage = (messageEvent) => {
|
|
73
|
+
if (messageEvent.origin === expectedOrigin) {
|
|
74
|
+
console.log(messageEvent);
|
|
75
|
+
const data = messageEvent.data;
|
|
76
|
+
if (id == data.id) {
|
|
77
|
+
if (data.error) {
|
|
78
|
+
console.error(`ERROR`, data.error);
|
|
79
|
+
rejectRecovery(data.error);
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
resolveRecovery(data.result);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
console.log(`different id : eventId = ${data.id}, expected id = ${id}`);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
// function continuouslyPingPopup(popup: Window) {
|
|
91
|
+
// const intervalId = setInterval(() => {
|
|
92
|
+
// // // console.log(`checking if popup is closed...`);
|
|
93
|
+
// if (currentPopup.popup !== popup) {
|
|
94
|
+
// console.log(`ping: new popup, we ignore closing state`);
|
|
95
|
+
// clearInterval(intervalId);
|
|
96
|
+
// return;
|
|
97
|
+
// }
|
|
98
|
+
// try {
|
|
99
|
+
// if ('closed' in popup && popup.closed) {
|
|
100
|
+
// console.log(`ping: popup is closed`);
|
|
101
|
+
// clearInterval(intervalId);
|
|
102
|
+
// }
|
|
103
|
+
// console.log(`ping: ${expectedOrigin}`);
|
|
104
|
+
// popup.postMessage({ id, type: 'ping' }, expectedOrigin);
|
|
105
|
+
// } catch (err) {}
|
|
106
|
+
// }, 300);
|
|
107
|
+
// }
|
|
108
|
+
function watchForPopupClosed(popup) {
|
|
109
|
+
const intervalId = setInterval(() => {
|
|
110
|
+
// console.log(`checking if popup is closed...`);
|
|
111
|
+
if (currentPopup.popup !== popup) {
|
|
112
|
+
console.log(`new popup, we ignore closing state`);
|
|
113
|
+
clearInterval(intervalId);
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
try {
|
|
117
|
+
if ('closed' in popup && popup.closed) {
|
|
118
|
+
console.log(`popup is closed`);
|
|
119
|
+
clearInterval(intervalId);
|
|
120
|
+
setTimeout(() => {
|
|
121
|
+
// we delay the rejection in case it conflict with an onMessage event
|
|
122
|
+
if (currentPopup.popup === popup) {
|
|
123
|
+
set({
|
|
124
|
+
closed: true,
|
|
125
|
+
launched: $popup.launched,
|
|
126
|
+
resolved: $popup.resolved,
|
|
127
|
+
error: $popup.error
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
}, 100);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
catch (err) { }
|
|
134
|
+
}, 200);
|
|
135
|
+
}
|
|
136
|
+
const store = {
|
|
137
|
+
subscribe: _store.subscribe,
|
|
138
|
+
cancel() {
|
|
139
|
+
// TODO
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
const storePromise = createStorePromise(store, (resolve, reject) => {
|
|
143
|
+
_resolveRecovery = resolve;
|
|
144
|
+
_rejectRecovery = reject;
|
|
145
|
+
id++;
|
|
146
|
+
urlObject.searchParams.append('origin', window.origin);
|
|
147
|
+
urlObject.searchParams.append('id', id.toString());
|
|
148
|
+
const popupParameters = options?.fullWindow
|
|
149
|
+
? ''
|
|
150
|
+
: 'popup=1,scrollbars=0,menubar=0,location=0,resizable=0,status=0,titlebar=0,toolbar=0,width=500,height=700';
|
|
151
|
+
console.log({ popupParameters });
|
|
152
|
+
const popup = window.open(urlObject.toString(), `${pathname}:${window.origin}`, popupParameters);
|
|
153
|
+
if (!popup) {
|
|
154
|
+
throw new Error(`could not open the login popup`);
|
|
155
|
+
}
|
|
156
|
+
currentPopup = { popup, onMessage, rejectRecovery: _rejectRecovery };
|
|
157
|
+
console.log(`listening to message... ${id}`);
|
|
158
|
+
window.addEventListener('message', currentPopup.onMessage);
|
|
159
|
+
watchForPopupClosed(popup);
|
|
160
|
+
// continuouslyPingPopup(popup);
|
|
161
|
+
});
|
|
162
|
+
return storePromise;
|
|
163
|
+
}
|
|
164
|
+
return { launchPopup };
|
|
165
|
+
}
|
package/dist/utils.d.ts
ADDED
package/dist/utils.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export function createStorePromise(store, executor) {
|
|
2
|
+
const storePromise = new Promise(executor);
|
|
3
|
+
for (const key of Object.keys(store)) {
|
|
4
|
+
if (key === 'then') {
|
|
5
|
+
throw new Error(`then field is not allowed`);
|
|
6
|
+
}
|
|
7
|
+
if (key == 'finally') {
|
|
8
|
+
throw new Error(`finally field is not allowed`);
|
|
9
|
+
}
|
|
10
|
+
storePromise[key] = store[key];
|
|
11
|
+
}
|
|
12
|
+
return storePromise;
|
|
13
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@etherplay/connect",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"publishConfig": {
|
|
6
|
+
"access": "public"
|
|
7
|
+
},
|
|
8
|
+
"files": [
|
|
9
|
+
"dist",
|
|
10
|
+
"!dist/**/*.test.*",
|
|
11
|
+
"!dist/**/*.spec.*"
|
|
12
|
+
],
|
|
13
|
+
"sideEffects": [
|
|
14
|
+
"**/*.css"
|
|
15
|
+
],
|
|
16
|
+
"svelte": "./dist/index.js",
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"type": "module",
|
|
19
|
+
"exports": {
|
|
20
|
+
".": {
|
|
21
|
+
"types": "./dist/index.d.ts",
|
|
22
|
+
"svelte": "./dist/index.js"
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"peerDependencies": {
|
|
26
|
+
"svelte": "^5.0.0"
|
|
27
|
+
},
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"@playwright/test": "^1.49.1",
|
|
30
|
+
"@sveltejs/adapter-auto": "^4.0.0",
|
|
31
|
+
"@sveltejs/kit": "^2.16.0",
|
|
32
|
+
"@sveltejs/package": "^2.0.0",
|
|
33
|
+
"@sveltejs/vite-plugin-svelte": "^5.0.0",
|
|
34
|
+
"@testing-library/jest-dom": "^6.6.3",
|
|
35
|
+
"@testing-library/svelte": "^5.2.4",
|
|
36
|
+
"eip-1193": "^0.6.1",
|
|
37
|
+
"jsdom": "^25.0.1",
|
|
38
|
+
"prettier": "^3.4.2",
|
|
39
|
+
"prettier-plugin-svelte": "^3.3.3",
|
|
40
|
+
"publint": "^0.3.2",
|
|
41
|
+
"svelte": "^5.0.0",
|
|
42
|
+
"svelte-check": "^4.0.0",
|
|
43
|
+
"typescript": "^5.0.0",
|
|
44
|
+
"vite": "^6.0.0",
|
|
45
|
+
"vite-plugin-node-polyfills": "^0.23.0",
|
|
46
|
+
"vitest": "^3.0.0"
|
|
47
|
+
},
|
|
48
|
+
"dependencies": {
|
|
49
|
+
"@account-kit/signer": "^4.13.0",
|
|
50
|
+
"@noble/curves": "^1.8.1",
|
|
51
|
+
"@noble/hashes": "^1.7.1",
|
|
52
|
+
"@scure/bip32": "^1.6.2",
|
|
53
|
+
"@scure/bip39": "^1.5.4",
|
|
54
|
+
"zustand": "^5.0.3",
|
|
55
|
+
"@etherplay/alchemy": "0.0.1"
|
|
56
|
+
},
|
|
57
|
+
"scripts": {
|
|
58
|
+
"dev": "vite dev --host --port 60002",
|
|
59
|
+
"build": "vite build && npm run prepack",
|
|
60
|
+
"preview": "vite preview",
|
|
61
|
+
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
|
|
62
|
+
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
|
|
63
|
+
"format": "prettier --write .",
|
|
64
|
+
"lint": "prettier --check .",
|
|
65
|
+
"test:unit": "vitest",
|
|
66
|
+
"test": "npm run test:unit -- --run && npm run test:e2e",
|
|
67
|
+
"test:e2e": "playwright test"
|
|
68
|
+
}
|
|
69
|
+
}
|