From eef6448f0842ca6a733d231d10b0d2a0ae08e12e Mon Sep 17 00:00:00 2001 From: Chalith Desaman Date: Tue, 19 Jan 2021 15:29:17 +0530 Subject: [PATCH] Handle unl change announcement in js client library. (#225) --- examples/js_client/hp-client-lib.js | 34 +++++++++++++++++------------ src/msg/bson/usrmsg_bson.cpp | 2 +- src/msg/json/usrmsg_json.cpp | 2 +- 3 files changed, 22 insertions(+), 16 deletions(-) diff --git a/examples/js_client/hp-client-lib.js b/examples/js_client/hp-client-lib.js index b60dc5a9..6b3834d1 100644 --- a/examples/js_client/hp-client-lib.js +++ b/examples/js_client/hp-client-lib.js @@ -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 { diff --git a/src/msg/bson/usrmsg_bson.cpp b/src/msg/bson/usrmsg_bson.cpp index f6f10a3c..ffb3ff77 100644 --- a/src/msg/bson/usrmsg_bson.cpp +++ b/src/msg/bson/usrmsg_bson.cpp @@ -139,7 +139,7 @@ namespace msg::usrmsg::bson * Message format: * { * "type": "unl_change", - * "unl": ["{[1byte(11101101) prefix][32byte]}", ...], // Binary pubkey list of unl nodes. + * "unl": ["{[1byte(11101101) prefix][32byte]}", ...] // Binary pubkey list of unl nodes. * } * @param unl_list The unl node pubkey list to be put in the message. */ diff --git a/src/msg/json/usrmsg_json.cpp b/src/msg/json/usrmsg_json.cpp index 9411b523..6137fb66 100644 --- a/src/msg/json/usrmsg_json.cpp +++ b/src/msg/json/usrmsg_json.cpp @@ -319,7 +319,7 @@ namespace msg::usrmsg::json * Message format: * { * "type": "unl_change", - * ["{[ed prefix][64 characters]}", ...], // Hex pubkey list of unl nodes. + * "unl": ["{[ed prefix][64 characters]}", ...] // Hex pubkey list of unl nodes. * } * @param unl_list The unl node pubkey list to be put in the message. */