Skip to content

Commit

Permalink
fix sensor key
Browse files Browse the repository at this point in the history
Signed-off-by: Artem Buslaev <[email protected]>
  • Loading branch information
Artem Buslaev committed Oct 21, 2024
1 parent a3875b6 commit 42f4a74
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
7 changes: 4 additions & 3 deletions common/src/helpers/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,16 +108,17 @@ export class Wallet extends NatsService {
public async getUserKey(
did: string,
keyType: KeyType,
entityId: string
entityId: string,
force: boolean = false,
): Promise<any> {
const hasPermissions = await this.sendMessage(
const hasPermissions = force || (await this.sendMessage(
MessageAPI.CHECK_KEY_PERMISSIONS,
{
did,
keyType,
entityId,
}
);
));

const user = new Users();
const { walletToken } = await user.getUserById(did);
Expand Down
19 changes: 16 additions & 3 deletions policy-service/src/policy-engine/blocks/action-block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,26 @@ export class InterfaceDocumentActionBlock {
}

if (ref.options.type === 'download') {
const sensorDid = document.document.credentialSubject[0].id;
const userDID = document.owner;
const sensor = await ref.databaseServer.getVcDocument({
id: document.id,
owner: user.did,
});
if (!sensor) {
throw new Error(`There is no document with id: ${document.id}`);
}
const sensorDid = sensor.document.credentialSubject[0].id;
const userDID = user.did;
if (userDID !== sensorDid) {
const sensorUser = await PolicyUtils.getUser(ref, sensorDid);
if (sensorUser) {
throw new Error('Sensor DID match with other user DID.');
}
}
const userCred = await PolicyUtils.getUserCredentials(ref, userDID);
const hederaCred = await userCred.loadHederaCredentials(ref);
const schemaObject = await PolicyUtils.loadSchemaByID(ref, ref.options.schema);
const schema = new Schema(schemaObject);
const didDocument = await userCred.loadSubDidDocument(ref, sensorDid);
const didDocument = await userCred.loadSubDidDocument(ref, sensorDid, true);
const sensorKey = await PolicyUtils.getAccountKey(ref, userDID, KeyType.KEY, sensorDid);
result = {
fileName: ref.options.filename || `${sensorDid}.config.json`,
Expand Down
6 changes: 3 additions & 3 deletions policy-service/src/policy-engine/policy-user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ export class UserCredentials {
return await this.loadSubDidDocument(ref, this._did);
}

public async loadSubDidDocument(ref: AnyBlockType, subDid: string): Promise<HederaDidDocument> {
public async loadSubDidDocument(ref: AnyBlockType, subDid: string, force: boolean = false): Promise<HederaDidDocument> {
const virtualUser = this._dryRun && subDid !== this._owner;

let row: DidDocument;
Expand Down Expand Up @@ -321,15 +321,15 @@ export class UserCredentials {
const hederaPrivateKey = await wallet.getUserKey(walletToken, KeyType.KEY, subDid);
//Ed25519Signature2018
if (Ed25519Signature2018) {
const privateKey = await wallet.getUserKey(walletToken, KeyType.DID_KEYS, Ed25519Signature2018);
const privateKey = await wallet.getUserKey(walletToken, KeyType.DID_KEYS, Ed25519Signature2018, force);
document.setPrivateKey(Ed25519Signature2018, privateKey);
} else {
const { id, privateKey } = await HederaEd25519Method.generateKeyPair(subDid, hederaPrivateKey);
document.setPrivateKey(id, privateKey);
}
//BbsBlsSignature2020
if (BbsBlsSignature2020) {
const privateKey = await wallet.getUserKey(walletToken, KeyType.DID_KEYS, BbsBlsSignature2020);
const privateKey = await wallet.getUserKey(walletToken, KeyType.DID_KEYS, BbsBlsSignature2020, force);
document.setPrivateKey(BbsBlsSignature2020, privateKey);
} else {
const { id, privateKey } = await HederaBBSMethod.generateKeyPair(subDid, hederaPrivateKey);
Expand Down

0 comments on commit 42f4a74

Please sign in to comment.