{"size": 1972, "ext": "js", "max_stars_repo_path": "lib/models/txid.js", "max_stars_repo_name": "braydonf/bwsv2", "max_stars_repo_stars_event_min_datetime": "2016-10-07T18:41:10.000Z", "max_stars_repo_stars_event_max_datetime": "2022-02-09T10:28:06.000Z", "max_issues_repo_path": "lib/models/txid.js", "max_issues_repo_name": "braydonf/bwsv2", "max_issues_repo_issues_event_min_datetime": "2016-08-02T19:48:34.000Z", "max_issues_repo_issues_event_max_datetime": "2018-09-12T14:19:44.000Z", "max_forks_repo_path": "lib/models/txid.js", "max_forks_repo_name": "braydonf/bwsv2", "max_forks_repo_forks_event_min_datetime": "2016-08-01T14:47:26.000Z", "max_forks_repo_forks_event_max_datetime": "2022-01-09T15:19:33.000Z"}
{"max_stars_count": 27, "max_issues_count": 8, "max_forks_count": 23, "avg_line_length": 27.3888888889, "max_line_length": 101, "alphanum_fraction": 0.6749492901}
'use strict';

var assert = require('assert');
var bitcore = require('bitcore-lib');

/**
 * @param {Buffer} walletId - The wallet id
 * @param {Object} options
 * @param {Number} options.height - The block height of the txid
 * @param {Number} options.index - The transaction index in the block
 * @param {Buffer} optinos.value - The transaction id
 */
function WalletTxid(walletId, options) {
  if (!(this instanceof WalletTxid)) {
    return new WalletTxid(walletId, options);
  }
  if (!options) {
    options = {};
  }
  this._initWalletId(walletId);
  this.height = options.height;
  this.index = options.index;
  if (options.value && !Buffer.isBuffer(options.value)) {
    this.value = new Buffer(options.value, 'hex');
  } else {
    this.value = options.value;
  }
}

WalletTxid.prototype._initWalletId = function(walletId) {
  if (Buffer.isBuffer(walletId)) {
    assert(walletId.length === 32, '"id" buffer is expected to be 32 bytes');
    this.walletId = walletId;
  } else {
    assert(bitcore.util.js.isHexa(walletId), '"id" is expected to be a hexa string if not a buffer');
    assert(walletId.length === 64, '"id" string is expected to have length of 64');
    this.walletId = new Buffer(walletId, 'hex');
  }
};

WalletTxid.create = function(walletId, height, index, value) {
  return new WalletTxid(walletId, {
    height: height,
    index: index,
    value: value
  });
};

WalletTxid.parseKey = function(key) {
  assert(Buffer.isBuffer(key), '"key" is expected to be a buffer');
  var result = {
    walletId: key.slice(0, 32),
    height: key.readUInt32BE(32),
    index: key.readUInt32BE(36)
  };
  return result;
};

WalletTxid.prototype.getKey = function() {
  var buffer = new Buffer(new Array(8));
  buffer.writeUInt32BE(this.height);
  buffer.writeUInt32BE(this.index, 4);
  var key = Buffer.concat([this.walletId, buffer]);
  return key;
};

WalletTxid.prototype.getValue = function() {
  return this.value;
};

module.exports = WalletTxid;
