Skip to content

Commit

Permalink
Release/sdk v2.0.0 beta.10 (#358)
Browse files Browse the repository at this point in the history
Co-authored-by: luisccastillo <[email protected]>
Co-authored-by: abbes benayache <[email protected]>
Co-authored-by: Le-Caignec <[email protected]>
Co-authored-by: Robin Le Caignec <[email protected]>
Co-authored-by: ErwanDecoster <[email protected]>
Co-authored-by: Erwan Decoster <[email protected]>
Co-authored-by: pjt <[email protected]>
  • Loading branch information
8 people authored Sep 20, 2024
1 parent ed87607 commit 1da6401
Show file tree
Hide file tree
Showing 25 changed files with 368 additions and 140 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Contract, toBeHex } from 'ethers';
import { IExec } from 'iexec';
import * as ADD_ONLY_APP_WHITELIST_REGISTRY_ABI from '../../abis/AddOnlyAppWhitelistRegistryABI.json';
import * as DATA_SHARING_ABI from '../../abis/DataProtectorSharingABI.json';
import { getEventFromLogs } from '../utils/transactionEvent.js';
import { getEventFromLogs } from '../utils/getEventFromLogs.js';

const createAddOnlyAppWhitelist = async (
iexec: IExec,
Expand Down Expand Up @@ -34,11 +34,11 @@ const createAddOnlyAppWhitelist = async (
txOptions
);
const createWhitelistReceipt = await createWhitelistTx.wait();
const specificEventForPreviousTx = getEventFromLogs(
'Transfer',
createWhitelistReceipt.logs,
{ strict: true }
);
const specificEventForPreviousTx = getEventFromLogs({
contract: addOnlyAppWhitelistRegistryContract,
eventName: 'Transfer',
logs: createWhitelistReceipt.logs,
});

const addOnlyAppWhitelistAddress = toBeHex(
specificEventForPreviousTx.args?.tokenId
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
export const getEventFromLogs = ({ contract, eventName, logs }) => {
const filter = contract.getEvent(eventName);
if (!filter) {
throw new Error(`Event filter not found for ${eventName}`);
}

const eventTopic = filter.fragment.topicHash;
// Find the event in the logs based on the topic
const eventFound = logs.find((log) => log.topics[0] === eventTopic);
if (!eventFound) {
throw new Error(`Event ${eventName} not found in logs`);
}

// Check if the event is already decoded
if (eventFound.args) {
return eventFound;
}

// If not decoded, decode the event
try {
return {
...eventFound,
args: contract.interface.decodeEventLog(
filter.fragment,
eventFound.data,
eventFound.topics
),
};
} catch (error) {
throw new Error(`Failed to decode event ${eventName}: ${error.message}`);
}
};

This file was deleted.

9 changes: 9 additions & 0 deletions packages/sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

All notable changes to this project will be documented in this file.

## [2.0.0-beta.10] (2024-09-20)

### Changed

- Added new `protectData` and `getProtectedData` return field: `multiaddr`.
- Improved the performance of `getCollectionOwners` and `getCollectionsByOwner`.
- The `collections` in `getCollectionOwners` are now ordered by `collections.creationTimestamp`.
- make input object for `getCollectionOwners`, `getCollectionSubscriptions` and `getRentals` optional

## [2.0.0-beta.9] (2024-09-02)

### Changed
Expand Down
4 changes: 2 additions & 2 deletions packages/sdk/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions packages/sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@iexec/dataprotector",
"version": "2.0.0-beta.9",
"version": "2.0.0-beta.10",
"description": "This product enables users to confidentially store data–such as mail address, documents, personal information ...",
"type": "module",
"types": "dist/src/index.d.ts",
Expand All @@ -17,12 +17,12 @@
"build": "rimraf dist && tsc --project tsconfig.build.json",
"build:watch": "npm run build -- --watch",
"check-types": "tsc --noEmit",
"test": "npm run test:unit && npm run test:e2e",
"test:coverage": "npm run test:unit:coverage && npm run test:e2e:coverage",
"test:unit": "NODE_OPTIONS=--experimental-vm-modules jest --testMatch \"**/tests/unit/**/*.unit.ts\"",
"test:unit:coverage": "NODE_OPTIONS=--experimental-vm-modules jest --testMatch \"**/tests/unit/**/*.unit.ts\" --coverage",
"test:e2e": "NODE_OPTIONS=--experimental-vm-modules jest --testMatch \"**/tests/**/*.test.ts\"",
"test:e2e:coverage": "NODE_OPTIONS=--experimental-vm-modules jest --testMatch \"**/tests/**/*.test.ts\" --coverage",
"test": "NODE_OPTIONS=--experimental-vm-modules jest --testMatch \"**/tests/**/*.test.ts\"",
"test:coverage": "NODE_OPTIONS=--experimental-vm-modules jest --testMatch \"**/tests/**/*.test.ts\" --coverage",
"test:unit": "NODE_OPTIONS=--experimental-vm-modules jest --testMatch \"**/tests/unit/**/*.test.ts\"",
"test:unit:coverage": "NODE_OPTIONS=--experimental-vm-modules jest --testMatch \"**/tests/unit/**/*.test.ts\" --coverage",
"test:e2e": "NODE_OPTIONS=--experimental-vm-modules jest --testMatch \"**/tests/e2e/**/*.test.ts\"",
"test:e2e:coverage": "NODE_OPTIONS=--experimental-vm-modules jest --testMatch \"**/tests/e2e/**/*.test.ts\" --coverage",
"lint": "eslint .",
"lint:fix": "eslint . --fix",
"format": "prettier --write \"(src|tests)/**/*.ts\"",
Expand Down
6 changes: 6 additions & 0 deletions packages/sdk/src/lib/dataProtectorCore/getProtectedData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
reverseSafeSchema,
} from '../../utils/data.js';
import { ValidationError, WorkflowError } from '../../utils/errors.js';
import { getMultiaddrAsString } from '../../utils/getMultiaddrAsString.js';
import { resolveENS } from '../../utils/resolveENS.js';
import {
addressOrEnsSchema,
Expand Down Expand Up @@ -96,6 +97,7 @@ export const getProtectedData = async ({
id
}
creationTimestamp
multiaddr
}
}
`;
Expand Down Expand Up @@ -164,12 +166,16 @@ function transformGraphQLResponse(
.map((protectedData) => {
try {
const schema = reverseSafeSchema(protectedData.schema);
const readableMultiAddr = getMultiaddrAsString({
multiaddrAsHexString: protectedData.multiaddr,
});
return {
name: protectedData.name,
address: protectedData.id,
owner: protectedData.owner.id,
schema,
creationTimestamp: Number(protectedData.creationTimestamp),
multiaddr: readableMultiAddr,
};
} catch (error) {
// Silently ignore the error to not return multiple errors in the console of the user
Expand Down
15 changes: 8 additions & 7 deletions packages/sdk/src/lib/dataProtectorCore/protectData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
WorkflowError,
} from '../../utils/errors.js';
import { getLogger } from '../../utils/logger.js';
import { getEventFromLogs } from '../../utils/transactionEvent.js';
import { getEventFromLogs } from '../../utils/getEventFromLogs.js';
import {
stringSchema,
throwIfMissing,
Expand Down Expand Up @@ -152,7 +152,7 @@ export const protectData = async ({
errorCause: e,
});
});
const multiaddr = `/ipfs/${cid}`;
const multiaddr = `/p2p/${cid}`;
vOnStatusUpdate({
title: 'UPLOAD_ENCRYPTED_FILE',
isDone: true,
Expand Down Expand Up @@ -192,11 +192,11 @@ export const protectData = async ({
});
});

const specificEventForPreviousTx = getEventFromLogs(
'DatasetSchema',
transactionReceipt.logs,
{ strict: true }
);
const specificEventForPreviousTx = getEventFromLogs({
contract: dataProtectorContract,
eventName: 'DatasetSchema',
logs: transactionReceipt.logs,
});
const protectedDataAddress = specificEventForPreviousTx.args?.dataset;

const txHash = transactionReceipt.hash;
Expand Down Expand Up @@ -251,6 +251,7 @@ export const protectData = async ({
transactionHash: txHash,
zipFile: file,
encryptionKey,
multiaddr,
};
} catch (e: any) {
logger.log(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ class IExecDataProtectorSharing extends IExecDataProtectorModule {
}

getCollectionOwners(
args: GetCollectionOwnersParams
args?: GetCollectionOwnersParams
): Promise<GetCollectionOwnersResponse> {
return getCollectionOwners({
...args,
Expand All @@ -327,15 +327,15 @@ class IExecDataProtectorSharing extends IExecDataProtectorModule {
}

getCollectionSubscriptions(
args: GetCollectionSubscriptionsParams
args?: GetCollectionSubscriptionsParams
): Promise<GetCollectionSubscriptionsResponse> {
return getCollectionSubscriptions({
...args,
graphQLClient: this.graphQLClient,
});
}

getRentals(args: GetRentalsParams): Promise<GetRentalsResponse> {
getRentals(args?: GetRentalsParams): Promise<GetRentalsResponse> {
return getRentals({
...args,
graphQLClient: this.graphQLClient,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import {
consumeProtectedDataErrorMessage,
handleIfProtocolError,
} from '../../utils/errors.js';
import { getEventFromLogs } from '../../utils/getEventFromLogs.js';
import { resolveENS } from '../../utils/resolveENS.js';
import { getFormattedKeyPair } from '../../utils/rsa.js';
import { getEventFromLogs } from '../../utils/transactionEvent.js';
import {
addressOrEnsSchema,
throwIfMissing,
Expand Down Expand Up @@ -182,11 +182,11 @@ export const consumeProtectedData = async ({
},
});

const specificEventForPreviousTx = getEventFromLogs(
'ProtectedDataConsumed',
transactionReceipt.logs,
{ strict: true }
);
const specificEventForPreviousTx = getEventFromLogs({
contract: sharingContract,
eventName: 'ProtectedDataConsumed',
logs: transactionReceipt.logs,
});

const dealId = specificEventForPreviousTx.args?.dealId;
const taskId = await iexec.deal.computeTaskId(dealId, 0);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { toBeHex } from 'ethers';
import { WorkflowError } from '../../utils/errors.js';
import { getEventFromLogs } from '../../utils/transactionEvent.js';
import { getEventFromLogs } from '../../utils/getEventFromLogs.js';
import { throwIfMissing } from '../../utils/validators.js';
import { IExecConsumer } from '../types/internalTypes.js';
import {
Expand Down Expand Up @@ -30,11 +30,11 @@ export const createAddOnlyAppWhitelist = async ({
);
const transactionReceipt = await tx.wait();

const specificEventForPreviousTx = getEventFromLogs(
'Transfer',
transactionReceipt.logs,
{ strict: true }
);
const specificEventForPreviousTx = getEventFromLogs({
contract: appWhitelistRegistryContract,
eventName: 'Transfer',
logs: transactionReceipt.logs,
});

const mintedTokenId = toBeHex(specificEventForPreviousTx.args?.tokenId);
return {
Expand Down
20 changes: 6 additions & 14 deletions packages/sdk/src/lib/dataProtectorSharing/createCollection.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { WorkflowError } from '../../utils/errors.js';
import { getEventFromLogs } from '../../utils/transactionEvent.js';
import { getEventFromLogs } from '../../utils/getEventFromLogs.js';
import { throwIfMissing } from '../../utils/validators.js';
import type {
CreateCollectionResponse,
Expand All @@ -25,26 +25,18 @@ export const createCollection = async ({
const tx = await sharingContract.createCollection(userAddress, txOptions);
const transactionReceipt = await tx.wait();

const specificEventForPreviousTx = getEventFromLogs(
'Transfer',
transactionReceipt.logs,
{ strict: true }
);
const specificEventForPreviousTx = getEventFromLogs({
contract: sharingContract,
eventName: 'Transfer',
logs: transactionReceipt.logs,
});

const mintedTokenId = specificEventForPreviousTx.args?.tokenId;
return {
collectionId: Number(mintedTokenId),
txHash: tx.hash,
};
} catch (e) {
// Try to extract some meaningful error like:
// "User denied transaction signature"
if (e?.info?.error?.message) {
throw new WorkflowError({
message: `Failed to create collection: ${e.info.error.message}`,
errorCause: e,
});
}
throw new WorkflowError({
message: 'Failed to create collection into collection smart contract',
errorCause: e,
Expand Down
55 changes: 14 additions & 41 deletions packages/sdk/src/lib/dataProtectorSharing/getCollectionsByOwner.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { WorkflowError } from '../../utils/errors.js';
import { addressSchema, throwIfMissing } from '../../utils/validators.js';
import {
addressSchema,
booleanSchema,
throwIfMissing,
} from '../../utils/validators.js';
import {
GetCollectionsByOwnerResponse,
GetCollectionsByOwnerParams,
Expand All @@ -19,47 +23,16 @@ export async function getCollectionsByOwner({
.label('owner')
.validateSync(owner);

const getCollectionsByOwnerQueryResponse = await getCollectionsByOwnerQuery(
{
graphQLClient,
owner: vOwner,
}
);

/**
* With graph-node >= 0.30.0, possible query:
* {
* protectedDatas(where: {
* or: [
* { isRentable: true },
* { isIncludedInSubscription: true },
* { isForSale: true },
* ]
* }) {
* id
* }
* }
* hence no need of this JS post filter!
*/
if (!includeHiddenProtectedDatas) {
return {
collections: getCollectionsByOwnerQueryResponse.collections.map(
(collection) => {
return {
...collection,
protectedDatas: collection.protectedDatas.filter(
(protectedData) =>
protectedData.isRentable ||
protectedData.isIncludedInSubscription ||
protectedData.isForSale
),
};
}
),
};
}
const vIncludeHiddenProtectedDatas = booleanSchema()
.required()
.label('includeHiddenProtectedDatas')
.validateSync(includeHiddenProtectedDatas);

return getCollectionsByOwnerQueryResponse;
return await getCollectionsByOwnerQuery({
graphQLClient,
owner: vOwner,
includeHiddenProtectedDatas: vIncludeHiddenProtectedDatas,
});
} catch (e) {
console.log('[getCollectionsByOwner] ERROR', e);
throw new WorkflowError({
Expand Down
Loading

0 comments on commit 1da6401

Please sign in to comment.