-
Notifications
You must be signed in to change notification settings - Fork 393
/
index.ts
91 lines (80 loc) · 3.13 KB
/
index.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
import browser from "webextension-polyfill"
import {
EXTERNAL_PORT_NAME,
PROVIDER_BRIDGE_TARGET,
WINDOW_PROVIDER_TARGET,
} from "@tallyho/provider-bridge-shared"
const windowOriginAtLoadTime = window.location.origin
const INJECTED_WINDOW_PROVIDER_SOURCE = "@@@WINDOW_PROVIDER@@@"
export function connectProviderBridge(): void {
const port = browser.runtime.connect({ name: EXTERNAL_PORT_NAME })
window.addEventListener("message", (event) => {
if (
event.origin === windowOriginAtLoadTime && // we want to recieve msgs only from the in-page script
event.source === window && // we want to recieve msgs only from the in-page script
event.data.target === PROVIDER_BRIDGE_TARGET
) {
// if dapp wants to connect let's grab its details
if (
event.data.request.method === "eth_requestAccounts" ||
event.data.request.method === "wallet_addEthereumChain"
) {
const faviconElements: NodeListOf<HTMLLinkElement> =
window.document.querySelectorAll("link[rel*='icon']")
const largestFavicon = [...faviconElements].sort((el) =>
parseInt(el.sizes?.toString().split("x")[0], 10)
)[0]
const faviconUrl = largestFavicon?.href ?? ""
const { title } = window.document ?? ""
event.data.request.params.push(title, faviconUrl)
}
// TODO: replace with better logging before v1. Now it's invaluable in debugging.
// eslint-disable-next-line no-console
console.log(
`%c content: inpage > background: ${JSON.stringify(event.data)}`,
"background: #bada55; color: #222"
)
port.postMessage(event.data)
}
})
port.onMessage.addListener((data) => {
// TODO: replace with better logging before v1. Now it's invaluable in debugging.
// eslint-disable-next-line no-console
console.log(
`%c content: background > inpage: ${JSON.stringify(data)}`,
"background: #222; color: #bada55"
)
window.postMessage(
{
...data,
target: WINDOW_PROVIDER_TARGET,
},
windowOriginAtLoadTime
)
})
// let's grab the internal config that also has chainId info
// we send the config on port initialization, but that needs to
// be as fast as possible, so we omit the chainId information
// from that payload to save the service call
port.postMessage({
request: { method: "tally_getConfig", origin: windowOriginAtLoadTime },
})
}
export function injectTallyWindowProvider(): void {
if (document.contentType !== "text/html") return
try {
const container = document.head || document.documentElement
const scriptTag = document.createElement("script")
// this makes the script loading blocking which is good for us
// bc we want to load before anybody has a chance to temper w/ the window obj
scriptTag.setAttribute("async", "false")
scriptTag.textContent = INJECTED_WINDOW_PROVIDER_SOURCE
container.insertBefore(scriptTag, container.children[0])
} catch (e) {
throw new Error(
`Taho: oh nos the content-script failed to initilaize the Taho window provider.
${e}
It's time for a seppuku...🗡`
)
}
}