@consento/crypto
Advanced tools
Comparing version 0.5.0 to 0.5.1
import { Buffer } from '../util'; | ||
import { IHandshakeInit, IReceiver, IHandshakeInitOptions, IHandshakeAccept, IHandshakeAcceptMessage, IHandshakeAcceptOptions, IHandshakeConfirmation, IHandshakeAcceptJSON, IHandshakeConfirmationOptions, IHandshakeConfirmationJSON, IConnection, IHandshakeInitJSON } from '../types'; | ||
import { IHandshakeInit, IReader, IHandshakeInitOptions, IHandshakeAccept, IHandshakeAcceptMessage, IHandshakeAcceptOptions, IHandshakeConfirmation, IHandshakeAcceptJSON, IHandshakeConfirmationOptions, IHandshakeConfirmationJSON, IConnection, IHandshakeInitJSON } from '../types'; | ||
import { Connection } from '../primitives'; | ||
export declare const HANDSHAKE_MSG_VERSION: Buffer; | ||
export declare class HandshakeInit implements IHandshakeInit { | ||
receiver: IReceiver; | ||
receiver: IReader; | ||
firstMessage: Uint8Array; | ||
@@ -8,0 +8,0 @@ handshakeSecret: Uint8Array; |
@@ -55,3 +55,3 @@ "use strict"; | ||
constructor({ receiver, handshakeSecret, firstMessage }) { | ||
this.receiver = new primitives_1.Receiver(receiver); | ||
this.receiver = new primitives_1.Reader(receiver); | ||
this.handshakeSecret = util_1.toBuffer(handshakeSecret); | ||
@@ -74,7 +74,7 @@ this.firstMessage = util_1.toBuffer(firstMessage); | ||
} | ||
const aliceSender = new primitives_1.Sender({ sendKey }); | ||
const aliceSender = new primitives_1.Writer({ writerKey: sendKey }); | ||
return new HandshakeConfirmation({ | ||
connection: new primitives_1.Connection({ | ||
sender: aliceSender, | ||
receiver: backChannel.receiver | ||
writer: aliceSender, | ||
reader: backChannel.reader | ||
}), | ||
@@ -85,3 +85,3 @@ // In case you are wondering why we not just simply return "backChannel" as sender | ||
// TODO: rethink | ||
finalMessage: backChannel.sender.sendKey | ||
finalMessage: backChannel.writer.writerKey | ||
}); | ||
@@ -101,4 +101,4 @@ } | ||
return new primitives_1.Connection({ | ||
receiver: this.receiver, | ||
sender: new primitives_1.Sender({ sendKey: message }) | ||
reader: this.reader, | ||
writer: new primitives_1.Writer({ writerKey: message }) | ||
}); | ||
@@ -122,3 +122,3 @@ } | ||
function initHandshake() { | ||
const { receiver, sender } = primitives_1.createChannel(); | ||
const { reader: receiver, writer: sender } = primitives_1.createChannel(); | ||
const handshake = createHandshake(); | ||
@@ -131,3 +131,3 @@ return new HandshakeInit({ | ||
handshake.publicKey, | ||
sender.sendKey | ||
sender.writerKey | ||
]) | ||
@@ -141,9 +141,9 @@ }); | ||
const secretKey = computeSecret(handshake.secretKey, token); | ||
const { receiver, sender } = primitives_1.createChannel(); | ||
const { reader: receiver, writer: sender } = primitives_1.createChannel(); | ||
return new HandshakeAccept({ | ||
sender: { sendKey }, | ||
receiver, | ||
writer: { writerKey: sendKey }, | ||
reader: receiver, | ||
acceptMessage: { | ||
token: util_1.bufferToString(handshake.publicKey, 'base64'), | ||
secret: util_1.bufferToString(secretbox_1.encrypt(secretKey, sender.sendKey), 'base64') | ||
secret: util_1.bufferToString(secretbox_1.encrypt(secretKey, sender.writerKey), 'base64') | ||
} | ||
@@ -150,0 +150,0 @@ }); |
{ | ||
"name": "@consento/crypto", | ||
"version": "0.5.0", | ||
"version": "0.5.1", | ||
"description": "Crypto functionality used in Consento", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -1,10 +0,10 @@ | ||
import { IAnnonymous, IReceiver, ISender, IChannel, IChannelJSON, IChannelOptions } from '../types'; | ||
import { IVerifier, IReader, IWriter, IChannel, IChannelJSON, IChannelOptions } from '../types'; | ||
export declare function createChannel(): Channel; | ||
export declare class Channel implements IChannel { | ||
receiver: IReceiver; | ||
sender: ISender; | ||
reader: IReader; | ||
writer: IWriter; | ||
type: 'channel'; | ||
constructor(opts: IChannelOptions); | ||
get annonymous(): IAnnonymous; | ||
get verifier(): IVerifier; | ||
toJSON(): IChannelJSON; | ||
} |
@@ -24,4 +24,4 @@ "use strict"; | ||
const util_1 = require("../util"); | ||
const Receiver_1 = require("./Receiver"); | ||
const Sender_1 = require("./Sender"); | ||
const Reader_1 = require("./Reader"); | ||
const Writer_1 = require("./Writer"); | ||
const sodium = __importStar(require("sodium-universal")); | ||
@@ -49,4 +49,4 @@ const { sodium_malloc: malloc, crypto_box_PUBLICKEYBYTES: CRYPTO_BOX_PUBLICKEYBYTES, crypto_box_SECRETKEYBYTES: CRYPTO_BOX_SECRETKEYBYTES, crypto_box_keypair: boxKeyPair, crypto_sign_PUBLICKEYBYTES: CRYPTO_SIGN_PUBLICKEYBYTES, crypto_sign_SECRETKEYBYTES: CRYPTO_SIGN_SECRETKEYBYTES, crypto_sign_keypair: signKeyPair } = sodium.default; | ||
return new Channel({ | ||
receiver: { receiveKey: util_1.Buffer.concat([encrypt.publicKey, sign.publicKey, encrypt.privateKey]) }, | ||
sender: { sendKey: util_1.Buffer.concat([encrypt.publicKey, sign.publicKey, sign.privateKey]) } | ||
reader: { readerKey: util_1.Buffer.concat([encrypt.publicKey, sign.publicKey, encrypt.privateKey]) }, | ||
writer: { writerKey: util_1.Buffer.concat([encrypt.publicKey, sign.publicKey, sign.privateKey]) } | ||
}); | ||
@@ -58,18 +58,18 @@ } | ||
this.type = 'channel'; | ||
this.receiver = (opts.receiver instanceof Receiver_1.Receiver) ? opts.receiver : new Receiver_1.Receiver(opts.receiver); | ||
this.sender = (opts.sender instanceof Sender_1.Sender) ? opts.sender : new Sender_1.Sender(opts.sender); | ||
this.reader = (opts.reader instanceof Reader_1.Reader) ? opts.reader : new Reader_1.Reader(opts.reader); | ||
this.writer = (opts.writer instanceof Writer_1.Writer) ? opts.writer : new Writer_1.Writer(opts.writer); | ||
if (opts.type !== undefined && opts.type !== 'channel') { | ||
throw new Error(`Can not restore a channel from a [${opts.type}]`); | ||
} | ||
if (!util_1.bufferEquals(this.receiver.id, this.sender.id)) { | ||
throw new Error('Can not create a channel with both the sender and the receiver have a different id! Did you mean to restore a connection?'); | ||
if (!util_1.bufferEquals(this.reader.channelKey, this.writer.channelKey)) { | ||
throw new Error('Can not create a channel with both the writer and the reader have a different id! Did you mean to restore a connection?'); | ||
} | ||
} | ||
get annonymous() { | ||
return this.receiver.annonymous; | ||
get verifier() { | ||
return this.reader.verifier; | ||
} | ||
toJSON() { | ||
return { | ||
receiver: this.receiver.toJSON(), | ||
sender: this.sender.toJSON(), | ||
reader: this.reader.toJSON(), | ||
writer: this.writer.toJSON(), | ||
type: 'channel' | ||
@@ -76,0 +76,0 @@ }; |
@@ -1,5 +0,5 @@ | ||
import { IConnection, IReceiver, ISender, IConnectionJSON, IConnectionOptions } from '../types'; | ||
import { IConnection, IReader, IWriter, IConnectionJSON, IConnectionOptions } from '../types'; | ||
export declare class Connection implements IConnection { | ||
receiver: IReceiver; | ||
sender: ISender; | ||
reader: IReader; | ||
writer: IWriter; | ||
type: 'connection'; | ||
@@ -6,0 +6,0 @@ constructor(opts: IConnectionOptions); |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Connection = void 0; | ||
const Receiver_1 = require("./Receiver"); | ||
const Sender_1 = require("./Sender"); | ||
const Reader_1 = require("./Reader"); | ||
const Writer_1 = require("./Writer"); | ||
const buffer_1 = require("../util/buffer"); | ||
@@ -10,9 +10,9 @@ class Connection { | ||
this.type = 'connection'; | ||
this.receiver = (opts.receiver instanceof Receiver_1.Receiver) ? opts.receiver : new Receiver_1.Receiver(opts.receiver); | ||
this.sender = (opts.sender instanceof Sender_1.Sender) ? opts.sender : new Sender_1.Sender(opts.sender); | ||
this.reader = (opts.reader instanceof Reader_1.Reader) ? opts.reader : new Reader_1.Reader(opts.reader); | ||
this.writer = (opts.writer instanceof Writer_1.Writer) ? opts.writer : new Writer_1.Writer(opts.writer); | ||
if (opts.type !== undefined && opts.type !== 'connection') { | ||
throw new Error(`Can not restore a connection from a [${opts.type}]`); | ||
} | ||
if (buffer_1.bufferEquals(this.receiver.id, this.sender.id)) { | ||
throw new Error('Can not create a connection with both the sender and the receiver have the same id! Did you mean to restore a channel?'); | ||
if (buffer_1.bufferEquals(this.reader.channelKey, this.writer.channelKey)) { | ||
throw new Error('Can not create a connection with both the writer and the reader have the same id! Did you mean to restore a channel?'); | ||
} | ||
@@ -22,4 +22,4 @@ } | ||
return { | ||
receiver: this.receiver.toJSON(), | ||
sender: this.sender.toJSON(), | ||
reader: this.reader.toJSON(), | ||
writer: this.writer.toJSON(), | ||
type: 'connection' | ||
@@ -26,0 +26,0 @@ }; |
@@ -1,5 +0,5 @@ | ||
export * from './Annonymous'; | ||
export * from './Verifier'; | ||
export * from './Channel'; | ||
export * from './Connection'; | ||
export * from './Sender'; | ||
export * from './Receiver'; | ||
export * from './Writer'; | ||
export * from './Reader'; |
@@ -13,7 +13,7 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
__exportStar(require("./Annonymous"), exports); | ||
__exportStar(require("./Verifier"), exports); | ||
__exportStar(require("./Channel"), exports); | ||
__exportStar(require("./Connection"), exports); | ||
__exportStar(require("./Sender"), exports); | ||
__exportStar(require("./Receiver"), exports); | ||
__exportStar(require("./Writer"), exports); | ||
__exportStar(require("./Reader"), exports); | ||
//# sourceMappingURL=index.js.map |
@@ -18,7 +18,7 @@ # @consento/crypto | ||
- `Channel` - an e2e encrypted setup consisting of one `Receiver` and one `Sender` | ||
- `Sender` - an object containing the keys that allow to encrypt data | ||
- `Receiver` - an object containing the keys that allow to decrypt data created by the `Sender` | ||
- `Channel` - an e2e encrypted setup consisting of one `Reader` and one `Writer` | ||
- `Writer` - an object containing the keys that allow to encrypt data - it can _write_ on that Channel - it can not read the data! | ||
- `Reader` - an object containing the keys that allow to decrypt data - it can _read_ on that Channel - it can not write to it! | ||
- `Verifier` - an object describing an the capability to verify if a message is part of a `Channel` without being able to read it. | ||
- `Connection` - an e2e encrypted setup consisting of the `Receiver` of one channel and the `Sender` of another. | ||
- `Annonymous` - an object describing an the capability to verify if a message is part of a `Channel` | ||
- `Blob` - a self-contained piece of data, like an image or pdf. | ||
@@ -33,6 +33,6 @@ - `Handshake` - the process to connect two separate processes/devices resulting in a `Connection` for each process. | ||
const { createChannel } = require('@consento/crypto') | ||
const { receiver, sender } = createChannel() | ||
const { writer, reader } = createChannel() | ||
const encrypted = sender.encrypt('hello world') | ||
const decrypted = receiver.decrypt(encrypted) | ||
const encrypted = writer.encrypt('hello world') | ||
const decrypted = reader.decrypt(encrypted) | ||
@@ -46,18 +46,15 @@ decrypted.body === 'hello world' | ||
const channel = createChannel() | ||
const { receiver } = channel // Can decrypt messages; _could_ encrypt messages, but these would not be signed and rejected! | ||
const { sender } = channel // Can only encrypt messages. | ||
const { annonymous } = channel // Object that can verify messages but not de-/encrypt messages. | ||
const { reader } = channel // Can decrypt messages; _could_ encrypt messages, but these would not be signed and rejected! | ||
const { writer } = channel // Can only encrypt messages. | ||
const { verifier } = channel // Object that can verify messages but not de-/encrypt messages. | ||
receiver.receiveKey // To backup/restore the receiver | ||
sender.sendKey // To backup/restore the sender | ||
annonymous.idBase64 === receiver.idBase64 === sender.idBase64 // The lookup id is same here | ||
reader.readerKey // To backup/restore the receiver | ||
writer.senderKey // To backup/restore the sender | ||
reader.channelKeyBase64 === writer.channelKeyBase64 === verifier.channelKeyBase64 | ||
// The lookup id is same here, the channelKey may be used to verify the data, can also be used for the channel | ||
sender.encryptKey === receiver.encryptKey // Key to encrypt messages | ||
writer.signKey // Allows the writer to sign messages, only privy to the writer | ||
reader.decryptKey // Allows the reader to decrypt messages, only privy to the reader | ||
receiver.decryptKey // Allows the receiver to decrypt messages | ||
sender.signKey // Allows the sender to sign messages | ||
receiver.receiveKey // allows decryption of messages | ||
receiver.annonymous.id // public channel id - can be shared with other people - also used to verify if a message was properly sent. | ||
receiver.id // shortcut on the sender for the channel id | ||
writer.encryptKey.equals(receiver.encryptKey) // Key to encrypt messages, receiver _can_ write but not sign the message, thus it exists pro-forma | ||
``` | ||
@@ -68,11 +65,11 @@ | ||
```javascript | ||
const { createChannel, Receiver, Sender, Annonymous } = require('@consento/crypto') | ||
const { receiver, sender, annonymous } = createChannel() | ||
const { createChannel, Reader, Writer, Verifier } = require('@consento/crypto') | ||
const { reader, writer, verifier } = createChannel() | ||
new Receiver(receiver.toJSON()) | ||
new Sender(sender.toJSON()) | ||
new Annonymous(annonymous.toJSON()) | ||
new Reader(reader.toJSON()) | ||
new Writer(writer.toJSON()) | ||
new Verifier(verifier.toJSON()) | ||
``` | ||
### .annonymous | ||
### .verifier | ||
@@ -83,7 +80,8 @@ Both the `.sender` and the `.receiver` object have a `.annoymous` field | ||
```javascript | ||
const { receiver, sender } = createChannel() | ||
receiver.annonymous.idBase64 === sender.annonymous.idBase64 | ||
const { writer, reader } = createChannel() | ||
writer.verifier.verify(...) | ||
reader.verifier.verify(...) | ||
``` | ||
#### sender.encrypt(body) | ||
#### writer.encrypt(body) | ||
@@ -95,3 +93,3 @@ Encrypt and sign a given input with the sender key. | ||
```javascript | ||
const encrypted = sender.encrypt('secret message') | ||
const encrypted = writer.encrypt('secret message') | ||
encrypted.signature // Uint8Array | ||
@@ -101,3 +99,3 @@ encrypted.body // Uint8Array | ||
#### sender.encryptOnly(body) | ||
#### writer.encryptOnly(body) | ||
@@ -110,7 +108,7 @@ Only encrypt the body. This is only recommended in an environment where the | ||
```javascript | ||
const encrypted = sender.encrypt('secret message') | ||
const encrypted = writer.encrypt('secret message') | ||
encrypted // Uint8Array with an encrypted message | ||
``` | ||
#### sender.sign(data) | ||
#### writer.sign(data) | ||
@@ -127,3 +125,3 @@ Signs a given data. This is only recommended in an environment where the | ||
#### annonymous.verify(signature, body) | ||
#### verifier.verify(signature, body) | ||
@@ -136,7 +134,7 @@ Using the annonymous object we can verify a given data. | ||
```javascript | ||
const encrypted = sender.encrypt('hello world') | ||
const bool = annonymous.verify(encrypted.signature, encrypted.body) | ||
const encrypted = writer.encrypt('hello world') | ||
const bool = verifier.verify(encrypted.signature, encrypted.body) | ||
``` | ||
#### annonymous.verifyMessage(message) | ||
#### verifier.verifyMessage(message) | ||
@@ -148,13 +146,13 @@ As a short-cut its also possible to just verify a message | ||
```javascript | ||
const bool = annonymous.verifyMessage(message) | ||
const bool = verifier.verifyMessage(message) | ||
``` | ||
#### receiver.decrypt(encrypted) | ||
#### reader.decrypt(encrypted) | ||
Get the content of a once encrypted message. | ||
- `encrypted` - `{ signature: Uint8Array, body: Uint8Array }` as created by `sender.encrypt` or `Uint8Array` created with `sender.encryptOnly` | ||
- `encrypted` - `{ signature: Uint8Array, body: Uint8Array }` as created by `writer.encrypt` or `Uint8Array` created with `writer.encryptOnly` | ||
```javascript | ||
const message = receiver.decrypt(message:) | ||
const message = reader.decrypt(message:) | ||
``` | ||
@@ -161,0 +159,0 @@ |
import { Buffer, toBuffer, bufferToString } from '../util' | ||
import { IHandshakeInit, IReceiver, IHandshakeInitOptions, IHandshakeAccept, IHandshakeAcceptMessage, IHandshakeAcceptOptions, IHandshakeConfirmation, IHandshakeAcceptJSON, IHandshakeConfirmationOptions, IHandshakeConfirmationJSON, IConnection, IHandshakeInitJSON } from '../types' | ||
import { createChannel, Receiver, Sender, Connection } from '../primitives' | ||
import { IHandshakeInit, IReader, IHandshakeInitOptions, IHandshakeAccept, IHandshakeAcceptMessage, IHandshakeAcceptOptions, IHandshakeConfirmation, IHandshakeAcceptJSON, IHandshakeConfirmationOptions, IHandshakeConfirmationJSON, IConnection, IHandshakeInitJSON } from '../types' | ||
import { createChannel, Reader, Writer, Connection } from '../primitives' | ||
import { randomBuffer } from '../util/randomBuffer' | ||
@@ -46,3 +46,3 @@ import { decrypt, encrypt } from '../util/secretbox' | ||
export class HandshakeInit implements IHandshakeInit { | ||
receiver: IReceiver | ||
receiver: IReader | ||
firstMessage: Uint8Array | ||
@@ -52,3 +52,3 @@ handshakeSecret: Uint8Array | ||
constructor ({ receiver, handshakeSecret, firstMessage }: IHandshakeInitOptions) { | ||
this.receiver = new Receiver(receiver) | ||
this.receiver = new Reader(receiver) | ||
this.handshakeSecret = toBuffer(handshakeSecret) | ||
@@ -73,7 +73,7 @@ this.firstMessage = toBuffer(firstMessage) | ||
} | ||
const aliceSender = new Sender({ sendKey }) | ||
const aliceSender = new Writer({ writerKey: sendKey }) | ||
return new HandshakeConfirmation({ | ||
connection: new Connection({ | ||
sender: aliceSender, | ||
receiver: backChannel.receiver | ||
writer: aliceSender, | ||
reader: backChannel.reader | ||
}), | ||
@@ -84,3 +84,3 @@ // In case you are wondering why we not just simply return "backChannel" as sender | ||
// TODO: rethink | ||
finalMessage: backChannel.sender.sendKey | ||
finalMessage: backChannel.writer.writerKey | ||
}) | ||
@@ -107,4 +107,4 @@ } | ||
return new Connection({ | ||
receiver: this.receiver, | ||
sender: new Sender({ sendKey: message }) | ||
reader: this.reader, | ||
writer: new Writer({ writerKey: message }) | ||
}) | ||
@@ -132,3 +132,3 @@ } | ||
export function initHandshake (): HandshakeInit { | ||
const { receiver, sender } = createChannel() | ||
const { reader: receiver, writer: sender } = createChannel() | ||
const handshake = createHandshake() | ||
@@ -141,3 +141,3 @@ return new HandshakeInit({ | ||
handshake.publicKey, | ||
sender.sendKey | ||
sender.writerKey | ||
]) | ||
@@ -154,11 +154,11 @@ }) | ||
const secretKey = computeSecret(handshake.secretKey, token) | ||
const { receiver, sender } = createChannel() | ||
const { reader: receiver, writer: sender } = createChannel() | ||
return new HandshakeAccept({ | ||
sender: { sendKey }, | ||
receiver, | ||
writer: { writerKey: sendKey }, | ||
reader: receiver, | ||
acceptMessage: { | ||
token: bufferToString(handshake.publicKey, 'base64'), | ||
secret: bufferToString(encrypt(secretKey, sender.sendKey), 'base64') | ||
secret: bufferToString(encrypt(secretKey, sender.writerKey), 'base64') | ||
} | ||
}) | ||
} |
import { | ||
IAnnonymous, | ||
IReceiver, | ||
ISender, | ||
IVerifier, | ||
IReader, | ||
IWriter, | ||
IChannel, | ||
@@ -10,4 +10,4 @@ IChannelJSON, | ||
import { bufferEquals, Buffer } from '../util' | ||
import { Receiver } from './Receiver' | ||
import { Sender } from './Sender' | ||
import { Reader } from './Reader' | ||
import { Writer } from './Writer' | ||
import * as sodium from 'sodium-universal' | ||
@@ -52,4 +52,4 @@ | ||
return new Channel({ | ||
receiver: { receiveKey: Buffer.concat([encrypt.publicKey, sign.publicKey, encrypt.privateKey]) }, | ||
sender: { sendKey: Buffer.concat([encrypt.publicKey, sign.publicKey, sign.privateKey]) } | ||
reader: { readerKey: Buffer.concat([encrypt.publicKey, sign.publicKey, encrypt.privateKey]) }, | ||
writer: { writerKey: Buffer.concat([encrypt.publicKey, sign.publicKey, sign.privateKey]) } | ||
}) | ||
@@ -59,19 +59,19 @@ } | ||
export class Channel implements IChannel { | ||
receiver: IReceiver | ||
sender: ISender | ||
reader: IReader | ||
writer: IWriter | ||
type: 'channel' = 'channel' | ||
constructor (opts: IChannelOptions) { | ||
this.receiver = (opts.receiver instanceof Receiver) ? opts.receiver : new Receiver(opts.receiver) | ||
this.sender = (opts.sender instanceof Sender) ? opts.sender : new Sender(opts.sender) | ||
this.reader = (opts.reader instanceof Reader) ? opts.reader : new Reader(opts.reader) | ||
this.writer = (opts.writer instanceof Writer) ? opts.writer : new Writer(opts.writer) | ||
if (opts.type !== undefined && opts.type as string !== 'channel') { | ||
throw new Error(`Can not restore a channel from a [${opts.type}]`) | ||
} | ||
if (!bufferEquals(this.receiver.id, this.sender.id)) { | ||
throw new Error('Can not create a channel with both the sender and the receiver have a different id! Did you mean to restore a connection?') | ||
if (!bufferEquals(this.reader.channelKey, this.writer.channelKey)) { | ||
throw new Error('Can not create a channel with both the writer and the reader have a different id! Did you mean to restore a connection?') | ||
} | ||
} | ||
get annonymous (): IAnnonymous { | ||
return this.receiver.annonymous | ||
get verifier (): IVerifier { | ||
return this.reader.verifier | ||
} | ||
@@ -81,4 +81,4 @@ | ||
return { | ||
receiver: this.receiver.toJSON(), | ||
sender: this.sender.toJSON(), | ||
reader: this.reader.toJSON(), | ||
writer: this.writer.toJSON(), | ||
type: 'channel' | ||
@@ -85,0 +85,0 @@ } |
@@ -1,19 +0,19 @@ | ||
import { IConnection, IReceiver, ISender, IConnectionJSON, IConnectionOptions } from '../types' | ||
import { Receiver } from './Receiver' | ||
import { Sender } from './Sender' | ||
import { IConnection, IReader, IWriter, IConnectionJSON, IConnectionOptions } from '../types' | ||
import { Reader } from './Reader' | ||
import { Writer } from './Writer' | ||
import { bufferEquals } from '../util/buffer' | ||
export class Connection implements IConnection { | ||
receiver: IReceiver | ||
sender: ISender | ||
reader: IReader | ||
writer: IWriter | ||
type: 'connection' = 'connection' | ||
constructor (opts: IConnectionOptions) { | ||
this.receiver = (opts.receiver instanceof Receiver) ? opts.receiver : new Receiver(opts.receiver) | ||
this.sender = (opts.sender instanceof Sender) ? opts.sender : new Sender(opts.sender) | ||
this.reader = (opts.reader instanceof Reader) ? opts.reader : new Reader(opts.reader) | ||
this.writer = (opts.writer instanceof Writer) ? opts.writer : new Writer(opts.writer) | ||
if (opts.type !== undefined && opts.type as string !== 'connection') { | ||
throw new Error(`Can not restore a connection from a [${opts.type}]`) | ||
} | ||
if (bufferEquals(this.receiver.id, this.sender.id)) { | ||
throw new Error('Can not create a connection with both the sender and the receiver have the same id! Did you mean to restore a channel?') | ||
if (bufferEquals(this.reader.channelKey, this.writer.channelKey)) { | ||
throw new Error('Can not create a connection with both the writer and the reader have the same id! Did you mean to restore a channel?') | ||
} | ||
@@ -24,4 +24,4 @@ } | ||
return { | ||
receiver: this.receiver.toJSON(), | ||
sender: this.sender.toJSON(), | ||
reader: this.reader.toJSON(), | ||
writer: this.writer.toJSON(), | ||
type: 'connection' | ||
@@ -28,0 +28,0 @@ } |
@@ -1,5 +0,5 @@ | ||
export * from './Annonymous' | ||
export * from './Verifier' | ||
export * from './Channel' | ||
export * from './Connection' | ||
export * from './Sender' | ||
export * from './Receiver' | ||
export * from './Writer' | ||
export * from './Reader' |
@@ -24,18 +24,18 @@ /* eslint-disable @typescript-eslint/method-signature-style */ | ||
export interface IAnnonymousJSON { | ||
id: string | ||
export interface IVerifierJSON { | ||
channelKey: string | ||
} | ||
export interface IAnnonymousOptions { | ||
id: IStringOrBuffer | ||
export interface IVerifierOptions { | ||
channelKey: IStringOrBuffer | ||
} | ||
export interface IChannelId { | ||
readonly id: Uint8Array | ||
readonly idBase64: string | ||
readonly idHex: string | ||
export interface IChannelActor { | ||
readonly channelKey: Uint8Array | ||
readonly channelKeyBase64: string | ||
readonly channelKeyHex: string | ||
} | ||
export interface IAnnonymous extends IChannelId { | ||
toJSON(): IAnnonymousJSON | ||
export interface IVerifier extends IChannelActor { | ||
toJSON(): IVerifierJSON | ||
verify(signature: Uint8Array, body: Uint8Array): boolean | ||
@@ -45,17 +45,17 @@ verifyMessage(message: IEncryptedMessage): boolean | ||
export interface ISenderJSON { | ||
sendKey: string | ||
export interface IWriterJSON { | ||
writerKey: string | ||
} | ||
export interface ISenderOptions { | ||
sendKey: IStringOrBuffer | ||
export interface IWriterOptions { | ||
writerKey: IStringOrBuffer | ||
} | ||
export interface ISender extends IChannelId { | ||
toJSON(): ISenderJSON | ||
readonly sendKey: Uint8Array | ||
readonly sendKeyBase64: string | ||
export interface IWriter extends IChannelActor { | ||
toJSON(): IWriterJSON | ||
readonly writerKey: Uint8Array | ||
readonly writerKeyBase64: string | ||
readonly encryptKey: Uint8Array | ||
readonly signKey: Uint8Array | ||
readonly annonymous: IAnnonymous | ||
readonly verifier: IVerifier | ||
sign(data: Uint8Array): Uint8Array | ||
@@ -66,15 +66,15 @@ encrypt(message: IEncodable): IEncryptedMessage | ||
export interface IReceiverJSON { | ||
receiveKey: string | ||
export interface IReaderJSON { | ||
readerKey: string | ||
} | ||
export interface IReceiverOptions { | ||
receiveKey: IStringOrBuffer | ||
export interface IReaderOptions { | ||
readerKey: IStringOrBuffer | ||
} | ||
export interface IReceiver extends IChannelId { | ||
readonly receiveKey: Uint8Array | ||
readonly receiveKeyBase64: string | ||
readonly annonymous: IAnnonymous | ||
toJSON(): IReceiverJSON | ||
export interface IReader extends IChannelActor { | ||
readonly readerKey: Uint8Array | ||
readonly readerKeyBase64: string | ||
readonly verifier: IVerifier | ||
toJSON(): IReaderJSON | ||
/** | ||
@@ -91,4 +91,4 @@ * Decrypts a message written by an associated Sender | ||
export interface IComJSON<TType = ComType> { | ||
receiver: IReceiverJSON | ||
sender: ISenderJSON | ||
reader: IReaderJSON | ||
writer: IWriterJSON | ||
type: TType | ||
@@ -98,9 +98,9 @@ } | ||
export interface IComOptions<TType = ComType> { | ||
sender: ISenderOptions | ||
receiver: IReceiverOptions | ||
writer: IWriterOptions | ||
reader: IReaderOptions | ||
type?: TType | ||
} | ||
export interface ICom<TType = ComType> { | ||
sender: ISender | ||
receiver: IReceiver | ||
writer: IWriter | ||
reader: IReader | ||
type: TType | ||
@@ -114,3 +114,3 @@ toJSON(): IComJSON<TType> | ||
export interface IChannel extends ICom<'channel'> { | ||
annonymous: IAnnonymous | ||
verifier: IVerifier | ||
} | ||
@@ -121,3 +121,3 @@ export type IChannelJSON = IComJSON<'channel'> | ||
export interface IHandshakeInitJSON { | ||
receiver: IReceiverJSON | ||
receiver: IReaderJSON | ||
firstMessage: string | ||
@@ -128,3 +128,3 @@ handshakeSecret: string | ||
export interface IHandshakeInitOptions { | ||
receiver: IReceiver | IReceiverOptions | ||
receiver: IReader | IReaderOptions | ||
firstMessage: IStringOrBuffer | ||
@@ -135,3 +135,3 @@ handshakeSecret: IStringOrBuffer | ||
export interface IHandshakeInit { | ||
receiver: IReceiver | ||
receiver: IReader | ||
firstMessage: Uint8Array | ||
@@ -138,0 +138,0 @@ handshakeSecret: Uint8Array |
@@ -17,31 +17,31 @@ import { IEncodable, IStringOrBuffer } from './util/types'; | ||
export declare type IDecryption = IDecryptionSuccess | IDecryptionError; | ||
export interface IAnnonymousJSON { | ||
id: string; | ||
export interface IVerifierJSON { | ||
channelKey: string; | ||
} | ||
export interface IAnnonymousOptions { | ||
id: IStringOrBuffer; | ||
export interface IVerifierOptions { | ||
channelKey: IStringOrBuffer; | ||
} | ||
export interface IChannelId { | ||
readonly id: Uint8Array; | ||
readonly idBase64: string; | ||
readonly idHex: string; | ||
export interface IChannelActor { | ||
readonly channelKey: Uint8Array; | ||
readonly channelKeyBase64: string; | ||
readonly channelKeyHex: string; | ||
} | ||
export interface IAnnonymous extends IChannelId { | ||
toJSON(): IAnnonymousJSON; | ||
export interface IVerifier extends IChannelActor { | ||
toJSON(): IVerifierJSON; | ||
verify(signature: Uint8Array, body: Uint8Array): boolean; | ||
verifyMessage(message: IEncryptedMessage): boolean; | ||
} | ||
export interface ISenderJSON { | ||
sendKey: string; | ||
export interface IWriterJSON { | ||
writerKey: string; | ||
} | ||
export interface ISenderOptions { | ||
sendKey: IStringOrBuffer; | ||
export interface IWriterOptions { | ||
writerKey: IStringOrBuffer; | ||
} | ||
export interface ISender extends IChannelId { | ||
toJSON(): ISenderJSON; | ||
readonly sendKey: Uint8Array; | ||
readonly sendKeyBase64: string; | ||
export interface IWriter extends IChannelActor { | ||
toJSON(): IWriterJSON; | ||
readonly writerKey: Uint8Array; | ||
readonly writerKeyBase64: string; | ||
readonly encryptKey: Uint8Array; | ||
readonly signKey: Uint8Array; | ||
readonly annonymous: IAnnonymous; | ||
readonly verifier: IVerifier; | ||
sign(data: Uint8Array): Uint8Array; | ||
@@ -51,13 +51,13 @@ encrypt(message: IEncodable): IEncryptedMessage; | ||
} | ||
export interface IReceiverJSON { | ||
receiveKey: string; | ||
export interface IReaderJSON { | ||
readerKey: string; | ||
} | ||
export interface IReceiverOptions { | ||
receiveKey: IStringOrBuffer; | ||
export interface IReaderOptions { | ||
readerKey: IStringOrBuffer; | ||
} | ||
export interface IReceiver extends IChannelId { | ||
readonly receiveKey: Uint8Array; | ||
readonly receiveKeyBase64: string; | ||
readonly annonymous: IAnnonymous; | ||
toJSON(): IReceiverJSON; | ||
export interface IReader extends IChannelActor { | ||
readonly readerKey: Uint8Array; | ||
readonly readerKeyBase64: string; | ||
readonly verifier: IVerifier; | ||
toJSON(): IReaderJSON; | ||
/** | ||
@@ -72,14 +72,14 @@ * Decrypts a message written by an associated Sender | ||
export interface IComJSON<TType = ComType> { | ||
receiver: IReceiverJSON; | ||
sender: ISenderJSON; | ||
reader: IReaderJSON; | ||
writer: IWriterJSON; | ||
type: TType; | ||
} | ||
export interface IComOptions<TType = ComType> { | ||
sender: ISenderOptions; | ||
receiver: IReceiverOptions; | ||
writer: IWriterOptions; | ||
reader: IReaderOptions; | ||
type?: TType; | ||
} | ||
export interface ICom<TType = ComType> { | ||
sender: ISender; | ||
receiver: IReceiver; | ||
writer: IWriter; | ||
reader: IReader; | ||
type: TType; | ||
@@ -92,3 +92,3 @@ toJSON(): IComJSON<TType>; | ||
export interface IChannel extends ICom<'channel'> { | ||
annonymous: IAnnonymous; | ||
verifier: IVerifier; | ||
} | ||
@@ -98,3 +98,3 @@ export declare type IChannelJSON = IComJSON<'channel'>; | ||
export interface IHandshakeInitJSON { | ||
receiver: IReceiverJSON; | ||
receiver: IReaderJSON; | ||
firstMessage: string; | ||
@@ -104,3 +104,3 @@ handshakeSecret: string; | ||
export interface IHandshakeInitOptions { | ||
receiver: IReceiver | IReceiverOptions; | ||
receiver: IReader | IReaderOptions; | ||
firstMessage: IStringOrBuffer; | ||
@@ -110,3 +110,3 @@ handshakeSecret: IStringOrBuffer; | ||
export interface IHandshakeInit { | ||
receiver: IReceiver; | ||
receiver: IReader; | ||
firstMessage: Uint8Array; | ||
@@ -113,0 +113,0 @@ handshakeSecret: Uint8Array; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
145745
0
260