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

fix: add base58 and fix bech32 for is btc address #1548

Merged
merged 1 commit into from
Dec 9, 2020
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
9 changes: 7 additions & 2 deletions src/lib/isBtcAddress.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import assertString from './util/assertString';

// supports Bech32 addresses
const btc = /^(bc1|[13])[a-zA-HJ-NP-Z0-9]{25,39}$/;
const bech32 = /^(bc1)[a-z0-9]{25,39}$/;
const base58 = /^(1|3)[A-HJ-NP-Za-km-z1-9]{25,39}$/;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have isBase58 validator, how about re-using it here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I only have an issue with the reuse since:

  • base58 will only be needed on A-HJ-NP-Za-km-z1-9 hence we will need to add (1|3) in front of it and check that base58 has 25-39 chars.
    Based on the above, I realized it will be hard to read and maintain the code

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, makes sense. I'm good then.


export default function isBtcAddress(str) {
assertString(str);
return btc.test(str);
// check for bech32
if (str.startsWith('bc1')) {
return bech32.test(str);
}
return base58.test(str);
}
6 changes: 6 additions & 0 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -8453,11 +8453,17 @@ describe('Validators', () => {
'1MUz4VMYui5qY1mxUiG8BQ1Luv6tqkvaiL',
'3J98t1WpEZ73CNmQviecrnyiWrnqRhWNLy',
'bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq',
'14qViLJfdGaP4EeHnDyJbEGQysnCpwk3gd',
'35bSzXvRKLpHsHMrzb82f617cV4Srnt7hS',
'17VZNX1SN5NtKa8UQFxwQbFeFc3iqRYhemt',
'bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4',
],
invalid: [
'4J98t1WpEZ73CNmQviecrnyiWrnqRhWNLy',
'0x56F0B8A998425c53c75C4A303D4eF987533c5597',
'pp8skudq3x5hzw8ew7vzsw8tn4k8wxsqsv0lt0mf3g',
'17VZNX1SN5NlKa8UQFxwQbFeFc3iqRYhem',
'BC1QW508D6QEJXTDG4Y5R3ZARVAYR0C5XW7KV8F3T4',
],
});
});
Expand Down
15 changes: 12 additions & 3 deletions validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -4000,10 +4000,19 @@ function isCurrency(str, options) {
return currencyRegex(options).test(str);
}

var btc = /^(bc1|[13])[a-zA-HJ-NP-Z0-9]{25,39}$/;
var bech32 = /^(bc1)[a-z0-9]{25,39}$/;
var base58 = /^(1|3)[A-HJ-NP-Za-km-z1-9]{25,39}$/;
function isBtcAddress(str) {
assertString(str);
return btc.test(str);
assertString(str); // check for bech32

if (str.startsWith('bc1')) {
console.log({
str: str
});
return bech32.test(str);
}

return base58.test(str);
}

/* eslint-disable max-len */
Expand Down
2 changes: 1 addition & 1 deletion validator.min.js

Large diffs are not rendered by default.