Skip to content

Commit

Permalink
Merge pull request #28 from mahnunchik/bech32-limit
Browse files Browse the repository at this point in the history
fix: bech32 prefix length
  • Loading branch information
paulmillr committed Dec 14, 2023
2 parents c5cbf7b + 2832b64 commit eef8051
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,7 @@ function genBech32(encoding: 'bech32' | 'bech32m') {
throw new Error(`bech32.encode prefix should be string, not ${typeof prefix}`);
if (!Array.isArray(words) || (words.length && typeof words[0] !== 'number'))
throw new Error(`bech32.encode words should be array of numbers, not ${typeof words}`);
if (prefix.length === 0) throw new TypeError(`Invalid prefix length ${prefix.length}`);
const actualLength = prefix.length + 7 + words.length;
if (limit !== false && actualLength > limit)
throw new TypeError(`Length ${actualLength} exceeds limit ${limit}`);
Expand Down
18 changes: 18 additions & 0 deletions test/bech32.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,15 @@ const BECH32_INVALID_DECODE = [
];

const BECH32_INVALID_ENCODE = [
{
prefix: '',
words: [],
},
{
prefix:
'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzfoobar',
words: [],
},
{
prefix: 'abc',
words: [128],
Expand Down Expand Up @@ -181,6 +190,15 @@ const BECH32M_INVALID_DECODE = [
];

const BECH32M_INVALID_ENCODE = [
{
prefix: '',
words: [],
},
{
prefix:
'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzfoobar',
words: [],
},
{
prefix: 'abc',
words: [128],
Expand Down

0 comments on commit eef8051

Please sign in to comment.