Skip to content

Commit

Permalink
Serialize web3IdCredentials before storage, to support bigints
Browse files Browse the repository at this point in the history
  • Loading branch information
Hjort committed Aug 22, 2023
1 parent ff5deee commit 7327762
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export default function AddWeb3IdCredential({ onAllow, onReject }: Props) {
}
// Find the next unused index
// TODO verify index is unused on chain?
const index = [...(web3IdCredentials || []), ...(storedWeb3IdCredentials.value || [])].reduce(
const index = [...web3IdCredentials.value, ...storedWeb3IdCredentials.value].reduce(
(best, cred) => (cred.issuer === credential.issuer ? Math.max(cred.index + 1, best) : best),
0
);
Expand All @@ -128,7 +128,7 @@ export default function AddWeb3IdCredential({ onAllow, onReject }: Props) {
id: createCredentialId(credentialHolderId, issuer, network),
index,
};
await setWeb3IdCredentials([...(web3IdCredentials || []), fullCredential]);
await setWeb3IdCredentials([...web3IdCredentials.value, fullCredential]);
if (metadata) {
const newMetadata = { ...verifiableCredentialMetadata.value };
newMetadata[metadataUrl.url] = metadata;
Expand All @@ -137,6 +137,10 @@ export default function AddWeb3IdCredential({ onAllow, onReject }: Props) {
return credentialSubjectId;
}

if (web3IdCredentials.loading || storedWeb3IdCredentials.loading) {
return null;
}

const urlDisplay = displayUrl(url);
return (
<ExternalRequestLayout>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ export const storedVerifiableCredentialMetadataAtom = atomWithChromeStorage<
>(ChromeStorageKey.VerifiableCredentialMetadata, {}, true);

export const sessionTemporaryVerifiableCredentialsAtom = atomWithChromeStorage<
Omit<VerifiableCredential, 'signature' | 'randomness'>[] | undefined
>(ChromeStorageKey.TemporaryVerifiableCredentials, []);
Omit<VerifiableCredential, 'signature' | 'randomness'>[]
>(ChromeStorageKey.TemporaryVerifiableCredentials, [], true);
44 changes: 42 additions & 2 deletions packages/browser-wallet/src/shared/storage/access.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { stringify } from '@concordium/browser-wallet-api/src/util';
import { parse } from '@shared/utils/payload-helpers';
import { VerifiableCredentialMetadata } from '@shared/utils/verifiable-credential-helpers';
import {
ChromeStorageKey,
Expand Down Expand Up @@ -111,6 +113,44 @@ export function useIndexedStorage<V>(
};
}

/**
* Factory function for creating a StorageAccessor, which serializes before storing the value and deserializes before loading the value, from a key.
*
* @param area storeage area to store value in
* @param key key used to store value
*/
export function makeSerializedStorageAccessor<V>(
area: chrome.storage.AreaName,
key: ChromeStorageKey
): StorageAccessor<V> {
const inner = makeStorageAccessor<string | undefined>(area, key);
return {
get: (): Promise<V | undefined> => inner.get().then((v) => (v !== undefined ? parse(v) : undefined)),
set: (value: V) => inner.set(stringify(value)),
remove: () => inner.remove(),
area,
};
}

/**
* Factory function for creating an IndexedStorageAccessor, which serializes before storing the value and deserializes before loading the value, from a key.
*
* @param area storage area to store value in
* @param key key used to store value
*/
export function makeSerializedAndIndexedStorageAccessor<Value>(
area: chrome.storage.AreaName,
key: ChromeStorageKey
): IndexedStorageAccessor<Value> {
const inner = makeIndexedStorageAccessor<string | undefined>(area, key);
return {
get: (index: string) => inner.get(index).then((s) => (s !== undefined ? parse(s) : undefined)),
set: (index: string, value: Value) => inner.set(index, stringify(value)),
remove: (index: string) => inner.remove(index),
area,
};
}

export const storedCurrentNetwork = makeStorageAccessor<NetworkConfiguration>(
'local',
ChromeStorageKey.NetworkConfiguration
Expand Down Expand Up @@ -148,7 +188,7 @@ export const storedTokenMetadata = makeStorageAccessor<Record<string, TokenMetad
ChromeStorageKey.TokenMetadata
);
export const storedAcceptedTerms = makeStorageAccessor<AcceptedTermsState>('local', ChromeStorageKey.AcceptedTerms);
export const storedVerifiableCredentials = makeIndexedStorageAccessor<VerifiableCredential[]>(
export const storedVerifiableCredentials = makeSerializedAndIndexedStorageAccessor<VerifiableCredential[]>(
'local',
ChromeStorageKey.VerifiableCredentials
);
Expand Down Expand Up @@ -187,6 +227,6 @@ export const sessionPendingTransactions = makeIndexedStorageAccessor<string[]>(
'session',
ChromeStorageKey.PendingTransactions
);
export const sessionVerifiableCredentials = makeIndexedStorageAccessor<
export const sessionVerifiableCredentials = makeSerializedAndIndexedStorageAccessor<
Omit<VerifiableCredential, 'signature' | 'randomness'>[]
>('session', ChromeStorageKey.TemporaryVerifiableCredentials);

0 comments on commit 7327762

Please sign in to comment.