bitpay / bitcore-explorers

Blockchain APIs for bitcore
http://bitcore.io
MIT License
67 stars 95 forks source link

Convey information about network confirmations when calling getUnspentUtxos #22

Open bilthon opened 8 years ago

bilthon commented 8 years ago

When querying for unspent transactions, one key information to be presented is how old it is. The getUnspentUtxos method from the Insight class returns an array of UnspentOutput objects.

The problem with this approach is that the important time information is capped from the raw HTTP response. Since the UnspentOutput object only have immutable fields the "timestamp" and "confirmation" attributes from the original response are left out when doing this:

unspent = _.map(unspent, UnspentOutput);

I see two ways to improve this method. We either add the "timestamp" and "confirmation" attributes to the UnspentOutput, or the getUnspentUtxos gets a new optional boolean parameter that when set to true will return the raw HTTP response. The former method will return a more complete version of the UsnpentOutput object. I don't see how this could break existing code. The latter will allow the caller to access these information without having to touch the UnspentOutput definition.

This would work like this:

Insight.prototype.getUnspentUtxos = function(addresses, raw, callback) {
  if(typeof(raw) == 'function'){
    callaback = raw
  }
  ...
  if(!raw || typeof(raw) == 'function')
    unspent = _.map(unspent, UnspentOutput);
}

This way we don't break anything that already uses this format of the API passing the callback as the second argument. And also provide a way to return the raw response if needed.

I'll be happy to submit a pull request with either modifications. But would like some opinions first.

Best regards Nelson