Skip to content

Commit

Permalink
Merge branch 'develop' into yushi/ergo-connector-action-map-typing
Browse files Browse the repository at this point in the history
  • Loading branch information
vsubhuman committed Jul 20, 2021
2 parents c64f11d + 538c5f6 commit 1c44694
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 12 deletions.
41 changes: 32 additions & 9 deletions packages/yoroi-ergo-connector/src/inject.js
Expand Up @@ -15,7 +15,7 @@ window.addEventListener("message", function(event) {
function ergo_request_read_access() {
return new Promise(function(resolve, reject) {
window.postMessage({
type: "connector_connect_request",
type: "connector_connect_request/ergo",
}, location.origin);
connectRequests.push({ resolve: resolve, reject: reject });
});
Expand All @@ -32,7 +32,7 @@ function ergo_check_read_access() {
function cardano_request_read_access() {
return new Promise(function(resolve, reject) {
window.postMessage({
type: "yoroi_connect_request/cardano",
type: "connector_connect_request/cardano",
}, location.origin);
connectRequests.push({ resolve: resolve, reject: reject });
});
Expand Down Expand Up @@ -236,10 +236,10 @@ function createYoroiPort() {
// events from Yoroi
yoroiPort = chrome.runtime.connect(extensionId);
yoroiPort.onMessage.addListener(message => {
alert("content script message: " + JSON.stringify(message));
if (message.type == "connector_rpc_response") {
// alert("content script message: " + JSON.stringify(message));
if (message.type === "connector_rpc_response") {
window.postMessage(message, location.origin);
} else if (message.type == "yoroi_connect_response") {
} else if (message.type === "yoroi_connect_response/ergo") {
if (message.success) {
if (!ergoApiInjected) {
// inject full API here
Expand All @@ -261,7 +261,7 @@ function createYoroiPort() {
type: "connector_connected",
success: message.success
}, location.origin);
} else if (message.type == "yoroi_connect_response/cardano") {
} else if (message.type === "yoroi_connect_response/cardano") {
if (message.success) {
if (!cardanoApiInjected) {
// inject full API here
Expand Down Expand Up @@ -328,8 +328,8 @@ if (shouldInject()) {
}
}, location.origin);
}
} else if (event.data.type == "connector_connect_request") {
if (fullApiInjected && yoroiPort) {
} else if (event.data.type === "connector_connect_request/ergo") {
if (ergoApiInjected && yoroiPort) {
// we can skip communication - API injected + hasn't been disconnected
window.postMessage({
type: "connector_connected",
Expand All @@ -346,7 +346,30 @@ if (shouldInject()) {
.then(imgBase64Url => {
yoroiPort.postMessage({
imgBase64Url,
type: "yoroi_connect_request",
type: "yoroi_connect_request/ergo",
url: location.hostname
});
});
}
} else if (event.data.type === "connector_connect_request/cardano") {
if (cardanoApiInjected && yoroiPort) {
// we can skip communication - API injected + hasn't been disconnected
window.postMessage({
type: "connector_connected",
success: true
}, location.origin);
} else {
if (yoroiPort == null) {
createYoroiPort();
}

// note: content scripts are subject to the same CORS policy as the website they are embedded in
// but since we are querying the website this script is injected into, it should be fine
convertImgToBase64(getFavicon(location.origin))
.then(imgBase64Url => {
yoroiPort.postMessage({
imgBase64Url,
type: "yoroi_connect_request/cardano",
url: location.hostname
});
});
Expand Down
Expand Up @@ -305,7 +305,7 @@ export default class WalletStore extends Store<StoresMap, ActionsMap> {
}
runInAction('refresh active wallet', () => {
if (this.selected == null && newWithCachedData.length === 1) {
this._setActiveWallet({
this.actions.wallets.setActiveWallet.trigger({
wallet: newWithCachedData[0],
});
}
Expand Down
4 changes: 2 additions & 2 deletions packages/yoroi-extension/chrome/extension/background.js
Expand Up @@ -610,13 +610,13 @@ chrome.runtime.onConnectExternal.addListener(port => {
});
}
}
if (message.type === 'yoroi_connect_request') {
if (message.type === 'yoroi_connect_request/ergo') {
await withDb(
async (_db, localStorageApi) => {
const publicDeriverId = await confirmConnect(tabId, message.url, localStorageApi);
const accepted = publicDeriverId !== null;
port.postMessage({
type: 'yoroi_connect_response',
type: 'yoroi_connect_response/ergo',
success: accepted
});
}
Expand Down

0 comments on commit 1c44694

Please sign in to comment.