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

131 lines
5.2 KiB
Plaintext
Raw Normal View History

_title: Byte Manipulation
_section: Byte Manipulation
Tra la la...
_subsection: Types
_heading: Bytes @<bytes>
2019-12-14 06:05:10 +03:00
A **Bytes** is any object which is an
[Array](https:/\/developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array) or
[TypedArray](https:/\/developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/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.
_heading: BytesLike @<byteslike>
2019-12-14 06:05:10 +03:00
A **BytesLike** can be either a [[bytes]] or a [[datahexstring]].
_heading: DataHexstring @<datahexstring>
A **DataHexstring** is identical to a [[hexstring]] except that it has
an even number of nibbles, and therefore is a valid representation of
binary data as a string.
_heading: Hexstring @<hexstring>
2019-12-14 06:05:10 +03:00
A **hexstring** is a string which has a ``0x`` prefix followed by any
number of nibbles (i.e. case-insensitive hexidecumal characters, ``0-9`` and ``a-f``).
_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**
- **_vs** --- The [compact representation](https://link_here) of the **(r, s)** and **v**
- **recoveryParam** --- The normalized (i.e. 0 or 1) value of **v**
_heading: SignatureLike @<signaturelike>
A **SignatureLike** is similar to a [[signature]], except redundant properties
may be omitted.
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
2019-12-14 06:05:10 +03:00
_property: utils.isBytes(object) => boolean @<utils-isbytes> @TS<bytes:>
Returns true if and only if //object// is a valid [[bytes]].
2019-12-14 06:05:10 +03:00
_property: utils.isBytesLike(object) => boolean @<utils-isbyteslike> @TS<bytes:>
Returns true if and only if //object// is a [[bytes]] or [[datahexstring]].
2019-12-14 06:05:10 +03:00
_property: utils.isHexString(object, [ length ] ) => boolean @<utils-ishexstring> @TS<bytes:>
Returns true if and only if //object// is a valid hex string.
If //length// is specified and //object// is not a valid [[datahexstring]] of
//length// bytes, an InvalidArgument error is thrown.
_subsection: Converting between Arrays and Hexstrings
2019-12-14 06:05:10 +03:00
_property: utils.arrayify(datahexstringOrArrayish [ , options ]) => Uint8Array @<utils-arrayify> @TS<bytes:>
Converts //datahexstringOrArrayish// to a Uint8Array.
2019-12-14 06:05:10 +03:00
_property: utils.hexlify(hexstringOrArrayish) => string @<utils-hexlify> @TS<bytes:>
Converts //hexstringOrArrayish// to a [[datahexstring]].
2019-12-14 06:05:10 +03:00
_property: utils.hexValue(aBigNumberish) => string @<utils-hexvalue> @TS<bytes:>
Converts //aBigNumberish// to a [[hexstring]], with no __unnecessary__ leading
zeros.
_heading: Examples
_code: bytes-conversion.js
_subsection: Array Manipulation
2019-12-14 06:05:10 +03:00
_property: utils.concat(arrayOfBytesLike) => Uint8Array @<utils-concat> @TS<bytes:>
Concatenates all the [[byteslike]] in //arrayOfBytesLike// into a single Uint8Array.
2019-12-14 06:05:10 +03:00
_property: utils.stripZeros(aBytesLike) => Uint8Array @<utils-stripzeros> @TS<bytes:>
Returns a Uint8Array with all leading ``0`` bytes of //aBtyesLike// removed.
2019-12-14 06:05:10 +03:00
_property: utils.zeroPad(aBytesLike, length) => Uint8Array @<utils-zeropad> @TS<bytes:>
Retutns a Uint8Array of the data in //aBytesLike// with ``0`` bytes prepended to
//length// bytes long.
If //aBytesLike// is already longer than //length// bytes long, an InvalidArgument
error will be thrown.
_subsection: Hexstring Manipulation
2019-12-14 06:05:10 +03:00
_property: utils.hexConcat(arrayOfBytesLike) => [[datahexstring]] @<utils-hexconcat> @TS<bytes:>
Concatenates all the [[byteslike]] in //arrayOfBytesLike// into a single [[datahexstring]]
2019-12-14 06:05:10 +03:00
_property: utils.hexDataLength(aBytesLike) => [[datahexstring]] @<utils-hexdatalength> @TS<bytes:>
Returns the length (in bytes) of //aBytesLike//.
2019-12-14 06:05:10 +03:00
_property: utils.hexDataSlice(aBytesLike, offset [ , endOffset ] ) => [[datahexstring]] @<utils-hexdataslice> @TS<bytes:>
Returns a [[datahexstring]] representation of a slice of //aBytesLike//, from
//offset// (in bytes) to //endOffset// (in bytes). If //endOffset// is
omitted, the length of //aBytesLike// is used.
2019-12-14 06:05:10 +03:00
_property: utils.hexStripZeros(aBytesLike) => [[hexstring]] @<utils-hexstripzeros> @TS<bytes:>
Returns a [[hexstring]] representation of //aBytesLike// with all
leading zeros removed.
2019-12-14 06:05:10 +03:00
_property: utils.hexZeroPad(aBytesLike, length) => [[datahexstring]] @<utils-hexzeropad> @TS<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
2019-12-14 06:05:10 +03:00
_property: utils.joinSignature(aSignatureLike) => [[datahexstring]] @<utils-joinsignature> @TS<bytes:>
Return the flat-format of //aSignaturelike//, which is 65 bytes (130 nibbles)
long, concatenating the **r**, **s** and (normalized) **v** of a Signature.
2019-12-14 06:05:10 +03:00
_property: utils.splitSignature(aSignatureLikeOrBytesLike) => [[signature]] @<utils-splitsignature> @TS<bytes:>
Return the full expanded-format of //aSignaturelike// or a flat-format [[datahexstring]].
Any missing properties will be computed.