bitcoinjs-lib
bitcoinjs-lib copied to clipboard
How to fetch an existing transaction?
For the life of me, I just can't figure out how to use NodeJS to fetch an existing bitcoin transaction on a local node. The code examples here seems too obfuscated for my knowledge and it does not look like the NodeJS type of code that I am used to.
Can anyone please provide the most simplistic code for fetching an existing bitcoin transaction making use of an RPC connection ?
################################
var bitcore = require('bitcoin-core');
var RpcClient = require('bitcoind-rpc');
var config = {
protocol: 'http',
host: 'localhost',
port: 8800,
user: 'myuser',
pass: 'mypass'
};
var rpc = new RpcClient(config);
// Next?? ################################
The RPC connection is working (tested with console.log), but don't know how to continue
I looked into bitcoin-core, bcoin and bitcore-lib, but I just can't make it work.
I have now tested the script below with bitcoinjs-lib and zeromq, but it returns what seems to be empty data. The blockchain is working and the debug.log file shows new transactions/blocks coming in. This is a TokenPay blockchain, which is a copy of the bitcoin blockchain.
The output below the script shows what this script returns. It is empty, why? Can anyone please assist me in progressing?
Bare in mind that I am new to blockchain programming.
var bitcoin = require('bitcoinjs-lib');
var zmq = require('zeromq');
var sock = zmq.socket('sub');
var addr = 'tcp://127.0.0.1:8800';
sock.connect(addr);
sock.subscribe('rawtx');
console.log('sock:', sock);
sock.on('message', function(topic, message) {
console.log('topic:', topic.toString());
if (topic.toString() === 'rawtx') {
var rawTx = message.toString('hex');
var tx = bitcoin.Transaction.fromHex(rawTx);
var txid = tx.getId();
console.log('received transaction', txid, tx);
}
});
######################### output:
sock: EventEmitter {
_events: [Object: null prototype] {},
_eventsCount: 0,
_maxListeners: undefined,
type: 'sub',
_zmq:
{ pending: false,
state: 0,
onReadReady: [Function],
onSendReady: [Function] },
_paused: false,
_isFlushingReads: false,
_isFlushingWrites: false,
_outgoing: BatchList { firstBatch: null, lastBatch: null, length: 0 } }
format your post with proper syntax highlighting and spacing please...
```javascript
console.log('Hello World!')
function test(data) {}
```
becomes
console.log('Hello World!')
function test(data) {}
I just can't figure out how to use NodeJS to fetch an existing bitcoin transaction on a local node.
You can't unless you:
- Have a txindex. (
--txindex=1requires a re-index which will take up to a day on old hardware) - Use RPC to query via txid using getrawtransaction.
I just can't figure out how to use NodeJS to fetch an existing bitcoin transaction on a local node.
You can't unless you:
- Have a txindex. (
--txindex=1requires a re-index which will take up to a day on old hardware)- Use RPC to query via txid using getrawtransaction.
I have now added the txindex=1 to my conf file for the daemon and restarted the daemon, but that did not seem to make a difference, since I simply reran my query and the output was the same as before, seemingly empty.
I have then also added the reindex=1 option to the conf file and restarted the daemon and now the entire chain is redownloading and I assume it is being stored in an indexed way. It will take about two days to download due to the slow relative slow connection speed and the 300MB blockchain file.
Once completed, I will rerun my queries and also with the getrawtransaction RPC method that you mentioned and provide feedback here.
Thank you for your help so far and I have also now fixed my code with you formatting recommendation.
I could get this to work, thank you! That helped me a great deal and I can move forward with my coding.
The answer was indeed that the transactions had to be indexed with the txindex=1 flag as you mentioned. That helped a lot.
While I was researching this, I also found other flags that one can set that I think will come in handy in the future with regards to querying the blockchain database:
server=1
addressindex=1
timestampindex=1
spentindex=1
Do you perhaps know of any other flags that one can add to the configuration?
Bare in mind, the performance of the blockchain is not that important as it is used for reporting information only.