Skip to content

Proposal: Internal use of RPC API#6356

Draft
kyranjamie wants to merge 2 commits intodevfrom
refactor/decouple-port
Draft

Proposal: Internal use of RPC API#6356
kyranjamie wants to merge 2 commits intodevfrom
refactor/decouple-port

Conversation

@kyranjamie
Copy link
Copy Markdown
Contributor

@kyranjamie kyranjamie commented Sep 3, 2025

Try out Leather build c77bc37Extension build, Test report, Storybook, Chromatic

This PR proposes that we utilise our RPC API internally. Rather than building some flows internally (send form signing step, sending inscriptions etc) we run actions exclusively via our API.

Demo

2025-09-05-000510.mp4
2025-09-05-000512.mp4

https://trustmachines.slack.com/archives/C05LRS7G44R/p1757059108955959

Motivation
Leather currently has a several ways of doing the same thing. Consider signing a tx. This is currently done in three places: legacy JWT flows, RPC API, and actually in the extension (send form). This is very costly to maintain and just not a particularly efficient way of building a wallet. We need to move quickly, and this slows us down.

Using the RPC API exclusively has several benefits. First, we'll be more efficient as we build one flow flawlessly and reuse it everywhere. Second, as we build our features for ourselves, we can also expose them as an API. Consider inscription sends. This can be done in the wallet, but there's no API for it. If we'd built this as an API from the start, we could've exposed this as an API for apps to use as well, slowly growing a more compelling suite of tools for builders.

Some changes to the message passing are made here. This is the existing behaviour: we pass the RPC events around, then the popup returns it directly to the tab.

Existing flow

image
Mermaid
sequenceDiagram
    participant App
    participant ContentScript
    participant Background
    participant Popup

    App->>ContentScript: new CustomEvent()
    ContentScript->>Background: chrome.runtime.Port
    Background->>Popup: chrome.window.open()
    Popup->>ContentScript: chome.tab.sendMessage()
    ContentScript->>App: window.postMessage()
Loading

Here, this changes such that the RPC result is returned via the background script.

Proposed flow

image
Mermaid
sequenceDiagram
    participant App
    participant ContentScript
    participant Background
    participant Popup

    App->>ContentScript: new CustomEvent()
    ContentScript->>Background: chrome.runtime.Port
    Background->>Popup: chrome.window.open()
    Popup->>Background: chome.runtime.sendMessage()
    Background->>ContentScript: chome.tab.sendMessage()
    ContentScript->>App: window.postMessage()
Loading

The reason for this change is that, if the extension can also trigger the RPC APIs, then it isn't necessarily a tab we need to send the result to. It could be the action popup that has no tabId. I've baked in some conditional logic that changes the transport depending if the origin is the extension. I'm not 100% on this change, as it does add an additional dependency on the background script, that has revealed some race condition behaviours (e.g. the closed window listener returns an error event as it's detected before the success result). Either these need to be fixed, or the conditional logic should happen in the popup.

@kyranjamie kyranjamie force-pushed the refactor/decouple-port branch from 39fa541 to e53e70f Compare September 3, 2025 12:24
@kyranjamie kyranjamie force-pushed the refactor/decouple-port branch 6 times, most recently from 3111f6d to 1ab220f Compare September 3, 2025 18:42
@kyranjamie kyranjamie force-pushed the refactor/decouple-port branch from 1ab220f to c77bc37 Compare September 3, 2025 18:51
@kyranjamie kyranjamie changed the title refactor: decouple port from rpc flow Proposal: Internal use of RPC API for Leather Sep 5, 2025
@kyranjamie kyranjamie changed the title Proposal: Internal use of RPC API for Leather Proposal: Internal use of RPC API Sep 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant