@cowprotocol/iframe-transport 1.2.0 → 2.0.3
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/.babelrc +20 -0
- package/CHANGELOG.md +96 -0
- package/LICENSE +674 -0
- package/jest.config.ts +11 -0
- package/package.json +11 -18
- package/project.json +42 -0
- package/src/IframeTransport.ts +73 -0
- package/src/iframeRpcProvider/IframeRpcProviderBridge.ts +192 -0
- package/src/iframeRpcProvider/WidgetEthereumProvider.ts +489 -0
- package/src/iframeRpcProvider/iframeRpcProviderEvents.ts +40 -0
- package/src/iframeRpcProvider/utils.ts +18 -0
- package/src/index.ts +5 -0
- package/src/types.ts +75 -0
- package/tsconfig.json +21 -0
- package/tsconfig.lib.json +10 -0
- package/tsconfig.spec.json +20 -0
- package/IframeTransport.d.ts +0 -11
- package/iframeRpcProvider/IframeRpcProviderBridge.d.ts +0 -45
- package/iframeRpcProvider/WidgetEthereumProvider.d.ts +0 -123
- package/iframeRpcProvider/iframeRpcProviderEvents.d.ts +0 -31
- package/iframeRpcProvider/utils.d.ts +0 -4
- package/index.d.ts +0 -5
- package/index.js +0 -1
- package/index.mjs +0 -498
- package/types.d.ts +0 -59
package/jest.config.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
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/package.json
CHANGED
|
@@ -1,23 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cowprotocol/iframe-transport",
|
|
3
|
-
"version": "
|
|
4
|
-
"type": "commonjs",
|
|
3
|
+
"version": "2.0.3",
|
|
5
4
|
"description": "CoW Swap iframe-transport",
|
|
6
|
-
"
|
|
7
|
-
"
|
|
8
|
-
|
|
9
|
-
"require": "./index.mjs"
|
|
10
|
-
},
|
|
5
|
+
"type": "commonjs",
|
|
6
|
+
"main": "./src/index.ts",
|
|
7
|
+
"types": "./src/index.d.ts",
|
|
11
8
|
"license": "ISC",
|
|
12
|
-
"
|
|
13
|
-
"
|
|
14
|
-
"url": "https://github.com/cowprotocol/cowswap"
|
|
9
|
+
"publishConfig": {
|
|
10
|
+
"access": "public"
|
|
15
11
|
},
|
|
16
|
-
"
|
|
17
|
-
"
|
|
18
|
-
"
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
],
|
|
22
|
-
"dependencies": {}
|
|
23
|
-
}
|
|
12
|
+
"dependencies": {
|
|
13
|
+
"eventemitter3": "^4.0.0",
|
|
14
|
+
"@cowprotocol/types": "3.2.0"
|
|
15
|
+
}
|
|
16
|
+
}
|
package/project.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,192 @@
|
|
|
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
|
+
}
|