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 · 1 comment
Open

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

gmaclennan opened this issue Aug 15, 2023 · 1 comment
Labels

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
@gmaclennan
Copy link
Member Author

We might want a looser check here. We should definitely check for an empty string, and a string that encodes to an empty Buffer, but do we want more than that. Maybe buf.length >=8 to ensure that we have some degree of uniqueness?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant