Skip to content

Commit

Permalink
Merge pull request #101 from zCloak-Network/batch-decrypt
Browse files Browse the repository at this point in the history
add batch decrypt
  • Loading branch information
whgreate authored Jan 12, 2024
2 parents bfd3457 + 7b2c106 commit c9b4b3e
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 1 deletion.
9 changes: 9 additions & 0 deletions .changeset/tough-rabbits-complain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"@zcloak/login-rpc-defines": patch
"@zcloak/message": patch
"@zcloak/login-providers": patch
"@zcloak/did": patch
"@zcloak/login-did": patch
---

add batch decrypt
8 changes: 8 additions & 0 deletions login/did/src/LoginDid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,12 @@ export class LoginDid extends Did implements IDidKeyring {
};
});
}

public override async batchDecrypt(
params: { sender: `did:zk:${string}`; message: `0x${string}` }[]
): Promise<Uint8Array[]> {
const decrypts = await this.provider.batchDecrypt(params);

return decrypts.map((data) => hexToU8a(data));
}
}
4 changes: 4 additions & 0 deletions login/providers/src/base/Provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,8 @@ export class BaseProvider extends Events<ProviderEvents> {
public async batchEncrypt(params: RpcRequest<'batch_encrypt'>): Promise<RpcResponse<'batch_encrypt'>> {
return this.request('batch_encrypt', params);
}

public async batchDecrypt(params: RpcRequest<'batch_decrypt'>): Promise<RpcResponse<'batch_decrypt'>> {
return this.request('batch_decrypt', params);
}
}
1 change: 1 addition & 0 deletions login/rpc-defines/src/defineZk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ declare module '@zcloak/login-rpc/rpcs' {
send_tx: [SendTxParams, any];
batch_sign: [BatchSignParams, SignedData[]];
batch_encrypt: [DidEncryptParams[], DidEncrypted[]];
batch_decrypt: [DidDecryptParams[], HexString[]];
}

interface RpcEvents {
Expand Down
43 changes: 42 additions & 1 deletion packages/message/src/decrypt/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import type { IDidKeyring } from '@zcloak/did/types';
import type { DidResolver } from '@zcloak/did-resolver';
import type { BaseMessage, DecryptedMessage, Message, MessageData, MessageType } from '../types';

import { assert, isHex, isNumber, u8aToString } from '@polkadot/util';
import { assert, isHex, isNumber, u8aToHex, u8aToString } from '@polkadot/util';

import { decodeMultibase } from '@zcloak/crypto';
import { DidKeyring } from '@zcloak/did/did/keyring';
import { isDidUrl, isSameUri } from '@zcloak/did/utils';
import { defaultResolver } from '@zcloak/did-resolver/defaults';
import { isRawCredential, isVC, isVP } from '@zcloak/vc/is';
Expand Down Expand Up @@ -172,3 +173,43 @@ export async function decryptMessage<T extends MessageType>(

return decryptedMessage;
}

export async function batchDecryptMessage<T extends MessageType>(
messages: Message<T>[],
did: DidKeyring,
resolver: DidResolver = defaultResolver
): Promise<DecryptedMessage<T>[]> {
messages.forEach((message) => verifyMessageEnvelope(message));

const decrypteds = await did.batchDecrypt(
messages.map((message) => {
return {
message: u8aToHex(decodeMultibase(message.encryptedMsg)),
sender: message.sender
};
}),
undefined,
resolver
);

const decryptedMessages: DecryptedMessage<T>[] = decrypteds.map((decrypted, index) => {
const message = messages[index];
const data: MessageData[T] = JSON.parse(u8aToString(decrypted));

return {
id: message.id,
reply: message.reply,
createTime: message.createTime,
version: message.version,
msgType: message.msgType,
sender: message.sender,
receiver: message.receiver,
ctype: message.ctype,
data
};
});

decryptedMessages.forEach((decryptedMessage) => verifyMessageData(decryptedMessage));

return decryptedMessages;
}
13 changes: 13 additions & 0 deletions protocol/did/src/did/keyring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,17 @@ export abstract class DidKeyring extends DidDetails implements IDidKeyring {

return Promise.all(encrypts);
}

public batchDecrypt(
params: {
sender: DidUrl;
message: HexString;
}[],
receiverUrl = this.getKeyUrl('keyAgreement'),
resolver: DidResolver = defaultResolver
): Promise<Uint8Array[]> {
const decrypts = params.map(({ message, sender }) => this.decrypt(message, sender, receiverUrl, resolver));

return Promise.all(decrypts);
}
}

0 comments on commit c9b4b3e

Please sign in to comment.