cmdruid / tapscript

A humble library for working with Tapscript and Bitcoin Transactions.
https://www.npmjs.com/package/@cmdcode/tapscript
Creative Commons Zero v1.0 Universal
188 stars 49 forks source link

Sign problem #9

Closed sondotpin closed 10 months ago

sondotpin commented 1 year ago
Error: crypto.getRandomValues must be defined
    at randomBytes$2 (/Users/sonpin/Documents/bitcoin/new/node_modules/@cmdcode/tapscript/dist/main.cjs:106:11)
    at Function.random (/Users/sonpin/Documents/bitcoin/new/node_modules/@cmdcode/tapscript/dist/main.cjs:1480:25)
    at sign (/Users/sonpin/Documents/bitcoin/new/node_modules/@cmdcode/tapscript/dist/main.cjs:7661:46)
    at Object.signTx [as sign] (/Users/sonpin/Documents/bitcoin/new/node_modules/@cmdcode/tapscript/dist/main.cjs:7656:17)

I've got this issue when sign

Signer.taproot.sign(seckey, txdata, 0, { extension: tapleaf })
cmdruid commented 1 year ago

Hello. This issue happens when you do not have the WebCrypto library or globalThis.crypto variable available. Please check the following:

Let me know if this helps!

skyrover7 commented 1 year ago

@cmdruid I am having the same issue, but only when running the node application from a hosted linux server.

on localhost (windows machine) I have no problem signing. works like a charm.

What's odd is that I am able to call the function from within server.js with var getRand = size => crypto.getRandomValues(new Uint8Array(size));, which i use later in the code to generate the user's private keys. this works on the server. but when i go to sign, I am getting the same error as @sondotpin

any ideas? i tried removing the crypto 1.0.1 package install that I had so it defaults to the inbuilt one.

I am on node V19.0.0

skyrover7 commented 1 year ago

in reference to #2 i made the change that was suggested there on our server and it worked!

great for now, but any thoughts on how/why this is happening?

cmdruid commented 1 year ago

What's odd is that I am able to call the function from within server.js with var getRand = size => crypto.getRandomValues(new Uint8Array(size));, which i use later in the code to generate the user's private keys. this works on the server. but when i go to sign, I am getting the same error as @sondotpin

Yes the crypto object may be available. The library is using globalThis.crypto to reference the crypto object so that it can bundle properly between nodejs and the browser. The caveat is that you have to use nodejs version 19+ or a modern browser that supports the new globalThis standard.

The alternative is a nightmarish polyfill of the crypto object and all sorts of hackery in order to get crypto to bundle properly between platforms and nested deep as a dependency. I tried to go that route and it simply wasn't worth it. I'm open to alternative solutions though and improving backwards compatibility of the crypto object.

skyrover7 commented 1 year ago

What's odd is that I am able to call the function from within server.js with var getRand = size => crypto.getRandomValues(new Uint8Array(size));, which i use later in the code to generate the user's private keys. this works on the server. but when i go to sign, I am getting the same error as @sondotpin

Yes the crypto object may be available. The library is using globalThis.crypto to reference the crypto object so that it can bundle properly between nodejs and the browser. The caveat is that you have to use nodejs version 19+ or a modern browser that supports the new globalThis standard.

The alternative is a nightmarish polyfill of the crypto object and all sorts of hackery in order to get crypto to bundle properly between platforms and nested deep as a dependency. I tried to go that route and it simply wasn't worth it. I'm open to alternative solutions though and improving backwards compatibility of the crypto object.

so does 19+ not include 19.0.0? that's where i am seeing this behavior

cmdruid commented 1 year ago

/Users/sonpin/Documents/bitcoin/new/node_modules/@cmdcode/tapscript/dist/main.cjs

I'm wondering if maybe globalThis.crypto behaves differently when used in a commonjs import versus an ES6 import.

Are you importing into a commonjs project in node?