Skip to content

Commit

Permalink
Handle bigints in backup
Browse files Browse the repository at this point in the history
  • Loading branch information
Hjort committed Aug 23, 2023
1 parent 7327762 commit 345000e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { fullscreenPromptContext } from '@popup/page-layouts/FullscreenPromptLay
import { noOp } from 'wallet-common-helpers';
import { decrypt } from '@shared/utils/crypto';
import { useHdWallet } from '@popup/shared/utils/account-helpers';
import JSONBigInt from 'json-bigint';
import { VerifiableCredentialCardWithStatusFromChain } from '../VerifiableCredential/VerifiableCredentialList';
import { ExportFormat, VerifiableCredentialExport } from './utils';

Expand Down Expand Up @@ -40,8 +41,16 @@ function DisplayResult({ imported }: { imported: VerifiableCredential[] }) {
}

async function parseExport(data: EncryptedData, encryptionKey: string): Promise<VerifiableCredentialExport> {
// TODO handle bigints
const backup: ExportFormat = JSON.parse(await decrypt(data, encryptionKey));
const decrypted = await decrypt(data, encryptionKey);
const backup: ExportFormat = JSONBigInt({
alwaysParseAsBig: true,
useNativeBigInt: true,
}).parse(decrypted);
// Change index to number, due to parse changing all numbers to bigints.
backup.value.verifiableCredentials = backup.value.verifiableCredentials.map((v) => ({
...v,
index: Number(v.index),
}));
// TODO validation
return backup.value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
} from '@popup/store/verifiable-credential';
import { networkConfigurationAtom } from '@popup/store/settings';
import { saveData } from '@popup/shared/utils/file-helpers';
import { stringify } from 'json-bigint';

export type VerifiableCredentialExport = {
verifiableCredentials: VerifiableCredential[];
Expand Down Expand Up @@ -43,8 +44,8 @@ function createExport(
},
};

// TODO handle bigints
return encrypt(JSON.stringify(exportContent), encryptionKey);
// Use json-bigint to serialize bigints as json numbers.
return encrypt(stringify(exportContent), encryptionKey);
}

export function useVerifiableCredentialExport() {
Expand Down

0 comments on commit 345000e

Please sign in to comment.