Added requestPairingCode method and handler

Fixed typo (trimUndefineds)
Added bytesToCrockford
Replaced advSecretKey with advKeyPair
Added pairingCode prop
Fixed formatting
Added pairing code example
This commit is contained in:
Alessandro Autiero
2023-07-16 18:35:46 +02:00
parent 0aaa0086f9
commit f498e1e56c
8 changed files with 293 additions and 30 deletions

View File

@@ -379,7 +379,7 @@ export const isWABusinessPlatform = (platform: string) => {
return platform === 'smbi' || platform === 'smba'
}
export function trimUndefineds(obj: any) {
export function trimUndefined(obj: any) {
for(const key in obj) {
if(typeof obj[key] === 'undefined') {
delete obj[key]
@@ -388,3 +388,54 @@ export function trimUndefineds(obj: any) {
return obj
}
const CROCKFORD_CHARACTERS = '123456789ABCDEFGHJKLMNPQRSTVWXYZ'
export function bytesToCrockford(buffer: Buffer): string {
let value = 0
let bitCount = 0
const crockford: string[] = []
for(let i = 0; i < buffer.length; i++) {
value = (value << 8) | (buffer[i] & 0xff)
bitCount += 8
while(bitCount >= 5) {
crockford.push(CROCKFORD_CHARACTERS.charAt((value >>> (bitCount - 5)) & 31))
bitCount -= 5
}
}
if(bitCount > 0) {
crockford.push(CROCKFORD_CHARACTERS.charAt((value << (5 - bitCount)) & 31))
}
return crockford.join('')
}
export async function derivePairingKey(pairingCode: string, salt: Buffer) {
const encoded = new TextEncoder().encode(pairingCode)
const cryptoKey = await crypto.subtle.importKey(
'raw',
encoded,
{
name: 'PBKDF2'
},
!1,
[
'deriveKey'
]
)
return await crypto.subtle.deriveKey({
name: 'PBKDF2',
hash: 'SHA-256',
salt: salt,
iterations: 2 << 16
}, cryptoKey, {
name: 'AES-CTR',
length: 256
}, false, [
'encrypt',
'decrypt'
])
}