ethers.js/docs/v5/api/utils/hashing/index.html
2020-11-22 23:07:13 -05:00

329 lines
40 KiB
HTML

<!DOCTYPE html>
<html class="paged">
<head>
<title>Hashing Algorithms</title>
<link rel="stylesheet" type="text/css" href="/v5/static/style.css">
</head>
<body>
<div class="sidebar">
<div class="header">
<div class="logo"><a href="/v5/"><div class="image"></div><div class="name">ethers</div><div class="version">v5.0</div></a></div>
<div class="search"><form action="/v5/search/" method="GET"><input name="search" id="search" /></form><span class="search-icon">&#9906;</span></div>
</div>
<div class="toc"><div>
<div class="link title"><a href="/v5/">Documentation</a></div><div class="base show link depth-1"><a href="/v5/getting-started/">Getting Started</a></div><div class="base show link depth-1"><a href="/v5/concepts/">Ethereum Basics</a></div><div class="hide link depth-2"><a href="/v5/concepts/events/">Events</a></div><div class="hide link depth-2"><a href="/v5/concepts/gas/">Gas</a></div><div class="hide link depth-2"><a href="/v5/concepts/security/">Security</a></div><div class="hide link depth-2"><a href="/v5/concepts/best-practices/">Best Practices</a></div><div class="base show link depth-1"><a href="/v5/api-keys/">Provider API Keys</a></div><div class="base ancestor show link depth-1"><a href="/v5/api/">Application Programming Interface</a></div><div class="show link depth-2"><a href="/v5/api/providers/">Providers</a></div><div class="hide link depth-3"><a href="/v5/api/providers/provider/">Provider</a></div><div class="hide link depth-3"><a href="/v5/api/providers/jsonrpc-provider/">JsonRpcProvider</a></div><div class="hide link depth-3"><a href="/v5/api/providers/api-providers/">API Providers</a></div><div class="hide link depth-3"><a href="/v5/api/providers/other/">Other Providers</a></div><div class="hide link depth-3"><a href="/v5/api/providers/types/">Types</a></div><div class="show link depth-2"><a href="/v5/api/signer/">Signers</a></div><div class="show link depth-2"><a href="/v5/api/contract/">Contract Interaction</a></div><div class="hide link depth-3"><a href="/v5/api/contract/contract/">Contract</a></div><div class="hide link depth-3"><a href="/v5/api/contract/contract-factory/">ContractFactory</a></div><div class="hide link depth-3"><a href="/v5/api/contract/example/">Example: ERC-20 Contract</a></div><div class="ancestor show link depth-2"><a href="/v5/api/utils/">Utilities</a></div><div class="show link depth-3"><a href="/v5/api/utils/abi/">Application Binary Interface</a></div><div class="hide link depth-4"><a href="/v5/api/utils/abi/coder/">AbiCoder</a></div><div class="hide link depth-4"><a href="/v5/api/utils/abi/formats/">ABI Formats</a></div><div class="hide link depth-4"><a href="/v5/api/utils/abi/fragments/">Fragments</a></div><div class="hide link depth-4"><a href="/v5/api/utils/abi/interface/">Interface</a></div><div class="show link depth-3"><a href="/v5/api/utils/address/">Addresses</a></div><div class="show link depth-3"><a href="/v5/api/utils/bignumber/">BigNumber</a></div><div class="show link depth-3"><a href="/v5/api/utils/bytes/">Byte Manipulation</a></div><div class="show link depth-3"><a href="/v5/api/utils/constants/">Constants</a></div><div class="show link depth-3"><a href="/v5/api/utils/display-logic/">Display Logic and Input</a></div><div class="show link depth-3"><a href="/v5/api/utils/encoding/">Encoding Utilities</a></div><div class="show link depth-3"><a href="/v5/api/utils/fixednumber/">FixedNumber</a></div><div class="myself ancestor ancestor show link depth-3"><a href="/v5/api/utils/hashing/">Hashing Algorithms</a></div><div class="show link depth-3"><a href="/v5/api/utils/hdnode/">HD Wallet</a></div><div class="show link depth-3"><a href="/v5/api/utils/logger/">Logging</a></div><div class="show link depth-3"><a href="/v5/api/utils/properties/">Property Utilities</a></div><div class="show link depth-3"><a href="/v5/api/utils/signing-key/">Signing Key</a></div><div class="show link depth-3"><a href="/v5/api/utils/strings/">Strings</a></div><div class="show link depth-3"><a href="/v5/api/utils/transactions/">Transactions</a></div><div class="show link depth-3"><a href="/v5/api/utils/web/">Web Utilities</a></div><div class="show link depth-3"><a href="/v5/api/utils/wordlists/">Wordlists</a></div><div class="show link depth-2"><a href="/v5/api/other/">Other Libraries</a></div><div class="hide link depth-3"><a href="/v5/api/other/assembly/">Assembly</a></div><div class="hide link depth-4"><a href="/v5/api/other/assembly/dialect/">Ethers ASM Dialect</a></div><div class="hide link depth-4"><a href="/v5/api/other/assembly/api/">Utilities</a></div><div class="hide link depth-4"><a href="/v5/api/other/assembly/ast/">Abstract Syntax Tree</a></div><div class="hide link depth-3"><a href="/v5/api/other/hardware/">Hardware Wallets</a></div><div class="show link depth-2"><a href="/v5/api/experimental/">Experimental</a></div><div class="base show link depth-1"><a href="/v5/cli/">Command Line Interfaces</a></div><div class="hide link depth-2"><a href="/v5/cli/ethers/">Sandbox Utility</a></div><div class="hide link depth-2"><a href="/v5/cli/asm/">Assembler</a></div><div class="hide link depth-2"><a href="/v5/cli/ens/">Ethereum Naming Service</a></div><div class="hide link depth-2"><a href="/v5/cli/typescript/">TypeScript</a></div><div class="hide link depth-2"><a href="/v5/cli/plugin/">Making Your Own</a></div><div class="base show link depth-1"><a href="/v5/cookbook/">Cookbook</a></div><div class="hide link depth-2"><a href="/v5/cookbook/react-native/">React Native (and ilk)</a></div><div class="base show link depth-1"><a href="/v5/migration/">Migration Guide</a></div><div class="hide link depth-2"><a href="/v5/migration/web3/">Migration: From Web3.js</a></div><div class="hide link depth-2"><a href="/v5/migration/ethers-v4/">Migration: From Ethers v4</a></div><div class="base show link depth-1"><a href="/v5/testing/">Testing</a></div><div class="base show link depth-1"><a href="/v5/contributing/">Contributing and Hacking</a></div><div class="base show link depth-1"><a href="/v5/documentation/">Flatworm Docs</a></div><div class="base show link depth-1"><a href="/v5/license/">License and Copyright</a></div>
</div></div>
<div class="footer">
<a href="/v5/single-page/">Single Page</a>
</div>
</div>
<div class="content">
<div class="breadcrumbs"><a href="/v5/">Documentation</a>&nbsp;&nbsp;&raquo;&nbsp;&nbsp;<a href="/v5/api/">API</a>&nbsp;&nbsp;&raquo;&nbsp;&nbsp;<a href="/v5/api/utils/">Utilities</a>&nbsp;&nbsp;&raquo;&nbsp;&nbsp;<span class="current">Hashing Algorithms</span></div>
<a name="hashing-algorithms"></a><a name="hashing-algorithms"></a><h1 class="show-anchors"><div>Hashing Algorithms<div class="anchors"><a class="self" href="/v5/api/utils/hashing/#hashing-algorithms"></a></div></div></h1><p>There are many hashing algorithms used throughout the blockchain space as well as some more complex usages which require utilities to facilitate these common operations.</p>
<a name="cryptographic-hash-functions"></a><a name="hashing-algorithms--cryptographic-hash-functions"></a><h2 class="show-anchors"><div>Cryptographic Hash Functions<div class="anchors"><a class="self" href="/v5/api/utils/hashing/#cryptographic-hash-functions"></a></div></div></h2><p>The <a href="https://en.wikipedia.org/wiki/Cryptographic_hash_function">Cryptographic Hash Functions</a> are a specific family of hash functions.</p>
<a name="utils-id"></a><div class="property show-anchors"><div class="signature"><span class="path">ethers</span><span class="symbol">.</span><span class="path">utils</span><span class="symbol">.</span><span class="method">id</span><span class="symbol">(</span> <span class="param">text</span> <span class="symbol">)</span> <span class="arrow">&rArr;</span> <span class="returns">string&lt; <a href="/v5/api/utils/bytes/#DataHexString">DataHexString</a>&lt; 32 &gt; &gt;</span><div class="anchors"><a class="self" href="/v5/api/utils/hashing/#utils-id"></a><a class="source" href="https://github.com/ethers-io/ethers.js/blob/master/packages/hash/src.ts/id.ts#L4">source</a></div></div><div class="body"><p>The Ethereum Identity function computes the <a href="https://en.wikipedia.org/wiki/SHA-3">KECCAK256</a> hash of the <i>text</i> bytes.</p>
</div></div><a name="utils-keccak256"></a><div class="property show-anchors"><div class="signature"><span class="path">ethers</span><span class="symbol">.</span><span class="path">utils</span><span class="symbol">.</span><span class="method">keccak256</span><span class="symbol">(</span> <span class="param">aBytesLike</span> <span class="symbol">)</span> <span class="arrow">&rArr;</span> <span class="returns">string&lt; <a href="/v5/api/utils/bytes/#DataHexString">DataHexString</a>&lt; 32 &gt; &gt;</span><div class="anchors"><a class="self" href="/v5/api/utils/hashing/#utils-keccak256"></a><a class="source" href="https://github.com/ethers-io/ethers.js/blob/master/packages/keccak256/src.ts/index.ts#L7">source</a></div></div><div class="body"><p>Returns the <a href="https://en.wikipedia.org/wiki/SHA-3">KECCAK256</a> digest <i>aBytesLike</i>.</p>
</div></div><a name="utils-ripemd160"></a><div class="property show-anchors"><div class="signature"><span class="path">ethers</span><span class="symbol">.</span><span class="path">utils</span><span class="symbol">.</span><span class="method">ripemd160</span><span class="symbol">(</span> <span class="param">aBytesLike</span> <span class="symbol">)</span> <span class="arrow">&rArr;</span> <span class="returns">string&lt; <a href="/v5/api/utils/bytes/#DataHexString">DataHexString</a>&lt; 20 &gt; &gt;</span><div class="anchors"><a class="self" href="/v5/api/utils/hashing/#utils-ripemd160"></a><a class="source" href="https://github.com/ethers-io/ethers.js/blob/master/packages/sha2/src.ts/sha2.ts#L13">source</a></div></div><div class="body"><p>Returns the <a href="https://en.m.wikipedia.org/wiki/RIPEMD">RIPEMD-160</a> digest of <i>aBytesLike</i>.</p>
</div></div><a name="utils-sha256"></a><div class="property show-anchors"><div class="signature"><span class="path">ethers</span><span class="symbol">.</span><span class="path">utils</span><span class="symbol">.</span><span class="method">sha256</span><span class="symbol">(</span> <span class="param">aBytesLike</span> <span class="symbol">)</span> <span class="arrow">&rArr;</span> <span class="returns">string&lt; <a href="/v5/api/utils/bytes/#DataHexString">DataHexString</a>&lt; 32 &gt; &gt;</span><div class="anchors"><a class="self" href="/v5/api/utils/hashing/#utils-sha256"></a><a class="source" href="https://github.com/ethers-io/ethers.js/blob/master/packages/sha2/src.ts/sha2.ts#L17">source</a></div></div><div class="body"><p>Returns the <a href="https://en.wikipedia.org/wiki/SHA-2">SHA2-256</a> digest of <i>aBytesLike</i>.</p>
</div></div><a name="utils-sha512"></a><div class="property show-anchors"><div class="signature"><span class="path">ethers</span><span class="symbol">.</span><span class="path">utils</span><span class="symbol">.</span><span class="method">sha512</span><span class="symbol">(</span> <span class="param">aBytesLike</span> <span class="symbol">)</span> <span class="arrow">&rArr;</span> <span class="returns">string&lt; <a href="/v5/api/utils/bytes/#DataHexString">DataHexString</a>&lt; 64 &gt; &gt;</span><div class="anchors"><a class="self" href="/v5/api/utils/hashing/#utils-sha512"></a><a class="source" href="https://github.com/ethers-io/ethers.js/blob/master/packages/sha2/src.ts/sha2.ts#L21">source</a></div></div><div class="body"><p>Returns the <a href="https://en.wikipedia.org/wiki/SHA-2">SHA2-512</a> digest of <i>aBytesLike</i>.</p>
</div></div><div class="code-title"><div>KECCAK256</div></div><div class="code">utils.keccak256([ 0x12, 0x34 ])
<span class="result ok">// '0x56570de287d73cd1cb6092bb8fdee6173974955fdef345ae579ee9f475ea7432'
</span>
utils.keccak256("0x")
<span class="result ok">// '0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470'
</span>
utils.keccak256("0x1234")
<span class="result ok">// '0x56570de287d73cd1cb6092bb8fdee6173974955fdef345ae579ee9f475ea7432'
</span>
<span class="comment">// The value MUST be data, such as:
</span><span class="comment">// - an Array of numbers
</span><span class="comment">// - a data hex string (e.g. "0x1234")
</span><span class="comment">// - a Uint8Array
</span>
<span class="comment">// Do NOT use UTF-8 strings that are not a DataHexstring
</span>utils.keccak256("hello world")
<span class="result error">// Error: invalid arrayify value (argument="value", value="hello world", code=INVALID_ARGUMENT, version=bytes/5.0.6)
</span>
<span class="comment">// If needed, convert strings to bytes first:
</span>utils.keccak256(utils.toUtf8Bytes("hello world"))
<span class="result ok">// '0x47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad'
</span>
<span class="comment">// Or equivalently use the identity function:
</span>utils.id("hello world")
<span class="result ok">// '0x47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad'
</span>
<span class="comment">// Keep in mind that the string "0x1234" represents TWO
</span><span class="comment">// bytes (i.e. [ 0x12, 0x34 ]. If you wish to compute the
</span><span class="comment">// hash of the 6 characters "0x1234", convert it to UTF-8
</span><span class="comment">// bytes first using utils.toUtf8Bytes.
</span>
<span class="comment">// Consider the following examples:
</span>
<span class="comment">// Hash of TWO (2) bytes:
</span>utils.keccak256("0x1234")
<span class="result ok">// '0x56570de287d73cd1cb6092bb8fdee6173974955fdef345ae579ee9f475ea7432'
</span>
<span class="comment">// Hash of TWO (2) bytes: (same result)
</span>utils.keccak256([ 0x12, 0x34 ])
<span class="result ok">// '0x56570de287d73cd1cb6092bb8fdee6173974955fdef345ae579ee9f475ea7432'
</span>
const bytes = utils.toUtf8Bytes("0x1234")
<span class="result ok">// Uint8Array [ 48, 120, 49, 50, 51, 52 ]
</span>
<span class="comment">// Hash of SIX (6) characters (different than above)
</span>utils.keccak256(bytes)
<span class="result ok">// '0x1ac7d1b81b7ba1025b36ccb86723da6ee5a87259f1c2fd5abe69d3200b512ec8'
</span>
<span class="comment">// Hash of SIX (6) characters (same result)
</span>utils.id("0x1234")
<span class="result ok">// '0x1ac7d1b81b7ba1025b36ccb86723da6ee5a87259f1c2fd5abe69d3200b512ec8'
</span></div><div class="code-title"><div>RIPEMD160</div></div><div class="code">utils.ripemd160("0x")
<span class="result ok">// '0x9c1185a5c5e9fc54612808977ee8f548b2258d31'
</span>
utils.ripemd160("0x1234")
<span class="result ok">// '0xc39867e393cb061b837240862d9ad318c176a96d'
</span></div><div class="code-title"><div>SHA-2</div></div><div class="code">utils.sha256("0x")
<span class="result ok">// '0xe3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'
</span>
utils.sha256("0x1234")
<span class="result ok">// '0x3a103a4e5729ad68c02a678ae39accfbc0ae208096437401b7ceab63cca0622f'
</span>
utils.sha512("0x")
<span class="result ok">// '0xcf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e'
</span>
utils.sha512("0x1234")
<span class="result ok">// '0x4c54886c9821195522d88ff4705c3e0c686b921054421e6ea598739c29c26e1ee75419aaceec94dd2e3c0dbb82ecf895c9f61215f375de6d800d9b99d3d4b816'
</span></div><a name="utils--hmac"></a><a name="hashing-algorithms--utils--hmac"></a><h2 class="show-anchors"><div>HMAC<div class="anchors"><a class="self" href="/v5/api/utils/hashing/#utils--hmac"></a></div></div></h2>
<a name="utils-computeHmac"></a><div class="property show-anchors"><div class="signature"><span class="path">ethers</span><span class="symbol">.</span><span class="path">utils</span><span class="symbol">.</span><span class="method">computeHmac</span><span class="symbol">(</span> <span class="param">algorithm</span> <span class="symbol">,</span> <span class="param">key</span> <span class="symbol">,</span> <span class="param">data</span> <span class="symbol">)</span> <span class="arrow">&rArr;</span> <span class="returns">string&lt; <a href="/v5/api/utils/bytes/#DataHexString">DataHexString</a> &gt;</span><div class="anchors"><a class="self" href="/v5/api/utils/hashing/#utils-computeHmac"></a><a class="source" href="https://github.com/ethers-io/ethers.js/blob/master/packages/sha2/src.ts/sha2.ts#L25">source</a></div></div><div class="body"><p>Returns the <a href="https://en.wikipedia.org/wiki/HMAC">HMAC</a> of <i>data</i> with <i>key</i> using the <a href="/v5/api/utils/hashing/#utils--hmac-supported-algorithm">Algorithm</a> <i>algorithm</i>.</p>
</div></div><a name="utils--hmac-supported-algorithm"></a><a name="hashing-algorithms--utils--hmac--utils--hmac-supported-algorithm"></a><h3 class="show-anchors"><div><b>HMAC Supported Algorithms</b><div class="anchors"><a class="self" href="/v5/api/utils/hashing/#utils--hmac-supported-algorithm"></a><a class="source" href="https://github.com/ethers-io/ethers.js/blob/master/packages/sha2/src.ts/types.ts#L1">source</a></div></div></h3>
<div class="property show-anchors"><div class="signature"><span class="path">ethers</span><span class="symbol">.</span><span class="path">utils</span><span class="symbol">.</span><span class="path">SupportedAlgorithm</span><span class="symbol">.</span><span class="method">sha256</span> <span class="arrow">&rArr;</span> <span class="returns">string</span><div class="anchors"></div></div><div class="body"><p>Use the <a href="https://en.wikipedia.org/wiki/SHA-2">SHA2-256</a> hash algorithm.</p>
</div></div><div class="property show-anchors"><div class="signature"><span class="path">ethers</span><span class="symbol">.</span><span class="path">utils</span><span class="symbol">.</span><span class="path">SupportedAlgorithm</span><span class="symbol">.</span><span class="method">sha512</span> <span class="arrow">&rArr;</span> <span class="returns">string</span><div class="anchors"></div></div><div class="body"><p>Use the <a href="https://en.wikipedia.org/wiki/SHA-2">SHA2-512</a> hash algorithm.</p>
</div></div><div class="code-title"><div>HMAC</div></div><div class="code">const key = "0x0102"
const data = "0x1234"
utils.computeHmac("sha256", key, data)
<span class="result ok">// '0x7553df81c628815cf569696cad13a37c606c5058df13d9dff4fee2cf5e9b5779'
</span></div><a name="utils--hashing-helpers"></a><a name="hashing-algorithms--utils--hashing-helpers"></a><h2 class="show-anchors"><div>Hashing Helpers<div class="anchors"><a class="self" href="/v5/api/utils/hashing/#utils--hashing-helpers"></a></div></div></h2>
<a name="utils-hashMessage"></a><div class="property show-anchors"><div class="signature"><span class="path">ethers</span><span class="symbol">.</span><span class="path">utils</span><span class="symbol">.</span><span class="method">hashMessage</span><span class="symbol">(</span> <span class="param">message</span> <span class="symbol">)</span> <span class="arrow">&rArr;</span> <span class="returns">string&lt; <a href="/v5/api/utils/bytes/#DataHexString">DataHexString</a>&lt; 32 &gt; &gt;</span><div class="anchors"><a class="self" href="/v5/api/utils/hashing/#utils-hashMessage"></a><a class="source" href="https://github.com/ethers-io/ethers.js/blob/master/packages/hash/src.ts/message.ts#L7">source</a></div></div><div class="body"><p>Computes the <a href="https://eips.ethereum.org/EIPS/eip-191">EIP-191</a> personal message digest of <i>message</i>. Personal messages are converted to UTF-8 bytes and prefixed with <code class="inline">\x19Ethereum Signed Message:</code> and the length of <i>message</i>.</p>
</div></div><div class="code-title"><div>Hashing Messages</div></div><div class="code"><span class="comment">// Hashing a string message
</span>utils.hashMessage("Hello World")
<span class="result ok">// '0xa1de988600a42c4b4ab089b619297c17d53cffae5d5120d82d8a92d0bb3b78f2'
</span>
<span class="comment">// Hashing binary data (also "Hello World", but as bytes)
</span>utils.hashMessage( [ 72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100 ])
<span class="result ok">// '0xa1de988600a42c4b4ab089b619297c17d53cffae5d5120d82d8a92d0bb3b78f2'
</span>
<span class="comment">// NOTE: It is important to understand how strings and binary
</span><span class="comment">// data is handled differently. A string is ALWAYS processed
</span><span class="comment">// as the bytes of the string, so a hexstring MUST be
</span><span class="comment">// converted to an ArrayLike object first.
</span>
<span class="comment">// Hashing a hex string is the same as hashing a STRING
</span><span class="comment">// Note: this is the hash of the 4 characters [ '0', 'x', '4', '2' ]
</span>utils.hashMessage("0x42")
<span class="result ok">// '0xf0d544d6e4a96e1c08adc3efabe2fcb9ec5e28db1ad6c33ace880ba354ab0fce'
</span>
<span class="comment">// Hashing the binary data
</span><span class="comment">// Note: this is the hash of the 1 byte [ 0x42 ]
</span>utils.hashMessage([ 0x42 ])
<span class="result ok">// '0xd18c12b87124f9ceb7e1d3a5d06a5ac92ecab15931417e8d1558d9a263f99d63'
</span>
<span class="comment">// Hashing the binary data
</span><span class="comment">// Note: similarly, this is the hash of the 1 byte [ 0x42 ]
</span>utils.hashMessage(utils.arrayify("0x42"))
<span class="result ok">// '0xd18c12b87124f9ceb7e1d3a5d06a5ac92ecab15931417e8d1558d9a263f99d63'
</span></div><a name="utils-namehash"></a><div class="property show-anchors"><div class="signature"><span class="path">ethers</span><span class="symbol">.</span><span class="path">utils</span><span class="symbol">.</span><span class="method">namehash</span><span class="symbol">(</span> <span class="param">name</span> <span class="symbol">)</span> <span class="arrow">&rArr;</span> <span class="returns">string&lt; <a href="/v5/api/utils/bytes/#DataHexString">DataHexString</a>&lt; 32 &gt; &gt;</span><div class="anchors"><a class="self" href="/v5/api/utils/hashing/#utils-namehash"></a><a class="source" href="https://github.com/ethers-io/ethers.js/blob/master/packages/hash/src.ts/namehash.ts#L27">source</a></div></div><div class="body"><p>Returns the <a href="https://docs.ens.domains/contract-api-reference/name-processing#hashing-names">ENS Namehash</a> of <i>name</i>.</p>
</div></div><div class="code-title"><div>Namehash</div></div><div class="code">utils.namehash("")
<span class="result ok">// '0x0000000000000000000000000000000000000000000000000000000000000000'
</span>
utils.namehash("eth")
<span class="result ok">// '0x93cdeb708b7545dc668eb9280176169d1c33cfd8ed6f04690a0bcc88a93fc4ae'
</span>
utils.namehash("ricmoo.firefly.eth")
<span class="result ok">// '0x0bcad17ecf260d6506c6b97768bdc2acfb6694445d27ffd3f9c1cfbee4a9bd6d'
</span>
utils.namehash("ricmoo.xyz")
<span class="result ok">// '0x7d56aa46358ba2f8b77d8e05bcabdd2358370dcf34e87810f8cea77ecb3fc57d'
</span></div><a name="TypedDataEncoder"></a><a name="hashing-algorithms--utils--hashing-helpers--TypedDataEncoder"></a><h3 class="show-anchors"><div>Typed Data Encoder<div class="anchors"><a class="self" href="/v5/api/utils/hashing/#TypedDataEncoder"></a><a class="source" href="https://github.com/ethers-io/ethers.js/blob/master/packages/hash/src.ts/typed-data.ts#L148">source</a></div></div></h3><p>The <b>TypedDataEncoder</b> is used to compute the various encoded data required for <a href="https://eips.ethereum.org/EIPS/eip-712">EIP-712</a> signed data.</p>
<p>Signed data requires a domain, list of structures and their members and the data itself.</p>
<p>The <b>domain</b> is an object with values for any of the standard domain properties.</p>
<p>The <b>types</b> is an object with each property being the name of a structure, mapping to an array of field descriptions. It should <b>not</b> include the <code class="inline">EIP712Domain</code> property unless it is required as a child structure of another.</p>
<div class="definition container-box note"><div class="term">Experimental Feature (this exported class name will change)</div><div class="body"><p>This is still an experimental feature. If using it, please specify the <b>exact</b> version of ethers you are using (e.g. spcify <code class="inline">"5.0.18"</code>, <b>not</b> <code class="inline">"^5.0.18"</code>) as the exported class name will be renamed from <code class="inline">_TypedDataEncoder</code> to <code class="inline">TypedDataEncoder</code> once it has been used in the field a bit.</p>
</div></div><a name="TypedDataEncoder-from"></a><div class="property show-anchors"><div class="signature"><span class="path">ethers</span><span class="symbol">.</span><span class="path">utils</span><span class="symbol">.</span><span class="path">_TypedDataEncoder</span><span class="symbol">.</span><span class="method">from</span><span class="symbol">(</span> <span class="param">types</span> <span class="symbol">)</span> <span class="arrow">&rArr;</span> <span class="returns">[TypedDataEncoder]</span><div class="anchors"><a class="self" href="/v5/api/utils/hashing/#TypedDataEncoder-from"></a><a class="source" href="https://github.com/ethers-io/ethers.js/blob/master/packages/hash/src.ts/typed-data.ts#L363">source</a></div></div><div class="body"><p>Creates a new <b>TypedDataEncoder</b> for <i>types</i>. This object is a fairly low-level object that most developers should not require using instances directly.</p>
<p>Most developers will find the static class methods below the most useful.</p>
</div></div><a name="TypedDataEncoder-encode"></a><div class="property show-anchors"><div class="signature"><span class="path">TypedDataEncoder</span><span class="symbol">.</span><span class="method">encode</span><span class="symbol">(</span> <span class="param">domain</span> <span class="symbol">,</span> <span class="param">types</span> <span class="symbol">,</span> <span class="param">values</span> <span class="symbol">)</span> <span class="arrow">&rArr;</span> <span class="returns">string</span><div class="anchors"><a class="self" href="/v5/api/utils/hashing/#TypedDataEncoder-encode"></a><a class="source" href="https://github.com/ethers-io/ethers.js/blob/master/packages/hash/src.ts/typed-data.ts#L392">source</a></div></div><div class="body"><p>Encodes the Returns the hashed <a href="https://eips.ethereum.org/EIPS/eip-712">EIP-712</a> domain.</p>
</div></div><a name="TypedDataEncoder-getPayload"></a><div class="property show-anchors"><div class="signature"><span class="path">TypedDataEncoder</span><span class="symbol">.</span><span class="method">getPayload</span><span class="symbol">(</span> <span class="param">domain</span> <span class="symbol">,</span> <span class="param">types</span> <span class="symbol">,</span> <span class="param">value</span> <span class="symbol">)</span> <span class="arrow">&rArr;</span> <span class="returns">any</span><div class="anchors"><a class="self" href="/v5/api/utils/hashing/#TypedDataEncoder-getPayload"></a><a class="source" href="https://github.com/ethers-io/ethers.js/blob/master/packages/hash/src.ts/typed-data.ts#L447">source</a></div></div><div class="body"><p>Returns the standard payload used by various JSON-RPC <code class="inline">eth_signTypedData*</code> calls.</p>
<p>All domain values and entries in value are normalized and the types are verified.</p>
</div></div><a name="TypedDataEncoder-getPrimaryType"></a><div class="property show-anchors"><div class="signature"><span class="path">TypedDataEncoder</span><span class="symbol">.</span><span class="method">getPrimaryType</span><span class="symbol">(</span> <span class="param">types</span> <span class="symbol">)</span> <span class="arrow">&rArr;</span> <span class="returns">string</span><div class="anchors"><a class="self" href="/v5/api/utils/hashing/#TypedDataEncoder-getPrimaryType"></a><a class="source" href="https://github.com/ethers-io/ethers.js/blob/master/packages/hash/src.ts/typed-data.ts#L367">source</a></div></div><div class="body"><p>Constructs a directed acyclic graph of the types and returns the root type, which can be used as the <b>primaryType</b> for <a href="https://eips.ethereum.org/EIPS/eip-712">EIP-712</a> payloads.</p>
</div></div><a name="TypedDataEncoder-hash"></a><div class="property show-anchors"><div class="signature"><span class="path">TypedDataEncoder</span><span class="symbol">.</span><span class="method">hash</span><span class="symbol">(</span> <span class="param">domain</span> <span class="symbol">,</span> <span class="param">types</span> <span class="symbol">,</span> <span class="param">values</span> <span class="symbol">)</span> <span class="arrow">&rArr;</span> <span class="returns">string&lt; <a href="/v5/api/utils/bytes/#DataHexString">DataHexString</a>&lt; 32 &gt; &gt;</span><div class="anchors"><a class="self" href="/v5/api/utils/hashing/#TypedDataEncoder-hash"></a><a class="source" href="https://github.com/ethers-io/ethers.js/blob/master/packages/hash/src.ts/typed-data.ts#L400">source</a></div></div><div class="body"><p>Returns the computed <a href="https://eips.ethereum.org/EIPS/eip-712">EIP-712</a> hash.</p>
</div></div><a name="TypedDataEncoder-hashDomain"></a><div class="property show-anchors"><div class="signature"><span class="path">TypedDataEncoder</span><span class="symbol">.</span><span class="method">hashDomain</span><span class="symbol">(</span> <span class="param">domain</span> <span class="symbol">)</span> <span class="arrow">&rArr;</span> <span class="returns">string&lt; <a href="/v5/api/utils/bytes/#DataHexString">DataHexString</a>&lt; 32 &gt; &gt;</span><div class="anchors"><a class="self" href="/v5/api/utils/hashing/#TypedDataEncoder-hashDomain"></a><a class="source" href="https://github.com/ethers-io/ethers.js/blob/master/packages/hash/src.ts/typed-data.ts#L375">source</a></div></div><div class="body"><p>Returns the hashed <a href="https://eips.ethereum.org/EIPS/eip-712">EIP-712</a> domain.</p>
</div></div><a name="TypedDataEncoder-resolveNames"></a><div class="property show-anchors"><div class="signature"><span class="path">TypedDataEncoder</span><span class="symbol">.</span><span class="method">resolveNames</span><span class="symbol">(</span> <span class="param">domain</span> <span class="symbol">,</span> <span class="param">types</span> <span class="symbol">,</span> <span class="param">value</span> <span class="symbol">,</span> <span class="param">resolveName</span> <span class="symbol">)</span> <span class="arrow">&rArr;</span> <span class="returns">Promise&lt; any &gt;</span><div class="anchors"><a class="self" href="/v5/api/utils/hashing/#TypedDataEncoder-resolveNames"></a><a class="source" href="https://github.com/ethers-io/ethers.js/blob/master/packages/hash/src.ts/typed-data.ts#L405">source</a></div></div><div class="body"><p>Returns a copy of value, where any leaf value with a type of <code class="inline">address</code> will have been recursively replacedwith the value of calling <i>resolveName</i> with that value.</p>
</div></div><div class="code-title"><div>Typed Data Example</div></div><div class="code">const domain = {
name: 'Ether Mail',
version: '1',
chainId: 1,
verifyingContract: '0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC'
}
<span class="comment">// The named list of all type definitions
</span>const types = {
Person: [
{ name: 'name', type: 'string' },
{ name: 'wallet', type: 'address' }
],
Mail: [
{ name: 'from', type: 'Person' },
{ name: 'to', type: 'Person' },
{ name: 'contents', type: 'string' }
]
}
<span class="comment">// The data to sign
</span>const value = {
from: {
name: 'Cow',
wallet: '0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826'
},
to: {
name: 'Bob',
wallet: '0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB'
},
contents: 'Hello, Bob!'
}
TypedDataEncoder.encode(domain, types, value)
<span class="result ok">// '0x1901f2cee375fa42b42143804025fc449deafd50cc031ca257e0b194a650a912090fc52c0ee5d84264471806290a3f2c4cecfc5490626bf912d01f240d7a274b371e'
</span>
TypedDataEncoder.getPayload(domain, types, value)
<span class="result ok">// {
</span><span class="result ok">// domain: {
</span><span class="result ok">// chainId: '1',
</span><span class="result ok">// name: 'Ether Mail',
</span><span class="result ok">// verifyingContract: '0xcccccccccccccccccccccccccccccccccccccccc',
</span><span class="result ok">// version: '1'
</span><span class="result ok">// },
</span><span class="result ok">// message: {
</span><span class="result ok">// contents: 'Hello, Bob!',
</span><span class="result ok">// from: {
</span><span class="result ok">// name: 'Cow',
</span><span class="result ok">// wallet: '0xcd2a3d9f938e13cd947ec05abc7fe734df8dd826'
</span><span class="result ok">// },
</span><span class="result ok">// to: {
</span><span class="result ok">// name: 'Bob',
</span><span class="result ok">// wallet: '0xbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'
</span><span class="result ok">// }
</span><span class="result ok">// },
</span><span class="result ok">// primaryType: 'Mail',
</span><span class="result ok">// types: {
</span><span class="result ok">// Mail: [
</span><span class="result ok">// {
</span><span class="result ok">// name: 'from',
</span><span class="result ok">// type: 'Person'
</span><span class="result ok">// },
</span><span class="result ok">// {
</span><span class="result ok">// name: 'to',
</span><span class="result ok">// type: 'Person'
</span><span class="result ok">// },
</span><span class="result ok">// {
</span><span class="result ok">// name: 'contents',
</span><span class="result ok">// type: 'string'
</span><span class="result ok">// }
</span><span class="result ok">// ],
</span><span class="result ok">// Person: [
</span><span class="result ok">// {
</span><span class="result ok">// name: 'name',
</span><span class="result ok">// type: 'string'
</span><span class="result ok">// },
</span><span class="result ok">// {
</span><span class="result ok">// name: 'wallet',
</span><span class="result ok">// type: 'address'
</span><span class="result ok">// }
</span><span class="result ok">// ]
</span><span class="result ok">// }
</span><span class="result ok">// }
</span>
TypedDataEncoder.getPrimaryType(types)
<span class="result ok">// 'Mail'
</span>
TypedDataEncoder.hash(domain, types, value)
<span class="result ok">// '0xbe609aee343fb3c4b28e1df9e632fca64fcfaede20f02e86244efddf30957bd2'
</span>
TypedDataEncoder.hashDomain(domain)
<span class="result ok">// '0xf2cee375fa42b42143804025fc449deafd50cc031ca257e0b194a650a912090f'
</span></div><a name="utils--solidity-hashing"></a><a name="hashing-algorithms--utils--solidity-hashing"></a><h2 class="show-anchors"><div>Solidity Hashing Algorithms<div class="anchors"><a class="self" href="/v5/api/utils/hashing/#utils--solidity-hashing"></a></div></div></h2><p>When using the Solidity <code class="inline">abi.packEncoded(...)</code> function, a non-standard <i>tightly packed</i> version of encoding is used. These functions implement the tightly packing algorithm.</p>
<a name="utils-solidityPack"></a><div class="property show-anchors"><div class="signature"><span class="path">ethers</span><span class="symbol">.</span><span class="path">utils</span><span class="symbol">.</span><span class="method">solidityPack</span><span class="symbol">(</span> <span class="param">types</span> <span class="symbol">,</span> <span class="param">values</span> <span class="symbol">)</span> <span class="arrow">&rArr;</span> <span class="returns">string&lt; <a href="/v5/api/utils/bytes/#DataHexString">DataHexString</a> &gt;</span><div class="anchors"><a class="self" href="/v5/api/utils/hashing/#utils-solidityPack"></a><a class="source" href="https://github.com/ethers-io/ethers.js/blob/master/packages/solidity/src.ts/index.ts#L75">source</a></div></div><div class="body"><p>Returns the non-standard encoded <i>values</i> packed according to their respective type in <i>types</i>.</p>
</div></div><a name="utils-solidityKeccak256"></a><div class="property show-anchors"><div class="signature"><span class="path">ethers</span><span class="symbol">.</span><span class="path">utils</span><span class="symbol">.</span><span class="method">solidityKeccak256</span><span class="symbol">(</span> <span class="param">types</span> <span class="symbol">,</span> <span class="param">values</span> <span class="symbol">)</span> <span class="arrow">&rArr;</span> <span class="returns">string&lt; <a href="/v5/api/utils/bytes/#DataHexString">DataHexString</a>&lt; 32 &gt; &gt;</span><div class="anchors"><a class="self" href="/v5/api/utils/hashing/#utils-solidityKeccak256"></a><a class="source" href="https://github.com/ethers-io/ethers.js/blob/master/packages/solidity/src.ts/index.ts#L84">source</a></div></div><div class="body"><p>Returns the <a href="https://en.wikipedia.org/wiki/SHA-3">KECCAK256</a> of the non-standard encoded <i>values</i> packed according to their respective type in <i>types</i>.</p>
</div></div><a name="utils-soliditySha256"></a><div class="property show-anchors"><div class="signature"><span class="path">ethers</span><span class="symbol">.</span><span class="path">utils</span><span class="symbol">.</span><span class="method">soliditySha256</span><span class="symbol">(</span> <span class="param">types</span> <span class="symbol">,</span> <span class="param">values</span> <span class="symbol">)</span> <span class="arrow">&rArr;</span> <span class="returns">string&lt; <a href="/v5/api/utils/bytes/#DataHexString">DataHexString</a>&lt; 32 &gt; &gt;</span><div class="anchors"><a class="self" href="/v5/api/utils/hashing/#utils-soliditySha256"></a><a class="source" href="https://github.com/ethers-io/ethers.js/blob/master/packages/solidity/src.ts/index.ts#L88">source</a></div></div><div class="body"><p>Returns the <a href="https://en.wikipedia.org/wiki/SHA-2">SHA2-256</a> of the non-standard encoded <i>values</i> packed according to their respective type in <i>types</i>.</p>
</div></div><div class="code-title"><div>Solidity Hashing</div></div><div class="code">utils.solidityPack([ "int16", "uint48" ], [ -1, 12 ])
<span class="result ok">// '0xffff00000000000c'
</span>
utils.solidityPack([ "string", "uint8" ], [ "Hello", 3 ])
<span class="result ok">// '0x48656c6c6f03'
</span>
utils.solidityKeccak256([ "int16", "uint48" ], [ -1, 12 ])
<span class="result ok">// '0x81da7abb5c9c7515f57dab2fc946f01217ab52f3bd8958bc36bd55894451a93c'
</span>
utils.soliditySha256([ "int16", "uint48" ], [ -1, 12 ])
<span class="result ok">// '0xa5580fb602f6e2ba9c588011dc4e6c2335e0f5d970dc45869db8f217efc6911a'
</span>
<span class="comment">// As a short example of the non-distinguished nature of
</span><span class="comment">// Solidity tight-packing (which is why it is inappropriate
</span><span class="comment">// for many things from a security point of view), consider
</span><span class="comment">// the following examples are all equal, despite representing
</span><span class="comment">// very different values and layouts.
</span>
utils.solidityPack([ "string", "string" ], [ "hello", "world01" ])
<span class="result ok">// '0x68656c6c6f776f726c643031'
</span>
utils.solidityPack([ "string", "string" ], [ "helloworld", "01" ])
<span class="result ok">// '0x68656c6c6f776f726c643031'
</span>
utils.solidityPack([ "string", "string", "uint16" ], [ "hell", "oworld", 0x3031 ])
<span class="result ok">// '0x68656c6c6f776f726c643031'
</span>
utils.solidityPack([ "uint96" ], [ "32309054545061485574011236401" ])
<span class="result ok">// '0x68656c6c6f776f726c643031'
</span></div>
<div class="footer">
<div class="nav previous"><a href="/v5/api/utils/fixednumber/"><span class="arrow">&larr;</span>FixedNumber</a></div>
<div class="nav next"><a href="/v5/api/utils/hdnode/">HD Wallet<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 License</a>. Generated on November 22, 2020, 11:5pm.</div>
</div>
<script src="/v5/static/script.js" type="text/javascript"></script>
<!--EXTRASCRIPT-->
</body>
</html>