From 7fb259067065e5aa8913387220ca36161fb6db73 Mon Sep 17 00:00:00 2001 From: "luk.lu" Date: Tue, 16 Nov 2021 09:57:02 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20b582hex=20=E9=87=8C=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E7=9A=84=E8=B0=83=E7=94=A8=E4=BA=86=20bs58check?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.js | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 25fa944..5eafa43 100644 --- a/index.js +++ b/index.js @@ -1210,13 +1210,24 @@ class TICrypto { * @param {*} hex * @return {*} * @memberof TICrypto + * 如果出现非HEX的字符,从这个字符(及其同Byte的另一个字符)起直到末尾,都会被忽略掉,但仍然成功返回一个串。 + * bs58check 和 bs58 可接受string, Buffer, ArrayBuffer, Array (包括空字符串'', 各种内容的数组例如包含 undefined,{...},等等); + * 不可接受 undefined, null, {...}, 等等,会返回 exception */ static hex2b58c(hex) { - return bs58check.encode(Buffer.from(hex, 'hex')) + try { + return bs58check.encode(Buffer.from(hex, 'hex')) + } catch (exception) { + return null + } } static hex2b58(hex) { - return bs58.encode(Buffer.from(hex, 'hex')) + try { + return bs58.encode(Buffer.from(hex, 'hex')) + } catch (exception) { + return null + } } /** @@ -1237,7 +1248,7 @@ class TICrypto { static b582hex(box) { try { - return bs58check.decode(box).toString('hex') + return bs58.decode(box).toString('hex') } catch (exception) { return null }