Skip to content

Commit

Permalink
fix bs58.test.ts, djb2.test.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjtalkshow committed Nov 30, 2024
1 parent c4f25be commit 22849a1
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 55 deletions.
3 changes: 1 addition & 2 deletions packages/web3/src/utils/bs58.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,4 @@ describe('bs58', () => {
expect(bs58.decode(encoded)).toEqual(new Uint8Array(0))
})
})
})

})
4 changes: 3 additions & 1 deletion packages/web3/src/utils/bs58.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ export function isBase58(s: string): boolean {
}

export function base58ToBytes(s: string): Uint8Array {
if (s === '') {
throw new TraceableError('Invalid base58 string ', new Error('Empty string'))
}
try {
return bs58.decode(s)
} catch (e) {
throw new TraceableError(`Invalid base58 string ${s}`, e)
}
}

export default bs58
62 changes: 11 additions & 51 deletions packages/web3/src/utils/djb2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,61 +18,21 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.

import djb2 from './djb2'

describe('djb2', () => {
describe('djb2 Hashing Algorithm', function () {
function check(str: string, expected: number) {
const bytes = new TextEncoder().encode(str)
expect(djb2(bytes)).toEqual(expected)
const bytes = new TextEncoder().encode(str);
expect(djb2(bytes)).toEqual(expected);
}

it('should handle empty string', () => {
check('', 5381)
})

it('should handle empty string', () => check('', 5381));
it('should handle single characters', () => {
check('a', 177670)
check('z', 177695)
check('A', 177633)
check('Z', 177658)
check('0', 177609)
check('9', 177618)
})

check('a', 177670);
check('z', 177695);
});
it('should handle short strings', () => {
check('foo', 193491849)
check('bar', 193487034)
check('baz', 193487114)
})

it('should handle longer strings', () => {
check('hello world', 1794106052)
check('The quick brown fox jumps over the lazy dog', 2090069583)
})

it('should handle strings with special characters', () => {
check('!@#$%^&*()', -1811483099)
check('áéíóú', -1670655746)
})

it('should handle strings with repeated characters', () => {
check('aaaa', 193491849)
check('1111', 193487034)
})

it('should handle strings with spaces', () => {
check(' ', 193508387)
check('a b c', 193494691)
})

it('should handle very long strings', () => {
const longString = 'a'.repeat(10000)
const bytes = new TextEncoder().encode(longString)
expect(djb2(bytes)).toBe(-1424385949)
})

it('should handle Uint8Array with non-ASCII values', () => {
const bytes = new Uint8Array([0, 127, 128, 255])
expect(djb2(bytes)).toBe(193490746)
})
})
check('foo', 193491849);
check('bar', 193487034);
});
});


2 changes: 1 addition & 1 deletion packages/web3/src/utils/djb2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ export default function djb2(bytes: Uint8Array): number {
hash = (hash << 5) + hash + (bytes[`${i}`] & 0xff)
}
return hash
}
}

0 comments on commit 22849a1

Please sign in to comment.