Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix: pass along toJSON args #586

Closed
wants to merge 1 commit into from

Conversation

tynes
Copy link
Member

@tynes tynes commented Aug 23, 2018

Without passing along the arguments here, this.getJSON will default will return incorrect information.

  getJSON(network, coin) {
    network = Network.get(network);

    let addr;
    if (!coin) {
      addr = this.getAddress();
      if (addr)
        addr = addr.toString(network);
    }

    return {
      prevout: this.prevout.toJSON(),
      script: this.script.toJSON(),
      witness: this.witness.toJSON(),
      sequence: this.sequence,
      address: addr,
      coin: coin ? coin.getJSON(network, true) : undefined
    };
  }

The network will default to main, which will result in an incorrect address

@codecov-io
Copy link

Codecov Report

Merging #586 into master will not change coverage.
The diff coverage is 0%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #586   +/-   ##
=======================================
  Coverage   52.82%   52.82%           
=======================================
  Files         104      104           
  Lines       27661    27661           
  Branches     4736     4736           
=======================================
  Hits        14611    14611           
  Misses      13050    13050
Impacted Files Coverage Δ
lib/primitives/input.js 83.04% <0%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 7178d9b...615b9dd. Read the comment docs.

@braydonf
Copy link
Contributor

braydonf commented Aug 23, 2018

I noticed a similar issue in an RPC method so there could be more. Here was the fix: braydonf@a72c50a

Edit: added a pr for it at #587

@tynes
Copy link
Member Author

tynes commented Aug 24, 2018

Seems like we could use a test suite that tries each network and asserts for anything that is network specific

@tuxcanfly
Copy link
Member

tuxcanfly commented Aug 24, 2018

Nice find. Agree on need to test all places where network is not being passed.

    let addr;
    if (!coin) {
      addr = this.getAddress();
      if (addr)
        addr = addr.toString(network);
    }

in this bit, even if coin is passed, the address is always null. Looks like getAddress accepts coin. Wouldn't we want this.getAddress(coin) here, if coin is available?

@braydonf
Copy link
Contributor

Anything remaining for this? A test?

@@ -298,7 +298,7 @@ class Input {
*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor: update jsdoc 👍

@tynes
Copy link
Member Author

tynes commented Jan 30, 2019

Bump: Noticing this in hsd as well, its on my TODOs to add some tests for this and get them into hsd as well. I think it will help developers a lot when they log their primitives to see the correctly formatted addresses

@braydonf
Copy link
Contributor

braydonf commented Feb 1, 2019

So most transaction and block related primitives do not pass these arguments for the toJSON method, including:

  • Output
  • Input
  • TX
  • TXMeta
  • MTX
  • Block
  • MerkleBlock

For consistency it would be good to have the similar behavior between these. I suspect that the reason for the lack of arguments for these methods is that it's typically used via JSON.stringify(tx), where there are no arguments.

What's an example of how the input is being logged?

@braydonf
Copy link
Contributor

Okay, closing as toJSON is specific to JSON.stringify and doesn't include arguments. There would need to be a network property in address, and potentially others, to resolve that case (similar to #720), if the network is not set in the global correctly.

@braydonf braydonf closed this Mar 26, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants