Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update e2e to accept eddsa signatures #784

Merged
merged 9 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions examples/bri-3/src/shared/testing/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export const supplierBpiSubjectEcdsaPublicKey =
'0x047a197a795a747c154dd92b217a048d315ef9ca1bfa9c15bfefe4e02fb338a70af23e7683b565a8dece5104a85ed24a50d791d8c5cb09ee21aabc927c98516539';
export const supplierBpiSubjectEcdsaPrivateKey =
'0x93b7ed4405c73a1dbd8936e67419ee4e711ed44225aeabe9a5acf49a9ec90e68';
export const buyerBpiSubjectEcdsaPublicKey =
'0x04203db7d27bab8d711acc52479efcfa9d7846e4e176d82389689f95cf06a51818b0b9ab1c2c8d72f1a32e236e6296c91c922a0dc3d0cb9afc269834fc5646b980';
export const buyerBpiSubjectEcdsaPrivateKey =
'0x32c8d8f4e53cd1920d1ad22b9d51a7b28216337f2b664fb8d33bbcfc3c455c62';
export const internalBpiSubjectEcdsaPublicKey =
'0x044e851fa6118d0d33f11ebf8d4cae2a25dca959f06c1ab87b8fec9ccbf0ca0021b7efc27c786f9480f9f11cfe8df1ae991329654308611148a35a2277ba5909fe';
export const internalBpiSubjectEcdsaPrivateKey =
'0x0fbdb56ab0fecb2f406fa807d9e6558baedacc1c15c0e2703b77d4c08441e4fe';
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;
}
82 changes: 59 additions & 23 deletions examples/bri-3/test/sriUseCase.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,30 @@ import { ethers } from 'ethers';
import * as request from 'supertest';
import { v4 } from 'uuid';
import { AppModule } from '../src/app.module';
import {
supplierBpiSubjectEcdsaPublicKey,
supplierBpiSubjectEcdsaPrivateKey,
buyerBpiSubjectEcdsaPublicKey,
buyerBpiSubjectEcdsaPrivateKey,
internalBpiSubjectEcdsaPublicKey,
internalBpiSubjectEcdsaPrivateKey,
} from '../src/shared/testing/constants';
import {
createEddsaPrivateKey,
createEddsaPublicKey,
createEddsaSignature,
} from '../src/shared/testing/utils';

jest.setTimeout(120000);

let accessToken: string;
let app: INestApplication;
let server: any;

const supplierBPiSubjectPublicKey =
'0x047a197a795a747c154dd92b217a048d315ef9ca1bfa9c15bfefe4e02fb338a70af23e7683b565a8dece5104a85ed24a50d791d8c5cb09ee21aabc927c98516539';
const supplierBPiSubjectPrivateKey =
'0x93b7ed4405c73a1dbd8936e67419ee4e711ed44225aeabe9a5acf49a9ec90e68';
const buyerBPiSubjectPublicKey =
'0x04203db7d27bab8d711acc52479efcfa9d7846e4e176d82389689f95cf06a51818b0b9ab1c2c8d72f1a32e236e6296c91c922a0dc3d0cb9afc269834fc5646b980';
const buyerBPiSubjectPrivateKey =
'0x32c8d8f4e53cd1920d1ad22b9d51a7b28216337f2b664fb8d33bbcfc3c455c62';

let supplierBpiSubjectEddsaPublicKey: string;
let supplierBpiSubjectEddsaPrivateKey: string;
let buyerBpiSubjectEddsaPublicKey: string;
let buyerBpiSubjectEddsaPrivateKey: string;
let createdWorkgroupId: string;
let createdWorkstepId: string;
let createdWorkflowId: string;
Expand All @@ -35,6 +43,29 @@ describe('SRI use-case end-to-end test', () => {
app = moduleFixture.createNestApplication();
await app.init();
server = app.getHttpServer();

const supplierWallet = new ethers.Wallet(supplierBpiSubjectEcdsaPrivateKey);
supplierBpiSubjectEddsaPrivateKey = await createEddsaPrivateKey(
supplierBpiSubjectEcdsaPublicKey,
supplierWallet,
);

supplierBpiSubjectEddsaPublicKey = await createEddsaPublicKey(
supplierBpiSubjectEddsaPrivateKey,
);

const buyerWallet = new ethers.Wallet(
buyerBpiSubjectEcdsaPrivateKey,
undefined,
);
buyerBpiSubjectEddsaPrivateKey = await createEddsaPrivateKey(
buyerBpiSubjectEcdsaPublicKey,
buyerWallet,
);

buyerBpiSubjectEddsaPublicKey = await createEddsaPublicKey(
buyerBpiSubjectEddsaPrivateKey,
);
});

