<divclass="link title"><ahref="/v5/">Documentation</a></div><divclass="base show link depth-1"><ahref="/v5/getting-started/">Getting Started</a></div><divclass="base show link depth-1"><ahref="/v5/concepts/">Ethereum Basics</a></div><divclass="hide link depth-2"><ahref="/v5/concepts/events/">Events</a></div><divclass="hide link depth-2"><ahref="/v5/concepts/gas/">Gas</a></div><divclass="hide link depth-2"><ahref="/v5/concepts/security/">Security</a></div><divclass="base ancestor show link depth-1"><ahref="/v5/api/">Application Programming Interface</a></div><divclass="show link depth-2"><ahref="/v5/api/contract/">Contract Interaction</a></div><divclass="hide link depth-3"><ahref="/v5/api/contract/contract/">Contract</a></div><divclass="hide link depth-3"><ahref="/v5/api/contract/contract-factory/">ContractFactory</a></div><divclass="hide link depth-3"><ahref="/v5/api/contract/example/">Example: ERC-20 Contract</a></div><divclass="show link depth-2"><ahref="/v5/api/signer/">Signers</a></div><divclass="show link depth-2"><ahref="/v5/api/providers/">Providers</a></div><divclass="hide link depth-3"><ahref="/v5/api/providers/provider/">Provider</a></div><divclass="hide link depth-3"><ahref="/v5/api/providers/jsonrpc-provider/">JsonRpcProvider</a></div><divclass="hide link depth-3"><ahref="/v5/api/providers/api-providers/">API Providers</a></div><divclass="hide link depth-3"><ahref="/v5/api/providers/other/">Other Providers</a></div><divclass="hide link depth-3"><ahref="/v5/api/providers/types/">Types</a></div><divclass="ancestor show link depth-2"><ahref="/v5/api/utils/">Utilities</a></div><divclass="show link depth-3"><ahref="/v5/api/utils/abi/">Application Binary Interface</a></div><divclass="hide link depth-4"><ahref="/v5/api/utils/abi/coder/">AbiCoder</a></div><divclass="hide link depth-4"><ahref="/v5/api/utils/abi/formats/">ABI Formats</a></div><divclass="hide link depth-4"><ahref="/v5/api/utils/abi/fragments/">Fragments</a></div><divclass="hide link depth-4"><ahref="/v5/api/utils/abi/interface/">Interface</a></div><divclass="show link depth-3"><ahref="/v5/api/utils/address/">Addresses</a></div><divclass="show link depth-3"><ahref="/v5/api/utils/bignumber/">BigNumber</a></div><divclass="show link depth-3"><ahref="/v5/api/utils/bytes/">Byte Manipulation</a></div><divclass="show link depth-3"><ahref="/v5/api/utils/constants/">Constants</a></div><divclass="show link depth-3"><ahref="/v5/api/utils/display-logic/">Display Logic and Input</a></div><divclass="show link depth-3"><ahref="/v5/api/utils/encoding/">Encoding Utilities</a></div><divclass="show link depth-3"><ahref="/v5/api/utils/fixednumber/">FixedNumber</a></div><divclass="show link depth-3"><ahref="/v5/api/utils/hashing/">Hashing Algorithms</a></div><divclass="show link depth-3"><ahref="/v5/api/utils/hdnode/">HD Wallet</a></div><divclass="show link depth-3"><ahref="/v5/api/utils/logger/">Logging</a></div><divclass="show link depth-3"><ahref="/v5/api/utils/properties/">Property Utilities</a></div><divclass="show link depth-3"><ahref="/v5/api/utils/signing-key/">Signing Key</a></div><divclass="myself ancestor ancestor show link depth-3"><ahref="/v5/api/utils/strings/">Strings</a></div><divclass="show link depth-3"><ahref="/v5/api/utils/transactions/">Transactions</a></div><divclass="show link depth-3"><ahref="/v5/api/utils/web/">Web Utilities</a></div><divclass="show link depth-3"><ahref="/v5/api/utils/wordlists/">Wordlists</a></div><divclass="show link depth-2"><ahref="/v5/api/other/">Other Libraries</a></div><divclass="hide link depth-3"><ahref="/v5/api/other/assembly/">Assembly</a></div><divclass="hide link depth-4"><ahref="/v5/api/other/assembly/dialect/">Ethers ASM Dialect</a></div><divclass="hide link depth-4"><ahref="/v5/api/other/assembly/api/">Utilities</a></div><divclass="hide link depth-4"><ahref="/v5/api/other/assembly/ast/">Abstract Syntax Tree</a></div><divclass="hide link depth-3"><ahref="/v5/api/other/hardware/">Hardware Wallets</a></div><divclass="sho
<aname="strings"></a><aname="strings"></a><h1class="show-anchors"><div>Strings<divclass="anchors"><aclass="self"href="/v5/api/utils/strings/#strings"></a></div></div></h1><p>Tra la la</p>
<aname="Bytes32String"></a><aname="strings--Bytes32String"></a><h2class="show-anchors"><div>Bytes32String<divclass="anchors"><aclass="self"href="/v5/api/utils/strings/#Bytes32String"></a></div></div></h2><p>A string in Solidity is length prefixed with its 256-bit (32 byte) length, which means that even short strings require 2 words (64 bytes) of storage.</p>
<p>In many cases, we deal with short strings, so instead of prefixing the string with its length, we can null-terminate it and fit it in a single word (32 bytes). Since we need only a single byte for the null termination, we can store strings up to 31 bytes long in a word.</p>
<divclass="definition container-box note"><divclass="term">Note</div><divclass="body"><p>Strings that are 31 <u><i>bytes</i></u> long may contain fewer than 31 <u><i>characters</i></u>, since UTF-8 requires multiple bytes to encode international characters.</p>
</div></div><aname="utils-parseBytes32"></a><divclass="property show-anchors"><divclass="signature"><spanclass="path">ethers</span><spanclass="symbol">.</span><spanclass="path">utils</span><spanclass="symbol">.</span><spanclass="method">parseBytes32String</span><spanclass="symbol">(</span><spanclass="param">aBytesLike</span><spanclass="symbol">)</span><spanclass="arrow">⇒</span><spanclass="returns">string</span><divclass="anchors"><aclass="self"href="/v5/api/utils/strings/#utils-parseBytes32"></a><aclass="source"href="https://github.com/ethers-io/ethers.js/blob/master/packages/strings/src.ts/bytes32.ts#L21">source</a></div></div><divclass="body"><p>Returns the decoded string represented by the <codeclass="inline">Bytes32</code> encoded data.</p>
</div></div><aname="utils-formatBytes32"></a><divclass="property show-anchors"><divclass="signature"><spanclass="path">ethers</span><spanclass="symbol">.</span><spanclass="path">utils</span><spanclass="symbol">.</span><spanclass="method">formatBytes32String</span><spanclass="symbol">(</span><spanclass="param">text</span><spanclass="symbol">)</span><spanclass="arrow">⇒</span><spanclass="returns">string<<ahref="/v5/api/utils/bytes/#DataHexString">DataHexString</a>< 32 >></span><divclass="anchors"><aclass="self"href="/v5/api/utils/strings/#utils-formatBytes32"></a><aclass="source"href="https://github.com/ethers-io/ethers.js/blob/master/packages/strings/src.ts/bytes32.ts#L9">source</a></div></div><divclass="body"><p>Returns a <codeclass="inline">bytes32</code> string representation of <i>text</i>. If the length of <i>text</i> exceeds 31 bytes, it will throw an error.</p>
<aname="utils-toUtf8Bytes"></a><divclass="property show-anchors"><divclass="signature"><spanclass="path">ethers</span><spanclass="symbol">.</span><spanclass="path">utils</span><spanclass="symbol">.</span><spanclass="method">toUtf8Bytes</span><spanclass="symbol">(</span><spanclass="param">text</span><spanclass="symbol">[</span><spanclass="symbol">,</span><spanclass="param">form</span> = <spanclass="param">current</span><spanclass="symbol">]</span><spanclass="symbol">)</span><spanclass="arrow">⇒</span><spanclass="returns">Uint8Array</span><divclass="anchors"><aclass="self"href="/v5/api/utils/strings/#utils-toUtf8Bytes"></a><aclass="source"href="https://github.com/ethers-io/ethers.js/blob/master/packages/strings/src.ts/utf8.ts#L202">source</a></div></div><divclass="body"><p>Returns the UTF-8 bytes of <i>text</i>, optionally normalizing it using the <ahref="/v5/api/utils/strings/#strings--unicode-normalization-form">UnicodeNormalizationForm</a><i>form</i>.</p>
</div></div><aname="utils-toUtf8CodePoints"></a><divclass="property show-anchors"><divclass="signature"><spanclass="path">ethers</span><spanclass="symbol">.</span><spanclass="path">utils</span><spanclass="symbol">.</span><spanclass="method">toUtf8CodePoints</span><spanclass="symbol">(</span><spanclass="param">text</span><spanclass="symbol">[</span><spanclass="symbol">,</span><spanclass="param">form</span> = <spanclass="param">current</span><spanclass="symbol">]</span><spanclass="symbol">)</span><spanclass="arrow">⇒</span><spanclass="returns">Array< number ></span><divclass="anchors"><aclass="self"href="/v5/api/utils/strings/#utils-toUtf8CodePoints"></a><aclass="source"href="https://github.com/ethers-io/ethers.js/blob/master/packages/strings/src.ts/utf8.ts#L293">source</a></div></div><divclass="body"><p>Returns the Array of codepoints of <i>text</i>, optionally normalized using the <ahref="/v5/api/utils/strings/#strings--unicode-normalization-form">UnicodeNormalizationForm</a><i>form</i>.</p>
</div></div><divclass="definition container-box note"><divclass="term">Note</div><divclass="body"><p>This function correctly splits each <b>user-perceived character</b> into its codepoint, accounting for surrogate pairs. This should not be confused with <codeclass="inline">string.split("")</code>, which destroys surrogate pairs, spliting between each UTF-16 codeunit instead.</p>
</div></div><aname="utils-toUtf8String"></a><divclass="property show-anchors"><divclass="signature"><spanclass="path">ethers</span><spanclass="symbol">.</span><spanclass="path">utils</span><spanclass="symbol">.</span><spanclass="method">toUtf8String</span><spanclass="symbol">(</span><spanclass="param">aBytesLike</span><spanclass="symbol">[</span><spanclass="symbol">,</span><spanclass="param">onError</span> = <spanclass="param">error</span><spanclass="symbol">]</span><spanclass="symbol">)</span><spanclass="arrow">⇒</span><spanclass="returns">string</span><divclass="anchors"><aclass="self"href="/v5/api/utils/strings/#utils-toUtf8String"></a><aclass="source"href="https://github.com/ethers-io/ethers.js/blob/master/packages/strings/src.ts/utf8.ts#L289">source</a></div></div><divclass="body"><p>Returns the string represented by the UTF-8 bytes of <i>aBytesLike</i>.</p>
<p>The <i>onError</i> is a <ahref="/v5/api/utils/strings/#strings--error-handling">Custom UTF-8 Error function</a> and if not specified it defaults to the <ahref="/v5/api/utils/strings/#strings--Utf8Error">error</a> function, which throws an error on <b>any</b> UTF-8 error.</p>
</div></div><aname="strings--unicode-normalization-form"></a><aname="strings--strings--unicode-normalization-form"></a><h2class="show-anchors"><div>UnicodeNormalizationForm<divclass="anchors"><aclass="self"href="/v5/api/utils/strings/#strings--unicode-normalization-form"></a><aclass="source"href="https://github.com/ethers-io/ethers.js/blob/master/packages/strings/src.ts/utf8.ts#L11">source</a></div></div></h2><p>There are several <ahref="https://en.wikipedia.org/wiki/Unicode_equivalence">commonly used forms</a> when normalizing UTF-8 data, which allow strings to be compared or hashed in a stable way.</p>
<divclass="property show-anchors"><divclass="signature"><spanclass="path">ethers</span><spanclass="symbol">.</span><spanclass="path">utils</span><spanclass="symbol">.</span><spanclass="path">UnicodeNormalizationForm</span><spanclass="symbol">.</span><spanclass="method">current</span><divclass="anchors"></div></div><divclass="body"><p>Maintain the current normalization form.</p>
</div></div><divclass="property show-anchors"><divclass="signature"><spanclass="path">ethers</span><spanclass="symbol">.</span><spanclass="path">utils</span><spanclass="symbol">.</span><spanclass="path">UnicodeNormalizationForm</span><spanclass="symbol">.</span><spanclass="method">NFC</span><divclass="anchors"></div></div><divclass="body"><p>The Composed Normalization Form. This form uses single codepoints which represent the fully composed character.</p>
<p>For example, the <b>é</b> is a single codepoint, <codeclass="inline">0x00e9</code>.</p>
</div></div><divclass="property show-anchors"><divclass="signature"><spanclass="path">ethers</span><spanclass="symbol">.</span><spanclass="path">utils</span><spanclass="symbol">.</span><spanclass="path">UnicodeNormalizationForm</span><spanclass="symbol">.</span><spanclass="method">NFD</span><divclass="anchors"></div></div><divclass="body"><p>The Decomposed Normalization Form. This form uses multiple codepoints (when necessary) to compose a character.</p>
<p>For example, the <b>é</b> is made up of two codepoints, <codeclass="inline">"0x0065"</code> (which is the letter <codeclass="inline">"e"</code>) and <codeclass="inline">"0x0301"</code> which is a special diacritic UTF-8 codepoint which indicates the previous character should have an acute accent.</p>
</div></div><divclass="property show-anchors"><divclass="signature"><spanclass="path">ethers</span><spanclass="symbol">.</span><spanclass="path">utils</span><spanclass="symbol">.</span><spanclass="path">UnicodeNormalizationForm</span><spanclass="symbol">.</span><spanclass="method">NFKC</span><divclass="anchors"></div></div><divclass="body"><p>The Composed Normalization Form with Canonical Equivalence. The Canonical representation folds characters which have the same syntactic representation but different semantic meaning.</p>
<p>For example, the Roman Numeral <b>I</b>, which has a UTF-8 codepoint <codeclass="inline">"0x2160"</code>, is folded into the capital letter I, <codeclass="inline">"0x0049"</code>.</p>
</div></div><divclass="property show-anchors"><divclass="signature"><spanclass="path">ethers</span><spanclass="symbol">.</span><spanclass="path">utils</span><spanclass="symbol">.</span><spanclass="path">UnicodeNormalizationForm</span><spanclass="symbol">.</span><spanclass="method">NFKD</span><divclass="anchors"></div></div><divclass="body"><p>The Decomposed Normalization Form with Canonical Equivalence. See NFKC for more an example.</p>
</div></div><divclass="definition container-box note"><divclass="term">Note</div><divclass="body"><p>Only certain specified characters are folded in Canonical Equivalence, and thus it should <b>not</b> be considered a method to acheive <i>any</i> level of security from <ahref="https://en.wikipedia.org/wiki/IDN_homograph_attack">homoglyph attacks</a>.</p>
</div></div><aname="strings--error-handling"></a><aname="strings--strings--error-handling"></a><h2class="show-anchors"><div>Custom UTF-8 Error Handling<divclass="anchors"><aclass="self"href="/v5/api/utils/strings/#strings--error-handling"></a></div></div></h2><p>When converting a string to its codepoints, there is the possibility of invalid byte sequences. Since certain situations may need specific ways to handle UTF-8 errors, a custom error handling function can be used, which has the signature:</p>
<divclass="property show-anchors"><divclass="signature"><spanclass="method">errorFunction</span><spanclass="symbol">(</span><spanclass="param">reason</span><spanclass="symbol">,</span><spanclass="param">offset</span><spanclass="symbol">,</span><spanclass="param">bytes</span><spanclass="symbol">,</span><spanclass="param">output</span><spanclass="symbol">[</span><spanclass="symbol">,</span><spanclass="param">badCodepoint</span><spanclass="symbol">]</span><spanclass="symbol">)</span><spanclass="arrow">⇒</span><spanclass="returns">number</span><divclass="anchors"></div></div><divclass="body"><p>The <i>reason</i> is one of the <ahref="/v5/api/utils/strings/#strings--error-reasons">UTF-8 Error Reasons</a>, <i>offset</i> is the index into <i>bytes</i> where the error was first encountered, output is the list of codepoints already processed (and may be modified) and in certain Error Reasons, the <i>badCodepoint</i> indicates the currently computed codepoint, but which would be rejected because its value is invalid.</p>
<p>This function should return the number of bytes to skip past keeping in mind the value at <i>offset</i> will already be consumed.</p>
<divclass="property show-anchors"><divclass="signature"><spanclass="path">ethers</span><spanclass="symbol">.</span><spanclass="path">utils</span><spanclass="symbol">.</span><spanclass="path">Utf8ErrorReason</span><spanclass="symbol">.</span><spanclass="method">BAD_PREFIX</span><divclass="anchors"></div></div><divclass="body"><p>A byte was encountered which is invalid to begin a UTF-8 byte sequence with.</p>
</div></div><divclass="property show-anchors"><divclass="signature"><spanclass="path">ethers</span><spanclass="symbol">.</span><spanclass="path">utils</span><spanclass="symbol">.</span><spanclass="path">Utf8ErrorReason</span><spanclass="symbol">.</span><spanclass="method">MISSING_CONTINUE</span><divclass="anchors"></div></div><divclass="body"><p>A UTF-8 sequence was begun, but did not have enough continuation bytes for the sequence. For this error the <i>ofset</i> is the index at which a continuation byte was expected.</p>
</div></div><divclass="property show-anchors"><divclass="signature"><spanclass="path">ethers</span><spanclass="symbol">.</span><spanclass="path">utils</span><spanclass="symbol">.</span><spanclass="path">Utf8ErrorReason</span><spanclass="symbol">.</span><spanclass="method">OUT_OF_RANGE</span><divclass="anchors"></div></div><divclass="body"><p>The computed codepoint is outside the range for valid UTF-8 codepoints (i.e. the codepoint is greater than 0x10ffff). This reason will pass the computed <i>badCountpoint</i> into the custom error function.</p>
</div></div><divclass="property show-anchors"><divclass="signature"><spanclass="path">ethers</span><spanclass="symbol">.</span><spanclass="path">utils</span><spanclass="symbol">.</span><spanclass="path">Utf8ErrorReason</span><spanclass="symbol">.</span><spanclass="method">OVERLONG</span><divclass="anchors"></div></div><divclass="body"><p>Due to the way UTF-8 allows variable length byte sequences to be used, it is possible to have multiple representations of the same character, which means <ahref="https://en.wikipedia.org/wiki/UTF-8#Overlong_encodings">overlong sequences</a> allow for a non-distinguished string to be formed, which can impact security as multiple strings that are otherwise equal can have different hashes.</p>
<p>Generally, overlong sequences are an attempt to circumvent some part of security, but in rare cases may be produced by lazy libraries or used to encode the null terminating character in a way that is safe to include in a <codeclass="inline">char*</code>.</p>
<p>This reason will pass the computed <i>badCountpoint</i> into the custom error function, which is actually a valid codepoint, just one that was arrived at through unsafe methods.</p>
</div></div><divclass="property show-anchors"><divclass="signature"><spanclass="path">ethers</span><spanclass="symbol">.</span><spanclass="path">utils</span><spanclass="symbol">.</span><spanclass="path">Utf8ErrorReason</span><spanclass="symbol">.</span><spanclass="method">OVERRUN</span><divclass="anchors"></div></div><divclass="body"><p>The string does not have enough characters remaining for the length of this sequence.</p>
</div></div><divclass="property show-anchors"><divclass="signature"><spanclass="path">ethers</span><spanclass="symbol">.</span><spanclass="path">utils</span><spanclass="symbol">.</span><spanclass="path">Utf8ErrorReason</span><spanclass="symbol">.</span><spanclass="method">UNEXPECTED_CONTINUE</span><divclass="anchors"></div></div><divclass="body"><p>This error is similar to BAD_PREFIX, since a continuation byte cannot begin a valid sequence, but many may wish to process this differently. However, most developers would want to trap this and perform the same operation as a BAD_PREFIX.</p>
</div></div><divclass="property show-anchors"><divclass="signature"><spanclass="path">ethers</span><spanclass="symbol">.</span><spanclass="path">utils</span><spanclass="symbol">.</span><spanclass="path">Utf8ErrorReason</span><spanclass="symbol">.</span><spanclass="method">UTF16_SURROGATE</span><divclass="anchors"></div></div><divclass="body"><p>The computed codepoint represents a value reserved for UTF-16 surrogate pairs. This reason will pass the computed surrogate half <i>badCountpoint</i> into the custom error function.</p>
</div></div><aname="strings--strings--error-handling--provided-utf-8-error-handling-functions"></a><h3class="show-anchors"><div>Provided UTF-8 Error Handling Functions<divclass="anchors"><aclass="self"href="/v5/api/utils/strings/#strings--strings--error-handling--provided-utf-8-error-handling-functions"></a></div></div></h3><p>There are already several functions available for the most common situations.</p>
<aname="strings--Utf8Error"></a><divclass="property show-anchors"><divclass="signature"><spanclass="path">ethers</span><spanclass="symbol">.</span><spanclass="path">utils</span><spanclass="symbol">.</span><spanclass="path">Utf8ErrorFuncs</span><spanclass="symbol">.</span><spanclass="method">error</span><divclass="anchors"><aclass="self"href="/v5/api/utils/strings/#strings--Utf8Error"></a><aclass="source"href="https://github.com/ethers-io/ethers.js/blob/master/packages/strings/src.ts/utf8.ts#L55">source</a></div></div><divclass="body"><p>The will throw an error on <b>any</b> error with a UTF-8 sequence, including invalid prefix bytes, overlong sequences, UTF-16 surrogate pairs.</p>
</div></div><aname="strings--Utf8Ignore"></a><divclass="property show-anchors"><divclass="signature"><spanclass="path">ethers</span><spanclass="symbol">.</span><spanclass="path">utils</span><spanclass="symbol">.</span><spanclass="path">Utf8ErrorFuncs</span><spanclass="symbol">.</span><spanclass="method">ignore</span><divclass="anchors"><aclass="self"href="/v5/api/utils/strings/#strings--Utf8Ignore"></a><aclass="source"href="https://github.com/ethers-io/ethers.js/blob/master/packages/strings/src.ts/utf8.ts#L59">source</a></div></div><divclass="body"><p>This will drop all invalid sequences (by consuming invalid prefix bytes and any following continuation bytes) from the final string as well as permit overlong sequences to be converted to their equivalent string.</p>
</div></div><aname="strings--Utf8Replace"></a><divclass="property show-anchors"><divclass="signature"><spanclass="path">ethers</span><spanclass="symbol">.</span><spanclass="path">utils</span><spanclass="symbol">.</span><spanclass="path">Utf8ErrorFuncs</span><spanclass="symbol">.</span><spanclass="method">replace</span><divclass="anchors"><aclass="self"href="/v5/api/utils/strings/#strings--Utf8Replace"></a><aclass="source"href="https://github.com/ethers-io/ethers.js/blob/master/packages/strings/src.ts/utf8.ts#L81">source</a></div></div><divclass="body"><p>This will replace all invalid sequences (by consuming invalid prefix bytes and any following continuation bytes) with the <ahref="https://en.wikipedia.org/wiki/Specials_%28Unicode_block%29#Replacement_character">UTF-8 Replacement Character</a>, (i.e. U+FFFD).</p>
<divclass="copyright">The content of this site is licensed under the <ahref="https://choosealicense.com/licenses/cc-by-4.0/">Creative Commons License</a>. Generated on July 5, 2020, 12:0am.</div>