@etherplay/connect 0.0.27 → 0.0.29
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 +49 -3169
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +61 -87
- package/dist/index.js.map +1 -1
- package/dist/utils.d.ts +0 -1
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +0 -7
- package/dist/utils.js.map +1 -1
- package/package.json +4 -4
- package/src/index.ts +186 -136
- package/src/utils.ts +0 -9
- package/src/provider.ts +0 -100
package/src/utils.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import {bytesToHex} from '@noble/hashes/utils';
|
|
2
1
|
import type {Readable} from 'svelte/store';
|
|
3
2
|
|
|
4
3
|
export function createStorePromise<U, T, V extends Readable<T>>(
|
|
@@ -20,14 +19,6 @@ export function createStorePromise<U, T, V extends Readable<T>>(
|
|
|
20
19
|
return storePromise;
|
|
21
20
|
}
|
|
22
21
|
|
|
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
|
-
}
|
|
30
|
-
|
|
31
22
|
/**
|
|
32
23
|
* Wraps any promise with a timeout
|
|
33
24
|
* @param promise The promise to wrap with a timeout
|
package/src/provider.ts
DELETED
|
@@ -1,100 +0,0 @@
|
|
|
1
|
-
import type {EIP1193WalletProvider, EIP1193WindowWalletProvider, Methods} from 'eip-1193';
|
|
2
|
-
import {createCurriedJSONRPC, CurriedRPC} from 'remote-procedure-call';
|
|
3
|
-
import {withTimeout} from './utils.js';
|
|
4
|
-
|
|
5
|
-
const signerMethods = [
|
|
6
|
-
'eth_accounts',
|
|
7
|
-
'eth_sign',
|
|
8
|
-
'eth_signTransaction',
|
|
9
|
-
'personal_sign',
|
|
10
|
-
'eth_signTypedData_v4',
|
|
11
|
-
'eth_signTypedData',
|
|
12
|
-
];
|
|
13
|
-
|
|
14
|
-
const connectedAccountMethods = ['eth_sendTransaction'];
|
|
15
|
-
|
|
16
|
-
const walletOnlyMethods = ['eth_requestAccounts', 'wallet_switchEthereumChain', 'wallet_addEthereumChain'];
|
|
17
|
-
|
|
18
|
-
export function createProvider(params: {
|
|
19
|
-
endpoint: string;
|
|
20
|
-
chainId: string;
|
|
21
|
-
prioritizeWalletProvider?: boolean;
|
|
22
|
-
requestsPerSecond?: number;
|
|
23
|
-
}): CurriedRPC<Methods> & {
|
|
24
|
-
setWalletProvider: (walletProvider: EIP1193WindowWalletProvider | undefined) => void;
|
|
25
|
-
setWalletStatus: (newStatus: 'connected' | 'locked' | 'disconnected') => void;
|
|
26
|
-
} & {
|
|
27
|
-
chainId: string;
|
|
28
|
-
} {
|
|
29
|
-
const {endpoint, chainId, prioritizeWalletProvider, requestsPerSecond} = params;
|
|
30
|
-
const jsonRPC = createCurriedJSONRPC(endpoint);
|
|
31
|
-
|
|
32
|
-
let walletProvider: EIP1193WindowWalletProvider | undefined;
|
|
33
|
-
let status: 'connected' | 'locked' | 'disconnected' = 'disconnected';
|
|
34
|
-
|
|
35
|
-
function setWalletProvider(newWalletProvider: EIP1193WindowWalletProvider | undefined) {
|
|
36
|
-
walletProvider = newWalletProvider;
|
|
37
|
-
}
|
|
38
|
-
function setWalletStatus(newStatus: 'connected' | 'locked' | 'disconnected') {
|
|
39
|
-
status = newStatus;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
const provider = {
|
|
43
|
-
async request(req: {method: string; params?: any[]}) {
|
|
44
|
-
const signingMethod =
|
|
45
|
-
signerMethods.includes(req.method) ||
|
|
46
|
-
connectedAccountMethods.includes(req.method) ||
|
|
47
|
-
walletOnlyMethods.includes(req.method) ||
|
|
48
|
-
req.method.indexOf('sign') != -1;
|
|
49
|
-
|
|
50
|
-
if (walletProvider) {
|
|
51
|
-
if (prioritizeWalletProvider || signingMethod) {
|
|
52
|
-
if (signingMethod) {
|
|
53
|
-
if (status !== 'connected') {
|
|
54
|
-
return Promise.reject({message: 'wallet provider is not connected', code: 4001});
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
let currentChainIdAsHex: string;
|
|
59
|
-
try {
|
|
60
|
-
currentChainIdAsHex = await withTimeout(
|
|
61
|
-
walletProvider.request({
|
|
62
|
-
method: 'eth_chainId',
|
|
63
|
-
}),
|
|
64
|
-
);
|
|
65
|
-
} catch (err) {
|
|
66
|
-
if (signingMethod) {
|
|
67
|
-
return Promise.reject(err);
|
|
68
|
-
} else {
|
|
69
|
-
// we fallback on jsonRPc if error while getting chain and not a signing method
|
|
70
|
-
return jsonRPC.request(req);
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
const currentChainId = Number(currentChainIdAsHex).toString();
|
|
75
|
-
if (chainId !== currentChainId) {
|
|
76
|
-
if (signingMethod) {
|
|
77
|
-
return Promise.reject({
|
|
78
|
-
message: `wallet provider is connected to a different chain, expected ${chainId} but got ${currentChainId}`,
|
|
79
|
-
code: 4001,
|
|
80
|
-
});
|
|
81
|
-
} else {
|
|
82
|
-
// we fallback on jsonRPc if invalid chain and not a signing method
|
|
83
|
-
return jsonRPC.request(req);
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
return walletProvider.request(req as any);
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
if (signingMethod) {
|
|
91
|
-
return Promise.reject(new Error('wallet provider is not connected'));
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
return jsonRPC.request(req);
|
|
95
|
-
},
|
|
96
|
-
} as unknown as EIP1193WalletProvider;
|
|
97
|
-
|
|
98
|
-
const curriedRPC = createCurriedJSONRPC<Methods>(provider as any, {requestsPerSecond});
|
|
99
|
-
return {...curriedRPC, setWalletProvider, setWalletStatus, chainId};
|
|
100
|
-
}
|