afterAll(async () => {
Expand All @@ -49,7 +80,10 @@ describe('SRI use-case end-to-end test', () => {
const createdBpiSubjectSupplierId =
await createExternalBpiSubjectAndReturnId(
'External Bpi Subject - Supplier',
supplierBPiSubjectPublicKey,
[
{ type: 'ecdsa', value: supplierBpiSubjectEcdsaPublicKey },
{ type: 'eddsa', value: supplierBpiSubjectEddsaPublicKey },
],
);

createdBpiSubjectAccountSupplierId =
Expand All @@ -60,7 +94,10 @@ describe('SRI use-case end-to-end test', () => {

const createdBpiSubjectBuyerId = await createExternalBpiSubjectAndReturnId(
'External Bpi Subject 2 - Buyer',
buyerBPiSubjectPublicKey,
[
{ type: 'ecdsa', value: buyerBpiSubjectEcdsaPublicKey },
{ type: 'eddsa', value: buyerBpiSubjectEddsaPublicKey },
],
);

createdBpiSubjectAccountBuyerId = await createBpiSubjectAccountAndReturnId(
Expand Down Expand Up @@ -113,7 +150,7 @@ describe('SRI use-case end-to-end test', () => {
createdWorkflowId,
createdWorkstepId,
createdBpiSubjectAccountSupplierId,
supplierBPiSubjectPrivateKey,
supplierBpiSubjectEddsaPrivateKey,
createdBpiSubjectAccountBuyerId,
`{
"supplierInvoiceID": "INV123",
Expand Down Expand Up @@ -145,27 +182,26 @@ describe('SRI use-case end-to-end test', () => {
});

async function loginAsInternalBpiSubjectAndReturnAnAccessToken(): Promise<string> {
// These two values must be inline with the value for the bpiAdmin from seed.ts
// internalBpiSubjectEcdsaPublicKey & internalBpiSubjectEcdsaPrivateKey must be inline with the value for the bpiAdmin from seed.ts
// These values are used for testing purposes only
const internalBpiSubjectPublicKey =
'0x044e851fa6118d0d33f11ebf8d4cae2a25dca959f06c1ab87b8fec9ccbf0ca0021b7efc27c786f9480f9f11cfe8df1ae991329654308611148a35a2277ba5909fe';
const internalBpiSubjectPrivateKey =
'0x0fbdb56ab0fecb2f406fa807d9e6558baedacc1c15c0e2703b77d4c08441e4fe';

const nonceResponse = await request(server)
.post('/auth/nonce')
.send({ publicKey: internalBpiSubjectPublicKey })
.send({ publicKey: internalBpiSubjectEcdsaPublicKey })
.expect(201);

const signer = new ethers.Wallet(internalBpiSubjectPrivateKey, undefined);
const signer = new ethers.Wallet(
internalBpiSubjectEcdsaPrivateKey,
undefined,
);
const signature = await signer.signMessage(nonceResponse.text);

const loginResponse = await request(server)
.post('/auth/login')
.send({
message: nonceResponse.text,
signature: signature,
publicKey: internalBpiSubjectPublicKey,
publicKey: internalBpiSubjectEcdsaPublicKey,
})
.expect(201);

Expand All @@ -174,7 +210,7 @@ async function loginAsInternalBpiSubjectAndReturnAnAccessToken(): Promise<string

async function createExternalBpiSubjectAndReturnId(
bpiSubjectName: string,
pk: string,
pk: { type: string; value: string }[],
): Promise<string> {
const createdBpiSubjectResponse = await request(server)
.post('/subjects')
Expand Down Expand Up @@ -296,8 +332,8 @@ async function createTransactionAndReturnId(
toSubjectAccountId: string,
payload: string,
): Promise<string> {
const signer = new ethers.Wallet(fromPrivatekey, undefined);
const signature = await signer.signMessage(payload);
//Eddsa signature
const signature = await createEddsaSignature(payload, fromPrivatekey);

const createdTransactionResponse = await request(server)
.post('/transactions')
Expand Down
Loading