Prettified

This commit is contained in:
mbhandary
2020-04-03 18:35:34 -04:00
parent 0f7bab354f
commit bca8cb9d4b

View File

@@ -5,7 +5,7 @@ const addressCodec = require("ripple-address-codec");
const keyCodec = require("ripple-keypairs"); const keyCodec = require("ripple-keypairs");
const TIPS = const TIPS =
'<p>Check if the xrp-ledger.toml file is actually hosted in the /.well-known/ location at the domain in your manifest. Check your server\'s HTTPS settings and certificate, and make sure your server provides the required <a href="xrp-ledger-toml.html#cors-setup">CORS header.</a></p>'; '<p>Check if the xrp-ledger.toml file is actually hosted in the /.well-known/ location at the domain in your manifest. Check your server\'s HTTPS settings and certificate, and make sure your server provides the required <a href="xrp-ledger-toml.html#cors-setup">CORS header.</a></p>';
const TOML_PATH = "/.well-known/xrp-ledger.toml"; const TOML_PATH = "/.well-known/xrp-ledger.toml";
const CLASS_GOOD = "badge badge-success"; const CLASS_GOOD = "badge badge-success";
const CLASS_BAD = "badge badge-danger"; const CLASS_BAD = "badge badge-danger";
@@ -14,95 +14,95 @@ var query_param = 0;
//This function makes the lists that output the status. //This function makes the lists that output the status.
function makeLogEntry(text, raw) { function makeLogEntry(text, raw) {
let log; let log;
if (raw) { if (raw) {
log = $("<li></li>") log = $("<li></li>")
.appendTo("#log") .appendTo("#log")
.append(text); .append(text);
} else { } else {
log = $("<li></li>") log = $("<li></li>")
.text(text + " ") .text(text + " ")
.appendTo("#log"); .appendTo("#log");
} }
log.resolve = function(text) { log.resolve = function (text) {
return $("<span></span>") return $("<span></span>")
.html(text) .html(text)
.appendTo(log); .appendTo(log);
}; };
return log; return log;
} }
//3. //3.
//Find the validator entry in the TOML file and verify the signature of the attestation. //Find the validator entry in the TOML file and verify the signature of the attestation.
async function parse_xrpl_toml(data, public_key_hex, public_key, message) { async function parse_xrpl_toml(data, public_key_hex, public_key, message) {
let parsed; let parsed;
let log1 = makeLogEntry("Parsing TOML data..."); let log1 = makeLogEntry("Parsing TOML data...");
try { try {
parsed = TOML(data); parsed = TOML(data);
log1.resolve("SUCCESS").addClass(CLASS_GOOD); log1.resolve("SUCCESS").addClass(CLASS_GOOD);
} catch (e) { } catch (e) {
log1.resolve(e).addClass(CLASS_BAD); log1.resolve(e).addClass(CLASS_BAD);
return; return;
} }
console.log(parsed); console.log(parsed);
let validator_entries = parsed.VALIDATORS; let validator_entries = parsed.VALIDATORS;
if (validator_entries) { if (validator_entries) {
if (!Array.isArray(validator_entries)) { if (!Array.isArray(validator_entries)) {
makeLogEntry("Validators:") makeLogEntry("Validators:")
.resolve("Wrong type - should be table-array") .resolve("Wrong type - should be table-array")
.addClass(CLASS_BAD); .addClass(CLASS_BAD);
} else { } else {
let validator_found = false; let validator_found = false;
for (i = 0; i < validator_entries.length; i++) { for (i = 0; i < validator_entries.length; i++) {
let pk = validator_entries[i]["public_key"]; let pk = validator_entries[i]["public_key"];
if (pk == public_key) { if (pk == public_key) {
validator_found = true; validator_found = true;
try { try {
var attestation = validator_entries[i]["attestation"]; var attestation = validator_entries[i]["attestation"];
} catch { } catch {
makeLogEntry("Attestation Not found").addClass(CLASS_BAD); makeLogEntry("Attestation Not found").addClass(CLASS_BAD);
} }
try { try {
var verify = keyCodec.verify( var verify = keyCodec.verify(
ascii_to_hexa(message), ascii_to_hexa(message),
attestation, attestation,
public_key_hex public_key_hex
); );
} catch (e) { } catch (e) {
makeLogEntry("Domain Verification Failed") makeLogEntry("Domain Verification Failed")
.resolve(e) .resolve(e)
.addClass(CLASS_BAD); .addClass(CLASS_BAD);
} }
if (verify) { if (verify) {
makeLogEntry("Domain Verification Succeeded").addClass(CLASS_GOOD); makeLogEntry("Domain Verification Succeeded").addClass(CLASS_GOOD);
} else { } else {
makeLogEntry("Domain Verification Failed").addClass(CLASS_BAD); makeLogEntry("Domain Verification Failed").addClass(CLASS_BAD);
} }
} }
} }
if (!validator_found) { if (!validator_found) {
makeLogEntry( makeLogEntry(
"The validator key for this manifest was not found in the TOML file" "The validator key for this manifest was not found in the TOML file"
).addClass(CLASS_BAD); ).addClass(CLASS_BAD);
} }
} }
} else { } else {
makeLogEntry("No Validators Found") makeLogEntry("No Validators Found")
.resolve("Failure") .resolve("Failure")
.addClass(CLASS_BAD); .addClass(CLASS_BAD);
} }
} }
function display_manifest(man) { function display_manifest(man) {
for (x in man){ for (x in man) {
log = makeLogEntry(x + ": " +man[x]); log = makeLogEntry(x + ": " + man[x]);
} }
} }
@@ -111,122 +111,118 @@ function display_manifest(man) {
//Use these to create the message that should have been signed by the validator's private key (the attestation). //Use these to create the message that should have been signed by the validator's private key (the attestation).
//Go to the domain and verify the signature of the attestation field in the appropriate validator entry. //Go to the domain and verify the signature of the attestation field in the appropriate validator entry.
function parse_manifest() { function parse_manifest() {
const manhex = $("#manifest").val(); const manhex = $("#manifest").val();
try { try {
var man = codec.decode(manhex); var man = codec.decode(manhex);
} catch (e) { } catch (e) {
makeLogEntry("Error decoding manifest") makeLogEntry("Error decoding manifest")
.resolve(e) .resolve(e)
.addClass(CLASS_BAD); .addClass(CLASS_BAD);
return; return;
} }
let seq = man["Sequence"];
let public_key_hex = man["PublicKey"];
let buff_pub = new Buffer(public_key_hex, "hex").toJSON().data;
let seq = man ["Sequence"]; let public_key = addressCodec.encodeNodePublic(buff_pub);
let public_key_hex = man["PublicKey"]; let ephemeral_public_key_hex = man["SigningPubKey"];
let buff_pub = new Buffer(public_key_hex, "hex").toJSON().data; let buff_eph_pub = new Buffer(ephemeral_public_key_hex, "hex").toJSON().data;
let public_key = addressCodec.encodeNodePublic(buff_pub); let ephemeral_public_key = addressCodec.encodeNodePublic(buff_eph_pub);
let ephemeral_public_key_hex = man["SigningPubKey"]; try {
let buff_eph_pub = new Buffer(ephemeral_public_key_hex, "hex").toJSON().data; var domain = hex_to_ascii(man["Domain"]);
let ephemeral_public_key = addressCodec.encodeNodePublic(buff_eph_pub); } catch {
makeLogEntry("Domain not found in manifest").addClass(CLASS_BAD);
display_manifest({
"Sequence": seq,
"Master Public Key": public_key,
"Ephemeral Public Key": ephemeral_public_key
});
return;
}
display_manifest({
"Sequence": seq,
"Domain": domain,
"Master Public Key": public_key,
"Ephemeral Public Key": ephemeral_public_key
})
try { //This is the message that was signed by the validator's private key.
var domain = hex_to_ascii(man["Domain"]); let message = "[domain-attestation-blob:" + domain + ":" + public_key + "]";
} catch { const url = "https://" + domain + TOML_PATH + "?v=" + query_param++;
makeLogEntry("Domain not found in manifest").addClass(CLASS_BAD); const log = makeLogEntry("Checking " + url + "...");
display_manifest({"Sequence":seq,
"Master Public Key": public_key,
"Ephemeral Public Key":ephemeral_public_key});
return;
}
$.ajax({
url: url,
display_manifest({"Sequence":seq, dataType: "text",
"Domain":domain, success: function (data) {
"Master Public Key": public_key, log.resolve("FOUND").addClass(CLASS_GOOD);
"Ephemeral Public Key":ephemeral_public_key}) parse_xrpl_toml(data, public_key_hex, public_key, message);
},
error: function (jqxhr, status, error) {
switch (status) {
case "timeout":
//This is the message that was signed by the validator's private key. err = "TIMEOUT";
let message = "[domain-attestation-blob:" + domain + ":" + public_key + "]"; break;
const url = "https://" + domain + TOML_PATH + "?v=" + query_param++; case "abort":
const log = makeLogEntry("Checking " + url + "..."); err = "ABORTED";
break;
$.ajax({ case "error":
url: url, err = "ERROR";
dataType: "text", break;
success: function(data) { default:
log.resolve("FOUND").addClass(CLASS_GOOD); err = "UNKNOWN";
parse_xrpl_toml(data, public_key_hex, public_key, message); }
}, log
error: function(jqxhr, status, error) { .resolve(err)
switch (status) { .addClass(CLASS_BAD)
case "timeout": .after(TIPS);
err = "TIMEOUT"; }
break; });
case "abort":
err = "ABORTED";
break;
case "error":
err = "ERROR";
break;
default:
err = "UNKNOWN";
}
log
.resolve(err)
.addClass(CLASS_BAD)
.after(TIPS);
}
});
} }
// Nifty hex/ascii helpers: // Nifty hex/ascii helpers:
//https://www.w3resource.com/javascript-exercises/javascript-string-exercise-28.php //https://www.w3resource.com/javascript-exercises/javascript-string-exercise-28.php
function hex_to_ascii(str1) { function hex_to_ascii(str1) {
var hex = str1.toString(); var hex = str1.toString();
var str = ""; var str = "";
for (var n = 0; n < hex.length; n += 2) { for (var n = 0; n < hex.length; n += 2) {
str += String.fromCharCode(parseInt(hex.substr(n, 2), 16)); str += String.fromCharCode(parseInt(hex.substr(n, 2), 16));
} }
return str; return str;
} }
//https://www.w3resource.com/javascript-exercises/javascript-string-exercise-27.php //https://www.w3resource.com/javascript-exercises/javascript-string-exercise-27.php
function ascii_to_hexa(str) { function ascii_to_hexa(str) {
var arr1 = []; var arr1 = [];
for (var n = 0, l = str.length; n < l; n++) { for (var n = 0, l = str.length; n < l; n++) {
var hex = Number(str.charCodeAt(n)).toString(16); var hex = Number(str.charCodeAt(n)).toString(16);
arr1.push(hex); arr1.push(hex);
} }
return arr1.join(""); return arr1.join("");
} }
function handle_submit(event) { function handle_submit(event) {
event.preventDefault(); event.preventDefault();
$(".result-title").show(); $(".result-title").show();
$("#result").show(); $("#result").show();
$("#log").empty(); $("#log").empty();
parse_manifest(); parse_manifest();
} }
//1. //1.
//Start the verification process when the user enters a manifest. //Start the verification process when the user enters a manifest.
$(document).ready(() => { $(document).ready(() => {
$("#manifest-entry").submit(handle_submit); $("#manifest-entry").submit(handle_submit);
}); });