lodewallet-xcode/static/js/injected.js

177 lines
32 KiB
JavaScript
Raw Normal View History

2023-01-19 19:08:25 +00:00
(async () => {
const lodePublicApi = {
apiVersion: "0.1.0",
name: "LodeWallet",
icon: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAYAAADDPmHLAAABfGlDQ1BpY2MAACiRfZE7SMNQFIb/PkSRSgcziDhEqE4WREUctQpFqBBqhVYdTG76giYNSYqLo+BacPCxWHVwcdbVwVUQBB8gjk5Oii5S4rlJoUWMBy7347/n/7n3XCDYqDDNCo8Dmm6b6WRCzOZWxe5XhBGFgACGZWYZc5KUgm993VMf1V2cZ/n3/Vl9at5iQEAknmWGaRNvEE9v2gbnfWKBlWSV+Jx4zKQLEj9yXfH4jXPR5SDPFMxMep5YIBaLHax0MCuZGvEUcUzVdMoPZj1WOW9x1io11ronf2Ekr68sc53WEJJYxBIkiFBQQxkV2IjTrpNiIU3nCR//oOuXyKWQqwxGjgVUoUF2/eB/8Hu2VmFywkuKJICuF8f5GAG6d4Fm3XG+jx2neQKEnoErve2vNoCZT9LrbS12BES3gYvrtqbsAZc7wMCTIZuyK4VoBQsF4P2MvikH9N8CvWve3FrnOH0AMjSr1A1wcAiMFil73efdPZ1z+7enNb8fJZ5yiHMphgoAAAAgY0hSTQAAeiYAAICEAAD6AAAAgOgAAHUwAADqYAAAOpgAABdwnLpRPAAAAAZiS0dEAAAAAAAA+UO7fwAAAAlwSFlzAAAuIwAALiMBeKU/dgAAAAd0SU1FB+YEDxQsNkyOETIAAAG/elRYdFJhdyBwcm9maWxlIHR5cGUgaWNjAAA4jaVTW44cIQz85xQ5gvGzOU4PNFLuf4EYDPPa2UibWEKIMpQLU6TftaZfI9QwwQg8QauSNiNgmpA2vYwNBdkYEeSQIicC2KWeZh8dII85O0FOmpWMDDgLCHCFf4juVVMwRjTCdlf2w0g/3N+UVYw0CmVcMEPyi4GhcSR0JUjNvEOw8XIEntn1Ht6OhZ+rFViTt3O2MRLtceAFv+odtyd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c64cEA9AA0Xn6zAkAGwEOd+CIAOQCuArgI4DKA61CJQ/L3xzcFrZUAggB0ATAYwDAA/e3/j8J/VnVzogpAHoBLAE7Y268ACv09MA1FayKAIAC9AYwGMAbA7VDZOufvjkEVHdcAHANwAEAaVAIJeAQ6ARCoK3scgIehrvg2/u5UPRABpANIAbAbwM9QCeS/aAAMAP4A4AOospYCYK2wVUAlhKcBtPP3oLpCoHEAE4C7AUwHcA9UBc5XYBzHMa1WC51OB41Gw6ASFgAQxhgnSRJkWYYsy9Xj4qvxoQAuAPgcwDYAGc08jl4jUAjAAFW2z4I68aZGfxAhNDw8nMXGxrIOHTrQjh07okOHDmjbti2JiYlBeHg4TCYTDAYD0Wg0CgBmn3zOZrOxyspKlJSU4ObNmywnJwdms5mYzWbk5uaSkpISThAE0sRxuwLgEwCfAcj298D7mwAIgKEA5gB4EEBIQ1/AcRyNi4ujvXv3ZoMGDWIDBgxAz549Sbt27biIiAhiMBiaMmFMURRYLBZaWFjIMjIy2Pnz59nJkyfZ2bNnuaysLM5isXCNfP+vAD4EsBVAaYuMtgv4kwA6AHgOwAyo2rzX0Ol0tFu3bsqIESPo6NGjyaBBg7j27dtzRqOxqavTKzDGWHFxMb18+TI9evQoPXjwIDl79qymuLi4ocSgADgM4B2ouoLS3H0PBOgBTAFwGg1TqGinTp2kp59+Wvj666+F/Px8iTFGWQDAZrMpZ8+elZYtW8YPHz5cMJlMSgO/rRzAvwB09vfkNDc6A1gL1SzyanD0er0ybNgwYdWqVfz169clSmlATLo7VFRUKCkpKcKMGTP4+Ph4CQ2zYM4CeBSqx/KWAgHwABqw6oOCgpTx48cLX375pVBeXi77e2IbCkop/fXXX8WFCxfynTt3FhtACBVQRUKMvyfNVwgBsBBq
enable: async function enable() {
const isEnabled = await __postMessage(["LodePublic_Enable", "stub"]);
if (isEnabled) {
return lodePrivateApi;
}
},
isEnabled: async function isEnabled() {
return await __postMessage(["LodePublic_IsEnabled", "stub"]);
},
};
const lodePrivateApi = {
getUsedAddresses: async function getUsedAddresses(paginate = null) {
return await __postMessage(["LodePublic_GetUsedAddressesCBOR", paginate]);
},
getBalance: async function getBalance() {
return await __postMessage(["LodePublic_GetBalanceCBOR", []]);
},
getNetworkId: async function getNetworkId() {
return await __postMessage(["LodePublic_GetNetworkId", []]);
},
getUtxos: async function getUtxos(amount = null, paginate = null) {
return await __postMessage(["LodePublic_GetUTxOCBOR", [amount, paginate]]);
},
signTx: async function signTx(tx, ps) {
return await __postMessage(["LodePublic_SignTx", ["stub", tx]]);
},
submitTx: async function submitTx(tx) {
return await __postMessage(["LodePublic_SubmitTx", tx]);
},
getUnusedAddresses: async () => [], /* No unused addresses in single address */
getChangeAddress: async () => {
return await __postMessage(["LodePublic_GetChangeAddressCBOR", []]);
},
getRewardAddresses: async () => {
return [await __postMessage(["LodePublic_GetRewardAddressCBOR", []])];
},
signData: async function signData(address, payload) {
return await __postMessage(["LodePublic_SignData", ["stub", address, payload]]);
},
/* TODO: remove this; here for backwards compat */
getRewardAddress: async () => {
return await __postMessage(["LodePublic_GetRewardAddressCBOR", []]);
},
/* TODO: remove this; here for backwards compat */
getCollateral: async function getCollateral() {
return await __postMessage(["LodePublic_GetCollateralCBOR", []]);
},
/* TODO: remove this; here for backwards compat */
onAccountChange: (cb) => {
console.log("onAccountChange: NOT IMPLEMENTED")
return { remove: () => console.log("REMOVE ACCOUNT CHANGE") }
},
/* TODO: remove this; here for backwards compat */
onNetworkChange: (cb) => {
console.log("onNetworkChange: NOT IMPLEMENTED")
return { remove: () => console.log("REMOVE NETWORK CHANGE") }
},
experimental: {
getCollateral: async function getCollateral() {
return await __postMessage(["LodePublic_GetCollateralCBOR", []]);
},
on: (eventName, cb) => {
console.log(".on not supported")
},
off: (eventName, cb) => {
console.log(".off not supported")
}
}
};
const VERBOSE = false;
function __log(msg) {
if (VERBOSE) console.log(msg)
};
function __notImplemented(name) {
console.log("============== NOT IMPLEMENTED =====================" + name);
return new Promise((res, rej) => rej("NOT IMPLEMENTED: "+name))
}
function __postMessage(msg) {
return new Promise((res, rej) => {
const requestId = Math.random().toString(36).substr(2, 9);
window.addEventListener('message', function responseHandler(e) {
__log("INJECTED RECEIVE MESSAGE: " + (e.data || ""));
__log(e);
const response = e.data;
if (
typeof response === 'undefined' ||
response === null ||
response.id === null ||
response.id !== requestId ||
response._sourceIsExtension !== true
)
return;
__log("INJECTED REMOVE MSG LISTENER");
window.removeEventListener('message', responseHandler);
if (response.error) rej(response.error);
else res(response.response);
});
__log("INJECTED POST MESSAGE: " + (msg || ""));
if (typeof AndroidBridge !== 'undefined') {
AndroidBridge.sendDomMessage(JSON.stringify({ id: requestId, target: 'lode', data: msg }));
} else if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.iOSBridge) { /* iOS */
console.log("WEBKIT BROWSER FOUND");
window.webkit.messageHandlers.iOSBridge.postMessage(JSON.stringify({ id: requestId, target: 'lode', data: msg }));
} else {
window.postMessage({ id: requestId, target: 'lode', data: msg });
}
});
}
// Some messages are sent by the extension without a prompt
// We listen for those here. Currently only used to check
// the wallet identity again.
window.addEventListener('message', async e => {
__log("PROMPT RECEIVE MESSAGE: " + (e.data || ""));
__log(e);
const response = e.data;
if (
typeof response === 'undefined' ||
response === null ||
response.id !== null ||
response.prompt === null ||
response.error !== null ||
response._sourceIsExtension !== true
) return;
await setWalletIdentity();
});
const namiBackwardsCompat = Object.assign({}, lodePublicApi, lodePrivateApi);
const setWalletIdentity = async () => {
const rsp = await __postMessage(["LodePublic_GetWalletIdentity", []]);
__log(rsp)
switch(rsp) {
case 'AccountWalletIdentity_LodeWallet':
window.cardano = Object.assign(
{},
window.cardano,
{ LodeWallet: lodePublicApi }
);
break;
case 'AccountWalletIdentity_Nami':
if(window.cardano) delete window.cardano.LodeWallet;
window.cardano = Object.assign(
{},
window.cardano,
namiBackwardsCompat,
{ nami: lodePublicApi }
);
break;
default:
__log("UNKNOWN WALLET IDENTITY RESPONSE");
window.cardano = Object.assign(
{},
window.cardano,
{ LodeWallet: lodePublicApi }
);
break;
}
}
await setWalletIdentity()
})();