-
Notifications
You must be signed in to change notification settings - Fork 3
/
injected-penumbra-global.ts
171 lines (146 loc) · 5.72 KB
/
injected-penumbra-global.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/**
* This file is injected by the extension as a content script, to create the
* mainworld global that allows pages to detect installed providers and connect
* to them.
*
* The global is identified by `Symbol.for('penumbra')` and consists of a record
* with string keys referring to `PenumbraProvider` objects that contain a
* simple API. The identifiers on this record should be unique, and correspond
* to a browser extension id. Providers should provide a link to their extension
* manifest in their record entry.
*
* The global is frozen to discourage mutation, but you should consider that the
* global and everything on it is only as trustable as the scripts running on
* the page. Imports, requires, includes, script tags, packages your webapp
* depends on, userscripts, or other extensions' content scripts could all
* mutate or preempt this, and all have the power to interfere or intercept
* connections.
*/
import '@penumbra-zone/client/global';
import { createPenumbraStateEvent, type PenumbraProvider } from '@penumbra-zone/client';
import { PenumbraState } from '@penumbra-zone/client/state';
import { PenumbraSymbol } from '@penumbra-zone/client/symbol';
import { PraxConnection } from '../message/prax';
import {
isPraxEndMessageEvent,
isPraxFailureMessageEvent,
isPraxPortMessageEvent,
PraxMessage,
unwrapPraxMessageEvent,
} from './message-event';
const connectMessage = {
[PRAX]: PraxConnection.Connect,
} satisfies PraxMessage<PraxConnection.Connect>;
const disconnectMessage = {
[PRAX]: PraxConnection.Disconnect,
} satisfies PraxMessage<PraxConnection.Disconnect>;
const initMessage = {
[PRAX]: PraxConnection.Init,
} satisfies PraxMessage<PraxConnection.Init>;
class PraxInjection {
private static singleton?: PraxInjection = new PraxInjection();
public static get penumbra() {
return new PraxInjection().injection;
}
private port?: MessagePort;
private presentState: PenumbraState = PenumbraState.Disconnected;
private manifestUrl = `${PRAX_ORIGIN}/manifest.json`;
private stateEvents = new EventTarget();
private injection: Readonly<PenumbraProvider> = Object.freeze({
/**
* Meet the 'request' method of the old page API to mitigate incompatibility
* with pd v0.80.0's bundled minifront. This prevents connection failure.
* @todo Remove when bundled frontends are updated beyond `a31d54a`
* @issue https://github.com/prax-wallet/web/issues/175
*/
request: async () => {
await Promise.resolve(this.port ?? this.postConnectRequest());
},
connect: () => Promise.resolve(this.port ?? this.postConnectRequest()),
disconnect: () => this.postDisconnectRequest(),
isConnected: () => Boolean(this.port && this.presentState === PenumbraState.Connected),
state: () => this.presentState,
manifest: String(this.manifestUrl),
addEventListener: this.stateEvents.addEventListener.bind(this.stateEvents),
removeEventListener: this.stateEvents.removeEventListener.bind(this.stateEvents),
});
private constructor() {
if (PraxInjection.singleton) {
return PraxInjection.singleton;
}
void this.listenPortMessage();
window.postMessage(initMessage, '/');
}
private setConnected(port: MessagePort) {
this.port = port;
this.presentState = PenumbraState.Connected;
this.stateEvents.dispatchEvent(createPenumbraStateEvent(PRAX_ORIGIN, this.presentState));
}
private setDisconnected() {
this.port = undefined;
this.presentState = PenumbraState.Disconnected;
this.stateEvents.dispatchEvent(createPenumbraStateEvent(PRAX_ORIGIN, this.presentState));
}
private setPending() {
this.port = undefined;
this.presentState = PenumbraState.Pending;
this.stateEvents.dispatchEvent(createPenumbraStateEvent(PRAX_ORIGIN, this.presentState));
}
private postConnectRequest() {
const attempt = this.listenPortMessage();
window.postMessage(connectMessage, '/', []);
return attempt;
}
private listenPortMessage() {
this.setPending();
const connection = Promise.withResolvers<MessagePort>();
const listener = (msg: MessageEvent<unknown>) => {
if (msg.origin === window.origin) {
if (isPraxPortMessageEvent(msg)) {
connection.resolve(unwrapPraxMessageEvent(msg));
} else if (isPraxFailureMessageEvent(msg)) {
connection.reject(
new Error('Connection request failed', { cause: unwrapPraxMessageEvent(msg) }),
);
}
}
};
void connection.promise
.then(port => this.setConnected(port))
.catch(() => this.setDisconnected())
.finally(() => window.removeEventListener('message', listener));
window.addEventListener('message', listener);
return connection.promise;
}
private postDisconnectRequest() {
const disconnection = Promise.withResolvers<void>();
const listener = (msg: MessageEvent<unknown>) => {
if (msg.origin === window.origin) {
if (isPraxEndMessageEvent(msg)) {
disconnection.resolve();
} else if (isPraxFailureMessageEvent(msg)) {
disconnection.reject(
new Error('Disconnect request failed', { cause: unwrapPraxMessageEvent(msg) }),
);
}
}
};
this.setDisconnected();
void disconnection.promise.finally(() => window.removeEventListener('message', listener));
window.addEventListener('message', listener);
window.postMessage(disconnectMessage, '/');
return disconnection.promise;
}
}
// inject prax
Object.defineProperty(
window[PenumbraSymbol] ??
// create the global if not present
Object.defineProperty(window, PenumbraSymbol, { value: {}, writable: false })[PenumbraSymbol],
PRAX_ORIGIN,
{
value: PraxInjection.penumbra,
writable: false,
enumerable: true,
},
);