Skip to content

Commit

Permalink
Fix signcryption keypair types
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelthomas2774 committed Jul 22, 2020
1 parent 4ba2a4d commit d48ec57
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ import * as tweetnacl from 'tweetnacl';
// If you know the sender's public key you can pass it to DearmorAndVerifyStream and it will emit an error if it doesn't match
const sender_key: Uint8Array = tweetnacl.sign.keyPair().publicKey;

const stream = new DearmorAndVerifyStream(recipient_keypair, sender_key);
const stream = new DearmorAndVerifyStream(sender_key);

stream.on('end', () => {
// If you didn't pass the sender's public key you should check it now
Expand Down
6 changes: 3 additions & 3 deletions src/signcryption/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export let debug_fix_key: Buffer | null = null;
export let debug_fix_keypair: tweetnacl.BoxKeyPair | null = null;

export async function signcrypt(
data: Uint8Array | string, keypair: tweetnacl.BoxKeyPair | null, recipients_keys: Uint8Array[]
data: Uint8Array | string, keypair: tweetnacl.SignKeyPair | null, recipients_keys: Uint8Array[]
): Promise<Buffer> {
const chunks = chunkBuffer(data, CHUNK_LENGTH);

Expand Down Expand Up @@ -58,13 +58,13 @@ export async function signcrypt(
export class SigncryptStream extends Transform {
readonly payload_key: Buffer;
readonly ephemeral_keypair: tweetnacl.BoxKeyPair;
readonly keypair: tweetnacl.BoxKeyPair | null;
readonly keypair: tweetnacl.SignKeyPair | null;
readonly header: SigncryptedMessageHeader;
private in_buffer = Buffer.alloc(0);
private payload_index = BigInt(0);
private i = 0;

constructor(keypair: tweetnacl.BoxKeyPair | null, recipients_keys: Uint8Array[]) {
constructor(keypair: tweetnacl.SignKeyPair | null, recipients_keys: Uint8Array[]) {
super();

// 1. Generate a random 32-byte payload key.
Expand Down
16 changes: 8 additions & 8 deletions src/with-armor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export async function signAndArmor(data: Uint8Array | string, keypair: tweetnacl
const signed = sign(data, keypair);
return armor(signed, {message_type: MessageType.SIGNED_MESSAGE});
}
export async function verifyArmored(signed: string, public_key: Uint8Array): Promise<DearmorAndVerifyResult> {
export async function verifyArmored(signed: string, public_key?: Uint8Array | null): Promise<DearmorAndVerifyResult> {
const dearmored = dearmor(signed);
return Object.assign(await verify(dearmored, public_key), {
remaining: dearmored.remaining,
Expand Down Expand Up @@ -113,9 +113,9 @@ export async function signDetachedAndArmor(data: Uint8Array | string, keypair: t
return armor(signed, {message_type: MessageType.DETACHED_SIGNATURE});
}
export async function verifyDetachedArmored(
signed: string, data: Uint8Array | string, public_key: Uint8Array
signature: string, data: Uint8Array | string, public_key?: Uint8Array | null
): Promise<DearmorAndVerifyDetachedResult> {
const dearmored = dearmor(signed);
const dearmored = dearmor(signature);
const result = await verifyDetached(dearmored, data, public_key);

return {
Expand All @@ -131,16 +131,16 @@ export interface DearmorAndVerifyDetachedResult extends VerifyDetachedResult {
}

export async function signcryptAndArmor(
data: Uint8Array | string, keypair: tweetnacl.BoxKeyPair | null, recipients_keys: Uint8Array[]
data: Uint8Array | string, keypair: tweetnacl.SignKeyPair | null, recipients_keys: Uint8Array[]
) {
const encrypted = await signcrypt(data, keypair, recipients_keys);
return armor(encrypted, {message_type: MessageType.ENCRYPTED_MESSAGE});
}
export async function dearmorAndDesigncrypt(
encrypted: string, keypair: tweetnacl.BoxKeyPair
signcrypted: string, keypair: tweetnacl.BoxKeyPair, sender?: Uint8Array | null
): Promise<DearmorAndDesigncryptResult> {
const dearmored = dearmor(encrypted);
return Object.assign(await designcrypt(dearmored, keypair), {
const dearmored = dearmor(signcrypted);
return Object.assign(await designcrypt(dearmored, keypair, sender), {
remaining: dearmored.remaining,
header_info: dearmored.header_info,
});
Expand All @@ -150,7 +150,7 @@ export type DearmorAndDesigncryptResult = DearmorResult & DesigncryptResult;

export class SigncryptAndArmorStream extends Pumpify {
constructor(
keypair: tweetnacl.BoxKeyPair | null, recipients_keys: Uint8Array[], armor_options?: Partial<ArmorOptions>
keypair: tweetnacl.SignKeyPair | null, recipients_keys: Uint8Array[], armor_options?: Partial<ArmorOptions>
) {
const encrypt = new SigncryptStream(keypair, recipients_keys);
const armor = new ArmorStream(Object.assign({
Expand Down

0 comments on commit d48ec57

Please sign in to comment.