@storacha/encrypt-upload-client 1.1.56 → 1.1.58
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/constants.d.ts +3 -3
- package/dist/config/constants.js +4 -3
- package/dist/config/env.d.ts +9 -6
- package/dist/config/service.d.ts +13 -13
- package/dist/core/client.d.ts +54 -41
- package/dist/core/client.js +68 -56
- package/dist/core/errors.d.ts +6 -6
- package/dist/core/metadata/encrypted-metadata.d.ts +13 -8
- package/dist/core/metadata/kms-metadata.d.ts +68 -36
- package/dist/core/metadata/lit-metadata.d.ts +63 -28
- package/dist/crypto/adapters/kms-crypto-adapter.d.ts +172 -137
- package/dist/crypto/adapters/lit-crypto-adapter.d.ts +107 -86
- package/dist/crypto/factories.browser.d.ts +9 -5
- package/dist/crypto/factories.browser.js +15 -7
- package/dist/crypto/factories.node.d.ts +13 -6
- package/dist/crypto/factories.node.js +19 -13
- package/dist/crypto/index.d.ts +5 -5
- package/dist/crypto/index.js +5 -5
- package/dist/crypto/symmetric/generic-aes-ctr-streaming-crypto.d.ts +58 -54
- package/dist/crypto/symmetric/generic-aes-ctr-streaming-crypto.js +174 -146
- package/dist/crypto/symmetric/node-aes-cbc-crypto.d.ts +36 -32
- package/dist/crypto/symmetric/node-aes-cbc-crypto.js +101 -95
- package/dist/examples/decrypt-test.d.ts +2 -2
- package/dist/examples/decrypt-test.js +78 -69
- package/dist/examples/encrypt-test.d.ts +5 -3
- package/dist/examples/encrypt-test.js +58 -55
- package/dist/handlers/decrypt-handler.d.ts +19 -5
- package/dist/handlers/encrypt-handler.d.ts +9 -3
- package/dist/handlers/encrypt-handler.js +93 -57
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/dist/protocols/lit.d.ts +33 -9
- package/dist/protocols/lit.js +134 -98
- package/dist/test/cid-verification.spec.d.ts +2 -2
- package/dist/test/cid-verification.spec.js +341 -313
- package/dist/test/crypto-compatibility.spec.d.ts +2 -2
- package/dist/test/crypto-compatibility.spec.js +184 -120
- package/dist/test/crypto-counter-security.spec.d.ts +2 -2
- package/dist/test/crypto-counter-security.spec.js +177 -138
- package/dist/test/crypto-streaming.spec.d.ts +2 -2
- package/dist/test/crypto-streaming.spec.js +208 -126
- package/dist/test/encrypted-metadata.spec.d.ts +2 -2
- package/dist/test/encrypted-metadata.spec.js +89 -62
- package/dist/test/factories.spec.d.ts +2 -2
- package/dist/test/factories.spec.js +275 -139
- package/dist/test/file-metadata.spec.d.ts +2 -2
- package/dist/test/file-metadata.spec.js +472 -416
- package/dist/test/fixtures/test-fixtures.d.ts +25 -20
- package/dist/test/fixtures/test-fixtures.js +61 -53
- package/dist/test/helpers/test-file-utils.d.ts +19 -14
- package/dist/test/helpers/test-file-utils.js +78 -76
- package/dist/test/https-enforcement.spec.d.ts +2 -2
- package/dist/test/https-enforcement.spec.js +278 -124
- package/dist/test/kms-crypto-adapter.spec.d.ts +2 -2
- package/dist/test/kms-crypto-adapter.spec.js +473 -304
- package/dist/test/lit-crypto-adapter.spec.d.ts +2 -2
- package/dist/test/lit-crypto-adapter.spec.js +206 -118
- package/dist/test/memory-efficiency.spec.d.ts +2 -2
- package/dist/test/memory-efficiency.spec.js +100 -87
- package/dist/test/mocks/key-manager.d.ts +71 -38
- package/dist/test/mocks/key-manager.js +129 -113
- package/dist/test/node-crypto-adapter.spec.d.ts +2 -2
- package/dist/test/node-crypto-adapter.spec.js +155 -102
- package/dist/test/node-generic-crypto-adapter.spec.d.ts +2 -2
- package/dist/test/node-generic-crypto-adapter.spec.js +134 -94
- package/dist/test/setup.d.ts +2 -2
- package/dist/test/setup.js +8 -9
- package/dist/tsconfig.spec.tsbuildinfo +1 -1
- package/dist/types.d.ts +219 -181
- package/dist/utils/file-metadata.d.ts +19 -13
- package/dist/utils.d.ts +14 -5
- package/package.json +4 -4
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { randomBytes, createCipheriv, createDecipheriv } from 'crypto'
|
|
2
|
-
import * as Type from '../../types.js'
|
|
3
|
-
const ENCRYPTION_ALGORITHM = 'aes-256-cbc'
|
|
4
|
-
const KEY_LENGTH = 256
|
|
5
|
-
const IV_LENGTH = 16
|
|
1
|
+
import { randomBytes, createCipheriv, createDecipheriv } from 'crypto'
|
|
2
|
+
import * as Type from '../../types.js'
|
|
3
|
+
const ENCRYPTION_ALGORITHM = 'aes-256-cbc'
|
|
4
|
+
const KEY_LENGTH = 256 // bits
|
|
5
|
+
const IV_LENGTH = 16 // bytes (128 bits, used as initialization vector)
|
|
6
6
|
/**
|
|
7
7
|
* NodeAesCbcCrypto implements AES-CBC symmetric encryption for Node.js environments.
|
|
8
8
|
* It uses AES-CBC mode for encryption via the Node.js crypto module.
|
|
@@ -13,98 +13,104 @@ const IV_LENGTH = 16; // bytes (128 bits, used as initialization vector)
|
|
|
13
13
|
* @implements {Type.SymmetricCrypto}
|
|
14
14
|
*/
|
|
15
15
|
export class NodeAesCbcCrypto {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
if (final.length) {
|
|
31
|
-
controller.enqueue(final);
|
|
32
|
-
}
|
|
33
|
-
},
|
|
34
|
-
});
|
|
35
|
-
return Promise.resolve({
|
|
36
|
-
key: symmetricKey,
|
|
37
|
-
iv: initializationVector,
|
|
38
|
-
encryptedStream: data.stream().pipeThrough(encryptStream),
|
|
39
|
-
});
|
|
40
|
-
}
|
|
41
|
-
/**
|
|
42
|
-
* @param {ReadableStream} encryptedData
|
|
43
|
-
* @param {Uint8Array} key
|
|
44
|
-
* @param {Uint8Array} iv
|
|
45
|
-
*/
|
|
46
|
-
async decryptStream(encryptedData, key, iv) {
|
|
47
|
-
const decipher = createDecipheriv(ENCRYPTION_ALGORITHM, key, iv);
|
|
48
|
-
const decryptor = new TransformStream({
|
|
49
|
-
async transform(chunk, controller) {
|
|
50
|
-
try {
|
|
51
|
-
const decryptedChunk = decipher.update(chunk);
|
|
52
|
-
if (decryptedChunk.length > 0) {
|
|
53
|
-
controller.enqueue(decryptedChunk);
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
catch (err) {
|
|
57
|
-
controller.error(err);
|
|
58
|
-
}
|
|
59
|
-
},
|
|
60
|
-
flush(controller) {
|
|
61
|
-
try {
|
|
62
|
-
const finalChunk = decipher.final();
|
|
63
|
-
if (finalChunk.length > 0) {
|
|
64
|
-
controller.enqueue(finalChunk);
|
|
65
|
-
}
|
|
66
|
-
controller.terminate();
|
|
67
|
-
}
|
|
68
|
-
catch (err) {
|
|
69
|
-
controller.error(err);
|
|
70
|
-
}
|
|
71
|
-
},
|
|
72
|
-
});
|
|
73
|
-
return Promise.resolve(encryptedData.pipeThrough(decryptor));
|
|
74
|
-
}
|
|
75
|
-
/**
|
|
76
|
-
* Combine key and IV into a single array for AES-CBC
|
|
77
|
-
*
|
|
78
|
-
* @param {Uint8Array} key - The AES key (KEY_LENGTH/8 bytes)
|
|
79
|
-
* @param {Uint8Array} iv - The AES-CBC IV (IV_LENGTH bytes)
|
|
80
|
-
* @returns {Uint8Array} Combined key and IV (KEY_LENGTH/8 + IV_LENGTH bytes)
|
|
81
|
-
*/
|
|
82
|
-
combineKeyAndIV(key, iv) {
|
|
83
|
-
const keyBytes = KEY_LENGTH / 8;
|
|
84
|
-
if (key.length !== keyBytes) {
|
|
85
|
-
throw new Error(`AES-${KEY_LENGTH} key must be ${keyBytes} bytes, got ${key.length}`);
|
|
16
|
+
/** @param {Type.BlobLike} data */
|
|
17
|
+
async encryptStream(data) {
|
|
18
|
+
const symmetricKey = randomBytes(KEY_LENGTH / 8) // KEY_LENGTH bits for AES
|
|
19
|
+
const initializationVector = randomBytes(IV_LENGTH) // IV_LENGTH bytes for AES
|
|
20
|
+
const cipher = createCipheriv(
|
|
21
|
+
ENCRYPTION_ALGORITHM,
|
|
22
|
+
symmetricKey,
|
|
23
|
+
initializationVector
|
|
24
|
+
)
|
|
25
|
+
const encryptStream = new TransformStream({
|
|
26
|
+
transform: async (chunk, controller) => {
|
|
27
|
+
const encryptedChunk = cipher.update(chunk)
|
|
28
|
+
if (encryptedChunk.length) {
|
|
29
|
+
controller.enqueue(encryptedChunk)
|
|
86
30
|
}
|
|
87
|
-
|
|
88
|
-
|
|
31
|
+
},
|
|
32
|
+
flush: (controller) => {
|
|
33
|
+
const final = cipher.final()
|
|
34
|
+
if (final.length) {
|
|
35
|
+
controller.enqueue(final)
|
|
89
36
|
}
|
|
90
|
-
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
37
|
+
},
|
|
38
|
+
})
|
|
39
|
+
return Promise.resolve({
|
|
40
|
+
key: symmetricKey,
|
|
41
|
+
iv: initializationVector,
|
|
42
|
+
encryptedStream: data.stream().pipeThrough(encryptStream),
|
|
43
|
+
})
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* @param {ReadableStream} encryptedData
|
|
47
|
+
* @param {Uint8Array} key
|
|
48
|
+
* @param {Uint8Array} iv
|
|
49
|
+
*/
|
|
50
|
+
async decryptStream(encryptedData, key, iv) {
|
|
51
|
+
const decipher = createDecipheriv(ENCRYPTION_ALGORITHM, key, iv)
|
|
52
|
+
const decryptor = new TransformStream({
|
|
53
|
+
async transform(chunk, controller) {
|
|
54
|
+
try {
|
|
55
|
+
const decryptedChunk = decipher.update(chunk)
|
|
56
|
+
if (decryptedChunk.length > 0) {
|
|
57
|
+
controller.enqueue(decryptedChunk)
|
|
58
|
+
}
|
|
59
|
+
} catch (err) {
|
|
60
|
+
controller.error(err)
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
flush(controller) {
|
|
64
|
+
try {
|
|
65
|
+
const finalChunk = decipher.final()
|
|
66
|
+
if (finalChunk.length > 0) {
|
|
67
|
+
controller.enqueue(finalChunk)
|
|
68
|
+
}
|
|
69
|
+
controller.terminate()
|
|
70
|
+
} catch (err) {
|
|
71
|
+
controller.error(err)
|
|
103
72
|
}
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
73
|
+
},
|
|
74
|
+
})
|
|
75
|
+
return Promise.resolve(encryptedData.pipeThrough(decryptor))
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Combine key and IV into a single array for AES-CBC
|
|
79
|
+
*
|
|
80
|
+
* @param {Uint8Array} key - The AES key (KEY_LENGTH/8 bytes)
|
|
81
|
+
* @param {Uint8Array} iv - The AES-CBC IV (IV_LENGTH bytes)
|
|
82
|
+
* @returns {Uint8Array} Combined key and IV (KEY_LENGTH/8 + IV_LENGTH bytes)
|
|
83
|
+
*/
|
|
84
|
+
combineKeyAndIV(key, iv) {
|
|
85
|
+
const keyBytes = KEY_LENGTH / 8
|
|
86
|
+
if (key.length !== keyBytes) {
|
|
87
|
+
throw new Error(
|
|
88
|
+
`AES-${KEY_LENGTH} key must be ${keyBytes} bytes, got ${key.length}`
|
|
89
|
+
)
|
|
90
|
+
}
|
|
91
|
+
if (iv.length !== IV_LENGTH) {
|
|
92
|
+
throw new Error(`AES-CBC IV must be ${IV_LENGTH} bytes, got ${iv.length}`)
|
|
93
|
+
}
|
|
94
|
+
return new Uint8Array([...key, ...iv])
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Split combined key and IV for AES-CBC
|
|
98
|
+
*
|
|
99
|
+
* @param {Uint8Array} combined - Combined key and IV (KEY_LENGTH/8 + IV_LENGTH bytes)
|
|
100
|
+
* @returns {{ key: Uint8Array, iv: Uint8Array }} Separated key and IV
|
|
101
|
+
*/
|
|
102
|
+
splitKeyAndIV(combined) {
|
|
103
|
+
const keyBytes = KEY_LENGTH / 8
|
|
104
|
+
const expectedLength = keyBytes + IV_LENGTH
|
|
105
|
+
if (combined.length !== expectedLength) {
|
|
106
|
+
throw new Error(
|
|
107
|
+
`AES-${KEY_LENGTH}-CBC combined key+IV must be ${expectedLength} bytes, got ${combined.length}`
|
|
108
|
+
)
|
|
109
|
+
}
|
|
110
|
+
return {
|
|
111
|
+
key: combined.subarray(0, keyBytes),
|
|
112
|
+
iv: combined.subarray(keyBytes, keyBytes + IV_LENGTH),
|
|
108
113
|
}
|
|
114
|
+
}
|
|
109
115
|
}
|
|
110
|
-
//# sourceMappingURL=node-aes-cbc-crypto.js.map
|
|
116
|
+
//# sourceMappingURL=node-aes-cbc-crypto.js.map
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export {}
|
|
2
|
-
//# sourceMappingURL=decrypt-test.d.ts.map
|
|
1
|
+
export {}
|
|
2
|
+
//# sourceMappingURL=decrypt-test.d.ts.map
|
|
@@ -1,73 +1,82 @@
|
|
|
1
|
-
import * as fs from 'fs'
|
|
2
|
-
import dotenv from 'dotenv'
|
|
3
|
-
import { CID } from 'multiformats'
|
|
4
|
-
import * as Client from '@storacha/client'
|
|
5
|
-
import * as Signer from '@ucanto/principal/ed25519'
|
|
6
|
-
import { StoreMemory } from '@storacha/client/stores/memory'
|
|
7
|
-
import { create } from '../src/index.js'
|
|
8
|
-
import { Wallet } from 'ethers'
|
|
9
|
-
import { serviceConf, receiptsEndpoint } from '../src/config/service.js'
|
|
10
|
-
import { createNodeLitAdapter } from '../src/crypto/factories.node.js'
|
|
11
|
-
import { LitNodeClient } from '@lit-protocol/lit-node-client'
|
|
12
|
-
import { extract } from '@ucanto/core/delegation'
|
|
13
|
-
dotenv.config()
|
|
1
|
+
import * as fs from 'fs'
|
|
2
|
+
import dotenv from 'dotenv'
|
|
3
|
+
import { CID } from 'multiformats'
|
|
4
|
+
import * as Client from '@storacha/client'
|
|
5
|
+
import * as Signer from '@ucanto/principal/ed25519'
|
|
6
|
+
import { StoreMemory } from '@storacha/client/stores/memory'
|
|
7
|
+
import { create } from '../src/index.js'
|
|
8
|
+
import { Wallet } from 'ethers'
|
|
9
|
+
import { serviceConf, receiptsEndpoint } from '../src/config/service.js'
|
|
10
|
+
import { createNodeLitAdapter } from '../src/crypto/factories.node.js'
|
|
11
|
+
import { LitNodeClient } from '@lit-protocol/lit-node-client'
|
|
12
|
+
import { extract } from '@ucanto/core/delegation'
|
|
13
|
+
dotenv.config()
|
|
14
14
|
async function main() {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
15
|
+
// set up storacha client with a new agent
|
|
16
|
+
const cid = CID.parse(
|
|
17
|
+
'bafyreifhwqmspdjsy6rgcmcizgodv7bwskgiehjhdx7wukax3z5r7tz5ji'
|
|
18
|
+
)
|
|
19
|
+
const delegationCarBuffer = fs.readFileSync('delegation.car')
|
|
20
|
+
const wallet = new Wallet(process.env.WALLET_PK || '')
|
|
21
|
+
const principal = Signer.parse(process.env.DELEGATEE_AGENT_PK || '')
|
|
22
|
+
const store = new StoreMemory()
|
|
23
|
+
const client = await Client.create({
|
|
24
|
+
principal,
|
|
25
|
+
store,
|
|
26
|
+
serviceConf,
|
|
27
|
+
receiptsEndpoint,
|
|
28
|
+
})
|
|
29
|
+
// Set up Lit client
|
|
30
|
+
const litClient = new LitNodeClient({
|
|
31
|
+
litNetwork: 'datil-dev',
|
|
32
|
+
})
|
|
33
|
+
await litClient.connect()
|
|
34
|
+
const encryptedClient = await create({
|
|
35
|
+
storachaClient: client,
|
|
36
|
+
cryptoAdapter: createNodeLitAdapter(litClient),
|
|
37
|
+
})
|
|
38
|
+
const res = await extract(delegationCarBuffer)
|
|
39
|
+
if (res.error) {
|
|
40
|
+
throw new Error(`Failed to extract delegation: ${res.error.message}`)
|
|
41
|
+
}
|
|
42
|
+
const decryptDelegation = res.ok
|
|
43
|
+
const decryptionCapability = decryptDelegation.capabilities.find(
|
|
44
|
+
(c) => c.can === 'space/content/decrypt'
|
|
45
|
+
)
|
|
46
|
+
if (!decryptionCapability) {
|
|
47
|
+
throw new Error('Failed to find decryption capability')
|
|
48
|
+
}
|
|
49
|
+
const spaceDID = /** @type {`did:key:${string}`} */ (
|
|
50
|
+
decryptionCapability.with
|
|
51
|
+
)
|
|
52
|
+
const decryptionConfig = {
|
|
53
|
+
wallet,
|
|
54
|
+
decryptDelegation,
|
|
55
|
+
spaceDID,
|
|
56
|
+
}
|
|
57
|
+
const decryptedContent = await encryptedClient.retrieveAndDecryptFile(
|
|
58
|
+
cid,
|
|
59
|
+
decryptionConfig
|
|
60
|
+
)
|
|
61
|
+
const reader = decryptedContent.stream.getReader()
|
|
62
|
+
const decoder = new TextDecoder()
|
|
63
|
+
let result = ''
|
|
64
|
+
let done = false
|
|
65
|
+
while (!done) {
|
|
66
|
+
const { value, done: isDone } = await reader.read()
|
|
67
|
+
done = isDone
|
|
68
|
+
if (value) {
|
|
69
|
+
result += decoder.decode(value, { stream: true })
|
|
39
70
|
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
}
|
|
45
|
-
const spaceDID = /** @type {`did:key:${string}`} */ (decryptionCapability.with);
|
|
46
|
-
const decryptionConfig = {
|
|
47
|
-
wallet,
|
|
48
|
-
decryptDelegation,
|
|
49
|
-
spaceDID,
|
|
50
|
-
};
|
|
51
|
-
const decryptedContent = await encryptedClient.retrieveAndDecryptFile(cid, decryptionConfig);
|
|
52
|
-
const reader = decryptedContent.stream.getReader();
|
|
53
|
-
const decoder = new TextDecoder();
|
|
54
|
-
let result = '';
|
|
55
|
-
let done = false;
|
|
56
|
-
while (!done) {
|
|
57
|
-
const { value, done: isDone } = await reader.read();
|
|
58
|
-
done = isDone;
|
|
59
|
-
if (value) {
|
|
60
|
-
result += decoder.decode(value, { stream: true });
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
console.log('================ RESULT ===================');
|
|
64
|
-
console.log(result);
|
|
65
|
-
console.log('===========================================');
|
|
71
|
+
}
|
|
72
|
+
console.log('================ RESULT ===================')
|
|
73
|
+
console.log(result)
|
|
74
|
+
console.log('===========================================')
|
|
66
75
|
}
|
|
67
76
|
main()
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
console.error(err)
|
|
71
|
-
process.exit(1)
|
|
72
|
-
})
|
|
73
|
-
//# sourceMappingURL=decrypt-test.js.map
|
|
77
|
+
.then(() => process.exit(0))
|
|
78
|
+
.catch((err) => {
|
|
79
|
+
console.error(err)
|
|
80
|
+
process.exit(1)
|
|
81
|
+
})
|
|
82
|
+
//# sourceMappingURL=decrypt-test.js.map
|
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
/** @param {string} data Base64 encoded CAR file */
|
|
2
|
-
export function parseProof(
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
export function parseProof(
|
|
3
|
+
data: string
|
|
4
|
+
): Promise<Signer.Delegation<Signer.Signer.Capabilities>>
|
|
5
|
+
import * as Signer from '@ucanto/principal/ed25519'
|
|
6
|
+
//# sourceMappingURL=encrypt-test.d.ts.map
|
|
@@ -1,61 +1,64 @@
|
|
|
1
|
-
import * as fs from 'fs'
|
|
2
|
-
import dotenv from 'dotenv'
|
|
3
|
-
import { CarReader } from '@ipld/car'
|
|
4
|
-
import * as Client from '@storacha/client'
|
|
5
|
-
import { importDAG } from '@ucanto/core/delegation'
|
|
6
|
-
import * as Signer from '@ucanto/principal/ed25519'
|
|
7
|
-
import { StoreMemory } from '@storacha/client/stores/memory'
|
|
8
|
-
import * as EncryptClient from '../src/index.js'
|
|
9
|
-
import { serviceConf, receiptsEndpoint } from '../src/config/service.js'
|
|
10
|
-
import { createNodeLitAdapter } from '../src/crypto/factories.node.js'
|
|
11
|
-
import { LitNodeClient } from '@lit-protocol/lit-node-client'
|
|
12
|
-
dotenv.config()
|
|
1
|
+
import * as fs from 'fs'
|
|
2
|
+
import dotenv from 'dotenv'
|
|
3
|
+
import { CarReader } from '@ipld/car'
|
|
4
|
+
import * as Client from '@storacha/client'
|
|
5
|
+
import { importDAG } from '@ucanto/core/delegation'
|
|
6
|
+
import * as Signer from '@ucanto/principal/ed25519'
|
|
7
|
+
import { StoreMemory } from '@storacha/client/stores/memory'
|
|
8
|
+
import * as EncryptClient from '../src/index.js'
|
|
9
|
+
import { serviceConf, receiptsEndpoint } from '../src/config/service.js'
|
|
10
|
+
import { createNodeLitAdapter } from '../src/crypto/factories.node.js'
|
|
11
|
+
import { LitNodeClient } from '@lit-protocol/lit-node-client'
|
|
12
|
+
dotenv.config()
|
|
13
13
|
/** @param {string} data Base64 encoded CAR file */
|
|
14
14
|
export async function parseProof(data) {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
15
|
+
const blocks = []
|
|
16
|
+
const reader = await CarReader.fromBytes(Buffer.from(data, 'base64'))
|
|
17
|
+
for await (const block of reader.blocks()) {
|
|
18
|
+
blocks.push(block)
|
|
19
|
+
}
|
|
20
|
+
return importDAG(blocks)
|
|
21
21
|
}
|
|
22
22
|
async function main() {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
23
|
+
// set up storacha client with a new agent
|
|
24
|
+
const principal = Signer.parse(process.env.AGENT_PK || '')
|
|
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 parseProof(process.env.PROOF || '')
|
|
34
|
+
const space = await client.addSpace(proof)
|
|
35
|
+
await client.setCurrentSpace(space.did())
|
|
36
|
+
// Set up Lit client
|
|
37
|
+
const litClient = new LitNodeClient({
|
|
38
|
+
litNetwork: 'datil-dev',
|
|
39
|
+
})
|
|
40
|
+
await litClient.connect()
|
|
41
|
+
const encryptedClient = await EncryptClient.create({
|
|
42
|
+
storachaClient: client,
|
|
43
|
+
cryptoAdapter: createNodeLitAdapter(litClient),
|
|
44
|
+
})
|
|
45
|
+
const fileContent = await fs.promises.readFile('./README.md')
|
|
46
|
+
const blob = new Blob([fileContent])
|
|
47
|
+
// Create encryption config
|
|
48
|
+
const encryptionConfig = {
|
|
49
|
+
issuer: principal,
|
|
50
|
+
spaceDID: space.did(),
|
|
51
|
+
}
|
|
52
|
+
const link = await encryptedClient.encryptAndUploadFile(
|
|
53
|
+
blob,
|
|
54
|
+
encryptionConfig
|
|
55
|
+
)
|
|
56
|
+
console.log(link)
|
|
54
57
|
}
|
|
55
58
|
main()
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
console.error(err)
|
|
59
|
-
process.exit(1)
|
|
60
|
-
})
|
|
61
|
-
//# sourceMappingURL=encrypt-test.js.map
|
|
59
|
+
.then(() => process.exit(0))
|
|
60
|
+
.catch((err) => {
|
|
61
|
+
console.error(err)
|
|
62
|
+
process.exit(1)
|
|
63
|
+
})
|
|
64
|
+
//# sourceMappingURL=encrypt-test.js.map
|
|
@@ -8,8 +8,22 @@
|
|
|
8
8
|
* @param {AsyncIterable<Uint8Array>|Uint8Array} content - The encrypted file content
|
|
9
9
|
* @returns {Promise<ReadableStream>} The decrypted file stream
|
|
10
10
|
*/
|
|
11
|
-
export function decryptFileWithKey(
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
11
|
+
export function decryptFileWithKey(
|
|
12
|
+
cryptoAdapter: Type.CryptoAdapter,
|
|
13
|
+
key: Uint8Array,
|
|
14
|
+
iv: Uint8Array,
|
|
15
|
+
content: AsyncIterable<Uint8Array> | Uint8Array
|
|
16
|
+
): Promise<ReadableStream>
|
|
17
|
+
export function retrieveAndDecrypt(
|
|
18
|
+
storachaClient: import('@storacha/client').Client,
|
|
19
|
+
cryptoAdapter: Type.CryptoAdapter,
|
|
20
|
+
gatewayURL: URL,
|
|
21
|
+
cid: Type.AnyLink,
|
|
22
|
+
decryptionConfig: Type.DecryptionConfig
|
|
23
|
+
): Promise<Type.DecryptionResult>
|
|
24
|
+
export function getCarFileFromPublicGateway(
|
|
25
|
+
gatewayURL: URL,
|
|
26
|
+
cid: string
|
|
27
|
+
): Promise<Uint8Array>
|
|
28
|
+
import * as Type from '../types.js'
|
|
29
|
+
//# sourceMappingURL=decrypt-handler.d.ts.map
|
|
@@ -1,3 +1,9 @@
|
|
|
1
|
-
export function encryptAndUpload(
|
|
2
|
-
import
|
|
3
|
-
|
|
1
|
+
export function encryptAndUpload(
|
|
2
|
+
storachaClient: import('@storacha/client').Client,
|
|
3
|
+
cryptoAdapter: Type.CryptoAdapter,
|
|
4
|
+
file: Type.BlobLike,
|
|
5
|
+
encryptionConfig: Type.EncryptionConfig,
|
|
6
|
+
uploadOptions?: Type.UploadOptions
|
|
7
|
+
): Promise<Type.AnyLink>
|
|
8
|
+
import * as Type from '../types.js'
|
|
9
|
+
//# sourceMappingURL=encrypt-handler.d.ts.map
|