resolve rpc responses back inpage

This commit is contained in:
ivan grachev 2024-01-23 19:32:35 +03:00
parent 517adbeb82
commit 280590ad6f
6 changed files with 13 additions and 13 deletions

View File

@ -199,16 +199,9 @@ class TokenaryEthereum extends EventEmitter {
case "eth_newPendingTransactionFilter":
case "eth_uninstallFilter":
case "eth_subscribe":
throw new ProviderRpcError(4200, `Tokenary does not support calling ${payload.method}. Please use your own solution`);
throw new ProviderRpcError(4200, `tiny wallet does not support ${payload.method}`);
default:
this.callbacks.delete(payload.id);
this.wrapResults.delete(payload.id);
return this.rpc
.call(payload)
.then((response) => {
wrapResult ? resolve(response) : resolve(response.result);
})
.catch(reject);
return this.rpc.call(payload);
}
});
}
@ -394,13 +387,13 @@ class TokenaryEthereum extends EventEmitter {
let callback = this.callbacks.get(id);
let wrapResult = this.wrapResults.get(id);
let data = { jsonrpc: "2.0", id: originId };
if (result !== null && result.jsonrpc && result.result) {
if (result !== null && result.result) {
data.result = result.result;
} else {
data.result = result;
}
if (callback) {
wrapResult ? callback(null, data) : callback(null, result);
wrapResult ? callback(null, data) : callback(null, data.result);
this.callbacks.delete(id);
} else {
console.log(`callback id: ${id} not found`);

View File

@ -45,7 +45,10 @@ announceProvider();
window.addEventListener("message", function(event) {
if (event.source == window && event.data && event.data.direction == "rpc-back") {
console.log("got it back inpage", event.data); // TODO: respond via rpc server
// TODO: check event.data.response is there
// TODO: make sure error is delivered as well
console.log("rpc back", event.data.response);
provider.sendResponse(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;

View File

@ -10,6 +10,7 @@ class RPCServer {
}
call(payload) {
payload.jsonrpc = "2.0";
window.postMessage({direction: "rpc", message: {id: payload.id, subject: "rpc", chainId: this.chainId, body: JSON.stringify(payload)}}, "*");
return true;
}

File diff suppressed because one or more lines are too long

View File

@ -82,6 +82,7 @@ class SafariWebExtensionHandler: NSObject, NSExtensionRequestHandling {
let task = URLSession.shared.dataTask(with: request) { [weak self] data, response, error in
if let data = data, let json = try? JSONSerialization.jsonObject(with: data) as? [String: Any] {
self?.respond(with: json, context: context)
// TODO: id is missing when there is an error
} else {
// TODO: respond with error
self?.respond(with: ["yo": "body", "chainId": chainId, "result": "gg"], context: context)

View File

@ -8,5 +8,7 @@
<array>
<string>group.io.tokenary</string>
</array>
<key>com.apple.security.network.client</key>
<true/>
</dict>
</plist>