Skip to content

Commit

Permalink
Merge pull request #155 from AElfProject/feature/addressLength
Browse files Browse the repository at this point in the history
feat: getByteCountByAddress
  • Loading branch information
hzz780 authored May 22, 2024
2 parents ef86409 + b513ffe commit 1ee98f0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
34 changes: 17 additions & 17 deletions src/util/formatters.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit 1ee98f0

Please sign in to comment.