@cowprotocol/iframe-transport 2.0.8 → 2.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/IframeTransport.d.ts +11 -0
- package/iframeRpcProvider/IframeRpcProviderBridge.d.ts +45 -0
- package/iframeRpcProvider/WidgetEthereumProvider.d.ts +123 -0
- package/iframeRpcProvider/iframeRpcProviderEvents.d.ts +31 -0
- package/iframeRpcProvider/utils.d.ts +4 -0
- package/index.cjs +1 -0
- package/index.d.ts +5 -0
- package/index.js +395 -0
- package/package.json +18 -6
- package/types.d.ts +59 -0
- package/.babelrc +0 -20
- package/CHANGELOG.md +0 -151
- package/jest.config.ts +0 -11
- package/project.json +0 -42
- package/src/IframeTransport.ts +0 -73
- package/src/iframeRpcProvider/IframeRpcProviderBridge.ts +0 -192
- package/src/iframeRpcProvider/WidgetEthereumProvider.ts +0 -489
- package/src/iframeRpcProvider/iframeRpcProviderEvents.ts +0 -40
- package/src/iframeRpcProvider/utils.ts +0 -18
- package/src/index.ts +0 -5
- package/src/types.ts +0 -75
- package/tsconfig.json +0 -21
- package/tsconfig.lib.json +0 -10
- package/tsconfig.spec.json +0 -20
package/types.d.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
export interface JsonRpcRequest {
|
|
2
|
+
id?: number;
|
|
3
|
+
method: string;
|
|
4
|
+
params?: any[];
|
|
5
|
+
}
|
|
6
|
+
export interface JsonRpcRequestMessage {
|
|
7
|
+
jsonrpc: '2.0';
|
|
8
|
+
id?: number;
|
|
9
|
+
method: string;
|
|
10
|
+
params: unknown[] | undefined;
|
|
11
|
+
}
|
|
12
|
+
export interface BaseJsonRpcResponseMessage {
|
|
13
|
+
id: number;
|
|
14
|
+
jsonrpc: '2.0';
|
|
15
|
+
}
|
|
16
|
+
export interface JsonRpcSucessfulResponseMessage<TResult = unknown> extends BaseJsonRpcResponseMessage {
|
|
17
|
+
result: TResult;
|
|
18
|
+
}
|
|
19
|
+
export interface JsonRpcError<TData = unknown> {
|
|
20
|
+
code: number;
|
|
21
|
+
message: string;
|
|
22
|
+
data?: TData;
|
|
23
|
+
}
|
|
24
|
+
export interface JsonRpcErrorResponseMessage<TErrorData = unknown> extends BaseJsonRpcResponseMessage {
|
|
25
|
+
error: JsonRpcError<TErrorData>;
|
|
26
|
+
}
|
|
27
|
+
export type JsonRpcResponse = JsonRpcRequestMessage | JsonRpcErrorResponseMessage | JsonRpcSucessfulResponseMessage;
|
|
28
|
+
export interface EthereumProvider {
|
|
29
|
+
/**
|
|
30
|
+
* Subscribes to Ethereum-related events.
|
|
31
|
+
* @param event - The event to subscribe to.
|
|
32
|
+
* @param args - Arguments for the event.
|
|
33
|
+
*/
|
|
34
|
+
on(event: string, args: unknown): void;
|
|
35
|
+
/**
|
|
36
|
+
* Sends a JSON-RPC request to the Ethereum provider and returns the response.
|
|
37
|
+
* @param params - JSON-RPC request parameters.
|
|
38
|
+
* @returns A promise that resolves with the response.
|
|
39
|
+
*/
|
|
40
|
+
request<T>(params: JsonRpcRequest): Promise<T>;
|
|
41
|
+
}
|
|
42
|
+
export type WindowListener = (event: MessageEvent<unknown>) => void;
|
|
43
|
+
export interface ProviderWcMetadata {
|
|
44
|
+
name: string;
|
|
45
|
+
description: string;
|
|
46
|
+
url: string;
|
|
47
|
+
icons: string[];
|
|
48
|
+
verifyUrl?: string;
|
|
49
|
+
redirect?: {
|
|
50
|
+
native?: string;
|
|
51
|
+
universal?: string;
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
export interface EIP6963ProviderInfo {
|
|
55
|
+
uuid: string;
|
|
56
|
+
name: string;
|
|
57
|
+
icon: string;
|
|
58
|
+
rdns: string;
|
|
59
|
+
}
|
package/.babelrc
DELETED
package/CHANGELOG.md
DELETED
|
@@ -1,151 +0,0 @@
|
|
|
1
|
-
# Changelog
|
|
2
|
-
|
|
3
|
-
## [2.0.8](https://github.com/cowprotocol/cowswap/compare/iframe-transport-v2.0.7...iframe-transport-v2.0.8) (2026-03-11)
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
### Dependencies
|
|
7
|
-
|
|
8
|
-
* The following workspace dependencies were updated
|
|
9
|
-
* dependencies
|
|
10
|
-
* @cowprotocol/types bumped to 4.0.0
|
|
11
|
-
|
|
12
|
-
## [2.0.7](https://github.com/cowprotocol/cowswap/compare/iframe-transport-v2.0.6...iframe-transport-v2.0.7) (2026-03-10)
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
### Dependencies
|
|
16
|
-
|
|
17
|
-
* The following workspace dependencies were updated
|
|
18
|
-
* dependencies
|
|
19
|
-
* @cowprotocol/types bumped to 3.4.0
|
|
20
|
-
|
|
21
|
-
## [2.0.6](https://github.com/cowprotocol/cowswap/compare/iframe-transport-v2.0.5...iframe-transport-v2.0.6) (2026-02-25)
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
### 🔧 Miscellaneous
|
|
25
|
-
|
|
26
|
-
* main -> dev ([9c6611e](https://github.com/cowprotocol/cowswap/commit/9c6611e90a4b23e23f6aaadc513de3c6d7f3b562))
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
### Dependencies
|
|
30
|
-
|
|
31
|
-
* The following workspace dependencies were updated
|
|
32
|
-
* dependencies
|
|
33
|
-
* @cowprotocol/types bumped to 3.3.2
|
|
34
|
-
|
|
35
|
-
## [2.0.5](https://github.com/cowprotocol/cowswap/compare/iframe-transport-v2.0.4...iframe-transport-v2.0.5) (2026-02-24)
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
### 🔧 Miscellaneous
|
|
39
|
-
|
|
40
|
-
* merge main into dev 23 02 ([#7062](https://github.com/cowprotocol/cowswap/issues/7062)) ([ac48c9c](https://github.com/cowprotocol/cowswap/commit/ac48c9c83284c6228ba71392edc1c1e0483b53ff))
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
### Dependencies
|
|
44
|
-
|
|
45
|
-
* The following workspace dependencies were updated
|
|
46
|
-
* dependencies
|
|
47
|
-
* @cowprotocol/types bumped to 3.3.1
|
|
48
|
-
|
|
49
|
-
## [2.0.4](https://github.com/cowprotocol/cowswap/compare/iframe-transport-v2.0.3...iframe-transport-v2.0.4) (2026-02-20)
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
### Dependencies
|
|
53
|
-
|
|
54
|
-
* The following workspace dependencies were updated
|
|
55
|
-
* dependencies
|
|
56
|
-
* @cowprotocol/types bumped to 3.3.0
|
|
57
|
-
|
|
58
|
-
## [2.0.3](https://github.com/cowprotocol/cowswap/compare/iframe-transport-v2.0.2...iframe-transport-v2.0.3) (2026-02-13)
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
### Dependencies
|
|
62
|
-
|
|
63
|
-
* The following workspace dependencies were updated
|
|
64
|
-
* dependencies
|
|
65
|
-
* @cowprotocol/types bumped to 3.2.0
|
|
66
|
-
|
|
67
|
-
## [2.0.2](https://github.com/cowprotocol/cowswap/compare/iframe-transport-v2.0.1...iframe-transport-v2.0.2) (2026-02-05)
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
### Dependencies
|
|
71
|
-
|
|
72
|
-
* The following workspace dependencies were updated
|
|
73
|
-
* dependencies
|
|
74
|
-
* @cowprotocol/types bumped to 3.1.0
|
|
75
|
-
|
|
76
|
-
## [2.0.1](https://github.com/cowprotocol/cowswap/compare/iframe-transport-v2.0.0...iframe-transport-v2.0.1) (2026-02-02)
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
### Dependencies
|
|
80
|
-
|
|
81
|
-
* The following workspace dependencies were updated
|
|
82
|
-
* dependencies
|
|
83
|
-
* @cowprotocol/types bumped to 3.0.1
|
|
84
|
-
|
|
85
|
-
## [2.0.0](https://github.com/cowprotocol/cowswap/compare/iframe-transport-v1.2.0...iframe-transport-v2.0.0) (2026-01-28)
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
### ⚠ BREAKING CHANGES
|
|
89
|
-
|
|
90
|
-
* migrate from yarn to pnpm ([#6847](https://github.com/cowprotocol/cowswap/issues/6847))
|
|
91
|
-
|
|
92
|
-
### Features
|
|
93
|
-
|
|
94
|
-
* migrate from yarn to pnpm ([#6847](https://github.com/cowprotocol/cowswap/issues/6847)) ([1c5d115](https://github.com/cowprotocol/cowswap/commit/1c5d11516d0ddb997b0ebbb4eee4adcb491204a4))
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
### Dependencies
|
|
98
|
-
|
|
99
|
-
* The following workspace dependencies were updated
|
|
100
|
-
* dependencies
|
|
101
|
-
* @cowprotocol/types bumped to 3.0.0
|
|
102
|
-
|
|
103
|
-
## [1.2.0](https://github.com/cowprotocol/cowswap/compare/iframe-transport-v1.1.1...iframe-transport-v1.2.0) (2026-01-26)
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
### Features
|
|
107
|
-
|
|
108
|
-
* fix types in iframe-transport lib ([#6882](https://github.com/cowprotocol/cowswap/issues/6882)) ([f419e21](https://github.com/cowprotocol/cowswap/commit/f419e219fefdd007101ba61053dcce6c218eeee0))
|
|
109
|
-
|
|
110
|
-
## [1.1.1](https://github.com/cowprotocol/cowswap/compare/iframe-transport-v1.1.0...iframe-transport-v1.1.1) (2026-01-26)
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
### Bug Fixes
|
|
114
|
-
|
|
115
|
-
* add a guard for ssr in eip6963 listener ([#6849](https://github.com/cowprotocol/cowswap/issues/6849)) ([aeaf7e8](https://github.com/cowprotocol/cowswap/commit/aeaf7e810648214252359620f7bb63b0b207c227))
|
|
116
|
-
* **widget-lib:** add compatibility with server rendering ([#6813](https://github.com/cowprotocol/cowswap/issues/6813)) ([76bd736](https://github.com/cowprotocol/cowswap/commit/76bd736f17d722f0a639542d4fcbd5e3f0be57b2))
|
|
117
|
-
|
|
118
|
-
## [1.1.0](https://github.com/cowprotocol/cowswap/compare/iframe-transport-v1.0.3...iframe-transport-v1.1.0) (2025-08-12)
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
### Features
|
|
122
|
-
|
|
123
|
-
* rename Permit hook dapp id ([#6138](https://github.com/cowprotocol/cowswap/issues/6138)) ([41b0b18](https://github.com/cowprotocol/cowswap/commit/41b0b182d4a673bb1799a699ea83029ee8c6d73b))
|
|
124
|
-
|
|
125
|
-
## [1.0.3](https://github.com/cowprotocol/cowswap/compare/iframe-transport-v1.0.2...iframe-transport-v1.0.3) (2025-06-18)
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
### Bug Fixes
|
|
129
|
-
|
|
130
|
-
* make order progress bar UI more stable ([#5834](https://github.com/cowprotocol/cowswap/issues/5834)) ([80cebd1](https://github.com/cowprotocol/cowswap/commit/80cebd1104a0a2e30e12ce8a4b3d62c212559cec))
|
|
131
|
-
|
|
132
|
-
## [1.0.2](https://github.com/cowprotocol/cowswap/compare/iframe-transport-v1.0.1...iframe-transport-v1.0.2) (2025-05-19)
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
### Bug Fixes
|
|
136
|
-
|
|
137
|
-
* **dev-deps-update:** bump the dev-deps group with 76 updates ([#5663](https://github.com/cowprotocol/cowswap/issues/5663)) ([552b349](https://github.com/cowprotocol/cowswap/commit/552b349f53762a01ccf008e9a2083248424cbafa))
|
|
138
|
-
|
|
139
|
-
## [1.0.1](https://github.com/cowprotocol/cowswap/compare/iframe-transport-v1.0.0...iframe-transport-v1.0.1) (2025-02-03)
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
### Bug Fixes
|
|
143
|
-
|
|
144
|
-
* display Metamask warning only for extension/mobile wallets ([#5370](https://github.com/cowprotocol/cowswap/issues/5370)) ([abb5683](https://github.com/cowprotocol/cowswap/commit/abb56834b1e21029ca66362f3b721cd47e080547))
|
|
145
|
-
|
|
146
|
-
## 1.0.0 (2024-09-30)
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
### Features
|
|
150
|
-
|
|
151
|
-
* **hooks-store:** support iframe hook-dapps ([#4885](https://github.com/cowprotocol/cowswap/issues/4885)) ([5116f29](https://github.com/cowprotocol/cowswap/commit/5116f296a0e4866d0a51a806b7abe55e809d26e9))
|
package/jest.config.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
export default {
|
|
2
|
-
displayName: 'iframe-transport',
|
|
3
|
-
preset: '../../jest.preset.js',
|
|
4
|
-
transform: {
|
|
5
|
-
'^(?!.*\\.(js|jsx|ts|tsx|css|json)$)': '@nx/react/plugins/jest',
|
|
6
|
-
'^.+\\.[tj]sx?$': ['babel-jest', { presets: ['@nx/react/babel'] }],
|
|
7
|
-
},
|
|
8
|
-
setupFilesAfterEnv: ['../../jest.setup.ts'],
|
|
9
|
-
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
|
|
10
|
-
coverageDirectory: '../../coverage/libs/iframe-transport',
|
|
11
|
-
}
|
package/project.json
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "iframe-transport",
|
|
3
|
-
"$schema": "../../node_modules/nx/schemas/project-schema.json",
|
|
4
|
-
"sourceRoot": "libs/iframe-transport/src",
|
|
5
|
-
"projectType": "library",
|
|
6
|
-
"tags": [],
|
|
7
|
-
"targets": {
|
|
8
|
-
"lint": {
|
|
9
|
-
"executor": "@nx/eslint:lint",
|
|
10
|
-
"outputs": ["{options.outputFile}"],
|
|
11
|
-
"options": {
|
|
12
|
-
"lintFilePatterns": ["libs/iframe-transport/**/*.{ts,tsx,js,jsx}"]
|
|
13
|
-
}
|
|
14
|
-
},
|
|
15
|
-
"build": {
|
|
16
|
-
"executor": "@nx/js:tsc",
|
|
17
|
-
"outputs": ["{options.outputPath}"],
|
|
18
|
-
"options": {
|
|
19
|
-
"outputPath": "dist/libs/iframe-transport",
|
|
20
|
-
"main": "libs/iframe-transport/src/index.ts",
|
|
21
|
-
"tsConfig": "libs/iframe-transport/tsconfig.lib.json",
|
|
22
|
-
"assets": ["libs/iframe-transport/*.md"],
|
|
23
|
-
"updateBuildableProjectDepsInPackageJson": true,
|
|
24
|
-
"buildableProjectDepsInPackageJsonType": "dependencies"
|
|
25
|
-
}
|
|
26
|
-
},
|
|
27
|
-
"test": {
|
|
28
|
-
"executor": "@nx/jest:jest",
|
|
29
|
-
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
|
|
30
|
-
"options": {
|
|
31
|
-
"jestConfig": "libs/iframe-transport/jest.config.ts",
|
|
32
|
-
"passWithNoTests": true
|
|
33
|
-
},
|
|
34
|
-
"configurations": {
|
|
35
|
-
"ci": {
|
|
36
|
-
"ci": true,
|
|
37
|
-
"codeCoverage": true
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
}
|
package/src/IframeTransport.ts
DELETED
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
// Yes, this is a hack. For some reason I can't specify other types as a key
|
|
2
|
-
import { WindowListener } from './types'
|
|
3
|
-
|
|
4
|
-
// @ts-ignore
|
|
5
|
-
type AbstractRecord = Record<unknown, unknown>
|
|
6
|
-
|
|
7
|
-
export class IframeTransport<MethodsEmitPayloadMap extends AbstractRecord> {
|
|
8
|
-
constructor(public readonly key: string) {}
|
|
9
|
-
|
|
10
|
-
postMessageToWindow<T extends keyof MethodsEmitPayloadMap>(
|
|
11
|
-
contentWindow: Window,
|
|
12
|
-
method: T,
|
|
13
|
-
payload: MethodsEmitPayloadMap[T],
|
|
14
|
-
): void {
|
|
15
|
-
const data = typeof payload === 'object' ? payload : {}
|
|
16
|
-
const postPayload = {
|
|
17
|
-
key: this.key,
|
|
18
|
-
method,
|
|
19
|
-
...data,
|
|
20
|
-
}
|
|
21
|
-
contentWindow.postMessage(
|
|
22
|
-
postPayload,
|
|
23
|
-
'*', // TODO: Change to CoW specific origin in production. https://github.com/cowprotocol/cowswap/issues/3828
|
|
24
|
-
)
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
listenToMessageFromWindow<T extends keyof MethodsEmitPayloadMap>(
|
|
28
|
-
contentWindow: Window,
|
|
29
|
-
method: T,
|
|
30
|
-
callback: (payload: MethodsEmitPayloadMap[T]) => void,
|
|
31
|
-
): (payload: MessageEvent<unknown>) => void {
|
|
32
|
-
const listener = (event: MessageEvent<unknown>): void => {
|
|
33
|
-
if (!isEventData(event.data) || event.data.key !== this.key || event.data.method !== method) {
|
|
34
|
-
return
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
callback(event.data as MethodsEmitPayloadMap[T])
|
|
38
|
-
}
|
|
39
|
-
contentWindow.addEventListener('message', listener)
|
|
40
|
-
|
|
41
|
-
return listener
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
stopListeningToMessageFromWindow<T extends keyof MethodsEmitPayloadMap>(
|
|
45
|
-
contentWindow: Window,
|
|
46
|
-
_method: T,
|
|
47
|
-
callback: (payload: MethodsEmitPayloadMap[T]) => void,
|
|
48
|
-
): void {
|
|
49
|
-
// TODO: Replace any with proper type definitions
|
|
50
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
51
|
-
contentWindow.removeEventListener('message', callback as any)
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
stopListeningWindowListener(contentWindow: Window, callback: WindowListener): void {
|
|
55
|
-
contentWindow.removeEventListener('message', callback)
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
interface EventData {
|
|
60
|
-
key: string
|
|
61
|
-
method: string
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
function isEventData(obj: unknown): obj is EventData {
|
|
65
|
-
return (
|
|
66
|
-
typeof obj === 'object' &&
|
|
67
|
-
obj !== null &&
|
|
68
|
-
'key' in obj &&
|
|
69
|
-
'method' in obj &&
|
|
70
|
-
typeof obj.key === 'string' &&
|
|
71
|
-
typeof obj.method === 'string'
|
|
72
|
-
)
|
|
73
|
-
}
|
|
@@ -1,192 +0,0 @@
|
|
|
1
|
-
import type { EIP6963AnnounceProviderEvent, EIP6963ProviderDetail } from '@cowprotocol/types'
|
|
2
|
-
|
|
3
|
-
import {
|
|
4
|
-
IframeRpcProviderEvents,
|
|
5
|
-
iframeRpcProviderTransport,
|
|
6
|
-
ProviderRpcRequestPayload,
|
|
7
|
-
ProviderRpcResponsePayload,
|
|
8
|
-
} from './iframeRpcProviderEvents'
|
|
9
|
-
import { getEip6963ProviderInfo, getProviderWcMetadata } from './utils'
|
|
10
|
-
|
|
11
|
-
import type { EthereumProvider, JsonRpcRequestMessage } from '../types'
|
|
12
|
-
|
|
13
|
-
const EVENTS_TO_FORWARD_TO_IFRAME = ['connect', 'disconnect', 'close', 'chainChanged', 'accountsChanged']
|
|
14
|
-
const eip6963Providers: EIP6963ProviderDetail[] = []
|
|
15
|
-
|
|
16
|
-
if (typeof window !== 'undefined') {
|
|
17
|
-
window.addEventListener('eip6963:announceProvider', (event: Event) => {
|
|
18
|
-
const providerEvent = event as EIP6963AnnounceProviderEvent
|
|
19
|
-
eip6963Providers.push(providerEvent.detail)
|
|
20
|
-
})
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Handles JSON-RPC request coming from an iFrame by delegating to a given Ethereum provider.
|
|
25
|
-
* The result will be passed back to the iFrame.
|
|
26
|
-
*
|
|
27
|
-
* Additionally, it will forward some special events from the wallet, to the iFrame window, for example connect/disconnect/chainChanged
|
|
28
|
-
*/
|
|
29
|
-
export class IframeRpcProviderBridge {
|
|
30
|
-
/**
|
|
31
|
-
* The Ethereum provider instance.
|
|
32
|
-
* When is null the JSON-RPC bridge is disconnected from the Ethereum provider.
|
|
33
|
-
* */
|
|
34
|
-
private ethereumProvider: EthereumProvider | null = null
|
|
35
|
-
|
|
36
|
-
/** Stored JSON-RPC requests, to queue them when disconnected. */
|
|
37
|
-
private requestWaitingForConnection: { [key: string]: JsonRpcRequestMessage } = {}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Creates an instance of IframeRpcProviderBridge.
|
|
41
|
-
* @param iframeWidow - The iFrame window that will post up general RPC messages and to which the IframeRpcProviderBridge will forward the RPC result.
|
|
42
|
-
* Also it will receive some special RPC events coming from the wallet, like connect/chainChanged,accountChanged
|
|
43
|
-
*/
|
|
44
|
-
constructor(private iframeWidow: Window) {}
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Disconnects the JSON-RPC bridge from the Ethereum provider.
|
|
48
|
-
*/
|
|
49
|
-
disconnect(): void {
|
|
50
|
-
if (typeof window === 'undefined') return
|
|
51
|
-
|
|
52
|
-
// Disconnect provider
|
|
53
|
-
this.ethereumProvider = null
|
|
54
|
-
iframeRpcProviderTransport.stopListeningToMessageFromWindow(
|
|
55
|
-
window,
|
|
56
|
-
IframeRpcProviderEvents.PROVIDER_RPC_REQUEST,
|
|
57
|
-
this.processRpcCallFromWindow,
|
|
58
|
-
)
|
|
59
|
-
|
|
60
|
-
iframeRpcProviderTransport.stopListeningToMessageFromWindow(
|
|
61
|
-
window,
|
|
62
|
-
IframeRpcProviderEvents.REQUEST_PROVIDER_META_INFO,
|
|
63
|
-
this.processProviderMetaInfoRequest,
|
|
64
|
-
)
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* Handles the 'connect' event and sets up event listeners for Ethereum provider events.
|
|
69
|
-
* @param newProvider - The Ethereum provider to connect.
|
|
70
|
-
*/
|
|
71
|
-
onConnect(newProvider: EthereumProvider): void {
|
|
72
|
-
if (typeof window === 'undefined') return
|
|
73
|
-
|
|
74
|
-
// Disconnect the previous provider
|
|
75
|
-
if (this.ethereumProvider) {
|
|
76
|
-
this.disconnect()
|
|
77
|
-
} else {
|
|
78
|
-
// Listen for messages coming to the main window (from the iFrame window)
|
|
79
|
-
iframeRpcProviderTransport.listenToMessageFromWindow(
|
|
80
|
-
window,
|
|
81
|
-
IframeRpcProviderEvents.PROVIDER_RPC_REQUEST,
|
|
82
|
-
this.processRpcCallFromWindow,
|
|
83
|
-
)
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
// Save the provider
|
|
87
|
-
this.ethereumProvider = newProvider
|
|
88
|
-
|
|
89
|
-
// Process pending requests
|
|
90
|
-
this.processPendingRequests()
|
|
91
|
-
|
|
92
|
-
// Register in the provider, the events that needs to be forwarded to the iFrame window
|
|
93
|
-
EVENTS_TO_FORWARD_TO_IFRAME.forEach((event) => {
|
|
94
|
-
newProvider.on(event, (params: unknown) => this.onProviderEvent(event, params))
|
|
95
|
-
})
|
|
96
|
-
|
|
97
|
-
// Listen for provider meta info request
|
|
98
|
-
iframeRpcProviderTransport.listenToMessageFromWindow(
|
|
99
|
-
window,
|
|
100
|
-
IframeRpcProviderEvents.REQUEST_PROVIDER_META_INFO,
|
|
101
|
-
this.processProviderMetaInfoRequest,
|
|
102
|
-
)
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
// TODO: Add proper return type annotation
|
|
106
|
-
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
|
107
|
-
private processPendingRequests() {
|
|
108
|
-
// Process pending requests
|
|
109
|
-
Object.keys(this.requestWaitingForConnection).forEach((key) => {
|
|
110
|
-
this.processRpcRequest(this.requestWaitingForConnection[key])
|
|
111
|
-
})
|
|
112
|
-
|
|
113
|
-
// Clear pending requests
|
|
114
|
-
this.requestWaitingForConnection = {}
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
/**
|
|
118
|
-
* Processes a JSON-RPC request and sends appropriate response or error via the content window.
|
|
119
|
-
* @param request - The JSON-RPC request to be processed.
|
|
120
|
-
*/
|
|
121
|
-
// TODO: Add proper return type annotation
|
|
122
|
-
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
|
123
|
-
processRpcRequest(request: JsonRpcRequestMessage) {
|
|
124
|
-
const { id, jsonrpc, method } = request
|
|
125
|
-
if (!this.ethereumProvider || !id) {
|
|
126
|
-
return
|
|
127
|
-
}
|
|
128
|
-
const requestPromise =
|
|
129
|
-
// Keep the legacy "enable" method for backward compatibility
|
|
130
|
-
method === 'enable'
|
|
131
|
-
? this.ethereumProvider.request({ method: 'eth_requestAccounts', id })
|
|
132
|
-
: this.ethereumProvider.request({ ...request, id })
|
|
133
|
-
|
|
134
|
-
// Do request, and forward the result or error to the iFrame window
|
|
135
|
-
requestPromise
|
|
136
|
-
.then((result) =>
|
|
137
|
-
this.forwardRpcResponseToIframe({
|
|
138
|
-
rpcResponse: { jsonrpc, id, result },
|
|
139
|
-
}),
|
|
140
|
-
)
|
|
141
|
-
.catch((error) =>
|
|
142
|
-
this.forwardRpcResponseToIframe({
|
|
143
|
-
rpcResponse: { jsonrpc, id, error },
|
|
144
|
-
}),
|
|
145
|
-
)
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
private processRpcCallFromWindow = ({ rpcRequest }: ProviderRpcRequestPayload): void => {
|
|
149
|
-
// If disconnected, ignore the message. Also persist the request
|
|
150
|
-
if (!this.ethereumProvider) {
|
|
151
|
-
if (rpcRequest.id) {
|
|
152
|
-
this.requestWaitingForConnection[rpcRequest.id] = rpcRequest
|
|
153
|
-
}
|
|
154
|
-
return
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
this.processRpcRequest(rpcRequest)
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
private processProviderMetaInfoRequest = (): void => {
|
|
161
|
-
if (!this.ethereumProvider) return
|
|
162
|
-
// We assume, that all EIP-6963 providers are already announced
|
|
163
|
-
const providerEip6963Info = getEip6963ProviderInfo(this.ethereumProvider, eip6963Providers)
|
|
164
|
-
const providerWcMetadata = getProviderWcMetadata(this.ethereumProvider)
|
|
165
|
-
|
|
166
|
-
// Send the provider meta info to the iFrame window
|
|
167
|
-
iframeRpcProviderTransport.postMessageToWindow(this.iframeWidow, IframeRpcProviderEvents.SEND_PROVIDER_META_INFO, {
|
|
168
|
-
providerEip6963Info,
|
|
169
|
-
providerWcMetadata,
|
|
170
|
-
})
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
private onProviderEvent(event: string, params: unknown): void {
|
|
174
|
-
iframeRpcProviderTransport.postMessageToWindow(this.iframeWidow, IframeRpcProviderEvents.PROVIDER_ON_EVENT, {
|
|
175
|
-
event,
|
|
176
|
-
params,
|
|
177
|
-
})
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
/**
|
|
181
|
-
* Forward a JSON-RPC message to the content window.
|
|
182
|
-
*/
|
|
183
|
-
// TODO: Add proper return type annotation
|
|
184
|
-
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
|
185
|
-
private forwardRpcResponseToIframe(params: ProviderRpcResponsePayload) {
|
|
186
|
-
iframeRpcProviderTransport.postMessageToWindow(
|
|
187
|
-
this.iframeWidow,
|
|
188
|
-
IframeRpcProviderEvents.PROVIDER_RPC_RESPONSE,
|
|
189
|
-
params,
|
|
190
|
-
)
|
|
191
|
-
}
|
|
192
|
-
}
|