-
Notifications
You must be signed in to change notification settings - Fork 238
/
Copy pathutils.ts
45 lines (38 loc) · 1.35 KB
/
utils.ts
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
import { keccak256 } from 'ethers/lib/utils';
import * as circomlib from 'circomlibjs';
import * as crypto from 'crypto';
import { ethers } from 'ethers';
export async function createEddsaPrivateKey(
ecdsaPublicKeyOwnerEthereumAccount: string,
signer: ethers.Wallet,
): Promise<string> {
const message = keccak256(ecdsaPublicKeyOwnerEthereumAccount);
const eddsaPrivateKey = await signer.signMessage(message);
return eddsaPrivateKey;
}
export async function createEddsaPublicKey(
eddsaPrivateKey: string,
): Promise<string> {
const eddsa = await circomlib.buildEddsa();
const babyJub = await circomlib.buildBabyjub();
const privateKeyBytes = Buffer.from(eddsaPrivateKey, 'hex');
const publicKeyPoints = eddsa.prv2pub(privateKeyBytes);
const eddsaPublicKey = Buffer.from(
babyJub.packPoint(publicKeyPoints),
).toString('hex');
return eddsaPublicKey;
}
export async function createEddsaSignature(
payload: any,
eddsaPrivateKey: string,
): Promise<string> {
const eddsa = await circomlib.buildEddsa();
const hashedPayload = crypto
.createHash('sha256')
.update(JSON.stringify(payload))
.digest();
const eddsaSignature = eddsa.signPedersen(eddsaPrivateKey, hashedPayload);
const packedSignature = eddsa.packSignature(eddsaSignature);
const signature = Buffer.from(packedSignature).toString('hex');
return signature;
}