Skip to content

Commit

Permalink
refactor:
Browse files Browse the repository at this point in the history
- implement assertDrivePrivacy method abstracting the logic to validate drive privacy
- improve graphql query performance
  • Loading branch information
thiagocarvalhodev committed Jul 19, 2024
1 parent 0f87fe1 commit ef4e9b9
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 25 deletions.
13 changes: 2 additions & 11 deletions src/ardrive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -599,19 +599,10 @@ export class ArDrive extends ArDriveAnonymous {
for (const entity of entitiesToUpload) {
const { destFolderId } = entity;
const destDriveId = await this.arFsDao.getDriveIdForFolderId(destFolderId);
const owner = await this.wallet.getAddress();

const isPublicDrive = await this.arFsDao.isPublicDrive(destDriveId);

// Private drive uploads require a drive key
if(!isPublicDrive && !entity.driveKey) {
throw new Error('Private drive requires a drive key to upload');
}

if(isPublicDrive && entity.driveKey) {
throw new Error('Public drive does not require a drive key to upload');
}
await this.arFsDao.assertDrivePrivacy(destDriveId, owner, entity.driveKey);

const owner = await this.wallet.getAddress();
preparedEntities.push({ ...entity, destDriveId, owner });
}

Expand Down
4 changes: 2 additions & 2 deletions src/arfs/arfs_builders/arfs_drive_builders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
EntityMetaDataTransactionData
} from '../../types';
import { Utf8ArrayToStr } from '../../utils/common';
import { ENCRYPTED_DATA_PLACEHOLDER, fakeEntityId } from '../../utils/constants';
import { drivePrivacyTagName, ENCRYPTED_DATA_PLACEHOLDER, fakeEntityId } from '../../utils/constants';
import { ArFSPublicDrive, ArFSPrivateDrive, ArFSDriveEntity } from '../arfs_entities';
import {
ArFSMetadataEntityBuilder,
Expand Down Expand Up @@ -171,7 +171,7 @@ export class ArFSPrivateDriveBuilder extends ArFSDriveBuilder<ArFSPrivateDrive>
case 'Drive-Auth-Mode':
this.driveAuthMode = value as DriveAuthMode;
break;
case 'Drive-Privacy':
case drivePrivacyTagName:
this.drivePrivacy = value as DrivePrivacy;
break;
default:
Expand Down
33 changes: 21 additions & 12 deletions src/arfs/arfsdao.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ import {
authTagLength,
defaultMaxConcurrentChunks,
ENCRYPTED_DATA_PLACEHOLDER,
turboProdUrl
turboProdUrl,
drivePrivacyTagName
} from '../utils/constants';
import { PrivateKeyData } from './private_key_data';
import {
Expand Down Expand Up @@ -105,7 +106,7 @@ import {
fileConflictInfoMap,
folderToNameAndIdMap
} from '../utils/mapper_functions';
import { buildQuery, ASCENDING_ORDER } from '../utils/query';
import { buildQuery, ASCENDING_ORDER, DESCENDING_ORDER } from '../utils/query';
import { Wallet } from '../wallet';
import { JWKWallet } from '../jwk_wallet';
import { ArFSEntityCache } from './arfs_entity_cache';
Expand Down Expand Up @@ -1776,29 +1777,37 @@ export class ArFSDAO extends ArFSDAOAnonymous {
await this.getAllFoldersOfPublicDrive({ driveId, owner, latestRevisionsOnly: true })
);
}

public async isPublicDrive(driveId: DriveID): Promise<boolean> {
console.log('Checking if drive is public...');

public async isPublicDrive(driveId: DriveID, address: ArweaveAddress): Promise<boolean> {
const gqlQuery = buildQuery({
tags: [
{ name: 'Entity-Type', value: 'drive' },
{ name: 'Drive-Id', value: `${driveId}` },
{ name: 'Drive-Id', value: `${driveId}` }
],
sort: ASCENDING_ORDER
owner: address,
sort: DESCENDING_ORDER
});

console.log('Querying the graphQL API...');

const transactions = await this.gatewayApi.gqlRequest(gqlQuery);

console.log('Checking if drive is public...');

const drivePrivacyFromTag = transactions.edges[0].node.tags.find((t) => t.name === 'Drive-Privacy');
const drivePrivacyFromTag = transactions.edges[0].node.tags.find((t) => t.name === drivePrivacyTagName);

return drivePrivacyFromTag?.value === 'public';
}

public async assertDrivePrivacy(driveId: DriveID, address: ArweaveAddress, driveKey?: DriveKey): Promise<void> {
const _isPublicDrive = await this.isPublicDrive(driveId, address);

// Private drive uploads require a drive key
if (!_isPublicDrive && !driveKey) {
throw new Error('Private drive requires a drive key to upload');
}

if (_isPublicDrive && driveKey) {
throw new Error('Public drive does not require a drive key to upload');
}
}

public async getOwnerAndAssertDrive(driveId: DriveID, driveKey?: DriveKey): Promise<ArweaveAddress> {
const cachedOwner = this.caches.ownerCache.get(driveId);
if (cachedOwner) {
Expand Down
2 changes: 2 additions & 0 deletions src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,5 @@ export const gqlTagNameRecord = {

export const gqlTagNameArray = Object.values(gqlTagNameRecord);
export type GqlTagName = typeof gqlTagNameArray[number];

export const drivePrivacyTagName = 'Drive-Privacy';

0 comments on commit ef4e9b9

Please sign in to comment.