Giter Club home page Giter Club logo

Comments (2)

junderw avatar junderw commented on June 19, 2024

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

from coinselect.

sheradoo avatar sheradoo commented on June 19, 2024

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

from coinselect.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.