mirror of
https://github.com/lil-org/wallet.git
synced 2025-01-06 04:13:11 +03:00
100 lines
3.7 KiB
JavaScript
100 lines
3.7 KiB
JavaScript
// ∅ 2024 lil org
|
|
// Rewrite of index.js from trust-web3-provider.
|
|
|
|
"use strict";
|
|
|
|
import TokenaryEthereum from "./ethereum";
|
|
import ProviderRpcError from "./error";
|
|
|
|
window.tokenary = {};
|
|
window.tokenary.postMessage = (name, id, body, provider) => {
|
|
const message = {name: name, id: id, provider: provider, body: body};
|
|
window.postMessage({direction: "from-page-script", message: message}, "*");
|
|
};
|
|
|
|
window.tokenary.disconnect = (provider) => {
|
|
const disconnectRequest = {subject: "disconnect", provider: provider};
|
|
window.postMessage(disconnectRequest, "*");
|
|
};
|
|
|
|
// - MARK: Ethereum
|
|
|
|
let provider = new TokenaryEthereum();
|
|
window.tokenary.eth = provider;
|
|
window.ethereum = provider;
|
|
window.web3 = {currentProvider: provider};
|
|
window.metamask = provider;
|
|
window.dispatchEvent(new Event('ethereum#initialized'));
|
|
|
|
// MARK: EIP-6963
|
|
|
|
function announceProvider() {
|
|
const info = {
|
|
uuid: "f9058af0-b501-43c4-bd7d-b43f83244681",
|
|
name: "tokenary",
|
|
icon: 'data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMTI4IiBoZWlnaHQ9IjEyOCIgdmlld0JveD0iMCAwIDEyOCAxMjgiIGZpbGw9Im5vbmUiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+CjxyZWN0IHdpZHRoPSIxMjgiIGhlaWdodD0iMTI4IiBmaWxsPSJ3aGl0ZSIvPgo8Y2lyY2xlIGN4PSI2NCIgY3k9IjY0IiByPSI0MC44NzUiIGZpbGw9IiMyQzdDRjUiLz4KPC9zdmc+Cg==',
|
|
rdns: "io.tokenary"
|
|
};
|
|
window.dispatchEvent(new CustomEvent("eip6963:announceProvider", { detail: Object.freeze({ info, provider }), }));
|
|
}
|
|
|
|
window.addEventListener("eip6963:requestProvider", function(event) { announceProvider(); });
|
|
announceProvider();
|
|
|
|
// - MARK: Process content script messages
|
|
|
|
window.addEventListener("message", function(event) {
|
|
if (event.source == window && event.data && event.data.direction == "rpc-back") {
|
|
provider.processTokenaryResponse(event.data.response.id, event.data.response);
|
|
} else if (event.source == window && event.data && event.data.direction == "from-content-script") {
|
|
const response = event.data.response;
|
|
const id = event.data.id;
|
|
|
|
if ("latestConfigurations" in response) {
|
|
const name = "didLoadLatestConfiguration";
|
|
var remainingProviders = new Set(["ethereum"]);
|
|
|
|
for(let configurationResponse of response.latestConfigurations) {
|
|
configurationResponse.name = name;
|
|
deliverResponseToSpecificProvider(id, configurationResponse, configurationResponse.provider);
|
|
remainingProviders.delete(configurationResponse.provider);
|
|
}
|
|
|
|
remainingProviders.forEach((provider) => {
|
|
deliverResponseToSpecificProvider(id, {name: "didLoadLatestConfiguration"}, provider);
|
|
});
|
|
} else {
|
|
deliverResponseToSpecificProvider(id, response, response.provider);
|
|
}
|
|
}
|
|
});
|
|
|
|
function deliverResponseToSpecificProvider(id, response, providerName) {
|
|
switch (providerName) {
|
|
case "ethereum":
|
|
provider.processTokenaryResponse(id, response);
|
|
break;
|
|
case "multiple":
|
|
response.bodies.forEach((body) => {
|
|
body.id = id;
|
|
body.name = response.name;
|
|
deliverResponseToSpecificProvider(id, body, body.provider);
|
|
});
|
|
|
|
response.providersToDisconnect.forEach((providerName) => {
|
|
switch (providerName) {
|
|
case "ethereum":
|
|
provider.externalDisconnect();
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
});
|
|
|
|
break;
|
|
default:
|
|
// pass unknown provider message to all providers
|
|
provider.processTokenaryResponse(id, response);
|
|
}
|
|
}
|