@storacha/encrypt-upload-client 1.1.70 → 1.1.71
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/package.json +4 -4
- package/dist/examples/decrypt-test.d.ts +0 -2
- package/dist/examples/decrypt-test.d.ts.map +0 -1
- package/dist/examples/decrypt-test.js +0 -102
- package/dist/examples/encrypt-test.d.ts +0 -2
- package/dist/examples/encrypt-test.d.ts.map +0 -1
- package/dist/examples/encrypt-test.js +0 -93
- package/dist/test/cid-verification.spec.d.ts +0 -2
- package/dist/test/cid-verification.spec.d.ts.map +0 -1
- package/dist/test/cid-verification.spec.js +0 -314
- package/dist/test/crypto-compatibility.spec.d.ts +0 -2
- package/dist/test/crypto-compatibility.spec.d.ts.map +0 -1
- package/dist/test/crypto-compatibility.spec.js +0 -124
- package/dist/test/crypto-counter-security.spec.d.ts +0 -2
- package/dist/test/crypto-counter-security.spec.d.ts.map +0 -1
- package/dist/test/crypto-counter-security.spec.js +0 -147
- package/dist/test/crypto-streaming.spec.d.ts +0 -2
- package/dist/test/crypto-streaming.spec.d.ts.map +0 -1
- package/dist/test/crypto-streaming.spec.js +0 -129
- package/dist/test/encrypted-metadata.spec.d.ts +0 -2
- package/dist/test/encrypted-metadata.spec.d.ts.map +0 -1
- package/dist/test/encrypted-metadata.spec.js +0 -68
- package/dist/test/factories.spec.d.ts +0 -2
- package/dist/test/factories.spec.d.ts.map +0 -1
- package/dist/test/factories.spec.js +0 -129
- package/dist/test/file-metadata.spec.d.ts +0 -2
- package/dist/test/file-metadata.spec.d.ts.map +0 -1
- package/dist/test/file-metadata.spec.js +0 -433
- package/dist/test/fixtures/test-fixtures.d.ts +0 -28
- package/dist/test/fixtures/test-fixtures.d.ts.map +0 -1
- package/dist/test/fixtures/test-fixtures.js +0 -63
- package/dist/test/helpers/test-file-utils.d.ts +0 -60
- package/dist/test/helpers/test-file-utils.d.ts.map +0 -1
- package/dist/test/helpers/test-file-utils.js +0 -139
- package/dist/test/https-enforcement.spec.d.ts +0 -2
- package/dist/test/https-enforcement.spec.d.ts.map +0 -1
- package/dist/test/https-enforcement.spec.js +0 -125
- package/dist/test/kms-crypto-adapter.spec.d.ts +0 -2
- package/dist/test/kms-crypto-adapter.spec.d.ts.map +0 -1
- package/dist/test/kms-crypto-adapter.spec.js +0 -305
- package/dist/test/lit-crypto-adapter.spec.d.ts +0 -2
- package/dist/test/lit-crypto-adapter.spec.d.ts.map +0 -1
- package/dist/test/lit-crypto-adapter.spec.js +0 -76
- package/dist/test/memory-efficiency.spec.d.ts +0 -2
- package/dist/test/memory-efficiency.spec.d.ts.map +0 -1
- package/dist/test/memory-efficiency.spec.js +0 -93
- package/dist/test/mocks/key-manager.d.ts +0 -58
- package/dist/test/mocks/key-manager.d.ts.map +0 -1
- package/dist/test/mocks/key-manager.js +0 -137
- package/dist/test/node-crypto-adapter.spec.d.ts +0 -2
- package/dist/test/node-crypto-adapter.spec.d.ts.map +0 -1
- package/dist/test/node-crypto-adapter.spec.js +0 -103
- package/dist/test/node-generic-crypto-adapter.spec.d.ts +0 -2
- package/dist/test/node-generic-crypto-adapter.spec.d.ts.map +0 -1
- package/dist/test/node-generic-crypto-adapter.spec.js +0 -95
- package/dist/test/setup.d.ts +0 -2
- package/dist/test/setup.d.ts.map +0 -1
- package/dist/test/setup.js +0 -12
- package/dist/tsconfig.spec.tsbuildinfo +0 -1
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@storacha/encrypt-upload-client",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.1.
|
|
4
|
+
"version": "1.1.71",
|
|
5
5
|
"license": "Apache-2.0 OR MIT",
|
|
6
6
|
"description": "Client for upload and download encrypted files",
|
|
7
7
|
"author": "Storacha",
|
|
@@ -82,9 +82,9 @@
|
|
|
82
82
|
"p-queue": "^9.0.1",
|
|
83
83
|
"p-retry": "^5.1.2",
|
|
84
84
|
"viem": "^2.39.0",
|
|
85
|
-
"@storacha/capabilities": "^2.
|
|
86
|
-
"@storacha/client": "^2.0.
|
|
87
|
-
"@storacha/upload-client": "^1.3.
|
|
85
|
+
"@storacha/capabilities": "^2.1.0",
|
|
86
|
+
"@storacha/client": "^2.0.1",
|
|
87
|
+
"@storacha/upload-client": "^1.3.8"
|
|
88
88
|
},
|
|
89
89
|
"devDependencies": {
|
|
90
90
|
"@lit-protocol/schemas": "^8.0.2",
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"decrypt-test.d.ts","sourceRoot":"","sources":["../../examples/decrypt-test.js"],"names":[],"mappings":""}
|
|
@@ -1,102 +0,0 @@
|
|
|
1
|
-
process.env.LOG_LEVEL = 'info';
|
|
2
|
-
import * as fs from 'fs';
|
|
3
|
-
import dotenv from 'dotenv';
|
|
4
|
-
import { CID } from 'multiformats';
|
|
5
|
-
import * as Client from '@storacha/client';
|
|
6
|
-
import * as Signer from '@ucanto/principal/ed25519';
|
|
7
|
-
import { StoreMemory } from '@storacha/client/stores/memory';
|
|
8
|
-
import { nagaTest } from '@lit-protocol/networks';
|
|
9
|
-
import { createLitClient } from '@lit-protocol/lit-client';
|
|
10
|
-
import { privateKeyToAccount } from 'viem/accounts';
|
|
11
|
-
import { create } from '../src/index.js';
|
|
12
|
-
import { serviceConf, receiptsEndpoint } from '../src/config/service.js';
|
|
13
|
-
import { createGenericLitAdapter } from '../src/crypto/factories.node.js';
|
|
14
|
-
import { extract } from '@ucanto/core/delegation';
|
|
15
|
-
import { createAuthManager, storagePlugins } from '@lit-protocol/auth';
|
|
16
|
-
dotenv.config();
|
|
17
|
-
const WALLET_PK = /** @type {`0x${string}`} */ (process.env.WALLET_PK) || '0x';
|
|
18
|
-
const DELEGATEE_AGENT_PK = process.env.DELEGATEE_AGENT_PK || '';
|
|
19
|
-
async function main() {
|
|
20
|
-
console.log('Starting encrypt-decrypt test example...');
|
|
21
|
-
// encrypted content CID
|
|
22
|
-
const cid = CID.parse('bafyreigmpvb4rhs6uod7qzxw65fxgruagg5x37swmf5p434cto36nlz7a4');
|
|
23
|
-
const delegationCarBuffer = fs.readFileSync('delegation.car');
|
|
24
|
-
const wallet = privateKeyToAccount(WALLET_PK);
|
|
25
|
-
const principal = Signer.parse(DELEGATEE_AGENT_PK);
|
|
26
|
-
const store = new StoreMemory();
|
|
27
|
-
const client = await Client.create({
|
|
28
|
-
principal,
|
|
29
|
-
store,
|
|
30
|
-
serviceConf,
|
|
31
|
-
receiptsEndpoint,
|
|
32
|
-
});
|
|
33
|
-
// Set up Lit client
|
|
34
|
-
const litClient = await createLitClient({
|
|
35
|
-
network: nagaTest,
|
|
36
|
-
});
|
|
37
|
-
const authManager = createAuthManager({
|
|
38
|
-
storage: storagePlugins.localStorageNode({
|
|
39
|
-
appName: 'my-app',
|
|
40
|
-
networkName: 'naga-local',
|
|
41
|
-
storagePath: './lit-auth-local',
|
|
42
|
-
}),
|
|
43
|
-
});
|
|
44
|
-
const encryptedClient = await create({
|
|
45
|
-
storachaClient: client,
|
|
46
|
-
cryptoAdapter: createGenericLitAdapter(litClient, authManager),
|
|
47
|
-
});
|
|
48
|
-
console.log('Extracting delegation from CAR file...');
|
|
49
|
-
const res = await extract(delegationCarBuffer);
|
|
50
|
-
if (res.error) {
|
|
51
|
-
throw new Error(`Failed to extract delegation: ${res.error.message}`);
|
|
52
|
-
}
|
|
53
|
-
const decryptDelegation = res.ok;
|
|
54
|
-
const decryptionCapability = decryptDelegation.capabilities.find((c) => c.can === 'space/content/decrypt');
|
|
55
|
-
if (!decryptionCapability) {
|
|
56
|
-
throw new Error('Failed to find decryption capability');
|
|
57
|
-
}
|
|
58
|
-
const spaceDID = /** @type {`did:key:${string}`} */ (decryptionCapability.with);
|
|
59
|
-
const paymentManager = await litClient.getPaymentManager({
|
|
60
|
-
account: wallet,
|
|
61
|
-
});
|
|
62
|
-
const balance = await paymentManager.getBalance({
|
|
63
|
-
userAddress: wallet.address,
|
|
64
|
-
});
|
|
65
|
-
console.log('Current Balance: ', balance.totalBalance);
|
|
66
|
-
/**
|
|
67
|
-
* Uncomment to deposit funds from your wallet to the Lit Payment Manager contract so you can pay for Lit Actions
|
|
68
|
-
* If you need testLPX tokens, please visit the faucet: https://chronicle-yellowstone-faucet.getlit.dev/
|
|
69
|
-
*/
|
|
70
|
-
// const depositReceipt = await paymentManager.deposit({
|
|
71
|
-
// amountInEth: '1',
|
|
72
|
-
// })
|
|
73
|
-
// console.log(`Deposit successful: ${depositReceipt.hash}`)
|
|
74
|
-
console.log('Retrieving and decrypting file...');
|
|
75
|
-
const decryptionConfig = {
|
|
76
|
-
wallet,
|
|
77
|
-
decryptDelegation,
|
|
78
|
-
spaceDID,
|
|
79
|
-
};
|
|
80
|
-
const decryptedContent = await encryptedClient.retrieveAndDecryptFile(cid, decryptionConfig);
|
|
81
|
-
const reader = decryptedContent.stream.getReader();
|
|
82
|
-
const decoder = new TextDecoder();
|
|
83
|
-
let result = '';
|
|
84
|
-
let done = false;
|
|
85
|
-
while (!done) {
|
|
86
|
-
const { value, done: isDone } = await reader.read();
|
|
87
|
-
done = isDone;
|
|
88
|
-
if (value) {
|
|
89
|
-
result += decoder.decode(value, { stream: true });
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
console.log('================ RESULT ===================');
|
|
93
|
-
console.log(result);
|
|
94
|
-
console.log('===========================================');
|
|
95
|
-
}
|
|
96
|
-
main()
|
|
97
|
-
.then(() => process.exit(0))
|
|
98
|
-
.catch((err) => {
|
|
99
|
-
console.error(err);
|
|
100
|
-
process.exit(1);
|
|
101
|
-
});
|
|
102
|
-
//# sourceMappingURL=decrypt-test.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"encrypt-test.d.ts","sourceRoot":"","sources":["../../examples/encrypt-test.js"],"names":[],"mappings":""}
|
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
import * as fs from 'fs';
|
|
2
|
-
import dotenv from 'dotenv';
|
|
3
|
-
import { DID } from '@ucanto/server';
|
|
4
|
-
import * as Client from '@storacha/client';
|
|
5
|
-
import * as Proof from '@storacha/client/proof';
|
|
6
|
-
import * as Signer from '@ucanto/principal/ed25519';
|
|
7
|
-
import { StoreMemory } from '@storacha/client/stores/memory';
|
|
8
|
-
import { decrypt } from '@storacha/capabilities/space';
|
|
9
|
-
import { nagaTest } from '@lit-protocol/networks';
|
|
10
|
-
import { createLitClient } from '@lit-protocol/lit-client';
|
|
11
|
-
import * as EncryptClient from '../src/index.js';
|
|
12
|
-
import { serviceConf, receiptsEndpoint } from '../src/config/service.js';
|
|
13
|
-
import { createGenericLitAdapter } from '../src/crypto/factories.node.js';
|
|
14
|
-
// import { CID } from 'multiformats'
|
|
15
|
-
import { createAuthManager, storagePlugins } from '@lit-protocol/auth';
|
|
16
|
-
dotenv.config();
|
|
17
|
-
const PROOF = process.env.PROOF || '';
|
|
18
|
-
const AGENT_PK = process.env.AGENT_PK || '';
|
|
19
|
-
const AUDIENCE_DID = process.env.AUDIENCE_DID || '';
|
|
20
|
-
async function main() {
|
|
21
|
-
console.log('Starting encrypt-upload-client example...');
|
|
22
|
-
// set up storacha client with a new agent
|
|
23
|
-
const principal = Signer.parse(AGENT_PK);
|
|
24
|
-
console.log(`Using agent: ${principal.did()}`);
|
|
25
|
-
const store = new StoreMemory();
|
|
26
|
-
const client = await Client.create({
|
|
27
|
-
principal,
|
|
28
|
-
store,
|
|
29
|
-
serviceConf,
|
|
30
|
-
receiptsEndpoint,
|
|
31
|
-
});
|
|
32
|
-
// now give Agent the delegation from the Space
|
|
33
|
-
const proof = await Proof.parse(PROOF);
|
|
34
|
-
const space = await client.addSpace(proof);
|
|
35
|
-
await client.setCurrentSpace(space.did());
|
|
36
|
-
console.log(`Using space: ${space.did()}, creating lit client...`);
|
|
37
|
-
// Set up Lit client
|
|
38
|
-
const litClient = await createLitClient({
|
|
39
|
-
network: nagaTest,
|
|
40
|
-
});
|
|
41
|
-
const authManager = createAuthManager({
|
|
42
|
-
storage: storagePlugins.localStorageNode({
|
|
43
|
-
appName: 'my-app',
|
|
44
|
-
networkName: 'naga-local',
|
|
45
|
-
storagePath: './lit-auth-local',
|
|
46
|
-
}),
|
|
47
|
-
});
|
|
48
|
-
const encryptedClient = await EncryptClient.create({
|
|
49
|
-
storachaClient: client,
|
|
50
|
-
cryptoAdapter: createGenericLitAdapter(litClient, authManager),
|
|
51
|
-
});
|
|
52
|
-
const fileContent = await fs.promises.readFile('./README.md');
|
|
53
|
-
const blob = new Blob([Uint8Array.from(fileContent)]);
|
|
54
|
-
// Create encryption config
|
|
55
|
-
const encryptionConfig = {
|
|
56
|
-
issuer: principal,
|
|
57
|
-
spaceDID: space.did(),
|
|
58
|
-
};
|
|
59
|
-
console.log('Encrypting and uploading file...');
|
|
60
|
-
const link = await encryptedClient.encryptAndUploadFile(blob, encryptionConfig);
|
|
61
|
-
console.log('Finished uploading file:', link);
|
|
62
|
-
// If we already have a CID
|
|
63
|
-
// const link = CID.parse(
|
|
64
|
-
// 'bafyreid6euzw23b5puazg6epubzaxibim7fioavoe6xydffibbzju4feae'
|
|
65
|
-
// )
|
|
66
|
-
console.log('🔄 Creating decrypt delegation with:');
|
|
67
|
-
const delegationOptions = {
|
|
68
|
-
issuer: principal,
|
|
69
|
-
audience: DID.parse(AUDIENCE_DID),
|
|
70
|
-
with: space.did(),
|
|
71
|
-
nb: {
|
|
72
|
-
resource: link,
|
|
73
|
-
},
|
|
74
|
-
expiration: new Date(Date.now() + 1000 * 60 * 10).getTime(), // 10 min,
|
|
75
|
-
proofs: [proof],
|
|
76
|
-
};
|
|
77
|
-
console.log({
|
|
78
|
-
...delegationOptions,
|
|
79
|
-
issuer: principal.did(),
|
|
80
|
-
audience: AUDIENCE_DID,
|
|
81
|
-
});
|
|
82
|
-
const delegation = await decrypt.delegate(delegationOptions);
|
|
83
|
-
const { ok: bytes } = await delegation.archive();
|
|
84
|
-
fs.writeFileSync('delegation.car', Buffer.from(/** @type Uint8Array<ArrayBufferLike>**/ (bytes)));
|
|
85
|
-
console.log(`✅ Delegation written to delegation.car`);
|
|
86
|
-
}
|
|
87
|
-
main()
|
|
88
|
-
.then(() => process.exit(0))
|
|
89
|
-
.catch((err) => {
|
|
90
|
-
console.error(err);
|
|
91
|
-
process.exit(1);
|
|
92
|
-
});
|
|
93
|
-
//# sourceMappingURL=encrypt-test.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"cid-verification.spec.d.ts","sourceRoot":"","sources":["../../test/cid-verification.spec.js"],"names":[],"mappings":""}
|
|
@@ -1,314 +0,0 @@
|
|
|
1
|
-
import assert from 'node:assert';
|
|
2
|
-
import { describe, test } from 'node:test';
|
|
3
|
-
import { getCarFileFromPublicGateway } from '../src/handlers/decrypt-handler.js';
|
|
4
|
-
import { createTestCar } from './helpers/test-file-utils.js';
|
|
5
|
-
await describe('CID Verification', async () => {
|
|
6
|
-
await describe('getCarFileFromPublicGateway', async () => {
|
|
7
|
-
await test('should construct correct gateway URL format', async () => {
|
|
8
|
-
// This is a basic test to verify the function exists and can be called
|
|
9
|
-
// Integration tests with real network calls would test full functionality
|
|
10
|
-
const gatewayURL = new URL('https://example.com');
|
|
11
|
-
const testCID = 'bafkreih5aznjvttude6c3wbvqeebb6rlx5wkbzyppv7z3aldwdht2oqadq';
|
|
12
|
-
// Mock fetch to avoid network calls in unit tests
|
|
13
|
-
const originalFetch = globalThis.fetch;
|
|
14
|
-
let capturedURL = '';
|
|
15
|
-
// @ts-ignore - Mock fetch for testing
|
|
16
|
-
globalThis.fetch = async (url) => {
|
|
17
|
-
capturedURL = url.toString();
|
|
18
|
-
// Return a mock response that looks like a failed fetch
|
|
19
|
-
return {
|
|
20
|
-
ok: false,
|
|
21
|
-
status: 404,
|
|
22
|
-
statusText: 'Not Found',
|
|
23
|
-
arrayBuffer: async () => {
|
|
24
|
-
throw new Error('Mock 404');
|
|
25
|
-
},
|
|
26
|
-
};
|
|
27
|
-
};
|
|
28
|
-
try {
|
|
29
|
-
await getCarFileFromPublicGateway(gatewayURL, testCID);
|
|
30
|
-
}
|
|
31
|
-
catch (error) {
|
|
32
|
-
// We expect this to fail with our mock, that's fine
|
|
33
|
-
}
|
|
34
|
-
finally {
|
|
35
|
-
globalThis.fetch = originalFetch;
|
|
36
|
-
}
|
|
37
|
-
// Verify the URL was constructed correctly
|
|
38
|
-
const expectedURL = `https://example.com/ipfs/${testCID}?format=car`;
|
|
39
|
-
assert.strictEqual(capturedURL, expectedURL, 'Should construct correct gateway URL');
|
|
40
|
-
});
|
|
41
|
-
await test('should be exported and callable', async () => {
|
|
42
|
-
// Basic smoke test
|
|
43
|
-
assert.strictEqual(typeof getCarFileFromPublicGateway, 'function', 'Should be a function');
|
|
44
|
-
// Verify it requires proper parameters
|
|
45
|
-
try {
|
|
46
|
-
// @ts-ignore - intentionally testing invalid input
|
|
47
|
-
await getCarFileFromPublicGateway(null, 'test');
|
|
48
|
-
assert.fail('Should throw error for null gateway URL');
|
|
49
|
-
}
|
|
50
|
-
catch (error) {
|
|
51
|
-
// Expected to fail - good
|
|
52
|
-
assert(error instanceof Error, 'Should throw an Error');
|
|
53
|
-
}
|
|
54
|
-
});
|
|
55
|
-
await test('should validate CID format', async () => {
|
|
56
|
-
const gatewayURL = new URL('https://example.com');
|
|
57
|
-
// Mock fetch to return valid CAR response
|
|
58
|
-
const originalFetch = globalThis.fetch;
|
|
59
|
-
// @ts-ignore - Mock fetch for testing
|
|
60
|
-
globalThis.fetch = async (url) => ({
|
|
61
|
-
ok: true,
|
|
62
|
-
status: 200,
|
|
63
|
-
statusText: 'OK',
|
|
64
|
-
arrayBuffer: async () => new ArrayBuffer(100), // Mock CAR data
|
|
65
|
-
});
|
|
66
|
-
try {
|
|
67
|
-
await getCarFileFromPublicGateway(gatewayURL, 'invalid-cid');
|
|
68
|
-
assert.fail('Should throw error for invalid CID');
|
|
69
|
-
}
|
|
70
|
-
catch (error) {
|
|
71
|
-
// Expected to fail due to invalid CID format
|
|
72
|
-
assert(error instanceof Error, 'Should throw an Error for invalid CID');
|
|
73
|
-
}
|
|
74
|
-
finally {
|
|
75
|
-
globalThis.fetch = originalFetch;
|
|
76
|
-
}
|
|
77
|
-
});
|
|
78
|
-
await test('should accept valid CAR file with matching root CID', async () => {
|
|
79
|
-
const gatewayURL = new URL('https://example.com');
|
|
80
|
-
// Create a valid KMS metadata CAR file
|
|
81
|
-
const testContent = {
|
|
82
|
-
encryptedDataCID: 'bafkreih5aznjvttude6c3wbvqeebb6rlx5wkbzyppv7z3aldwdht2oqadq',
|
|
83
|
-
encryptedSymmetricKey: 'test-encrypted-key',
|
|
84
|
-
space: 'did:key:z6MkwDK3M4PxU1FqcSt6quBH1xRBSGnPRdQYP9B13h3Wq5X1',
|
|
85
|
-
kms: {
|
|
86
|
-
provider: 'google-kms',
|
|
87
|
-
keyId: 'test-key-id',
|
|
88
|
-
algorithm: 'RSA-OAEP-2048-SHA256',
|
|
89
|
-
},
|
|
90
|
-
};
|
|
91
|
-
const { car, actualRootCID } = await createTestCar(testContent);
|
|
92
|
-
// Mock fetch to return the valid CAR
|
|
93
|
-
const originalFetch = globalThis.fetch;
|
|
94
|
-
// @ts-ignore - Mock fetch for testing
|
|
95
|
-
globalThis.fetch = async (url) => ({
|
|
96
|
-
ok: true,
|
|
97
|
-
status: 200,
|
|
98
|
-
statusText: 'OK',
|
|
99
|
-
arrayBuffer: async () => car.buffer,
|
|
100
|
-
});
|
|
101
|
-
try {
|
|
102
|
-
const result = await getCarFileFromPublicGateway(gatewayURL, actualRootCID.toString());
|
|
103
|
-
assert(result instanceof Uint8Array, 'Should return Uint8Array');
|
|
104
|
-
assert.deepStrictEqual(result, car, 'Should return the exact CAR file');
|
|
105
|
-
}
|
|
106
|
-
finally {
|
|
107
|
-
globalThis.fetch = originalFetch;
|
|
108
|
-
}
|
|
109
|
-
});
|
|
110
|
-
await test('should reject CAR file with wrong root CID (tampering detection)', async () => {
|
|
111
|
-
const gatewayURL = new URL('https://example.com');
|
|
112
|
-
// Create a CAR file with content A
|
|
113
|
-
const originalContent = {
|
|
114
|
-
encryptedDataCID: 'bafkreih5aznjvttude6c3wbvqeebb6rlx5wkbzyppv7z3aldwdht2oqadq',
|
|
115
|
-
encryptedSymmetricKey: 'original-encrypted-key',
|
|
116
|
-
space: 'did:key:z6MkwDK3M4PxU1FqcSt6quBH1xRBSGnPRdQYP9B13h3Wq5X1',
|
|
117
|
-
kms: {
|
|
118
|
-
provider: 'google-kms',
|
|
119
|
-
keyId: 'original-key-id',
|
|
120
|
-
algorithm: 'RSA-OAEP-2048-SHA256',
|
|
121
|
-
},
|
|
122
|
-
};
|
|
123
|
-
const { actualRootCID: originalCID } = await createTestCar(originalContent);
|
|
124
|
-
// Create a different CAR file with content B
|
|
125
|
-
const tamperedContent = {
|
|
126
|
-
encryptedDataCID: 'bafkreidb6v6sjfnpnf6lqkh7p4w7zfzqfuzn2lqhp5x6zkojfuzwzlhpny',
|
|
127
|
-
encryptedSymmetricKey: 'tampered-encrypted-key',
|
|
128
|
-
space: 'did:key:z6MkMaliciousSpaceDIDThatShouldNotBeAccepted',
|
|
129
|
-
kms: {
|
|
130
|
-
provider: 'google-kms',
|
|
131
|
-
keyId: 'tampered-key-id',
|
|
132
|
-
algorithm: 'RSA-OAEP-2048-SHA256',
|
|
133
|
-
},
|
|
134
|
-
};
|
|
135
|
-
const { car: tamperedCar } = await createTestCar(tamperedContent);
|
|
136
|
-
// Mock fetch to return the tampered CAR when requesting the original CID
|
|
137
|
-
const originalFetch = globalThis.fetch;
|
|
138
|
-
// @ts-ignore - Mock fetch for testing
|
|
139
|
-
globalThis.fetch = async (url) => ({
|
|
140
|
-
ok: true,
|
|
141
|
-
status: 200,
|
|
142
|
-
statusText: 'OK',
|
|
143
|
-
arrayBuffer: async () => tamperedCar.buffer, // Return wrong CAR!
|
|
144
|
-
});
|
|
145
|
-
try {
|
|
146
|
-
await getCarFileFromPublicGateway(gatewayURL, originalCID.toString());
|
|
147
|
-
assert.fail('Should throw error for CID verification failure');
|
|
148
|
-
}
|
|
149
|
-
catch (error) {
|
|
150
|
-
assert(error instanceof Error, 'Should throw an Error');
|
|
151
|
-
assert(error.message.includes('CID verification failed'), `Should mention CID verification failure. Got: ${error.message}`);
|
|
152
|
-
}
|
|
153
|
-
finally {
|
|
154
|
-
globalThis.fetch = originalFetch;
|
|
155
|
-
}
|
|
156
|
-
});
|
|
157
|
-
await test('should detect when malicious gateway serves completely different CAR', async () => {
|
|
158
|
-
const gatewayURL = new URL('https://example.com');
|
|
159
|
-
// CID we're requesting
|
|
160
|
-
const requestedCID = 'bafkreih5aznjvttude6c3wbvqeebb6rlx5wkbzyppv7z3aldwdht2oqadq';
|
|
161
|
-
// Create a completely different CAR file
|
|
162
|
-
const maliciousContent = {
|
|
163
|
-
encryptedDataCID: 'bafkreig6h5fimhfvj3wmlsf4fzj2d2ndqxd7qnugcgcmkn6dcqzxcq5zdu',
|
|
164
|
-
encryptedSymmetricKey: 'malicious-encrypted-key',
|
|
165
|
-
space: 'did:key:z6MkMaliciousSpaceDIDControlledByAttacker',
|
|
166
|
-
kms: {
|
|
167
|
-
provider: 'google-kms',
|
|
168
|
-
keyId: 'attacker-controlled-key',
|
|
169
|
-
algorithm: 'RSA-OAEP-2048-SHA256',
|
|
170
|
-
},
|
|
171
|
-
};
|
|
172
|
-
const { car: maliciousCar } = await createTestCar(maliciousContent);
|
|
173
|
-
// Mock fetch to return the malicious CAR
|
|
174
|
-
const originalFetch = globalThis.fetch;
|
|
175
|
-
// @ts-ignore - Mock fetch for testing
|
|
176
|
-
globalThis.fetch = async (url) => ({
|
|
177
|
-
ok: true,
|
|
178
|
-
status: 200,
|
|
179
|
-
statusText: 'OK',
|
|
180
|
-
arrayBuffer: async () => maliciousCar.buffer,
|
|
181
|
-
});
|
|
182
|
-
try {
|
|
183
|
-
await getCarFileFromPublicGateway(gatewayURL, requestedCID);
|
|
184
|
-
assert.fail('Should throw error when gateway serves wrong CAR');
|
|
185
|
-
}
|
|
186
|
-
catch (error) {
|
|
187
|
-
assert(error instanceof Error, 'Should throw an Error');
|
|
188
|
-
assert(error.message.includes('CID verification failed'), `Should mention CID verification failure. Got: ${error.message}`);
|
|
189
|
-
}
|
|
190
|
-
finally {
|
|
191
|
-
globalThis.fetch = originalFetch;
|
|
192
|
-
}
|
|
193
|
-
});
|
|
194
|
-
await test('should handle network errors gracefully', async () => {
|
|
195
|
-
const gatewayURL = new URL('https://example.com');
|
|
196
|
-
const testCID = 'bafkreih5aznjvttude6c3wbvqeebb6rlx5wkbzyppv7z3aldwdht2oqadq';
|
|
197
|
-
// Mock fetch to return network error
|
|
198
|
-
const originalFetch = globalThis.fetch;
|
|
199
|
-
// @ts-ignore - Mock fetch for testing
|
|
200
|
-
globalThis.fetch = async (url) => ({
|
|
201
|
-
ok: false,
|
|
202
|
-
status: 500,
|
|
203
|
-
statusText: 'Internal Server Error',
|
|
204
|
-
arrayBuffer: async () => {
|
|
205
|
-
throw new Error('Network error');
|
|
206
|
-
},
|
|
207
|
-
});
|
|
208
|
-
try {
|
|
209
|
-
await getCarFileFromPublicGateway(gatewayURL, testCID);
|
|
210
|
-
assert.fail('Should throw error for network errors');
|
|
211
|
-
}
|
|
212
|
-
catch (error) {
|
|
213
|
-
assert(error instanceof Error, 'Should throw an Error');
|
|
214
|
-
assert(error.message.includes('Failed to fetch'), `Should mention fetch failure. Got: ${error.message}`);
|
|
215
|
-
}
|
|
216
|
-
finally {
|
|
217
|
-
globalThis.fetch = originalFetch;
|
|
218
|
-
}
|
|
219
|
-
});
|
|
220
|
-
});
|
|
221
|
-
await describe('Metadata Tampering Detection', async () => {
|
|
222
|
-
await test('should prevent modification of space DID in metadata', async () => {
|
|
223
|
-
const gatewayURL = new URL('https://example.com');
|
|
224
|
-
// Original metadata with legitimate space DID
|
|
225
|
-
const originalMetadata = {
|
|
226
|
-
encryptedDataCID: 'bafkreie2hvzhqzj3ixnmjh7h3nkhdyp6qxhqltkq6qxf3wxq7hqxd6nzde',
|
|
227
|
-
encryptedSymmetricKey: 'encrypted-key-data',
|
|
228
|
-
space: 'did:key:z6MkwDK3M4PxU1FqcSt6quBH1xRBSGnPRdQYP9B13h3Wq5X1', // Original space
|
|
229
|
-
kms: {
|
|
230
|
-
provider: 'google-kms',
|
|
231
|
-
keyId: 'test-key-id',
|
|
232
|
-
algorithm: 'RSA-OAEP-2048-SHA256',
|
|
233
|
-
},
|
|
234
|
-
};
|
|
235
|
-
// Tampered metadata with different space DID
|
|
236
|
-
const tamperedMetadata = {
|
|
237
|
-
...originalMetadata,
|
|
238
|
-
space: 'did:key:z6MkMaliciousSpaceDIDThatShouldNotBeAccepted', // Malicious space!
|
|
239
|
-
};
|
|
240
|
-
// Create CAR files for both
|
|
241
|
-
const { actualRootCID: originalCID } = await createTestCar(originalMetadata);
|
|
242
|
-
const { car: tamperedCar } = await createTestCar(tamperedMetadata);
|
|
243
|
-
// Mock fetch to return tampered CAR when requesting original CID
|
|
244
|
-
const originalFetch = globalThis.fetch;
|
|
245
|
-
// @ts-ignore - Mock fetch for testing
|
|
246
|
-
globalThis.fetch = async (url) => ({
|
|
247
|
-
ok: true,
|
|
248
|
-
status: 200,
|
|
249
|
-
statusText: 'OK',
|
|
250
|
-
arrayBuffer: async () => tamperedCar.buffer, // Returns tampered metadata!
|
|
251
|
-
});
|
|
252
|
-
try {
|
|
253
|
-
await getCarFileFromPublicGateway(gatewayURL, originalCID.toString());
|
|
254
|
-
assert.fail('Should detect space DID tampering via CID verification');
|
|
255
|
-
}
|
|
256
|
-
catch (error) {
|
|
257
|
-
assert(error instanceof Error, 'Should throw an Error');
|
|
258
|
-
assert(error.message.includes('CID verification failed'), `Should catch tampering via CID verification. Got: ${error.message}`);
|
|
259
|
-
}
|
|
260
|
-
finally {
|
|
261
|
-
globalThis.fetch = originalFetch;
|
|
262
|
-
}
|
|
263
|
-
});
|
|
264
|
-
await test('should prevent complete metadata substitution attacks', async () => {
|
|
265
|
-
const gatewayURL = new URL('https://example.com');
|
|
266
|
-
// Original legitimate metadata
|
|
267
|
-
const originalMetadata = {
|
|
268
|
-
encryptedDataCID: 'bafkreih5aznjvttude6c3wbvqeebb6rlx5wkbzyppv7z3aldwdht2oqadq',
|
|
269
|
-
encryptedSymmetricKey: 'original-encrypted-key',
|
|
270
|
-
space: 'did:key:z6MkwDK3M4PxU1FqcSt6quBH1xRBSGnPRdQYP9B13h3Wq5X1',
|
|
271
|
-
kms: {
|
|
272
|
-
provider: 'google-kms',
|
|
273
|
-
keyId: 'legitimate-key-id',
|
|
274
|
-
algorithm: 'RSA-OAEP-2048-SHA256',
|
|
275
|
-
},
|
|
276
|
-
};
|
|
277
|
-
// Completely different malicious metadata (using same CID but different content)
|
|
278
|
-
const maliciousMetadata = {
|
|
279
|
-
encryptedDataCID: 'bafkreih5aznjvttude6c3wbvqeebb6rlx5wkbzyppv7z3aldwdht2oqadq',
|
|
280
|
-
encryptedSymmetricKey: 'malicious-encrypted-key',
|
|
281
|
-
space: 'did:key:z6MkMaliciousSpaceDIDControlledByAttacker',
|
|
282
|
-
kms: {
|
|
283
|
-
provider: 'google-kms',
|
|
284
|
-
keyId: 'attacker-controlled-key',
|
|
285
|
-
algorithm: 'RSA-OAEP-2048-SHA256',
|
|
286
|
-
},
|
|
287
|
-
};
|
|
288
|
-
// Create CAR files for both
|
|
289
|
-
const { actualRootCID: originalCID } = await createTestCar(originalMetadata);
|
|
290
|
-
const { car: maliciousCar } = await createTestCar(maliciousMetadata);
|
|
291
|
-
// Mock fetch to return completely different metadata
|
|
292
|
-
const originalFetch = globalThis.fetch;
|
|
293
|
-
// @ts-ignore - Mock fetch for testing
|
|
294
|
-
globalThis.fetch = async (url) => ({
|
|
295
|
-
ok: true,
|
|
296
|
-
status: 200,
|
|
297
|
-
statusText: 'OK',
|
|
298
|
-
arrayBuffer: async () => maliciousCar.buffer, // Complete substitution attack!
|
|
299
|
-
});
|
|
300
|
-
try {
|
|
301
|
-
await getCarFileFromPublicGateway(gatewayURL, originalCID.toString());
|
|
302
|
-
assert.fail('Should detect complete metadata substitution via CID verification');
|
|
303
|
-
}
|
|
304
|
-
catch (error) {
|
|
305
|
-
assert(error instanceof Error, 'Should throw an Error');
|
|
306
|
-
assert(error.message.includes('CID verification failed'), `Should catch complete substitution via CID verification. Got: ${error.message}`);
|
|
307
|
-
}
|
|
308
|
-
finally {
|
|
309
|
-
globalThis.fetch = originalFetch;
|
|
310
|
-
}
|
|
311
|
-
});
|
|
312
|
-
});
|
|
313
|
-
});
|
|
314
|
-
//# sourceMappingURL=cid-verification.spec.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"crypto-compatibility.spec.d.ts","sourceRoot":"","sources":["../../test/crypto-compatibility.spec.js"],"names":[],"mappings":""}
|