Skip to content

Commit 3e66a4a

Browse files
committed
add base58 for is btc address
1 parent 302d295 commit 3e66a4a

File tree

4 files changed

+27
-6
lines changed

4 files changed

+27
-6
lines changed

src/lib/isBtcAddress.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
import assertString from './util/assertString';
22

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

67
export default function isBtcAddress(str) {
78
assertString(str);
8-
return btc.test(str);
9+
// check for bech32
10+
if (str.startsWith('bc1')) {
11+
console.log({ str });
12+
return bech32.test(str);
13+
}
14+
return base58.test(str);
915
}

test/validators.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8453,11 +8453,17 @@ describe('Validators', () => {
84538453
'1MUz4VMYui5qY1mxUiG8BQ1Luv6tqkvaiL',
84548454
'3J98t1WpEZ73CNmQviecrnyiWrnqRhWNLy',
84558455
'bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq',
8456+
'14qViLJfdGaP4EeHnDyJbEGQysnCpwk3gd',
8457+
'35bSzXvRKLpHsHMrzb82f617cV4Srnt7hS',
8458+
'17VZNX1SN5NtKa8UQFxwQbFeFc3iqRYhemt',
8459+
'bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4',
84568460
],
84578461
invalid: [
84588462
'4J98t1WpEZ73CNmQviecrnyiWrnqRhWNLy',
84598463
'0x56F0B8A998425c53c75C4A303D4eF987533c5597',
84608464
'pp8skudq3x5hzw8ew7vzsw8tn4k8wxsqsv0lt0mf3g',
8465+
'17VZNX1SN5NlKa8UQFxwQbFeFc3iqRYhem',
8466+
'BC1QW508D6QEJXTDG4Y5R3ZARVAYR0C5XW7KV8F3T4',
84618467
],
84628468
});
84638469
});

validator.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4000,10 +4000,19 @@ function isCurrency(str, options) {
40004000
return currencyRegex(options).test(str);
40014001
}
40024002

4003-
var btc = /^(bc1|[13])[a-zA-HJ-NP-Z0-9]{25,39}$/;
4003+
var bech32 = /^(bc1)[a-z0-9]{25,39}$/;
4004+
var base58 = /^(1|3)[A-HJ-NP-Za-km-z1-9]{25,39}$/;
40044005
function isBtcAddress(str) {
4005-
assertString(str);
4006-
return btc.test(str);
4006+
assertString(str); // check for bech32
4007+
4008+
if (str.startsWith('bc1')) {
4009+
console.log({
4010+
str: str
4011+
});
4012+
return bech32.test(str);
4013+
}
4014+
4015+
return base58.test(str);
40074016
}
40084017

40094018
/* eslint-disable max-len */

validator.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)