Skip to content

Commit

Permalink
Merge pull request #154 from AElfProject/feature/test-chain-util-addr…
Browse files Browse the repository at this point in the history
…essLength

feat: add address length check
  • Loading branch information
hzz780 authored May 22, 2024
2 parents ec96a9f + 097da78 commit 68d67e3
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 16 additions & 14 deletions src/util/formatters.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
1 change: 1 addition & 0 deletions test/unit/util/formatter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 68d67e3

Please sign in to comment.