Skip to content

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

Description

@gmaclennan

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions