From 87b38c4515785960bbbdc6af0cfc30002dbfae9e Mon Sep 17 00:00:00 2001
From: "[1000i100] Millicent Billette" <git@1000i100.fr>
Date: Sun, 20 Nov 2022 19:38:48 +0100
Subject: [PATCH] v3.4.2 FIX: checkKey throw too_short error on <43 base58 key
 and no more fill them up to 43 characters

---
 CHANGELOG.fr.md     | 7 ++++++-
 npm/package.json    | 2 +-
 src/crypto.mjs      | 2 +-
 src/crypto.test.mjs | 4 ++++
 4 files changed, 12 insertions(+), 3 deletions(-)

diff --git a/CHANGELOG.fr.md b/CHANGELOG.fr.md
index 448826a..8c18354 100644
--- a/CHANGELOG.fr.md
+++ b/CHANGELOG.fr.md
@@ -13,6 +13,10 @@ et ce projet adhère au [versionnage sémantique](https://semver.org/spec/v2.0.0
 
 ## [Non-publié/Non-Stabilisé] (par [1000i100])
 
+## [Version 3.4.2] - 2022-11-20 (par [1000i100])
+### Corrections
+- checkKey ne complète plus automatiquement les clef trop courtes et envoi donc l'erreur attendue pour les clefs trops courtes.
+
 ## [Version 3.4.1] - 2022-11-20 (par [1000i100])
 ### Corrections
 - checkKey envoi désormais des erreurs nommées, utilisable pour guider les usagers.
@@ -105,8 +109,9 @@ et ce projet adhère au [versionnage sémantique](https://semver.org/spec/v2.0.0
 - intégration des librairies de crypto nécessaires
 - calcul de la clef publique correspondant à chaque combinaison de secrets saisie, et comparaison à la clef publique de référence.
 
-[Non-publié/Non-Stabilisé]: https://git.duniter.org/libs/g1lib.js/-/compare/v3.4.1...main
+[Non-publié/Non-Stabilisé]: https://git.duniter.org/libs/g1lib.js/-/compare/v3.4.2...main
 
+[Version 3.4.2]: https://git.duniter.org/libs/g1lib.js/-/compare/v3.4.1...v3.4.2
 [Version 3.4.1]: https://git.duniter.org/libs/g1lib.js/-/compare/v3.4.0...v3.4.1
 [Version 3.4.0]: https://git.duniter.org/libs/g1lib.js/-/compare/v3.3.3...v3.4.0
 [Version 3.3.3]: https://git.duniter.org/libs/g1lib.js/-/compare/v3.3.2...v3.3.3
diff --git a/npm/package.json b/npm/package.json
index 2770980..4b0c127 100644
--- a/npm/package.json
+++ b/npm/package.json
@@ -1,6 +1,6 @@
 {
   "name": "g1lib",
-  "version": "3.4.1",
+  "version": "3.4.2",
   "description": "An ubiquitous static javascript toolbox lib for Ǧ1 / Duniter ecosystem with reliability in mind.",
   "main": "nodejs/all.mjs",
 	"browser": "browser/all.mjs",
diff --git a/src/crypto.mjs b/src/crypto.mjs
index 36d66f6..428ced0 100644
--- a/src/crypto.mjs
+++ b/src/crypto.mjs
@@ -153,7 +153,7 @@ export function checkKey(pubKey, checkRawPubKey= true) {
 	let b58pubKey;
 	try {
 		const binPubKey = pubKey2bin(pubKey)
-		b58pubKey = b58.encode(binPubKey);
+		b58pubKey = typeof pubKey === 'string' ? pubKey : b58.encode(binPubKey);
 	} catch (err){
 		if(err.message.match(/base58/)) throw new CustomError('not_b58', 'Character out of base 58, see rfc/0009_Duniter_Blockchain_Protocol_V11.md#public-key for details.');
 		if(err.message.match(/out of bounds/)) throw new CustomError('too_long','Binary key too long, see rfc/0009_Duniter_Blockchain_Protocol_V11.md#public-key for details.');
diff --git a/src/crypto.test.mjs b/src/crypto.test.mjs
index 6c07e9b..7453216 100644
--- a/src/crypto.test.mjs
+++ b/src/crypto.test.mjs
@@ -130,7 +130,11 @@ test("isEd25519PubKey fail if point is not on ed25519", (t) => t.false(app.isEd2
 test('checkKey accept valid pubKey with no checksum', t => t.true(app.checkKey(pubKey)));
 test('checkKey throw if empty pubkey is given', t => t.throws(() => app.checkKey(''),{name:'empty'}));
 test('checkKey throw if under_sized string is given', t => t.throws(() => app.checkKey('test'),{name:'too_short'}));
+test('checkKey throw too_short if 41 characters long string is given', t => t.throws(() => app.checkKey('fffffffffffffffffffffffffffffffffffffffff'),{name:'too_short'}));
+test('checkKey throw too_short if 42 characters long string is given', t => t.throws(() => app.checkKey('ffffffffffffffffffffffffffffffffffffffffff'),{name:'too_short'}));
+test('checkKey throw too_long if given string is 44 characters but more than 32bits', t => t.throws(() => app.checkKey('ffffffffffffffffffffffffffffffffffffffffffff'),{name:'too_long'}));
 test('checkKey throw if over_sized string is given', t => t.throws(() => app.checkKey(pubKey+pubKey),{name:'too_long'}));
+test('checkKey throw too_long if 45 characters long string is given', t => t.throws(() => app.checkKey('fffffffffffffffffffffffffffffffffffffffffffff'),{name:'too_long'}));
 test('checkKey throw if invalid pubkey is given (not on ed25519 curve)', t => t.throws(() => app.checkKey(pubKey.replace(/6/,'9')),{name:'bad_ed25519_point'}));
 test('checkKey throw if checksum is incorrect', t => t.throws(() => app.checkKey(`${pubKey}:111`),{name:'bad_checksum'}));
 test('checkKey throw if not b58 string is given', t => t.throws(() => app.checkKey(`___`),{name:'not_b58'}));
-- 
GitLab