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

chore: extract more tools #1461

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions apps/browser-extension-wallet/.env.defaults
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ CEXPLORER_URL_SANCHONET=https://sancho.cexplorer.io

# Manifest.json
LACE_EXTENSION_KEY=gafhhkghbfjjkeiendhlofajokpaflmk
LACE_EXTENSION_ID=gafhhkghbfjjkeiendhlofajokpaflmk
NAMI_EXTENSION_ID=gafhhkghbfjjkeiendhlofajokpaflmk

# Extension uninstall redirect
LACE_EXTENSION_UNINSTALL_REDIRECT_URL=https://forms.gle/XNcPfWafY8XgxkYw7
Expand Down
2 changes: 2 additions & 0 deletions apps/browser-extension-wallet/.env.developerpreview
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ CEXPLORER_URL_SANCHONET=https://sancho.cexplorer.io

# Manifest.json
LACE_EXTENSION_KEY=djcdfchkaijggdjokfomholkalbffgil
LACE_EXTENSION_ID=djcdfchkaijggdjokfomholkalbffgil
NAMI_EXTENSION_ID=djcdfchkaijggdjokfomholkalbffgil

# Extension uninstall redirect
LACE_EXTENSION_UNINSTALL_REDIRECT_URL=
Expand Down
2 changes: 2 additions & 0 deletions apps/browser-extension-wallet/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ CEXPLORER_URL_SANCHONET=https://sancho.cexplorer.io

# Manifest.json
LACE_EXTENSION_KEY=gafhhkghbfjjkeiendhlofajokpaflmk
LACE_EXTENSION_ID=gafhhkghbfjjkeiendhlofajokpaflmk
NAMI_EXTENSION_ID=gafhhkghbfjjkeiendhlofajokpaflmk

# Midnight
MIDNIGHT_EVENT_BANNER_REMINDER_TIME=129600000
Expand Down
1 change: 0 additions & 1 deletion apps/browser-extension-wallet/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
"@react-rxjs/core": "^0.9.8",
"@react-rxjs/utils": "^0.9.5",
"@shiroyasha9/axios-fetch-adapter": "^1.0.3",
"@xsy/nami-migration-tool": "file:./xsy-nami-migration-tool-0.0.39.tgz",
"antd": "^4.24.10",
"are-you-es5": "^2.1.2",
"bignumber.js": "9.0.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { runtime } from 'webextension-polyfill';
import { useHistory } from 'react-router-dom';
import { walletRoutePaths as routes } from '@routes/wallet-paths';
import { useCurrencyStore } from '@providers/currency';
import { MigrationState } from '@xsy/nami-migration-tool/dist/migrator/migration-state.data';
import { MigrationState } from './migration-tool/migrator/migration-state.data';
import { useTheme } from '@providers/ThemeProvider/context';

