-
Notifications
You must be signed in to change notification settings - Fork 613
Expand file tree
/
Copy pathaccount-utils.ts
More file actions
61 lines (54 loc) · 1.78 KB
/
Copy pathaccount-utils.ts
File metadata and controls
61 lines (54 loc) · 1.78 KB
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
/**
* Shared account contract instantiation logic.
*
* Centralizes the key derivation + contract setup sequence used by
* account creation, deployment, and registration.
*/
import { getAztecCore } from "./aztec-imports";
/**
* Derives keys and instantiates a Schnorr account contract from a secret and salt.
*
* Returns everything needed for PXE registration and deployment:
* - secretFr / saltFr — field element versions of the inputs
* - publicKeys — derived public keys
* - signingKey — Schnorr signing key
* - accountContract — the SchnorrAccountContract instance
* - artifact — the contract artifact
* - instance — the contract instance with computed address
*/
export async function instantiateAccount(secret: string, salt: string) {
const {
Fr,
GrumpkinScalar,
deriveKeys,
deriveSecretKeyFromSigningKey,
SchnorrAccountContract,
getContractInstanceFromInstantiationParams,
} = await getAztecCore();
const signingKey = GrumpkinScalar.fromString(secret);
const saltFr = Fr.fromString(salt);
const secretFr = await deriveSecretKeyFromSigningKey(signingKey);
const { publicKeys } = await deriveKeys(secretFr);
const accountContract = new SchnorrAccountContract(signingKey);
const initInfo = await accountContract.getInitializationFunctionAndArgs();
const { constructorName, constructorArgs } = initInfo ?? {
constructorName: undefined,
constructorArgs: undefined,
};
const artifact = await accountContract.getContractArtifact();
const instance = await getContractInstanceFromInstantiationParams(artifact, {
constructorArtifact: constructorName,
constructorArgs,
salt: saltFr,
publicKeys,
});
return {
secretFr,
saltFr,
publicKeys,
signingKey,
accountContract,
artifact,
instance,
};
}