Change Profile Picture + Change Group Settings + Send Message to Oneself

This commit is contained in:
Adhiraj
2020-07-19 23:26:48 +05:30
parent da73905b5e
commit 7f2468efcf
12 changed files with 130 additions and 54 deletions

View File

@@ -2,12 +2,12 @@ import { WA } from './Constants'
import { proto } from '../../WAMessage/WAMessage'
export default class Encoder {
data: Array<number> = []
data: number[] = []
pushByte(value: number) {
this.data.push(value & 0xff)
}
pushInt(value: number, n: number, littleEndian = false) {
pushInt(value: number, n: number, littleEndian=false) {
for (let i = 0; i < n; i++) {
const curShift = littleEndian ? i : n - 1 - i
this.data.push((value >> (curShift * 8)) & 0xff)
@@ -16,17 +16,17 @@ export default class Encoder {
pushInt20(value: number) {
this.pushBytes([(value >> 16) & 0x0f, (value >> 8) & 0xff, value & 0xff])
}
pushBytes(bytes: Uint8Array | Array<number>) {
this.data.push.apply(this.data, bytes)
pushBytes(bytes: Uint8Array | Buffer | number[]) {
bytes.forEach (b => this.data.push(b))
//this.data.push.apply(this.data, bytes)
}
pushString(str: string) {
const bytes = Buffer.from (str, 'utf-8')
this.pushBytes(bytes)
}
writeByteLength(length: number) {
if (length >= 4294967296) {
throw new Error('string too large to encode: ' + length)
}
if (length >= 4294967296) throw new Error('string too large to encode: ' + length)
if (length >= 1 << 20) {
this.pushByte(WA.Tags.BINARY_32)
this.pushInt(length, 4) // 32 bit integer
@@ -101,19 +101,18 @@ export default class Encoder {
this.pushBytes([WA.Tags.LIST_16, listSize])
}
}
writeChildren(children: string | Array<WA.Node> | Object) {
if (!children) {
return
}
writeChildren(children: string | Array<WA.Node> | Buffer | Object) {
if (!children) return
if (typeof children === 'string') {
this.writeString(children, true)
} else if (Buffer.isBuffer(children)) {
this.writeByteLength (children.length)
this.pushBytes(children)
} else if (Array.isArray(children)) {
this.writeListStart(children.length)
children.forEach((c) => {
if (c) this.writeNode(c)
})
} else if (typeof children === 'object') {
children.forEach(c => c && this.writeNode(c))
} else if (typeof children === 'object') {
const buffer = WA.Message.encode(children as proto.WebMessageInfo).finish()
this.writeByteLength(buffer.length)
this.pushBytes(buffer)

View File

@@ -3,7 +3,7 @@ import Encoder from './Encoder'
import Decoder from './Decoder'
describe('Binary Coding Tests', () => {
const testVectors: [[string, Object]] = [
const testVectors: [string, Object][] = [
[
'f806092f5a0a10f804f80234fc6c0a350a1b39313735323938373131313740732e77686174736170702e6e657410011a143345423030393637354537454433374141424632122b0a292a7069616e6f20726f6f6d2074696d696e6773206172653a2a0a20363a3030414d2d31323a3030414d18b3faa7f3052003f80234fc4c0a410a1b39313735323938373131313740732e77686174736170702e6e657410001a20304643454335333330463634393239433645394132434646443242433845414418bdfaa7f305c00101f80234fc930a350a1b39313735323938373131313740732e77686174736170702e6e657410011a14334542303033433742353339414644303937353312520a50536f727279206672656e2c204920636f756c646e277420756e6465727374616e6420274c69627261272e2054797065202768656c702720746f206b6e6f77207768617420616c6c20492063616e20646f18c1faa7f3052003f80234fc540a410a1b39313735323938373131313740732e77686174736170702e6e657410001a20413132333042384436423041314437393345433241453245413043313638443812090a076c69627261727918c2faa7f305',
[
@@ -62,12 +62,20 @@ describe('Binary Coding Tests', () => {
],
],
],
[
'f8063f2dfafc0831323334353637385027fc0431323334f801f80228fc0701020304050607',
[
'picture',
{jid: '12345678@c.us', id: '1234'},
[['image', null, Buffer.from([1,2,3,4,5,6,7])]]
]
]
]
const encoder = new Encoder()
const decoder = new Decoder()
it('should decode strings', () => {
testVectors.forEach((pair) => {
testVectors.forEach(pair => {
const buff = Buffer.from(pair[0], 'hex')
const decoded = decoder.read(buff)