From b513ffeb57213d2dce62ffe2f186419504de6226 Mon Sep 17 00:00:00 2001 From: AbigailDeng Date: Wed, 22 May 2024 20:21:15 +0800 Subject: [PATCH] feat: getByteCountByAddress --- package.json | 2 +- src/util/formatters.js | 34 +++++++++++++++++----------------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/package.json b/package.json index 5ded1a79..408b44ba 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "aelf-sdk", - "version": "3.4.11-beta.0", + "version": "3.4.12", "description": "aelf-sdk js library", "main": "dist/aelf.cjs.js", "browser": "dist/aelf.umd.js", diff --git a/src/util/formatters.js b/src/util/formatters.js index 2dce4828..d73fce40 100644 --- a/src/util/formatters.js +++ b/src/util/formatters.js @@ -5,31 +5,31 @@ * @date 2015 */ import descriptor from '@aelfqueen/protobufjs/ext/descriptor'; -import { - base58 -} from './utils'; +import bs58 from 'bs58'; +import { base58 } from './utils'; + +const getByteCountByAddress = base58Str => { + // convert a Base58 string to a binary array and get its byte count + const buffer = bs58.decode(base58Str); + // get byte + const byteCount = buffer.length; + // last four digits are the checksum + return byteCount; +}; export const inputAddressFormatter = address => { let realAddress = address; if (address && address.value) { realAddress = address.value; } - if (realAddress.indexOf('_') > 0) { - const parts = realAddress.split('_'); - const list = parts.filter(v => { - try { - base58.decode(v, 'hex'); - return true; - } catch (e) { - return false; - } - }); - if (list.length === 0) { + try { + if (realAddress.indexOf('_') > 0) { + const parts = realAddress.split('_'); + realAddress = parts?.[1]; + } + if (getByteCountByAddress(realAddress) !== 36) { throw new Error('Invalid address'); } - [realAddress] = list; - } - try { base58.decode(realAddress, 'hex'); } catch (e) { throw new Error('Invalid address');