diff --git a/README.md b/README.md index 12aeeda4..3713f15a 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ | Statements | Branches | Functions | Lines | | --------------------------- | ----------------------- | ------------------------- | ----------------- | -| ![Statements](https://img.shields.io/badge/statements-97.03%25-brightgreen.svg?style=flat) | ![Branches](https://img.shields.io/badge/branches-94.36%25-brightgreen.svg?style=flat) | ![Functions](https://img.shields.io/badge/functions-97.18%25-brightgreen.svg?style=flat) | ![Lines](https://img.shields.io/badge/lines-97.23%25-brightgreen.svg?style=flat) | +| ![Statements](https://img.shields.io/badge/statements-96.18%25-brightgreen.svg?style=flat) | ![Branches](https://img.shields.io/badge/branches-93.46%25-brightgreen.svg?style=flat) | ![Functions](https://img.shields.io/badge/functions-96.71%25-brightgreen.svg?style=flat) | ![Lines](https://img.shields.io/badge/lines-96.34%25-brightgreen.svg?style=flat) | ## 1. Introduction diff --git a/src/util/formatters.js b/src/util/formatters.js index 2dce4828..cfa9fdbf 100644 --- a/src/util/formatters.js +++ b/src/util/formatters.js @@ -5,31 +5,33 @@ * @date 2015 */ import descriptor from '@aelfqueen/protobufjs/ext/descriptor'; +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'); diff --git a/test/unit/util/formatter.test.js b/test/unit/util/formatter.test.js index 96d6759a..acae0c89 100644 --- a/test/unit/util/formatter.test.js +++ b/test/unit/util/formatter.test.js @@ -26,6 +26,7 @@ describe('test formatter', () => { }) ).toBe('rkws1GibTwWQnLyLvpRtnDQiZYf51tEqQDwpGaou5s4ZQvi1v'); expect(() => inputAddressFormatter('test')).toThrow('Invalid address'); + expect(() => inputAddressFormatter('JxF12TrwUP45BMd0OIl')).toThrow('Invalid address'); }); test('test output file descriptor set formatter', () => { const name = outputFileDescriptorSetFormatter(