Skip to content

Commit 753ad70

Browse files
committed
Create test utils for e2e tests
1 parent 4ced852 commit 753ad70

File tree

2 files changed

+50
-42
lines changed

2 files changed

+50
-42
lines changed

Diff for: examples/bri-3/src/shared/testing/utils.ts

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { keccak256 } from 'ethers/lib/utils';
2+
import * as circomlib from 'circomlibjs';
3+
import * as crypto from 'crypto';
4+
import { ethers } from 'ethers';
5+
6+
export async function createEddsaPrivateKey(
7+
ecdsaPublicKeyOwnerEthereumAccount: string,
8+
signer: ethers.Wallet,
9+
): Promise<string> {
10+
const message = keccak256(ecdsaPublicKeyOwnerEthereumAccount);
11+
const eddsaPrivateKey = await signer.signMessage(message);
12+
13+
return eddsaPrivateKey;
14+
}
15+
16+
export async function createEddsaPublicKey(
17+
eddsaPrivateKey: string,
18+
): Promise<string> {
19+
const eddsa = await circomlib.buildEddsa();
20+
const babyJub = await circomlib.buildBabyjub();
21+
22+
const privateKeyBytes = Buffer.from(eddsaPrivateKey, 'hex');
23+
const publicKeyPoints = eddsa.prv2pub(privateKeyBytes);
24+
const eddsaPublicKey = Buffer.from(
25+
babyJub.packPoint(publicKeyPoints),
26+
).toString('hex');
27+
28+
return eddsaPublicKey;
29+
}
30+
31+
export async function createEddsaSignature(
32+
payload: any,
33+
eddsaPrivateKey: string,
34+
): Promise<string> {
35+
const eddsa = await circomlib.buildEddsa();
36+
const hashedPayload = crypto
37+
.createHash('sha256')
38+
.update(JSON.stringify(payload))
39+
.digest();
40+
41+
const eddsaSignature = eddsa.signPedersen(eddsaPrivateKey, hashedPayload);
42+
const packedSignature = eddsa.packSignature(eddsaSignature);
43+
const signature = Buffer.from(packedSignature).toString('hex');
44+
return signature;
45+
}

Diff for: examples/bri-3/test/sriUseCase.e2e-spec.ts

+5-42
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ import { ethers } from 'ethers';
44
import * as request from 'supertest';
55
import { v4 } from 'uuid';
66
import { AppModule } from '../src/app.module';
7-
import { keccak256 } from 'ethers/lib/utils';
8-
import * as circomlib from 'circomlibjs';
9-
import * as crypto from 'crypto';
107
import {
118
supplierBpiSubjectEcdsaPublicKey,
129
supplierBpiSubjectEcdsaPrivateKey,
@@ -15,6 +12,11 @@ import {
1512
internalBpiSubjectEcdsaPublicKey,
1613
internalBpiSubjectEcdsaPrivateKey,
1714
} from '../src/shared/testing/constants';
15+
import {
16+
createEddsaPrivateKey,
17+
createEddsaPublicKey,
18+
createEddsaSignature,
19+
} from '../src/shared/testing/utils';
1820

1921
jest.setTimeout(120000);
2022

@@ -368,42 +370,3 @@ async function fetchBpiAccount(bpiAccountId: string): Promise<any> {
368370

369371
return JSON.parse(getBpiAccountResponse.text);
370372
}
371-
372-
async function createEddsaPrivateKey(
373-
ecdsaPublicKeyOwnerEthereumAccount: string,
374-
signer: ethers.Wallet,
375-
): Promise<string> {
376-
const message = keccak256(ecdsaPublicKeyOwnerEthereumAccount);
377-
const eddsaPrivateKey = await signer.signMessage(message);
378-
379-
return eddsaPrivateKey;
380-
}
381-
382-
async function createEddsaPublicKey(eddsaPrivateKey: string): Promise<string> {
383-
const eddsa = await circomlib.buildEddsa();
384-
const babyJub = await circomlib.buildBabyjub();
385-
386-
const privateKeyBytes = Buffer.from(eddsaPrivateKey, 'hex');
387-
const publicKeyPoints = eddsa.prv2pub(privateKeyBytes);
388-
const eddsaPublicKey = Buffer.from(
389-
babyJub.packPoint(publicKeyPoints),
390-
).toString('hex');
391-
392-
return eddsaPublicKey;
393-
}
394-
395-
async function createEddsaSignature(
396-
payload: any,
397-
eddsaPrivateKey: string,
398-
): Promise<string> {
399-
const eddsa = await circomlib.buildEddsa();
400-
const hashedPayload = crypto
401-
.createHash('sha256')
402-
.update(JSON.stringify(payload))
403-
.digest();
404-
405-
const eddsaSignature = eddsa.signPedersen(eddsaPrivateKey, hashedPayload);
406-
const packedSignature = eddsa.packSignature(eddsaSignature);
407-
const signature = Buffer.from(packedSignature).toString('hex');
408-
return signature;
409-
}

0 commit comments

Comments
 (0)