diff --git a/src/basex.mjs b/src/basex.mjs index 8f9941afc9ee1c1f80cb48fbfffaac79cab7b3e8..146fadae0210ce033a471d756eb2f9d12d15debd 100644 --- a/src/basex.mjs +++ b/src/basex.mjs @@ -20,7 +20,6 @@ export function basex(ALPHABET) { return { encode: source => encode(source, config), - decodeUnsafe: string => decodeUnsafe(string, config), decode: string => decode(string, config) }; } diff --git a/src/crypto.mjs b/src/crypto.mjs index c565223fbf755998cb2b5c022d8ecd41df0a7bde..993af3b3f8c635eee0e04d9b78b0fe99db12be78 100644 --- a/src/crypto.mjs +++ b/src/crypto.mjs @@ -54,18 +54,20 @@ export function pubKey2shortKey(pubKey) { return `${pubKeyBegin}…${pubKeyEnd}:${checksum}`; } -export function pubKey2checksum(b58pubKey, b58viewDependant = false, checksumWithoutLeadingZero = false) { +export function pubKey2checksum(b58pubKey, b58viewDependant = false, checksumWithLeadingZero = false, doubleSha256 = true) { let binPubKey; if (b58viewDependant) binPubKey = b58.decode(b58pubKey); else binPubKey = b58pubKey2bin(b58pubKey); - const hash = sha256.digest(sha256.digest(binPubKey)); - if (checksumWithoutLeadingZero) { + let hash = sha256.digest(binPubKey); + if (doubleSha256) hash = sha256.digest(hash); + if (!checksumWithLeadingZero) { const shorterHash = sliceInitialsZero(hash); return b58.encode(shorterHash).substr(0, 3); } return b58.encode(hash).substr(0, 3); } -export function sliceInitialsZero(array){ + +export function sliceInitialsZero(array) { let zero = 0 while (array[zero] === 0) zero++; return array.slice(zero); @@ -86,5 +88,9 @@ export function checkKey(pubKeyWithChecksum) { if (pubKey2checksum(b58pubKey, true) === checkSum) return true; if (pubKey2checksum(b58pubKey, false, true) === checkSum) return true; if (pubKey2checksum(b58pubKey, true, true) === checkSum) return true; + if (pubKey2checksum(b58pubKey, false, false, false) === checkSum) return true; + if (pubKey2checksum(b58pubKey, true, false, false) === checkSum) return true; + if (pubKey2checksum(b58pubKey, false, true, false) === checkSum) return true; + if (pubKey2checksum(b58pubKey, true, true, false) === checkSum) return true; throw new Error('Bad checksum'); } diff --git a/src/crypto.test.mjs b/src/crypto.test.mjs index cde24ac5c49a73ad31588d99f57fefbb17329fa6..378e09c2b968523ef0ef0a48c5ed2fd18b01ad6b 100644 --- a/src/crypto.test.mjs +++ b/src/crypto.test.mjs @@ -46,19 +46,19 @@ test('pubKey2checksum 2Bjy : 8pQ', t => t.is(app.pubKey2checksum('2BjyvjoAf5qik7 test('pubKey2checksum ascii 2Bjy : 5vi', t => t.is(app.pubKey2checksum('2BjyvjoAf5qik7R8TKDJAHJugsX23YgJGi2LmBUv2nx', true), '5vi')); test('pubKey2checksum 1111 : 3ud', t => t.is(app.pubKey2checksum('11111111111111111111111111111111'), '3ud')); test('pubKey2checksum "" : 3ud', t => t.is(app.pubKey2checksum(''), '3ud')); -test('pubKey2checksum 1pubKey542 : 1ML', t => t.is(app.pubKey2checksum('1pubKey542'), '1ML')); -test('pubKey2checksum pubKey542 : 1ML', t => t.is(app.pubKey2checksum('pubKey542'), '1ML')); -test('pubKey2checksum ascii 1111111111111111111111111pubKey542 : 1ML', t => t.is(app.pubKey2checksum('1111111111111111111111111pubKey542', true), '1ML')); +test('pubKey2checksum 1pubKey542 : MLT', t => t.is(app.pubKey2checksum('1pubKey542'), 'MLT')); +test('pubKey2checksum pubKey542 : MLT', t => t.is(app.pubKey2checksum('pubKey542'), 'MLT')); +test('pubKey2checksum ascii 1111111111111111111111111pubKey542 : MLT', t => t.is(app.pubKey2checksum('1111111111111111111111111pubKey542', true), 'MLT')); test('pubKey2checksum ascii 1pubKey542 : DSs', t => t.is(app.pubKey2checksum('1pubKey542', true), 'DSs')); test('pubKey2checksum ascii pubKey542 : DEE', t => t.is(app.pubKey2checksum('pubKey542', true), 'DEE')); -test('pubKey2checksum checksumWithoutLeadingZero 1pubKey542 : MLT', t => t.is(app.pubKey2checksum('pubKey542', false, true), 'MLT')); +test('pubKey2checksum checksumWithLeadingZero 1pubKey542 : 1ML', t => t.is(app.pubKey2checksum('pubKey542', false, true), '1ML')); test('checkKey pubKey542:1ML', t => t.true(app.checkKey('pubKey542:1ML'))); test('checkKey pubKey542:MLT', t => t.true(app.checkKey('pubKey542:MLT'))); test('checkKey pubKey542:DEE', t => t.true(app.checkKey('pubKey542:DEE'))); -test('checkKey 11111111111111111111111pubKey49311:14R', t => t.true(app.checkKey('11111111111111111111111pubKey49311:14R'))); test('checkKey 11111111111111111111111pubKey49311:4Ru', t => t.true(app.checkKey('11111111111111111111111pubKey49311:4Ru'))); +test('checkKey 11111111111111111111111pubKey49311:14R', t => t.true(app.checkKey('11111111111111111111111pubKey49311:14R'))); test('checkKey 111pubKey49311:14R', t => t.true(app.checkKey('111pubKey49311:14R'))); test('checkKey 11pubKey49311:14R', t => t.true(app.checkKey('11pubKey49311:14R'))); test('checkKey 1pubKey49311:14R', t => t.true(app.checkKey('1pubKey49311:14R'))); @@ -72,5 +72,16 @@ test('checkKey false pubKey49311:111', t => t.throws(() => app.checkKey('pubKey4 test('checkKey false 0pubKey49311:any', t => t.throws(() => app.checkKey('0pubKey49311:any'))); -test('pubKey2checksum 11111111111111111111111pubKey49311 : 14R', t => t.is(app.pubKey2checksum('11111111111111111111111pubKey49311'), '14R')); -test('pubKey2checksum pubKey49311 : 14R', t => t.is(app.pubKey2checksum('pubKey49311'), '14R')); +test('pubKey2checksum 11111111111111111111111pubKey49311 : 4Ru', t => t.is(app.pubKey2checksum('11111111111111111111111pubKey49311'), '4Ru')); +test('pubKey2checksum pubKey49311 : 4Ru', t => t.is(app.pubKey2checksum('pubKey49311'), '4Ru')); + +test('pubKey2checksum de Nd5...21o:3Q3',t=>t.is(app.pubKey2checksum('Nd5kTAZmFDuKoi1mAZkZERenV6efyYyyLoHMTe721o'), '3Q3')); +test('pubKey2checksum simpleSha de Nd5...21o:3Q3',t=>t.is(app.pubKey2checksum('Nd5kTAZmFDuKoi1mAZkZERenV6efyYyyLoHMTe721o',false,false,false), 'FCd')); +test('checkKey accept simpleSha pub664777:4fv',t=>t.true(app.checkKey('pub664777:4fv'))); +test('checkKey accept simpleSha pub664777:14f',t=>t.true(app.checkKey('pub664777:14f'))); +test('checkKey accept simpleSha pub664777:2u3',t=>t.true(app.checkKey('pub664777:2u3'))); +test('checkKey accept simpleSha pub664777:12u',t=>t.true(app.checkKey('pub664777:12u'))); + +test('pubKey2checksum simpleSha viewDependant Nd5...21o',t=>t.is(app.pubKey2checksum('Nd5kTAZmFDuKoi1mAZkZERenV6efyYyyLoHMTe721o', true, false, false),'7G5')); +test('pubKey2checksum simpleSha checksumWithLeadingZero Nd5...21o',t=>t.is(app.pubKey2checksum('Nd5kTAZmFDuKoi1mAZkZERenV6efyYyyLoHMTe721o', false, true, false),'FCd')); +test('pubKey2checksum simpleSha viewDependant checksumWithLeadingZero Nd5...21o',t=>t.is(app.pubKey2checksum('Nd5kTAZmFDuKoi1mAZkZERenV6efyYyyLoHMTe721o', true, true, false),'7G5'));