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

52 lines
34 KiB
HTML
Raw Normal View History

2020-02-18 01:56:50 +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-3"><a href="/concepts/events/#solidity-topics">Solidity Topics</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">Wallet</a></div><div class="hide link depth-3"><a href="/api/signer/#voidsigner">VoidSigner</a></div><div class="hide link depth-3"><a href="/api/signer/#externallyownedaccount">ExternallyOwnedAccount</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/">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/#alchemyprovider">AlchemyProvider</a></div><div class="hide link depth-4"><a href="/api/providers/api-providers/#cloudfrontprovider">CloudfrontProvider</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
2019-12-14 06:05:10 +03:00
<a name="byte-manipulation"></a><h1 class="show-anchors"><div>Byte Manipulation<div class="anchors"><a class="self" href="#byte-manipulation"></a></div></div></h1>
<p>Tra la la...</p>
2019-12-14 06:05:10 +03:00
<a name="types"></a><h2 class="show-anchors"><div>Types<div class="anchors"><a class="self" href="#types"></a></div></div></h2>
<a name="bytes"></a><a name="bytes"></a><h3 class="show-anchors"><div>Bytes<div class="anchors"><a class="self" href="#bytes"></a></div></div></h3>
<p>A <b>Bytes</b> 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 class="show-anchors"><div>BytesLike<div class="anchors"><a class="self" href="#byteslike"></a></div></div></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/#datahexstring">DataHexstring</a>.</p>
<a name="datahexstring"></a><a name="datahexstring"></a><h3 class="show-anchors"><div>DataHexstring<div class="anchors"><a class="self" href="#datahexstring"></a></div></div></h3>
<p>A <b>DataHexstring</b> is identical to a <a href="/api/utils/bytes/#hexstring">Hexstring</a> except that it has an even number of nibbles, and therefore is a valid representation of binary data as a string.</p>
<a name="hexstring"></a><a name="hexstring"></a><h3 class="show-anchors"><div>Hexstring<div class="anchors"><a class="self" href="#hexstring"></a></div></div></h3>
2020-02-02 08:53:22 +03:00
<p>A <b>Hexstring</b> is a string which has a <code class="inline">0x</code> prefix followed by any number of nibbles (i.e. case-insensitive hexidecumal characters, <code class="inline">0-9</code> and <code class="inline">a-f</code>).</p>
2019-12-14 06:05:10 +03:00
<a name="signature"></a><a name="signature"></a><h3 class="show-anchors"><div>Signature<div class="anchors"><a class="self" href="#signature"></a></div></div></h3>
2020-02-02 08:53:22 +03:00
<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://eips.ethereum.org/EIPS/eip-2098">compact representation</a> of the <b>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="signature-flat"></a><a name="flat-format-signature"></a><h3 class="show-anchors"><div>Flat-Format Signature<div class="anchors"><a class="self" href="#signature-flat"></a></div></div></h3>
<p>A <b>Flat-Format Signature</b> is a common Signature format where the r, s and v are concanenated into a 65 byte (130 nibble) <a href="/api/utils/bytes/#datahexstring">DataHexstring</a>.</p>
2019-12-14 06:05:10 +03:00
<a name="signaturelike"></a><a name="signaturelike"></a><h3 class="show-anchors"><div>SignatureLike<div class="anchors"><a class="self" href="#signaturelike"></a></div></div></h3>
2020-02-02 08:53:22 +03:00
<p>A <b>SignatureLike</b> is similar to a <a href="/api/utils/bytes/#signature">Signature</a>, except redundant properties may be omitted or it may be a <a href="/api/utils/bytes/#signature-flat">Flat-Format Signature</a>.</p><p>For example, if <b>_vs</b> is specified, <b>s</b> and <b>v</b> may be omitted. Likewise, if <b>recoveryParam</b> is provided, <b>v</b> may be omitted (as in these cases the missing values can be computed).</p>
2019-12-14 06:05:10 +03:00
<a name="inspection"></a><h2 class="show-anchors"><div>Inspection<div class="anchors"><a class="self" href="#inspection"></a></div></div></h2>
<a name="utils-isbytes"></a><div class="property show-anchors"><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 class="anchors"><a class="self" href="#utils-isbytes"></a><a class="source" href="https://github.com/ethers-io/ethers.js/blob/ethers-v5-beta/packages/bytes/src.ts/index.ts#L73">source</a></div></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>
<a name="utils-isbyteslike"></a><div class="property show-anchors"><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 class="anchors"><a class="self" href="#utils-isbyteslike"></a><a class="source" href="https://github.com/ethers-io/ethers.js/blob/ethers-v5-beta/packages/bytes/src.ts/index.ts#L69">source</a></div></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 <a href="/api/utils/bytes/#datahexstring">DataHexstring</a>.</p></div></div>
<a name="utils-ishexstring"></a><div class="property show-anchors"><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 class="anchors"><a class="self" href="#utils-ishexstring"></a><a class="source" href="https://github.com/ethers-io/ethers.js/blob/ethers-v5-beta/packages/bytes/src.ts/index.ts#L184">source</a></div></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 and <i>object</i> is not a valid <a href="/api/utils/bytes/#datahexstring">DataHexstring</a> of <i>length</i> bytes, an InvalidArgument error is thrown.</p></div></div>
<a name="converting-between-arrays-and-hexstrings"></a><h2 class="show-anchors"><div>Converting between Arrays and Hexstrings<div class="anchors"><a class="self" href="#converting-between-arrays-and-hexstrings"></a></div></div></h2>
<a name="utils-arrayify"></a><div class="property show-anchors"><div class="signature"><span class="path">utils</span> . <span class="method">arrayify</span> ( <span class="param">datahexstringOrArrayish</span> [ , <span class="param">options</span> ] ) <span class="arrow">&rArr;</span> <span class="returns">Uint8Array</span><div class="anchors"><a class="self" href="#utils-arrayify"></a><a class="source" href="https://github.com/ethers-io/ethers.js/blob/ethers-v5-beta/packages/bytes/src.ts/index.ts#L91">source</a></div></div><div class="body"><p>Converts <i>datahexstringOrArrayish</i> to a Uint8Array.</p></div></div>
2020-02-02 08:53:22 +03:00
<a name="utils-hexlify"></a><div class="property show-anchors"><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&lt;&thinsp;<a href="/api/utils/bytes/#datahexstring">DataHexstring</a>&thinsp;&gt;</span><div class="anchors"><a class="self" href="#utils-hexlify"></a><a class="source" href="https://github.com/ethers-io/ethers.js/blob/ethers-v5-beta/packages/bytes/src.ts/index.ts#L194">source</a></div></div><div class="body"><p>Converts <i>hexstringOrArrayish</i> to a <a href="/api/utils/bytes/#datahexstring">DataHexstring</a>.</p></div></div>
<a name="utils-hexvalue"></a><div class="property show-anchors"><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&lt;&thinsp;<a href="/api/utils/bytes/#hexstring">Hexstring</a>&thinsp;&gt;</span><div class="anchors"><a class="self" href="#utils-hexvalue"></a><a class="source" href="https://github.com/ethers-io/ethers.js/blob/ethers-v5-beta/packages/bytes/src.ts/index.ts#L287">source</a></div></div><div class="body"><p>Converts <i>aBigNumberish</i> to a <a href="/api/utils/bytes/#hexstring">Hexstring</a>, with no <u>unnecessary</u> leading zeros.</p></div></div>
2019-12-14 06:05:10 +03:00
<a name="examples"></a><h3 class="show-anchors"><div>Examples<div class="anchors"><a class="self" href="#examples"></a></div></div></h3>
2020-02-18 01:56:50 +03:00
<div class="code">Skipping JavaScript Evaluation.</div>
2019-12-14 06:05:10 +03:00
<a name="array-manipulation"></a><h2 class="show-anchors"><div>Array Manipulation<div class="anchors"><a class="self" href="#array-manipulation"></a></div></div></h2>
<a name="utils-concat"></a><div class="property show-anchors"><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 class="anchors"><a class="self" href="#utils-concat"></a><a class="source" href="https://github.com/ethers-io/ethers.js/blob/ethers-v5-beta/packages/bytes/src.ts/index.ts#L140">source</a></div></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>
<a name="utils-stripzeros"></a><div class="property show-anchors"><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 class="anchors"><a class="self" href="#utils-stripzeros"></a><a class="source" href="https://github.com/ethers-io/ethers.js/blob/ethers-v5-beta/packages/bytes/src.ts/index.ts#L154">source</a></div></div><div class="body"><p>Returns a Uint8Array with all leading <code class="inline">0</code> bytes of <i>aBtyesLike</i> removed.</p></div></div>
<a name="utils-zeropad"></a><div class="property show-anchors"><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 class="anchors"><a class="self" href="#utils-zeropad"></a><a class="source" href="https://github.com/ethers-io/ethers.js/blob/ethers-v5-beta/packages/bytes/src.ts/index.ts#L171">source</a></div></div><div class="body"><p>Retutns a Uint8Array of the data in <i>aBytesLike</i> with <code class="inline">0</code> bytes prepended to <i>length</i> bytes long.</p><p>If <i>aBytesLike</i> is already longer than <i>length</i> bytes long, an InvalidArgument error will be thrown.</p></div></div>
<a name="hexstring-manipulation"></a><h2 class="show-anchors"><div>Hexstring Manipulation<div class="anchors"><a class="self" href="#hexstring-manipulation"></a></div></div></h2>
2020-02-02 08:53:22 +03:00
<a name="utils-hexconcat"></a><div class="property show-anchors"><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&lt;&thinsp;<a href="/api/utils/bytes/#datahexstring">DataHexstring</a>&thinsp;&gt;</span><div class="anchors"><a class="self" href="#utils-hexconcat"></a><a class="source" href="https://github.com/ethers-io/ethers.js/blob/ethers-v5-beta/packages/bytes/src.ts/index.ts#L279">source</a></div></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/#datahexstring">DataHexstring</a></p></div></div>
<a name="utils-hexdatalength"></a><div class="property show-anchors"><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">string&lt;&thinsp;<a href="/api/utils/bytes/#datahexstring">DataHexstring</a>&thinsp;&gt;</span><div class="anchors"><a class="self" href="#utils-hexdatalength"></a><a class="source" href="https://github.com/ethers-io/ethers.js/blob/ethers-v5-beta/packages/bytes/src.ts/index.ts#L253">source</a></div></div><div class="body"><p>Returns the length (in bytes) of <i>aBytesLike</i>.</p></div></div>
<a name="utils-hexdataslice"></a><div class="property show-anchors"><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&lt;&thinsp;<a href="/api/utils/bytes/#datahexstring">DataHexstring</a>&thinsp;&gt;</span><div class="anchors"><a class="self" href="#utils-hexdataslice"></a><a class="source" href="https://github.com/ethers-io/ethers.js/blob/ethers-v5-beta/packages/bytes/src.ts/index.ts#L263">source</a></div></div><div class="body"><p>Returns a <a href="/api/utils/bytes/#datahexstring">DataHexstring</a> representation of a slice of <i>aBytesLike</i>, from <i>offset</i> (in bytes) to <i>endOffset</i> (in bytes). If <i>endOffset</i> is omitted, the length of <i>aBytesLike</i> is used.</p></div></div>
<a name="utils-hexstripzeros"></a><div class="property show-anchors"><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&lt;&thinsp;<a href="/api/utils/bytes/#hexstring">Hexstring</a>&thinsp;&gt;</span><div class="anchors"><a class="self" href="#utils-hexstripzeros"></a><a class="source" href="https://github.com/ethers-io/ethers.js/blob/ethers-v5-beta/packages/bytes/src.ts/index.ts#L293">source</a></div></div><div class="body"><p>Returns a <a href="/api/utils/bytes/#hexstring">Hexstring</a> representation of <i>aBytesLike</i> with all leading zeros removed.</p></div></div>
<a name="utils-hexzeropad"></a><div class="property show-anchors"><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&lt;&thinsp;<a href="/api/utils/bytes/#datahexstring">DataHexstring</a>&thinsp;&gt;</span><div class="anchors"><a class="self" href="#utils-hexzeropad"></a><a class="source" href="https://github.com/ethers-io/ethers.js/blob/ethers-v5-beta/packages/bytes/src.ts/index.ts#L305">source</a></div></div><div class="body"><p>Returns a <a href="/api/utils/bytes/#datahexstring">DataHexstring</a> representation of <i>aBytesLike</i> padded to <i>length</i> bytes.</p><p>If <i>aBytesLike</i> is already longer than <i>length</i> bytes long, an InvalidArgument error will be thrown.</p></div></div>
2019-12-14 06:05:10 +03:00
<a name="signature-conversion"></a><h2 class="show-anchors"><div>Signature Conversion<div class="anchors"><a class="self" href="#signature-conversion"></a></div></div></h2>
2020-02-02 08:53:22 +03:00
<a name="utils-joinsignature"></a><div class="property show-anchors"><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&lt;&thinsp;<a href="/api/utils/bytes/#signature-flat">FlatSignature</a>&thinsp;&gt;</span><div class="anchors"><a class="self" href="#utils-joinsignature"></a><a class="source" href="https://github.com/ethers-io/ethers.js/blob/ethers-v5-beta/packages/bytes/src.ts/index.ts#L442">source</a></div></div><div class="body"><p>Return the flat-format of <i>aSignaturelike</i>, which is 65 bytes (130 nibbles) long, concatenating the <b>r</b>, <b>s</b> and (normalized) <b>v</b> of a Signature.</p></div></div>
2020-02-18 01:56:50 +03:00
<a name="utils-splitsignature"></a><div class="property show-anchors"><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"><a href="/api/utils/bytes/#signature">Signature</a></span><div class="anchors"><a class="self" href="#utils-splitsignature"></a><a class="source" href="https://github.com/ethers-io/ethers.js/blob/ethers-v5-beta/packages/bytes/src.ts/index.ts#L323">source</a></div></div><div class="body"><p>Return the full expanded-format of <i>aSignaturelike</i> or a flat-format <a href="/api/utils/bytes/#datahexstring">DataHexstring</a>. Any missing properties will be computed.</p></div></div>
<a name="random-bytes"></a><h2 class="show-anchors"><div>Random Bytes<div class="anchors"><a class="self" href="#random-bytes"></a></div></div></h2>
<div class="property show-anchors"><div class="signature"><span class="path">ethers</span> . <span class="path">utils</span> . <span class="method">randomBytes</span> ( <span class="param">length</span> ) <span class="arrow">&rArr;</span> <span class="returns">Uint8Array</span><div class="anchors"></div></div><div class="body"><p>Return a new Uint8Array of <i>length</i> random bytes.</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:41882a2bd89dad9b58819ecf614d21d4e52eb11c50f1fba0ebbe3446a71ca069 -->