Bitcoin Forum
May 05, 2024, 12:53:53 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: bitcore-lib to connect to my full running node  (Read 193 times)
Jessica432 (OP)
Newbie
*
Offline Offline

Activity: 16
Merit: 1


View Profile
September 29, 2018, 01:55:34 AM
Merited by bones261 (1)
 #1

I have written a node js application which exposes API to do two things:
1) Get balance of any address
2) Transfer funds from one address to another

I am doing it using bitcore-lib and it works great.

Now I want bitcore-lib to hit my bitcoin running node. I need help in how can I do that?

From internet forums, there are suggestions to change bitcore-node.json, but I do not find such file in bitcore-lib. I think this should be easy to do that, but I am unable to find where to make the change!
1714913633
Hero Member
*
Offline Offline

Posts: 1714913633

View Profile Personal Message (Offline)

Ignore
1714913633
Reply with quote  #2

1714913633
Report to moderator
Bitcoin addresses contain a checksum, so it is very unlikely that mistyping an address will cause you to lose money.
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
1714913633
Hero Member
*
Offline Offline

Posts: 1714913633

View Profile Personal Message (Offline)

Ignore
1714913633
Reply with quote  #2

1714913633
Report to moderator
1714913633
Hero Member
*
Offline Offline

Posts: 1714913633

View Profile Personal Message (Offline)

Ignore
1714913633
Reply with quote  #2

1714913633
Report to moderator
1714913633
Hero Member
*
Offline Offline

Posts: 1714913633

View Profile Personal Message (Offline)

Ignore
1714913633
Reply with quote  #2

1714913633
Report to moderator
aliashraf
Legendary
*
Offline Offline

Activity: 1456
Merit: 1174

Always remember the cause!


View Profile WWW
September 29, 2018, 04:56:27 AM
 #2

Just create the file:
Code:
touch bitcore-node.json
then edit this file and put your json there, something like this:
Code:

{
  "network": "livenet",
  "port": 3001,
  "services": [
    "bitcoind",
    "web",
    "insight-api",
    "insight-ui",
    "<additional_service>"
  ],
  "servicesConfig": {
    "bitcoind": {
      "spawn": {
        "datadir": "/home/<youruser>/.bitcoin",
        "exec": "/home/<youruser>/bitcoin/src/bitcoind"
      }
    }
  }
}
Have fun. Wink
Jessica432 (OP)
Newbie
*
Offline Offline

Activity: 16
Merit: 1


View Profile
September 29, 2018, 08:53:34 AM
 #3

Thank you so much, and I really appreciate your help.

I just created the bitcore-node.json file in the same folder where I have index.js of my nodejs application on windows machine. I set the rpcuser and rpcpassword as it is set in the bitcoin.conf file. I am running Bitcoin core full node on my windows box.

I want to know if there is a way if I can confirm if my code(send money transactions etc) is hitting the local running node or not? I tested them and they are running successfully as they were running earlier. I will really appreciate any pointers in this direction.

Below is my bitcore-node.json file:

{
  "network": "livenet",
  "port": 3001,
  "services": [
    "bitcoind",
    "web",
    "insight-api",
    "insight-ui",
    "web"
  ],
  "servicesConfig": {
    "bitcoind": {
      "connect": [
        {
          "rpcuser": "admin1",
          "rpcpassword": "admin2",
          "zmqpubrawtx": "tcp://127.0.0.1:8332"
        }
      ]
    }
  }
}
aliashraf
Legendary
*
Offline Offline

Activity: 1456
Merit: 1174

Always remember the cause!


View Profile WWW
September 29, 2018, 10:18:24 AM
 #4

Thank you so much, and I really appreciate your help.

I just created the bitcore-node.json file in the same folder where I have index.js of my nodejs application on windows machine. I set the rpcuser and rpcpassword as it is set in the bitcoin.conf file. I am running Bitcoin core full node on my windows box.

I want to know if there is a way if I can confirm if my code(send money transactions etc) is hitting the local running node or not? I tested them and they are running successfully as they were running earlier. I will really appreciate any pointers in this direction.

You can:
1) run bitcoind with debug switch for mempool on (-debug=mempool,[other option,])
2) Navigate to the /.bitcoin folder
3) View the last few recent entries into the debug.log file

you can find entries like <timestamp> AcceptToMemoryPool: peer=<#peer num> <txid>


if you know your txid this would help resolving your nodejs service peer number meanwhile  Wink
Jessica432 (OP)
Newbie
*
Offline Offline

Activity: 16
Merit: 1


View Profile
September 29, 2018, 10:54:49 AM
 #5

I got the following error: C:\Program Files\Bitcoin\daemon>bitcoind -debug=mempool
Error: Unable to start HTTP server. See debug log for details.

And C:\Users\shris\AppData\Roaming\Bitcoin\debug.log does not have any recent enteries. Is there someway I can show my desktop to you quick and you have a look.
aliashraf
Legendary
*
Offline Offline

Activity: 1456
Merit: 1174

Always remember the cause!


