Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cardano RPC #2224

Merged
merged 3 commits into from
Jul 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 41 additions & 8 deletions packages/yoroi-ergo-connector/src/inject.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// sets up RPC communication with the connector + access check/request functions
const initialInject = `
var ergoConnectRequests = [];
var connectRequests = [];

window.addEventListener("message", function(event) {
if (event.data.type == "connector_connected") {
if (event.data.err !== undefined) {
ergoConnectRequests.forEach(promise => promise.reject(event.data.err));
connectRequests.forEach(promise => promise.reject(event.data.err));
} else {
ergoConnectRequests.forEach(promise => promise.resolve(event.data.success));
connectRequests.forEach(promise => promise.resolve(event.data.success));
}
}
});
Expand All @@ -17,7 +17,7 @@ function ergo_request_read_access() {
window.postMessage({
type: "connector_connect_request",
}, location.origin);
ergoConnectRequests.push({ resolve: resolve, reject: reject });
connectRequests.push({ resolve: resolve, reject: reject });
});
}

Expand All @@ -34,14 +34,14 @@ function cardano_request_read_access() {
window.postMessage({
type: "yoroi_connect_request/cardano",
}, location.origin);
ergoConnectRequests.push({ resolve: resolve, reject: reject });
connectRequests.push({ resolve: resolve, reject: reject });
});
}

// @todo
function cardano_check_read_access() {
if (typeof cardano !== "undefined") {
return 'Under development'
return cardano._cardano_rpc_call("ping", []);
} else {
return Promise.resolve(false);
}
Expand All @@ -55,7 +55,7 @@ var ergoRpcUid = 0;
var ergoRpcResolver = new Map();

window.addEventListener("message", function(event) {
if (event.data.type == "connector_rpc_response") {
if (event.data.type == "connector_rpc_response" && event.data.protocol === "ergo") {
console.log("page received from connector: " + JSON.stringify(event.data) + " with source = " + event.source + " and origin = " + event.origin);
const rpcPromise = ergoRpcResolver.get(event.data.uid);
if (rpcPromise !== undefined) {
Expand Down Expand Up @@ -113,6 +113,7 @@ class ErgoAPI {
return new Promise(function(resolve, reject) {
window.postMessage({
type: "connector_rpc_request",
protocol: "ergo",
uid: ergoRpcUid,
function: func,
params: params
Expand All @@ -129,7 +130,24 @@ const ergo = Object.freeze(new ErgoAPI());

const cardanoApiInject = `

// @todo add cardano rpc resolver when it is done
// RPC setup
var cardanoRpcUid = 0;
var cardanoRpcResolver = new Map();

window.addEventListener("message", function(event) {
if (event.data.type == "connector_rpc_response" && event.data.protocol === "cardano") {
console.log("page received from connector: " + JSON.stringify(event.data) + " with source = " + event.source + " and origin = " + event.origin);
const rpcPromise = cardanoRpcResolver.get(event.data.uid);
if (rpcPromise !== undefined) {
const ret = event.data.return;
if (ret.err !== undefined) {
rpcPromise.reject(ret.err);
} else {
rpcPromise.resolve(ret.ok);
}
}
}
});

class CardanoAPI {
constructor(){
Expand All @@ -139,6 +157,21 @@ class CardanoAPI {
getInitTimestamp(){
return this.initTimestamp
}

_cardano_rpc_call(func, params) {
return new Promise(function(resolve, reject) {
window.postMessage({
type: "connector_rpc_request",
protocol: "cardano",
uid: cardanoRpcUid,
function: func,
params: params
}, location.origin);
console.log("cardanoRpcUid = " + cardanoRpcUid);
cardanoRpcResolver.set(cardanoRpcUid, { resolve: resolve, reject: reject });
cardanoRpcUid += 1;
});
}
}

const cardano = Object.freeze(new CardanoAPI())
Expand Down
1 change: 1 addition & 0 deletions packages/yoroi-extension/chrome/extension/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,7 @@ chrome.runtime.onConnectExternal.addListener(port => {
function rpcResponse(response) {
port.postMessage({
type: 'connector_rpc_response',
protocol: message.protocol,
uid: message.uid,
return: response
});
Expand Down