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

Refactor messaging system for XRPL protocol endpoints #323

Merged
merged 7 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 0 additions & 4 deletions packages/api/src/helpers/extensionMessaging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,6 @@ const serializeMessage = (msg: APIMessages): any => {
modifiedMsg.payload.flags = JSON.stringify(modifiedMsg.payload.flags);
}

if (typeof modifiedMsg.payload?.transaction === 'object') {
modifiedMsg.payload.transaction = JSON.stringify(modifiedMsg.payload.transaction);
}

if (modifiedMsg.payload?.NFTokenOffers) {
modifiedMsg.payload.NFTokenOffers = JSON.stringify(modifiedMsg.payload.NFTokenOffers);
}
Expand Down
53 changes: 18 additions & 35 deletions packages/extension/cypress/e2e/sign_transaction.cy.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { Network, NETWORK } from '@gemwallet/constants';

describe('Sign Transaction', () => {
// deepcode ignore NoHardcodedPasswords: password used for testing purposes
const PASSWORD = 'SECRET_PASSWORD';
import { navigate } from '../utils/navigation';

const URL =
'http://localhost:3000/sign-transaction?storageKey=1693425372955.3833&id=210401828&requestMessage=undefined&sign=transaction';

// deepcode ignore NoHardcodedPasswords: password used for testing purposes
const PASSWORD = 'SECRET_PASSWORD';

describe('Sign Transaction', () => {
beforeEach(() => {
// Mock the localStorage with a wallet already loaded
cy.window().then((win) => {
Expand All @@ -21,8 +26,13 @@ describe('Sign Transaction', () => {
});

it('Sign Transaction', () => {
const url = `http://localhost:3000?transaction=%7B%22TransactionType%22%3A%22Payment%22%2C%22Destination%22%3A%22rhikRdkFw28csKw9z7fVoBjWncz1HSoQij%22%2C%22Amount%22%3A%22100000%22%7D&id=210329246&requestMessage=undefined&sign=transaction`;
navigate(url, PASSWORD);
const transaction = {
TransactionType: 'Payment',
Destination: 'rhikRdkFw28csKw9z7fVoBjWncz1HSoQij',
Amount: '100000'
};

navigate(URL, PASSWORD, '1693425372955.3833', { transaction });

cy.get('h1[data-testid="page-title"]').should('have.text', 'Sign Transaction');

Expand All @@ -40,7 +50,7 @@ describe('Sign Transaction', () => {
});

it('Sign Transaction (Set Trustline SOLO 1000000)', () => {
const transaction = JSON.stringify({
const transaction = {
TransactionType: 'TrustSet',
LimitAmount: {
currency: '534F4C4F00000000000000000000000000000000',
Expand All @@ -56,9 +66,8 @@ describe('Sign Transaction', () => {
}
],
Fee: '199'
});
const url = `http://localhost:3000?transaction=${transaction}&requestMessage=undefined&sign=transaction`;
navigate(url, PASSWORD);
};
navigate(URL, PASSWORD, '1693425372955.3833', { transaction });

cy.get('h1[data-testid="page-title"]').should('have.text', 'Sign Transaction');

Expand All @@ -80,30 +89,4 @@ describe('Sign Transaction', () => {
});
cy.get('p[data-testid="transaction-subtitle"]').should('have.text', 'Transaction Successful');
});

const navigate = (url: string, password: string) => {
cy.visit(url, {
onBeforeLoad(win) {
(win as any).chrome = (win as any).chrome || {};
(win as any).chrome.runtime = {
sendMessage(message, cb) {}
};

(win as any).chrome.storage = {
local: {
get(key, cb) {},
set(obj, cb) {
if (cb) cb();
}
}
};

cy.stub((win as any).chrome.runtime, 'sendMessage').resolves({});
}
});

// Login
cy.get('input[name="password"]').type(password);
cy.contains('button', 'Unlock').click();
};
});
53 changes: 18 additions & 35 deletions packages/extension/cypress/e2e/submit_transaction.cy.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { Network, NETWORK } from '@gemwallet/constants';

describe('Submit Transaction', () => {
// deepcode ignore NoHardcodedPasswords: password used for testing purposes
const PASSWORD = 'SECRET_PASSWORD';
import { navigate } from '../utils/navigation';

const URL =
'http://localhost:3000/submit-transaction?storageKey=1693425372955.3833&id=210401828&requestMessage=undefined&submit=transaction';

// deepcode ignore NoHardcodedPasswords: password used for testing purposes
const PASSWORD = 'SECRET_PASSWORD';

describe('Submit Transaction', () => {
beforeEach(() => {
// Mock the localStorage with a wallet already loaded
cy.window().then((win) => {
Expand All @@ -21,8 +26,13 @@ describe('Submit Transaction', () => {
});

it('Submit Transaction', () => {
const url = `http://localhost:3000?transaction=%7B%22TransactionType%22%3A%22Payment%22%2C%22Destination%22%3A%22rhikRdkFw28csKw9z7fVoBjWncz1HSoQij%22%2C%22Amount%22%3A%22100000%22%7D&id=210329246&requestMessage=undefined&submit=transaction`;
navigate(url, PASSWORD);
const transaction = {
TransactionType: 'Payment',
Destination: 'rhikRdkFw28csKw9z7fVoBjWncz1HSoQij',
Amount: '100000'
};

navigate(URL, PASSWORD, '1693425372955.3833', { transaction });

cy.get('h1[data-testid="page-title"]').should('have.text', 'Submit Transaction');

Expand Down Expand Up @@ -50,7 +60,7 @@ describe('Submit Transaction', () => {
});

it('Submit Transaction (Set Trustline SOLO 1000000)', () => {
const transaction = JSON.stringify({
const transaction = {
TransactionType: 'TrustSet',
LimitAmount: {
currency: '534F4C4F00000000000000000000000000000000',
Expand All @@ -66,9 +76,8 @@ describe('Submit Transaction', () => {
}
],
Fee: '199'
});
const url = `http://localhost:3000?transaction=${transaction}&requestMessage=undefined&submit=transaction`;
navigate(url, PASSWORD);
};
navigate(URL, PASSWORD, '1693425372955.3833', { transaction });

cy.get('h1[data-testid="page-title"]').should('have.text', 'Submit Transaction');

Expand Down Expand Up @@ -100,30 +109,4 @@ describe('Submit Transaction', () => {
});
cy.get('p[data-testid="transaction-subtitle"]').should('have.text', 'Transaction Successful');
});

const navigate = (url: string, password: string) => {
cy.visit(url, {
onBeforeLoad(win) {
(win as any).chrome = (win as any).chrome || {};
(win as any).chrome.runtime = {
sendMessage(message, cb) {}
};

(win as any).chrome.storage = {
local: {
get(key, cb) {},
set(obj, cb) {
if (cb) cb();
}
}
};

cy.stub((win as any).chrome.runtime, 'sendMessage').resolves({});
}
});

// Login
cy.get('input[name="password"]').type(password);
cy.contains('button', 'Unlock').click();
};
});
47 changes: 2 additions & 45 deletions packages/extension/cypress/e2e/submit_transactions_bulk.cy.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Network, NETWORK } from '@gemwallet/constants';

const mockSessionStorage = {};
import { navigate } from '../utils/navigation';

describe('Submit Transactions (Bulk)', () => {
// deepcode ignore NoHardcodedPasswords: password used for testing purposes
Expand Down Expand Up @@ -91,7 +91,7 @@ describe('Submit Transactions (Bulk)', () => {
});

it('Submit Transactions', () => {
const url = `http://localhost:3000?submit-transactions-bulk&storageKey=1693425372955.3833&id=93425114&requestMessage=undefined&submit=txBulk`;
const url = `http://localhost:3000/submit-transactions-bulk?storageKey=1693425372955.3833&id=93425114&requestMessage=undefined&submit=txBulk`;

navigate(url, PASSWORD, '1693425372955.3833', {
transactions: transactionsMap,
Expand Down Expand Up @@ -128,47 +128,4 @@ describe('Submit Transactions (Bulk)', () => {
});
cy.get('p[data-testid="transaction-subtitle"]').should('have.text', 'Transactions Successful');
});

const navigate = (url: string, password: string, storageKey: string, transactions) => {
cy.visit(url, {
onBeforeLoad(win) {
(win as any).chrome = (win as any).chrome || {};
(win as any).chrome.runtime = {
sendMessage(message, cb) {}
};

(win as any).chrome.storage = {
local: {
get(key, cb) {},
set(obj, cb) {
if (cb) cb();
}
},
session: {
set(obj, cb) {
Object.assign(mockSessionStorage, obj);
if (cb) cb();
},
get(key, cb) {
cb(mockSessionStorage[key]);
},
remove(key, cb) {
delete mockSessionStorage[key];
if (cb) cb();
}
}
};

(win as any).chrome.storage.session.set({
[storageKey]: { [storageKey]: JSON.stringify(transactions) }
});

(win as any).chrome.runtime.lastError = null;
}
});

// Login
cy.get('input[name="password"]').type(password);
cy.contains('button', 'Unlock').click();
};
});
44 changes: 44 additions & 0 deletions packages/extension/cypress/utils/navigation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const mockSessionStorage = {};

export const navigate = (url: string, password: string, storageKey: string, data) => {
cy.visit(url, {
onBeforeLoad(win) {
(win as any).chrome = (win as any).chrome || {};
(win as any).chrome.runtime = {
sendMessage(message, cb) {}
};

(win as any).chrome.storage = {
local: {
get(key, cb) {},
set(obj, cb) {
if (cb) cb();
}
},
session: {
set(obj, cb) {
Object.assign(mockSessionStorage, obj);
if (cb) cb();
},
get(key, cb) {
cb(mockSessionStorage[key]);
},
remove(key, cb) {
delete mockSessionStorage[key];
if (cb) cb();
}
}
};

(win as any).chrome.storage.session.set({
[storageKey]: { [storageKey]: JSON.stringify(data) }
});

(win as any).chrome.runtime.lastError = null;
}
});

// Login
cy.get('input[name="password"]').type(password);
cy.contains('button', 'Unlock').click();
};
42 changes: 20 additions & 22 deletions packages/extension/src/chromeServices/background/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ let currentReceivingMessage: string | undefined = undefined; // For reject messa

// Used to send a message to the view through the chrome.storage.local memory.
// Useful when the data to send is big.
const sendInMemoryMessage = ({
const sendMessageInMemory = ({
payload,
parameter,
receivingMessage,
Expand Down Expand Up @@ -426,31 +426,29 @@ chrome.runtime.onMessage.addListener(
}
});
} else if (type === 'REQUEST_SUBMIT_TRANSACTION/V3') {
handleTransactionRequest({
payload: message.payload,
sender,
parameter: PARAMETER_SUBMIT_TRANSACTION,
receivingMessage: 'RECEIVE_SUBMIT_TRANSACTION/V3',
errorPayload: {
type: ResponseType.Reject,
result: undefined
}
});
const { payload } = message;
try {
sendMessageInMemory({
payload,
parameter: PARAMETER_SUBMIT_TRANSACTION,
receivingMessage: 'RECEIVE_SUBMIT_TRANSACTION/V3',
sender
});
} catch (e) {}
FlorianBouron marked this conversation as resolved.
Show resolved Hide resolved
} else if (type === 'REQUEST_SIGN_TRANSACTION/V3') {
handleTransactionRequest({
payload: message.payload,
sender,
parameter: PARAMETER_SIGN_TRANSACTION,
receivingMessage: 'RECEIVE_SIGN_TRANSACTION/V3',
errorPayload: {
type: ResponseType.Reject,
result: undefined
}
});
const { payload } = message;
try {
sendMessageInMemory({
payload,
parameter: PARAMETER_SIGN_TRANSACTION,
receivingMessage: 'RECEIVE_SIGN_TRANSACTION/V3',
sender
});
} catch (e) {}
} else if (type === 'REQUEST_SUBMIT_BULK_TRANSACTIONS/V3') {
const { payload } = message;
try {
sendInMemoryMessage({
sendMessageInMemory({
payload,
parameter: PARAMETER_SUBMIT_TRANSACTIONS_BULK,
receivingMessage: 'RECEIVE_SUBMIT_BULK_TRANSACTIONS/V3',
Expand Down
Loading