properly handle the case where the (flag-derived) key is invalid (e.g., namely, unpopulated field)

this practically rewrites the JavaScript code in the index.html file

now the tool works without manual intervention
main
SI 2023-11-22 20:10:27 +01:00
parent d2a8c1eba8
commit c186f2b318
1 changed files with 29 additions and 32 deletions

View File

@ -21,46 +21,43 @@
var inpFlag = document.querySelector("#inp-flag"); var inpFlag = document.querySelector("#inp-flag");
var outpObscuredFlag = document.querySelector("#outp-obscured-flag"); var outpObscuredFlag = document.querySelector("#outp-obscured-flag");
var outpSignature = document.querySelector("#outp-signature"); var outpSignature = document.querySelector("#outp-signature");
var secp256k1n = new BN('115792089237316195423570985008687907852837564279074904382605163141518161494337', 10);
function displayObscuredFlag() { function getPrivateKey() {
var k = inpFlag.value; var s = inpFlag.value;
var t = "0x"; var k = new BN(0);
for (i in k) { var h = "0x";
t += k.charCodeAt(i).toString(16).padStart(2, "0"); for (i in s) {
var c = s.charCodeAt(i);
k = k.muln(16).addn(c);
h += c.toString(16).padStart(2, "0");
} }
try { if (k.mod(secp256k1n).eqn(0)) {
outpObscuredFlag.value = addressFromKey(t); return null;
} catch { }
return h;
}
function updateOutputValues(ev) {
var k = getPrivateKey();
if (k === null) {
outpObscuredFlag.value = ""; outpObscuredFlag.value = "";
}
}
function displaySignature() {
var m = inpContestantAddress.value;
var k = inpFlag.value;
var t = "0x";
for (i in k) {
t += k.charCodeAt(i).toString(16).padStart(2, "0");
}
try {
outpSignature.value = signMessage(m, t);
} catch {
outpSignature.value = ""; outpSignature.value = "";
} else {
outpObscuredFlag.value = addressFromKey(k);
var m = inpContestantAddress.value;
if (m.slice(0, 2) !== "0x") {
outpSignature.value = "";
} else {
outpSignature.value = signMessage(m, k);
}
} }
} }
function onContestantAddressChange(ev) { inpContestantAddress.oninput = updateOutputValues;
displaySignature(); inpFlag.oninput = updateOutputValues;
}
function onFlagChange(ev) {
displayObscuredFlag();
displaySignature();
}
inpContestantAddress.oninput = onContestantAddressChange; updateOutputValues();
inpFlag.oninput = onFlagChange;
displayObscuredFlag();
displaySignature();
</script> </script>
</body> </body>