fix: replace base-64 with more robust Buffer
The original implementation was using a combination of atob and the base-64 library. Buffer is a more common and robust solution.
This commit is contained in:
parent
2ed5c9b376
commit
7040e63e41
|
@ -1,6 +1,6 @@
|
||||||
import { NativeModules, Platform, NativeEventEmitter } from 'react-native';
|
import { NativeModules, Platform, NativeEventEmitter } from 'react-native';
|
||||||
import { decode, encode } from 'base-64';
|
|
||||||
import bigInt from 'big-integer';
|
import bigInt from 'big-integer';
|
||||||
|
import { Buffer } from 'buffer';
|
||||||
|
|
||||||
const LINKING_ERROR =
|
const LINKING_ERROR =
|
||||||
`The package '@waku/react-native' doesn't seem to be linked. Make sure: \n\n` +
|
`The package '@waku/react-native' doesn't seem to be linked. Make sure: \n\n` +
|
||||||
|
@ -28,14 +28,13 @@ export class WakuMessage {
|
||||||
timestamp?: Date = undefined;
|
timestamp?: Date = undefined;
|
||||||
|
|
||||||
toJSON() {
|
toJSON() {
|
||||||
const b64encoded = encode(String.fromCharCode(...this.payload));
|
|
||||||
return {
|
return {
|
||||||
contentTopic: this.contentTopic,
|
contentTopic: this.contentTopic,
|
||||||
version: this.version,
|
version: this.version,
|
||||||
timestamp: this.timestamp
|
timestamp: this.timestamp
|
||||||
? bigInt(this.timestamp.valueOf()).multiply(OneMillion).toString(10)
|
? bigInt(this.timestamp.valueOf()).multiply(OneMillion).toString(10)
|
||||||
: 0,
|
: 0,
|
||||||
payload: b64encoded,
|
payload: Buffer.from(this.payload).toString('base64'),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -57,11 +56,7 @@ export function onMessage(cb: (arg0: any) => void) {
|
||||||
: undefined;
|
: undefined;
|
||||||
signal.event.wakuMessage.version = msg.version || 0;
|
signal.event.wakuMessage.version = msg.version || 0;
|
||||||
signal.event.wakuMessage.contentTopic = msg.contentTopic;
|
signal.event.wakuMessage.contentTopic = msg.contentTopic;
|
||||||
signal.event.wakuMessage.payload = new Uint8Array(
|
signal.event.wakuMessage.payload = Buffer.from(msg.payload, 'base64');
|
||||||
decode(msg.payload ?? [])
|
|
||||||
.split('')
|
|
||||||
.map((c: any) => c.charCodeAt(0))
|
|
||||||
);
|
|
||||||
cb(signal.event);
|
cb(signal.event);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -389,11 +384,9 @@ export class DecodedPayload {
|
||||||
signature: String | null = '';
|
signature: String | null = '';
|
||||||
|
|
||||||
toJSON() {
|
toJSON() {
|
||||||
const b64payload = encode(String.fromCharCode(...this.payload));
|
|
||||||
const b64padding = encode(String.fromCharCode(...this.padding));
|
|
||||||
return {
|
return {
|
||||||
payload: b64payload,
|
payload: Buffer.from(this.payload).toString('base64'),
|
||||||
padding: b64padding,
|
padding: Buffer.from(this.padding).toString('base64'),
|
||||||
pubkey: this.pubkey,
|
pubkey: this.pubkey,
|
||||||
signature: this.signature,
|
signature: this.signature,
|
||||||
};
|
};
|
||||||
|
@ -419,16 +412,8 @@ export function decodeSymmetric(
|
||||||
reject(response.error);
|
reject(response.error);
|
||||||
} else {
|
} else {
|
||||||
let decodedPayload = new DecodedPayload();
|
let decodedPayload = new DecodedPayload();
|
||||||
decodedPayload.payload = new Uint8Array(
|
decodedPayload.payload = Buffer.from(response.result.data, 'base64');
|
||||||
atob(response.result.payload)
|
decodedPayload.padding = Buffer.from(response.result.padding, 'base64');
|
||||||
.split('')
|
|
||||||
.map((c) => c.charCodeAt(0))
|
|
||||||
);
|
|
||||||
decodedPayload.padding = new Uint8Array(
|
|
||||||
atob(response.result.padding)
|
|
||||||
.split('')
|
|
||||||
.map((c) => c.charCodeAt(0))
|
|
||||||
);
|
|
||||||
decodedPayload.pubkey = response.result.pubkey;
|
decodedPayload.pubkey = response.result.pubkey;
|
||||||
decodedPayload.signature = response.result.signature;
|
decodedPayload.signature = response.result.signature;
|
||||||
resolve(decodedPayload);
|
resolve(decodedPayload);
|
||||||
|
@ -455,16 +440,8 @@ export function decodeAsymmetric(
|
||||||
reject(response.error);
|
reject(response.error);
|
||||||
} else {
|
} else {
|
||||||
let decodedPayload = new DecodedPayload();
|
let decodedPayload = new DecodedPayload();
|
||||||
decodedPayload.payload = new Uint8Array(
|
decodedPayload.payload = Buffer.from(response.result.data, 'base64');
|
||||||
atob(response.result.payload)
|
decodedPayload.padding = Buffer.from(response.result.padding, 'base64');
|
||||||
.split('')
|
|
||||||
.map((c) => c.charCodeAt(0))
|
|
||||||
);
|
|
||||||
decodedPayload.padding = new Uint8Array(
|
|
||||||
atob(response.result.padding)
|
|
||||||
.split('')
|
|
||||||
.map((c) => c.charCodeAt(0))
|
|
||||||
);
|
|
||||||
decodedPayload.pubkey = response.result.pubkey;
|
decodedPayload.pubkey = response.result.pubkey;
|
||||||
decodedPayload.signature = response.result.signature;
|
decodedPayload.signature = response.result.signature;
|
||||||
resolve(decodedPayload);
|
resolve(decodedPayload);
|
||||||
|
@ -745,9 +722,7 @@ export function storeQuery(
|
||||||
? new Date(bigInt(t).divide(OneMillion).toJSNumber())
|
? new Date(bigInt(t).divide(OneMillion).toJSNumber())
|
||||||
: undefined;
|
: undefined;
|
||||||
response.result.messages[i].payload = new Uint8Array(
|
response.result.messages[i].payload = new Uint8Array(
|
||||||
decode(response.result.messages[i].payload ?? [])
|
Buffer.from(response.result.messages[i].payload ?? [], 'base64')
|
||||||
.split('')
|
|
||||||
.map((c) => c.charCodeAt(0))
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue