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

fix sensor key #4293

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
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