Skip to content

Commit

Permalink
some cleanup (#3407)
Browse files Browse the repository at this point in the history
  • Loading branch information
armaniferrante authored Mar 18, 2023
1 parent 83782ea commit fd3bebb
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 98 deletions.
57 changes: 24 additions & 33 deletions packages/background/src/backend/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1322,6 +1322,10 @@ export class Backend {
);
}

///////////////////////////////////////////////////////////////////////////////
// User account.
///////////////////////////////////////////////////////////////////////////////

/**
* Add a public key to a Backpack account via the Backpack API.
*/
Expand Down Expand Up @@ -1541,31 +1545,12 @@ export class Backend {
return json;
}

/**
* Query the Backpack API to check if a user has already used any of the
* blockchain/public key pairs from a list.
*/
async findServerPublicKeyConflicts(
serverPublicKeys: ServerPublicKey[]
): Promise<string[]> {
const url = `${BACKEND_API_URL}/publicKeys`;
const response = await fetch(url, {
method: "POST",
body: JSON.stringify(serverPublicKeys),
headers: {
"Content-Type": "application/json",
},
}).then((r) => r.json());

return response;
}

/**
* Find a `WalletDescriptor` that can be used to create a new account.
* This requires that the sub wallets on the account index are not used by a
* existing user account. This is checked by querying the Backpack API.
*
* This only works for mnemonics or a keyring store unlocked with a mnemoni
* This only works for mnemonics or a keyring store unlocked with a mnemonic
* because the background service worker can't use a Ledger.
*/
async findWalletDescriptor(
Expand Down Expand Up @@ -1605,6 +1590,25 @@ export class Backend {
}
}

/**
* Query the Backpack API to check if a user has already used any of the
* blockchain/public key pairs from a list.
*/
async findServerPublicKeyConflicts(
serverPublicKeys: ServerPublicKey[]
): Promise<string[]> {
const url = `${BACKEND_API_URL}/publicKeys`;
const response = await fetch(url, {
method: "POST",
body: JSON.stringify(serverPublicKeys),
headers: {
"Content-Type": "application/json",
},
}).then((r) => r.json());

return response;
}

///////////////////////////////////////////////////////////////////////////////
// Preferences.
///////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -2023,19 +2027,6 @@ export class Backend {

return SUCCESS_RESPONSE;
}

async pluginLocalStorageGet(xnftAddress: string, key: string): Promise<any> {
return await store.LocalStorageDb.get(`${xnftAddress}:${key}`);
}

async pluginLocalStoragePut(
xnftAddress: string,
key: string,
value: any
): Promise<any> {
await store.LocalStorageDb.set(`${xnftAddress}:${key}`, value);
return SUCCESS_RESPONSE;
}
}

