ethers.js/docs.wrm/api/utils/bytes.wrm

196 lines
6.9 KiB
Plaintext
Raw Normal View History

_section: Byte Manipulation
2021-02-08 22:52:31 +03:00
While there are many high-level APIs for interacting with
Ethereum, such as [Contracts](Contract) and [Providers](Provider),
a lot of the low level access requires byte manipulation
operations.
Many of these operations are used internally, but can also be
used to help normalize binary data representations from the
output of various functions and methods.
_subsection: Types
2020-05-08 10:24:40 +03:00
_heading: Bytes @<Bytes>
2019-12-14 06:05:10 +03:00
A **Bytes** is any object which is an
2020-02-02 15:58:29 +03:00
[Array](link-js-array) or [TypedArray](link-js-typedarray) with
each value in the valid byte range (i.e. between 0 and 255 inclusive),
or is an Object with a ``length`` property where each indexed property
is in the valid byte range.
2020-05-08 10:24:40 +03:00
_heading: BytesLike @<BytesLike>
2020-05-08 10:24:40 +03:00
A **BytesLike** can be either a [[Bytes]] or a [[DataHexString]].
2019-12-14 06:05:10 +03:00
2020-05-08 10:24:40 +03:00
_heading: DataHexString @<DataHexString>
2019-12-14 06:05:10 +03:00
2020-05-08 10:24:40 +03:00
A **DataHexstring** is identical to a [[HexString]] except that it has
2019-12-14 06:05:10 +03:00
an even number of nibbles, and therefore is a valid representation of
binary data as a string.
2020-05-08 10:24:40 +03:00
_heading: HexString @<HexString>
2020-01-10 09:01:00 +03:00
A **Hexstring** is a string which has a ``0x`` prefix followed by any
2020-10-03 19:30:15 +03:00
number of nibbles (i.e. case-insensitive hexadecimal characters, ``0-9`` and ``a-f``).
2020-05-08 10:24:40 +03:00
_heading: Signature @<Signature>
- **r** and **s** --- The x co-ordinate of **r** and the **s** value of the signature
- **v** --- The parity of the y co-ordinate of **r**
- **yParityAndS** --- The [compact representation](link-eip-2098) of the **s** and **v**
- **_vs** --- Deprecated property; renamed to yParityAndS
- **recoveryParam** --- The normalized (i.e. 0 or 1) value of **v**
- **compact** - The full siggnature using [compact representation](link-eip-2098)
2020-05-08 10:24:40 +03:00
_heading: Raw Signature @<signature-raw> @inherit<string\<[[DataHexString]]\<65\>\>>
2020-01-10 09:01:00 +03:00
2020-05-08 10:24:40 +03:00
A **Raw Signature** is a common Signature format where the r, s and v are
2020-10-03 19:30:15 +03:00
concatenated into a 65 byte (130 nibble) [[DataHexString]].
2020-01-10 09:01:00 +03:00
2020-05-08 10:24:40 +03:00
_heading: SignatureLike @<SignatureLike>
2020-05-08 10:24:40 +03:00
A **SignatureLike** is similar to a [[Signature]], except redundant properties
may be omitted or it may be a [[signature-raw]].
2019-12-14 06:05:10 +03:00
For example, if **_vs** is specified, **s** and **v** may be omitted. Likewise,
if **recoveryParam** is provided, **v** may be omitted (as in these cases the
missing values can be computed).
_subsection: Inspection
2020-05-08 10:24:40 +03:00
_property: ethers.utils.isBytes(object) => boolean @<utils-isBytes> @SRC<bytes>
Returns true if and only if //object// is a valid [[Bytes]].
2020-05-08 10:24:40 +03:00
_property: ethers.utils.isBytesLike(object) => boolean @<utils-isBytesLike> @SRC<bytes>
Returns true if and only if //object// is a [[Bytes]] or [[DataHexString]].
2020-05-08 10:24:40 +03:00
_property: ethers.utils.isHexString(object, [ length ] ) => boolean @<utils-isHexString> @SRC<bytes>
2019-12-14 06:05:10 +03:00
Returns true if and only if //object// is a valid hex string.
2020-05-08 10:24:40 +03:00
If //length// is specified and //object// is not a valid [[DataHexString]] of
2019-12-14 06:05:10 +03:00
//length// bytes, an InvalidArgument error is thrown.
_subsection: Converting between Arrays and Hexstrings
2020-05-08 10:24:40 +03:00
_property: ethers.utils.arrayify(DataHexStringOrArrayish [ , options ]) => Uint8Array @<utils-arrayify> @SRC<bytes>
Converts //DataHexStringOrArrayish// to a Uint8Array.
2020-05-08 10:24:40 +03:00
_property: ethers.utils.hexlify(hexstringOrArrayish) => string<[[DataHexString]]> @<utils-hexlify> @SRC<bytes>
Converts //hexstringOrArrayish// to a [[DataHexString]].
2020-05-08 10:24:40 +03:00
_property: ethers.utils.hexValue(aBigNumberish) => string<[[HexString]]> @<utils-hexValue> @SRC<bytes>
Converts //aBigNumberish// to a [[HexString]], with no __unnecessary__ leading
2019-12-14 06:05:10 +03:00
zeros.
2020-05-08 10:24:40 +03:00
_code: Examples @lang<javascript>
// Convert a hexstring to a Uint8Array
//_result:
arrayify("0x1234")
//_log:
// Convert an Array to a hexstring
//_result:
hexlify([1, 2, 3, 4])
//_log:
// Convert an Object to a hexstring
//_result:
hexlify({ length: 2, "0": 1, "1": 2 })
//_log:
// Convert an Array to a hexstring
//_result:
hexlify([ 1 ])
//_log:
// Convert a number to a stripped hex value
//_result:
hexValue(1)
//_log:
// Convert an Array to a stripped hex value
//_result:
hexValue([ 1, 2 ])
//_log:
_subsection: Array Manipulation
2020-02-25 22:57:11 +03:00
_property: ethers.utils.concat(arrayOfBytesLike) => Uint8Array @<utils-concat> @SRC<bytes>
2020-05-08 10:24:40 +03:00
Concatenates all the [[BytesLike]] in //arrayOfBytesLike// into a single Uint8Array.
2020-05-08 10:24:40 +03:00
_property: ethers.utils.stripZeros(aBytesLike) => Uint8Array @<utils-stripZeros> @SRC<bytes>
2019-12-14 06:05:10 +03:00
Returns a Uint8Array with all leading ``0`` bytes of //aBtyesLike// removed.
2020-05-08 10:24:40 +03:00
_property: ethers.utils.zeroPad(aBytesLike, length) => Uint8Array @<utils-zeroPad> @SRC<bytes>
2020-10-03 19:30:15 +03:00
Returns a Uint8Array of the data in //aBytesLike// with ``0`` bytes prepended to
2019-12-14 06:05:10 +03:00
//length// bytes long.
If //aBytesLike// is already longer than //length// bytes long, an InvalidArgument
error will be thrown.
_subsection: Hexstring Manipulation
2020-05-08 10:24:40 +03:00
_property: ethers.utils.hexConcat(arrayOfBytesLike) => string<[[DataHexString]]> @<utils-hexConcat> @SRC<bytes>
Concatenates all the [[BytesLike]] in //arrayOfBytesLike// into a single [[DataHexString]]
2020-05-08 10:24:40 +03:00
_property: ethers.utils.hexDataLength(aBytesLike) => string<[[DataHexString]]> @<utils-hexDataLength> @SRC<bytes>
Returns the length (in bytes) of //aBytesLike//.
2020-05-08 10:24:40 +03:00
_property: ethers.utils.hexDataSlice(aBytesLike, offset [ , endOffset ] ) => string<[[DataHexString]]> @<utils-hexDataSlice> @SRC<bytes>
Returns a [[DataHexString]] representation of a slice of //aBytesLike//, from
2019-12-14 06:05:10 +03:00
//offset// (in bytes) to //endOffset// (in bytes). If //endOffset// is
omitted, the length of //aBytesLike// is used.
2020-05-08 10:24:40 +03:00
_property: ethers.utils.hexStripZeros(aBytesLike) => string<[[HexString]]> @<utils-hexStripZeros> @SRC<bytes>
Returns a [[HexString]] representation of //aBytesLike// with all
2019-12-14 06:05:10 +03:00
leading zeros removed.
2020-05-08 10:24:40 +03:00
_property: ethers.utils.hexZeroPad(aBytesLike, length) => string<[[DataHexString]]> @<utils-hexZeroPad> @SRC<bytes>
Returns a [[DataHexString]] representation of //aBytesLike// padded to //length// bytes.
2019-12-14 06:05:10 +03:00
If //aBytesLike// is already longer than //length// bytes long, an InvalidArgument
error will be thrown.
_subsection: Signature Conversion
2020-05-08 10:24:40 +03:00
_property: ethers.utils.joinSignature(aSignatureLike) => string<[RawSignature](signature-raw)> @<utils-joinSignature> @SRC<bytes>
Return the raw-format of //aSignaturelike//, which is 65 bytes (130 nibbles)
2019-12-14 06:05:10 +03:00
long, concatenating the **r**, **s** and (normalized) **v** of a Signature.
2020-05-08 10:24:40 +03:00
_property: ethers.utils.splitSignature(aSignatureLikeOrBytesLike) => [[Signature]] @<utils-splitSignature> @SRC<bytes>
Return the full expanded-format of //aSignaturelike// or a raw-format [[DataHexString]].
2019-12-14 06:05:10 +03:00
Any missing properties will be computed.
2020-02-18 01:56:13 +03:00
_subsection: Random Bytes
2020-11-23 07:06:27 +03:00
_property: ethers.utils.randomBytes(length) => Uint8Array @<utils-randomBytes> @SRC<random/random>
2020-02-18 01:56:13 +03:00
Return a new Uint8Array of //length// random bytes.
2020-02-25 22:57:11 +03:00
2020-05-08 10:24:40 +03:00
_property: ethers.utils.shuffled(array) => Array<any> @<utils-shuffled> @SRC<random>
2020-02-25 22:57:11 +03:00
Return a copy of //array// shuffled using [[link-wiki-shuffle]].
_code: Examples @lang<javascript>
//_result:
utils.randomBytes(8)
//_log:
2020-05-08 10:24:40 +03:00
const data = [ 1, 2, 3, 4, 5, 6, 7 ];
// Returns a new Array
//_result:
2020-05-08 10:24:40 +03:00
utils.shuffled(data);
//_log:
2020-05-08 10:24:40 +03:00
// The Original is unscathed...
//_result:
2020-05-08 10:24:40 +03:00
data
//_log: