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

New Solana Provider using secure-background/client for web & xnfts #4213

Merged
merged 53 commits into from
Jun 23, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
01e8ca2
add madlads.com to wl
ph101pp May 29, 2023
ad21075
restructure secure-background
ph101pp May 31, 2023
de7d81a
Fully typed Client-Server requests
ph101pp Jun 1, 2023
6031406
setup secure background channels
ph101pp Jun 2, 2023
3feef39
End to end working message signing through new client and secure-back…
ph101pp Jun 2, 2023
e7bf3f4
Working Content->background->UI->background->Content message passing
ph101pp Jun 3, 2023
3f66501
Merge branch 'master' into philipp/secure-background-core
ph101pp Jun 3, 2023
628783f
Split secure-background & secure-client
ph101pp Jun 5, 2023
43e5777
secure-background/clients exports
ph101pp Jun 5, 2023
f3a2067
Merge branch 'master' into philipp/secure-background-core
ph101pp Jun 12, 2023
b39e392
Clarify Transport From/To naming + correctly close Popup after openin…
ph101pp Jun 12, 2023
9db8ad6
Add ExtensionTransports and solidify Error handling
ph101pp Jun 12, 2023
52bc4e9
UnlockKeyring event + new extension message passing
ph101pp Jun 13, 2023
58f0627
Combine SecureUI and SecureBackground events + add transports to recoil
ph101pp Jun 14, 2023
130e5e0
fix build:fresh
ph101pp Jun 14, 2023
012469b
Merge branch 'master' into philipp/secure-background-core
ph101pp Jun 14, 2023
2c3899b
Add secure-client to dockerfiles
ph101pp Jun 14, 2023
c568de2
Add request.origin
ph101pp Jun 14, 2023
fba97dc
Add tamagui to secure-client & fix circular dependencies
ph101pp Jun 14, 2023
1211ec8
fix devmode transport issues
ph101pp Jun 14, 2023
020b803
Working confirmation prompt
ph101pp Jun 14, 2023
c454070
Merge branch 'master' into philipp/secure-background-core
ph101pp Jun 14, 2023
0fa5d19
Create RequestResponder
ph101pp Jun 14, 2023
ceb43e6
e2e solana signMessage w/o unlock & approve origin
ph101pp Jun 15, 2023
6c8dd99
SecureStore refactor
ph101pp Jun 15, 2023
1cfaae7
Merge branch 'master' into philipp/secure-background-core
ph101pp Jun 15, 2023
667e604
Remove secure-client import from solana provider for merge
ph101pp Jun 15, 2023
f583a4b
Lock down deprecated and private methods in SecureStore
ph101pp Jun 15, 2023
a3a1828
Clean up KeyringStoreState enum and move it to secure-background/types
ph101pp Jun 16, 2023
da8c40d
Merge branch 'master' into philipp/secure-background-core-rebased
ph101pp Jun 16, 2023
f2eb471
Lock down deprecated and private methods in SecureStore
ph101pp Jun 15, 2023
74a62ac
Clean up KeyringStoreState enum and move it to secure-background/types
ph101pp Jun 16, 2023
814cd95
Fetch user data from secureUI
ph101pp Jun 16, 2023
7ee9262
move origin from clients to transport.
ph101pp Jun 16, 2023
03f078a
Setup SecureUI request subscription & queue
ph101pp Jun 16, 2023
6d4757f
Fix import issue.
ph101pp Jun 18, 2023
3743f79
cleanup
ph101pp Jun 18, 2023
9c0494f
animation improvement
ph101pp Jun 19, 2023
470cbb3
Sign Message request with forced unlock
ph101pp Jun 20, 2023
4ccf122
Solana provider connect
ph101pp Jun 21, 2023
d9846b5
Approve Origin Request
ph101pp Jun 21, 2023
971eaa5
Add solana disconnect
ph101pp Jun 21, 2023
3b6e71e
solana send & sendAndConfirm transaction
ph101pp Jun 22, 2023
e5a9bf7
Fix xnft connection -> use "enhanced" background connection
ph101pp Jun 22, 2023
c7400a2
Emit keyring unlocked event
ph101pp Jun 22, 2023
fa3febb
Use default Connection for backend/solana-connect and properly reset …
ph101pp Jun 22, 2023
fcbce99
Solana SignAllTransactions
ph101pp Jun 22, 2023
ad3b594
new NotificationsClient
ph101pp Jun 22, 2023
fbf638e
Cleanup and logging
ph101pp Jun 22, 2023
5654918
Merge branch 'master' into philipp/secure-background-ui
ph101pp Jun 22, 2023
339afe1
separate extension & secureUI origins
ph101pp Jun 22, 2023
a88f75d
Cleanup + disable new provider
ph101pp Jun 22, 2023
c055135
remove console.log
ph101pp Jun 22, 2023
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
Prev Previous commit
Next Next commit
Working Content->background->UI->background->Content message passing
  • Loading branch information
