Skip to content

Commit

Permalink
Align web-stream-tools version with OpenPGP.js, fix test-type-definit…
Browse files Browse the repository at this point in the history
…ions

Also, rename `MaybeStream` to `MaybeWebStream` to help differentiate it from
`openpgpjs.MaybeStream`, which can also be a NodeStream.
  • Loading branch information
larabr committed Apr 17, 2024
1 parent 73ac95b commit 5b3bcbf
Show file tree
Hide file tree
Showing 11 changed files with 42 additions and 38 deletions.
4 changes: 2 additions & 2 deletions lib/crypto/hash.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { MaybeStream } from '../pmcrypto';
import type { MaybeWebStream } from '../pmcrypto';
import md5 from './_md5';

export const SHA256 = async (data: Uint8Array) => {
Expand All @@ -23,7 +23,7 @@ export const unsafeMD5 = (data: Uint8Array) => md5(data);
* DO NOT USE in contexts where collision resistance is important
* @see openpgp.crypto.hash.sha1
*/
export async function unsafeSHA1(data: MaybeStream<Uint8Array>) {
export async function unsafeSHA1(data: MaybeWebStream<Uint8Array>) {
if (data instanceof Uint8Array) {
const digest = await crypto.subtle.digest('SHA-1', data);
return new Uint8Array(digest);
Expand Down
3 changes: 2 additions & 1 deletion lib/key/forwarding.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { KDFParams, PrivateKey, UserID, SecretSubkeyPacket, SecretKeyPacket, MaybeArray, Subkey, config as defaultConfig, SubkeyOptions, enums } from '../openpgp';
import { KDFParams, PrivateKey, UserID, SecretSubkeyPacket, SecretKeyPacket, Subkey, config as defaultConfig, SubkeyOptions, enums } from '../openpgp';
import { getBigInteger } from '../bigInteger';
import { generateKey, reformatKey } from './utils';
import { serverTime } from '../serverTime';
import { MaybeArray } from '../utils';

export async function computeProxyParameter(
forwarderSecret: Uint8Array,
Expand Down
2 changes: 1 addition & 1 deletion lib/key/utils.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {
generateKey,
reformatKey,
generateSessionKey as openpgp_generateSessionKey,
MaybeArray,
PrivateKey,
PublicKey,
Key,
Expand All @@ -13,6 +12,7 @@ import {
Signature,
Message
} from '../openpgp';
import { MaybeArray } from '../utils';

export { generateKey, reformatKey, GenerateKeyOptions };

Expand Down
4 changes: 2 additions & 2 deletions lib/message/processMIME.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { verifyMessage } from './verify';
import { VERIFICATION_STATUS, MAX_ENC_HEADER_LENGTH } from '../constants';
import { serverTime } from '../serverTime';
import { parseMail, generateFileName } from './parseMail';
import { readSignature, PublicKey, MaybeArray, Signature as OpenPGPSignature } from '../openpgp';
import { arrayToHexString, utf8ArrayToString } from '../utils';
import { readSignature, PublicKey, Signature as OpenPGPSignature } from '../openpgp';
import { arrayToHexString, utf8ArrayToString, MaybeArray } from '../utils';

import type { Attachment } from './parseMail';

Expand Down
2 changes: 1 addition & 1 deletion lib/message/sign.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { removeTrailingSpaces } from './utils';
* @param {String|ReadableStream<String>} textData - text data to sign
* @param {Uint8Array|ReadableStream<Uint8Array>} binaryData - binary data to sign
* @param {Boolean} stripTrailingSpaces - whether trailing spaces should be removed from `textData`
* @returns Promise<{Message|Signature|MaybeStream<String>|MaybeStream<Uint8Array>}> signed message object, signature object, or corresponding serialised data
* @returns Promise<{Message|Signature|MaybeWebStream<String>|MaybeWebStream<Uint8Array>}> signed message object, signature object, or corresponding serialised data
* @throws on signing error
*/
export default async function signMessage({
Expand Down
30 changes: 15 additions & 15 deletions lib/pmcrypto.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,21 +67,21 @@ export type DecryptSessionKeyOptionsPmcrypto = Parameters<typeof openpgp_decrypt
// This differs from `openpgp.decryptSessionKeys` in the return type
export function decryptSessionKey(options: DecryptSessionKeyOptionsPmcrypto): Promise<SessionKey | undefined>;

export interface DecryptOptionsPmcrypto<T extends MaybeStream<Data>> extends DecryptOptions {
export interface DecryptOptionsPmcrypto<T extends MaybeWebStream<Data>> extends DecryptOptions {
message: Message<T>;
encryptedSignature?: Message<MaybeStream<Data>>;
encryptedSignature?: Message<MaybeWebStream<Data>>;
context?: ContextVerificationOptions
}

export interface DecryptResultPmcrypto<DataType extends openpgp_DecryptMessageResult['data'] = MaybeStream<Data>> {
export interface DecryptResultPmcrypto<DataType extends openpgp_DecryptMessageResult['data'] = MaybeWebStream<Data>> {
data: DataType;
signatures: DataType extends WebStream<Data> ? Promise<OpenPGPSignature[]> : OpenPGPSignature[];
filename: string;
verified: DataType extends WebStream<Data> ? Promise<VERIFICATION_STATUS> : VERIFICATION_STATUS;
verificationErrors?: DataType extends WebStream<Data> ? Promise<Error[]> : Error[];
}

export function decryptMessage<DataType extends MaybeStream<Data>, FormatType extends DecryptOptions['format'] = 'utf8'>(
export function decryptMessage<DataType extends MaybeWebStream<Data>, FormatType extends DecryptOptions['format'] = 'utf8'>(
options: DecryptOptionsPmcrypto<DataType> & { format?: FormatType }
): Promise<
FormatType extends 'utf8' ?
Expand All @@ -95,14 +95,14 @@ export function decryptMessage<DataType extends MaybeStream<Data>, FormatType ex
never
>;

export type MaybeStream<T extends Uint8Array | string> = T | WebStream<T>;
export type Data = string | Uint8Array;
export type MaybeWebStream<T extends Data> = T | WebStream<T>;
export { WebStream };

export interface EncryptOptionsPmcrypto<T extends MaybeStream<Data>> extends Omit<EncryptOptions, 'message' | 'signatureNotations'> {
textData?: T extends MaybeStream<string> ? T : never;
binaryData?: T extends MaybeStream<Uint8Array> ? T : never;
stripTrailingSpaces?: T extends MaybeStream<string> ? boolean : never;
export interface EncryptOptionsPmcrypto<T extends MaybeWebStream<Data>> extends Omit<EncryptOptions, 'message' | 'signatureNotations'> {
textData?: T extends MaybeWebStream<string> ? T : never;
binaryData?: T extends MaybeWebStream<Uint8Array> ? T : never;
stripTrailingSpaces?: T extends MaybeWebStream<string> ? boolean : never;
detached?: boolean;
context?: ContextSigningOptions;
}
Expand All @@ -119,7 +119,7 @@ export interface EncryptResult<
}

export function encryptMessage<
DataType extends MaybeStream<Data>,
DataType extends MaybeWebStream<Data>,
FormatType extends EncryptOptions['format'] = 'armored', // extends 'string' also works, but it gives unclear error if passed unexpected 'format' values
DetachedType extends boolean = false
>(
Expand Down Expand Up @@ -149,15 +149,15 @@ export function encryptMessage<
never
>;

export interface SignOptionsPmcrypto<T extends MaybeStream<Data>> extends Omit<SignOptions, 'message' | 'signatureNotations'> {
textData?: T extends MaybeStream<string> ? T : never;
binaryData?: T extends MaybeStream<Uint8Array> ? T : never;
stripTrailingSpaces?: T extends MaybeStream<string> ? boolean : never;
export interface SignOptionsPmcrypto<T extends MaybeWebStream<Data>> extends Omit<SignOptions, 'message' | 'signatureNotations'> {
textData?: T extends MaybeWebStream<string> ? T : never;
binaryData?: T extends MaybeWebStream<Uint8Array> ? T : never;
stripTrailingSpaces?: T extends MaybeWebStream<string> ? boolean : never;
context?: ContextSigningOptions;
}

export function signMessage<
DataType extends MaybeStream<Data>,
DataType extends MaybeWebStream<Data>,
FormatType extends SignOptions['format'] = 'armored',
DetachedType extends boolean = false
>(
Expand Down
8 changes: 5 additions & 3 deletions lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Most of these util functions are copied as-is from https://github.com/openpgpjs/openpgpjs/blob/v5.0.0/src/util.js
import type { MaybeStream, WebStream } from './pmcrypto';
import type { MaybeWebStream, WebStream } from './pmcrypto';

export type MaybeArray<T> = T | Array<T>;

const ifDefined = <T, R>(cb: (input: T) => R) => <U extends T | undefined>(input: U) => {
return (input !== undefined ? cb(input as T) : undefined) as U extends T ? R : undefined;
Expand Down Expand Up @@ -111,7 +113,7 @@ export const arrayToHexString = (bytes: Uint8Array) => {
*/
export function stringToUtf8Array(str: string): Uint8Array;
export function stringToUtf8Array(str: WebStream<string>): WebStream<Uint8Array>;
export function stringToUtf8Array(str: MaybeStream<string>): MaybeStream<Uint8Array> {
export function stringToUtf8Array(str: MaybeWebStream<string>): MaybeWebStream<Uint8Array> {
const encoder = new TextEncoder();

if (isString(str)) return encoder.encode(str);
Expand Down Expand Up @@ -141,7 +143,7 @@ export function stringToUtf8Array(str: MaybeStream<string>): MaybeStream<Uint8Ar
*/
export function utf8ArrayToString(utf8: Uint8Array): string;
export function utf8ArrayToString(utf8: WebStream<Uint8Array>): WebStream<string>;
export function utf8ArrayToString(utf8: MaybeStream<Uint8Array>): MaybeStream<string> {
export function utf8ArrayToString(utf8: MaybeWebStream<Uint8Array>): MaybeWebStream<string> {
const decoder = new TextDecoder();

if (utf8 instanceof Uint8Array) return decoder.decode(utf8);
Expand Down
17 changes: 10 additions & 7 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"dependencies": {
"@openpgp/asmcrypto.js": "^3.1.0",
"@openpgp/noble-hashes": "^1.3.3",
"@openpgp/web-stream-tools": "^0.0.14",
"@openpgp/web-stream-tools": "~0.1.1",
"jsmimeparser": "npm:@protontech/jsmimeparser@^3.0.1",
"openpgp": "protonmail/openpgpjs#v6"
},
Expand Down
4 changes: 1 addition & 3 deletions test/message/encryptMessage.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { expect } from 'chai';
// @ts-ignore missing web-stream-tools types
import { readToEnd, toStream, WebStream } from '@openpgp/web-stream-tools';

import { config as globalConfig, CompressedDataPacket, enums, SymEncryptedSessionKeyPacket, PartialConfig } from '../../lib/openpgp';

import { decryptKey, readPrivateKey, verifyMessage, encryptMessage, decryptMessage, generateSessionKey, readSignature, readMessage, encryptSessionKey, decryptSessionKey } from '../../lib';
Expand Down Expand Up @@ -314,7 +312,7 @@ describe('message encryption and decryption', () => {
const encryptedArmoredMessage = await readToEnd(encrypted);

const { data: decrypted, verified } = await decryptMessage({
message: await readMessage({ armoredMessage: toStream(encryptedArmoredMessage) }),
message: await readMessage({ armoredMessage: toStream(encryptedArmoredMessage) as WebStream<string> }),
encryptedSignature: await readMessage({ armoredMessage: encryptedSignature }),
sessionKeys: sessionKey,
verificationKeys: [decryptedPrivateKey.toPublic()],
Expand Down
4 changes: 2 additions & 2 deletions test/message/signMessage.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from 'chai';
import { readToEnd, WebStream } from '@openpgp/web-stream-tools';
import { readToEnd } from '@openpgp/web-stream-tools';
import { verifyMessage, signMessage, generateKey, readSignature } from '../../lib';
import { VERIFICATION_STATUS } from '../../lib/constants';
import { stringToUtf8Array } from '../../lib/utils';
Expand Down Expand Up @@ -103,7 +103,7 @@ describe('message signing', () => {
});

it('signMessage/verifyMessage - it verifies a streamed message it has signed', async () => {
const inputStream: WebStream<string> = new ReadableStream<string>({
const inputStream = new ReadableStream<string>({
pull: (controller) => { for (let i = 0; i < 10000; i++) { controller.enqueue('string'); } controller.close(); }
});
const inputData = 'string'.repeat(10000);
Expand Down

0 comments on commit 5b3bcbf

Please sign in to comment.