Skip to content

Commit

Permalink
splits out sign blob and sign tx bodies
Browse files Browse the repository at this point in the history
  • Loading branch information
aristidesstaffieri committed Jul 28, 2023
1 parent f037bdc commit 440302a
Show file tree
Hide file tree
Showing 12 changed files with 765 additions and 649 deletions.
15 changes: 7 additions & 8 deletions @shared/api/external.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,14 @@ export const submitTransaction = async (

export const submitBlob = async (
blob: string,
opts:
| {
network: string
accountToSign: string
networkPassphrase: string
},
opts: {
network: string;
accountToSign: string;
networkPassphrase: string;
},
): Promise<string> => {
let response = { signedTransaction: "", error: "" };
const {network, networkPassphrase, accountToSign} = opts
const { network, networkPassphrase, accountToSign } = opts;
try {
response = await sendMessageToContentScript({
transactionXDR: blob,
Expand All @@ -97,7 +96,7 @@ export const submitBlob = async (
throw error;
}
return signedTransaction;
}
};

export const requestNetwork = async (): Promise<string> => {
let response = { network: "", error: "" };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ import {
} from "background/helpers/dataStorage";
import { publicKeySelector } from "background/ducks/session";

import { blobQueue, responseQueue, transactionQueue } from "./popupMessageListener";
import {
blobQueue,
responseQueue,
transactionQueue,
} from "./popupMessageListener";

const localStore = dataStorageAccess(browserLocalStorage);

Expand Down Expand Up @@ -189,7 +193,7 @@ export const freighterApiMessageListener = (

transactionQueue.push(transaction);
const encodedBlob = encodeObject(transactionInfo);

const popup = browser.windows.create({
url: chrome.runtime.getURL(
`/index.html#/sign-transaction?${encodedBlob}`,
Expand Down Expand Up @@ -224,10 +228,7 @@ export const freighterApiMessageListener = (
};

const submitBlob = async () => {
const {
transactionXdr,
accountToSign,
} = request;
const { transactionXdr, accountToSign } = request;

const { tab, url: tabUrl = "" } = sender;
const domain = getUrlHostname(tabUrl);
Expand All @@ -243,11 +244,11 @@ export const freighterApiMessageListener = (
tab,
blob: transactionXdr,
url: tabUrl,
accountToSign
}
accountToSign,
};

blobQueue.push(blob);
const encodedBlob = encodeObject(blob)
const encodedBlob = encodeObject(blob);
const popup = browser.windows.create({
url: chrome.runtime.getURL(
`/index.html#/sign-transaction?${encodedBlob}`,
Expand Down
18 changes: 10 additions & 8 deletions extension/src/background/messageListener/popupMessageListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,12 @@ export const transactionQueue: Array<{
toXDR: () => void;
}> = [];
export const blobQueue: Array<{
isDomainListedAllowed: boolean,
domain: string,
tab: browser.Tabs.Tab | undefined,
blob: string,
url: string,
accountToSign: string
isDomainListedAllowed: boolean;
domain: string;
tab: browser.Tabs.Tab | undefined;
blob: string;
url: string;
accountToSign: string;
}> = [];

interface KeyPair {
Expand Down Expand Up @@ -916,7 +916,9 @@ export const popupMessageListener = (request: Request, sessionStore: Store) => {
const sourceKeys = SDK.Keypair.fromSecret(privateKey);

const blob = blobQueue.pop();
const response = blob ? await sourceKeys.sign(Buffer.from(blob.blob, 'base64')) : null
const response = blob
? await sourceKeys.sign(Buffer.from(blob.blob, "base64"))
: null;

const blobResponse = responseQueue.pop();

Expand All @@ -927,7 +929,7 @@ export const popupMessageListener = (request: Request, sessionStore: Store) => {
}

return { error: "Session timed out" };
}
};

const rejectTransaction = () => {
transactionQueue.pop();
Expand Down
4 changes: 2 additions & 2 deletions extension/src/helpers/__tests__/stellar.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe("getTransactionInfo", () => {
flaggedKeys: { test: { tags: [""] } },
tab: {},
});
const info = getTransactionInfo("foo")
const info = getTransactionInfo("foo");
if (!("blob" in info)) {
expect(info.isHttpsDomain).toBe(true);
}
Expand All @@ -40,7 +40,7 @@ describe("getTransactionInfo", () => {
flaggedKeys: { test: { tags: [""] } },
tab: {},
});
const info = getTransactionInfo("foo")
const info = getTransactionInfo("foo");
if (!("blob" in info)) {
expect(info.isHttpsDomain).toBe(false);
}
Expand Down
2 changes: 1 addition & 1 deletion extension/src/helpers/stellar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const getTransactionInfo = (search: string) => {
const searchParams = parsedSearchParam(search);

if ("blob" in searchParams) {
return searchParams
return searchParams;
}

const {
Expand Down
16 changes: 9 additions & 7 deletions extension/src/helpers/urls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import browser from "webextension-polyfill";
import { TransactionInfo } from "../types/transactions";

export interface BlobToSign {
isDomainListedAllowed: boolean,
domain: string,
tab: browser.Tabs.Tab | undefined,
blob: string,
url: string,
accountToSign: string
isDomainListedAllowed: boolean;
domain: string;
tab: browser.Tabs.Tab | undefined;
blob: string;
url: string;
accountToSign: string;
}

export const encodeObject = (obj: {}) =>
Expand All @@ -22,7 +22,9 @@ export const newTabHref = (path = "", queryParams = "") =>

export const removeQueryParam = (url = "") => url.replace(/\?(.*)/, "");

export const parsedSearchParam = (param: string): TransactionInfo | BlobToSign => {
export const parsedSearchParam = (
param: string,
): TransactionInfo | BlobToSign => {
const decodedSearchParam = decodeString(param.replace("?", ""));
return decodedSearchParam ? JSON.parse(decodedSearchParam) : {};
};
Expand Down
13 changes: 3 additions & 10 deletions extension/src/popup/ducks/access.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
rejectAccess as internalRejectAccess,
grantAccess as internalGrantAccess,
signTransaction as internalSignTransaction,
signBlob as internalSignBlob
signBlob as internalSignBlob,
} from "@shared/api/internal";

export const grantAccess = createAsyncThunk("grantAccess", internalGrantAccess);
Expand All @@ -19,10 +19,7 @@ export const signTransaction = createAsyncThunk(
internalSignTransaction,
);

export const signBlob = createAsyncThunk(
"signBlob",
internalSignBlob,
);
export const signBlob = createAsyncThunk("signBlob", internalSignBlob);

// Basically an alias for metrics purposes
export const rejectTransaction = createAsyncThunk(
Expand All @@ -31,8 +28,4 @@ export const rejectTransaction = createAsyncThunk(
);

// Basically an alias for metrics purposes
export const rejectBlob = createAsyncThunk(
"rejectBlob",
internalRejectAccess,
);

export const rejectBlob = createAsyncThunk("rejectBlob", internalRejectAccess);
4 changes: 2 additions & 2 deletions extension/src/popup/metrics/views.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,14 @@ registerHandler<AppState>(navigate, (_, a) => {
const info = getTransactionInfo(search);

if (!("blob" in info)) {
const { operations, operationTypes } = info
const { operations, operationTypes } = info;
const METRIC_OPTIONS = {
domain: getUrlDomain(url),
subdomain: getUrlHostname(url),
number_of_operations: operations.length,
operationTypes,
};

emitMetric(eventName, METRIC_OPTIONS);
}
} else {
Expand Down
Loading

0 comments on commit 440302a

Please sign in to comment.