export const SUCCESS_RESPONSE = "success";
Expand Down
39 changes: 0 additions & 39 deletions packages/background/src/frontend/server-ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,6 @@ import {
UI_RPC_METHOD_NAVIGATION_TO_DEFAULT,
UI_RPC_METHOD_NAVIGATION_TO_ROOT,
UI_RPC_METHOD_PASSWORD_UPDATE,
UI_RPC_METHOD_PLUGIN_LOCAL_STORAGE_GET,
UI_RPC_METHOD_PLUGIN_LOCAL_STORAGE_PUT,
UI_RPC_METHOD_PREFERENCES_READ,
UI_RPC_METHOD_PREVIEW_PUBKEYS,
UI_RPC_METHOD_SET_FEATURE_GATES,
Expand Down Expand Up @@ -369,18 +367,6 @@ async function handle<T = any>(
case UI_RPC_METHOD_KEYRING_STORE_CHECK_PASSWORD:
return await handleKeyringStoreCheckPassword(ctx, params[0]);
//
// xNFT storage.
//
case UI_RPC_METHOD_PLUGIN_LOCAL_STORAGE_GET:
return await handlePluginLocalStorageGet(ctx, params[0], params[1]);
case UI_RPC_METHOD_PLUGIN_LOCAL_STORAGE_PUT:
return await handlePluginLocalStoragePut(
ctx,
params[0],
params[1],
params[2]
);
//
// Solana.
//
case UI_RPC_METHOD_SOLANA_SIMULATE:
Expand Down Expand Up @@ -1145,28 +1131,3 @@ async function handlePreviewPubkeys(
const resp = await ctx.backend.previewPubkeys(...args);
return [resp];
}

// This API is only safe because it assumes the frontend UI code is doing
// the proper gatekeeping. It shouldn't allow other xNFTs to call this
// api with a fake plugin string.
async function handlePluginLocalStorageGet(
ctx: Context<Backend>,
xnftAddress: string,
key: string
): Promise<RpcResponse<any>> {
const resp = await ctx.backend.pluginLocalStorageGet(xnftAddress, key);
return [resp];
}

// This API is only safe because it assumes the frontend UI code is doing
// the proper gatekeeping. It shouldn't allow other xNFTs to call this
// api with a fake plugin string.
async function handlePluginLocalStoragePut(
ctx: Context<Backend>,
xnftAddress: string,
key: string,
value: any
): Promise<RpcResponse<any>> {
const resp = await ctx.backend.pluginLocalStoragePut(xnftAddress, key, value);
return [resp];
}
4 changes: 0 additions & 4 deletions packages/common/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,6 @@ export const UI_RPC_METHOD_NAVIGATION_TO_ROOT =
export const UI_RPC_METHOD_NAVIGATION_TO_DEFAULT =
"ui-rpc-method-navigation-to-default";
export const UI_RPC_METHOD_PASSWORD_UPDATE = "ui-rpc-method-password-update";
export const UI_RPC_METHOD_PLUGIN_LOCAL_STORAGE_GET =
"ui-rpc-method-plugin-storage-get";
export const UI_RPC_METHOD_PLUGIN_LOCAL_STORAGE_PUT =
"ui-rpc-method-plugin-storage-put";
export const UI_RPC_METHOD_SET_FEATURE_GATES =
"ui-rpc-method-set-feature-gates";
export const UI_RPC_METHOD_GET_FEATURE_GATES =
Expand Down
22 changes: 0 additions & 22 deletions packages/common/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ import {
SOLANA_RPC_METHOD_SIGN_MESSAGE as PLUGIN_SOLANA_RPC_METHOD_SIGN_MESSAGE,
SOLANA_RPC_METHOD_SIGN_TX as PLUGIN_SOLANA_RPC_METHOD_SIGN_TX,
SOLANA_RPC_METHOD_SIMULATE as PLUGIN_SOLANA_RPC_METHOD_SIMULATE_TX,
UI_RPC_METHOD_PLUGIN_LOCAL_STORAGE_GET,
UI_RPC_METHOD_PLUGIN_LOCAL_STORAGE_PUT,
} from "./constants";
import { getLogger } from "./logging";
import type { Event, RpcResponse, XnftMetadata, XnftPreference } from "./types";
Expand Down Expand Up @@ -415,10 +413,6 @@ export class Plugin {

const { method, params } = req;
switch (method) {
case PLUGIN_RPC_METHOD_LOCAL_STORAGE_GET:
return await this._handleGet(params[0]);
case PLUGIN_RPC_METHOD_LOCAL_STORAGE_PUT:
return await this._handlePut(params[0], params[1]);
case PLUGIN_RPC_METHOD_WINDOW_OPEN:
return await this._handleWindowOpen(params[0]);
case PLUGIN_RPC_METHOD_PLUGIN_OPEN:
Expand Down Expand Up @@ -582,22 +576,6 @@ export class Plugin {
return ["success"];
}

private async _handleGet(key: string): Promise<RpcResponse> {
const resp = await this._backgroundClient?.request({
method: UI_RPC_METHOD_PLUGIN_LOCAL_STORAGE_GET,
params: [this.xnftAddress.toString(), key],
});
return [resp];
}

private async _handlePut(key: string, value: any): Promise<RpcResponse> {
const resp = await this._backgroundClient?.request({
method: UI_RPC_METHOD_PLUGIN_LOCAL_STORAGE_PUT,
params: [this.xnftAddress.toString(), key, value],
});
return [resp];
}

private async _handlePopout(fullscreen: boolean): Promise<RpcResponse> {
if (fullscreen) {
window.open("popup.html", "_blank");
Expand Down

1 comment on commit fd3bebb

@vercel
Copy link

@vercel vercel bot commented on fd3bebb Mar 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.