cryptocoinjs / secp256k1-node

Node.js binding for an Optimized C library for EC operations on curve secp256k1
Other
341 stars 120 forks source link

Support Buffer in node environment #180

Closed mmv08 closed 3 years ago

mmv08 commented 3 years ago

~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 commented 3 years ago

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

This code makes no sense.

mmv08 commented 3 years ago

@junderw copypaste gone wrong, sorry :)

mmv08 commented 3 years ago

@junderw any updates?

fanatid commented 3 years ago

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

mmv08 commented 3 years ago

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 commented 3 years ago

@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 commented 3 years ago

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