const namiMigrationRemoteApi = consumeRemoteApi<Pick<NamiMigrationAPI, 'startMigration' | 'checkMigrationStatus'>>(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { consumeRemoteApi, RemoteApiPropertyType } from '@cardano-sdk/web-extension';
import { NamiMigrationAPI, NamiMigrationChannels } from '@lib/scripts/background/nami-migration';
import { getBackgroundStorage } from '@lib/scripts/background/storage';
import { MigrationState } from '@xsy/nami-migration-tool/dist/migrator/migration-state.data';
import { MigrationState } from './migration-tool/migrator/migration-state.data';
import React, { Fragment, useCallback, useEffect, useState } from 'react';
import { Route, Switch, useHistory, useLocation } from 'react-router-dom';
import { runtime, storage } from 'webextension-polyfill';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/* eslint-disable no-console */
import { runtime, tabs } from 'webextension-polyfill';
import { State as MigrationData } from '../migrator/migration-data.data';
import { MigrationState } from '../migrator/migration-state.data';
import { LaceMessages } from './shared/types';
import { createLaceMigrationPingListener } from './lace/create-lace-migration-ping-listener';
import { NAMI_EXTENSION_ID } from './lace/environment';
import { createLaceMigrationOpenListener } from './lace/create-lace-migration-open-listener';
import { LACE_EXTENSION_ID } from './nami/environment';

type CheckMigrationStatus = () => Promise<MigrationState>;

export const checkMigrationStatus: CheckMigrationStatus = () => {
const message: LaceMessages = { type: 'status' };

return runtime.sendMessage(NAMI_EXTENSION_ID, message);
};

type RequestMigrationData = () => Promise<MigrationData>;

export const requestMigrationData: RequestMigrationData = () => {
const message: LaceMessages = { type: 'data' };
return runtime.sendMessage(NAMI_EXTENSION_ID, message);
};

type AbortMigration = () => Promise<void>;

export const abortMigration: AbortMigration = () => {
const message: LaceMessages = { type: 'abort' };
return runtime.sendMessage(NAMI_EXTENSION_ID, message);
};

type CompleteMigration = () => Promise<void>;

export const completeMigration: CompleteMigration = () => {
const message: LaceMessages = { type: 'completed' };
return runtime.sendMessage(NAMI_EXTENSION_ID, message);
};

export const handleNamiRequests = (): void => {
console.log('[NAMI MIGRATION] createLaceMigrationPingListener');
runtime.onMessageExternal.addListener(createLaceMigrationPingListener(NAMI_EXTENSION_ID));
console.log('[NAMI MIGRATION] createLaceMigrationOpenListener');
runtime.onMessageExternal.addListener(
createLaceMigrationOpenListener(NAMI_EXTENSION_ID, LACE_EXTENSION_ID, tabs.create)
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* eslint-disable no-console */
import { MessageSender, NamiMessages } from '../shared/types';

export const createLaceMigrationOpenListener =
(namiExtensionId: string, laceExtensionId: string, createTab: ({ url }: { url: string }) => void) =>
async (message: NamiMessages, sender: MessageSender): Promise<void> => {
console.log('[NAMI MIGRATION] createLaceMigrationOpenListener', message, sender);
if (message === NamiMessages.open && sender.id === namiExtensionId) {
createTab({ url: `chrome-extension://${laceExtensionId}/app.html` });
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { createLaceMigrationPingListener } from './create-lace-migration-ping-listener';
import { NamiLacePingProtocol } from '../shared/types';

describe('create lace migration ping listener', () => {
const namiId = 'fakeNamiId';

it('should answer ping messages from Nami', async () => {
const listener = createLaceMigrationPingListener(namiId);
const response = await listener(NamiLacePingProtocol.ping, { id: namiId });
expect(response).toBe(NamiLacePingProtocol.pong);
});

it('should ignore messages not coming from Nami extension', async () => {
const listener = createLaceMigrationPingListener(namiId);
const response = await listener(NamiLacePingProtocol.ping, {
id: 'otherId'
});
expect(response).toBeUndefined();
});

it('should ignore other messages coming from Nami', async () => {
const listener = createLaceMigrationPingListener(namiId);
const response = await listener('other' as NamiLacePingProtocol, {
id: namiId
});
expect(response).toBeUndefined();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* eslint-disable consistent-return */
/* eslint-disable no-console */
import { MessageSender, NamiLacePingProtocol } from '../shared/types';

export const createLaceMigrationPingListener =
(namiExtensionId: string) =>
async (message: NamiLacePingProtocol, sender: MessageSender): Promise<void | NamiLacePingProtocol.pong> => {
console.log('[NAMI MIGRATION] createLaceMigrationPingListener', message, sender);
if (message === NamiLacePingProtocol.ping && sender.id === namiExtensionId) {
console.log('[NAMI MIGRATION] Sending pong message to Nami');
return NamiLacePingProtocol.pong;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
if (process.env.NAMI_EXTENSION_ID === undefined) {
throw new Error('process.env.NAMI_EXTENSION_ID must be defined');
}
export const NAMI_EXTENSION_ID = process.env.NAMI_EXTENSION_ID;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
if (process.env.LACE_EXTENSION_ID === undefined) {
throw new Error('process.env.LACE_EXTENSION_ID must be defined');
}
export const LACE_EXTENSION_ID = process.env.LACE_EXTENSION_ID;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export enum MigrationExceptions {
NotInProgress = 'not-in-progress',
FailedToParse = 'failed-to-parse'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { State } from '../../migrator/nami-storage.data';

export const createMockNamiStore = (mockedState: Partial<State> = {}): { set: jest.Mock; get: jest.Mock } => {
const store = {
set: jest.fn(),
get: jest.fn()
};
store.get.mockResolvedValue(mockedState);
return store;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import * as Nami from '../../migrator/nami-storage.data';

export interface MessageSender {
id?: string;
}

export type SendMessageToExtension = (id: string, message: unknown) => Promise<string | void>;

export enum NamiLacePingProtocol {
ping = 'ping',
pong = 'pong'
}

export type NamiStore = {
set: (value: Partial<Nami.State>) => Promise<void>;
get: () => Promise<Partial<Nami.State>>;
};

export type LaceMessages =
| {
type: 'status';
}
| {
type: 'data';
}
| {
type: 'abort';
}
| {
type: 'completed';
};

export enum NamiMessages {
'open' = 'open'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
export interface State {
encryptedPrivateKey: string;
accounts: Account[];
hardwareWallets: HarwareWallet[];
dapps: string[];
currency: 'usd' | 'eur';
analytics: Analytics;
themeColor: string;
}

export interface Account {
index: number;
name: string;
extendedAccountPublicKey: string;
collaterals: Record<Networks, Collateral | undefined>;
paymentAddresses: Record<Networks, string>;
}

export interface HarwareWallet extends Account {
vendor: 'ledger' | 'trezor';
}

export interface Collateral {
lovelace: string;
tx: {
hash: string;
index: number;
};
}

export type Networks = 'mainnet' | 'preview' | 'preprod';

export interface Analytics {
enabled: boolean;
userId: string;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export enum MigrationState {
None = 'none',
InProgress = 'in-progress',
Completed = 'completed',
Dismissed = 'dismissed'
}
Loading
Loading