2020-06-12 03:38:55 -04:00
|
|
|
_section: Hashing Algorithms @<hashing-algorithms>
|
2019-08-21 01:53:47 -04:00
|
|
|
|
2020-11-22 23:03:50 -05:00
|
|
|
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.
|
2019-08-21 01:53:47 -04:00
|
|
|
|
|
|
|
|
2020-06-12 03:38:55 -04:00
|
|
|
_subsection: Cryptographic Hash Functions @<cryptographic-hash-functions>
|
2019-08-21 01:53:47 -04:00
|
|
|
|
2020-02-02 07:58:29 -05:00
|
|
|
The [Cryptographic Hash Functions](link-wiki-cryptographichash)
|
2019-08-21 01:53:47 -04:00
|
|
|
are a specific family of hash functions.
|
|
|
|
|
2020-05-08 03:24:40 -04:00
|
|
|
_property: ethers.utils.id(text) => string<[[DataHexString]]<32>> @<utils-id> @SRC<hash>
|
2020-09-22 23:51:59 -04:00
|
|
|
The Ethereum Identity function computes the [KECCAK256](link-wiki-sha3) hash of the //text// bytes.
|
2020-05-08 03:24:40 -04:00
|
|
|
|
|
|
|
_property: ethers.utils.keccak256(aBytesLike) => string<[[DataHexString]]<32>> @<utils-keccak256> @SRC<keccak256>
|
2020-02-02 07:58:29 -05:00
|
|
|
Returns the [KECCAK256](link-wiki-sha3) digest //aBytesLike//.
|
2019-08-21 01:53:47 -04:00
|
|
|
|
2020-05-08 03:24:40 -04:00
|
|
|
_property: ethers.utils.ripemd160(aBytesLike) => string<[[DataHexString]]<20>> @<utils-ripemd160> @SRC<sha2>
|
2020-02-02 07:58:29 -05:00
|
|
|
Returns the [RIPEMD-160](link-wiki-ripemd) digest of //aBytesLike//.
|
2019-08-21 01:53:47 -04:00
|
|
|
|
2020-05-08 03:24:40 -04:00
|
|
|
_property: ethers.utils.sha256(aBytesLike) => string<[[DataHexString]]<32>> @<utils-sha256> @SRC<sha2:function.sha256>
|
2020-02-02 07:58:29 -05:00
|
|
|
Returns the [SHA2-256](link-wiki-sha2) digest of //aBytesLike//.
|
2019-08-21 01:53:47 -04:00
|
|
|
|
2020-05-08 03:24:40 -04:00
|
|
|
_property: ethers.utils.sha512(aBytesLike) => string<[[DataHexString]]<64>> @<utils-sha512> @SRC<sha2:function.sha512>
|
2020-02-02 07:58:29 -05:00
|
|
|
Returns the [SHA2-512](link-wiki-sha2) digest of //aBytesLike//.
|
2019-08-21 01:53:47 -04:00
|
|
|
|
2020-05-08 03:24:40 -04:00
|
|
|
_code: KECCAK256 @lang<javascript>
|
|
|
|
|
2021-06-04 01:17:56 -04:00
|
|
|
//_result:
|
2020-05-08 03:24:40 -04:00
|
|
|
utils.keccak256([ 0x12, 0x34 ])
|
2021-06-04 01:17:56 -04:00
|
|
|
//_log:
|
2020-05-08 03:24:40 -04:00
|
|
|
|
2021-06-04 01:17:56 -04:00
|
|
|
//_result:
|
2020-05-08 03:24:40 -04:00
|
|
|
utils.keccak256("0x")
|
2021-06-04 01:17:56 -04:00
|
|
|
//_log:
|
2020-05-08 03:24:40 -04:00
|
|
|
|
2021-06-04 01:17:56 -04:00
|
|
|
//_result:
|
2020-05-08 03:24:40 -04:00
|
|
|
utils.keccak256("0x1234")
|
2021-06-04 01:17:56 -04:00
|
|
|
//_log:
|
2020-05-08 03:24:40 -04:00
|
|
|
|
|
|
|
// The value MUST be data, such as:
|
|
|
|
// - an Array of numbers
|
|
|
|
// - a data hex string (e.g. "0x1234")
|
|
|
|
// - a Uint8Array
|
|
|
|
|
|
|
|
// Do NOT use UTF-8 strings that are not a DataHexstring
|
2021-06-04 01:17:56 -04:00
|
|
|
//_throws:
|
2020-05-08 03:24:40 -04:00
|
|
|
utils.keccak256("hello world")
|
2021-06-04 01:17:56 -04:00
|
|
|
//_log:
|
2020-05-08 03:24:40 -04:00
|
|
|
|
|
|
|
// If needed, convert strings to bytes first:
|
2021-06-04 01:17:56 -04:00
|
|
|
//_result:
|
2020-05-08 03:24:40 -04:00
|
|
|
utils.keccak256(utils.toUtf8Bytes("hello world"))
|
2021-06-04 01:17:56 -04:00
|
|
|
//_log:
|
2020-05-08 03:24:40 -04:00
|
|
|
|
|
|
|
// Or equivalently use the identity function:
|
2021-06-04 01:17:56 -04:00
|
|
|
//_result:
|
2020-05-08 03:24:40 -04:00
|
|
|
utils.id("hello world")
|
2021-06-04 01:17:56 -04:00
|
|
|
//_log:
|
2020-05-08 03:24:40 -04:00
|
|
|
|
|
|
|
// Keep in mind that the string "0x1234" represents TWO
|
|
|
|
// bytes (i.e. [ 0x12, 0x34 ]. If you wish to compute the
|
|
|
|
// hash of the 6 characters "0x1234", convert it to UTF-8
|
|
|
|
// bytes first using utils.toUtf8Bytes.
|
|
|
|
|
|
|
|
// Consider the following examples:
|
|
|
|
|
|
|
|
// Hash of TWO (2) bytes:
|
2021-06-04 01:17:56 -04:00
|
|
|
//_result:
|
2020-05-08 03:24:40 -04:00
|
|
|
utils.keccak256("0x1234")
|
2021-06-04 01:17:56 -04:00
|
|
|
//_log:
|
2020-05-08 03:24:40 -04:00
|
|
|
|
|
|
|
// Hash of TWO (2) bytes: (same result)
|
2021-06-04 01:17:56 -04:00
|
|
|
//_result:
|
2020-05-08 03:24:40 -04:00
|
|
|
utils.keccak256([ 0x12, 0x34 ])
|
2021-06-04 01:17:56 -04:00
|
|
|
//_log:
|
2020-05-08 03:24:40 -04:00
|
|
|
|
2021-06-04 01:17:56 -04:00
|
|
|
//_result:
|
|
|
|
bytes = utils.toUtf8Bytes("0x1234")
|
|
|
|
//_log:
|
2020-05-08 03:24:40 -04:00
|
|
|
|
|
|
|
// Hash of SIX (6) characters (different than above)
|
2021-06-04 01:17:56 -04:00
|
|
|
//_result:
|
2020-05-08 03:24:40 -04:00
|
|
|
utils.keccak256(bytes)
|
2021-06-04 01:17:56 -04:00
|
|
|
//_log:
|
2020-05-08 03:24:40 -04:00
|
|
|
|
|
|
|
// Hash of SIX (6) characters (same result)
|
2021-06-04 01:17:56 -04:00
|
|
|
//_result:
|
2020-05-08 03:24:40 -04:00
|
|
|
utils.id("0x1234")
|
2021-06-04 01:17:56 -04:00
|
|
|
//_log:
|
2020-05-08 03:24:40 -04:00
|
|
|
|
|
|
|
_code: RIPEMD160 @lang<javascript>
|
|
|
|
|
2021-06-04 01:17:56 -04:00
|
|
|
//_result:
|
2020-05-08 03:24:40 -04:00
|
|
|
utils.ripemd160("0x")
|
2021-06-04 01:17:56 -04:00
|
|
|
//_log:
|
2020-05-08 03:24:40 -04:00
|
|
|
|
2021-06-04 01:17:56 -04:00
|
|
|
//_result:
|
2020-05-08 03:24:40 -04:00
|
|
|
utils.ripemd160("0x1234")
|
2021-06-04 01:17:56 -04:00
|
|
|
//_log:
|
2020-05-08 03:24:40 -04:00
|
|
|
|
|
|
|
_code: SHA-2 @lang<javascript>
|
|
|
|
|
2021-06-04 01:17:56 -04:00
|
|
|
//_result:
|
2020-05-08 03:24:40 -04:00
|
|
|
utils.sha256("0x")
|
2021-06-04 01:17:56 -04:00
|
|
|
//_log:
|
2019-08-21 01:53:47 -04:00
|
|
|
|
2021-06-04 01:17:56 -04:00
|
|
|
//_result:
|
2020-05-08 03:24:40 -04:00
|
|
|
utils.sha256("0x1234")
|
2021-06-04 01:17:56 -04:00
|
|
|
//_log:
|
2019-08-21 01:53:47 -04:00
|
|
|
|
2021-06-04 01:17:56 -04:00
|
|
|
//_result:
|
2020-05-08 03:24:40 -04:00
|
|
|
utils.sha512("0x")
|
2021-06-04 01:17:56 -04:00
|
|
|
//_log:
|
2020-05-08 03:24:40 -04:00
|
|
|
|
2021-06-04 01:17:56 -04:00
|
|
|
//_result:
|
2020-05-08 03:24:40 -04:00
|
|
|
utils.sha512("0x1234")
|
2021-06-04 01:17:56 -04:00
|
|
|
//_log:
|
2020-05-08 03:24:40 -04:00
|
|
|
|
|
|
|
|
|
|
|
_subsection: HMAC @<utils--hmac>
|
|
|
|
|
|
|
|
_property: ethers.utils.computeHmac(algorithm, key, data) => string<[[DataHexString]]> @<utils-computeHmac> @SRC<sha2>
|
|
|
|
Returns the [HMAC](link-wiki-hmac) of //data// with //key//
|
|
|
|
using the [Algorithm](utils--hmac-supported-algorithm) //algorithm//.
|
|
|
|
|
|
|
|
_heading: **HMAC Supported Algorithms** @<utils--hmac-supported-algorithm> @SRC<sha2:enum.SupportedAlgorithm>
|
2019-08-21 01:53:47 -04:00
|
|
|
|
2020-02-02 00:52:20 -05:00
|
|
|
_property: ethers.utils.SupportedAlgorithm.sha256 => string
|
2020-02-02 07:58:29 -05:00
|
|
|
Use the [SHA2-256](link-wiki-sha2) hash algorithm.
|
2019-08-21 01:53:47 -04:00
|
|
|
|
2020-02-02 00:52:20 -05:00
|
|
|
_property: ethers.utils.SupportedAlgorithm.sha512 => string
|
2020-02-02 07:58:29 -05:00
|
|
|
Use the [SHA2-512](link-wiki-sha2) hash algorithm.
|
2019-08-21 01:53:47 -04:00
|
|
|
|
2020-05-08 03:24:40 -04:00
|
|
|
_code: HMAC @lang<javascript>
|
2019-08-21 01:53:47 -04:00
|
|
|
|
2020-11-22 23:03:50 -05:00
|
|
|
const key = "0x0102"
|
|
|
|
const data = "0x1234"
|
2021-06-04 01:17:56 -04:00
|
|
|
//_result:
|
2020-05-08 03:24:40 -04:00
|
|
|
utils.computeHmac("sha256", key, data)
|
2021-06-04 01:17:56 -04:00
|
|
|
//_log:
|
2019-08-21 01:53:47 -04:00
|
|
|
|
2020-05-08 03:24:40 -04:00
|
|
|
|
2020-06-12 03:38:55 -04:00
|
|
|
_subsection: Hashing Helpers @<utils--hashing-helpers>
|
2020-05-08 03:24:40 -04:00
|
|
|
|
|
|
|
_property: ethers.utils.hashMessage(message) => string<[[DataHexString]]<32>> @<utils-hashMessage> @SRC<hash>
|
2020-02-25 14:57:11 -05:00
|
|
|
Computes the [[link-eip-191]] personal message digest of //message//. Personal messages are
|
2020-02-02 00:52:20 -05:00
|
|
|
converted to UTF-8 bytes and prefixed with ``\\x19Ethereum Signed Message:``
|
2019-08-21 01:53:47 -04:00
|
|
|
and the length of //message//.
|
|
|
|
|
2020-05-08 03:24:40 -04:00
|
|
|
_code: Hashing Messages @lang<javascript>
|
|
|
|
|
2020-11-22 23:03:50 -05:00
|
|
|
// Hashing a string message
|
2021-06-04 01:17:56 -04:00
|
|
|
//_result:
|
2020-11-22 23:03:50 -05:00
|
|
|
utils.hashMessage("Hello World")
|
2021-06-04 01:17:56 -04:00
|
|
|
//_log:
|
2020-11-22 23:03:50 -05:00
|
|
|
|
|
|
|
// Hashing binary data (also "Hello World", but as bytes)
|
2021-06-04 01:17:56 -04:00
|
|
|
//_result:
|
2020-11-22 23:03:50 -05:00
|
|
|
utils.hashMessage( [ 72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100 ])
|
2021-06-04 01:17:56 -04:00
|
|
|
//_log:
|
2020-11-22 23:03:50 -05:00
|
|
|
|
|
|
|
// NOTE: It is important to understand how strings and binary
|
|
|
|
// data is handled differently. A string is ALWAYS processed
|
|
|
|
// as the bytes of the string, so a hexstring MUST be
|
|
|
|
// converted to an ArrayLike object first.
|
2020-05-08 03:24:40 -04:00
|
|
|
|
2020-11-22 23:03:50 -05:00
|
|
|
// Hashing a hex string is the same as hashing a STRING
|
|
|
|
// Note: this is the hash of the 4 characters [ '0', 'x', '4', '2' ]
|
2021-06-04 01:17:56 -04:00
|
|
|
//_result:
|
2020-11-22 23:03:50 -05:00
|
|
|
utils.hashMessage("0x42")
|
2021-06-04 01:17:56 -04:00
|
|
|
//_log:
|
2020-11-22 23:03:50 -05:00
|
|
|
|
|
|
|
// Hashing the binary data
|
|
|
|
// Note: this is the hash of the 1 byte [ 0x42 ]
|
2021-06-04 01:17:56 -04:00
|
|
|
//_result:
|
2020-11-22 23:03:50 -05:00
|
|
|
utils.hashMessage([ 0x42 ])
|
2021-06-04 01:17:56 -04:00
|
|
|
//_log:
|
2020-11-22 23:03:50 -05:00
|
|
|
|
|
|
|
// Hashing the binary data
|
|
|
|
// Note: similarly, this is the hash of the 1 byte [ 0x42 ]
|
2021-06-04 01:17:56 -04:00
|
|
|
//_result:
|
2020-11-22 23:03:50 -05:00
|
|
|
utils.hashMessage(utils.arrayify("0x42"))
|
2021-06-04 01:17:56 -04:00
|
|
|
//_log:
|
2020-11-22 23:03:50 -05:00
|
|
|
|
|
|
|
|
|
|
|
_property: ethers.utils.namehash(name) => string<[[DataHexString]]<32>> @<utils-namehash> @SRC<hash>
|
|
|
|
Returns the [ENS Namehash](link-namehash) of //name//.
|
2020-05-08 03:24:40 -04:00
|
|
|
|
|
|
|
_code: Namehash @lang<javascript>
|
|
|
|
|
2021-06-04 01:17:56 -04:00
|
|
|
//_result:
|
2020-05-08 03:24:40 -04:00
|
|
|
utils.namehash("")
|
2021-06-04 01:17:56 -04:00
|
|
|
//_log:
|
2020-05-08 03:24:40 -04:00
|
|
|
|
2021-06-04 01:17:56 -04:00
|
|
|
//_result:
|
2020-05-08 03:24:40 -04:00
|
|
|
utils.namehash("eth")
|
2021-06-04 01:17:56 -04:00
|
|
|
//_log:
|
2020-05-08 03:24:40 -04:00
|
|
|
|
2021-06-04 01:17:56 -04:00
|
|
|
//_result:
|
2020-05-08 03:24:40 -04:00
|
|
|
utils.namehash("ricmoo.firefly.eth")
|
2021-06-04 01:17:56 -04:00
|
|
|
//_log:
|
2020-05-08 03:24:40 -04:00
|
|
|
|
2021-06-04 01:17:56 -04:00
|
|
|
//_result:
|
2020-05-08 03:24:40 -04:00
|
|
|
utils.namehash("ricmoo.xyz")
|
2021-06-04 01:17:56 -04:00
|
|
|
//_log:
|
2020-05-08 03:24:40 -04:00
|
|
|
|
2020-11-22 23:03:50 -05:00
|
|
|
_heading: Typed Data Encoder @<TypedDataEncoder> @SRC<hash:class.TypedDataEncoder>
|
|
|
|
|
|
|
|
The **TypedDataEncoder** is used to compute the various encoded data required
|
|
|
|
for [[link-eip-712]] signed data.
|
|
|
|
|
|
|
|
Signed data requires a domain, list of structures and their members and the data
|
|
|
|
itself.
|
|
|
|
|
|
|
|
The **domain** is an object with values for any of the standard domain
|
|
|
|
properties.
|
|
|
|
|
|
|
|
The **types** is an object with each property being the name of a structure, mapping
|
|
|
|
to an array of field descriptions. It should **not** include the ``EIP712Domain``
|
|
|
|
property unless it is required as a child structure of another.
|
|
|
|
|
|
|
|
_note: Experimental Feature (this exported class name will change)
|
|
|
|
This is still an experimental feature. If using it, please specify the **exact**
|
|
|
|
version of ethers you are using (e.g. spcify ``"5.0.18"``, **not** ``"^5.0.18"``) as
|
|
|
|
the exported class name will be renamed from ``_TypedDataEncoder`` to ``TypedDataEncoder`` once
|
|
|
|
it has been used in the field a bit.
|
|
|
|
|
|
|
|
_property: ethers.utils._TypedDataEncoder.from(types) => [TypedDataEncoder] @<TypedDataEncoder-from> @SRC<hash:TypedDataEncoder.from>
|
|
|
|
|
|
|
|
Creates a new **TypedDataEncoder** for //types//. This object is a fairly
|
|
|
|
low-level object that most developers should not require using instances
|
|
|
|
directly.
|
|
|
|
|
|
|
|
Most developers will find the static class methods below the most useful.
|
|
|
|
|
|
|
|
_property: TypedDataEncoder.encode(domain, types, values) => string @<TypedDataEncoder-encode> @SRC<hash:staticmethod.TypedDataEncoder.encode>
|
|
|
|
|
|
|
|
Encodes the Returns the hashed [[link-eip-712]] domain.
|
|
|
|
|
|
|
|
_property: TypedDataEncoder.getPayload(domain, types, value) => any @<TypedDataEncoder-getPayload> @SRC<hash:TypedDataEncoder.getPayload>
|
|
|
|
|
|
|
|
Returns the standard payload used by various JSON-RPC ``eth_signTypedData*``
|
|
|
|
calls.
|
|
|
|
|
|
|
|
All domain values and entries in value are normalized and the types are
|
|
|
|
verified.
|
|
|
|
|
|
|
|
_property: TypedDataEncoder.getPrimaryType(types) => string @<TypedDataEncoder-getPrimaryType> @SRC<hash:TypedDataEncoder.getPrimaryType>
|
|
|
|
|
|
|
|
Constructs a directed acyclic graph of the types and returns the
|
|
|
|
root type, which can be used as the **primaryType** for [[link-eip-712]]
|
|
|
|
payloads.
|
|
|
|
|
|
|
|
_property: TypedDataEncoder.hash(domain, types, values) => string<[[DataHexString]]<32>> @<TypedDataEncoder-hash> @SRC<hash:staticmethod.TypedDataEncoder.hash>
|
|
|
|
|
|
|
|
Returns the computed [[link-eip-712]] hash.
|
|
|
|
|
|
|
|
_property: TypedDataEncoder.hashDomain(domain) => string<[[DataHexString]]<32>> @<TypedDataEncoder-hashDomain> @SRC<hash:TypedDataEncoder.hashDomain>
|
|
|
|
|
|
|
|
Returns the hashed [[link-eip-712]] domain.
|
|
|
|
|
|
|
|
_property: TypedDataEncoder.resolveNames(domain, types, value, resolveName) => Promise<any> @<TypedDataEncoder-resolveNames> @SRC<hash:TypedDataEncoder.resolveNames>
|
|
|
|
|
|
|
|
Returns a copy of value, where any leaf value with a type of ``address`` will have
|
|
|
|
been recursively replacedwith the value of calling //resolveName// with that value.
|
|
|
|
|
|
|
|
_code: Typed Data Example @lang<javascript>
|
|
|
|
|
2021-06-04 01:17:56 -04:00
|
|
|
//_hide: TypedDataEncoder = ethers.utils._TypedDataEncoder
|
2020-11-22 23:03:50 -05:00
|
|
|
|
2021-06-04 01:17:56 -04:00
|
|
|
domain = {
|
2020-11-22 23:03:50 -05:00
|
|
|
name: 'Ether Mail',
|
|
|
|
version: '1',
|
|
|
|
chainId: 1,
|
|
|
|
verifyingContract: '0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC'
|
2021-06-04 01:17:56 -04:00
|
|
|
};
|
2020-11-22 23:03:50 -05:00
|
|
|
|
|
|
|
// The named list of all type definitions
|
2021-06-04 01:17:56 -04:00
|
|
|
types = {
|
2020-11-22 23:03:50 -05:00
|
|
|
Person: [
|
|
|
|
{ name: 'name', type: 'string' },
|
|
|
|
{ name: 'wallet', type: 'address' }
|
|
|
|
],
|
|
|
|
Mail: [
|
|
|
|
{ name: 'from', type: 'Person' },
|
|
|
|
{ name: 'to', type: 'Person' },
|
|
|
|
{ name: 'contents', type: 'string' }
|
|
|
|
]
|
2021-06-04 01:17:56 -04:00
|
|
|
};
|
2020-11-22 23:03:50 -05:00
|
|
|
|
|
|
|
// The data to sign
|
2021-06-04 01:17:56 -04:00
|
|
|
value = {
|
2020-11-22 23:03:50 -05:00
|
|
|
from: {
|
|
|
|
name: 'Cow',
|
|
|
|
wallet: '0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826'
|
|
|
|
},
|
|
|
|
to: {
|
|
|
|
name: 'Bob',
|
|
|
|
wallet: '0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB'
|
|
|
|
},
|
|
|
|
contents: 'Hello, Bob!'
|
2021-06-04 01:17:56 -04:00
|
|
|
};
|
2020-11-22 23:03:50 -05:00
|
|
|
|
2021-06-04 01:17:56 -04:00
|
|
|
//_result:
|
2020-11-22 23:03:50 -05:00
|
|
|
TypedDataEncoder.encode(domain, types, value)
|
2021-06-04 01:17:56 -04:00
|
|
|
//_log:
|
2020-11-22 23:03:50 -05:00
|
|
|
|
2021-06-04 01:17:56 -04:00
|
|
|
//_result:
|
2020-11-22 23:03:50 -05:00
|
|
|
TypedDataEncoder.getPayload(domain, types, value)
|
2021-06-04 01:17:56 -04:00
|
|
|
//_log:
|
2020-11-22 23:03:50 -05:00
|
|
|
|
2021-06-04 01:17:56 -04:00
|
|
|
//_result:
|
2020-11-22 23:03:50 -05:00
|
|
|
TypedDataEncoder.getPrimaryType(types)
|
2021-06-04 01:17:56 -04:00
|
|
|
//_log:
|
2020-11-22 23:03:50 -05:00
|
|
|
|
2021-06-04 01:17:56 -04:00
|
|
|
//_result:
|
2020-11-22 23:03:50 -05:00
|
|
|
TypedDataEncoder.hash(domain, types, value)
|
2021-06-04 01:17:56 -04:00
|
|
|
//_log:
|
2020-11-22 23:03:50 -05:00
|
|
|
|
2021-06-04 01:17:56 -04:00
|
|
|
//_result:
|
2020-11-22 23:03:50 -05:00
|
|
|
TypedDataEncoder.hashDomain(domain)
|
2021-06-04 01:17:56 -04:00
|
|
|
//_log:
|
2020-11-22 23:03:50 -05:00
|
|
|
|
2019-08-21 01:53:47 -04:00
|
|
|
|
2020-06-12 03:38:55 -04:00
|
|
|
_subsection: Solidity Hashing Algorithms @<utils--solidity-hashing>
|
2019-08-21 01:53:47 -04:00
|
|
|
|
|
|
|
When using the Solidity ``abi.packEncoded(...)`` function, a non-standard
|
|
|
|
//tightly packed// version of encoding is used. These functions implement
|
|
|
|
the tightly packing algorithm.
|
|
|
|
|
2020-05-08 03:24:40 -04:00
|
|
|
_property: ethers.utils.solidityPack(types, values) => string<[[DataHexString]]> @<utils-solidityPack> @SRC<solidity:pack>
|
|
|
|
Returns the non-standard encoded //values// packed according to
|
2020-10-03 13:30:15 -03:00
|
|
|
their respective type in //types//.
|
2020-05-08 03:24:40 -04:00
|
|
|
|
|
|
|
_property: ethers.utils.solidityKeccak256(types, values) => string<[[DataHexString]]<32>> @<utils-solidityKeccak256> @SRC<solidity:keccak256>
|
|
|
|
Returns the [KECCAK256](link-wiki-sha3) of the non-standard encoded //values// packed
|
|
|
|
according to their respective type in //types//.
|
|
|
|
|
|
|
|
_property: ethers.utils.soliditySha256(types, values) => string<[[DataHexString]]<32>> @<utils-soliditySha256> @SRC<solidity:sha256>
|
|
|
|
Returns the [SHA2-256](link-wiki-sha2) of the non-standard encoded //values// packed
|
|
|
|
according to their respective type in //types//.
|
|
|
|
|
|
|
|
_code: Solidity Hashing @lang<javascript>
|
|
|
|
|
2021-06-04 01:17:56 -04:00
|
|
|
//_result:
|
2020-05-08 03:24:40 -04:00
|
|
|
utils.solidityPack([ "int16", "uint48" ], [ -1, 12 ])
|
2021-06-04 01:17:56 -04:00
|
|
|
//_log:
|
2019-08-21 01:53:47 -04:00
|
|
|
|
2021-06-04 01:17:56 -04:00
|
|
|
//_result:
|
2020-05-08 03:24:40 -04:00
|
|
|
utils.solidityPack([ "string", "uint8" ], [ "Hello", 3 ])
|
2021-06-04 01:17:56 -04:00
|
|
|
//_log:
|
2019-08-21 01:53:47 -04:00
|
|
|
|
2021-06-04 01:17:56 -04:00
|
|
|
//_result:
|
2020-05-08 03:24:40 -04:00
|
|
|
utils.solidityKeccak256([ "int16", "uint48" ], [ -1, 12 ])
|
2021-06-04 01:17:56 -04:00
|
|
|
//_log:
|
2019-08-21 01:53:47 -04:00
|
|
|
|
2021-06-04 01:17:56 -04:00
|
|
|
//_result:
|
2020-05-08 03:24:40 -04:00
|
|
|
utils.soliditySha256([ "int16", "uint48" ], [ -1, 12 ])
|
2021-06-04 01:17:56 -04:00
|
|
|
//_log:
|
2019-08-21 01:53:47 -04:00
|
|
|
|
2020-11-22 23:03:50 -05:00
|
|
|
// As a short example of the non-distinguished nature of
|
|
|
|
// Solidity tight-packing (which is why it is inappropriate
|
|
|
|
// for many things from a security point of view), consider
|
|
|
|
// the following examples are all equal, despite representing
|
|
|
|
// very different values and layouts.
|
|
|
|
|
2021-06-04 01:17:56 -04:00
|
|
|
//_result:
|
2020-11-22 23:03:50 -05:00
|
|
|
utils.solidityPack([ "string", "string" ], [ "hello", "world01" ])
|
2021-06-04 01:17:56 -04:00
|
|
|
//_log:
|
2020-11-22 23:03:50 -05:00
|
|
|
|
2021-06-04 01:17:56 -04:00
|
|
|
//_result:
|
2020-11-22 23:03:50 -05:00
|
|
|
utils.solidityPack([ "string", "string" ], [ "helloworld", "01" ])
|
2021-06-04 01:17:56 -04:00
|
|
|
//_log:
|
2020-11-22 23:03:50 -05:00
|
|
|
|
2021-06-04 01:17:56 -04:00
|
|
|
//_result:
|
2020-11-22 23:03:50 -05:00
|
|
|
utils.solidityPack([ "string", "string", "uint16" ], [ "hell", "oworld", 0x3031 ])
|
2021-06-04 01:17:56 -04:00
|
|
|
//_log:
|
2020-11-22 23:03:50 -05:00
|
|
|
|
2021-06-04 01:17:56 -04:00
|
|
|
//_result:
|
2020-11-22 23:03:50 -05:00
|
|
|
utils.solidityPack([ "uint96" ], [ "32309054545061485574011236401" ])
|
2021-06-04 01:17:56 -04:00
|
|
|
//_log:
|