You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

358 lines
23 KiB

8 years ago
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: primitives/outpoint.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: primitives/outpoint.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>/*!
* outpoint.js - outpoint object for bcoin
* Copyright (c) 2014-2017, Christopher Jeffrey (MIT License).
* https://github.com/bcoin-org/bcoin
*/
'use strict';
var util = require('../utils/util');
var assert = require('assert');
var StaticWriter = require('../utils/writer');
var BufferReader = require('../utils/reader');
var encoding = require('../utils/encoding');
/**
* Represents a COutPoint.
* @alias module:primitives.Outpoint
* @constructor
* @param {Hash?} hash
* @param {Number?} index
* @property {Hash} hash
* @property {Number} index
*/
function Outpoint(hash, index) {
if (!(this instanceof Outpoint))
return new Outpoint(hash, index);
this.hash = encoding.NULL_HASH;
this.index = 0xffffffff;
if (hash != null) {
assert(typeof hash === 'string', 'Hash must be a string.');
assert(util.isUInt32(index), 'Index must be a uint32.');
this.hash = hash;
this.index = index;
}
}
/**
* Inject properties from options object.
* @private
* @param {Object} options
*/
Outpoint.prototype.fromOptions = function fromOptions(options) {
assert(options, 'Outpoint data is required.');
assert(typeof options.hash === 'string', 'Hash must be a string.');
assert(util.isUInt32(options.index), 'Index must be a uint32.');
this.hash = options.hash;
this.index = options.index;
return this;
};
/**
* Instantate outpoint from options object.
* @param {Object} options
* @returns {Outpoint}
*/
Outpoint.fromOptions = function fromOptions(options) {
return new Outpoint().fromOptions(options);
};
/**
* Test whether the outpoint is null (hash of zeroes
* with max-u32 index). Used to detect coinbases.
* @returns {Boolean}
*/
Outpoint.prototype.isNull = function isNull() {
return this.index === 0xffffffff &amp;&amp; this.hash === encoding.NULL_HASH;
};
/**
* Get little-endian hash.
* @returns {Hash}
*/
Outpoint.prototype.rhash = function rhash() {
return util.revHex(this.hash);
};
/**
* Get little-endian hash.
* @returns {Hash}
*/
Outpoint.prototype.txid = function txid() {
return this.rhash();
};
/**
* Serialize outpoint to a key
* suitable for a hash table.
* @returns {String}
*/
Outpoint.prototype.toKey = function toKey() {
return Outpoint.toKey(this.hash, this.index);
};
/**
* Inject properties from hash table key.
* @private
* @param {String} key
* @returns {Outpoint}
*/
Outpoint.prototype.fromKey = function fromKey(key) {
assert(key.length > 64);
this.hash = key.slice(0, 64);
this.index = +key.slice(64);
return this;
};
/**
* Instantiate outpoint from hash table key.
* @param {String} key
* @returns {Outpoint}
*/
Outpoint.fromKey = function fromKey(key) {
return new Outpoint().fromKey(key);
};
/**
* Write outpoint to a buffer writer.
* @param {BufferWriter} bw
*/
Outpoint.prototype.toWriter = function toWriter(bw) {
bw.writeHash(this.hash);
bw.writeU32(this.index);
return bw;
};
/**
* Calculate size of outpoint.
* @returns {Number}
*/
Outpoint.prototype.getSize = function getSize() {
return 36;
};
/**
* Serialize outpoint.
* @returns {Buffer}
*/
Outpoint.prototype.toRaw = function toRaw() {
return this.toWriter(new StaticWriter(36)).render();
};
/**
* Inject properties from buffer reader.
* @private
* @param {BufferReader} br
*/
Outpoint.prototype.fromReader = function fromReader(br) {
this.hash = br.readHash('hex');
this.index = br.readU32();
return this;
};
/**
* Inject properties from serialized data.
* @private
* @param {Buffer} data
*/
Outpoint.prototype.fromRaw = function fromRaw(data) {
return this.fromReader(new BufferReader(data));
};
/**
* Instantiate outpoint from a buffer reader.
* @param {BufferReader} br
* @returns {Outpoint}
*/
Outpoint.fromReader = function fromReader(br) {
return new Outpoint().fromReader(br);
};
/**
* Instantiate outpoint from serialized data.
* @param {Buffer} data
* @returns {Outpoint}
*/
Outpoint.fromRaw = function fromRaw(data) {
return new Outpoint().fromRaw(data);
};
/**
* Inject properties from json object.
* @private
* @params {Object} json
*/
Outpoint.prototype.fromJSON = function fromJSON(json) {
assert(json, 'Outpoint data is required.');
assert(typeof json.hash === 'string', 'Hash must be a string.');
assert(util.isUInt32(json.index), 'Index must be a uint32.');
this.hash = util.revHex(json.hash);
this.index = json.index;
return this;
};
/**
* Convert the outpoint to an object suitable
* for JSON serialization. Note that the hash
* will be reversed to abide by bitcoind's legacy
* of little-endian uint256s.
* @returns {Object}
*/
Outpoint.prototype.toJSON = function toJSON() {
return {
hash: util.revHex(this.hash),
index: this.index
};
};
/**
* Instantiate outpoint from json object.
* @param {Object} json
* @returns {Outpoint}
*/
Outpoint.fromJSON = function fromJSON(json) {
return new Outpoint().fromJSON(json);
};
/**
* Inject properties from tx.
* @private
* @param {TX} tx
* @param {Number} index
*/
Outpoint.prototype.fromTX = function fromTX(tx, index) {
assert(tx);
assert(typeof index === 'number');
assert(index >= 0);
this.hash = tx.hash('hex');
this.index = index;
return this;
};
/**
* Instantiate outpoint from tx.
* @param {TX} tx
* @param {Number} index
* @returns {Outpoint}
*/
Outpoint.fromTX = function fromTX(tx, index) {
return new Outpoint().fromTX(tx, index);
};
/**
* Serialize outpoint to a key
* suitable for a hash table.
* @param {Hash} hash
* @param {Number} index
* @returns {String}
*/
Outpoint.toKey = function toKey(hash, index) {
assert(typeof hash === 'string');
assert(hash.length === 64);
assert(index >= 0);
return hash + index;
};
/**
* Convert the outpoint to a user-friendly string.
* @returns {String}
*/
Outpoint.prototype.inspect = function inspect() {
return '&lt;Outpoint: ' + this.rhash() + '/' + this.index + '>';
};
/**
* Test an object to see if it is an outpoint.
* @param {Object} obj
* @returns {Boolean}
*/
Outpoint.isOutpoint = function isOutpoint(obj) {
return obj
&amp;&amp; typeof obj.hash === 'string'
&amp;&amp; typeof obj.index === 'number'
&amp;&amp; typeof obj.toKey === 'function';
};
/*
* Expose
*/
module.exports = Outpoint;
</code></pre>
</article>
</section>
</div>
<nav>
8 years ago
<h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-bcoin.html">bcoin</a></li><li><a href="module-bip70.html">bip70</a></li><li><a href="module-bip70_pk.html">bip70/pk</a></li><li><a href="module-bip70_x509.html">bip70/x509</a></li><li><a href="module-blockchain.html">blockchain</a></li><li><a href="module-blockchain_common.html">blockchain/common</a></li><li><a href="module-btc.html">btc</a></li><li><a href="module-coins.html">coins</a></li><li><a href="module-crypto.html">crypto</a></li><li><a href="module-crypto_chachapoly.html">crypto/chachapoly</a></li><li><a href="module-crypto_ec.html">crypto/ec</a></li><li><a href="module-crypto_pk.html">crypto/pk</a></li><li><a href="module-crypto_schnorr.html">crypto/schnorr</a></li><li><a href="module-crypto_siphash.html">crypto/siphash</a></li><li><a href="module-db.html">db</a></li><li><a href="module-hd.html">hd</a></li><li><a href="module-http.html">http</a></li><li><a href="module-mempool.html">mempool</a></li><li><a href="module-mining.html">mining</a></li><li><a href="module-net.html">net</a></li><li><a href="module-net_bip152.html">net/bip152</a></li><li><a href="module-net_common.html">net/common</a></li><li><a href="module-net_dns.html">net/dns</a></li><li><a href="module-net_packets.html">net/packets</a></li><li><a href="module-net_socks.html">net/socks</a></li><li><a href="module-net_tcp.html">net/tcp</a></li><li><a href="module-node.html">node</a></li><li><a href="module-node_config.html">node/config</a></li><li><a href="module-primitives.html">primitives</a></li><li><a href="module-protocol.html">protocol</a></li><li><a href="module-protocol_consensus.html">protocol/consensus</a></li><li><a href="module-protocol_errors.html">protocol/errors</a></li><li><a href="module-protocol_networks.html">protocol/networks</a></li><li><a href="module-protocol_policy.html">protocol/policy</a></li><li><a href="module-script.html">script</a></li><li><a href="module-script_common.html">script/common</a></li><li><a href="module-utils.html">utils</a></li><li><a href="module-utils_asn1.html">utils/asn1</a></li><li><a href="module-utils_base32.html">utils/base32</a></li><li><a href="module-utils_base58.html">utils/base58</a></li><li><a href="module-utils_co.html">utils/co</a></li><li><a href="module-utils_encoding.html">utils/encoding</a></li><li><a href="module-utils_ip.html">utils/ip</a></li><li><a href="module-utils_pem.html">utils/pem</a></li><li><a href="module-utils_protobuf.html">utils/protobuf</a></li><li><a href="module-utils_util.html">utils/util</a></li><li><a href="module-wallet.html">wallet</a></li><li><a href="module-wallet_common.html">wallet/common</a></li><li><a href="module-wallet_records.html">wallet/records</a></li><li><a href="module-workers.html">workers</a></li><li><a href="module-workers_jobs.html">workers/jobs</a></li><li><a href="module-workers_packets.html">workers/packets</a></li></ul><h3>Classes</h3><ul><li><a href="Environment.html">Environment</a></li><li><a href="module-bip70.Payment.html">Payment</a></li><li><a href="module-bip70.PaymentACK.html">PaymentACK</a></li><li><a href="module-bip70.PaymentDetails.html">PaymentDetails</a></li><li><a href="module-bip70.PaymentRequest.html">PaymentRequest</a></li><li><a href="module-blockchain.Chain.html">Chain</a></li><li><a href="module-blockchain.ChainDB.html">ChainDB</a></li><li><a href="module-blockchain.ChainEntry.html">ChainEntry</a></li><li><a href="module-blockchain.ChainFlags.html">ChainFlags</a></li><li><a href="module-blockchain.ChainOptions.html">ChainOptions</a></li><li><a href="module-blockchain.ChainState.html">ChainState</a></li><li><a href="module-blockchain.DeploymentState.html">DeploymentState</a></li><li><a href="module-blockchain.StateCache.html">StateCache</a></li><li><a href="module-btc.Amount.html">Amount</a></li><li><a href="module-btc.URI.html">URI</a></li><li><a href="module-coins.CoinEntry.html">CoinEntry</a></li><li><a href="module-coins.Coins.html">Coins</a></li><li><a href="module-coins.CoinView.html">CoinView</a></li><li><a href="module-coins.UndoCoi
8 years ago
</nav>
<br class="clear">
<footer>
8 years ago
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Fri Feb 10 2017 09:40:23 GMT-0800 (PST)
8 years ago
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>