ethers.js/docs/api/utils/bytes/index.html

45 lines
19 KiB
HTML
Raw Normal View History

2019-08-22 08:52:17 +03:00
<html><head><title>Byte Manipulation</title><link rel="stylesheet" type="text/css" href="/static/style.css"></head><body><div class="sidebar"><div class="header"><div class="logo"><a href="/"><div class="image"></div><div class="name">ethers</div><div class="version">v5.0-beta</div></a></div></div><div class="toc"><div><div class="link title"><a href="/">Documentation</a></div><div class="base show link depth-1"><a href="/getting-started/">Getting Started</a></div><div class="hide link depth-2"><a href="/getting-started/#installing">Installing</a></div><div class="hide link depth-2"><a href="/getting-started/#importing">Importing</a></div><div class="base show link depth-1"><a href="/concepts/">Concepts</a></div><div class="hide link depth-2"><a href="/concepts/events/">Events</a></div><div class="hide link depth-2"><a href="/concepts/gas/">Gas</a></div><div class="hide link depth-3"><a href="/concepts/gas/#gas-price">Gas Price</a></div><div class="hide link depth-3"><a href="/concepts/gas/#gas-limit">Gas Limit</a></div><div class="base ancestor show link depth-1"><a href="/api/">Application Programming Interface</a></div><div class="show link depth-2"><a href="/api/contract/">Contracts</a></div><div class="hide link depth-3"><a href="/api/contract/#buckets">Buckets</a></div><div class="show link depth-2"><a href="/api/signer/">Signers</a></div><div class="hide link depth-3"><a href="/api/signer/#signer">Signer</a></div><div class="hide link depth-3"><a href="/api/signer/#wallet-inherits-signer">Wallet inherits Signer</a></div><div class="show link depth-2"><a href="/api/providers/">Providers</a></div><div class="hide link depth-3"><a href="/api/providers/provider/">Provider</a></div><div class="hide link depth-4"><a href="/api/providers/provider/#accounts-methods">Accounts Methods</a></div><div class="hide link depth-4"><a href="/api/providers/provider/#blocks-methods">Blocks Methods</a></div><div class="hide link depth-4"><a href="/api/providers/provider/#ethereum-naming-service-(ens)-methods">Ethereum Naming Service (ENS) Methods</a></div><div class="hide link depth-4"><a href="/api/providers/provider/#logs-methods">Logs Methods</a></div><div class="hide link depth-4"><a href="/api/providers/provider/#network-status-methods">Network Status Methods</a></div><div class="hide link depth-4"><a href="/api/providers/provider/#transactions-methods">Transactions Methods</a></div><div class="hide link depth-4"><a href="/api/providers/provider/#event-emitter-methods">Event Emitter Methods</a></div><div class="hide link depth-4"><a href="/api/providers/provider/#inspection-methods">Inspection Methods</a></div><div class="hide link depth-3"><a href="/api/providers/jsonrpc-provider/">JSON-RPC Provider</a></div><div class="hide link depth-4"><a href="/api/providers/jsonrpc-provider/#jsonrpcprovider">JsonRpcProvider</a></div><div class="hide link depth-4"><a href="/api/providers/jsonrpc-provider/#jsonrpcsigner">JsonRpcSigner</a></div><div class="hide link depth-4"><a href="/api/providers/jsonrpc-provider/#jsonrpcuncheckedsigner">JsonRpcUncheckedSigner</a></div><div class="hide link depth-3"><a href="/api/providers/api-providers/">API Providers</a></div><div class="hide link depth-4"><a href="/api/providers/api-providers/#etherscanprovider">EtherscanProvider</a></div><div class="hide link depth-4"><a href="/api/providers/api-providers/#infuraprovider">InfuraProvider</a></div><div class="hide link depth-4"><a href="/api/providers/api-providers/#nodesmithprovider">NodesmithProvider</a></div><div class="hide link depth-4"><a href="/api/providers/api-providers/#alchemyprovider">AlchemyProvider</a></div><div class="hide link depth-3"><a href="/api/providers/other/">Other Providers</a></div><div class="hide link depth-4"><a href="/api/providers/other/#fallbackprovider">FallbackProvider</a></div><div class="hide link depth-4"><a href="/api/providers/other/#ipcprovider">IpcProvider</a></div><div class="hide link depth-3"><a href="/api/providers/types/">Types</a></div><div class="hide link depth-4"><a href="/api/providers/types/#blocks">Bloc
<a name="byte-manipulation"></a><h1>Byte Manipulation</h1>
<p>Tra la la...</p>
<a name="types"></a><h2>Types</h2>
<a name="bytes"></a><a name="bytes"></a><h3>Bytes</h3>
<p>A Bytes object is any object which is an <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array">Array</a> or <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray">TypedArray</a> with each value in the valid byte range (i.e. between 0 and 255 inclusive), or is an Object with a <code class="inline">length</code> property where each indexed property is in the valid byte range.</p>
<a name="byteslike"></a><a name="byteslike"></a><h3>BytesLike</h3>
<p>A <b>BytesLike</b> can be either a <a href="/api/utils/bytes/#bytes">Bytes</a> or a <a href="/api/utils/bytes/#hexstring">Hexstring</a>.</p>
<a name="hexstring"></a><a name="hexstring"></a><h3>Hexstring</h3>
<p>A <b>hexstring</b> is a string which has a <code class="inline">0x</code> prefix followed by</p>
<a name="signature"></a><a name="signature"></a><h3>Signature</h3>
<ul><li><b>r</b> and <b>s</b> &mdash; The x co-ordinate of <b>r</b> and the <b>s</b> value of the signature</li><li><b>v</b> &mdash; The parity of the y co-ordinate of <b>r</b></li><li><b>_vs</b> &mdash; The <a href="https://link_here">compact representation</a> of the <b>(r, s)</b> and <b>v</b></li><li><b>recoveryParam</b> &mdash; The normalized (i.e. 0 or 1) value of <b>v</b></li></ul>
<a name="signaturelike"></a><a name="signaturelike"></a><h3>SignatureLike</h3>
<p>A <b>SignatureLike</b> is similar to a <a href="/api/utils/bytes/#signature">Signature</a>, except redundant properties may be omitted.</p><p>For example, if *_vs* is specified, <b>(r, s)</b> and <b>v</b> can be omitted. Likewise, if <b>recoverParam</b> is provided, <b>v</b> can be omitted (as it can be computed).</p>
<a name="inspection"></a><h2>Inspection</h2>
<div class="property"><div class="signature"><span class="path">utils</span> . <span class="method">isBytes</span> ( <span class="param">object</span> ) <span class="arrow">&rArr;</span> <span class="returns">boolean</span></div><div class="body"><p>Returns true if and only if <i>object</i> is a valid <a href="/api/utils/bytes/#bytes">Bytes</a>.</p></div></div>
<div class="property"><div class="signature"><span class="path">utils</span> . <span class="method">isBytesLike</span> ( <span class="param">object</span> ) <span class="arrow">&rArr;</span> <span class="returns">boolean</span></div><div class="body"><p>Returns true if and only if <i>object</i> is a <a href="/api/utils/bytes/#bytes">Bytes</a> or an Array or TypedArray where each value is a valid byte (i.e. between 0 and 255 inclusive).</p></div></div>
<div class="property"><div class="signature"><span class="path">utils</span> . <span class="method">isHexString</span> ( <span class="param">object</span> , [ <span class="param">length</span> ] ) <span class="arrow">&rArr;</span> <span class="returns">boolean</span></div><div class="body"><p>Returns true if and only if <i>object</i> is a valid hex string; if <i>length</i> is specified the length (in bytes) is also verified.</p></div></div>
<a name="converting-between-arrays-and-hexstrings"></a><h2>Converting between Arrays and Hexstrings</h2>
<div class="property"><div class="signature"><span class="path">utils</span> . <span class="method">arrayify</span> ( <span class="param">hexstringOrArrayish</span> [ , <span class="param">options</span> ] ) <span class="arrow">&rArr;</span> <span class="returns">Uint8Array</span></div><div class="body"><p>Converts <i>hexstringOrArrayish</i> to a Uint8Array. If a <a href="/api/utils/bytes/#hexstring">Hexstring</a> is passed in, the length must be even.</p></div></div>
<div class="property"><div class="signature"><span class="path">utils</span> . <span class="method">hexlify</span> ( <span class="param">hexstringOrArrayish</span> ) <span class="arrow">&rArr;</span> <span class="returns">string</span></div><div class="body"><p>Converts <i>hexstringOrArrayish</i> to a <a href="/api/utils/bytes/#hexstring">Hexstring</a>. The result will always be zero-padded to even length.</p></div></div>
<div class="property"><div class="signature"><span class="path">utils</span> . <span class="method">hexValue</span> ( <span class="param">aBigNumberish</span> ) <span class="arrow">&rArr;</span> <span class="returns">string</span></div><div class="body"><p>Converts <i>aBigNumberish</i> to a <a href="/api/utils/bytes/#hexstring">Hexstring</a>, with no unecessary leading zeros. The result of this function can be of odd-length.</p></div></div>
<a name="examples"></a><h3>Examples</h3>
2019-08-22 08:52:17 +03:00
<div class="code"><br><span class="comment">// Convert a hexstring to a Uint8Array</span><br>arrayify("0x1234")<br><span class="result ok">// [ 18, 52 ]</span><br><br><span class="comment">// Convert an Array to a hexstring</span><br>hexlify([1, 2, 3, 4])<br><span class="result ok">// 0x01020304</span><br><br><span class="comment">// Convert an Object to a hexstring</span><br>hexlify({ length: 2, "0": 1, "1": 2 })<br><span class="result ok">// 0x0102</span><br><br><span class="comment">// Convert an Array to a hexstring</span><br>hexlify([ 1 ])<br><span class="result ok">// 0x01</span><br><br><span class="comment">// Convert a number to a stripped hex value</span><br>hexValue(1)<br><span class="result ok">// 0x1</span><br><br><span class="comment">// Convert an Array to a stripped hex value</span><br>hexValue([ 1, 2 ])<br><span class="result ok">// 0x102</span><br></div>
<a name="array-manipulation"></a><h2>Array Manipulation</h2>
<div class="property"><div class="signature"><span class="path">utils</span> . <span class="method">concat</span> ( <span class="param">arrayOfBytesLike</span> ) <span class="arrow">&rArr;</span> <span class="returns">Uint8Array</span></div><div class="body"><p>Concatenates all the <a href="/api/utils/bytes/#byteslike">BytesLike</a> in <i>arrayOfBytesLike</i> into a single Uint8Array.</p></div></div>
<div class="property"><div class="signature"><span class="path">utils</span> . <span class="method">stripZeros</span> ( <span class="param">aBytesLike</span> ) <span class="arrow">&rArr;</span> <span class="returns">Uint8Array</span></div><div class="body"><p>Concatenates all the <a href="/api/utils/bytes/#byteslike">BytesLike</a> in <i>arrayOfBytesLike</i></p></div></div>
<div class="property"><div class="signature"><span class="path">utils</span> . <span class="method">zeroPad</span> ( <span class="param">aBytesLike</span> , <span class="param">length</span> ) <span class="arrow">&rArr;</span> <span class="returns">Uint8Array</span></div><div class="body"><p>Concatenates all the <a href="/api/utils/bytes/#byteslike">BytesLike</a> in <i>arrayOfBytesLike</i></p></div></div>
<a name="hexstring-manipulation"></a><h2>Hexstring Manipulation</h2>
<div class="property"><div class="signature"><span class="path">utils</span> . <span class="method">hexConcat</span> ( <span class="param">arrayOfBytesLike</span> ) <span class="arrow">&rArr;</span> <span class="returns">string</span></div><div class="body"><p>Concatenates all the <a href="/api/utils/bytes/#byteslike">BytesLike</a> in <i>arrayOfBytesLike</i> into a single <a href="/api/utils/bytes/#hexstring">Hexstring</a></p></div></div>
<div class="property"><div class="signature"><span class="path">utils</span> . <span class="method">hexDataLength</span> ( <span class="param">aBytesLike</span> ) <span class="arrow">&rArr;</span> <span class="returns">number</span></div><div class="body"><p>Returns the length (in bytes) of <i>aBytesLike</i>.</p><p>This will <b>throw and error</b> if <i>aBytesLike</i> is a <a href="/api/utils/bytes/#hexstring">Hexstring</a> but is of odd-length.</p></div></div>
<div class="property"><div class="signature"><span class="path">utils</span> . <span class="method">hexDataSlice</span> ( <span class="param">aBytesLike</span> , <span class="param">offset</span> [ , <span class="param">endOffset</span> ] ) <span class="arrow">&rArr;</span> <span class="returns">string</span></div><div class="body"><p>Returns the length (in bytes) of <i>aBytesLike</i>.</p></div></div>
<div class="property"><div class="signature"><span class="path">utils</span> . <span class="method">hexStripZeros</span> ( <span class="param">aBytesLike</span> ) <span class="arrow">&rArr;</span> <span class="returns">string</span></div><div class="body"><p>@TODO</p></div></div>
<div class="property"><div class="signature"><span class="path">utils</span> . <span class="method">hexZeroPad</span> ( <span class="param">aBytesLike</span> , <span class="param">length</span> ) <span class="arrow">&rArr;</span> <span class="returns">string</span></div><div class="body"><p>@TODO</p></div></div>
<a name="signature-conversion"></a><h2>Signature Conversion</h2>
<div class="property"><div class="signature"><span class="path">utils</span> . <span class="method">joinSignature</span> ( <span class="param">aSignatureLike</span> ) <span class="arrow">&rArr;</span> <span class="returns">string</span></div><div class="body"><p>Return the flat-format of a <a href="/api/utils/bytes/#signaturelike">SignatureLike</a>, which is 65 bytes (130 nibbles) long, concatenating the <b>r</b>, <b>s</b> and <b>v</b> of a Signature.</p></div></div>
2019-08-22 08:52:17 +03:00
<div class="property"><div class="signature"><span class="path">utils</span> . <span class="method">splitSignature</span> ( <span class="param">aSignatureLikeOrBytesLike</span> ) <span class="arrow">&rArr;</span> <span class="returns">Signature</span></div><div class="body"><p>Return the full expanded-format of a <a href="/api/utils/bytes/#signaturelike">SignatureLike</a> or a flat-format <a href="/api/utils/bytes/#hexstring">Hexstring</a>. Any missing properties will be computed.</p></div></div><div class="footer"><div class="nav previous"><a href="/api/utils/bignumber/"><span class="arrow">&larr;</span>BigNumber</a></div> <div class="nav next"><a href="/api/utils/constants/">Constants<span class="arrow">&rarr;</span></a></div></div><div class="copyright">The content of this site is licensed under the <a href="https://choosealicense.com/licenses/cc-by-4.0/">Creative Commons Attribution 4.0 International License</a>.</div></div><script src="/script.js" type="text/javascript"></script></body></html><!-- ContentHash:07938d148295667a14f2c5a06dddf2fc179b3b8a326bd000f82efa641da15d53 -->