Skip to content

Commit 89aa296

Browse files
feat(sdk): add uploadMode option for protectData
1 parent 9752aff commit 89aa296

File tree

2 files changed

+62
-19
lines changed

2 files changed

+62
-19
lines changed

packages/sdk/src/lib/dataProtectorCore/protectData.ts

Lines changed: 53 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { multiaddr as Multiaddr } from '@multiformats/multiaddr';
2-
import { DEFAULT_DATA_NAME } from '../../config/config.js';
3-
import { add } from '../../services/ipfs.js';
2+
import {
3+
DEFAULT_ARWEAVE_GATEWAY,
4+
DEFAULT_DATA_NAME,
5+
} from '../../config/config.js';
6+
import * as arweave from '../../services/arweave.js';
7+
import * as ipfs from '../../services/ipfs.js';
48
import {
59
createZipFromObject,
610
ensureDataObjectIsValid,
@@ -43,6 +47,7 @@ export const protectData = async ({
4347
iexecDebug = throwIfMissing(),
4448
dataprotectorContractAddress,
4549
name = DEFAULT_DATA_NAME,
50+
uploadMode = 'ipfs',
4651
ipfsNode,
4752
ipfsGateway,
4853
allowDebug = false,
@@ -54,6 +59,10 @@ export const protectData = async ({
5459
IpfsNodeAndGateway &
5560
ProtectDataParams): Promise<ProtectedDataWithSecretProps> => {
5661
const vName = stringSchema().label('name').validateSync(name);
62+
const vUploadMode = stringSchema()
63+
.oneOf(['ipfs', 'arweave'])
64+
.label('uploadMode')
65+
.validateSync(uploadMode);
5766
const vIpfsNodeUrl = urlSchema().label('ipfsNode').validateSync(ipfsNode);
5867
const vIpfsGateway = urlSchema()
5968
.label('ipfsGateway')
@@ -149,28 +158,53 @@ export const protectData = async ({
149158
title: 'UPLOAD_ENCRYPTED_FILE',
150159
isDone: false,
151160
});
152-
const cid = await add(encryptedFile, {
153-
ipfsNode: vIpfsNodeUrl,
154-
ipfsGateway: vIpfsGateway,
155-
}).catch((e: Error) => {
156-
throw new WorkflowError({
157-
message: 'Failed to upload encrypted data',
158-
errorCause: e,
161+
162+
let multiaddr: string;
163+
let multiaddrBytes: Uint8Array;
164+
165+
if (vUploadMode === 'arweave') {
166+
const arweaveId = await arweave.add(encryptedFile).catch((e: Error) => {
167+
throw new WorkflowError({
168+
message: 'Failed to upload encrypted data',
169+
errorCause: e,
170+
});
159171
});
160-
});
161-
const multiaddr = `/p2p/${cid}`;
162-
vOnStatusUpdate({
163-
title: 'UPLOAD_ENCRYPTED_FILE',
164-
isDone: true,
165-
payload: {
166-
cid,
167-
},
168-
});
172+
multiaddr = `${DEFAULT_ARWEAVE_GATEWAY}/${arweaveId}`;
173+
multiaddrBytes = new TextEncoder().encode(multiaddr);
174+
vOnStatusUpdate({
175+
title: 'UPLOAD_ENCRYPTED_FILE',
176+
isDone: true,
177+
payload: {
178+
arweaveId,
179+
},
180+
});
181+
} else {
182+
// ipfs fallback
183+
const cid = await ipfs
184+
.add(encryptedFile, {
185+
ipfsNode: vIpfsNodeUrl,
186+
ipfsGateway: vIpfsGateway,
187+
})
188+
.catch((e: Error) => {
189+
throw new WorkflowError({
190+
message: 'Failed to upload encrypted data',
191+
errorCause: e,
192+
});
193+
});
194+
multiaddr = `/p2p/${cid}`;
195+
multiaddrBytes = Multiaddr(multiaddr).bytes;
196+
vOnStatusUpdate({
197+
title: 'UPLOAD_ENCRYPTED_FILE',
198+
isDone: true,
199+
payload: {
200+
cid,
201+
},
202+
});
203+
}
169204

170205
const { provider, signer, txOptions } =
171206
await iexec.config.resolveContractsClient();
172207

173-
const multiaddrBytes = Multiaddr(multiaddr).bytes;
174208
const ownerAddress = await signer.getAddress();
175209

176210
vOnStatusUpdate({

packages/sdk/src/lib/types/coreTypes.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ export type ProtectDataParams = {
7272
*/
7373
allowDebug?: boolean;
7474

75+
/**
76+
* specify the platform used for storing the encrypted payload of the protected data
77+
*
78+
* - `"ipfs"` (default): https://ipfs.tech/
79+
* - `"arweave"`: https://arweave.org/
80+
*/
81+
uploadMode?: 'ipfs' | 'arweave';
82+
7583
/**
7684
* Callback function that will get called at each step of the process
7785
*/
@@ -98,6 +106,7 @@ export type ProtectedDataCreationProps = {
98106
transactionHash: string;
99107
zipFile: Uint8Array;
100108
encryptionKey: string;
109+
multiaddr: string;
101110
};
102111

103112
export type ProtectedDataWithSecretProps = ProtectedData &

0 commit comments

Comments
 (0)