/
CreateAccount.tsx
124 lines (112 loc) · 3.86 KB
/
CreateAccount.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import React, { useMemo, useState } from 'react';
import { useNavigate } from 'react-router-dom';
import { AccountWorkerInput } from './types';
import {
credNumber,
identityIndex,
network,
seedPhraseKey,
selectedIdentityProviderKey,
} from './constants';
import {
getAccountSigningKey,
sendCredentialDeploymentTransaction,
getCryptographicParameters,
getIdentityProviders,
getDefaultTransactionExpiry,
createCredentialDeploymentKeysAndRandomness,
} from './util';
import {
CredentialDeploymentTransaction,
CredentialInputNoSeed,
IdentityObjectV1,
getAccountAddress,
signCredentialTransaction,
} from '@concordium/web-sdk';
const worker = new Worker(new URL('./account-worker.ts', import.meta.url));
export function CreateAccount({ identity }: { identity: IdentityObjectV1 }) {
const navigate = useNavigate();
const [createButtonDisabled, setCreateButtonDisabled] =
useState<boolean>(false);
const seedPhrase = useMemo(() => localStorage.getItem(seedPhraseKey), []);
const selectedIdentityProviderIdentity = useMemo(
() => localStorage.getItem(selectedIdentityProviderKey),
[]
);
async function createAndSendAccount() {
setCreateButtonDisabled(true);
if (!seedPhrase || selectedIdentityProviderIdentity === null) {
setCreateButtonDisabled(false);
return;
}
const selectedIdentityProvider = (await getIdentityProviders()).find(
(idp) =>
idp.ipInfo.ipIdentity ===
Number.parseInt(selectedIdentityProviderIdentity)
);
if (!selectedIdentityProvider) {
setCreateButtonDisabled(false);
return;
}
const listener = (worker.onmessage = async (
e: MessageEvent<CredentialDeploymentTransaction>
) => {
worker.removeEventListener('message', listener);
const credentialDeploymentTransaction = e.data;
const signingKey = getAccountSigningKey(
seedPhrase,
credentialDeploymentTransaction.unsignedCdi.ipIdentity
);
const signature = await signCredentialTransaction(
credentialDeploymentTransaction,
signingKey
);
await sendCredentialDeploymentTransaction(
credentialDeploymentTransaction,
signature
);
const accountAddress = getAccountAddress(
credentialDeploymentTransaction.unsignedCdi.credId
);
navigate(`/account/${accountAddress.address}`);
});
const global = await getCryptographicParameters();
const {
idCredSec,
prfKey,
attributeRandomness,
blindingRandomness,
credentialPublicKeys,
} = createCredentialDeploymentKeysAndRandomness(
seedPhrase,
network,
selectedIdentityProvider.ipInfo.ipIdentity,
identityIndex,
credNumber
);
const credentialInput: CredentialInputNoSeed = {
revealedAttributes: [],
idObject: identity,
globalContext: global,
credNumber,
ipInfo: selectedIdentityProvider.ipInfo,
arsInfos: selectedIdentityProvider.arsInfos,
attributeRandomness,
credentialPublicKeys,
idCredSec,
prfKey,
sigRetrievelRandomness: blindingRandomness,
};
const expiry = getDefaultTransactionExpiry();
const workerInput: AccountWorkerInput = {
credentialInput,
expiry,
};
worker.postMessage(workerInput);
}
return (
<button disabled={createButtonDisabled} onClick={createAndSendAccount}>
Create account
</button>
);
}