Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
sheradoo opened this issue Nov 16, 2023 · 2 comments
Open

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

sheradoo opened this issue Nov 16, 2023 · 2 comments

Comments

@sheradoo
Copy link

sheradoo commented Nov 16, 2023

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
Copy link
Member

junderw commented Nov 16, 2023

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

@sheradoo
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants