feat: add signal repository + tests

This commit is contained in:
Adhiraj Singh
2023-03-18 12:25:47 +05:30
parent 2eea17fe9f
commit fe1d0649b5
21 changed files with 500 additions and 206 deletions

View File

@@ -3,7 +3,12 @@ import type { Contact } from './Contact'
import type { MinimalMessage } from './Message'
export type KeyPair = { public: Uint8Array, private: Uint8Array }
export type SignedKeyPair = { keyPair: KeyPair, signature: Uint8Array, keyId: number }
export type SignedKeyPair = {
keyPair: KeyPair
signature: Uint8Array
keyId: number
timestampS?: number
}
export type ProtocolAddress = {
name: string // jid
@@ -57,8 +62,8 @@ export type AuthenticationCreds = SignalCreds & {
export type SignalDataTypeMap = {
'pre-key': KeyPair
'session': any
'sender-key': any
'session': Uint8Array
'sender-key': Uint8Array
'sender-key-memory': { [jid: string]: boolean }
'app-state-sync-key': proto.Message.IAppStateSyncKeyData
'app-state-sync-version': LTHashState

View File

@@ -32,10 +32,10 @@ export interface GroupMetadata {
export interface WAGroupCreateResponse {
status: number
gid?: string
participants?: [{ [key: string]: any }]
participants?: [{ [key: string]: {} }]
}
export interface GroupModificationResponse {
status: number
participants?: { [key: string]: any }
participants?: { [key: string]: {} }
}

View File

@@ -1,5 +1,4 @@
import { AxiosRequestConfig } from 'axios'
import type NodeCache from 'node-cache'
import type { Logger } from 'pino'
import type { Readable } from 'stream'
import type { URL } from 'url'
@@ -19,9 +18,9 @@ export type WATextMessage = proto.Message.IExtendedTextMessage
export type WAContextInfo = proto.IContextInfo
export type WALocationMessage = proto.Message.ILocationMessage
export type WAGenericMediaMessage = proto.Message.IVideoMessage | proto.Message.IImageMessage | proto.Message.IAudioMessage | proto.Message.IDocumentMessage | proto.Message.IStickerMessage
// eslint-disable-next-line no-unused-vars
// eslint-disable-next-line no-unused-vars, @typescript-eslint/no-unused-vars
export import WAMessageStubType = proto.WebMessageInfo.StubType
// eslint-disable-next-line no-unused-vars
// eslint-disable-next-line no-unused-vars, @typescript-eslint/no-unused-vars
export import WAMessageStatus = proto.WebMessageInfo.Status
export type WAMediaUpload = Buffer | { url: URL | string } | { stream: Readable }
/** Set of message types that are supported by the library */

View File

@@ -3,12 +3,13 @@ import { WAMediaUpload } from './Message'
export type CatalogResult = {
data: {
paging: { cursors: { before: string, after: string } }
// eslint-disable-next-line @typescript-eslint/no-explicit-any
data: any[]
}
}
export type ProductCreateResult = {
data: { product: any }
data: { product: {} }
}
export type CatalogStatus = {

68
src/Types/Signal.ts Normal file
View File

@@ -0,0 +1,68 @@
import { proto } from '../../WAProto'
type DecryptGroupSignalOpts = {
group: string
authorJid: string
msg: Uint8Array
}
type ProcessSenderKeyDistributionMessageOpts = {
item: proto.Message.ISenderKeyDistributionMessage
authorJid: string
}
type DecryptSignalProtoOpts = {
jid: string
type: 'pkmsg' | 'msg'
ciphertext: Uint8Array
}
type EncryptMessageOpts = {
jid: string
data: Uint8Array
}
type EncryptGroupMessageOpts = {
group: string
data: Uint8Array
meId: string
}
type PreKey = {
keyId: number
publicKey: Uint8Array
}
type SignedPreKey = PreKey & {
signature: Uint8Array
}
type E2ESession = {
registrationId: number
identityKey: Uint8Array
signedPreKey: SignedPreKey
preKey: PreKey
}
type E2ESessionOpts = {
jid: string
session: E2ESession
}
export type SignalRepository = {
decryptGroupMessage(opts: DecryptGroupSignalOpts): Promise<Uint8Array>
processSenderKeyDistributionMessage(
opts: ProcessSenderKeyDistributionMessageOpts
): Promise<void>
decryptMessage(opts: DecryptSignalProtoOpts): Promise<Uint8Array>
encryptMessage(opts: EncryptMessageOpts): Promise<{
type: 'pkmsg' | 'msg'
ciphertext: Uint8Array
}>
encryptGroupMessage(opts: EncryptGroupMessageOpts): Promise<{
senderKeyDistributionMessage: Uint8Array
ciphertext: Uint8Array
}>
injectE2ESession(opts: E2ESessionOpts): Promise<void>
jidToSignalProtocolAddress(jid: string): string
}

View File

@@ -4,8 +4,9 @@ import type { Agent } from 'https'
import type { Logger } from 'pino'
import type { URL } from 'url'
import { proto } from '../../WAProto'
import { AuthenticationState, TransactionCapabilityOptions } from './Auth'
import { AuthenticationState, SignalAuthState, TransactionCapabilityOptions } from './Auth'
import { MediaConnInfo } from './Message'
import { SignalRepository } from './Signal'
export type WAVersion = [number, number, number]
export type WABrowserDescription = [string, string, string]
@@ -106,7 +107,10 @@ export type SocketConfig = {
options: AxiosRequestConfig<{}>
/**
* fetch a message from your store
* implement this so that messages failed to send (solves the "this message can take a while" issue) can be retried
* implement this so that messages failed to send
* (solves the "this message can take a while" issue) can be retried
* */
getMessage: (key: proto.IMessageKey) => Promise<proto.IMessage | undefined>
makeSignalRepository: (auth: SignalAuthState) => SignalRepository
}

View File

@@ -8,6 +8,7 @@ export * from './Socket'
export * from './Events'
export * from './Product'
export * from './Call'
export * from './Signal'
import { AuthenticationState } from './Auth'
import { SocketConfig } from './Socket'