@storacha/encrypt-upload-client 0.0.36 → 1.0.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/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 +20 -0
- package/dist/crypto/factories.browser.d.ts.map +1 -0
- package/dist/crypto/factories.browser.js +28 -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/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
package/dist/core/client.d.ts
CHANGED
|
@@ -3,10 +3,9 @@ export class EncryptedClient implements Type.EncryptedClient {
|
|
|
3
3
|
/**
|
|
4
4
|
* @param {import('@storacha/client').Client} storachaClient
|
|
5
5
|
* @param {Type.CryptoAdapter} cryptoAdapter
|
|
6
|
-
* @param {import('@lit-protocol/lit-node-client').LitNodeClient} litClient
|
|
7
6
|
* @param {URL} gatewayURL
|
|
8
7
|
*/
|
|
9
|
-
constructor(storachaClient: import("@storacha/client").Client, cryptoAdapter: Type.CryptoAdapter,
|
|
8
|
+
constructor(storachaClient: import("@storacha/client").Client, cryptoAdapter: Type.CryptoAdapter, gatewayURL: URL);
|
|
10
9
|
/**
|
|
11
10
|
* @type {Type.CryptoAdapter}
|
|
12
11
|
* @protected
|
|
@@ -17,32 +16,29 @@ export class EncryptedClient implements Type.EncryptedClient {
|
|
|
17
16
|
* @protected
|
|
18
17
|
*/
|
|
19
18
|
protected _storachaClient: import("@storacha/client").Client;
|
|
20
|
-
/**
|
|
21
|
-
* @type {import('@lit-protocol/lit-node-client').LitNodeClient}
|
|
22
|
-
* @protected
|
|
23
|
-
*/
|
|
24
|
-
protected _litClient: import("@lit-protocol/lit-node-client").LitNodeClient;
|
|
25
19
|
/**
|
|
26
20
|
* @type {URL}
|
|
27
21
|
* @protected
|
|
28
22
|
*/
|
|
29
23
|
protected _gatewayURL: URL;
|
|
30
24
|
/**
|
|
31
|
-
*
|
|
25
|
+
* Encrypt and upload a file to the Storacha network
|
|
32
26
|
*
|
|
33
27
|
* @param {Type.BlobLike} file - The file to upload
|
|
28
|
+
* @param {Type.EncryptionConfig} encryptionConfig - User-provided encryption configuration
|
|
29
|
+
* @param {Type.UploadOptions} [uploadOptions] - User-provided upload options
|
|
34
30
|
* @returns {Promise<Type.AnyLink>} - The link to the uploaded file
|
|
35
31
|
*/
|
|
36
|
-
|
|
32
|
+
encryptAndUploadFile(file: Type.BlobLike, encryptionConfig: Type.EncryptionConfig, uploadOptions?: Type.UploadOptions): Promise<Type.AnyLink>;
|
|
37
33
|
/**
|
|
38
34
|
* Retrieve and decrypt a file from the Storacha network
|
|
39
35
|
*
|
|
40
|
-
* @param {Type.LitWalletSigner | Type.LitPkpSigner} signer - The wallet or PKP key signer to decrypt the file
|
|
41
36
|
* @param {Type.AnyLink} cid - The link to the file to retrieve
|
|
42
|
-
* @param {Uint8Array} delegationCAR - The delegation that gives permission to decrypt
|
|
37
|
+
* @param {Uint8Array} delegationCAR - The delegation that gives permission to decrypt (required for both strategies)
|
|
38
|
+
* @param {Type.DecryptionOptions} decryptionOptions - User-provided decryption options
|
|
43
39
|
* @returns {Promise<ReadableStream>} - The decrypted file
|
|
44
40
|
*/
|
|
45
|
-
retrieveAndDecryptFile(
|
|
41
|
+
retrieveAndDecryptFile(cid: Type.AnyLink, delegationCAR: Uint8Array, decryptionOptions: Type.DecryptionOptions): Promise<ReadableStream>;
|
|
46
42
|
}
|
|
47
43
|
export function create(options: Type.EncryptedClientOptions): Promise<EncryptedClient>;
|
|
48
44
|
import * as Type from '../types.js';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../src/core/client.js"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../src/core/client.js"],"names":[],"mappings":"AAKA,yCAAyC;AACzC,wCADiB,IAAI,CAAC,eAAe;IAoBnC;;;;OAIG;IACH,4BAJW,OAAO,kBAAkB,EAAE,MAAM,iBACjC,IAAI,CAAC,aAAa,cAClB,GAAG,EAMb;IA3BD;;;OAGG;IACH,0BAHU,IAAI,CAAC,aAAa,CAGd;IAEd;;;OAGG;IACH,2BAHU,OAAO,kBAAkB,EAAE,MAAM,CAG5B;IAEf;;;OAGG;IACH,uBAHU,GAAG,CAGF;IAaX;;;;;;;OAOG;IACH,2BALW,IAAI,CAAC,QAAQ,oBACb,IAAI,CAAC,gBAAgB,kBACrB,IAAI,CAAC,aAAa,GAChB,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,CAUjC;IAED;;;;;;;OAOG;IACH,4BALW,IAAI,CAAC,OAAO,iBACZ,UAAU,qBACV,IAAI,CAAC,iBAAiB,GACpB,OAAO,CAAC,cAAc,CAAC,CAWnC;CACF;AASM,gCAFI,IAAI,CAAC,sBAAsB,4BAQrC;sBAvFqB,aAAa"}
|
package/dist/core/client.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import * as Type from '../types.js';
|
|
2
|
-
import { getLitClient } from '../protocols/lit.js';
|
|
3
2
|
import { GATEWAY_URL } from '../config/constants.js';
|
|
4
3
|
import { encryptAndUpload } from '../handlers/encrypt-handler.js';
|
|
5
4
|
import { retrieveAndDecrypt } from '../handlers/decrypt-handler.js';
|
|
@@ -15,11 +14,6 @@ export class EncryptedClient {
|
|
|
15
14
|
* @protected
|
|
16
15
|
*/
|
|
17
16
|
_storachaClient;
|
|
18
|
-
/**
|
|
19
|
-
* @type {import('@lit-protocol/lit-node-client').LitNodeClient}
|
|
20
|
-
* @protected
|
|
21
|
-
*/
|
|
22
|
-
_litClient;
|
|
23
17
|
/**
|
|
24
18
|
* @type {URL}
|
|
25
19
|
* @protected
|
|
@@ -28,50 +22,47 @@ export class EncryptedClient {
|
|
|
28
22
|
/**
|
|
29
23
|
* @param {import('@storacha/client').Client} storachaClient
|
|
30
24
|
* @param {Type.CryptoAdapter} cryptoAdapter
|
|
31
|
-
* @param {import('@lit-protocol/lit-node-client').LitNodeClient} litClient
|
|
32
25
|
* @param {URL} gatewayURL
|
|
33
26
|
*/
|
|
34
|
-
constructor(storachaClient, cryptoAdapter,
|
|
27
|
+
constructor(storachaClient, cryptoAdapter, gatewayURL) {
|
|
35
28
|
this._storachaClient = storachaClient;
|
|
36
29
|
this._cryptoAdapter = cryptoAdapter;
|
|
37
|
-
this._litClient = litClient;
|
|
38
30
|
this._gatewayURL = gatewayURL;
|
|
39
31
|
}
|
|
40
32
|
/**
|
|
41
|
-
*
|
|
33
|
+
* Encrypt and upload a file to the Storacha network
|
|
42
34
|
*
|
|
43
35
|
* @param {Type.BlobLike} file - The file to upload
|
|
36
|
+
* @param {Type.EncryptionConfig} encryptionConfig - User-provided encryption configuration
|
|
37
|
+
* @param {Type.UploadOptions} [uploadOptions] - User-provided upload options
|
|
44
38
|
* @returns {Promise<Type.AnyLink>} - The link to the uploaded file
|
|
45
39
|
*/
|
|
46
|
-
async
|
|
47
|
-
return encryptAndUpload(this._storachaClient, this.
|
|
40
|
+
async encryptAndUploadFile(file, encryptionConfig, uploadOptions = {}) {
|
|
41
|
+
return encryptAndUpload(this._storachaClient, this._cryptoAdapter, file, encryptionConfig, uploadOptions);
|
|
48
42
|
}
|
|
49
43
|
/**
|
|
50
44
|
* Retrieve and decrypt a file from the Storacha network
|
|
51
45
|
*
|
|
52
|
-
* @param {Type.LitWalletSigner | Type.LitPkpSigner} signer - The wallet or PKP key signer to decrypt the file
|
|
53
46
|
* @param {Type.AnyLink} cid - The link to the file to retrieve
|
|
54
|
-
* @param {Uint8Array} delegationCAR - The delegation that gives permission to decrypt
|
|
47
|
+
* @param {Uint8Array} delegationCAR - The delegation that gives permission to decrypt (required for both strategies)
|
|
48
|
+
* @param {Type.DecryptionOptions} decryptionOptions - User-provided decryption options
|
|
55
49
|
* @returns {Promise<ReadableStream>} - The decrypted file
|
|
56
50
|
*/
|
|
57
|
-
async retrieveAndDecryptFile(
|
|
58
|
-
return retrieveAndDecrypt(this._storachaClient, this.
|
|
51
|
+
async retrieveAndDecryptFile(cid, delegationCAR, decryptionOptions) {
|
|
52
|
+
return retrieveAndDecrypt(this._storachaClient, this._cryptoAdapter, this._gatewayURL, cid, delegationCAR, decryptionOptions);
|
|
59
53
|
}
|
|
60
54
|
}
|
|
61
55
|
/**
|
|
62
|
-
* Creates a new
|
|
63
|
-
*
|
|
64
|
-
* If no Lit Client is provided, one will be created based on the LIT_NETWORK environment variable, defaulting to "DatilTest" if not set.
|
|
56
|
+
* Creates a new EncryptedClient.
|
|
65
57
|
*
|
|
66
58
|
* If no Gateway URL is provided, the default value of 'https://w3s.link' will be used.
|
|
67
59
|
*
|
|
68
60
|
* @param {Type.EncryptedClientOptions} options
|
|
69
61
|
*/
|
|
70
62
|
export const create = async (options) => {
|
|
71
|
-
const litClient = options.litClient ?? (await getLitClient());
|
|
72
63
|
const cryptoAdapter = options.cryptoAdapter;
|
|
73
64
|
const gatewayURL = options.gatewayURL ?? GATEWAY_URL;
|
|
74
65
|
const storachaClient = options.storachaClient;
|
|
75
|
-
return new EncryptedClient(storachaClient, cryptoAdapter,
|
|
66
|
+
return new EncryptedClient(storachaClient, cryptoAdapter, gatewayURL);
|
|
76
67
|
};
|
|
77
68
|
//# sourceMappingURL=client.js.map
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export function extract(archive: Uint8Array): any;
|
|
2
|
+
export function create(strategy: "lit" | "kms", data: any): import("../../types.js").LitMetadataView | import("../../types.js").KMSMetadataView;
|
|
3
|
+
export function getSupportedVersions(): string[];
|
|
4
|
+
export function isVersionSupported(version: string): boolean;
|
|
5
|
+
import * as LitMetadata from './lit-metadata.js';
|
|
6
|
+
import * as KMSMetadata from './kms-metadata.js';
|
|
7
|
+
export { LitMetadata, KMSMetadata };
|
|
8
|
+
//# sourceMappingURL=encrypted-metadata.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"encrypted-metadata.d.ts","sourceRoot":"","sources":["../../../src/core/metadata/encrypted-metadata.js"],"names":[],"mappings":"AAuBO,iCAHI,UAAU,GACR,GAAG,CA8Bf;AAQM,iCAHI,KAAK,GAAG,KAAK,QACb,GAAG,uFAWb;AAKM,iDAAuD;AAOvD,4CAFI,MAAM,WAEgD;6BA1EpC,mBAAmB;6BACnB,mBAAmB"}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Universal Encrypted Metadata Orchestrator
|
|
3
|
+
* Routes between different encryption strategies (Lit, KMS, etc.)
|
|
4
|
+
* Each network gets its own version
|
|
5
|
+
*/
|
|
6
|
+
import * as LitMetadata from './lit-metadata.js';
|
|
7
|
+
import * as KMSMetadata from './kms-metadata.js';
|
|
8
|
+
import { CAR, error } from '@ucanto/core';
|
|
9
|
+
import * as dagCBOR from '@ipld/dag-cbor';
|
|
10
|
+
import { UnknownFormat } from '../errors.js';
|
|
11
|
+
const FORMATS = {
|
|
12
|
+
[LitMetadata.version]: LitMetadata, // 'encrypted-metadata@0.1'
|
|
13
|
+
[KMSMetadata.version]: KMSMetadata, // 'encrypted-metadata@0.2'
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Universal extract function - tries each registered format
|
|
17
|
+
*
|
|
18
|
+
* @param {Uint8Array} archive
|
|
19
|
+
* @returns {any}
|
|
20
|
+
*/
|
|
21
|
+
export const extract = (archive) => {
|
|
22
|
+
// Decode CAR to check version
|
|
23
|
+
const { roots } = CAR.decode(archive);
|
|
24
|
+
if (!roots.length) {
|
|
25
|
+
return error(new UnknownFormat('missing root block'));
|
|
26
|
+
}
|
|
27
|
+
const { code } = roots[0].cid;
|
|
28
|
+
if (code !== dagCBOR.code) {
|
|
29
|
+
return error(new UnknownFormat(`unexpected root CID codec: 0x${code.toString(16)}`));
|
|
30
|
+
}
|
|
31
|
+
// Check which version this metadata uses
|
|
32
|
+
const value = dagCBOR.decode(roots[0].bytes);
|
|
33
|
+
for (const [version, formatModule] of Object.entries(FORMATS)) {
|
|
34
|
+
if (value && typeof value === 'object' && version in value) {
|
|
35
|
+
// Found matching version, delegate to specific format module
|
|
36
|
+
return formatModule.extract(archive);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return error(new UnknownFormat('Unknown metadata format - no matching version found'));
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* Create metadata for specific strategy
|
|
43
|
+
*
|
|
44
|
+
* @param {'lit' | 'kms'} strategy
|
|
45
|
+
* @param {any} data
|
|
46
|
+
*/
|
|
47
|
+
export const create = (strategy, data) => {
|
|
48
|
+
switch (strategy) {
|
|
49
|
+
case 'lit':
|
|
50
|
+
return LitMetadata.create(data);
|
|
51
|
+
case 'kms':
|
|
52
|
+
return KMSMetadata.create(data);
|
|
53
|
+
default:
|
|
54
|
+
throw new Error(`Unknown encryption strategy: ${strategy}`);
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
/**
|
|
58
|
+
* Get available format versions
|
|
59
|
+
*/
|
|
60
|
+
export const getSupportedVersions = () => Object.keys(FORMATS);
|
|
61
|
+
/**
|
|
62
|
+
* Check if a version is supported
|
|
63
|
+
*
|
|
64
|
+
* @param {string} version
|
|
65
|
+
*/
|
|
66
|
+
export const isVersionSupported = (version) => version in FORMATS;
|
|
67
|
+
// Re-export format modules for direct access if needed
|
|
68
|
+
export { LitMetadata, KMSMetadata };
|
|
69
|
+
//# sourceMappingURL=encrypted-metadata.js.map
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
export const version: "encrypted-metadata@0.2";
|
|
2
|
+
export const KMSMetadataSchema: Schema.VariantSchema<{
|
|
3
|
+
"encrypted-metadata@0.2": Schema.StructSchema<{
|
|
4
|
+
encryptedDataCID: Schema.Schema<Link.Link<unknown, number, number, 0 | 1>, any>;
|
|
5
|
+
encryptedSymmetricKey: Schema.StringSchema<string, unknown>;
|
|
6
|
+
space: Schema.StringSchema<string, unknown>;
|
|
7
|
+
kms: Schema.StructSchema<{
|
|
8
|
+
provider: Schema.StringSchema<string, unknown>;
|
|
9
|
+
keyId: Schema.StringSchema<string, unknown>;
|
|
10
|
+
algorithm: Schema.StringSchema<string, unknown>;
|
|
11
|
+
}, any>;
|
|
12
|
+
}, unknown>;
|
|
13
|
+
}, unknown>;
|
|
14
|
+
export const KMSMetadataInputSchema: Schema.StructSchema<{
|
|
15
|
+
encryptedDataCID: Schema.StringSchema<string, unknown>;
|
|
16
|
+
encryptedSymmetricKey: Schema.StringSchema<string, unknown>;
|
|
17
|
+
space: Schema.StringSchema<string, unknown>;
|
|
18
|
+
kms: Schema.StructSchema<{
|
|
19
|
+
provider: Schema.StringSchema<string, unknown>;
|
|
20
|
+
keyId: Schema.StringSchema<string, unknown>;
|
|
21
|
+
algorithm: Schema.StringSchema<string, unknown>;
|
|
22
|
+
}, any>;
|
|
23
|
+
}, unknown>;
|
|
24
|
+
export function create(kmsMetadataInput: Types.KMSMetadata | Types.KMSMetadataInput): Types.KMSMetadataView;
|
|
25
|
+
export function toJSON(kmsMetadata: Types.KMSMetadataView): Types.KMSMetadataInput;
|
|
26
|
+
export function parse(kmsMetadataInput: Types.KMSMetadataInput): Types.KMSMetadata;
|
|
27
|
+
export function archiveBlock(kmsMetadataInput: Types.KMSMetadata): Promise<import("@ucanto/interface").Block>;
|
|
28
|
+
export function archive(kmsMetadata: Types.KMSMetadata): Promise<Types.Result<Uint8Array>>;
|
|
29
|
+
export function extract(archive: Uint8Array): Types.Result<Types.KMSMetadataView, Types.UnknownFormat>;
|
|
30
|
+
export function view({ root }: {
|
|
31
|
+
root: Types.IPLDBlock;
|
|
32
|
+
}): Types.Result<Types.KMSMetadataView, Types.UnknownFormat>;
|
|
33
|
+
import * as Link from 'multiformats/link';
|
|
34
|
+
import { Schema } from '@ucanto/core';
|
|
35
|
+
import * as Types from '../../types.js';
|
|
36
|
+
//# sourceMappingURL=kms-metadata.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"kms-metadata.d.ts","sourceRoot":"","sources":["../../../src/core/metadata/kms-metadata.js"],"names":[],"mappings":"AASA,sBAAuB,wBAAwB,CAAA;AAE/C;;;;;;;;;;;YAWE;AAEF;;;;;;;;;YASE;AAgEK,yCAHI,KAAK,CAAC,WAAW,GAAC,KAAK,CAAC,gBAAgB,GACtC,KAAK,CAAC,eAAe,CAE2C;AAMtE,oCAHI,KAAK,CAAC,eAAe,GACnB,KAAK,CAAC,gBAAgB,CAOjC;AAMK,wCAHI,KAAK,CAAC,gBAAgB,GACpB,KAAK,CAAC,WAAW,CAO5B;AAMK,+CAHI,KAAK,CAAC,WAAW,GACf,OAAO,CAAC,OAAO,mBAAmB,EAAE,KAAK,CAAC,CAOtD;AAMM,qCAHI,KAAK,CAAC,WAAW,GACf,OAAO,CAAC,KAAK,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC,CAK7C;AAMM,iCAHI,UAAU,GACR,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,eAAe,EAAE,KAAK,CAAC,aAAa,CAAC,CAiBpE;AAOM,+BAHJ;IAAgC,IAAI,EAA5B,KAAK,CAAC,SAAS;CACvB,GAAU,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,eAAe,EAAE,KAAK,CAAC,aAAa,CAAC,CAiBpE;sBApLqB,mBAAmB;uBAEF,cAAc;uBAE9B,gBAAgB"}
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { CID } from 'multiformats';
|
|
2
|
+
import * as dagCBOR from '@ipld/dag-cbor';
|
|
3
|
+
import * as Link from 'multiformats/link';
|
|
4
|
+
import { sha256 } from 'multiformats/hashes/sha2';
|
|
5
|
+
import { CAR, ok, error, Schema } from '@ucanto/core';
|
|
6
|
+
import * as Types from '../../types.js';
|
|
7
|
+
import { UnknownFormat } from '../errors.js';
|
|
8
|
+
export const version = 'encrypted-metadata@0.2';
|
|
9
|
+
export const KMSMetadataSchema = Schema.variant({
|
|
10
|
+
[version]: Schema.struct({
|
|
11
|
+
encryptedDataCID: Schema.link(),
|
|
12
|
+
encryptedSymmetricKey: Schema.string(),
|
|
13
|
+
space: Schema.string(), // SpaceDID as string
|
|
14
|
+
kms: Schema.struct({
|
|
15
|
+
provider: Schema.string(),
|
|
16
|
+
keyId: Schema.string(),
|
|
17
|
+
algorithm: Schema.string(),
|
|
18
|
+
}),
|
|
19
|
+
}),
|
|
20
|
+
});
|
|
21
|
+
export const KMSMetadataInputSchema = Schema.struct({
|
|
22
|
+
encryptedDataCID: Schema.string(),
|
|
23
|
+
encryptedSymmetricKey: Schema.string(),
|
|
24
|
+
space: Schema.string(),
|
|
25
|
+
kms: Schema.struct({
|
|
26
|
+
provider: Schema.string(),
|
|
27
|
+
keyId: Schema.string(),
|
|
28
|
+
algorithm: Schema.string(),
|
|
29
|
+
}),
|
|
30
|
+
});
|
|
31
|
+
/** @implements {Types.KMSMetadataView} */
|
|
32
|
+
class KMSMetadata {
|
|
33
|
+
#encryptedDataCID;
|
|
34
|
+
#encryptedSymmetricKey;
|
|
35
|
+
#space;
|
|
36
|
+
#kms;
|
|
37
|
+
/** @param {Types.KMSMetadata|Types.KMSMetadataInput} kmsMetadataInput */
|
|
38
|
+
constructor(kmsMetadataInput) {
|
|
39
|
+
/** @type {Types.KMSMetadata} */
|
|
40
|
+
let kmsMetadata;
|
|
41
|
+
if (KMSMetadataInputSchema.is(kmsMetadataInput)) {
|
|
42
|
+
kmsMetadata = parse(
|
|
43
|
+
/** @type {Types.KMSMetadataInput} */ (kmsMetadataInput));
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
kmsMetadata = /** @type {Types.KMSMetadata} */ (kmsMetadataInput);
|
|
47
|
+
}
|
|
48
|
+
this.#encryptedDataCID = kmsMetadata.encryptedDataCID;
|
|
49
|
+
this.#encryptedSymmetricKey = kmsMetadata.encryptedSymmetricKey;
|
|
50
|
+
this.#space = kmsMetadata.space;
|
|
51
|
+
this.#kms = kmsMetadata.kms;
|
|
52
|
+
}
|
|
53
|
+
get encryptedDataCID() {
|
|
54
|
+
return this.#encryptedDataCID;
|
|
55
|
+
}
|
|
56
|
+
get encryptedSymmetricKey() {
|
|
57
|
+
return this.#encryptedSymmetricKey;
|
|
58
|
+
}
|
|
59
|
+
get space() {
|
|
60
|
+
return this.#space;
|
|
61
|
+
}
|
|
62
|
+
get kms() {
|
|
63
|
+
return this.#kms;
|
|
64
|
+
}
|
|
65
|
+
archiveBlock() {
|
|
66
|
+
/** @type {Types.KMSMetadata} */
|
|
67
|
+
const input = {
|
|
68
|
+
encryptedDataCID: this.encryptedDataCID,
|
|
69
|
+
encryptedSymmetricKey: this.encryptedSymmetricKey,
|
|
70
|
+
space: this.space,
|
|
71
|
+
kms: this.kms,
|
|
72
|
+
};
|
|
73
|
+
return archiveBlock(input);
|
|
74
|
+
}
|
|
75
|
+
/** @returns {Types.KMSMetadataInput} */
|
|
76
|
+
toJSON() {
|
|
77
|
+
return toJSON(this);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* @param {Types.KMSMetadata|Types.KMSMetadataInput} kmsMetadataInput
|
|
82
|
+
* @returns {Types.KMSMetadataView}
|
|
83
|
+
*/
|
|
84
|
+
export const create = (kmsMetadataInput) => new KMSMetadata(kmsMetadataInput);
|
|
85
|
+
/**
|
|
86
|
+
* @param {Types.KMSMetadataView} kmsMetadata
|
|
87
|
+
* @returns {Types.KMSMetadataInput}
|
|
88
|
+
*/
|
|
89
|
+
export const toJSON = (kmsMetadata) => ({
|
|
90
|
+
encryptedDataCID: kmsMetadata.encryptedDataCID.toString(),
|
|
91
|
+
encryptedSymmetricKey: kmsMetadata.encryptedSymmetricKey,
|
|
92
|
+
space: kmsMetadata.space,
|
|
93
|
+
kms: kmsMetadata.kms,
|
|
94
|
+
});
|
|
95
|
+
/**
|
|
96
|
+
* @param {Types.KMSMetadataInput} kmsMetadataInput
|
|
97
|
+
* @returns {Types.KMSMetadata}
|
|
98
|
+
*/
|
|
99
|
+
export const parse = (kmsMetadataInput) => ({
|
|
100
|
+
encryptedDataCID: CID.parse(kmsMetadataInput.encryptedDataCID),
|
|
101
|
+
encryptedSymmetricKey: kmsMetadataInput.encryptedSymmetricKey,
|
|
102
|
+
space: /** @type {Types.SpaceDID} */ (kmsMetadataInput.space),
|
|
103
|
+
kms: kmsMetadataInput.kms,
|
|
104
|
+
});
|
|
105
|
+
/**
|
|
106
|
+
* @param {Types.KMSMetadata} kmsMetadataInput
|
|
107
|
+
* @returns {Promise<import('@ucanto/interface').Block>}
|
|
108
|
+
*/
|
|
109
|
+
export const archiveBlock = async (kmsMetadataInput) => {
|
|
110
|
+
const bytes = dagCBOR.encode({ [version]: kmsMetadataInput });
|
|
111
|
+
const digest = await sha256.digest(bytes);
|
|
112
|
+
const cid = Link.create(dagCBOR.code, digest);
|
|
113
|
+
return { cid, bytes };
|
|
114
|
+
};
|
|
115
|
+
/**
|
|
116
|
+
* @param {Types.KMSMetadata} kmsMetadata
|
|
117
|
+
* @returns {Promise<Types.Result<Uint8Array>>}
|
|
118
|
+
*/
|
|
119
|
+
export const archive = async (kmsMetadata) => {
|
|
120
|
+
const block = await archiveBlock(kmsMetadata);
|
|
121
|
+
return ok(CAR.encode({ roots: [block] }));
|
|
122
|
+
};
|
|
123
|
+
/**
|
|
124
|
+
* @param {Uint8Array} archive
|
|
125
|
+
* @returns {Types.Result<Types.KMSMetadataView, Types.UnknownFormat>}
|
|
126
|
+
*/
|
|
127
|
+
export const extract = (archive) => {
|
|
128
|
+
const { roots } = CAR.decode(archive);
|
|
129
|
+
if (!roots.length) {
|
|
130
|
+
return error(new UnknownFormat('missing root block'));
|
|
131
|
+
}
|
|
132
|
+
const { code } = roots[0].cid;
|
|
133
|
+
if (code !== dagCBOR.code) {
|
|
134
|
+
return error(new UnknownFormat(`unexpected root CID codec: 0x${code.toString(16)}`));
|
|
135
|
+
}
|
|
136
|
+
return view({ root: roots[0] });
|
|
137
|
+
};
|
|
138
|
+
/**
|
|
139
|
+
* @param {object} source
|
|
140
|
+
* @param {Types.IPLDBlock} source.root
|
|
141
|
+
* @returns {Types.Result<Types.KMSMetadataView, Types.UnknownFormat>}
|
|
142
|
+
*/
|
|
143
|
+
export const view = ({ root }) => {
|
|
144
|
+
const value = dagCBOR.decode(root.bytes);
|
|
145
|
+
const [matchedVersion, kmsMetadataData] = KMSMetadataSchema.match(value);
|
|
146
|
+
switch (matchedVersion) {
|
|
147
|
+
case version: {
|
|
148
|
+
const kmsMetadata = create(
|
|
149
|
+
/** @type {Types.KMSMetadata}*/ (kmsMetadataData));
|
|
150
|
+
return ok(kmsMetadata);
|
|
151
|
+
}
|
|
152
|
+
default:
|
|
153
|
+
return error(new UnknownFormat(`unknown KMS metadata version: ${matchedVersion}`));
|
|
154
|
+
}
|
|
155
|
+
};
|
|
156
|
+
//# sourceMappingURL=kms-metadata.js.map
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export const version: "encrypted-metadata@0.1";
|
|
2
|
-
export const
|
|
2
|
+
export const LitMetadataSchema: Schema.VariantSchema<{
|
|
3
3
|
"encrypted-metadata@0.1": Schema.StructSchema<{
|
|
4
4
|
encryptedDataCID: Schema.Schema<Link.Link<unknown, number, number, 0 | 1>, any>;
|
|
5
5
|
identityBoundCiphertext: Schema.Schema<Uint8Array<ArrayBufferLike>, unknown>;
|
|
@@ -7,22 +7,22 @@ export const EncryptedMetadataSchema: Schema.VariantSchema<{
|
|
|
7
7
|
accessControlConditions: Schema.Schema<Schema.Dictionary<string, unknown>[], unknown>;
|
|
8
8
|
}, unknown>;
|
|
9
9
|
}, unknown>;
|
|
10
|
-
export const
|
|
10
|
+
export const LitMetadataInputSchema: Schema.StructSchema<{
|
|
11
11
|
encryptedDataCID: Schema.StringSchema<string, unknown>;
|
|
12
12
|
identityBoundCiphertext: Schema.StringSchema<string, unknown>;
|
|
13
13
|
plaintextKeyHash: Schema.StringSchema<string, unknown>;
|
|
14
14
|
accessControlConditions: Schema.Schema<Schema.Dictionary<string, unknown>[], unknown>;
|
|
15
15
|
}, unknown>;
|
|
16
|
-
export function create(encryptedMetadataInput: Types.
|
|
17
|
-
export function toJSON(encryptedMetadata: Types.
|
|
18
|
-
export function parse(encryptedMetadataInput: Types.
|
|
19
|
-
export function archiveBlock(encryptedMetadataInput: Types.
|
|
20
|
-
export function archive(
|
|
21
|
-
export function extract(archive: Uint8Array): Types.Result<Types.
|
|
16
|
+
export function create(encryptedMetadataInput: Types.LitMetadata | Types.LitMetadataInput): Types.LitMetadataView;
|
|
17
|
+
export function toJSON(encryptedMetadata: Types.LitMetadataView): Types.LitMetadataInput;
|
|
18
|
+
export function parse(encryptedMetadataInput: Types.LitMetadataInput): Types.LitMetadata;
|
|
19
|
+
export function archiveBlock(encryptedMetadataInput: Types.LitMetadata): Promise<import("@ucanto/interface").Block>;
|
|
20
|
+
export function archive(encryptedMetadata: Types.LitMetadata): Promise<Types.Result<Uint8Array>>;
|
|
21
|
+
export function extract(archive: Uint8Array): Types.Result<Types.LitMetadataView, Types.UnknownFormat>;
|
|
22
22
|
export function view({ root }: {
|
|
23
23
|
root: Types.IPLDBlock;
|
|
24
|
-
}): Types.Result<Types.
|
|
24
|
+
}): Types.Result<Types.LitMetadataView, Types.UnknownFormat>;
|
|
25
25
|
import * as Link from 'multiformats/link';
|
|
26
26
|
import { Schema } from '@ucanto/core';
|
|
27
|
-
import * as Types from '
|
|
28
|
-
//# sourceMappingURL=
|
|
27
|
+
import * as Types from '../../types.js';
|
|
28
|
+
//# sourceMappingURL=lit-metadata.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lit-metadata.d.ts","sourceRoot":"","sources":["../../../src/core/metadata/lit-metadata.js"],"names":[],"mappings":"AAUA,sBAAuB,wBAAwB,CAAA;AAE/C;;;;;;;YAUE;AAEF;;;;;YAQE;AAmEK,+CAHI,KAAK,CAAC,WAAW,GAAC,KAAK,CAAC,gBAAgB,GACtC,KAAK,CAAC,eAAe,CAGgB;AAM3C,0CAHI,KAAK,CAAC,eAAe,GACnB,KAAK,CAAC,gBAAgB,CASjC;AAMK,8CAHI,KAAK,CAAC,gBAAgB,GACpB,KAAK,CAAC,WAAW,CAS5B;AAMK,qDAHI,KAAK,CAAC,WAAW,GACf,OAAO,CAAC,OAAO,mBAAmB,EAAE,KAAK,CAAC,CAOtD;AAMM,2CAHI,KAAK,CAAC,WAAW,GACf,OAAO,CAAC,KAAK,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC,CAK7C;AAMM,iCAHI,UAAU,GACR,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,eAAe,EAAE,KAAK,CAAC,aAAa,CAAC,CAiBpE;AAOM,+BAHJ;IAAgC,IAAI,EAA5B,KAAK,CAAC,SAAS;CACvB,GAAU,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,eAAe,EAAE,KAAK,CAAC,aAAa,CAAC,CAiBpE;sBA3LqB,mBAAmB;uBAEF,cAAc;uBAE9B,gBAAgB"}
|
|
@@ -3,11 +3,11 @@ import * as dagCBOR from '@ipld/dag-cbor';
|
|
|
3
3
|
import * as Link from 'multiformats/link';
|
|
4
4
|
import { sha256 } from 'multiformats/hashes/sha2';
|
|
5
5
|
import { CAR, ok, error, Schema } from '@ucanto/core';
|
|
6
|
-
import * as Types from '
|
|
7
|
-
import { UnknownFormat } from '
|
|
8
|
-
import { stringToBytes, bytesToString } from '
|
|
6
|
+
import * as Types from '../../types.js';
|
|
7
|
+
import { UnknownFormat } from '../errors.js';
|
|
8
|
+
import { stringToBytes, bytesToString } from '../../utils.js';
|
|
9
9
|
export const version = 'encrypted-metadata@0.1';
|
|
10
|
-
export const
|
|
10
|
+
export const LitMetadataSchema = Schema.variant({
|
|
11
11
|
[version]: Schema.struct({
|
|
12
12
|
encryptedDataCID: Schema.link(),
|
|
13
13
|
identityBoundCiphertext: Schema.bytes(),
|
|
@@ -18,7 +18,7 @@ export const EncryptedMetadataSchema = Schema.variant({
|
|
|
18
18
|
}).array(),
|
|
19
19
|
}),
|
|
20
20
|
});
|
|
21
|
-
export const
|
|
21
|
+
export const LitMetadataInputSchema = Schema.struct({
|
|
22
22
|
encryptedDataCID: Schema.string(),
|
|
23
23
|
identityBoundCiphertext: Schema.string(),
|
|
24
24
|
plaintextKeyHash: Schema.string(),
|
|
@@ -27,22 +27,22 @@ export const EncryptedMetadataInputSchema = Schema.struct({
|
|
|
27
27
|
value: Schema.unknown(),
|
|
28
28
|
}).array(),
|
|
29
29
|
});
|
|
30
|
-
/** @implements {Types.
|
|
31
|
-
class
|
|
30
|
+
/** @implements {Types.LitMetadataView} */
|
|
31
|
+
class LitEncryptedMetadata {
|
|
32
32
|
#encryptedDataCID;
|
|
33
33
|
#identityBoundCiphertext;
|
|
34
34
|
#plaintextKeyHash;
|
|
35
35
|
#accessControlConditions;
|
|
36
|
-
/** @param {Types.
|
|
36
|
+
/** @param {Types.LitMetadata|Types.LitMetadataInput} encryptedMetadataInput */
|
|
37
37
|
constructor(encryptedMetadataInput) {
|
|
38
|
-
/** @type {Types.
|
|
38
|
+
/** @type {Types.LitMetadata} */
|
|
39
39
|
let encryptedMetadata;
|
|
40
|
-
if (
|
|
40
|
+
if (LitMetadataInputSchema.is(encryptedMetadataInput)) {
|
|
41
41
|
encryptedMetadata = parse(
|
|
42
|
-
/** @type {Types.
|
|
42
|
+
/** @type {Types.LitMetadataInput} */ (encryptedMetadataInput));
|
|
43
43
|
}
|
|
44
44
|
else {
|
|
45
|
-
encryptedMetadata = /** @type {Types.
|
|
45
|
+
encryptedMetadata = /** @type {Types.LitMetadata} */ (encryptedMetadataInput);
|
|
46
46
|
}
|
|
47
47
|
this.#encryptedDataCID = encryptedMetadata.encryptedDataCID;
|
|
48
48
|
this.#identityBoundCiphertext = encryptedMetadata.identityBoundCiphertext;
|
|
@@ -62,18 +62,8 @@ class EncryptedMetadata {
|
|
|
62
62
|
get accessControlConditions() {
|
|
63
63
|
return this.#accessControlConditions;
|
|
64
64
|
}
|
|
65
|
-
archive() {
|
|
66
|
-
/** @type {Types.EncryptedMetadata} */
|
|
67
|
-
const input = {
|
|
68
|
-
encryptedDataCID: this.encryptedDataCID,
|
|
69
|
-
identityBoundCiphertext: this.identityBoundCiphertext,
|
|
70
|
-
plaintextKeyHash: this.plaintextKeyHash,
|
|
71
|
-
accessControlConditions: this.accessControlConditions,
|
|
72
|
-
};
|
|
73
|
-
return archive(input);
|
|
74
|
-
}
|
|
75
65
|
archiveBlock() {
|
|
76
|
-
/** @type {Types.
|
|
66
|
+
/** @type {Types.LitMetadata} */
|
|
77
67
|
const input = {
|
|
78
68
|
encryptedDataCID: this.encryptedDataCID,
|
|
79
69
|
identityBoundCiphertext: this.identityBoundCiphertext,
|
|
@@ -82,19 +72,19 @@ class EncryptedMetadata {
|
|
|
82
72
|
};
|
|
83
73
|
return archiveBlock(input);
|
|
84
74
|
}
|
|
85
|
-
/** @returns {Types.
|
|
75
|
+
/** @returns {Types.LitMetadataInput} */
|
|
86
76
|
toJSON() {
|
|
87
77
|
return toJSON(this);
|
|
88
78
|
}
|
|
89
79
|
}
|
|
90
80
|
/**
|
|
91
|
-
* @param {Types.
|
|
92
|
-
* @returns {Types.
|
|
81
|
+
* @param {Types.LitMetadata|Types.LitMetadataInput} encryptedMetadataInput
|
|
82
|
+
* @returns {Types.LitMetadataView}
|
|
93
83
|
*/
|
|
94
|
-
export const create = (encryptedMetadataInput) => new
|
|
84
|
+
export const create = (encryptedMetadataInput) => new LitEncryptedMetadata(encryptedMetadataInput);
|
|
95
85
|
/**
|
|
96
|
-
* @param {Types.
|
|
97
|
-
* @returns {Types.
|
|
86
|
+
* @param {Types.LitMetadataView} encryptedMetadata
|
|
87
|
+
* @returns {Types.LitMetadataInput}
|
|
98
88
|
*/
|
|
99
89
|
export const toJSON = (encryptedMetadata) => ({
|
|
100
90
|
encryptedDataCID: encryptedMetadata.encryptedDataCID.toString(),
|
|
@@ -103,8 +93,8 @@ export const toJSON = (encryptedMetadata) => ({
|
|
|
103
93
|
accessControlConditions: encryptedMetadata.accessControlConditions,
|
|
104
94
|
});
|
|
105
95
|
/**
|
|
106
|
-
* @param {Types.
|
|
107
|
-
* @returns {Types.
|
|
96
|
+
* @param {Types.LitMetadataInput} encryptedMetadataInput
|
|
97
|
+
* @returns {Types.LitMetadata}
|
|
108
98
|
*/
|
|
109
99
|
export const parse = (encryptedMetadataInput) => ({
|
|
110
100
|
encryptedDataCID: CID.parse(encryptedMetadataInput.encryptedDataCID),
|
|
@@ -113,7 +103,7 @@ export const parse = (encryptedMetadataInput) => ({
|
|
|
113
103
|
accessControlConditions: encryptedMetadataInput.accessControlConditions,
|
|
114
104
|
});
|
|
115
105
|
/**
|
|
116
|
-
* @param {Types.
|
|
106
|
+
* @param {Types.LitMetadata} encryptedMetadataInput
|
|
117
107
|
* @returns {Promise<import('@ucanto/interface').Block>}
|
|
118
108
|
*/
|
|
119
109
|
export const archiveBlock = async (encryptedMetadataInput) => {
|
|
@@ -123,16 +113,16 @@ export const archiveBlock = async (encryptedMetadataInput) => {
|
|
|
123
113
|
return { cid, bytes };
|
|
124
114
|
};
|
|
125
115
|
/**
|
|
126
|
-
* @param {Types.
|
|
116
|
+
* @param {Types.LitMetadata} encryptedMetadata
|
|
127
117
|
* @returns {Promise<Types.Result<Uint8Array>>}
|
|
128
118
|
*/
|
|
129
|
-
export const archive = async (
|
|
130
|
-
const block = await archiveBlock(
|
|
119
|
+
export const archive = async (encryptedMetadata) => {
|
|
120
|
+
const block = await archiveBlock(encryptedMetadata);
|
|
131
121
|
return ok(CAR.encode({ roots: [block] }));
|
|
132
122
|
};
|
|
133
123
|
/**
|
|
134
124
|
* @param {Uint8Array} archive
|
|
135
|
-
* @returns {Types.Result<Types.
|
|
125
|
+
* @returns {Types.Result<Types.LitMetadataView, Types.UnknownFormat>}
|
|
136
126
|
*/
|
|
137
127
|
export const extract = (archive) => {
|
|
138
128
|
const { roots } = CAR.decode(archive);
|
|
@@ -148,19 +138,19 @@ export const extract = (archive) => {
|
|
|
148
138
|
/**
|
|
149
139
|
* @param {object} source
|
|
150
140
|
* @param {Types.IPLDBlock} source.root
|
|
151
|
-
* @returns {Types.Result<Types.
|
|
141
|
+
* @returns {Types.Result<Types.LitMetadataView, Types.UnknownFormat>}
|
|
152
142
|
*/
|
|
153
143
|
export const view = ({ root }) => {
|
|
154
144
|
const value = dagCBOR.decode(root.bytes);
|
|
155
|
-
const [
|
|
156
|
-
switch (
|
|
145
|
+
const [matchedVersion, encryptedMetadataData] = LitMetadataSchema.match(value);
|
|
146
|
+
switch (matchedVersion) {
|
|
157
147
|
case version: {
|
|
158
148
|
const encryptedMetadata = create(
|
|
159
|
-
/** @type {Types.
|
|
149
|
+
/** @type {Types.LitMetadata}*/ (encryptedMetadataData));
|
|
160
150
|
return ok(encryptedMetadata);
|
|
161
151
|
}
|
|
162
152
|
default:
|
|
163
|
-
return error(new UnknownFormat(`unknown
|
|
153
|
+
return error(new UnknownFormat(`unknown Lit metadata version: ${matchedVersion}`));
|
|
164
154
|
}
|
|
165
155
|
};
|
|
166
|
-
//# sourceMappingURL=
|
|
156
|
+
//# sourceMappingURL=lit-metadata.js.map
|