Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add address length check #154

Merged
merged 2 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading