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

fix(dw): transfer page crash + csv export #2732

Merged
merged 4 commits into from
Dec 12, 2024
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
2 changes: 2 additions & 0 deletions packages/apps/dev-wallet/src/App/routes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { Networks } from '@/pages/networks/networks';
import { Ready } from '@/pages/ready/ready';
import { AutoBackup } from '@/pages/settings/auto-backup/auto-backup';
import { ChangePassword } from '@/pages/settings/change-password/change-password';
import { ExportData } from '@/pages/settings/export-data/export-data';
import { RevealPhrase } from '@/pages/settings/reveal-phrase/reveal-phrase';
import { Settings } from '@/pages/settings/settings';
import { SignatureBuilder } from '@/pages/signature-builder/signature-builder';
Expand Down Expand Up @@ -126,6 +127,7 @@ export const Routes: FC = () => {
<Route path="/contacts" element={<Contacts />} />
<Route path="/settings" element={<Settings />} />
<Route path="/settings/auto-backup" element={<AutoBackup />} />
<Route path="/settings/export-data" element={<ExportData />} />
<Route
path="/account-discovery/:keySourceId"
element={<AccountDiscovery />}
Expand Down
2 changes: 1 addition & 1 deletion packages/apps/dev-wallet/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const config = {
colorList,
defaultAccentColor: colorList[0],
DB: {
DB_VERSION: 42,
DB_VERSION: 43,
DB_NAME: 'dev-wallet',
},
ACCOUNTS: {
Expand Down
38 changes: 24 additions & 14 deletions packages/apps/dev-wallet/src/modules/account/account.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,18 @@ const createAccountRepository = ({
);
return accounts;
},
async getAccountsByProfileId(profileId: string, networkUUID: UUID) {
const accounts: Array<IAccount> = await getAll(
'account',
IDBKeyRange.only([profileId, networkUUID]),
'profile-network',
);
return accounts;
async getAccountsByProfileId(
profileId: string,
networkUUID?: UUID,
): Promise<IAccount[]> {
if (networkUUID) {
return getAll(
'account',
IDBKeyRange.only([profileId, networkUUID]),
'profile-network',
);
}
return getAll('account', profileId, 'profileId');
},
addFungible: async (fungible: Fungible): Promise<void> => {
return add('fungible', fungible);
Expand Down Expand Up @@ -167,13 +172,18 @@ const createAccountRepository = ({
await actions.updateWatchedAccount(updatedAccount);
return updatedAccount;
},
async getWatchedAccountsByProfileId(profileId: string, networkUUID: UUID) {
const accounts: Array<IWatchedAccount> = await getAll(
'watched-account',
IDBKeyRange.only([profileId, networkUUID]),
'profile-network',
);
return accounts;
async getWatchedAccountsByProfileId(
profileId: string,
networkUUID?: UUID,
): Promise<IWatchedAccount[]> {
if (networkUUID) {
return getAll(
'watched-account',
IDBKeyRange.only([profileId, networkUUID]),
'profile-network',
);
}
return getAll('watched-account', profileId, 'profileId');
},
};
return actions;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// check the change log for more details
import { IContact } from '@/modules/contact/contact.repository';
import { BuiltInPredicate } from '@kadena/client';
import { getAllItems, putItem } from '../indexeddb';

const changeLog = [
'add guard to the contacts; this was missed in the 40 to 41 migration',
];

export async function migrateFrom42to43(
db: IDBDatabase,
transaction: IDBTransaction,
) {
console.log('migrating from 42 to 43', 'change log:');
changeLog.forEach((log, index) => console.log(index, log));
const update = putItem(db, transaction);
const allContacts = await getAllItems(db, transaction)<IContact>('contact');
await Promise.all(
allContacts.map(async (contact) => {
const account: IContact['account'] & {
keyset?: { pred: BuiltInPredicate; keys: string[] };
} = contact.account;
if (!account || !account.keyset) return Promise.resolve();
return update<IContact>('contact', {
...contact,
account: {
...account,
guard: {
principal: account.address,
pred: account.keyset.pred,
keys: account.keyset.keys,
},
},
});
}),
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { migrateFrom38to39 } from './migrateFrom38to39';
import { migrateFrom39to40 } from './migrateFrom39to40';
import { migrateFrom40to41 } from './migrateFrom40to41';
import { migrateFrom41to42 } from './migrateFrom41to42';
import { migrateFrom42to43 } from './migrateFrom42to43';

const { DB_NAME, DB_VERSION } = config.DB;

Expand All @@ -15,6 +16,7 @@ const migrationMap = {
39: migrateFrom39to40,
40: migrateFrom40to41,
41: migrateFrom41to42,
42: migrateFrom42to43,
};

export async function migration(result: {
Expand Down
28 changes: 20 additions & 8 deletions packages/apps/dev-wallet/src/modules/security/fallback.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,25 @@ export interface SecureContext {
ttl?: number;
}

const getPassword = (sessionEntropy: string, encryptionKey: Uint8Array) => {
const phrase = new TextEncoder().encode(sessionEntropy);
const password = new Uint8Array(phrase.length + encryptionKey.length);
password.set(phrase);
password.set(encryptionKey, phrase.length);
return password;
};

export function fallbackSecurityService() {
let context: SecureContext | null = null;
let clearTimer: NodeJS.Timeout | null = null;
console.log('Service Worker is not available, using fallback service');

async function setSecurityPhrase({
sessionEntropy,
phrase,
keepPolicy,
ttl,
}: {
phrase: string;
keepPolicy: ISetSecurityPhrase['payload']['keepPolicy'];
ttl?: number;
}) {
}: ISetSecurityPhrase['payload']) {
if (clearTimer) {
clearTimeout(clearTimer);
clearTimer = null;
Expand All @@ -32,7 +37,11 @@ export function fallbackSecurityService() {
const encryptionKey = randomBytes(32);
context = {
encryptionKey,
encryptionPhrase: await kadenaEncrypt(encryptionKey, phrase, 'buffer'),
encryptionPhrase: await kadenaEncrypt(
getPassword(sessionEntropy, encryptionKey),
phrase,
'buffer',
),
keepPolicy: keepPolicy,
};
if (context.keepPolicy === 'short-time') {
Expand All @@ -46,12 +55,15 @@ export function fallbackSecurityService() {
return { result: 'success' };
}

async function getSecurityPhrase() {
async function getSecurityPhrase(sessionEntropy: string) {
if (!context) {
return null;
}
return new TextDecoder().decode(
await kadenaDecrypt(context.encryptionKey, context.encryptionPhrase),
await kadenaDecrypt(
getPassword(sessionEntropy, context.encryptionKey),
context.encryptionPhrase,
),
);
}

Expand Down
15 changes: 4 additions & 11 deletions packages/apps/dev-wallet/src/modules/security/security.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,19 @@ import {
import { sendMessageToServiceWorker } from '@/utils/service-worker-com';
import { fallbackSecurityService } from './fallback.service';

async function setSecurityPhrase({
phrase,
keepPolicy,
ttl,
}: {
phrase: string;
keepPolicy: ISetSecurityPhrase['payload']['keepPolicy'];
ttl?: number;
}) {
async function setSecurityPhrase(payload: ISetSecurityPhrase['payload']) {
const { result } = (await sendMessageToServiceWorker({
action: 'setSecurityPhrase',
payload: { phrase, keepPolicy, ttl },
payload,
})) as ISetPhraseResponse;

return { result };
}

async function getSecurityPhrase() {
async function getSecurityPhrase(sessionEntropy: string) {
const { phrase } = (await sendMessageToServiceWorker({
action: 'getSecurityPhrase',
payload: { sessionEntropy },
})) as IGetPhraseResponse;
return phrase;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export type ITransaction = {
export interface TransactionRepository {
getTransactionList: (
profileId: string,
networkUUID: UUID,
networkUUID?: UUID,
status?: TransactionStatus,
) => Promise<ITransaction[]>;
getTransaction: (uuid: string) => Promise<ITransaction>;
Expand All @@ -90,7 +90,7 @@ const createTransactionRepository = ({
return {
getTransactionList: async (
profileId: string,
networkUUID: UUID,
networkUUID?: UUID,
status?: TransactionStatus,
): Promise<ITransaction[]> => {
if (status) {
Expand All @@ -100,11 +100,14 @@ const createTransactionRepository = ({
'network-status',
);
}
return getAll(
'transaction',
IDBKeyRange.only([profileId, networkUUID]),
'network',
);
if (networkUUID) {
return getAll(
'transaction',
IDBKeyRange.only([profileId, networkUUID]),
'network',
);
}
return getAll('transaction', profileId, 'profileId');
},
getTransaction: async (uuid: string): Promise<ITransaction> => {
return getOne('transaction', uuid);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ function usePassword(profile: IProfile | undefined) {
profileRef.current = profile;
const prompt = usePrompt();
const getPassword = useCallback(async () => {
const phrase = await securityService.getSecurityPhrase();
const phrase = await securityService.getSecurityPhrase(
Session.get('sessionId') as string,
);
return phrase;
}, []);

Expand All @@ -87,6 +89,7 @@ function usePassword(profile: IProfile | undefined) {
const { result } = (await securityService.setSecurityPhrase({
phrase: password,
keepPolicy,
sessionEntropy: Session.get('sessionId') as string,
})) as ISetPhraseResponse;
if (result !== 'success') {
throw new Error('Failed to set password');
Expand Down
Loading
Loading