|
1 | 1 | import type { RecordsQueryReplyEntry } from '../../../../src/interfaces/records/types.js'; |
2 | 2 | import type { DerivedPrivateJwk, EncryptionInput, ProtocolDefinition, RecordsWriteMessage } from '../../../../src/index.js'; |
3 | 3 |
|
4 | | - |
5 | 4 | import chaiAsPromised from 'chai-as-promised'; |
6 | 5 | import emailProtocolDefinition from '../../../vectors/protocol-definitions/email.json' assert { type: 'json' }; |
7 | 6 | import sinon from 'sinon'; |
@@ -618,7 +617,7 @@ describe('RecordsQueryHandler.handle()', () => { |
618 | 617 | }); |
619 | 618 |
|
620 | 619 | describe('encryption scenarios', () => { |
621 | | - it('should only be able to decrypt record with a correct derived private key', async () => { |
| 620 | + it('should only be able to decrypt record with a correct derived private key - protocols derivation scheme', async () => { |
622 | 621 | // scenario, Bob writes into Alice's DWN an encrypted "email", alice is able to decrypt it |
623 | 622 |
|
624 | 623 | // creating Alice and Bob persona and setting up a stub DID resolver |
@@ -651,11 +650,8 @@ describe('RecordsQueryHandler.handle()', () => { |
651 | 650 | initializationVector : dataEncryptionInitializationVector, |
652 | 651 | key : dataEncryptionKey, |
653 | 652 | keyEncryptionInputs : [{ |
654 | | - publicKey: { |
655 | | - derivationScheme : KeyDerivationScheme.Protocols, |
656 | | - derivationPath : [], |
657 | | - derivedPublicKey : alice.keyPair.publicJwk // reusing signing key for encryption purely as a convenience |
658 | | - } |
| 653 | + derivationScheme : KeyDerivationScheme.Protocols, |
| 654 | + publicKey : alice.keyPair.publicJwk // reusing signing key for encryption purely as a convenience |
659 | 655 | }] |
660 | 656 | }; |
661 | 657 |
|
@@ -683,25 +679,39 @@ describe('RecordsQueryHandler.handle()', () => { |
683 | 679 |
|
684 | 680 | const unsignedRecordsWrite = queryReply.entries![0] as RecordsQueryReplyEntry; |
685 | 681 |
|
686 | | - // test able to decrypt the message using a derived key |
| 682 | + |
| 683 | + // test able to decrypt the message using the root key |
687 | 684 | const rootPrivateKey: DerivedPrivateJwk = { |
688 | 685 | derivationScheme : KeyDerivationScheme.Protocols, |
689 | | - derivationPath : [], |
690 | 686 | derivedPrivateKey : alice.keyPair.privateJwk |
691 | 687 | }; |
692 | | - const relativeDescendantDerivationPath = Records.constructKeyDerivationPath( |
693 | | - KeyDerivationScheme.Protocols, |
694 | | - message.recordId, |
695 | | - message.contextId, |
696 | | - message.descriptor |
697 | | - ); |
698 | | - const descendantPrivateKey: DerivedPrivateJwk = await HdKey.derivePrivateKey(rootPrivateKey, relativeDescendantDerivationPath); |
699 | | - |
700 | 688 | const cipherStream = DataStream.fromBytes(Encoder.base64UrlToBytes(unsignedRecordsWrite.encodedData!)); |
701 | 689 |
|
702 | | - const plaintextDataStream = await Records.decrypt(unsignedRecordsWrite, descendantPrivateKey, cipherStream); |
| 690 | + const plaintextDataStream = await Records.decrypt(unsignedRecordsWrite, rootPrivateKey, cipherStream); |
703 | 691 | const plaintextBytes = await DataStream.toBytes(plaintextDataStream); |
704 | 692 | expect(Comparer.byteArraysEqual(plaintextBytes, bobMessageBytes)).to.be.true; |
| 693 | + |
| 694 | + |
| 695 | + // test able to decrypt the message using a derived key |
| 696 | + const derivationPath = [KeyDerivationScheme.Protocols]; // the first path segment of `protocol` derivation scheme |
| 697 | + const derivedPrivateKey: DerivedPrivateJwk = await HdKey.derivePrivateKey(rootPrivateKey, derivationPath); |
| 698 | + |
| 699 | + const cipherStream2 = DataStream.fromBytes(Encoder.base64UrlToBytes(unsignedRecordsWrite.encodedData!)); |
| 700 | + |
| 701 | + const plaintextDataStream2 = await Records.decrypt(unsignedRecordsWrite, derivedPrivateKey, cipherStream2); |
| 702 | + const plaintextBytes2 = await DataStream.toBytes(plaintextDataStream2); |
| 703 | + expect(Comparer.byteArraysEqual(plaintextBytes2, bobMessageBytes)).to.be.true; |
| 704 | + |
| 705 | + |
| 706 | + // test able to decrypt the message using a key derived from a derived key |
| 707 | + const protocolsUriDerivationPathSegment = [message.descriptor.protocol!]; // the 2nd path segment of `protocol` derivation scheme |
| 708 | + const derivedPrivateKey2: DerivedPrivateJwk = await HdKey.derivePrivateKey(derivedPrivateKey, protocolsUriDerivationPathSegment); |
| 709 | + |
| 710 | + const cipherStream3 = DataStream.fromBytes(Encoder.base64UrlToBytes(unsignedRecordsWrite.encodedData!)); |
| 711 | + |
| 712 | + const plaintextDataStream3 = await Records.decrypt(unsignedRecordsWrite, derivedPrivateKey2, cipherStream3); |
| 713 | + const plaintextBytes3 = await DataStream.toBytes(plaintextDataStream3); |
| 714 | + expect(Comparer.byteArraysEqual(plaintextBytes3, bobMessageBytes)).to.be.true; |
705 | 715 | }); |
706 | 716 | }); |
707 | 717 | }); |
|
0 commit comments