bitcoinjs/wif

Wif behaving differently when being used in Electron Application Production

Taha-daboussi opened this issue · 3 comments

So am using "ecpair" library to get ECPairFactory and then using @bitcoinerlab/secp256k1
as the ecc interface so I can import Bitcoin taproot private key.
When running this code in development it worked perfectly , but when I built it into my Electron application I ran into 1 problem
getting an error of " expected buffer , received Uint8Array "
after debugging the code I found out where the issue was

in the node_moules/wif/index.js function decodeRaw

image

for some reason returning two different values if production or development . was able to fix this (not really a fix )

by doing this in the "decode" function

function decode (string, version) {
  const data = decodeRaw(bs58check.decode(string), version) 
  if(data.privateKey instanceof Uint8Array ){
    const uint8Array = new Uint8Array(Object.keys(data.privateKey).map(key => data.privateKey[key]));
// Convert Uint8Array to Buffer
    const buffer = Buffer.from(uint8Array);
    data.privateKey = buffer
  }
  return data
}

Please let me know if there is any other solution other my temp fix

This sounds like an Electron issue. Electron treating Uint8Array as interchangeable with Buffer is probably not a good idea. I'm not sure why Electron would do such a thing.

I assume their production settings cause something to get changed, or maybe it's the bundler's problem.

Check if fixed in v5.0.0

(Oh, I guess you are using this as a dependency, so we'd need to update ecpair: @Nesopie)