Skip to content

Commit

Permalink
Build fix
Browse files Browse the repository at this point in the history
  • Loading branch information
biscuitdey committed Apr 12, 2024
1 parent b2080cc commit 9f3a896
Show file tree
Hide file tree
Showing 12 changed files with 85 additions and 55 deletions.
39 changes: 28 additions & 11 deletions examples/bri-3/prisma/seed.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { PrismaClient } from '@prisma/client';
import { PrismaClient, PublicKeyType } from '@prisma/client';
import { type } from 'os';

const prisma = new PrismaClient();

Expand All @@ -23,17 +24,33 @@ async function main() {

await prisma.bpiSubject.create({
data: {
name: 'BpiAdmin',
description: 'Internal Bpi Subject of this Bpi',
publicKey: '0x044e851fa6118d0d33f11ebf8d4cae2a25dca959f06c1ab87b8fec9ccbf0ca0021b7efc27c786f9480f9f11cfe8df1ae991329654308611148a35a2277ba5909fe',
// private key 0x0fbdb56ab0fecb2f406fa807d9e6558baedacc1c15c0e2703b77d4c08441e4fe, used for testing purposes only
loginNonce: '',
roles: {
connect: {
id: internalBpiSubjectRole.id
},
name: 'BpiAdmin',
description: 'Internal Bpi Subject of this Bpi',
publicKeys: {
createMany: {
data: [
{
type: PublicKeyType.ECDSA,
value:
'0x044e851fa6118d0d33f11ebf8d4cae2a25dca959f06c1ab87b8fec9ccbf0ca0021b7efc27c786f9480f9f11cfe8df1ae991329654308611148a35a2277ba5909fe',
},
{
type: PublicKeyType.EDDSA,
value:
'0x044e851fa6118d0d33f11ebf8d4cae2a25dca959f06c1ab87b8fec9ccbf0ca0021b7efc27c786f9480f9f11cfe8df1ae991329654308611148a35a2277ba5909fe',
},
],
},
}
},

// private key 0x0fbdb56ab0fecb2f406fa807d9e6558baedacc1c15c0e2703b77d4c08441e4fe, used for testing purposes only
loginNonce: '',
roles: {
connect: {
id: internalBpiSubjectRole.id,
},
},
},
});
}
main()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class CreateBpiMessageCommandHandler
this.authAgent.throwIfSignatureVerificationFails(
command.content,
command.signature,
fromBpiSubject.publicKey,
fromBpiSubject.publicKeys[0].value,
);

const newBpiMessageCandidate = this.agent.createNewBpiMessage(
Expand All @@ -47,7 +47,7 @@ export class CreateBpiMessageCommandHandler
);

