I am trying to set up bsock in my bcoin application, with the aim of listening to some transactions that are broadcasted in the Bitcoin network. This would enable users of my app to have real time information on (unconfirmed) payments to addresses which are relevant for them, without having to actively query my node at regular intervals.
BCoin.io documentation is rather good and I managed to establish a socket connection using bsock. I manage to subsibe to the mempool channel to listen to 'tx'. I ve also set up the bloom filter with the appropriate rules.
The application properly logs transactions in the mempool that match the filter. So far so good.
Anyway, I cannot get the addresses of inputs and outputs of the transaction object being logged.
Each address property in the TX looks like this:
address: <Address: type=witness version=0 str=bc1qsgjma9uamep8k6eln4j9xeq6aqhrljmnwhtjdj>
Needless to say, this format doesn't allow me to do much with addresses...
What should I do to get the address string (in this case: 'bc1qsgjma9uamep8k6eln4j9xeq6aqhrljmnwhtjdj') in JS using the BCOIN library?
Thanks a lot
Sorry but this doesnt work.
Here is the response I get:
Node -- TX Event:
{
height: -1,
block: null,
time: 0,
date: null,
index: -1,
version: 1,
inputs: [
{
type: 'witnesspubkeyhash',
subtype: null,
address: <Address: type=witness version=0 str=bc1q4vvn4qcnssuz4cye6p5zs7yycndzep4ey80jtl>,
script: <Script: >,
witness: <Witness: 3044022043bc6f67b4bab6f0c38c3334d81d3e1d5ea746df70bd74fd14d687e900790f730220778e21beed3068e3a17b7b533585bf11525c25ffaa40cc8ae47d286dd3fc6cd901 02f84d8111044774051c7bd35ca9f4e975b1b3
4bc70f5c2ec96d734b9407818cf0>,
redeem: null,
sequence: 4294967295,
prevout: <Outpoint: 6477d78f01945d1c8f0c44735dc02703871b9f7f61dd5c759a8aae69f133506e/1>,
coin: null
}
],
outputs: [
{
type: 'scripthash',
value: '0.00011244',
script: <Script: OP_HASH160 0x14 0x4aa79fc5cf098ab5911134e0ba16c9e5466c307c OP_EQUAL>,
address: <Address: type=scripthash version=-1 str=38VkjU2CxU77zhbq95iFr47njnGJVjpEpG>
},
{
type: 'witnesspubkeyhash',
value: '0.00002918',
script: <Script: OP_0 0x14 0xcd83248f8bcd6093e14e7794bd22f6dc3edbbcb9>,
address: <Address: type=witness version=0 str=bc1qekpjfrute4sf8c2ww72t6ghkmsldh09ewyr35p>
}
],
locktime: 0
}
Outputs
OUT value: 11244
OUT: undefined
(node:8138) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'toString' of undefined
at EventEmitter.<anonymous> (/home/mmmilione/api/index.js:64:66)
at EventEmitter.emit (events.js:210:5)
at Socket.handleEvent (/home/mmmilione/api/node_modules/bsock/lib/socket.js:685:19)
at Socket.handlePacket (/home/mmmilione/api/node_modules/bsock/lib/socket.js:620:21)
at Socket.handleMessage (/home/mmmilione/api/node_modules/bsock/lib/socket.js:526:21)
at Socket.handleFrame (/home/mmmilione/api/node_modules/bsock/lib/socket.js:448:21)
at Parser.<anonymous> (/home/mmmilione/api/node_modules/bsock/lib/socket.js:150:20)
at Parser.emit (events.js:210:5)
at Parser.feedBinary (/home/mmmilione/api/node_modules/bsock/lib/parser.js:40:10)
at Socket.onMessage (/home/mmmilione/api/node_modules/bsock/lib/socket.js:238:17)
(node:8138) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not
handled with .catch(). (rejection id: 2)
(node:8138) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
My Code:
nodeSocket.bind('tx', (raw) => {
const newTX = TX.fromRaw(raw);
console.log('Node -- TX Event:\n', newTX);
console.log("Outputs");
//newTX.outputs.forEach(output =>{
for(let out = 0; out < newTX.outputs.length; out++){
console.log('OUT value: ', newTX.outputs[out].value);
console.log('OUT: ', newTX.outputs[out].address);
console.log('OUT Address String', newTX.outputs[out].address.toString('main'));
}
});
As you can see, when I log the TX I can see the address propert of the input in the wierd format I mentioned before.
But, when I try to log it individually (only the address), it returns undefined.
As a result, as I try to call the toString method the app throws an error, saying tht it cannot read the property toString of undefined.