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

Updated #14

Merged
merged 22 commits into from
Jun 6, 2024
Merged
Changes from 1 commit
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
Next Next commit
Update index.ts
  • Loading branch information
bobslavtriev committed Jun 6, 2024
commit 8e5044f7ce5401a87d697545ed584f4ea3d682f4
62 changes: 56 additions & 6 deletions src/Utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { curve } from 'libsignal'
import { randomBytes } from 'crypto'
import { v4 } from 'uuid'
import { KeyPair, valueReplacer, valueReviver } from '../Types'
import { randomBytes, randomUUID } from 'crypto'
import { KeyPair, valueReplacer, valueReviver, AppDataSync, Bits } from '../Types'

const generateKeyPair = () => {
const { pubKey, privKey } = curve.generateKeyPair()
Expand All @@ -26,6 +25,56 @@ const signedKeyPair = (identityKeyPair: KeyPair, keyId: number) => {
return { keyPair: preKey, signature, keyId }
}

const allocate = (str: string) => {
let p = str.length

if (!p){
return new Uint8Array(1)
}

let n = 0

while (--p % 4 > 1 && str.charAt(p) === "="){
++n
}

return new Uint8Array(Math.ceil(str.length * 3) / 4 - n).fill(0)
}

const parseTimestamp = (timestamp: Bits | number) => {
if (typeof timestamp === 'string') {
return parseInt(timestamp, 10)
}

if (typeof timestamp === "number") {
return timestamp
}

return {
low: 0,
high: 0,
unsigned: false
}
}

export const fromObject = (args: AppDataSync) => {
const message = {
keyData: Array.isArray(args.keyData) ? args.keyData : new Uint8Array(),
fingerprint: {
rawId: args.fingerprint.rawId,
currentIndex: args.fingerprint.rawId,
deviceIndexes: Array.isArray(args.fingerprint.deviceIndexes) ? args.fingerprint.deviceIndexes : []
},
timestamp: parseTimestamp(args.timestamp)
}

if (typeof args.keyData === "string") {
message.keyData = allocate(args.keyData)
}

return message
}

export const BufferJSON = {
replacer: (_: string, value: valueReplacer) => {
if(value?.type === 'Buffer' && Array.isArray(value?.data)) {
Expand Down Expand Up @@ -60,13 +109,14 @@ export const initAuthCreds = () => {
accountSettings: {
unarchiveChats: false
},
deviceId: Buffer.from(v4().replace(/-/g, ''), 'hex').toString('base64url'),
phoneId: v4(),
deviceId: Buffer.from(randomUUID().replace(/-/g, ''), 'hex').toString('base64url'),
phoneId: randomUUID(),
identityId: randomBytes(20),
backupToken: randomBytes(20),
registered: false,
registration: {},
pairingCode: undefined,
lastPropHash: undefined
lastPropHash: undefined,
routingInfo: undefined
}
}