Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/network/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
DownloadFileFunction,
ToBinaryDataFunction,
BinaryData,
CryptoWithBucketKey,
} from './types';

export class FileVersionOneError extends Error {
Expand Down Expand Up @@ -71,3 +72,50 @@ export async function downloadFile(
throw err;
}
}

export async function downloadFileWithBucketKey(
fileId: string,
bucketId: string,
bucketKey: Buffer,
network: Network,
crypto: CryptoWithBucketKey,
toBinaryData: ToBinaryDataFunction,
downloadFile: DownloadFileFunction,
decryptFile: DecryptFileFunction,
opts?: { token: string },
): Promise<void> {
let iv: BinaryData;
let key: BinaryData;

try {
const fileInfo = await network.getDownloadLinks(bucketId, fileId, opts?.token);
const { index, shards, version, size } = fileInfo;

if (!version || version === 1) {
throw new FileVersionOneError();
}

iv = toBinaryData(index, BinaryDataEncoding.HEX).slice(0, 16);
key = await crypto.generateFileKeyFromBucketKey(bucketKey, toBinaryData(index, BinaryDataEncoding.HEX));
const downloadables = shards.sort((sA, sB) => sA.index - sB.index);

await downloadFile(downloadables, fileInfo);
await decryptFile(crypto.algorithm.type, key, iv, size);
} catch (err) {
const context = getNetworkErrorContext(
{
bucketId,
fileId,
user: network.credentials.username,
crypto: {
bucketKey,
},
},
err,
);

(err as ErrorWithContext).context = context;

throw err;
}
}
4 changes: 4 additions & 0 deletions src/network/errors/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export type NetworkUploadContext = {
user: string;
crypto: {
mnemonic?: string;
bucketKey?: Buffer;
};
};
export type NetworkDownloadContext = {
Expand All @@ -15,6 +16,7 @@ export type NetworkDownloadContext = {
user: string;
crypto: {
mnemonic?: string;
bucketKey?: Buffer;
};
};

Expand All @@ -30,9 +32,11 @@ export function getNetworkErrorContext(input: NetworkContext, err: unknown): Net
const output = Object.assign({}, input);

delete output.crypto.mnemonic;
delete output.crypto.bucketKey;

if (err instanceof UploadInvalidMnemonicError || err instanceof DownloadInvalidMnemonicError) {
output.crypto.mnemonic = input.crypto.mnemonic;
output.crypto.bucketKey = input.crypto.bucketKey;
}

return output;
Expand Down
13 changes: 10 additions & 3 deletions src/network/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,21 @@ export const ALGORITHMS: Record<SymmetricCryptoAlgorithm, Algorithm> = {
},
};

export type Crypto = {
type CryptoBase = {
algorithm: Algorithm;
validateMnemonic: (mnemonic: string) => boolean;
randomBytes: (bytesLength: number) => BinaryData;
generateFileKey: (mnemonic: string, bucketId: string, index: BinaryData | string) => Promise<BinaryData>;
computeHmac?: (key: BinaryData, shardHashes: string[]) => Promise<HmacPayload>;
};

export type Crypto = CryptoBase & {
validateMnemonic: (mnemonic: string) => boolean;
generateFileKey: (mnemonic: string, bucketId: string, index: BinaryData | string) => Promise<BinaryData>;
};

export type CryptoWithBucketKey = CryptoBase & {
generateFileKeyFromBucketKey: (bucketKey: Buffer, index: BinaryData) => Promise<BinaryData>;
};

export type EncryptFileFunction = (
algorithm: SymmetricCryptoAlgorithm,
key: BinaryData,
Expand Down
Loading