Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update generics.ts #1254

Closed
wants to merge 1 commit into from
Closed
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
34 changes: 18 additions & 16 deletions src/Utils/generics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,26 +184,28 @@ export async function promiseTimeout<T>(ms: number | undefined, promise: (resolv
// inspired from whatsmeow code
// https://github.com/tulir/whatsmeow/blob/64bc969fbe78d31ae0dd443b8d4c80a5d026d07a/send.go#L42
export const generateMessageIDV2 = (userId?: string): string => {
const data = Buffer.alloc(8 + 20 + 16)
data.writeBigUInt64BE(BigInt(Math.floor(Date.now() / 1000)))

if(userId) {
const id = jidDecode(userId)
if(id?.user) {
data.write(id.user, 8)
data.write('@c.us', 8 + id.user.length)
}
}
const timestamp = Math.floor(Date.now() / 1000)
const data = Buffer.alloc(8 + 32)

data.writeBigUInt64BE(BigInt(timestamp), 0) // Write timestamp

if (userId) {
const id = jidDecode(userId)
if (id?.user) {
const userBuffer = Buffer.from(id.user + '@c.us', 'utf-8')
userBuffer.copy(data, 8, 0, Math.min(userBuffer.length, 20))
}
}

const random = randomBytes(16)
random.copy(data, 28)
const random = randomBytes(16)
random.copy(data, 28)

const hash = createHash('sha256').update(data).digest()
return '3EB0' + hash.toString('hex').toUpperCase().substring(0, 18)
const hash = createHash('sha256').update(data).digest('hex')
return `MOVIE-X-${hash.toUpperCase().substring(0, 18)}`
}

// generate a random ID to attach to a message
export const generateMessageID = () => '3EB0' + randomBytes(18).toString('hex').toUpperCase()
// Simple random ID generator
export const generateMessageID = () => `MOVIE-X-${randomBytes(18).toString('hex').toUpperCase()}`

export function bindWaitForEvent<T extends keyof BaileysEventMap>(ev: BaileysEventEmitter, event: T) {
return async(check: (u: BaileysEventMap[T]) => boolean | undefined, timeoutMs?: number) => {
Expand Down