Skip to content

Commit

Permalink
Create test utils for e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
biscuitdey authored and ognjenkurtic committed Feb 28, 2024
1 parent c07fe54 commit 6a3239f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 42 deletions.
45 changes: 45 additions & 0 deletions examples/bri-3/src/shared/testing/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,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;
}
47 changes: 5 additions & 42 deletions examples/bri-3/test/sriUseCase.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ import { ethers } from 'ethers';
import * as request from 'supertest';
import { v4 } from 'uuid';
import { AppModule } from '../src/app.module';
import { keccak256 } from 'ethers/lib/utils';
import * as circomlib from 'circomlibjs';
import * as crypto from 'crypto';
import {
supplierBpiSubjectEcdsaPublicKey,
supplierBpiSubjectEcdsaPrivateKey,
Expand All @@ -15,6 +12,11 @@ import {
internalBpiSubjectEcdsaPublicKey,
internalBpiSubjectEcdsaPrivateKey,
} from '../src/shared/testing/constants';
import {
createEddsaPrivateKey,
createEddsaPublicKey,
createEddsaSignature,
} from '../src/shared/testing/utils';

jest.setTimeout(120000);

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

return JSON.parse(getBpiAccountResponse.text);
}

async function createEddsaPrivateKey(
ecdsaPublicKeyOwnerEthereumAccount: string,
signer: ethers.Wallet,
): Promise<string> {
const message = keccak256(ecdsaPublicKeyOwnerEthereumAccount);
const eddsaPrivateKey = await signer.signMessage(message);

return eddsaPrivateKey;
}

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;
}

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;
}

0 comments on commit 6a3239f

Please sign in to comment.