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

Throw when trying to encode bad hex values to Buffer #115

Open
gmaclennan opened this issue Aug 15, 2023 · 0 comments
Open

Throw when trying to encode bad hex values to Buffer #115

gmaclennan opened this issue Aug 15, 2023 · 0 comments

Comments

@gmaclennan
Copy link
Member

Currently if you try to pass an invalid hex value as an ID, e.g. if you pass fieldIds: [ 'shhh' ] then encode and decode will work without error, but you will get back an empty string: fieldIds: [ '' ]. This is because Buffer.from('shhh', 'hex') returns an empty Buffer.

I think it makes sense to throw if any of these id fields is invalid. We should do:

function idBufferFromHex(idString) {
  const buf = Buffer.from(idString, hex)
  if (buf.length !== 32) throw new Error('invalid id, must be 32-bytes encoded as hex string')
  return buf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant