js cleanups and error msgs improvments

This commit is contained in:
moneroexamples 2018-01-28 08:19:23 +08:00
parent 5473bf25ee
commit 6c811626cb
2 changed files with 28 additions and 33 deletions

View file

@ -291,7 +291,7 @@ var cnUtil = (function(initConfig) {
this.sc_reduce = function(hex) { this.sc_reduce = function(hex) {
var input = hextobin(hex); var input = hextobin(hex);
if (input.length !== 64) { if (input.length !== 64) {
throw "Invalid input length"; throw "sc_reduce: Invalid hextobin(hex) input length";
} }
var mem = Module._malloc(64); var mem = Module._malloc(64);
Module.HEAPU8.set(input, mem); Module.HEAPU8.set(input, mem);
@ -304,7 +304,7 @@ var cnUtil = (function(initConfig) {
this.sc_reduce32 = function(hex) { this.sc_reduce32 = function(hex) {
var input = hextobin(hex); var input = hextobin(hex);
if (input.length !== 32) { if (input.length !== 32) {
throw "Invalid input length"; throw "sc_reduce32: Invalid hextobin(hex) input length";
} }
var mem = Module._malloc(32); var mem = Module._malloc(32);
Module.HEAPU8.set(input, mem); Module.HEAPU8.set(input, mem);
@ -319,7 +319,7 @@ var cnUtil = (function(initConfig) {
inlen = Math.floor(input.length / 2); inlen = Math.floor(input.length / 2);
}*/ }*/
if (input.length % 2 !== 0 || !this.valid_hex(input)) { if (input.length % 2 !== 0 || !this.valid_hex(input)) {
throw "Input invalid"; throw "cn_fast_hash: Input invalid";
} }
//update to use new keccak impl (approx 45x faster) //update to use new keccak impl (approx 45x faster)
//var state = this.keccak(input, inlen, HASH_STATE_BYTES); //var state = this.keccak(input, inlen, HASH_STATE_BYTES);
@ -349,7 +349,7 @@ var cnUtil = (function(initConfig) {
this.sec_key_to_pub = function(sec) { this.sec_key_to_pub = function(sec) {
if (sec.length !== 64) { if (sec.length !== 64) {
throw "Invalid sec length"; throw "sec_key_to_pub: Invalid sec length";
} }
return bintohex(nacl.ll.ge_scalarmult_base(hextobin(sec))); return bintohex(nacl.ll.ge_scalarmult_base(hextobin(sec)));
}; };
@ -388,7 +388,7 @@ var cnUtil = (function(initConfig) {
};*/ };*/
this.ge_scalarmult = function(pub, sec) { this.ge_scalarmult = function(pub, sec) {
if (pub.length !== 64 || sec.length !== 64) { if (pub.length !== 64 || sec.length !== 64) {
throw "Invalid input length"; throw "ge_scalarmult: Invalid pub or sec input lengths";
} }
return bintohex(nacl.ll.ge_scalarmult(hextobin(pub), hextobin(sec))); return bintohex(nacl.ll.ge_scalarmult(hextobin(pub), hextobin(sec)));
}; };
@ -413,7 +413,7 @@ var cnUtil = (function(initConfig) {
this.decrypt_payment_id = function(payment_id8, tx_public_key, acc_prv_view_key) { this.decrypt_payment_id = function(payment_id8, tx_public_key, acc_prv_view_key) {
if (payment_id8.length !== 16) throw "Invalid input length!"; if (payment_id8.length !== 16) throw "decrypt_payment_id: Invalid payment_id8 length!";
var key_derivation = this.generate_key_derivation(tx_public_key, acc_prv_view_key); var key_derivation = this.generate_key_derivation(tx_public_key, acc_prv_view_key);
@ -438,7 +438,7 @@ var cnUtil = (function(initConfig) {
// Generate keypair from seed 2 // Generate keypair from seed 2
// as in simplewallet // as in simplewallet
this.generate_keys = function(seed) { this.generate_keys = function(seed) {
if (seed.length !== 64) throw "Invalid input length!"; if (seed.length !== 64) throw "generate_keys: Invalid seed input length!";
var sec = this.sc_reduce32(seed); var sec = this.sc_reduce32(seed);
var pub = this.sec_key_to_pub(sec); var pub = this.sec_key_to_pub(sec);
return { return {
@ -605,7 +605,7 @@ var cnUtil = (function(initConfig) {
this.generate_key_derivation = function(pub, sec) { this.generate_key_derivation = function(pub, sec) {
if (pub.length !== 64 || sec.length !== 64) { if (pub.length !== 64 || sec.length !== 64) {
throw "Invalid input length"; throw "generate_key_derivation: Invalid pub or sec keys lengths";
} }
var P = this.ge_scalarmult(pub, sec); var P = this.ge_scalarmult(pub, sec);
return this.ge_scalarmult(P, d2s(8)); //mul8 to ensure group return this.ge_scalarmult(P, d2s(8)); //mul8 to ensure group
@ -614,7 +614,7 @@ var cnUtil = (function(initConfig) {
this.derivation_to_scalar = function(derivation, output_index) { this.derivation_to_scalar = function(derivation, output_index) {
var buf = ""; var buf = "";
if (derivation.length !== (STRUCT_SIZES.EC_POINT * 2)) { if (derivation.length !== (STRUCT_SIZES.EC_POINT * 2)) {
throw "Invalid derivation length!"; throw "derivation_to_scalar: Invalid derivation length!";
} }
buf += derivation; buf += derivation;
var enc = encode_varint(output_index); var enc = encode_varint(output_index);
@ -627,7 +627,7 @@ var cnUtil = (function(initConfig) {
this.derive_secret_key = function(derivation, out_index, sec) { this.derive_secret_key = function(derivation, out_index, sec) {
if (derivation.length !== 64 || sec.length !== 64) { if (derivation.length !== 64 || sec.length !== 64) {
throw "Invalid input length!"; throw "derive_secret_key: Invalid derivation or sec input length!";
} }
var scalar_m = Module._malloc(STRUCT_SIZES.EC_SCALAR); var scalar_m = Module._malloc(STRUCT_SIZES.EC_SCALAR);
var scalar_b = hextobin(this.derivation_to_scalar(derivation, out_index)); var scalar_b = hextobin(this.derivation_to_scalar(derivation, out_index));
@ -685,7 +685,7 @@ var cnUtil = (function(initConfig) {
this.derive_public_key = function(derivation, out_index, pub) { this.derive_public_key = function(derivation, out_index, pub) {
if (derivation.length !== 64 || pub.length !== 64) { if (derivation.length !== 64 || pub.length !== 64) {
throw "Invalid input length!"; throw "derive_public_key: Invalid derivation or pub key input lengths!";
} }
var s = this.derivation_to_scalar(derivation, out_index); var s = this.derivation_to_scalar(derivation, out_index);
return bintohex(nacl.ll.ge_add(hextobin(pub), hextobin(this.ge_scalarmult_base(s)))); return bintohex(nacl.ll.ge_add(hextobin(pub), hextobin(this.ge_scalarmult_base(s))));
@ -693,7 +693,7 @@ var cnUtil = (function(initConfig) {
this.hash_to_ec = function(key) { this.hash_to_ec = function(key) {
if (key.length !== (KEY_SIZE * 2)) { if (key.length !== (KEY_SIZE * 2)) {
throw "Invalid input length"; throw "hash_to_ec: Invalid key input length";
} }
var h_m = Module._malloc(HASH_SIZE); var h_m = Module._malloc(HASH_SIZE);
var point_m = Module._malloc(STRUCT_SIZES.GE_P2); var point_m = Module._malloc(STRUCT_SIZES.GE_P2);
@ -715,7 +715,7 @@ var cnUtil = (function(initConfig) {
//returns a 32 byte point via "ge_p3_tobytes" rather than a 160 byte "p3", otherwise same as above; //returns a 32 byte point via "ge_p3_tobytes" rather than a 160 byte "p3", otherwise same as above;
this.hash_to_ec_2 = function(key) { this.hash_to_ec_2 = function(key) {
if (key.length !== (KEY_SIZE * 2)) { if (key.length !== (KEY_SIZE * 2)) {
throw "Invalid input length"; throw "hash_to_ec_2: Invalid key input length";
} }
var h_m = Module._malloc(HASH_SIZE); var h_m = Module._malloc(HASH_SIZE);
var point_m = Module._malloc(STRUCT_SIZES.GE_P2); var point_m = Module._malloc(STRUCT_SIZES.GE_P2);
@ -739,7 +739,7 @@ var cnUtil = (function(initConfig) {
this.generate_key_image_2 = function(pub, sec) { this.generate_key_image_2 = function(pub, sec) {
if (!pub || !sec || pub.length !== 64 || sec.length !== 64) { if (!pub || !sec || pub.length !== 64 || sec.length !== 64) {
throw "Invalid input length"; throw "generate_key_image_2: Invalid pub or sec keys input length";
} }
var pub_m = Module._malloc(KEY_SIZE); var pub_m = Module._malloc(KEY_SIZE);
var sec_m = Module._malloc(KEY_SIZE); var sec_m = Module._malloc(KEY_SIZE);
@ -863,7 +863,7 @@ var cnUtil = (function(initConfig) {
this.ge_add = function(p1, p2) { this.ge_add = function(p1, p2) {
if (p1.length !== 64 || p2.length !== 64) { if (p1.length !== 64 || p2.length !== 64) {
throw "Invalid input length!"; throw "ge_add: Invalid p1 or p2 input lengths!";
} }
return bintohex(nacl.ll.ge_add(hextobin(p1), hextobin(p2))); return bintohex(nacl.ll.ge_add(hextobin(p1), hextobin(p2)));
}; };
@ -877,7 +877,7 @@ var cnUtil = (function(initConfig) {
//adds two scalars together //adds two scalars together
this.sc_add = function(scalar1, scalar2) { this.sc_add = function(scalar1, scalar2) {
if (scalar1.length !== 64 || scalar2.length !== 64) { if (scalar1.length !== 64 || scalar2.length !== 64) {
throw "Invalid input length!"; throw "sc_add: Invalid scalar1 or scalar2 input length!";
} }
var scalar1_m = Module._malloc(STRUCT_SIZES.EC_SCALAR); var scalar1_m = Module._malloc(STRUCT_SIZES.EC_SCALAR);
var scalar2_m = Module._malloc(STRUCT_SIZES.EC_SCALAR); var scalar2_m = Module._malloc(STRUCT_SIZES.EC_SCALAR);
@ -895,7 +895,7 @@ var cnUtil = (function(initConfig) {
//subtracts one scalar from another //subtracts one scalar from another
this.sc_sub = function(scalar1, scalar2) { this.sc_sub = function(scalar1, scalar2) {
if (scalar1.length !== 64 || scalar2.length !== 64) { if (scalar1.length !== 64 || scalar2.length !== 64) {
throw "Invalid input length!"; throw "sc_sub: Invalid scalar1 or scalar2 input lengths!";
} }
var scalar1_m = Module._malloc(STRUCT_SIZES.EC_SCALAR); var scalar1_m = Module._malloc(STRUCT_SIZES.EC_SCALAR);
var scalar2_m = Module._malloc(STRUCT_SIZES.EC_SCALAR); var scalar2_m = Module._malloc(STRUCT_SIZES.EC_SCALAR);
@ -913,7 +913,7 @@ var cnUtil = (function(initConfig) {
//fun mul function //fun mul function
this.sc_mul = function(scalar1, scalar2) { this.sc_mul = function(scalar1, scalar2) {
if (scalar1.length !== 64 || scalar2.length !== 64) { if (scalar1.length !== 64 || scalar2.length !== 64) {
throw "Invalid input length!"; throw "sc_mul: Invalid scalar1 or scalar2 input lengths!";
} }
return d2s(JSBigInt(s2d(scalar1)).multiply(JSBigInt(s2d(scalar2))).remainder(l).toString()); return d2s(JSBigInt(s2d(scalar1)).multiply(JSBigInt(s2d(scalar2))).remainder(l).toString());
}; };
@ -971,7 +971,7 @@ var cnUtil = (function(initConfig) {
this.ge_double_scalarmult_base_vartime = function(c, P, r) { this.ge_double_scalarmult_base_vartime = function(c, P, r) {
if (c.length !== 64 || P.length !== 64 || r.length !== 64) { if (c.length !== 64 || P.length !== 64 || r.length !== 64) {
throw "Invalid input length!"; throw "ge_double_scalarmult_base_vartime: Invalid c, P or r input lengths!";
} }
return bintohex(nacl.ll.ge_double_scalarmult_base_vartime(hextobin(c), hextobin(P), hextobin(r))); return bintohex(nacl.ll.ge_double_scalarmult_base_vartime(hextobin(c), hextobin(P), hextobin(r)));
}; };
@ -1012,7 +1012,7 @@ var cnUtil = (function(initConfig) {
this.ge_double_scalarmult_postcomp_vartime = function(r, P, c, I) { this.ge_double_scalarmult_postcomp_vartime = function(r, P, c, I) {
if (c.length !== 64 || P.length !== 64 || r.length !== 64 || I.length !== 64) { if (c.length !== 64 || P.length !== 64 || r.length !== 64 || I.length !== 64) {
throw "Invalid input length!"; throw "ge_double_scalarmult_postcomp_vartime: Invalid r, p, c or I input lengths!";
} }
var Pb = this.hash_to_ec_2(P); var Pb = this.hash_to_ec_2(P);
return bintohex(nacl.ll.ge_double_scalarmult_postcomp_vartime(hextobin(r), hextobin(Pb), hextobin(c), hextobin(I))); return bintohex(nacl.ll.ge_double_scalarmult_postcomp_vartime(hextobin(r), hextobin(Pb), hextobin(c), hextobin(I)));

View file

@ -173,6 +173,8 @@
var tx_public_key = $("#tx_pub_key").text(); var tx_public_key = $("#tx_pub_key").text();
// when we process multi-ouput tx, it can have extra public keys
// due to sub-addresses
var add_tx_pub_keys = $("#add_tx_pub_keys").text().split(';').slice(0, -1); var add_tx_pub_keys = $("#add_tx_pub_keys").text().split(';').slice(0, -1);
console.log("add_tx_pub_keys: ", add_tx_pub_keys); console.log("add_tx_pub_keys: ", add_tx_pub_keys);
@ -190,6 +192,8 @@
return; return;
} }
// not used when decoding, but used when proving.
// so we just use array here
multiple_tx_secret_keys = []; multiple_tx_secret_keys = [];
try { try {
@ -199,7 +203,7 @@
add_tx_pub_keys, multiple_tx_secret_keys, false); add_tx_pub_keys, multiple_tx_secret_keys, false);
} catch(err){ } catch(err){
console.log(err); console.log(err);
$("#decode-prove-results").html('<h4>Error:' + err.message + '</h4>' ); $("#decode-prove-results").html('<h4>Error: ' + err + '</h4>' );
} }
}); });
@ -216,7 +220,7 @@
// when using subaddress, there can be more than one tx_prv_key // when using subaddress, there can be more than one tx_prv_key
var multiple_tx_prv_keys = parse_str_secret_key(tx_prv_key); var multiple_tx_prv_keys = parse_str_secret_key(tx_prv_key);
console.log("multiple_tx_prv_keys: ", multiple_tx_prv_keys); //console.log("multiple_tx_prv_keys: ", multiple_tx_prv_keys);
try { try {
@ -226,13 +230,13 @@
add_tx_pub_keys, multiple_tx_prv_keys, true); add_tx_pub_keys, multiple_tx_prv_keys, true);
} catch(err){ } catch(err){
console.log(err); console.log(err);
$("#decode-prove-results").html('<h4>Error:' + err.message + '</h4>' ); $("#decode-prove-results").html('<h4>Error: ' + err + '</h4>' );
} }
}); });
}); });
// based on C++ by stoffu // based on C++ code by stoffu
function parse_str_secret_key(key_str) { function parse_str_secret_key(key_str) {
var multiple_tx_secret_keys = []; var multiple_tx_secret_keys = [];
@ -303,17 +307,8 @@
var output_pub_key = output.target.key; var output_pub_key = output.target.key;
var amount = output.amount; var amount = output.amount;
// var pubkey_generated = (add_tx_pub_keys
// ? derive_public_key(add_key_derivation[output_idx],
// output_idx, address_pub_key)
// : derive_public_key(key_derivation, output_idx, address_pub_key));
var pubkey_generated = derive_public_key(key_derivation, output_idx, address_pub_key); var pubkey_generated = derive_public_key(key_derivation, output_idx, address_pub_key);
//console.log(pubkey_generated);
var mine_output = (output_pub_key == pubkey_generated); var mine_output = (output_pub_key == pubkey_generated);
var with_additional = false; var with_additional = false;