@storacha/encrypt-upload-client 0.0.39 → 1.0.0
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/config/env.d.ts.map +1 -1
- package/dist/config/env.js +19 -6
- package/dist/core/client.d.ts +8 -12
- package/dist/core/client.d.ts.map +1 -1
- package/dist/core/client.js +12 -21
- package/dist/core/metadata/encrypted-metadata.d.ts +8 -0
- package/dist/core/metadata/encrypted-metadata.d.ts.map +1 -0
- package/dist/core/metadata/encrypted-metadata.js +69 -0
- package/dist/core/metadata/kms-metadata.d.ts +36 -0
- package/dist/core/metadata/kms-metadata.d.ts.map +1 -0
- package/dist/core/metadata/kms-metadata.js +156 -0
- package/dist/core/{encrypted-metadata.d.ts → metadata/lit-metadata.d.ts} +11 -11
- package/dist/core/metadata/lit-metadata.d.ts.map +1 -0
- package/dist/core/{encrypted-metadata.js → metadata/lit-metadata.js} +32 -42
- package/dist/crypto/adapters/kms-crypto-adapter.d.ts +148 -0
- package/dist/crypto/adapters/kms-crypto-adapter.d.ts.map +1 -0
- package/dist/crypto/adapters/kms-crypto-adapter.js +321 -0
- package/dist/crypto/adapters/lit-crypto-adapter.d.ts +96 -0
- package/dist/crypto/adapters/lit-crypto-adapter.d.ts.map +1 -0
- package/dist/crypto/adapters/lit-crypto-adapter.js +210 -0
- package/dist/crypto/factories.browser.d.ts +11 -0
- package/dist/crypto/factories.browser.d.ts.map +1 -0
- package/dist/crypto/factories.browser.js +16 -0
- package/dist/crypto/factories.node.d.ts +26 -0
- package/dist/crypto/factories.node.d.ts.map +1 -0
- package/dist/crypto/factories.node.js +38 -0
- package/dist/crypto/index.d.ts +5 -0
- package/dist/crypto/index.d.ts.map +1 -0
- package/dist/crypto/index.js +7 -0
- package/dist/crypto/symmetric/generic-aes-ctr-streaming-crypto.d.ts +76 -0
- package/dist/crypto/symmetric/generic-aes-ctr-streaming-crypto.d.ts.map +1 -0
- package/dist/crypto/symmetric/generic-aes-ctr-streaming-crypto.js +177 -0
- package/dist/crypto/symmetric/node-aes-cbc-crypto.d.ts +43 -0
- package/dist/crypto/symmetric/node-aes-cbc-crypto.d.ts.map +1 -0
- package/dist/crypto/symmetric/node-aes-cbc-crypto.js +110 -0
- package/dist/handlers/decrypt-handler.d.ts +9 -4
- package/dist/handlers/decrypt-handler.d.ts.map +1 -1
- package/dist/handlers/decrypt-handler.js +62 -93
- package/dist/handlers/encrypt-handler.d.ts +1 -1
- package/dist/handlers/encrypt-handler.d.ts.map +1 -1
- package/dist/handlers/encrypt-handler.js +31 -41
- package/dist/index.d.ts +0 -1
- package/dist/index.js +0 -1
- package/dist/protocols/lit.d.ts +1 -3
- package/dist/protocols/lit.d.ts.map +1 -1
- package/dist/types.d.ts +135 -20
- package/dist/types.d.ts.map +1 -1
- package/package.json +27 -18
- package/dist/core/encrypted-metadata.d.ts.map +0 -1
- package/dist/crypto-adapters/browser-crypto-adapter.d.ts +0 -42
- package/dist/crypto-adapters/browser-crypto-adapter.d.ts.map +0 -1
- package/dist/crypto-adapters/browser-crypto-adapter.js +0 -109
- package/dist/crypto-adapters/node-crypto-adapter.d.ts +0 -17
- package/dist/crypto-adapters/node-crypto-adapter.d.ts.map +0 -1
- package/dist/crypto-adapters/node-crypto-adapter.js +0 -66
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LitCryptoAdapter implements the complete CryptoAdapter interface using Lit Protocol.
|
|
3
|
+
* It uses composition with a SymmetricCrypto implementation for file encryption/decryption
|
|
4
|
+
* and Lit Protocol for key management operations.
|
|
5
|
+
*
|
|
6
|
+
* @class
|
|
7
|
+
* @implements {Type.CryptoAdapter}
|
|
8
|
+
*/
|
|
9
|
+
export class LitCryptoAdapter implements Type.CryptoAdapter {
|
|
10
|
+
/**
|
|
11
|
+
* Create a new Lit crypto adapter
|
|
12
|
+
*
|
|
13
|
+
* @param {Type.SymmetricCrypto} symmetricCrypto - The symmetric crypto implementation (browser or node)
|
|
14
|
+
* @param {import('@lit-protocol/lit-node-client').LitNodeClient} litClient - The Lit client instance
|
|
15
|
+
*/
|
|
16
|
+
constructor(symmetricCrypto: Type.SymmetricCrypto, litClient: import("@lit-protocol/lit-node-client").LitNodeClient);
|
|
17
|
+
symmetricCrypto: Type.SymmetricCrypto;
|
|
18
|
+
litClient: import("@lit-protocol/lit-node-client").LitNodeClient;
|
|
19
|
+
/**
|
|
20
|
+
* Encrypt a stream of data using the symmetric crypto implementation
|
|
21
|
+
*
|
|
22
|
+
* @param {Type.BlobLike} data - The data to encrypt
|
|
23
|
+
* @returns {Promise<Type.EncryptOutput>} - The encrypted data
|
|
24
|
+
*/
|
|
25
|
+
encryptStream(data: Type.BlobLike): Promise<Type.EncryptOutput>;
|
|
26
|
+
/**
|
|
27
|
+
* Decrypt a stream of data using the symmetric crypto implementation
|
|
28
|
+
*
|
|
29
|
+
* @param {ReadableStream} encryptedData - The encrypted data to decrypt
|
|
30
|
+
* @param {Uint8Array} key - The key to use for decryption
|
|
31
|
+
* @param {Uint8Array} iv - The initialization vector to use for decryption
|
|
32
|
+
* @returns {Promise<ReadableStream>} - The decrypted data
|
|
33
|
+
*/
|
|
34
|
+
decryptStream(encryptedData: ReadableStream, key: Uint8Array, iv: Uint8Array): Promise<ReadableStream>;
|
|
35
|
+
/**
|
|
36
|
+
* Encrypt a symmetric key using the Lit crypto adapter
|
|
37
|
+
*
|
|
38
|
+
* @param {Uint8Array} key - The symmetric key to encrypt
|
|
39
|
+
* @param {Uint8Array} iv - The initialization vector to encrypt
|
|
40
|
+
* @param {Type.EncryptionConfig} encryptionConfig - The encryption configuration
|
|
41
|
+
* @returns {Promise<Type.EncryptedKeyResult>} - The encrypted key result
|
|
42
|
+
*/
|
|
43
|
+
encryptSymmetricKey(key: Uint8Array, iv: Uint8Array, encryptionConfig: Type.EncryptionConfig): Promise<Type.EncryptedKeyResult>;
|
|
44
|
+
/**
|
|
45
|
+
* Decrypt a symmetric key using the Lit crypto adapter
|
|
46
|
+
*
|
|
47
|
+
* @param {string} encryptedKey - The encrypted key to decrypt
|
|
48
|
+
* @param {object} configs - The decryption configuration
|
|
49
|
+
* @param {Type.DecryptionOptions} configs.decryptionOptions - The decryption options
|
|
50
|
+
* @param {Type.ExtractedMetadata} configs.metadata - The extracted metadata
|
|
51
|
+
* @param {Uint8Array} configs.delegationCAR - The delegation CAR
|
|
52
|
+
* @param {Type.AnyLink} configs.resourceCID - The resource CID
|
|
53
|
+
* @param {import('@storacha/client/types').Signer<import('@storacha/client/types').DID, import('@storacha/client/types').SigAlg>} configs.issuer - The issuer
|
|
54
|
+
* @param {import('@storacha/client/types').DID} configs.audience - The audience
|
|
55
|
+
* @returns {Promise<{ key: Uint8Array, iv: Uint8Array }>} - The decrypted key and IV
|
|
56
|
+
*/
|
|
57
|
+
decryptSymmetricKey(encryptedKey: string, configs: {
|
|
58
|
+
decryptionOptions: Type.DecryptionOptions;
|
|
59
|
+
metadata: Type.ExtractedMetadata;
|
|
60
|
+
delegationCAR: Uint8Array;
|
|
61
|
+
resourceCID: Type.AnyLink;
|
|
62
|
+
issuer: import("@storacha/client/types").Signer<import("@storacha/client/types").DID, import("@storacha/client/types").SigAlg>;
|
|
63
|
+
audience: import("@storacha/client/types").DID;
|
|
64
|
+
}): Promise<{
|
|
65
|
+
key: Uint8Array;
|
|
66
|
+
iv: Uint8Array;
|
|
67
|
+
}>;
|
|
68
|
+
/**
|
|
69
|
+
* Extract encrypted metadata from a CAR file
|
|
70
|
+
*
|
|
71
|
+
* @param {Uint8Array} car - The CAR file to extract metadata from
|
|
72
|
+
* @returns {Type.ExtractedMetadata} - The extracted metadata
|
|
73
|
+
*/
|
|
74
|
+
extractEncryptedMetadata(car: Uint8Array): Type.ExtractedMetadata;
|
|
75
|
+
/**
|
|
76
|
+
* Get the encrypted key from the metadata
|
|
77
|
+
*
|
|
78
|
+
* @param {Type.ExtractedMetadata} metadata - The metadata to get the encrypted key from
|
|
79
|
+
* @returns {string} - The encrypted key
|
|
80
|
+
*/
|
|
81
|
+
getEncryptedKey(metadata: Type.ExtractedMetadata): string;
|
|
82
|
+
/**
|
|
83
|
+
* Encode metadata for upload
|
|
84
|
+
*
|
|
85
|
+
* @param {string} encryptedDataCID - The CID of the encrypted data
|
|
86
|
+
* @param {string} encryptedKey - The encrypted key
|
|
87
|
+
* @param {Type.LitKeyMetadata} metadata - The metadata to encode
|
|
88
|
+
* @returns {Promise<{ cid: import('@storacha/upload-client/types').AnyLink, bytes: Uint8Array }>} - The encoded metadata
|
|
89
|
+
*/
|
|
90
|
+
encodeMetadata(encryptedDataCID: string, encryptedKey: string, metadata: Type.LitKeyMetadata): Promise<{
|
|
91
|
+
cid: import("@storacha/upload-client/types").AnyLink;
|
|
92
|
+
bytes: Uint8Array;
|
|
93
|
+
}>;
|
|
94
|
+
}
|
|
95
|
+
import * as Type from '../../types.js';
|
|
96
|
+
//# sourceMappingURL=lit-crypto-adapter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lit-crypto-adapter.d.ts","sourceRoot":"","sources":["../../../src/crypto/adapters/lit-crypto-adapter.js"],"names":[],"mappings":"AAMA;;;;;;;GAOG;AACH,yCAFgB,IAAI,CAAC,aAAa;IAGhC;;;;;OAKG;IACH,6BAHW,IAAI,CAAC,eAAe,aACpB,OAAO,+BAA+B,EAAE,aAAa,EAK/D;IAFC,sCAAsC;IACtC,iEAA0B;IAG5B;;;;;OAKG;IACH,oBAHW,IAAI,CAAC,QAAQ,GACX,OAAO,CAAC,IAAI,CAAC,aAAa,CAAC,CAIvC;IAED;;;;;;;OAOG;IACH,6BALW,cAAc,OACd,UAAU,MACV,UAAU,GACR,OAAO,CAAC,cAAc,CAAC,CAInC;IAED;;;;;;;OAOG;IACH,yBALW,UAAU,MACV,UAAU,oBACV,IAAI,CAAC,gBAAgB,GACnB,OAAO,CAAC,IAAI,CAAC,kBAAkB,CAAC,CAgC5C;IAED;;;;;;;;;;;;OAYG;IACH,kCAVW,MAAM,WAEd;QAAwC,iBAAiB,EAAjD,IAAI,CAAC,iBAAiB;QACU,QAAQ,EAAxC,IAAI,CAAC,iBAAiB;QACF,aAAa,EAAjC,UAAU;QACY,WAAW,EAAjC,IAAI,CAAC,OAAO;QACoH,MAAM,EAAtI,OAAO,wBAAwB,EAAE,MAAM,CAAC,OAAO,wBAAwB,EAAE,GAAG,EAAE,OAAO,wBAAwB,EAAE,MAAM,CAAC;QACxE,QAAQ,EAAtD,OAAO,wBAAwB,EAAE,GAAG;KAC5C,GAAU,OAAO,CAAC;QAAE,GAAG,EAAE,UAAU,CAAC;QAAC,EAAE,EAAE,UAAU,CAAA;KAAE,CAAC,CAsFxD;IAED;;;;;OAKG;IACH,8BAHW,UAAU,GACR,IAAI,CAAC,iBAAiB,CA4BlC;IAED;;;;;OAKG;IACH,0BAHW,IAAI,CAAC,iBAAiB,GACpB,MAAM,CAOlB;IAED;;;;;;;OAOG;IACH,iCALW,MAAM,gBACN,MAAM,YACN,IAAI,CAAC,cAAc,GACjB,OAAO,CAAC;QAAE,GAAG,EAAE,OAAO,+BAA+B,EAAE,OAAO,CAAC;QAAC,KAAK,EAAE,UAAU,CAAA;KAAE,CAAC,CAehG;CACF;sBA5PqB,gBAAgB"}
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
import { base64 } from 'multiformats/bases/base64';
|
|
2
|
+
import * as Lit from '../../protocols/lit.js';
|
|
3
|
+
import * as EncryptedMetadata from '../../core/metadata/encrypted-metadata.js';
|
|
4
|
+
import { createDecryptWrappedInvocation } from '../../utils.js';
|
|
5
|
+
import * as Type from '../../types.js';
|
|
6
|
+
/**
|
|
7
|
+
* LitCryptoAdapter implements the complete CryptoAdapter interface using Lit Protocol.
|
|
8
|
+
* It uses composition with a SymmetricCrypto implementation for file encryption/decryption
|
|
9
|
+
* and Lit Protocol for key management operations.
|
|
10
|
+
*
|
|
11
|
+
* @class
|
|
12
|
+
* @implements {Type.CryptoAdapter}
|
|
13
|
+
*/
|
|
14
|
+
export class LitCryptoAdapter {
|
|
15
|
+
/**
|
|
16
|
+
* Create a new Lit crypto adapter
|
|
17
|
+
*
|
|
18
|
+
* @param {Type.SymmetricCrypto} symmetricCrypto - The symmetric crypto implementation (browser or node)
|
|
19
|
+
* @param {import('@lit-protocol/lit-node-client').LitNodeClient} litClient - The Lit client instance
|
|
20
|
+
*/
|
|
21
|
+
constructor(symmetricCrypto, litClient) {
|
|
22
|
+
this.symmetricCrypto = symmetricCrypto;
|
|
23
|
+
this.litClient = litClient;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Encrypt a stream of data using the symmetric crypto implementation
|
|
27
|
+
*
|
|
28
|
+
* @param {Type.BlobLike} data - The data to encrypt
|
|
29
|
+
* @returns {Promise<Type.EncryptOutput>} - The encrypted data
|
|
30
|
+
*/
|
|
31
|
+
async encryptStream(data) {
|
|
32
|
+
return this.symmetricCrypto.encryptStream(data);
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Decrypt a stream of data using the symmetric crypto implementation
|
|
36
|
+
*
|
|
37
|
+
* @param {ReadableStream} encryptedData - The encrypted data to decrypt
|
|
38
|
+
* @param {Uint8Array} key - The key to use for decryption
|
|
39
|
+
* @param {Uint8Array} iv - The initialization vector to use for decryption
|
|
40
|
+
* @returns {Promise<ReadableStream>} - The decrypted data
|
|
41
|
+
*/
|
|
42
|
+
async decryptStream(encryptedData, key, iv) {
|
|
43
|
+
return this.symmetricCrypto.decryptStream(encryptedData, key, iv);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Encrypt a symmetric key using the Lit crypto adapter
|
|
47
|
+
*
|
|
48
|
+
* @param {Uint8Array} key - The symmetric key to encrypt
|
|
49
|
+
* @param {Uint8Array} iv - The initialization vector to encrypt
|
|
50
|
+
* @param {Type.EncryptionConfig} encryptionConfig - The encryption configuration
|
|
51
|
+
* @returns {Promise<Type.EncryptedKeyResult>} - The encrypted key result
|
|
52
|
+
*/
|
|
53
|
+
async encryptSymmetricKey(key, iv, encryptionConfig) {
|
|
54
|
+
// Step 1. Combine key and IV to encrypt a single string
|
|
55
|
+
const combinedKeyAndIV = this.symmetricCrypto.combineKeyAndIV(key, iv);
|
|
56
|
+
// Step 2. Create access control conditions and encrypt with Lit
|
|
57
|
+
const { spaceDID } = encryptionConfig;
|
|
58
|
+
const accessControlConditions = Lit.getAccessControlConditions(spaceDID);
|
|
59
|
+
// Step 3. Encrypt the base64 encoded combined key and IV with Lit
|
|
60
|
+
const dataToEncrypt = base64.encode(combinedKeyAndIV);
|
|
61
|
+
const { ciphertext, dataToEncryptHash } = await Lit.encryptString({
|
|
62
|
+
dataToEncrypt,
|
|
63
|
+
accessControlConditions,
|
|
64
|
+
}, this.litClient);
|
|
65
|
+
// Step 4. Return the encrypted key and metadata
|
|
66
|
+
return {
|
|
67
|
+
strategy: /** @type {'lit'} */ ('lit'),
|
|
68
|
+
encryptedKey: ciphertext,
|
|
69
|
+
metadata: {
|
|
70
|
+
plaintextKeyHash: dataToEncryptHash,
|
|
71
|
+
accessControlConditions:
|
|
72
|
+
/** @type {import('@lit-protocol/types').AccessControlConditions} */ (accessControlConditions),
|
|
73
|
+
},
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Decrypt a symmetric key using the Lit crypto adapter
|
|
78
|
+
*
|
|
79
|
+
* @param {string} encryptedKey - The encrypted key to decrypt
|
|
80
|
+
* @param {object} configs - The decryption configuration
|
|
81
|
+
* @param {Type.DecryptionOptions} configs.decryptionOptions - The decryption options
|
|
82
|
+
* @param {Type.ExtractedMetadata} configs.metadata - The extracted metadata
|
|
83
|
+
* @param {Uint8Array} configs.delegationCAR - The delegation CAR
|
|
84
|
+
* @param {Type.AnyLink} configs.resourceCID - The resource CID
|
|
85
|
+
* @param {import('@storacha/client/types').Signer<import('@storacha/client/types').DID, import('@storacha/client/types').SigAlg>} configs.issuer - The issuer
|
|
86
|
+
* @param {import('@storacha/client/types').DID} configs.audience - The audience
|
|
87
|
+
* @returns {Promise<{ key: Uint8Array, iv: Uint8Array }>} - The decrypted key and IV
|
|
88
|
+
*/
|
|
89
|
+
async decryptSymmetricKey(encryptedKey, configs) {
|
|
90
|
+
const { decryptionOptions, metadata, delegationCAR, resourceCID, issuer, audience, } = configs;
|
|
91
|
+
// Validate Lit metadata
|
|
92
|
+
if (metadata.strategy !== 'lit') {
|
|
93
|
+
throw new Error('LitCryptoAdapter can only handle Lit metadata');
|
|
94
|
+
}
|
|
95
|
+
const { plaintextKeyHash, accessControlConditions } = metadata;
|
|
96
|
+
// Step 1. Extract spaceDID from access control conditions
|
|
97
|
+
const spaceDID = /** @type {Type.SpaceDID} */ (accessControlConditions[0].parameters[1]);
|
|
98
|
+
// Step 2. Create session signatures if not provided
|
|
99
|
+
let sessionSigs = decryptionOptions.sessionSigs;
|
|
100
|
+
if (!sessionSigs) {
|
|
101
|
+
const acc =
|
|
102
|
+
/** @type import('@lit-protocol/types').AccessControlConditions */ (
|
|
103
|
+
/** @type {unknown} */ (accessControlConditions));
|
|
104
|
+
const expiration = new Date(Date.now() + 1000 * 60 * 5).toISOString(); // 5 min
|
|
105
|
+
// Step 2.1. Create session signatures for the wallet if provided
|
|
106
|
+
if (decryptionOptions.wallet) {
|
|
107
|
+
sessionSigs = await Lit.getSessionSigs(this.litClient, {
|
|
108
|
+
wallet: decryptionOptions.wallet,
|
|
109
|
+
dataToEncryptHash: plaintextKeyHash,
|
|
110
|
+
expiration,
|
|
111
|
+
accessControlConditions: acc,
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
// Step 2.2. Otherwise, create session signatures for the PKP if provided
|
|
115
|
+
else if (decryptionOptions.pkpPublicKey && decryptionOptions.authMethod) {
|
|
116
|
+
sessionSigs = await Lit.getPkpSessionSigs(this.litClient, {
|
|
117
|
+
pkpPublicKey: decryptionOptions.pkpPublicKey,
|
|
118
|
+
authMethod: decryptionOptions.authMethod,
|
|
119
|
+
dataToEncryptHash: plaintextKeyHash,
|
|
120
|
+
expiration,
|
|
121
|
+
accessControlConditions: acc,
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
else {
|
|
125
|
+
throw new Error('Session signatures or signer (wallet/PKP) required for Lit decryption');
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
// Step 3. Create wrapped UCAN invocation
|
|
129
|
+
const wrappedInvocationJSON = await createDecryptWrappedInvocation({
|
|
130
|
+
delegationCAR,
|
|
131
|
+
spaceDID,
|
|
132
|
+
resourceCID,
|
|
133
|
+
issuer,
|
|
134
|
+
audience,
|
|
135
|
+
expiration: new Date(Date.now() + 1000 * 60 * 10).getTime(), // 10 min
|
|
136
|
+
});
|
|
137
|
+
// Step 4. Execute the Lit Action with all the prepared context to decrypt the symmetric key
|
|
138
|
+
const decryptedString = await Lit.executeUcanValidationAction(this.litClient, {
|
|
139
|
+
sessionSigs,
|
|
140
|
+
spaceDID,
|
|
141
|
+
identityBoundCiphertext: encryptedKey,
|
|
142
|
+
plaintextKeyHash,
|
|
143
|
+
accessControlConditions,
|
|
144
|
+
wrappedInvocationJSON,
|
|
145
|
+
});
|
|
146
|
+
// Step 5. Lit Action returns a base64-encoded string, so decode it to Uint8Array
|
|
147
|
+
const combinedKeyAndIV = base64.decode(decryptedString);
|
|
148
|
+
// Step 6. Use symmetric crypto to split the combined key and IV
|
|
149
|
+
return this.symmetricCrypto.splitKeyAndIV(combinedKeyAndIV);
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Extract encrypted metadata from a CAR file
|
|
153
|
+
*
|
|
154
|
+
* @param {Uint8Array} car - The CAR file to extract metadata from
|
|
155
|
+
* @returns {Type.ExtractedMetadata} - The extracted metadata
|
|
156
|
+
*/
|
|
157
|
+
extractEncryptedMetadata(car) {
|
|
158
|
+
const encryptedContentResult = EncryptedMetadata.extract(car);
|
|
159
|
+
if (encryptedContentResult.error) {
|
|
160
|
+
throw encryptedContentResult.error;
|
|
161
|
+
}
|
|
162
|
+
const encryptedContent = encryptedContentResult.ok.toJSON();
|
|
163
|
+
// Validate it's Lit format
|
|
164
|
+
if (!encryptedContent.identityBoundCiphertext ||
|
|
165
|
+
!encryptedContent.accessControlConditions) {
|
|
166
|
+
throw new Error('Invalid Lit Protocol metadata format - missing identityBoundCiphertext or accessControlConditions');
|
|
167
|
+
}
|
|
168
|
+
// Return with strategy identifier
|
|
169
|
+
return {
|
|
170
|
+
strategy: /** @type {'lit'} */ ('lit'),
|
|
171
|
+
encryptedDataCID: encryptedContent.encryptedDataCID,
|
|
172
|
+
identityBoundCiphertext: encryptedContent.identityBoundCiphertext,
|
|
173
|
+
plaintextKeyHash: encryptedContent.plaintextKeyHash,
|
|
174
|
+
accessControlConditions: encryptedContent.accessControlConditions,
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Get the encrypted key from the metadata
|
|
179
|
+
*
|
|
180
|
+
* @param {Type.ExtractedMetadata} metadata - The metadata to get the encrypted key from
|
|
181
|
+
* @returns {string} - The encrypted key
|
|
182
|
+
*/
|
|
183
|
+
getEncryptedKey(metadata) {
|
|
184
|
+
if (metadata.strategy !== 'lit') {
|
|
185
|
+
throw new Error('LitCryptoAdapter can only handle Lit metadata');
|
|
186
|
+
}
|
|
187
|
+
return metadata.identityBoundCiphertext;
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Encode metadata for upload
|
|
191
|
+
*
|
|
192
|
+
* @param {string} encryptedDataCID - The CID of the encrypted data
|
|
193
|
+
* @param {string} encryptedKey - The encrypted key
|
|
194
|
+
* @param {Type.LitKeyMetadata} metadata - The metadata to encode
|
|
195
|
+
* @returns {Promise<{ cid: import('@storacha/upload-client/types').AnyLink, bytes: Uint8Array }>} - The encoded metadata
|
|
196
|
+
*/
|
|
197
|
+
async encodeMetadata(encryptedDataCID, encryptedKey, metadata) {
|
|
198
|
+
const litMetadata = /** @type {Type.LitKeyMetadata} */ (metadata);
|
|
199
|
+
/** @type {Type.LitMetadataInput} */
|
|
200
|
+
const uploadData = {
|
|
201
|
+
encryptedDataCID,
|
|
202
|
+
identityBoundCiphertext: encryptedKey,
|
|
203
|
+
plaintextKeyHash: litMetadata.plaintextKeyHash,
|
|
204
|
+
accessControlConditions: litMetadata.accessControlConditions,
|
|
205
|
+
};
|
|
206
|
+
const encryptedMetadata = EncryptedMetadata.create('lit', uploadData);
|
|
207
|
+
return await encryptedMetadata.archiveBlock();
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
//# sourceMappingURL=lit-crypto-adapter.js.map
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Create a KMS crypto adapter for browser environments
|
|
3
|
+
* Uses the generic AES-CTR streaming crypto implementation
|
|
4
|
+
* Works in browser and Node.js environments
|
|
5
|
+
*
|
|
6
|
+
* @param {URL|string} keyManagerServiceURL
|
|
7
|
+
* @param {string} keyManagerServiceDID
|
|
8
|
+
*/
|
|
9
|
+
export function createGenericKMSAdapter(keyManagerServiceURL: URL | string, keyManagerServiceDID: string): KMSCryptoAdapter;
|
|
10
|
+
import { KMSCryptoAdapter } from './adapters/kms-crypto-adapter.js';
|
|
11
|
+
//# sourceMappingURL=factories.browser.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"factories.browser.d.ts","sourceRoot":"","sources":["../../src/crypto/factories.browser.js"],"names":[],"mappings":"AAGA;;;;;;;GAOG;AACH,8DAHW,GAAG,GAAC,MAAM,wBACV,MAAM,oBAYhB;iCApBgC,kCAAkC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { GenericAesCtrStreamingCrypto } from './symmetric/generic-aes-ctr-streaming-crypto.js';
|
|
2
|
+
import { KMSCryptoAdapter } from './adapters/kms-crypto-adapter.js';
|
|
3
|
+
/**
|
|
4
|
+
* Create a KMS crypto adapter for browser environments
|
|
5
|
+
* Uses the generic AES-CTR streaming crypto implementation
|
|
6
|
+
* Works in browser and Node.js environments
|
|
7
|
+
*
|
|
8
|
+
* @param {URL|string} keyManagerServiceURL
|
|
9
|
+
* @param {string} keyManagerServiceDID
|
|
10
|
+
*/
|
|
11
|
+
export function createGenericKMSAdapter(keyManagerServiceURL, keyManagerServiceDID) {
|
|
12
|
+
const symmetricCrypto = new GenericAesCtrStreamingCrypto();
|
|
13
|
+
return new KMSCryptoAdapter(symmetricCrypto, keyManagerServiceURL,
|
|
14
|
+
/** @type {`did:${string}:${string}`} */ (keyManagerServiceDID));
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=factories.browser.js.map
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Create a KMS crypto adapter for Node.js using the generic AES-CTR streaming crypto.
|
|
3
|
+
* Works in Node.js & browser environments.
|
|
4
|
+
*
|
|
5
|
+
* @param {URL|string} keyManagerServiceURL
|
|
6
|
+
* @param {string} keyManagerServiceDID
|
|
7
|
+
*/
|
|
8
|
+
export function createGenericKMSAdapter(keyManagerServiceURL: URL | string, keyManagerServiceDID: string): KMSCryptoAdapter;
|
|
9
|
+
/**
|
|
10
|
+
* Create a Lit crypto adapter for Node.js using AES-CBC (legacy).
|
|
11
|
+
* Compatible with previous versions of the library.
|
|
12
|
+
*
|
|
13
|
+
* @deprecated Use createGenericLitAdapter instead for new uploads.
|
|
14
|
+
* @param {import('@lit-protocol/lit-node-client').LitNodeClient} litClient
|
|
15
|
+
*/
|
|
16
|
+
export function createNodeLitAdapter(litClient: import("@lit-protocol/lit-node-client").LitNodeClient): LitCryptoAdapter;
|
|
17
|
+
/**
|
|
18
|
+
* Create a Lit crypto adapter for Node.js using the generic AES-CTR streaming crypto.
|
|
19
|
+
* Works in Node.js & browser environments.
|
|
20
|
+
*
|
|
21
|
+
* @param {import('@lit-protocol/lit-node-client').LitNodeClient} litClient
|
|
22
|
+
*/
|
|
23
|
+
export function createGenericLitAdapter(litClient: import("@lit-protocol/lit-node-client").LitNodeClient): LitCryptoAdapter;
|
|
24
|
+
import { KMSCryptoAdapter } from './adapters/kms-crypto-adapter.js';
|
|
25
|
+
import { LitCryptoAdapter } from './adapters/lit-crypto-adapter.js';
|
|
26
|
+
//# sourceMappingURL=factories.node.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"factories.node.d.ts","sourceRoot":"","sources":["../../src/crypto/factories.node.js"],"names":[],"mappings":"AAKA;;;;;;GAMG;AACH,8DAHW,GAAG,GAAC,MAAM,wBACV,MAAM,oBAYhB;AAED;;;;;;GAMG;AACH,gDAFW,OAAO,+BAA+B,EAAE,aAAa,oBAK/D;AAED;;;;;GAKG;AACH,mDAFW,OAAO,+BAA+B,EAAE,aAAa,oBAK/D;iCA1CgC,kCAAkC;iCADlC,kCAAkC"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { GenericAesCtrStreamingCrypto } from './symmetric/generic-aes-ctr-streaming-crypto.js';
|
|
2
|
+
import { NodeAesCbcCrypto } from './symmetric/node-aes-cbc-crypto.js';
|
|
3
|
+
import { LitCryptoAdapter } from './adapters/lit-crypto-adapter.js';
|
|
4
|
+
import { KMSCryptoAdapter } from './adapters/kms-crypto-adapter.js';
|
|
5
|
+
/**
|
|
6
|
+
* Create a KMS crypto adapter for Node.js using the generic AES-CTR streaming crypto.
|
|
7
|
+
* Works in Node.js & browser environments.
|
|
8
|
+
*
|
|
9
|
+
* @param {URL|string} keyManagerServiceURL
|
|
10
|
+
* @param {string} keyManagerServiceDID
|
|
11
|
+
*/
|
|
12
|
+
export function createGenericKMSAdapter(keyManagerServiceURL, keyManagerServiceDID) {
|
|
13
|
+
const symmetricCrypto = new GenericAesCtrStreamingCrypto();
|
|
14
|
+
return new KMSCryptoAdapter(symmetricCrypto, keyManagerServiceURL,
|
|
15
|
+
/** @type {`did:${string}:${string}`} */ (keyManagerServiceDID));
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Create a Lit crypto adapter for Node.js using AES-CBC (legacy).
|
|
19
|
+
* Compatible with previous versions of the library.
|
|
20
|
+
*
|
|
21
|
+
* @deprecated Use createGenericLitAdapter instead for new uploads.
|
|
22
|
+
* @param {import('@lit-protocol/lit-node-client').LitNodeClient} litClient
|
|
23
|
+
*/
|
|
24
|
+
export function createNodeLitAdapter(litClient) {
|
|
25
|
+
const symmetricCrypto = new NodeAesCbcCrypto();
|
|
26
|
+
return new LitCryptoAdapter(symmetricCrypto, litClient);
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Create a Lit crypto adapter for Node.js using the generic AES-CTR streaming crypto.
|
|
30
|
+
* Works in Node.js & browser environments.
|
|
31
|
+
*
|
|
32
|
+
* @param {import('@lit-protocol/lit-node-client').LitNodeClient} litClient
|
|
33
|
+
*/
|
|
34
|
+
export function createGenericLitAdapter(litClient) {
|
|
35
|
+
const symmetricCrypto = new GenericAesCtrStreamingCrypto();
|
|
36
|
+
return new LitCryptoAdapter(symmetricCrypto, litClient);
|
|
37
|
+
}
|
|
38
|
+
//# sourceMappingURL=factories.node.js.map
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { GenericAesCtrStreamingCrypto } from "./symmetric/generic-aes-ctr-streaming-crypto.js";
|
|
2
|
+
export { NodeAesCbcCrypto } from "./symmetric/node-aes-cbc-crypto.js";
|
|
3
|
+
export { LitCryptoAdapter } from "./adapters/lit-crypto-adapter.js";
|
|
4
|
+
export { KMSCryptoAdapter } from "./adapters/kms-crypto-adapter.js";
|
|
5
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/crypto/index.js"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
// Symmetric crypto implementations (algorithm-specific)
|
|
2
|
+
export { GenericAesCtrStreamingCrypto } from './symmetric/generic-aes-ctr-streaming-crypto.js';
|
|
3
|
+
export { NodeAesCbcCrypto } from './symmetric/node-aes-cbc-crypto.js';
|
|
4
|
+
// Strategy adapters (composition-based)
|
|
5
|
+
export { LitCryptoAdapter } from './adapters/lit-crypto-adapter.js';
|
|
6
|
+
export { KMSCryptoAdapter } from './adapters/kms-crypto-adapter.js';
|
|
7
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* GenericAesCtrStreamingCrypto implements TRUE streaming AES-CTR encryption for any JavaScript environment.
|
|
3
|
+
*
|
|
4
|
+
* This implementation:
|
|
5
|
+
* - Uses Web Crypto API (available in both Node.js 16+ and modern browsers)
|
|
6
|
+
* - Emits encrypted chunks immediately without buffering
|
|
7
|
+
* - Supports files of any size with bounded memory usage
|
|
8
|
+
* - Uses TransformStream for clean, standardized streaming
|
|
9
|
+
* - Provides identical results across Node.js and browser environments
|
|
10
|
+
*
|
|
11
|
+
* Key features:
|
|
12
|
+
* - Memory usage: O(1) - constant memory regardless of file size
|
|
13
|
+
* - Supports unlimited file sizes (1TB+)
|
|
14
|
+
* - Cross-platform compatibility (Node.js 16+ and modern browsers)
|
|
15
|
+
* - Clean streaming implementation with automatic resource management
|
|
16
|
+
* - Built-in error handling via TransformStream
|
|
17
|
+
*
|
|
18
|
+
* @class
|
|
19
|
+
* @implements {Type.SymmetricCrypto}
|
|
20
|
+
*/
|
|
21
|
+
export class GenericAesCtrStreamingCrypto implements Type.SymmetricCrypto {
|
|
22
|
+
/**
|
|
23
|
+
* Generate a random AES key
|
|
24
|
+
*
|
|
25
|
+
* @returns {Promise<Uint8Array>} A random AES key
|
|
26
|
+
*/
|
|
27
|
+
generateKey(): Promise<Uint8Array>;
|
|
28
|
+
/**
|
|
29
|
+
* Properly increment AES-CTR counter with 128-bit arithmetic
|
|
30
|
+
*
|
|
31
|
+
* @param {Uint8Array} counter - The base counter (16 bytes)
|
|
32
|
+
* @param {number} increment - The value to add
|
|
33
|
+
* @returns {Uint8Array} - New counter with proper carry propagation
|
|
34
|
+
*/
|
|
35
|
+
incrementCounter(counter: Uint8Array, increment: number): Uint8Array;
|
|
36
|
+
/**
|
|
37
|
+
* Encrypt a stream of data using AES-CTR with TRUE streaming (no buffering).
|
|
38
|
+
*
|
|
39
|
+
* @param {Blob} data The data to encrypt.
|
|
40
|
+
* @returns {Promise<{ key: Uint8Array, iv: Uint8Array, encryptedStream: ReadableStream }>}
|
|
41
|
+
*/
|
|
42
|
+
encryptStream(data: Blob): Promise<{
|
|
43
|
+
key: Uint8Array;
|
|
44
|
+
iv: Uint8Array;
|
|
45
|
+
encryptedStream: ReadableStream;
|
|
46
|
+
}>;
|
|
47
|
+
/**
|
|
48
|
+
* Decrypt a stream of data using AES-CTR with TRUE streaming (no buffering).
|
|
49
|
+
*
|
|
50
|
+
* @param {ReadableStream} encryptedData The encrypted data stream.
|
|
51
|
+
* @param {Uint8Array} key The encryption key.
|
|
52
|
+
* @param {Uint8Array} iv The initialization vector (counter).
|
|
53
|
+
* @returns {Promise<ReadableStream>} A stream of decrypted data.
|
|
54
|
+
*/
|
|
55
|
+
decryptStream(encryptedData: ReadableStream, key: Uint8Array, iv: Uint8Array): Promise<ReadableStream>;
|
|
56
|
+
/**
|
|
57
|
+
* Combine key and IV into a single array for AES-CTR
|
|
58
|
+
*
|
|
59
|
+
* @param {Uint8Array} key - The AES key (KEY_LENGTH/8 bytes)
|
|
60
|
+
* @param {Uint8Array} iv - The AES-CTR IV (IV_LENGTH bytes)
|
|
61
|
+
* @returns {Uint8Array} Combined key and IV (KEY_LENGTH/8 + IV_LENGTH bytes)
|
|
62
|
+
*/
|
|
63
|
+
combineKeyAndIV(key: Uint8Array, iv: Uint8Array): Uint8Array;
|
|
64
|
+
/**
|
|
65
|
+
* Split combined key and IV for AES-CTR
|
|
66
|
+
*
|
|
67
|
+
* @param {Uint8Array} combined - Combined key and IV (KEY_LENGTH/8 + IV_LENGTH bytes)
|
|
68
|
+
* @returns {{ key: Uint8Array, iv: Uint8Array }} Separated key and IV
|
|
69
|
+
*/
|
|
70
|
+
splitKeyAndIV(combined: Uint8Array): {
|
|
71
|
+
key: Uint8Array;
|
|
72
|
+
iv: Uint8Array;
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
import * as Type from '../../types.js';
|
|
76
|
+
//# sourceMappingURL=generic-aes-ctr-streaming-crypto.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"generic-aes-ctr-streaming-crypto.d.ts","sourceRoot":"","sources":["../../../src/crypto/symmetric/generic-aes-ctr-streaming-crypto.js"],"names":[],"mappings":"AAOA;;;;;;;;;;;;;;;;;;;GAmBG;AACH,qDAFgB,IAAI,CAAC,eAAe;IASlC;;;;OAIG;IACH,eAFa,OAAO,CAAC,UAAU,CAAC,CAI/B;IAED;;;;;;OAMG;IACH,0BAJW,UAAU,aACV,MAAM,GACJ,UAAU,CAsBtB;IAED;;;;;OAKG;IACH,oBAHW,IAAI,GACF,OAAO,CAAC;QAAE,GAAG,EAAE,UAAU,CAAC;QAAC,EAAE,EAAE,UAAU,CAAC;QAAC,eAAe,EAAE,cAAc,CAAA;KAAE,CAAC,CAsDzF;IAED;;;;;;;OAOG;IACH,6BALW,cAAc,OACd,UAAU,MACV,UAAU,GACR,OAAO,CAAC,cAAc,CAAC,CAiDnC;IAED;;;;;;OAMG;IACH,qBAJW,UAAU,MACV,UAAU,GACR,UAAU,CAatB;IAED;;;;;OAKG;IACH,wBAHW,UAAU,GACR;QAAE,GAAG,EAAE,UAAU,CAAC;QAAC,EAAE,EAAE,UAAU,CAAA;KAAE,CAc/C;CACF;sBApOqB,gBAAgB"}
|