await this.messagingAgent.publishMessage(
toBpiSubject.publicKey,
toBpiSubject.publicKeys[0].value,
this.messagingAgent.serializeBpiMessage(newBpiMessage),
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class ProcessInboundMessageCommandHandler
const isSignatureValid = this.authAgent.verifySignatureAgainstPublicKey(
command.content,
command.signature,
fromBpiSubject.publicKey,
fromBpiSubject.publicKeys[0].value,
);

if (!isSignatureValid) {
Expand All @@ -55,7 +55,7 @@ export class ProcessInboundMessageCommandHandler
);

await this.messagingAgent.publishMessage(
toBpiSubject.publicKey,
toBpiSubject.publicKeys[0].value,
this.messagingAgent.serializeBpiMessage(newBpiMessage),
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
} from '../api/err.messages';
import { BpiSubjectStorageAgent } from './bpiSubjectsStorage.agent';
import { BpiSubjectRoleName } from '../models/bpiSubjectRole';
import { PublicKeyDto } from '../api/dtos/request/publicKey.dto';
import { PublicKey, PublicKeyType } from '../models/publicKey';

// Agent methods have extremely declarative names and perform a single task
@Injectable()
Expand All @@ -27,11 +29,36 @@ export class BpiSubjectAgent {
public async createNewExternalBpiSubject(
name: string,
description: string,
publicKeys: PublicKeyDto[],
): Promise<BpiSubject> {
const externalRole = await this.storageAgent.getBpiSubjectRoleByName(
BpiSubjectRoleName.EXTERNAL_BPI_SUBJECT,
);
return new BpiSubject(v4(), name, description, [], [externalRole]);

const bpiSubjectId = v4();

const publicKeyCandidates = publicKeys.map((key) => {
let publicKeyType;

switch (key.type.toLowerCase()) {
case 'ecdsa':
publicKeyType = PublicKeyType.ECDSA;
break;
case 'eddsa':
publicKeyType = PublicKeyType.EDDSA;
break;
default:
}
return new PublicKey(v4(), publicKeyType, key.value, bpiSubjectId);
});

return new BpiSubject(
bpiSubjectId,
name,
description,
publicKeyCandidates,
[externalRole],
);
}

public async fetchUpdateCandidateAndThrowIfUpdateValidationFails(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,7 @@ export class BpiSubjectStorageAgent extends PrismaService {
}),
},
publicKeys: {
connect: bpiSubject.publicKeys.map((pk) => {
return {
id: pk.id,
};
}),
createMany: { data: bpiSubject.publicKeys },
},
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import { CommandHandler, ICommandHandler } from '@nestjs/cqrs';
import { BpiSubjectAgent } from '../../agents/bpiSubjects.agent';
import { BpiSubjectStorageAgent } from '../../agents/bpiSubjectsStorage.agent';
import { CreateBpiSubjectCommand } from './createBpiSubject.command';
import { PublicKeyType } from '../../models/publicKey';
import { v4 } from 'uuid';

@CommandHandler(CreateBpiSubjectCommand)
export class CreateBpiSubjectCommandHandler
Expand All @@ -20,34 +18,13 @@ export class CreateBpiSubjectCommandHandler
const newBpiSubjectCandidate = await this.agent.createNewExternalBpiSubject(
command.name,
command.description,
command.publicKeys,
);

const newBpiSubject = await this.storageAgent.storeNewBpiSubject(
newBpiSubjectCandidate,
);

const newPublicKeys = await Promise.all(
command.publicKeys.map(async (key) => {
let publicKeyType;
switch (key.type.toLowerCase()) {
case 'ecdsa':
publicKeyType = PublicKeyType.ECDSA;
break;
case 'eddsa':
publicKeyType = PublicKeyType.EDDSA;
break;
default:
}

return await this.storageAgent.storePublicKey(
v4(),
publicKeyType,
key.value,
newBpiSubject.id,
);
}),
);

return newBpiSubject.id;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ export class TransactionAgent {
const isSignatureValid = this.authAgent.verifySignatureAgainstPublicKey(
tx.payload,
tx.signature,
tx.fromBpiSubjectAccount.ownerBpiSubject.publicKey,
tx.fromBpiSubjectAccount.ownerBpiSubject.publicKeys[0].value,
);

if (!isSignatureValid) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class ProcessInboundTransactionCommandHandler
const isSignatureValid = this.authAgent.verifySignatureAgainstPublicKey(
command.payload,
command.signature,
subjectAccounts[0].ownerBpiSubject.publicKey,
subjectAccounts[0].ownerBpiSubject.publicKeys[0].value,
);

if (!isSignatureValid) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,14 @@ export class WorkstepExecutedEventHandler
);

// Change channels to publish message here
const channels = [event.tx.fromBpiSubjectAccount.ownerBpiSubject.publicKey];
const channels = [
event.tx.fromBpiSubjectAccount.ownerBpiSubject.publicKeys[0].value,
];

if (event.status == 'Success') {
channels.push(event.tx.toBpiSubjectAccount.ownerBpiSubject.publicKey);
channels.push(
event.tx.toBpiSubjectAccount.ownerBpiSubject.publicKeys[0].value,
);
}

await this.publishMessage(channels, bpiMessage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ export const computeEcdsaSigPublicInputs = (tx: Transaction) => {
ethers.utils.arrayify(ethers.utils.hashMessage(tx.payload)),
);

const publicKey = tx.fromBpiSubjectAccount.ownerBpiSubject.publicKey;
const publicKey =
tx.fromBpiSubjectAccount.ownerBpiSubject.publicKeys[0].value;

return computeEffectiveEcdsaSigPublicInputs(
ecdsaSignature,
Expand Down Expand Up @@ -113,7 +114,7 @@ export const computeEddsaSigPublicInputs = async (tx: Transaction) => {
.digest();

const publicKey =
tx.fromBpiSubjectAccount.ownerBpiSubject.publicKey.split(',');
tx.fromBpiSubjectAccount.ownerBpiSubject.publicKeys[0].value.split(',');

const publicKeyPoints = [
Uint8Array.from(Buffer.from(publicKey[0], 'hex')),
Expand Down
5 changes: 3 additions & 2 deletions examples/bri-3/src/shared/testing/builders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { BpiMerkleTree } from '../../bri/merkleTree/models/bpiMerkleTree';
import { Workflow } from '../../bri/workgroup/workflows/models/workflow';
import { Workgroup } from '../../bri/workgroup/workgroups/models/workgroup';
import { Workstep } from '../../bri/workgroup/worksteps/models/workstep';
import { PublicKey } from '../../bri/identity/bpiSubjects/models/publicKey';

export class WorkstepBuilder {
private id: string;
Expand Down Expand Up @@ -121,7 +122,7 @@ export class BpiSubjectBuilder {
private id: string;
private name: string;
private description: string;
private publicKey: string;
private publicKey: PublicKey[];
private loginNonce: string;
private roles: BpiSubjectRole[];

Expand All @@ -142,7 +143,7 @@ export class BpiSubjectBuilder {
return this;
}

setPublicKey(publicKey: string): BpiSubjectBuilder {
setPublicKey(publicKey: PublicKey[]): BpiSubjectBuilder {
this.publicKey = publicKey;
return this;
}
Expand Down
11 changes: 9 additions & 2 deletions examples/bri-3/src/shared/testing/testData.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ import {
WorkgroupBuilder,
WorkstepBuilder,
} from './builders';
import {
PublicKey,
PublicKeyType,
} from '../../bri/identity/bpiSubjects/models/publicKey';

// A place to encapsulate creation of test data objects used for controller testing.
// These objects will later be used to mock prisma.client calls only once during test bootstrap
Expand Down Expand Up @@ -51,11 +55,14 @@ export class TestDataHelper {
};

public static createBpiSubject = () => {
const bpiSubjectId = uuid();
const bpiSubject = new BpiSubjectBuilder()
.setId(uuid())
.setId(bpiSubjectId)
.setName('name')
.setDescription('desc')
.setPublicKey('pk')
.setPublicKey([
new PublicKey(uuid(), PublicKeyType.ECDSA, '12345', bpiSubjectId),
])
.setRoles([
new BpiSubjectRole(
uuid(),
Expand Down

0 comments on commit 9f3a896

Please sign in to comment.