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

Support Buffer in node environment #180

Closed

Conversation

mmv08
Copy link

@mmv08 mmv08 commented Jun 23, 2021

Fixes #175

If I understand correctly, Buffer is a subclass of Uint8Array that extends it, thus it should be ok to use Buffer.

API reference mentions that the browser's Buffer is lacking behind, this is why I included isNode flag

@junderw
Copy link
Member

junderw commented Jun 23, 2021

You just removed an assert and created a local variable that isn't used anywhere.

This code makes no sense.

@mmv08
Copy link
Author

mmv08 commented Jun 23, 2021

@junderw copypaste gone wrong, sorry :)

@mmv08
Copy link
Author

mmv08 commented Jul 26, 2021

@junderw any updates?

@fanatid
Copy link
Member

fanatid commented Jul 26, 2021

Why do we need this change?
Buffer.alloc(4) instanceof Uint8Array in node return true

@mmv08
Copy link
Author

mmv08 commented Jul 26, 2021

Lol, I tried to reproduce it again so I can properly explain but I couldn't. I had a problem with the validation when trying to sign a transaction with a ledger hardware wallet, cannot reproduce it anymore 🤷‍♂️ I'm sorry

@mmv08 mmv08 closed this Jul 26, 2021
@mmv08
Copy link
Author

mmv08 commented Aug 9, 2021

@fanatid I managed to reproduce this again
Screenshot 2021-08-09 at 10 12 35

The error happens within this snippet:

import { bufferToHex, ecrecover, pubToAddress } from 'ethereumjs-util'

export const isTxHashSignedWithPrefix = (txHash: string, signature: string, ownerAddress: string): boolean => {
  let hasPrefix
  try {
    const rsvSig = {
      r: Buffer.from(signature.slice(2, 66), 'hex'),
      s: Buffer.from(signature.slice(66, 130), 'hex'),
      v: parseInt(signature.slice(130, 132), 16),
    }
    const recoveredData = ecrecover(Buffer.from(txHash.slice(2), 'hex'), rsvSig.v, rsvSig.r, rsvSig.s)
    const recoveredAddress = bufferToHex(pubToAddress(recoveredData))
    hasPrefix = !sameString(recoveredAddress, ownerAddress)
  } catch (e) {
    console.error(e)
    hasPrefix = true
  }
  return hasPrefix
}

@mmv08
Copy link
Author

mmv08 commented Aug 9, 2021

I haven't had time to dive in into ethereumjs-util and see if they perform some manipulations over signature Buffer

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

Successfully merging this pull request may close these issues.

instanceof Uint8Array returning false
3 participants