<p>Looks like you are using</p>

<pre><code>console.log(`address: ${address}`)
</code></pre>

<p>Which will call the custom inspect function <a href="https://github.com/bcoin-org/bcoin/blob/6beb54dddc1256cca379ad765b71fd5cdee427f3/lib/primitives/address.js#L285-L296" rel="nofollow noreferrer">here</a>.</p>

<p>What you need to do is be explicit and call</p>

<p><code>address.toString('main')</code></p>

<p>as defined <a href="https://github.com/bcoin-org/bcoin/blob/6beb54dddc1256cca379ad765b71fd5cdee427f3/lib/primitives/address.js#L273-L283" rel="nofollow noreferrer">here</a></p>

<p>I encourage you to reach out to us on <a href="https://bcoin.io/slack-signup" rel="nofollow noreferrer">slack</a> or IRC #bcoin if you want to chat more interactively with the developers. </p>

CC BY-SA 4.0
2020-05-03T19:48:14.440Z
Bad news my friend. That's not working.
Mario Mazzola
CC BY-SA 4.0
2020-05-03T19:53:20.403Z
Join slack or irc and we can debug together. I'm not sure exactly where you're getting the address object or how you are parsing it.
pinhead
CC BY-SA 4.0
2020-05-03T16:58:33.273Z
2020-05-03T16:58:33.273Z
pinhead
<p>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.</p>

<p>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.</p>

<p>The application properly logs transactions in the mempool that match the filter. So far so good.</p>

<p>Anyway, I cannot get the addresses of inputs and outputs of the transaction object being logged.</p>

<p>Each address property in the TX looks like this:</p>

<pre><code>address: &lt;Address: type=witness version=0 str=bc1qsgjma9uamep8k6eln4j9xeq6aqhrljmnwhtjdj&gt;
</code></pre>

<p>Needless to say, this format doesn't allow me to do much with addresses...</p>

<p>What should I do to get the address string (in this case: 'bc1qsgjma9uamep8k6eln4j9xeq6aqhrljmnwhtjdj') in JS using the BCOIN library?</p>

<p>Thanks a lot</p>

<hr>

<p>Sorry but this doesnt work. </p>

<p>Here is the response I get:</p>

<pre><code>Node -- TX Event:

    {
        height: -1,
          block: null,
          time: 0,
          date: null,
          index: -1,
          version: 1,
          inputs: [
            {
              type: 'witnesspubkeyhash',
              subtype: null,
              address: &lt;Address: type=witness version=0 str=bc1q4vvn4qcnssuz4cye6p5zs7yycndzep4ey80jtl&gt;,
              script: &lt;Script: &gt;,
              witness: &lt;Witness: 3044022043bc6f67b4bab6f0c38c3334d81d3e1d5ea746df70bd74fd14d687e900790f730220778e21beed3068e3a17b7b533585bf11525c25ffaa40cc8ae47d286dd3fc6cd901 02f84d8111044774051c7bd35ca9f4e975b1b3
        4bc70f5c2ec96d734b9407818cf0&gt;,
              redeem: null,
              sequence: 4294967295,
              prevout: &lt;Outpoint: 6477d78f01945d1c8f0c44735dc02703871b9f7f61dd5c759a8aae69f133506e/1&gt;,
              coin: null
            }
          ],
          outputs: [
            {
              type: 'scripthash',
              value: '0.00011244',
              script: &lt;Script: OP_HASH160 0x14 0x4aa79fc5cf098ab5911134e0ba16c9e5466c307c OP_EQUAL&gt;,
              address: &lt;Address: type=scripthash version=-1 str=38VkjU2CxU77zhbq95iFr47njnGJVjpEpG&gt;
            },
            {
              type: 'witnesspubkeyhash',
              value: '0.00002918',
              script: &lt;Script: OP_0 0x14 0xcd83248f8bcd6093e14e7794bd22f6dc3edbbcb9&gt;,
              address: &lt;Address: type=witness version=0 str=bc1qekpjfrute4sf8c2ww72t6ghkmsldh09ewyr35p&gt;
            }
          ],
          locktime: 0
        }
        Outputs
        OUT value:  11244
        OUT:  undefined
        (node:8138) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'toString' of undefined
            at EventEmitter.&lt;anonymous&gt; (/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.&lt;anonymous&gt; (/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.
</code></pre>

<p>My Code:</p>

<pre><code>nodeSocket.bind('tx', (raw) =&gt; {
  const newTX = TX.fromRaw(raw);
  console.log('Node -- TX Event:\n', newTX);
  console.log("Outputs");
  //newTX.outputs.forEach(output =&gt;{
  for(let out = 0; out &lt; 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'));
  }
});
</code></pre>

<p>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.</p>

<p>But, when I try to log it individually (only the address), it returns undefined.</p>

<p>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.</p>

<bcoin>
BCoin bsock: listen to transactions and make sense of them
Mario Mazzola
# BCoin bsock: listen to transactions and make sense of them

(Asked by: username_0 on 2020-05-03)

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.

**Tags:** bcoin


---

## 1 answers

### Answer by username_1 on 2020-05-03. Score: 1

Looks like you are using

```
console.log(`address: ${address}`)

```

Which will call the custom inspect function [here](https://github.com/bcoin-org/bcoin/blob/6beb54dddc1256cca379ad765b71fd5cdee427f3/lib/primitives/address.js#L285-L296).

What you need to do is be explicit and call

`address.toString('main')`

as defined [here](https://github.com/bcoin-org/bcoin/blob/6beb54dddc1256cca379ad765b71fd5cdee427f3/lib/primitives/address.js#L273-L283)

I encourage you to reach out to us on [slack](https://bcoin.io/slack-signup) or IRC #bcoin if you want to chat more interactively with the developers.

> **Comments:**
> **username_0**: Bad news my friend. That's not working.  
> **username_1**: Join slack or irc and we can debug together. I'm not sure exactly where you're getting the address object or how you are parsing it.  

---

