libitx / txforge

Modern Bitcoin transaction builder, capable of supporting any non-standard and custom script type.
Apache License 2.0
64 stars 22 forks source link

node:internal/modules/cjs/loader:936 #23

Open jsim77 opened 2 years ago

jsim77 commented 2 years ago

I'm using VSCode on p2pkh.js but it seems that the code content itself requires Identification and Function, which is why the code p2pkh.js becomes like this below:

const { forgeTx } =require('txforge')

// We'll use these Casts in our transaction const casts = () => { P2PKH, OpReturn }

// You'll need UTXOs to fund a transaction. Use the toUTXO helper to turn // your UTXO data into the required objects. const utxo = () => toUTXO({ txid, // utxo transaction id vout, // utxo output index satoshis, // utxo amount script // utxo lock script })

// Forge a transaction const P2PKH = () => { P2PKH } const myPrivateKey = () => { myPrivateKey }

const tx = forgeTx({

inputs: [ function unlock() { P2PKH.unlock(utxo, { privkey: myPrivateKey })
} ], outputs: [ function lock() { P2PKH.lock(5000, { address: '1DBz6V6CmvjZTvfjvWpvvwuM1X7GkRmWEq' }), OpReturn.lock(0, { data: ['meta', '1DBz6V6CmvjZTvfjvWpvvwuM1X7GkRmWEq', txid] }) } ], change: { address: '1Nro9WkpaKm9axmcfPVp79dAJU1Gx7VmMZ' } })

// And behold! Forged by the Gods and found by a King - a transaction is born. console.log(tx.toHex())

node:internal/modules/cjs/loader:936 throw err; ^

Error: Cannot find module '/Users/jsjs77/node_modules/@runonbitcoin/nimble/classes/p2pkhs.js' at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15) at Function.Module._load (node:internal/modules/cjs/loader:778:27) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12) at node:internal/main/run_main_module:17:47 { code: 'MODULE_NOT_FOUND', requireStack: []

Any solution for the above? Note that my VScode in Mac unable to Import, that's why use "const"

Thanks

libitx commented 2 years ago

The casts object is exported from the main module. So you use it like this:

const { forgeTx, casts } =require('txforge')

// Get the casts you will use in your transaction
const { P2PKH, OpReturn } = casts
jsim77 commented 2 years ago

Thanks for getting back to me along with the solution. I have amended and right now, the new error as the following:

Error: P2PKH unlock must be created with valid 'privkey' as per the screenshot.

Screenshot 2022-08-17 at 1 01 02 PM

Then, I did input the "privkey" from my wallet (saved) and to Run again but the Error message still the same as per the screenshot below:

Screenshot 2022-08-17 at 1 29 09 PM

Does this mean I need to create 'privkey' in a separate coding?

Please advice, thanks so much.