View Profile WWW
September 29, 2018, 11:25:19 AM
 #6

I got the following error: C:\Program Files\Bitcoin\daemon>bitcoind -debug=mempool
Error: Unable to start HTTP server. See debug log for details.

And C:\Users\shris\AppData\Roaming\Bitcoin\debug.log does not have any recent enteries. Is there someway I can show my desktop to you quick and you have a look.
You should stop bitcoind before starting it again, this error is typically because of you trying to run it when another instance is running. check with your taskmanager.

No need to your desktop, for the time being.
Jessica432 (OP)
Newbie
*
Offline Offline

Activity: 16
Merit: 1


View Profile
September 29, 2018, 12:30:45 PM
Last edit: September 29, 2018, 12:53:09 PM by Jessica432
 #7

Bitcoind works now and I tried but debug.log is not getting updated.

Just FYI.. here is the code I am using (only showing relevant lines)
I am wondering if birtcore-lib is really doing the transaction and connecting to some node. Or it is Insight which is doing the transaction over the wire. Insight connects to server https://test-insight.bitpay.com -In that case I need to create my own insight server.

Code:

var Insight = require('bitcore-explorers').Insight;
var bitcore = require('bitcore-lib');

var insight = new Insight("testnet");
insight.getUnspentUtxos(<fromaddress>, function (error, utxos) {
   let bitcore_transaction = new bitcore.Transaction()
                            .from(utxos)
                            .to(transaction.toaddress, transactionAmount)
                            .change(transaction.fromaddress)
                            .sign(new bitcore.PrivateKey(transactio[Suspicious link removed]ivatekey));


   insight.broadcast(bitcore_transaction, <error handler function>);
}
aliashraf
Legendary
*
Offline Offline

Activity: 1456
Merit: 1174

Always remember the cause!


View Profile WWW
September 29, 2018, 02:37:25 PM
Last edit: September 29, 2018, 04:17:52 PM by aliashraf
 #8

Jessica,
You are using insight which is the simple but less secure way because it uses a third party service, and it has nothing to do with your local bitcoin full node. You should start over by introducing bitcoind as a service to bitcore,
For this to happen you need to
1- Install a bitcore node,
2-  Add bitcoind as service to this node,
3-  Install bitcore-lib,
3- use extended methods and events that are now available to do what you want. e.g. getting balance for every address and generating / relaying a transaction for spending that balance.

Below I try to present a very simplified and basic approach to your inquiry. I hope it would help:
Code:
var bitcoreNode= require('bitcore-node');
var Bitcoin = bitcoreNode.services.Bitcoin;
var Node = bitcoreNode.Node;

var configuration = {
  datadir: '/home/user/.bitcoin',
  network: 'testnet',
  services: [
    {
      name: 'bitcoind',
      module: Bitcoin,
      config: {
        spawn: {
          datadir: '/home/<username>/.bitcoin',
          exec: '/home/<username>/bitcore-node/bin/bitcoind'
        }
      }
    }
  ]
};

var node = new Node(configuration);

node.start(function() {
 console.log('Bitcoin Node is starting ...');
});

node.on('ready', function() {
  console.log('Bitcoin Node Ready');
});

node.on('error', function(Err) {
  console.error(Err);
});

Now you have run bitcoind and connected to it as a service, an extended set of apis are available now and you can use it for your application
For example you can get total satoshi balance of an address by
Code:
var total= node.services.bitcoind.getAddressBalance(<some address>, false ) ;
You need to find all UTXOs for a given address as well:
Code:
var address =<some address>;
var UTXOs = node.services.bitcoind.getAddressUnspentOutputs(address, options);
var utx0=  {"txId" : UTXOs[0].txId,
  "outputIndex" : UTXOs[0].outputIndex,
  "address" : UTXOs[0].address,
  "script" : UTXOs[0].script,
  "satoshis" : UTXOs[0].satoshi
};
// I deliberately wrote it foolishly to disclose the object content ;)
// checking last few blocks to use a proper fee:
var numberOfBlocks = 5;
var suggestedFee = node.services.bitcoind.estimateFee(numberOfBlocks);

You also need bitcore-lib installed and required.
Code:
var bitcore = require('bitcore-lib');
var privateKey = new bitcore.PrivateKey(<privateKey>);

var transaction = new bitcore.Transaction()
  .from(utxo)
  .to(<destnation address>, suggestedFee )
  .sign(privateKey);
Now it is time to send the transaction:
Code:
var hashSent = node.services.bitcoind.sendTransaction(transaction.serialize());

console.log( "transaction sent:"+hash);
Obviously this is not a working code, exception handling and a lot of details are missing here. But you can use the skeleton and classes/methods/events used above as a starting point to navigate through the documentations.

Jessica,
I have been in your situation many times and I know what a headache it is, but as of my experience,  eventually you have to elaborate a bit lot more and you shouldn't rest until you have finished every detail. Unfortunately I'm not a bitcore enthusiast/practitioner that much, just made a review about it long time ago and i afraid I wouldn't be of much more help. Good luck. Smiley

 
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!