cryptocoinjs / secp256k1-node

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

instanceof Uint8Array returning false #175

Open bitsanity opened 3 years ago

bitsanity commented 3 years ago

Using secp256k1 npm module within nwjs-sdk-v0.51.2-linux-x64

let sigbytes = [an ECDSA signature in DER format, 71 bytes]
let dersig = Uint8Array.from( sigbytes );
let sigobj = SECP256K1.signatureImport( dersig );

Result

Error: Expected signature to be an Uint8Array", source: [...]/node_modules/secp256k1/lib/index.js (18)

Origin of the Error

function isUint8Array (name, value, length) {
  assert(value instanceof Uint8Array, `Expected ${name} to be an Uint8Array`)

Root Cause

The instanceof operator returns false. The typeof operator returns object, so javascript is seeing value as an object not as a Uint8Array.

Workaround/Fix

function isUint8Array (name, value, length) {
  assert(value instanceof Uint8Array || value.constructor.name === 'Uint8Array', `Expected ${name} to be an Uint8Array`)
fanatid commented 3 years ago

I do not understand, instanceof does not work in nwjs?

bitsanity commented 3 years ago

Oops accidentally hit close. fanatid - yes correct for some reason the instanceof operator is failing for Uint8Array - sees as object for some reason.

bitsanity commented 3 years ago

The workaround/fix works for me. I get the signature imported ok.

goncer commented 2 years ago

I am having this error, why not use the node implementation?

const isuint8 = require('util').types.isUint8Array; in:

    privateKeyVerify (seckey) {
      isUint8Array('private key', seckey, 32)

      return secp256k1.privateKeyVerify(seckey) === 0
    },

I tested for a Buffer in the console, and it works,

isuint8(value)
>true
value instanceof Uint8Array
>false
value
>Buffer(32) [86, 56, 62, 77, 229, 4, 46, 49, 85, 225, 47, 117, 104, 122, 39, 225, 167, 132, 235, 238, 234, 100, 41, 84, 208, >162, 176, 13, 122, 151, 47, 92]