@enbox/dwn-sdk-js 0.0.4 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/browser.mjs +135 -0
- package/dist/browser.mjs.map +7 -0
- package/dist/esm/generated/precompiled-validators.js +640 -510
- package/dist/esm/generated/precompiled-validators.js.map +1 -1
- package/dist/esm/src/core/auth.js +6 -1
- package/dist/esm/src/core/auth.js.map +1 -1
- package/dist/esm/src/core/dwn-error.js +3 -0
- package/dist/esm/src/core/dwn-error.js.map +1 -1
- package/dist/esm/src/core/protocol-authorization.js +4 -0
- package/dist/esm/src/core/protocol-authorization.js.map +1 -1
- package/dist/esm/src/dwn.js +14 -0
- package/dist/esm/src/dwn.js.map +1 -1
- package/dist/esm/src/handlers/protocols-configure.js.map +1 -1
- package/dist/esm/src/handlers/records-delete.js +13 -0
- package/dist/esm/src/handlers/records-delete.js.map +1 -1
- package/dist/esm/src/handlers/records-subscribe.js +121 -66
- package/dist/esm/src/handlers/records-subscribe.js.map +1 -1
- package/dist/esm/src/handlers/records-write.js +1 -1
- package/dist/esm/src/handlers/records-write.js.map +1 -1
- package/dist/esm/src/index.js +1 -1
- package/dist/esm/src/index.js.map +1 -1
- package/dist/esm/src/interfaces/protocols-configure.js.map +1 -1
- package/dist/esm/src/interfaces/records-delete.js +1 -0
- package/dist/esm/src/interfaces/records-delete.js.map +1 -1
- package/dist/esm/src/interfaces/records-subscribe.js +2 -0
- package/dist/esm/src/interfaces/records-subscribe.js.map +1 -1
- package/dist/esm/src/interfaces/records-write.js +28 -45
- package/dist/esm/src/interfaces/records-write.js.map +1 -1
- package/dist/esm/src/jose/jws/general/verifier.js +9 -1
- package/dist/esm/src/jose/jws/general/verifier.js.map +1 -1
- package/dist/esm/src/smt/smt-utils.js +1 -1
- package/dist/esm/src/smt/smt-utils.js.map +1 -1
- package/dist/esm/src/types/records-types.js.map +1 -1
- package/dist/esm/src/utils/encryption.js +221 -78
- package/dist/esm/src/utils/encryption.js.map +1 -1
- package/dist/esm/src/utils/hd-key.js +6 -7
- package/dist/esm/src/utils/hd-key.js.map +1 -1
- package/dist/esm/src/utils/protocols.js +12 -10
- package/dist/esm/src/utils/protocols.js.map +1 -1
- package/dist/esm/src/utils/records.js +33 -44
- package/dist/esm/src/utils/records.js.map +1 -1
- package/dist/esm/tests/features/protocol-composition.spec.js +26 -21
- package/dist/esm/tests/features/protocol-composition.spec.js.map +1 -1
- package/dist/esm/tests/features/records-tags.spec.js +5 -5
- package/dist/esm/tests/features/records-tags.spec.js.map +1 -1
- package/dist/esm/tests/handlers/records-delete.spec.js +120 -2
- package/dist/esm/tests/handlers/records-delete.spec.js.map +1 -1
- package/dist/esm/tests/handlers/records-read.spec.js +25 -26
- package/dist/esm/tests/handlers/records-read.spec.js.map +1 -1
- package/dist/esm/tests/handlers/records-subscribe.spec.js +103 -0
- package/dist/esm/tests/handlers/records-subscribe.spec.js.map +1 -1
- package/dist/esm/tests/handlers/records-write.spec.js +124 -10
- package/dist/esm/tests/handlers/records-write.spec.js.map +1 -1
- package/dist/esm/tests/interfaces/messages-get.spec.js +3 -2
- package/dist/esm/tests/interfaces/messages-get.spec.js.map +1 -1
- package/dist/esm/tests/interfaces/records-write.spec.js +43 -34
- package/dist/esm/tests/interfaces/records-write.spec.js.map +1 -1
- package/dist/esm/tests/scenarios/end-to-end-tests.spec.js +4 -4
- package/dist/esm/tests/scenarios/end-to-end-tests.spec.js.map +1 -1
- package/dist/esm/tests/utils/encryption-callbacks.spec.js +21 -24
- package/dist/esm/tests/utils/encryption-callbacks.spec.js.map +1 -1
- package/dist/esm/tests/utils/encryption.spec.js +69 -66
- package/dist/esm/tests/utils/encryption.spec.js.map +1 -1
- package/dist/esm/tests/utils/filters.spec.js +1 -0
- package/dist/esm/tests/utils/filters.spec.js.map +1 -1
- package/dist/esm/tests/utils/test-data-generator.js +28 -7
- package/dist/esm/tests/utils/test-data-generator.js.map +1 -1
- package/dist/esm/tests/validation/json-schemas/protocols/protocols-configure.spec.js +1 -1
- package/dist/esm/tests/validation/json-schemas/protocols/protocols-configure.spec.js.map +1 -1
- package/dist/types/generated/precompiled-validators.d.ts.map +1 -1
- package/dist/types/src/core/auth.d.ts +3 -1
- package/dist/types/src/core/auth.d.ts.map +1 -1
- package/dist/types/src/core/dwn-error.d.ts +3 -0
- package/dist/types/src/core/dwn-error.d.ts.map +1 -1
- package/dist/types/src/core/protocol-authorization.d.ts.map +1 -1
- package/dist/types/src/dwn.d.ts +12 -0
- package/dist/types/src/dwn.d.ts.map +1 -1
- package/dist/types/src/handlers/protocols-configure.d.ts.map +1 -1
- package/dist/types/src/handlers/records-delete.d.ts.map +1 -1
- package/dist/types/src/handlers/records-subscribe.d.ts +17 -28
- package/dist/types/src/handlers/records-subscribe.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +4 -4
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/interfaces/records-delete.d.ts +4 -0
- package/dist/types/src/interfaces/records-delete.d.ts.map +1 -1
- package/dist/types/src/interfaces/records-subscribe.d.ts +4 -1
- package/dist/types/src/interfaces/records-subscribe.d.ts.map +1 -1
- package/dist/types/src/interfaces/records-write.d.ts +23 -53
- package/dist/types/src/interfaces/records-write.d.ts.map +1 -1
- package/dist/types/src/jose/jws/general/verifier.d.ts.map +1 -1
- package/dist/types/src/types/encryption-types.d.ts +9 -8
- package/dist/types/src/types/encryption-types.d.ts.map +1 -1
- package/dist/types/src/types/protocols-types.d.ts +65 -16
- package/dist/types/src/types/protocols-types.d.ts.map +1 -1
- package/dist/types/src/types/records-types.d.ts +7 -26
- package/dist/types/src/types/records-types.d.ts.map +1 -1
- package/dist/types/src/utils/encryption.d.ts +157 -28
- package/dist/types/src/utils/encryption.d.ts.map +1 -1
- package/dist/types/src/utils/hd-key.d.ts +2 -3
- package/dist/types/src/utils/hd-key.d.ts.map +1 -1
- package/dist/types/src/utils/protocols.d.ts.map +1 -1
- package/dist/types/src/utils/records.d.ts +3 -4
- package/dist/types/src/utils/records.d.ts.map +1 -1
- package/dist/types/tests/features/protocol-composition.spec.d.ts.map +1 -1
- package/dist/types/tests/handlers/records-delete.spec.d.ts.map +1 -1
- package/dist/types/tests/handlers/records-read.spec.d.ts.map +1 -1
- package/dist/types/tests/handlers/records-subscribe.spec.d.ts.map +1 -1
- package/dist/types/tests/handlers/records-write.spec.d.ts.map +1 -1
- package/dist/types/tests/utils/test-data-generator.d.ts +7 -0
- package/dist/types/tests/utils/test-data-generator.d.ts.map +1 -1
- package/package.json +8 -19
- package/src/core/auth.ts +12 -1
- package/src/core/dwn-error.ts +3 -0
- package/src/core/protocol-authorization.ts +8 -0
- package/src/dwn.ts +15 -0
- package/src/handlers/protocols-configure.ts +4 -4
- package/src/handlers/records-delete.ts +12 -0
- package/src/handlers/records-subscribe.ts +174 -75
- package/src/handlers/records-write.ts +1 -1
- package/src/index.ts +4 -4
- package/src/interfaces/protocols-configure.ts +5 -5
- package/src/interfaces/records-delete.ts +9 -3
- package/src/interfaces/records-subscribe.ts +6 -1
- package/src/interfaces/records-write.ts +33 -105
- package/src/jose/jws/general/verifier.ts +11 -1
- package/src/smt/smt-utils.ts +1 -1
- package/src/types/encryption-types.ts +9 -8
- package/src/types/protocols-types.ts +72 -18
- package/src/types/records-types.ts +7 -29
- package/src/utils/encryption.ts +346 -88
- package/src/utils/hd-key.ts +9 -10
- package/src/utils/protocols.ts +15 -13
- package/src/utils/records.ts +47 -55
- package/dist/bundles/dwn.js +0 -151
|
@@ -7,113 +7,256 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
7
7
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
|
-
import
|
|
11
|
-
import
|
|
12
|
-
|
|
13
|
-
|
|
10
|
+
import { concatBytes } from '@noble/ciphers/utils';
|
|
11
|
+
import { Encoder } from './encoder.js';
|
|
12
|
+
import { KeyDerivationScheme } from './hd-key.js';
|
|
13
|
+
import { AesGcm, AesKw, ConcatKdf, X25519, XChaCha20Poly1305 } from '@enbox/crypto';
|
|
14
14
|
/**
|
|
15
|
-
*
|
|
15
|
+
* Content encryption algorithms supported by the DWN.
|
|
16
|
+
* Both are AEAD (Authenticated Encryption with Associated Data) ciphers.
|
|
17
|
+
*/
|
|
18
|
+
export var ContentEncryptionAlgorithm;
|
|
19
|
+
(function (ContentEncryptionAlgorithm) {
|
|
20
|
+
/** AES-256 in Galois/Counter Mode. NIST-approved, hardware-accelerated. 96-bit nonce. */
|
|
21
|
+
ContentEncryptionAlgorithm["A256GCM"] = "A256GCM";
|
|
22
|
+
/** XChaCha20-Poly1305. 192-bit nonce (safe to randomize). Constant-time. */
|
|
23
|
+
ContentEncryptionAlgorithm["XC20P"] = "XC20P";
|
|
24
|
+
})(ContentEncryptionAlgorithm || (ContentEncryptionAlgorithm = {}));
|
|
25
|
+
/**
|
|
26
|
+
* Key agreement algorithm used by the DWN.
|
|
27
|
+
* ECDH-ES with X25519 key agreement and AES-256 Key Wrap.
|
|
28
|
+
*/
|
|
29
|
+
export var KeyAgreementAlgorithm;
|
|
30
|
+
(function (KeyAgreementAlgorithm) {
|
|
31
|
+
KeyAgreementAlgorithm["EcdhEsA256kw"] = "ECDH-ES+A256KW";
|
|
32
|
+
})(KeyAgreementAlgorithm || (KeyAgreementAlgorithm = {}));
|
|
33
|
+
/** Size of the AES-GCM authentication tag in bytes. */
|
|
34
|
+
const AES_GCM_TAG_LENGTH_BYTES = 16;
|
|
35
|
+
/** Size of the Poly1305 authentication tag in bytes. */
|
|
36
|
+
const POLY1305_TAG_LENGTH_BYTES = 16;
|
|
37
|
+
/**
|
|
38
|
+
* Utility class for DWN encryption operations using JWE (RFC 7516).
|
|
39
|
+
* Uses ECDH-ES+A256KW key agreement with X25519 and either AES-256-GCM or XChaCha20-Poly1305
|
|
40
|
+
* for authenticated content encryption.
|
|
16
41
|
*/
|
|
17
42
|
export class Encryption {
|
|
18
43
|
/**
|
|
19
|
-
* Encrypts
|
|
44
|
+
* Encrypts data using an AEAD cipher (A256GCM or XC20P).
|
|
45
|
+
* Returns ciphertext with the authentication tag appended.
|
|
20
46
|
*/
|
|
21
|
-
static
|
|
47
|
+
static aeadEncrypt(algorithm, keyBytes, iv, plaintext) {
|
|
22
48
|
return __awaiter(this, void 0, void 0, function* () {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
49
|
+
if (algorithm === ContentEncryptionAlgorithm.A256GCM) {
|
|
50
|
+
const keyJwk = { kty: 'oct', k: Encoder.bytesToBase64Url(keyBytes), alg: 'A256GCM' };
|
|
51
|
+
// Web Crypto AES-GCM returns ciphertext || tag
|
|
52
|
+
const combined = yield AesGcm.encrypt({ data: plaintext, iv, key: keyJwk });
|
|
53
|
+
const ciphertext = combined.slice(0, combined.length - AES_GCM_TAG_LENGTH_BYTES);
|
|
54
|
+
const tag = combined.slice(combined.length - AES_GCM_TAG_LENGTH_BYTES);
|
|
55
|
+
return { ciphertext, tag };
|
|
56
|
+
}
|
|
57
|
+
else if (algorithm === ContentEncryptionAlgorithm.XC20P) {
|
|
58
|
+
// @noble/ciphers XChaCha20-Poly1305 returns ciphertext || tag
|
|
59
|
+
const combined = yield XChaCha20Poly1305.encryptRaw({ data: plaintext, keyBytes, nonce: iv });
|
|
60
|
+
const ciphertext = combined.slice(0, combined.length - POLY1305_TAG_LENGTH_BYTES);
|
|
61
|
+
const tag = combined.slice(combined.length - POLY1305_TAG_LENGTH_BYTES);
|
|
62
|
+
return { ciphertext, tag };
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
throw new Error(`Unsupported content encryption algorithm: ${algorithm}`);
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Decrypts data using an AEAD cipher (A256GCM or XC20P).
|
|
71
|
+
* Expects ciphertext and tag as separate inputs.
|
|
72
|
+
*/
|
|
73
|
+
static aeadDecrypt(algorithm, keyBytes, iv, ciphertext, tag) {
|
|
74
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
75
|
+
// Both Web Crypto (AES-GCM) and @noble/ciphers (XChaCha20-Poly1305) expect ciphertext || tag
|
|
76
|
+
const combined = concatBytes(ciphertext, tag);
|
|
77
|
+
if (algorithm === ContentEncryptionAlgorithm.A256GCM) {
|
|
78
|
+
const keyJwk = { kty: 'oct', k: Encoder.bytesToBase64Url(keyBytes), alg: 'A256GCM' };
|
|
79
|
+
return AesGcm.decrypt({ data: combined, iv, key: keyJwk });
|
|
80
|
+
}
|
|
81
|
+
else if (algorithm === ContentEncryptionAlgorithm.XC20P) {
|
|
82
|
+
return XChaCha20Poly1305.decryptRaw({ data: combined, keyBytes, nonce: iv });
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
throw new Error(`Unsupported content encryption algorithm: ${algorithm}`);
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Encrypts data as a ReadableStream using an AEAD cipher.
|
|
91
|
+
* Collects all chunks, encrypts, and returns a new stream of ciphertext || tag.
|
|
92
|
+
* The iv and tag are NOT embedded in the stream — they are stored in the JWE structure.
|
|
93
|
+
*/
|
|
94
|
+
static aeadEncryptStream(algorithm, keyBytes, iv, plaintextStream) {
|
|
95
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
96
|
+
const plaintext = yield Encryption.readStream(plaintextStream);
|
|
97
|
+
const { ciphertext, tag } = yield Encryption.aeadEncrypt(algorithm, keyBytes, iv, plaintext);
|
|
98
|
+
const ciphertextStream = new ReadableStream({
|
|
99
|
+
start(controller) {
|
|
100
|
+
controller.enqueue(ciphertext);
|
|
101
|
+
controller.close();
|
|
33
102
|
}
|
|
34
103
|
});
|
|
35
|
-
return
|
|
104
|
+
return { ciphertextStream, tag };
|
|
36
105
|
});
|
|
37
106
|
}
|
|
38
107
|
/**
|
|
39
|
-
* Decrypts
|
|
108
|
+
* Decrypts a ciphertext stream using an AEAD cipher.
|
|
109
|
+
* Returns a ReadableStream of plaintext.
|
|
40
110
|
*/
|
|
41
|
-
static
|
|
111
|
+
static aeadDecryptStream(algorithm, keyBytes, iv, ciphertextStream, tag) {
|
|
42
112
|
return __awaiter(this, void 0, void 0, function* () {
|
|
43
|
-
const
|
|
44
|
-
const
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
const finalChunk = decipher.final();
|
|
50
|
-
if (finalChunk.length > 0) {
|
|
51
|
-
controller.enqueue(new Uint8Array(finalChunk));
|
|
52
|
-
}
|
|
113
|
+
const ciphertext = yield Encryption.readStream(ciphertextStream);
|
|
114
|
+
const plaintext = yield Encryption.aeadDecrypt(algorithm, keyBytes, iv, ciphertext, tag);
|
|
115
|
+
return new ReadableStream({
|
|
116
|
+
start(controller) {
|
|
117
|
+
controller.enqueue(plaintext);
|
|
118
|
+
controller.close();
|
|
53
119
|
}
|
|
54
120
|
});
|
|
55
|
-
return cipherStream.pipeThrough(transform);
|
|
56
121
|
});
|
|
57
122
|
}
|
|
58
123
|
/**
|
|
59
|
-
*
|
|
60
|
-
*
|
|
61
|
-
*
|
|
124
|
+
* Performs ECDH-ES key agreement with X25519 and wraps the CEK using AES-256 Key Wrap.
|
|
125
|
+
*
|
|
126
|
+
* @param ephemeralPrivateKey - Ephemeral X25519 private key (JWK).
|
|
127
|
+
* @param recipientPublicKey - Recipient's X25519 public key (JWK).
|
|
128
|
+
* @param cek - The Content Encryption Key to wrap.
|
|
129
|
+
* @returns The wrapped CEK bytes.
|
|
62
130
|
*/
|
|
63
|
-
static
|
|
131
|
+
static ecdhEsWrapKey(ephemeralPrivateKey, recipientPublicKey, cek) {
|
|
64
132
|
return __awaiter(this, void 0, void 0, function* () {
|
|
65
|
-
//
|
|
66
|
-
const
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
133
|
+
// 1. ECDH shared secret
|
|
134
|
+
const sharedSecret = yield X25519.sharedSecret({
|
|
135
|
+
privateKeyA: ephemeralPrivateKey,
|
|
136
|
+
publicKeyB: recipientPublicKey,
|
|
137
|
+
});
|
|
138
|
+
// 2. Derive KEK via Concat KDF (RFC 7518 Section 4.6.2)
|
|
139
|
+
const kek = yield ConcatKdf.deriveKey({
|
|
140
|
+
sharedSecret,
|
|
141
|
+
keyDataLen: 256,
|
|
142
|
+
fixedInfo: {
|
|
143
|
+
algorithmId: 'A256KW',
|
|
144
|
+
partyUInfo: '',
|
|
145
|
+
partyVInfo: '',
|
|
146
|
+
suppPubInfo: 256,
|
|
147
|
+
},
|
|
148
|
+
});
|
|
149
|
+
// 3. AES-256 Key Wrap
|
|
150
|
+
const cekJwk = { kty: 'oct', k: Encoder.bytesToBase64Url(cek), alg: 'A256GCM' };
|
|
151
|
+
const kekJwk = { kty: 'oct', k: Encoder.bytesToBase64Url(kek), alg: 'A256KW' };
|
|
152
|
+
const wrappedKey = yield AesKw.wrapKey({ unwrappedKey: cekJwk, encryptionKey: kekJwk });
|
|
153
|
+
return wrappedKey;
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Performs ECDH-ES key agreement with X25519 and unwraps the CEK using AES-256 Key Unwrap.
|
|
158
|
+
*
|
|
159
|
+
* @param recipientPrivateKey - Recipient's X25519 private key (JWK).
|
|
160
|
+
* @param ephemeralPublicKey - Ephemeral X25519 public key from the JWE recipient header (JWK).
|
|
161
|
+
* @param wrappedKey - The wrapped CEK bytes.
|
|
162
|
+
* @returns The unwrapped CEK bytes.
|
|
163
|
+
*/
|
|
164
|
+
static ecdhEsUnwrapKey(recipientPrivateKey, ephemeralPublicKey, wrappedKey) {
|
|
165
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
166
|
+
// 1. ECDH shared secret
|
|
167
|
+
const sharedSecret = yield X25519.sharedSecret({
|
|
168
|
+
privateKeyA: recipientPrivateKey,
|
|
169
|
+
publicKeyB: ephemeralPublicKey,
|
|
170
|
+
});
|
|
171
|
+
// 2. Derive KEK via Concat KDF
|
|
172
|
+
const kek = yield ConcatKdf.deriveKey({
|
|
173
|
+
sharedSecret,
|
|
174
|
+
keyDataLen: 256,
|
|
175
|
+
fixedInfo: {
|
|
176
|
+
algorithmId: 'A256KW',
|
|
177
|
+
partyUInfo: '',
|
|
178
|
+
partyVInfo: '',
|
|
179
|
+
suppPubInfo: 256,
|
|
180
|
+
},
|
|
181
|
+
});
|
|
182
|
+
// 3. AES-256 Key Unwrap
|
|
183
|
+
const kekJwk = { kty: 'oct', k: Encoder.bytesToBase64Url(kek), alg: 'A256KW' };
|
|
184
|
+
const unwrappedJwk = yield AesKw.unwrapKey({
|
|
185
|
+
wrappedKeyBytes: wrappedKey,
|
|
186
|
+
wrappedKeyAlgorithm: 'A256GCM',
|
|
187
|
+
decryptionKey: kekJwk,
|
|
188
|
+
});
|
|
189
|
+
return Encoder.base64UrlToBytes(unwrappedJwk.k);
|
|
86
190
|
});
|
|
87
191
|
}
|
|
88
192
|
/**
|
|
89
|
-
*
|
|
90
|
-
*
|
|
91
|
-
*
|
|
193
|
+
* Builds a JWE encryption property structure from encryption input.
|
|
194
|
+
* The ciphertext (encrypted record data) is stored separately in the DataStore,
|
|
195
|
+
* so only the key wrapping metadata, IV, and authentication tag are included here.
|
|
196
|
+
*
|
|
197
|
+
* @param encryptionInput - Describes the CEK, IV, and recipient key encryption inputs.
|
|
198
|
+
* @param tag - The authentication tag produced by the AEAD cipher during data encryption.
|
|
92
199
|
*/
|
|
93
|
-
static
|
|
200
|
+
static buildJwe(encryptionInput, tag) {
|
|
94
201
|
return __awaiter(this, void 0, void 0, function* () {
|
|
95
|
-
|
|
96
|
-
const
|
|
97
|
-
const
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
]
|
|
103
|
-
const
|
|
104
|
-
|
|
202
|
+
var _a;
|
|
203
|
+
const enc = (_a = encryptionInput.algorithm) !== null && _a !== void 0 ? _a : ContentEncryptionAlgorithm.A256GCM;
|
|
204
|
+
const protectedHeader = {
|
|
205
|
+
alg: KeyAgreementAlgorithm.EcdhEsA256kw,
|
|
206
|
+
enc,
|
|
207
|
+
};
|
|
208
|
+
const protectedHeaderBase64url = Encoder.stringToBase64Url(JSON.stringify(protectedHeader));
|
|
209
|
+
const recipients = [];
|
|
210
|
+
for (const keyInput of encryptionInput.keyEncryptionInputs) {
|
|
211
|
+
// Generate ephemeral X25519 key pair for each recipient
|
|
212
|
+
const ephemeralPrivateKey = yield X25519.generateKey();
|
|
213
|
+
const ephemeralPublicKey = yield X25519.getPublicKey({ key: ephemeralPrivateKey });
|
|
214
|
+
// Wrap the CEK
|
|
215
|
+
const wrappedKey = yield Encryption.ecdhEsWrapKey(ephemeralPrivateKey, keyInput.publicKey, encryptionInput.key);
|
|
216
|
+
const recipientHeader = {
|
|
217
|
+
kid: keyInput.publicKeyId,
|
|
218
|
+
epk: ephemeralPublicKey,
|
|
219
|
+
derivationScheme: keyInput.derivationScheme,
|
|
220
|
+
};
|
|
221
|
+
// Attach derived public key for protocolContext scheme
|
|
222
|
+
if (keyInput.derivationScheme === KeyDerivationScheme.ProtocolContext) {
|
|
223
|
+
recipientHeader.derivedPublicKey = keyInput.publicKey;
|
|
224
|
+
}
|
|
225
|
+
recipients.push({
|
|
226
|
+
header: recipientHeader,
|
|
227
|
+
encrypted_key: Encoder.bytesToBase64Url(wrappedKey),
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
return {
|
|
231
|
+
protected: protectedHeaderBase64url,
|
|
232
|
+
iv: Encoder.bytesToBase64Url(encryptionInput.initializationVector),
|
|
233
|
+
tag: Encoder.bytesToBase64Url(tag),
|
|
234
|
+
recipients,
|
|
235
|
+
};
|
|
105
236
|
});
|
|
106
237
|
}
|
|
107
238
|
/**
|
|
108
|
-
*
|
|
239
|
+
* Parses the JWE protected header from its base64url encoding.
|
|
240
|
+
*/
|
|
241
|
+
static parseProtectedHeader(protectedBase64url) {
|
|
242
|
+
return Encoder.base64UrlToObject(protectedBase64url);
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* Reads a ReadableStream to completion and returns all bytes concatenated.
|
|
109
246
|
*/
|
|
110
|
-
static
|
|
111
|
-
return
|
|
247
|
+
static readStream(stream) {
|
|
248
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
249
|
+
const reader = stream.getReader();
|
|
250
|
+
const chunks = [];
|
|
251
|
+
for (;;) {
|
|
252
|
+
const { done, value } = yield reader.read();
|
|
253
|
+
if (done) {
|
|
254
|
+
break;
|
|
255
|
+
}
|
|
256
|
+
chunks.push(value);
|
|
257
|
+
}
|
|
258
|
+
return concatBytes(...chunks);
|
|
259
|
+
});
|
|
112
260
|
}
|
|
113
261
|
}
|
|
114
|
-
export var EncryptionAlgorithm;
|
|
115
|
-
(function (EncryptionAlgorithm) {
|
|
116
|
-
EncryptionAlgorithm["Aes256Ctr"] = "A256CTR";
|
|
117
|
-
EncryptionAlgorithm["EciesSecp256k1"] = "ECIES-ES256K";
|
|
118
|
-
})(EncryptionAlgorithm || (EncryptionAlgorithm = {}));
|
|
119
262
|
//# sourceMappingURL=encryption.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"encryption.js","sourceRoot":"","sources":["../../../../src/utils/encryption.ts"],"names":[],"mappings":";;;;;;;;;
|
|
1
|
+
{"version":3,"file":"encryption.js","sourceRoot":"","sources":["../../../../src/utils/encryption.ts"],"names":[],"mappings":";;;;;;;;;AAGA,OAAO,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AACnD,OAAO,EAAE,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,GAAG,MAAM,MAAM,CAAC,IAAI,EAAE,CAAC;gBAC5C,IAAI,IAAI,EAAE,CAAC;oBAAC,MAAM;gBAAC,CAAC;gBACpB,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;YACrB,CAAC;YACD,OAAO,WAAW,CAAC,GAAG,MAAM,CAAC,CAAC;QAChC,CAAC;KAAA;CACF"}
|
|
@@ -9,7 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
};
|
|
10
10
|
import { Encoder } from './encoder.js';
|
|
11
11
|
import { getWebcryptoSubtle } from '@noble/ciphers/webcrypto';
|
|
12
|
-
import {
|
|
12
|
+
import { X25519 } from '@enbox/crypto';
|
|
13
13
|
import { DwnError, DwnErrorCode } from '../core/dwn-error.js';
|
|
14
14
|
export var KeyDerivationScheme;
|
|
15
15
|
(function (KeyDerivationScheme) {
|
|
@@ -30,15 +30,15 @@ export var KeyDerivationScheme;
|
|
|
30
30
|
export class HdKey {
|
|
31
31
|
/**
|
|
32
32
|
* Derives a descendant private key.
|
|
33
|
-
*
|
|
33
|
+
* Uses X25519 keys for encryption key derivation.
|
|
34
34
|
*/
|
|
35
35
|
static derivePrivateKey(ancestorKey, subDerivationPath) {
|
|
36
36
|
return __awaiter(this, void 0, void 0, function* () {
|
|
37
37
|
var _a;
|
|
38
|
-
const ancestorPrivateKey =
|
|
38
|
+
const ancestorPrivateKey = yield X25519.privateKeyToBytes({ privateKey: ancestorKey.derivedPrivateKey });
|
|
39
39
|
const ancestorPrivateKeyDerivationPath = (_a = ancestorKey.derivationPath) !== null && _a !== void 0 ? _a : [];
|
|
40
40
|
const derivedPrivateKeyBytes = yield HdKey.derivePrivateKeyBytes(ancestorPrivateKey, subDerivationPath);
|
|
41
|
-
const derivedPrivateKeyJwk = yield
|
|
41
|
+
const derivedPrivateKeyJwk = yield X25519.bytesToPrivateKey({ privateKeyBytes: derivedPrivateKeyBytes });
|
|
42
42
|
const derivedDescendantPrivateKey = {
|
|
43
43
|
rootKeyId: ancestorKey.rootKeyId,
|
|
44
44
|
derivationScheme: ancestorKey.derivationScheme,
|
|
@@ -50,12 +50,12 @@ export class HdKey {
|
|
|
50
50
|
}
|
|
51
51
|
/**
|
|
52
52
|
* Derives a descendant public key from an ancestor private key.
|
|
53
|
-
*
|
|
53
|
+
* Uses X25519 keys for encryption key derivation.
|
|
54
54
|
*/
|
|
55
55
|
static derivePublicKey(ancestorKey, subDerivationPath) {
|
|
56
56
|
return __awaiter(this, void 0, void 0, function* () {
|
|
57
57
|
const derivedDescendantPrivateKey = yield HdKey.derivePrivateKey(ancestorKey, subDerivationPath);
|
|
58
|
-
const derivedDescendantPublicKey = yield
|
|
58
|
+
const derivedDescendantPublicKey = yield X25519.getPublicKey({ key: derivedDescendantPrivateKey.derivedPrivateKey });
|
|
59
59
|
return derivedDescendantPublicKey;
|
|
60
60
|
});
|
|
61
61
|
}
|
|
@@ -80,7 +80,6 @@ export class HdKey {
|
|
|
80
80
|
}
|
|
81
81
|
/**
|
|
82
82
|
* Derives a key using HMAC-based Extract-and-Expand Key Derivation Function (HKDF) as defined in RFC 5869.
|
|
83
|
-
* TODO: Consolidate HKDF implementation and usage with web5-js - https://github.com/enboxorg/enbox/issues/742
|
|
84
83
|
*/
|
|
85
84
|
static deriveKeyUsingHkdf(params) {
|
|
86
85
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hd-key.js","sourceRoot":"","sources":["../../../../src/utils/hd-key.ts"],"names":[],"mappings":";;;;;;;;;AAEA,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AACvC,OAAO,EAAE,kBAAkB,EAAE,MAAM,0BAA0B,CAAC;AAC9D,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"hd-key.js","sourceRoot":"","sources":["../../../../src/utils/hd-key.ts"],"names":[],"mappings":";;;;;;;;;AAEA,OAAO,EAAE,OAAO,EAAE,MAAM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G,MAAM;gBACb,IAAI,EAAG,aAAa;gBACpB,IAAI,EAAG,IAAI,UAAU,CAAC,CAAC,CAAC,EAAE,8CAA8C;gBACxE,IAAI;aACL,EACD,YAAY,EACZ,gBAAgB,GAAG,CAAC,CAAC,6BAA6B;aACnD,CAAC;YAEF,0CAA0C;YAC1C,MAAM,eAAe,GAAG,IAAI,UAAU,CAAC,gBAAgB,CAAC,CAAC;YACzD,OAAO,eAAe,CAAC;QACzB,CAAC;KAAA;IAED;;;OAGG;IACK,MAAM,CAAC,yBAAyB,CAAC,YAAsB;QAC7D,IAAI,YAAY,CAAC,QAAQ,CAAC,EAAE,CAAC,EAAE,CAAC;YAC9B,MAAM,IAAI,QAAQ,CAAC,YAAY,CAAC,0BAA0B,EAAE,gCAAgC,YAAY,EAAE,CAAC,CAAC;QAC9G,CAAC;IACH,CAAC;CACF"}
|
|
@@ -7,7 +7,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
7
7
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
|
-
import {
|
|
10
|
+
import { X25519 } from '@enbox/crypto';
|
|
11
11
|
import { HdKey, KeyDerivationScheme } from '../utils/hd-key.js';
|
|
12
12
|
/**
|
|
13
13
|
* Parses a string that may be a cross-protocol reference in `alias:path` format.
|
|
@@ -68,18 +68,19 @@ export class Protocols {
|
|
|
68
68
|
for (const key in ruleSet) {
|
|
69
69
|
if (!key.startsWith('$')) {
|
|
70
70
|
const currentPath = [...parentPath, key];
|
|
71
|
+
const childRuleSet = ruleSet[key];
|
|
71
72
|
// Skip $ref nodes — they are governed by the referenced protocol's encryption keys.
|
|
72
73
|
// Still recurse into children, which belong to the composing protocol.
|
|
73
|
-
if (
|
|
74
|
-
yield injectKeysViaCallback(
|
|
74
|
+
if (childRuleSet.$ref !== undefined) {
|
|
75
|
+
yield injectKeysViaCallback(childRuleSet, currentPath);
|
|
75
76
|
continue;
|
|
76
77
|
}
|
|
77
78
|
const publicKeyJwk = yield keyDeriver.derivePublicKey(currentPath);
|
|
78
|
-
|
|
79
|
+
childRuleSet.$encryption = {
|
|
79
80
|
rootKeyId: keyDeriver.rootKeyId,
|
|
80
81
|
publicKeyJwk,
|
|
81
82
|
};
|
|
82
|
-
yield injectKeysViaCallback(
|
|
83
|
+
yield injectKeysViaCallback(childRuleSet, currentPath);
|
|
83
84
|
}
|
|
84
85
|
}
|
|
85
86
|
});
|
|
@@ -96,15 +97,16 @@ export class Protocols {
|
|
|
96
97
|
// if we encounter a nested rule set (a property name that doesn't begin with '$'), recursively inject the `$encryption` property
|
|
97
98
|
if (!key.startsWith('$')) {
|
|
98
99
|
const derivedPrivateKey = yield HdKey.derivePrivateKey(parentKey, [key]);
|
|
100
|
+
const childRuleSet = ruleSet[key];
|
|
99
101
|
// Skip $ref nodes — they are governed by the referenced protocol's encryption keys.
|
|
100
102
|
// Still recurse into children, which belong to the composing protocol.
|
|
101
|
-
if (
|
|
102
|
-
yield addEncryptionProperty(
|
|
103
|
+
if (childRuleSet.$ref !== undefined) {
|
|
104
|
+
yield addEncryptionProperty(childRuleSet, derivedPrivateKey);
|
|
103
105
|
continue;
|
|
104
106
|
}
|
|
105
|
-
const publicKeyJwk = yield
|
|
106
|
-
|
|
107
|
-
yield addEncryptionProperty(
|
|
107
|
+
const publicKeyJwk = yield X25519.getPublicKey({ key: derivedPrivateKey.derivedPrivateKey });
|
|
108
|
+
childRuleSet.$encryption = { rootKeyId, publicKeyJwk };
|
|
109
|
+
yield addEncryptionProperty(childRuleSet, derivedPrivateKey);
|
|
108
110
|
}
|
|
109
111
|
}
|
|
110
112
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"protocols.js","sourceRoot":"","sources":["../../../../src/utils/protocols.ts"],"names":[],"mappings":";;;;;;;;;AAKA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"protocols.js","sourceRoot":"","sources":["../../../../src/utils/protocols.ts"],"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