Handle unl change announcement in js client library. (#225)

This commit is contained in:
Chalith Desaman
2021-01-19 15:29:17 +05:30
committed by GitHub
parent 146bb1d46c
commit eef6448f08
3 changed files with 22 additions and 16 deletions

View File

@@ -378,6 +378,20 @@
return false;
}
const validateAndEmitUnlChange = (changedUnl) => {
// If this is currently a trusted connection, notify unl update.
const trustedKeys = getTrustedKeys();
if (trustedKeys && trustedKeys[pubkey]) {
// Prepare sorted new unl lookup object for equality comparison.
const newUnl = {};
changedUnl.sort().forEach(k => newUnl[k] = true);
// Only emit unl change event if the unl has really changed.
if (JSON.stringify(trustedKeys) != JSON.stringify(newUnl))
emitter && emitter.emit(events.unlChange, changedUnl);
}
}
const handshakeMessageHandler = (m) => {
if (connectionStatus == 0 && m.type == "user_challenge" && m.hp_version && m.contract_id) {
@@ -438,17 +452,7 @@
handshakeResolver && handshakeResolver(true);
console.log(`Connected to ${server}`);
// If this is currently a trusted connection, notify unl update.
const trustedKeys = getTrustedKeys();
if (trustedKeys && trustedKeys[pubkey]) {
// Prepare sorted new unl lookup object for equality comparison.
const newUnl = {};
m.unl.sort().forEach(k => newUnl[k] = true);
// Only emit unl change event if the unl has really changed.
if (JSON.stringify(trustedKeys) != JSON.stringify(newUnl))
emitter && emitter.emit(events.unlChange, m.unl);
}
validateAndEmitUnlChange(m.unl);
return true;
}
@@ -494,9 +498,11 @@
statResponseResolvers = [];
}
else if (m.type == "unl_change") {
// UNL change announcement message is handled in this block.
console.log("Received :", m.type);
console.log(m.unl);
if (m.unl) {
// Convert unl pubkeys to hex string.
let unl = m.unl.map(k => msgHelper.stringifyValue(k));
validateAndEmitUnlChange(unl);
}
}
else {

View File

@@ -139,7 +139,7 @@ namespace msg::usrmsg::bson
* Message format:
* {
* "type": "unl_change",
* "unl": ["<pubkey1>{[1byte(11101101) prefix][32byte]}", ...], // Binary pubkey list of unl nodes.
* "unl": ["<pubkey1>{[1byte(11101101) prefix][32byte]}", ...] // Binary pubkey list of unl nodes.
* }
* @param unl_list The unl node pubkey list to be put in the message.
*/

View File

@@ -319,7 +319,7 @@ namespace msg::usrmsg::json
* Message format:
* {
* "type": "unl_change",
* ["<pubkey1>{[ed prefix][64 characters]}", ...], // Hex pubkey list of unl nodes.
* "unl": ["<pubkey1>{[ed prefix][64 characters]}", ...] // Hex pubkey list of unl nodes.
* }
* @param unl_list The unl node pubkey list to be put in the message.
*/