ph101pp committed Jun 3, 2023
commit e7bf3f4dbb1cc109c3d2d45e5188285cf77a6033
4 changes: 2 additions & 2 deletions packages/app-extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
"@cardinal/token-manager": "^1.7.9",
"@coral-xyz/background": "*",
"@coral-xyz/chat-sdk": "*",
"@coral-xyz/provider-core": "*",
"@coral-xyz/provider-injection": "*",
"@coral-xyz/chat-xplat": "*",
"@coral-xyz/common": "*",
"@coral-xyz/db": "*",
"@coral-xyz/message-sdk": "*",
"@coral-xyz/provider-core": "*",
"@coral-xyz/provider-injection": "*",
"@coral-xyz/react-common": "*",
"@coral-xyz/recoil": "*",
"@coral-xyz/secure-background": "*",
Expand Down
21 changes: 19 additions & 2 deletions packages/app-extension/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ import {
isValidEventOrigin,
openPopupWindow,
} from "@coral-xyz/common";
import {
SecureUITransportReceiver,
SecureUITransportSender,
} from "@coral-xyz/secure-background/src/clients";
import type { SECURE_UI_EVENTS } from "@coral-xyz/secure-background/src/services/secureUI/events";
import { v4 } from "uuid";

import "./index.css";

Expand All @@ -25,9 +31,17 @@ chrome.runtime
})
.catch(console.error);

// Connect to the background script so it can detect if the popup is closed
chrome.runtime.connect();
const secureUITransportReceiver =
new SecureUITransportReceiver<SECURE_UI_EVENTS>();

secureUITransportReceiver.setHandler(async (request) => {
await new Promise((resolve) => setTimeout(resolve, 1000));
return {
...request,
request: undefined,
response: { approved: true },
};
});
//
// Configure event listeners.
//
Expand Down Expand Up @@ -56,6 +70,9 @@ root.render(
<Suspense fallback={null}>
<App />
</Suspense>
<Suspense fallback={null}>
{/* <SecureUI receiver={secureUITransportReceiver} /> */}
</Suspense>
<Suspense fallback={null}>
<LedgerIframe />
</Suspense>
Expand Down
17 changes: 11 additions & 6 deletions packages/background/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { IS_MOBILE } from "@coral-xyz/common";
import {
KeyringStore,
mockTransportClient,
mockTransportSender,
startSecureService,
} from "@coral-xyz/secure-background";
import { ContentScriptTransportServer } from "@coral-xyz/secure-background/src/clients";
import {
ContentScriptTransportReceiver,
SecureUITransportReceiver,
SecureUITransportSender,
} from "@coral-xyz/secure-background/src/clients";
import { EventEmitter } from "eventemitter3";

import * as coreBackend from "./backend/core";
Expand Down Expand Up @@ -38,14 +42,15 @@ export function start(cfg: Config): Background {
const _ethereumConnection = ethereumConnection.start(cfg, events, ethereumB);

if (!cfg.isMobile) {
const contentScriptServer = new ContentScriptTransportServer();
const contentScriptReceiver = new ContentScriptTransportReceiver();
const secureUISender = new SecureUITransportSender();

// New secure service
startSecureService(
{
backendNotificationClient: mockTransportClient,
secureUIClient: mockTransportClient,
secureServer: contentScriptServer,
backendNotificationClient: mockTransportSender,
secureUIClient: secureUISender,
secureServer: contentScriptReceiver,
secureStorage: LocalStorageDb,
},
keyringStore
Expand Down
1 change: 1 addition & 0 deletions packages/common/src/channel/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./app-ui";
export * from "./content-script";
export * from "./injected-request-manager";
export * from "./plugin";
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
import type {
Event,
ResponseHandler,
RpcRequest,
RpcResponse,
} from "@coral-xyz/common";
import { getLogger } from "@coral-xyz/common";
import type { Event, ResponseHandler, RpcRequest, RpcResponse } from "../";
import { getLogger, isValidEventOrigin } from "../";

import { isValidEventOrigin } from ".";

const logger = getLogger("common/request-manager");

export class RequestManager {
export class InjectedRequestManager {
private _responseResolvers: { [requestId: number]: ResponseHandler } = {};
private _requestId = 0;
private _requestChannel: string;
private _responseChannel: string;
private _parent?: boolean;
private _url?: string;
private _logger: any;

constructor(
requestChannel: string,
Expand All @@ -28,6 +20,8 @@ export class RequestManager {
this._requestId = 0;
this._responseResolvers = {};
this._parent = parent;
this._logger = getLogger("common/request-manager");

if (parent) {
this._url = window.location.href;
}
Expand All @@ -45,7 +39,7 @@ export class RequestManager {
const { id, result, error } = event.data.detail;
const resolver = this._responseResolvers[id];
if (!resolver) {
logger.error("unexpected event", event);
this._logger.error("unexpected event", event);
throw new Error("unexpected event");
}
delete this._responseResolvers[id];
Expand Down
2 changes: 2 additions & 0 deletions packages/common/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export const CHANNEL_SOLANA_RPC_RESPONSE = "channel-solana-rpc-response";
export const CHANNEL_SECURE_BACKGROUND_REQUEST = "channel-secure-event-request";
export const CHANNEL_SECURE_BACKGROUND_RESPONSE =
"channel-secure-event-response";
export const CHANNEL_SECURE_UI_REQUEST = "channel-secure-ui-request";
export const CHANNEL_SECURE_UI_RESPONSE = "channel-secure-ui-response";
export const CHANNEL_SOLANA_NOTIFICATION = "channel-solana-notification";
export const CHANNEL_SOLANA_CONNECTION_RPC_UI =
"channel-solana-connection-rpc-ui";
Expand Down
10 changes: 5 additions & 5 deletions packages/provider-core/src/common/ethereum.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { InjectedRequestManager } from "@coral-xyz/common";
import {
ETHEREUM_RPC_METHOD_SIGN_AND_SEND_TX,
ETHEREUM_RPC_METHOD_SIGN_MESSAGE,
Expand All @@ -8,13 +9,12 @@ import type { UnsignedTransaction } from "@ethersproject/transactions";
import { ethers } from "ethers";

import type { ChainedRequestManager } from "../chained-request-manager";
import type { RequestManager } from "../request-manager";

const { base58: bs58 } = ethers.utils;

export async function signTransaction(
publicKey: string,
requestManager: RequestManager | ChainedRequestManager,
requestManager: InjectedRequestManager | ChainedRequestManager,
transaction: UnsignedTransaction
): Promise<TransactionRequest> {
const serializedTx = encodeTransaction(transaction);
Expand All @@ -26,7 +26,7 @@ export async function signTransaction(

export async function sendTransaction(
publicKey: string,
requestManager: RequestManager | ChainedRequestManager,
requestManager: InjectedRequestManager | ChainedRequestManager,
transaction: UnsignedTransaction
): Promise<any> {
const serializedTx = encodeTransaction(transaction);
Expand All @@ -38,7 +38,7 @@ export async function sendTransaction(

export async function sendAndConfirmTransaction(
publicKey: string,
requestManager: RequestManager | ChainedRequestManager,
requestManager: InjectedRequestManager | ChainedRequestManager,
transaction: UnsignedTransaction
): Promise<any> {
const signature = this.sendTransaction(
Expand All @@ -52,7 +52,7 @@ export async function sendAndConfirmTransaction(

export async function signMessage(
publicKey: string,
requestManager: RequestManager | ChainedRequestManager,
requestManager: InjectedRequestManager | ChainedRequestManager,
msg: string
): Promise<Uint8Array> {
const encodedMsg = ethers.utils.base58.encode(ethers.utils.toUtf8Bytes(msg));
Expand Down
14 changes: 7 additions & 7 deletions packages/provider-core/src/common/solana.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { InjectedRequestManager } from "@coral-xyz/common";
import {
isVersionedTransaction,
SOLANA_RPC_METHOD_SIGN_ALL_TXS,
Expand All @@ -22,13 +23,12 @@ import type {
import { decode, encode } from "bs58";

import type { ChainedRequestManager } from "../chained-request-manager";
import type { RequestManager } from "../request-manager";

export async function sendAndConfirm<
T extends Transaction | VersionedTransaction
>(
publicKey: PublicKey,
requestManager: RequestManager | ChainedRequestManager,
requestManager: InjectedRequestManager | ChainedRequestManager,
connection: Connection,
tx: T,
signers?: Signer[],
Expand All @@ -48,7 +48,7 @@ export async function sendAndConfirm<

export async function send<T extends Transaction | VersionedTransaction>(
publicKey: PublicKey,
requestManager: RequestManager | ChainedRequestManager,
requestManager: InjectedRequestManager | ChainedRequestManager,
connection: Connection,
tx: T,
signers?: Signer[],
Expand Down Expand Up @@ -90,7 +90,7 @@ export async function signTransaction<
T extends Transaction | VersionedTransaction
>(
publicKey: PublicKey,
requestManager: RequestManager | ChainedRequestManager,
requestManager: InjectedRequestManager | ChainedRequestManager,
connection: Connection,
tx: T
): Promise<T> {
Expand Down Expand Up @@ -118,7 +118,7 @@ export async function signAllTransactions<
T extends Transaction | VersionedTransaction
>(
publicKey: PublicKey,
requestManager: RequestManager | ChainedRequestManager,
requestManager: InjectedRequestManager | ChainedRequestManager,
connection: Connection,
txs: Array<T>
): Promise<Array<T>> {
Expand Down Expand Up @@ -163,7 +163,7 @@ export async function signAllTransactions<

export async function simulate<T extends Transaction | VersionedTransaction>(
publicKey: PublicKey,
requestManager: RequestManager | ChainedRequestManager,
requestManager: InjectedRequestManager | ChainedRequestManager,
connection: Connection,
tx: T,
signers?: Signer[],
Expand Down Expand Up @@ -201,7 +201,7 @@ export async function simulate<T extends Transaction | VersionedTransaction>(

export async function signMessage(
publicKey: PublicKey,
requestManager: RequestManager | ChainedRequestManager,
requestManager: InjectedRequestManager | ChainedRequestManager,
msg: Uint8Array
): Promise<Uint8Array> {
const msgStr = encode(msg);
Expand Down
1 change: 0 additions & 1 deletion packages/provider-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@ export { ChainedRequestManager } from "./chained-request-manager";
export { ProviderEthereumInjection } from "./provider-ethereum";
export { ProviderEthereumXnftInjection } from "./provider-ethereum-xnft";
export { ProviderSolanaInjection } from "./provider-solana";
export { RequestManager } from "./request-manager";
export { ProviderRootXnftInjection } from "./root-provider-xnft";
export { isValidEventOrigin } from "@coral-xyz/common";
6 changes: 3 additions & 3 deletions packages/provider-core/src/provider-ethereum-xnft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
CHANNEL_ETHEREUM_CONNECTION_INJECTED_RESPONSE,
CHANNEL_PLUGIN_NOTIFICATION,
getLogger,
InjectedRequestManager,
PLUGIN_NOTIFICATION_CONNECT,
PLUGIN_NOTIFICATION_ETHEREUM_CONNECTION_URL_UPDATED,
PLUGIN_NOTIFICATION_ETHEREUM_PUBLIC_KEY_UPDATED,
Expand All @@ -16,7 +17,6 @@ import type { ethers } from "ethers";
import * as cmn from "./common/ethereum";
import { PrivateEventEmitter } from "./common/PrivateEventEmitter";
import type { ChainedRequestManager } from "./chained-request-manager";
import { RequestManager } from "./request-manager";
import { isValidEventOrigin } from ".";

const logger = getLogger("provider-xnft-injection");
Expand All @@ -26,7 +26,7 @@ const logger = getLogger("provider-xnft-injection");
//
export class ProviderEthereumXnftInjection extends PrivateEventEmitter {
#requestManager: ChainedRequestManager | ChainedRequestManager;
#connectionRequestManager: RequestManager;
#connectionRequestManager: InjectedRequestManager;

#publicKey?: string;
#connectionUrl?: string;
Expand All @@ -39,7 +39,7 @@ export class ProviderEthereumXnftInjection extends PrivateEventEmitter {
}

this.#requestManager = requestManager;
this.#connectionRequestManager = new RequestManager(
this.#connectionRequestManager = new InjectedRequestManager(
CHANNEL_ETHEREUM_CONNECTION_INJECTED_REQUEST,
CHANNEL_ETHEREUM_CONNECTION_INJECTED_RESPONSE
);
Expand Down
10 changes: 5 additions & 5 deletions packages/provider-core/src/provider-ethereum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
ETHEREUM_RPC_METHOD_CONNECT,
ETHEREUM_RPC_METHOD_SWITCH_CHAIN,
getLogger,
InjectedRequestManager,
NOTIFICATION_ETHEREUM_ACTIVE_WALLET_UPDATED,
NOTIFICATION_ETHEREUM_CHAIN_ID_UPDATED,
NOTIFICATION_ETHEREUM_CONNECTED,
Expand All @@ -20,7 +21,6 @@ import { ethers } from "ethers";
import EventEmitter from "eventemitter3";

import * as cmn from "./common/ethereum";
import { RequestManager } from "./request-manager";
import { isValidEventOrigin } from ".";

const logger = getLogger("provider-ethereum-injection");
Expand Down Expand Up @@ -99,12 +99,12 @@ export class ProviderEthereumInjection extends EventEmitter {
/**
* Channel to send extension specific RPC requests to the extension.
*/
requestManager: RequestManager;
requestManager: InjectedRequestManager;

/**
* Channel to send Solana connection API requests to the extension.
*/
connectionRequestManager: RequestManager;
connectionRequestManager: InjectedRequestManager;

/**
* The chain ID of the currently connected Ethereum chain.
Expand Down Expand Up @@ -144,12 +144,12 @@ export class ProviderEthereumInjection extends EventEmitter {
constructor() {
super();

this.requestManager = new RequestManager(
this.requestManager = new InjectedRequestManager(
CHANNEL_ETHEREUM_RPC_REQUEST,
CHANNEL_ETHEREUM_RPC_RESPONSE
);

this.connectionRequestManager = new RequestManager(
this.connectionRequestManager = new InjectedRequestManager(
CHANNEL_ETHEREUM_CONNECTION_INJECTED_REQUEST,
CHANNEL_ETHEREUM_CONNECTION_INJECTED_RESPONSE
);
Expand Down
Loading