@etherplay/connect 0.0.8 → 0.0.10
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/dist/index.d.ts +3149 -2
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +283 -61
- package/dist/index.js.map +1 -0
- package/dist/popup.d.ts +1 -0
- package/dist/popup.d.ts.map +1 -0
- package/dist/popup.js +7 -6
- package/dist/popup.js.map +1 -0
- package/dist/provider.d.ts +13 -0
- package/dist/provider.d.ts.map +1 -0
- package/dist/provider.js +48 -0
- package/dist/provider.js.map +1 -0
- package/dist/utils.d.ts +1 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +1 -0
- package/dist/utils.js.map +1 -0
- package/package.json +18 -41
- package/src/index.ts +898 -0
- package/src/popup.ts +204 -0
- package/src/provider.ts +68 -0
- package/src/utils.ts +29 -0
- package/README.md +0 -58
package/src/popup.ts
ADDED
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import {writable, type Readable} from 'svelte/store';
|
|
2
|
+
import {createStorePromise} from './utils.js';
|
|
3
|
+
|
|
4
|
+
export type Error = {
|
|
5
|
+
message: string;
|
|
6
|
+
type?: string;
|
|
7
|
+
cause?: any;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export type Popup = {
|
|
11
|
+
launched: boolean;
|
|
12
|
+
closed: boolean;
|
|
13
|
+
resolved: boolean;
|
|
14
|
+
error?: Error;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export type PopupPromise<T> = ReturnType<typeof createStorePromise<T, Popup, Readable<Popup> & {cancel: () => void}>>;
|
|
18
|
+
|
|
19
|
+
export function createPopupLauncher<T>() {
|
|
20
|
+
let id = 1;
|
|
21
|
+
let currentPopup:
|
|
22
|
+
| {
|
|
23
|
+
popup: Window;
|
|
24
|
+
onMessage: (messageEvent: MessageEvent) => void;
|
|
25
|
+
rejectRecovery: (error: Error) => void;
|
|
26
|
+
}
|
|
27
|
+
| {popup: undefined} = {popup: undefined};
|
|
28
|
+
|
|
29
|
+
function launchPopup(
|
|
30
|
+
url: string,
|
|
31
|
+
options?: {fullWindow?: boolean},
|
|
32
|
+
): Promise<T> & Readable<Popup> & {cancel: () => void} {
|
|
33
|
+
const urlObject = new URL(url);
|
|
34
|
+
const expectedOrigin = `${urlObject.protocol}//${urlObject.host}`;
|
|
35
|
+
const pathname = urlObject.pathname;
|
|
36
|
+
|
|
37
|
+
let $popup: Popup = {
|
|
38
|
+
closed: false,
|
|
39
|
+
launched: false,
|
|
40
|
+
resolved: false,
|
|
41
|
+
};
|
|
42
|
+
const _store = writable<Popup>($popup);
|
|
43
|
+
function set(state: Popup) {
|
|
44
|
+
$popup = state;
|
|
45
|
+
_store.set(state);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (currentPopup.popup) {
|
|
49
|
+
console.log(`stop listening to message from old popup`);
|
|
50
|
+
window.removeEventListener('message', currentPopup.onMessage);
|
|
51
|
+
const tmpRejectRecovery = currentPopup.rejectRecovery;
|
|
52
|
+
let couldCloseExistingPopup = false;
|
|
53
|
+
try {
|
|
54
|
+
currentPopup.popup.close();
|
|
55
|
+
couldCloseExistingPopup = true;
|
|
56
|
+
} catch (err) {
|
|
57
|
+
console.error(err);
|
|
58
|
+
}
|
|
59
|
+
currentPopup = {popup: undefined};
|
|
60
|
+
if (couldCloseExistingPopup) {
|
|
61
|
+
tmpRejectRecovery({message: 'popup closed so new one can take over'});
|
|
62
|
+
} else {
|
|
63
|
+
tmpRejectRecovery({message: 'popup replaced'});
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
let _resolveRecovery: (state: T) => void;
|
|
68
|
+
let _rejectRecovery: (error: Error) => void;
|
|
69
|
+
|
|
70
|
+
function resolveRecovery(state: T) {
|
|
71
|
+
currentPopup = {popup: undefined};
|
|
72
|
+
console.log(`stop listening to message as we resolved it`);
|
|
73
|
+
window.removeEventListener('message', onMessage);
|
|
74
|
+
|
|
75
|
+
if (_resolveRecovery) {
|
|
76
|
+
set({
|
|
77
|
+
closed: true,
|
|
78
|
+
launched: true,
|
|
79
|
+
resolved: true,
|
|
80
|
+
});
|
|
81
|
+
_resolveRecovery(state);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function rejectRecovery(error: Error) {
|
|
86
|
+
currentPopup = {popup: undefined};
|
|
87
|
+
console.log(`stop listening to message as we rejected it`, error);
|
|
88
|
+
window.removeEventListener('message', onMessage);
|
|
89
|
+
if (_rejectRecovery) {
|
|
90
|
+
set({
|
|
91
|
+
closed: true,
|
|
92
|
+
launched: true,
|
|
93
|
+
resolved: true,
|
|
94
|
+
error: {
|
|
95
|
+
message: 'errored',
|
|
96
|
+
cause: error,
|
|
97
|
+
},
|
|
98
|
+
});
|
|
99
|
+
_rejectRecovery(error);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const onMessage = (messageEvent: MessageEvent) => {
|
|
104
|
+
if (messageEvent.origin === expectedOrigin) {
|
|
105
|
+
console.log(messageEvent);
|
|
106
|
+
const data = messageEvent.data;
|
|
107
|
+
if (id == data.id) {
|
|
108
|
+
if (data.error) {
|
|
109
|
+
console.error(`ERROR`, data.error);
|
|
110
|
+
rejectRecovery(data.error);
|
|
111
|
+
} else {
|
|
112
|
+
resolveRecovery(data.result);
|
|
113
|
+
}
|
|
114
|
+
} else {
|
|
115
|
+
console.log(`different id : eventId = ${data.id}, expected id = ${id}`);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
// function continuouslyPingPopup(popup: Window) {
|
|
121
|
+
// const intervalId = setInterval(() => {
|
|
122
|
+
// // // console.log(`checking if popup is closed...`);
|
|
123
|
+
// if (currentPopup.popup !== popup) {
|
|
124
|
+
// console.log(`ping: new popup, we ignore closing state`);
|
|
125
|
+
// clearInterval(intervalId);
|
|
126
|
+
// return;
|
|
127
|
+
// }
|
|
128
|
+
// try {
|
|
129
|
+
// if ('closed' in popup && popup.closed) {
|
|
130
|
+
// console.log(`ping: popup is closed`);
|
|
131
|
+
// clearInterval(intervalId);
|
|
132
|
+
// }
|
|
133
|
+
// console.log(`ping: ${expectedOrigin}`);
|
|
134
|
+
// popup.postMessage({ id, type: 'ping' }, expectedOrigin);
|
|
135
|
+
// } catch (err) {}
|
|
136
|
+
// }, 300);
|
|
137
|
+
// }
|
|
138
|
+
|
|
139
|
+
function watchForPopupClosed(popup: Window) {
|
|
140
|
+
const intervalId = setInterval(() => {
|
|
141
|
+
// console.log(`checking if popup is closed...`);
|
|
142
|
+
if (currentPopup.popup !== popup) {
|
|
143
|
+
console.log(`new popup, we ignore closing state`);
|
|
144
|
+
clearInterval(intervalId);
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
try {
|
|
148
|
+
if ('closed' in popup && popup.closed) {
|
|
149
|
+
console.log(`popup is closed`);
|
|
150
|
+
clearInterval(intervalId);
|
|
151
|
+
|
|
152
|
+
setTimeout(() => {
|
|
153
|
+
// we delay the rejection in case it conflict with an onMessage event
|
|
154
|
+
if (currentPopup.popup === popup) {
|
|
155
|
+
set({
|
|
156
|
+
closed: true,
|
|
157
|
+
launched: $popup.launched,
|
|
158
|
+
resolved: $popup.resolved,
|
|
159
|
+
error: $popup.error,
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
}, 100);
|
|
163
|
+
}
|
|
164
|
+
} catch (err) {}
|
|
165
|
+
}, 200);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const store = {
|
|
169
|
+
subscribe: _store.subscribe,
|
|
170
|
+
cancel() {
|
|
171
|
+
// TODO
|
|
172
|
+
},
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
const storePromise = createStorePromise<T, Popup, Readable<Popup> & {cancel: () => void}>(
|
|
176
|
+
store,
|
|
177
|
+
(resolve, reject) => {
|
|
178
|
+
_resolveRecovery = resolve;
|
|
179
|
+
_rejectRecovery = reject;
|
|
180
|
+
id++;
|
|
181
|
+
urlObject.searchParams.append('origin', window.origin);
|
|
182
|
+
urlObject.searchParams.append('id', id.toString());
|
|
183
|
+
const popupParameters = options?.fullWindow
|
|
184
|
+
? ''
|
|
185
|
+
: 'popup=1,scrollbars=0,menubar=0,location=0,resizable=0,status=0,titlebar=0,toolbar=0,width=500,height=700';
|
|
186
|
+
|
|
187
|
+
console.log({popupParameters});
|
|
188
|
+
const popup = window.open(urlObject.toString(), `${pathname}:${window.origin}`, popupParameters);
|
|
189
|
+
if (!popup) {
|
|
190
|
+
throw new Error(`could not open the login popup`);
|
|
191
|
+
}
|
|
192
|
+
currentPopup = {popup, onMessage, rejectRecovery: _rejectRecovery};
|
|
193
|
+
console.log(`listening to message... ${id}`);
|
|
194
|
+
window.addEventListener('message', currentPopup.onMessage);
|
|
195
|
+
watchForPopupClosed(popup);
|
|
196
|
+
// continuouslyPingPopup(popup);
|
|
197
|
+
},
|
|
198
|
+
);
|
|
199
|
+
|
|
200
|
+
return storePromise;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
return {launchPopup};
|
|
204
|
+
}
|
package/src/provider.ts
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import type {EIP1193WalletProvider, EIP1193WindowWalletProvider, Methods} from 'eip-1193';
|
|
2
|
+
import {createCurriedJSONRPC, CurriedRPC} from 'remote-procedure-call';
|
|
3
|
+
|
|
4
|
+
const signerMethods = [
|
|
5
|
+
'eth_accounts',
|
|
6
|
+
'eth_sign',
|
|
7
|
+
'eth_signTransaction',
|
|
8
|
+
'personal_sign',
|
|
9
|
+
'eth_signTypedData_v4',
|
|
10
|
+
'eth_signTypedData',
|
|
11
|
+
];
|
|
12
|
+
|
|
13
|
+
const connectedAccountMethods = ['eth_sendTransaction'];
|
|
14
|
+
|
|
15
|
+
const walletOnlyMethods = ['eth_requestAccounts', 'wallet_switchEthereumChain', 'wallet_addEthereumChain'];
|
|
16
|
+
|
|
17
|
+
export function createProvider(params: {
|
|
18
|
+
endpoint: string;
|
|
19
|
+
chainId: string;
|
|
20
|
+
prioritizeWalletProvider?: boolean;
|
|
21
|
+
requestsPerSecond?: number;
|
|
22
|
+
}): CurriedRPC<Methods> & {setWalletProvider: (walletProvider: EIP1193WindowWalletProvider | undefined) => void} & {
|
|
23
|
+
chainId: string;
|
|
24
|
+
} {
|
|
25
|
+
const {endpoint, chainId, prioritizeWalletProvider, requestsPerSecond} = params;
|
|
26
|
+
const jsonRPC = createCurriedJSONRPC(endpoint);
|
|
27
|
+
|
|
28
|
+
let walletProvider: EIP1193WindowWalletProvider | undefined;
|
|
29
|
+
|
|
30
|
+
function setWalletProvider(walletProvider: EIP1193WindowWalletProvider | undefined) {
|
|
31
|
+
walletProvider = walletProvider;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const provider = {
|
|
35
|
+
request(req: {method: string; params?: any[]}) {
|
|
36
|
+
if (walletProvider) {
|
|
37
|
+
const signingMethod =
|
|
38
|
+
signerMethods.includes(req.method) ||
|
|
39
|
+
connectedAccountMethods.includes(req.method) ||
|
|
40
|
+
walletOnlyMethods.includes(req.method) ||
|
|
41
|
+
req.method.indexOf('sign') != -1;
|
|
42
|
+
if (prioritizeWalletProvider || signingMethod) {
|
|
43
|
+
const currentChainIdAsHex = walletProvider.request({
|
|
44
|
+
method: 'eth_chainId',
|
|
45
|
+
});
|
|
46
|
+
const currentChainId = Number(currentChainIdAsHex).toString();
|
|
47
|
+
if (chainId !== currentChainId) {
|
|
48
|
+
if (signingMethod) {
|
|
49
|
+
return Promise.reject(
|
|
50
|
+
new Error(
|
|
51
|
+
`wallet provider is connected to a different chain, expected ${chainId} but got ${currentChainId}`,
|
|
52
|
+
),
|
|
53
|
+
);
|
|
54
|
+
} else {
|
|
55
|
+
// we fallback on jsonRPc if invalid chain and not a signing method
|
|
56
|
+
return jsonRPC.request(req);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return walletProvider.request(req as any);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return jsonRPC.request(req);
|
|
64
|
+
},
|
|
65
|
+
} as unknown as EIP1193WalletProvider;
|
|
66
|
+
|
|
67
|
+
return {...createCurriedJSONRPC<Methods>(provider as any, {requestsPerSecond}), setWalletProvider, chainId};
|
|
68
|
+
}
|
package/src/utils.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import {bytesToHex} from '@noble/hashes/utils';
|
|
2
|
+
import type {Readable} from 'svelte/store';
|
|
3
|
+
|
|
4
|
+
export function createStorePromise<U, T, V extends Readable<T>>(
|
|
5
|
+
store: V,
|
|
6
|
+
executor: (resolve: (value: U | PromiseLike<U>) => void, reject: (reason?: any) => void) => void,
|
|
7
|
+
): Promise<U> & V {
|
|
8
|
+
const storePromise = new Promise<U>(executor) as Promise<U> & V;
|
|
9
|
+
|
|
10
|
+
for (const key of Object.keys(store)) {
|
|
11
|
+
if (key === 'then') {
|
|
12
|
+
throw new Error(`then field is not allowed`);
|
|
13
|
+
}
|
|
14
|
+
if (key == 'finally') {
|
|
15
|
+
throw new Error(`finally field is not allowed`);
|
|
16
|
+
}
|
|
17
|
+
(storePromise as any)[key] = (store as any)[key];
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return storePromise;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const encoder = new TextEncoder();
|
|
24
|
+
|
|
25
|
+
export function hashMessage(message: string): `0x${string}` {
|
|
26
|
+
const messageAsBytes = encoder.encode(message);
|
|
27
|
+
const msg = `0x${bytesToHex(messageAsBytes)}` as `0x${string}`;
|
|
28
|
+
return msg;
|
|
29
|
+
}
|
package/README.md
DELETED
|
@@ -1,58 +0,0 @@
|
|
|
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
|
-
```
|