Skip to content

Commit

Permalink
refactor(extension): use the new migration flow
Browse files Browse the repository at this point in the history
  • Loading branch information
renanvalentin committed Apr 29, 2024
1 parent 0877108 commit 45497d2
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type * as Nami from '@xsy/nami-migration-tool';
import type * as Nami from '@xsy/nami-migration-tool/dist/migrator/migration-data.data';

export const state: Nami.State = {
encryptedPrivateKey:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type * as Nami from '@xsy/nami-migration-tool';
import type * as Nami from '@xsy/nami-migration-tool/dist/migrator/migration-data.data';
import * as Extension from '@cardano-sdk/web-extension';
import { Wallet } from '@lace/cardano';
import { HexBlob } from '@cardano-sdk/util';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { runtime, tabs } from 'webextension-polyfill';
import { walletRoutePaths } from '@routes/wallet-paths';
import type * as Nami from '@xsy/nami-migration-tool';
import { runtime } from 'webextension-polyfill';
import * as laceMigrationClient from '@xsy/nami-migration-tool/dist/cross-extension-messaging/lace-migration-client.extension';
import { MigrationState } from '@xsy/nami-migration-tool/dist/migrator/migration-state.data';
import { walletRepository, walletManager, getBaseDbName } from './wallet';

import { run, CollateralRepository } from './nami-migration-runner';
import { currencyCode } from '@providers/currency/constants';
import { getWalletStoreId } from '@cardano-sdk/web-extension';
import { exposeApi, getWalletStoreId, RemoteApiPropertyType } from '@cardano-sdk/web-extension';
import { storage } from '@cardano-sdk/wallet';

const namiId = 'igonoepjobamjpghplmhjoleleipkkpk';
import { of } from 'rxjs';

const collateralRepository: CollateralRepository = async ({ utxo, chainId, walletId, accountIndex }) => {
const walletStoreId = getWalletStoreId(walletId, chainId, accountIndex);
Expand All @@ -17,22 +16,48 @@ const collateralRepository: CollateralRepository = async ({ utxo, chainId, walle
db.setAll([utxo]);
};

runtime.onMessageExternal.addListener(async (request, sender) => {
if (sender.id !== namiId) return;

const state: Nami.State = request;
const startMigration = async () => {
const state = await laceMigrationClient.requestMigrationData();

await run({ walletRepository, walletManager, state, collateralRepository });

const encodedData = Buffer.from(
JSON.stringify({
currency: state.currency === 'usd' ? currencyCode.usd : currencyCode.eur,
analytics: state.analytics
})
).toString('base64');
await laceMigrationClient.completeMigration();

const path = walletRoutePaths.nami.root;
const params = `?data=${encodedData}`;
return {
currency: state.currency === 'usd' ? currencyCode.usd : currencyCode.eur,
analytics: state.analytics
};
};

await tabs.create({ url: `app.html#${path}${params}` }).catch((error) => console.error(error));
});
export enum NamiMigrationChannels {
MIGRATION = 'migration'
}

export interface NamiMigrationAPI {
checkMigrationStatus: () => Promise<MigrationState>;
startMigration: () => Promise<{
currency: currencyCode;
analytics: {
enabled: boolean;
userId: string;
};
}>;
abortMigration: () => Promise<void>;
}

exposeApi<NamiMigrationAPI>(
{
api$: of({
startMigration,
checkMigrationStatus: laceMigrationClient.checkMigrationStatus,
abortMigration: laceMigrationClient.abortMigration
}),
baseChannel: NamiMigrationChannels.MIGRATION,
properties: {
startMigration: RemoteApiPropertyType.MethodReturningPromise,
checkMigrationStatus: RemoteApiPropertyType.MethodReturningPromise,
abortMigration: RemoteApiPropertyType.MethodReturningPromise
}
},
{ logger: console, runtime }
);

0 comments on commit 45497d2

Please sign in to comment.