bitcoinjs / coinselect

An unspent transaction output (UTXO) selection module for bitcoin.
MIT License
176 stars 101 forks source link

Module cannot be found , npm I —dev-save @types/coinselect #85

Open sheradoo opened 7 months ago

sheradoo commented 7 months ago

My coinselect module was working fine but two days ago it starts telling me module cannot be found , I have tried everything including adding js extensions to the module import , nothing seems to be working, I am using an esm module type in my package.json file , every other module is importing fine including bitcoinjs-lib but only coinselect and please is there any better alternative

junderw commented 7 months ago

Please post your versions for node, npm, etc. and create a minimal reproduction that we can try to reproduce.

sheradoo commented 7 months ago

Please post your versions for node, npm, etc. and create a minimal reproduction that we can try to reproduce.

Npm v 9.8.0 Node v 20.9.0

this is the error it gives below

Could not find a declaration file for module 'coinselect'. 'c:/Users/USER/Desktop/Richie_Exchange/node_modules/coinselect/index.js' implicitly has an 'any' type. Try npm i --save-dev @types/coinselect if it exists or add a new declaration (.d.ts) file containing declare module 'coinselect';ts(7016) module "c:/Users/USER/Desktop/Richie_Exchange/node_modules/coinselect/index"

/////////////222/////// Below is my package.json file

{ "name": "richie_exchange", "version": "1.0.0", "description": "", "main": "app", "type": "module", "esModuleInterop": true, "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "", "license": "ISC", "dependencies": { "axios": "^1.4.0", "bip32": "^4.0.0", "bip39": "^3.1.0", "bitcoinjs-lib": "^6.1.3", "coinselect": "^3.1.13", "dotenv": "^16.3.1", "ecpair": "^2.1.0", "express": "^4.18.2", "got": "^13.0.0", "tiny-secp256k1": "^2.2.3" } }

////////://::::::::::::::::://////// And this is my code

import bitcoin, { Psbt } from 'bitcoinjs-lib'; import coinSelect from 'coinselect' import ECPairFactory from 'ecpair'; import * as ecc from 'tiny-secp256k1';

const ECPair = ECPairFactory.ECPairFactory(ecc);

import btcFunctions from './function/bitcoin_service.js'; import { config } from 'dotenv';

const createTransactionTX = {

async CreateTX(publicKeyFrom, privateKeyFrom, publicKeyTo, changeAddress, amoutTosend, network) { if (network == "test") { var btcNetwork = bitcoin.networks.testnet; } else { btcNetwork = bitcoin.networks.bitcoin; }

const data = await btcFunctions.getBalance(publicKeyFrom, network);
if (!data) {
  return;
}

var balance = data.balance;  // + data.unconfirmed_balance, should not be able to spend from uncomfirmed balance

if (balance > amoutTosend) {
  const unspentOutputs = await btcFunctions.getUnspentOutputs(publicKeyFrom, network);
  if (!unspentOutputs) {
    return;
  }

  const keyPair = ECPair.fromWIF(privateKeyFrom, btcNetwork)
  const p2wpkh = bitcoin.payments.p2wpkh({ pubkey: keyPair.publicKey, network: btcNetwork })
  var scriptPubkey = p2wpkh.output.toString('hex');
  console.log("////////////////////////////////////////////////////////////");
  //const redeemScript = p2wpkh.redeem.output; 
  //var scriptPubkey2 = keyPair.publicKey;
  //console.log(scriptPubkey);
  //console.log(p2wpkh.pubkey.toString('hex'));
  //console.log(p2wpkh.address);

  //fee
  var feePerByte = await btcFunctions.getFeePerByte();
  var feeRate = feePerByte.regular * 100000000;  //fee by byte on regular mode, APi provider blockchain.info

  var txRef = await btcFunctions.getTxRef(unspentOutputs);
  //var rawHex = await btcFunctions.getRawHex(txRef[0].tx_hash, network);

  //console.log(balance);

  //console.log(txRef);

  //console.log(feePerByte);

  console.log("////////////////////////////////////////////////////////////");
  let utxoS = [];

  if (txRef && txRef.length > 0) {
    for (const tx of txRef) {
      utxoS.push({
        txHash: tx.tx_hash,
        txIndex: tx.tx_output_n,
        amount: tx.value * 100000000,
        witnessUtxo: { script: Buffer.from(p2wpkh.output, 'hex'), value: tx.value * 100000000 },
        // redeemScript: redeemScript

      });
    }
  }

  if (!utxoS.length) {
    return;
  }

  var utxos = [];
  var totalAmount = 0;
  for (const utxo of utxoS) {
    utxos.push({
      txId: utxo.txHash,
      vout: utxo.txIndex,
      value: utxo.amount,
      witnessUtxo: utxo.witnessUtxo,
    });
    //totalAmount += utxo.amount;
  }

  let targets = [
    {
      address: publicKeyTo,
      value: amoutTosend * 100000000 // value in satoshi (0.5 BTC)
    }
  ]

  //console.log(amoutTosend * 100000000);
  let { inputs, outputs, fee } = coinSelect(utxos, targets, feeRate)
  console.log(fee);
  if (!inputs || !outputs) return

  const psbt = new bitcoin.Psbt({ network: btcNetwork });

  inputs.forEach(input =>
    psbt.addInput({
      hash: input.txId,
      index: input.vout,
      witnessUtxo: input.witnessUtxo,
      //privateKeyFrom: input.privateKeyFrom
    })
  )

  outputs.forEach(output => {
    if (!output.address) {
      output.address = publicKeyFrom;
    }
    //console.log(output.value);
    psbt.addOutput({
      address: output.address,
      value: output.value
    });
  })

  psbt.signAllInputs(keyPair);
  psbt.finalizeAllInputs();
  const tx = psbt.extractTransaction(true);
  const Signedtx = tx.toHex();
  const txID = tx.getId();
  console.log(Signedtx)

} else {
  console.log("You do not have enough coin");
}

}

}

export default createTransactionTX;

please check if I am doing anything wrong I am a new bie