-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #579 from Concordium/ui-update/web3-import-export
UI update/web3 import export
- Loading branch information
Showing
16 changed files
with
514 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
v18 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
142 changes: 132 additions & 10 deletions
142
packages/browser-wallet/src/popup/popupX/pages/Web3Id/Web3IdImport.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
packages/browser-wallet/src/popup/popupX/pages/Web3Id/utils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { useHdWallet } from '@popup/shared/utils/account-helpers'; | ||
import { NetworkConfiguration, VerifiableCredential, VerifiableCredentialSchema } from '@shared/storage/types'; | ||
import { VerifiableCredentialMetadata } from '@shared/utils/verifiable-credential-helpers'; | ||
import { encrypt } from '@shared/utils/crypto'; | ||
import { getNet } from '@shared/utils/network-helpers'; | ||
import { useAtomValue } from 'jotai'; | ||
import { | ||
storedVerifiableCredentialSchemasAtom, | ||
storedVerifiableCredentialsAtom, | ||
storedVerifiableCredentialMetadataAtom, | ||
} 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[]; | ||
schemas: Record<string, VerifiableCredentialSchema>; | ||
metadata: Record<string, VerifiableCredentialMetadata>; | ||
}; | ||
|
||
export type ExportFormat = { | ||
type: 'concordium-browser-wallet-verifiable-credentials'; | ||
v: number; | ||
environment: string; // 'testnet' or 'mainnet' | ||
value: VerifiableCredentialExport; | ||
}; | ||
|
||
function createExport( | ||
verifiableCredentials: VerifiableCredential[], | ||
schemas: Record<string, VerifiableCredentialSchema>, | ||
metadata: Record<string, VerifiableCredentialMetadata>, | ||
network: NetworkConfiguration, | ||
encryptionKey: string | ||
) { | ||
const exportContent: ExportFormat = { | ||
type: 'concordium-browser-wallet-verifiable-credentials', | ||
v: 0, | ||
environment: getNet(network).toLowerCase(), | ||
value: { | ||
verifiableCredentials, | ||
schemas, | ||
metadata, | ||
}, | ||
}; | ||
|
||
// Use json-bigint to serialize bigints as json numbers. | ||
// Ensure that Dates are stored as timestamps to not lose typing (otherwise they are serialized as strings). | ||
return encrypt(stringify(exportContent), encryptionKey); | ||
} | ||
|
||
export function useVerifiableCredentialExport() { | ||
const verifiableCredentials = useAtomValue(storedVerifiableCredentialsAtom); | ||
const schemas = useAtomValue(storedVerifiableCredentialSchemasAtom); | ||
const metadata = useAtomValue(storedVerifiableCredentialMetadataAtom); | ||
const network = useAtomValue(networkConfigurationAtom); | ||
const wallet = useHdWallet(); | ||
|
||
if (schemas.loading || verifiableCredentials.loading || metadata.loading || !wallet) { | ||
return undefined; | ||
} | ||
|
||
const handleExport = async () => { | ||
const key = wallet.getVerifiableCredentialBackupEncryptionKey().toString('hex'); | ||
const data = await createExport(verifiableCredentials.value, schemas.value, metadata.value, network, key); | ||
saveData(data, `web3IdCredentials.export`); | ||
}; | ||
|
||
return handleExport; | ||
} |
Oops, something went wrong.