{"id":34584515,"url":"https://github.com/exodusoss/bytes","last_synced_at":"2026-03-04T09:01:14.193Z","repository":{"id":328001004,"uuid":"885154008","full_name":"ExodusOSS/bytes","owner":"ExodusOSS","description":"Data structures handling","archived":false,"fork":false,"pushed_at":"2026-02-28T17:07:37.000Z","size":41445,"stargazers_count":12,"open_issues_count":8,"forks_count":5,"subscribers_count":1,"default_branch":"main","last_synced_at":"2026-02-28T17:51:33.782Z","etag":null,"topics":["base32","base58","base58check","base64","bech32","bech32m","encoding","hex","textdecoder","textencoder","uint8array","utf16","utf8","wif"],"latest_commit_sha":null,"homepage":"https://npmjs.com/@exodus/bytes","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ExodusOSS.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2024-11-08T03:52:36.000Z","updated_at":"2026-02-28T17:07:41.000Z","dependencies_parsed_at":"2026-01-02T11:02:14.252Z","dependency_job_id":"cea04b27-e369-4166-947b-8440eb72af11","html_url":"https://github.com/ExodusOSS/bytes","commit_stats":null,"previous_names":["exodusoss/bytes"],"tags_count":29,"template":false,"template_full_name":null,"purl":"pkg:github/ExodusOSS/bytes","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ExodusOSS%2Fbytes","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ExodusOSS%2Fbytes/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ExodusOSS%2Fbytes/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ExodusOSS%2Fbytes/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ExodusOSS","download_url":"https://codeload.github.com/ExodusOSS/bytes/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ExodusOSS%2Fbytes/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30076935,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-04T08:01:56.766Z","status":"ssl_error","status_checked_at":"2026-03-04T08:00:42.919Z","response_time":59,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["base32","base58","base58check","base64","bech32","bech32m","encoding","hex","textdecoder","textencoder","uint8array","utf16","utf8","wif"],"created_at":"2025-12-24T10:28:39.102Z","updated_at":"2026-03-04T09:01:14.186Z","avatar_url":"https://github.com/ExodusOSS.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# @exodus/bytes\n\n[![](https://img.shields.io/npm/v/@exodus/bytes?style=flat-square)](https://npmjs.org/package/@exodus/bytes)\n[![](https://img.shields.io/github/release/ExodusOSS/bytes?style=flat-square\u0026logo=github)](https://github.com/ExodusOSS/bytes/releases)\n[![](https://img.shields.io/npm/dm/@exodus/bytes?style=flat-square)](https://www.npmcharts.com/compare/@exodus/bytes?minimal=true)\n[![](https://img.shields.io/npm/l/@exodus/bytes?style=flat-square\u0026color=blue)](https://github.com/ExodusOSS/bytes/blob/HEAD/LICENSE)\n[![](https://img.shields.io/github/check-runs/ExodusOSS/bytes/main?style=flat-square\u0026logo=github)](https://github.com/ExodusOSS/bytes/actions/workflows/test.yml?query=branch%3Amain)\n[![](https://img.shields.io/badge/Documentation-3178c6?style=flat-square\u0026logo=TypeScript\u0026logoColor=fff)](https://exodusoss.github.io/bytes/)\n\n`Uint8Array` conversion to and from `base64`, `base32`, `base58`, `hex`, `utf8`, `utf16`, `bech32` and `wif`\n\nAnd a [`TextEncoder` / `TextDecoder` polyfill](#textencoder--textdecoder-polyfill)\n\nSee [documentation](https://exodusoss.github.io/bytes/).\n\n## Strict\n\nPerforms proper input validation, ensures no garbage-in-garbage-out\n\nTested in CI with [@exodus/test](https://github.com/ExodusMovement/test#exodustest) on:\n\n[![Node.js](https://img.shields.io/badge/Node.js-338750?style=for-the-badge\u0026logo=Node.js\u0026logoColor=FFF)](https://nodejs.org/api/test.html)\n[![Deno](https://img.shields.io/badge/Deno-121417?style=for-the-badge\u0026logo=Deno\u0026logoColor=FFF)](https://deno.com/)\n[![Bun](https://img.shields.io/badge/Bun-F472B6?style=for-the-badge\u0026logo=Bun\u0026logoColor=FFF)](https://bun.sh/)\n[![Electron](https://img.shields.io/badge/Electron-2F3242?style=for-the-badge\u0026logo=Electron\u0026logoColor=A2ECFB)](http://electronjs.org/)\n[![workerd](https://img.shields.io/badge/workerd-F38020?style=for-the-badge\u0026logo=cloudflareworkers\u0026logoColor=FFF)](https://github.com/cloudflare/workerd)\\\n[![Chrome](https://img.shields.io/badge/Chrome-4285F4?style=for-the-badge\u0026logo=GoogleChrome\u0026logoColor=FFF)](https://www.chromium.org/Home/)\n[![WebKit](https://img.shields.io/badge/WebKit-006CFF?style=for-the-badge\u0026logo=Safari\u0026logoColor=FFF)](http://webkit.org/)\n[![Firefox](https://img.shields.io/badge/Firefox-FF7139?style=for-the-badge\u0026logo=Firefox\u0026logoColor=FFF)](https://github.com/mozilla-firefox)\n[![Servo](https://img.shields.io/badge/Servo-009D9A?style=for-the-badge)](https://servo.org/)\\\n[![Hermes](https://img.shields.io/badge/Hermes-282C34?style=for-the-badge\u0026logo=React)](https://hermesengine.dev)\n[![V8](https://img.shields.io/badge/V8-4285F4?style=for-the-badge\u0026logo=V8\u0026logoColor=white)](https://v8.dev/docs/d8)\n[![JavaScriptCore](https://img.shields.io/badge/JavaScriptCore-006CFF?style=for-the-badge)](https://docs.webkit.org/Deep%20Dive/JSC/JavaScriptCore.html)\n[![SpiderMonkey](https://img.shields.io/badge/SpiderMonkey-FFD681?style=for-the-badge)](https://spidermonkey.dev/)\\\n[![QuickJS](https://img.shields.io/badge/QuickJS-E58200?style=for-the-badge)](https://github.com/quickjs-ng/quickjs)\n[![XS](https://img.shields.io/badge/XS-0B307A?style=for-the-badge)](https://github.com/Moddable-OpenSource/moddable)\n[![GraalJS](https://img.shields.io/badge/GraalJS-C74634?style=for-the-badge)](https://github.com/oracle/graaljs)\n\n## Fast\n\n* `10-20x` faster than `Buffer` polyfill\n* `2-10x` faster than `iconv-lite`\n\nThe above was for the js fallback\n\nIt's up to `100x` when native impl is available \\\ne.g. in `utf8fromString` on Hermes / React Native or `fromHex` in Chrome\n\nAlso:\n* `3-8x` faster than `bs58`\n* `10-30x` faster than `@scure/base` (or `\u003e100x` on Node.js \u003c25)\n* Faster in `utf8toString` / `utf8fromString` than `Buffer` or `TextDecoder` / `TextEncoder` on Node.js\n\nSee [Performance](./Performance.md) for more info\n\n## TextEncoder / TextDecoder polyfill\n\n```js\nimport { TextDecoder, TextEncoder } from '@exodus/bytes/encoding.js'\nimport { TextDecoderStream, TextEncoderStream } from '@exodus/bytes/encoding.js' // Requires Streams\n```\n\nLess than half the bundle size of [text-encoding](https://npmjs.com/text-encoding), [whatwg-encoding](https://npmjs.com/whatwg-encoding) or [iconv-lite](https://npmjs.com/iconv-lite) (gzipped or not).\\\nAlso [much faster](#fast) than all of those.\n\n\u003e [!TIP]\n\u003e See also the [lite version](#lite-version) to get this down to 8 KiB gzipped.\n\nSpec compliant, passing WPT and covered with extra tests.\\\nMoreover, tests for this library uncovered [bugs in all major implementations](https://docs.google.com/spreadsheets/d/1pdEefRG6r9fZy61WHGz0TKSt8cO4ISWqlpBN5KntIvQ/edit).\\\nIncluding all three major browser engines being wrong at UTF-8.\\\nSee [WPT pull request](https://github.com/web-platform-tests/wpt/pull/56892).\n\nIt works correctly even in environments that have native implementations broken (that's all of them currently).\\\nRuns (and passes WPT) on Node.js built without ICU.\n\n\u003e [!NOTE]\n\u003e [Faster than Node.js native implementation on Node.js](https://github.com/nodejs/node/issues/61041#issuecomment-3649242024).\n\u003e\n\u003e The JS multi-byte version is as fast as native impl in Node.js and browsers, but (unlike them) returns correct results.\n\u003e\n\u003e For encodings where native version is known to be fast and correct, it is automatically used.\\\n\u003e Some single-byte encodings are faster than native in all three major browser engines.\n\nSee [analysis table](https://docs.google.com/spreadsheets/d/1pdEefRG6r9fZy61WHGz0TKSt8cO4ISWqlpBN5KntIvQ/edit) for more info.\n\n### Caveat: `TextDecoder` / `TextEncoder` APIs are lossy by default per spec\n\n_These are only provided as a compatibility layer, prefer hardened APIs instead in new code._\n\n * `TextDecoder` can (and should) be used with `{ fatal: true }` option for all purposes demanding correctness / lossless transforms\n\n * `TextEncoder` does not support a fatal mode per spec, it always performs replacement.\n\n   That is not suitable for hashing, cryptography or consensus applications.\\\n   Otherwise there would be non-equal strings with equal signatures and hashes — the collision is caused by the lossy transform of a JS string to bytes.\n   Those also survive e.g. `JSON.stringify`/`JSON.parse` or being sent over network.\n\n   Use strict APIs in new applications, see `utf8fromString` / `utf16fromString` below.\\\n   Those throw on non-well-formed strings by default.\n\n### Lite version\n\nAlternate exports exist that can help reduce bundle size, see comparison:\n\n| import | size |\n| - | - |\n| [@exodus/bytes/encoding-browser.js](#exodusbytesencoding-browserjs-) | \u003csub\u003e![](https://img.shields.io/bundlejs/size/@exodus/bytes/encoding-browser.js?style=flat-square)\u003c/sub\u003e |\n| [@exodus/bytes/encoding-lite.js](#exodusbytesencoding-litejs-) | \u003csub\u003e![](https://img.shields.io/bundlejs/size/@exodus/bytes/encoding-lite.js?style=flat-square)\u003c/sub\u003e |\n| [@exodus/bytes/encoding.js](#exodusbytesencodingjs-) | \u003csub\u003e![](https://img.shields.io/bundlejs/size/@exodus/bytes/encoding.js?style=flat-square)\u003c/sub\u003e |\n| `text-encoding` | \u003csub\u003e![](https://img.shields.io/bundlejs/size/text-encoding?style=flat-square)\u003c/sub\u003e |\n| `iconv-lite` | \u003csub\u003e![](https://img.shields.io/bundlejs/size/iconv-lite/lib/index.js?style=flat-square)\u003c/sub\u003e |\n| `whatwg-encoding` | \u003csub\u003e![](https://img.shields.io/bundlejs/size/whatwg-encoding?style=flat-square)\u003c/sub\u003e |\n\nLibraries are advised to use single-purpose hardened `@exodus/bytes/utf8.js` / `@exodus/bytes/utf16.js` APIs for Unicode.\n\nApplications (including React Native apps) are advised to load either `@exodus/bytes/encoding-lite.js` or `@exodus/bytes/encoding.js`\n(depending on whether legacy multi-byte support is needed) and use that as a global polyfill.\n\n#### `@exodus/bytes/encoding-lite.js`\n\nIf you don't need support for legacy multi-byte encodings.\n\nReduces the bundle size ~12x, while still keeping `utf-8`, `utf-16le`, `utf-16be` and all single-byte encodings specified by the spec.\nThe only difference is support for legacy multi-byte encodings.\n\nSee [the list of encodings](https://encoding.spec.whatwg.org/#names-and-labels).\n\nThis can be useful for example in React Native global TextDecoder polyfill,\nif you are sure that you don't need legacy multi-byte encodings support.\n\n#### `@exodus/bytes/encoding-browser.js`\n\nResolves to a tiny import in browser bundles, preferring native `TextDecoder` / `TextEncoder`.\n\nFor non-browsers (Node.js, React Native), loads a full implementation.\n\n\u003e [!NOTE]\n\u003e This is not the default behavior for `@exodus/bytes/encoding.js` because all major browser implementations have bugs,\n\u003e which `@exodus/bytes/encoding.js` fixes. Only use if you are ok with that.\n\n## API\n\n### @exodus/bytes/utf8.js \u003csub\u003e![](https://img.shields.io/bundlejs/size/@exodus/bytes/utf8.js?style=flat-square)\u003c/sub\u003e\n\nUTF-8 encoding/decoding\n\n```js\nimport { utf8fromString, utf8toString } from '@exodus/bytes/utf8.js'\n\n// loose\nimport { utf8fromStringLoose, utf8toStringLoose } from '@exodus/bytes/utf8.js'\n```\n\n_These methods by design encode/decode BOM (codepoint `U+FEFF` Byte Order Mark) as-is._\\\n_If you need BOM handling or detection, use `@exodus/bytes/encoding.js`_\n\n#### `utf8fromString(string, format = 'uint8')`\n\nEncode a string to UTF-8 bytes (strict mode)\n\nThrows on invalid Unicode (unpaired surrogates)\n\nThis is similar to the following snippet (but works on all engines):\n```js\n// Strict encode, requiring Unicode codepoints to be valid\nif (typeof string !== 'string' || !string.isWellFormed()) throw new TypeError()\nreturn new TextEncoder().encode(string)\n```\n\n#### `utf8fromStringLoose(string, format = 'uint8')`\n\nEncode a string to UTF-8 bytes (loose mode)\n\nReplaces invalid Unicode (unpaired surrogates) with replacement codepoints `U+FFFD`\nper [WHATWG Encoding](https://encoding.spec.whatwg.org/) specification.\n\n_Such replacement is a non-injective function, is irreversable and causes collisions.\\\nPrefer using strict throwing methods for cryptography applications._\n\nThis is similar to the following snippet (but works on all engines):\n```js\n// Loose encode, replacing invalid Unicode codepoints with U+FFFD\nif (typeof string !== 'string') throw new TypeError()\nreturn new TextEncoder().encode(string)\n```\n\n#### `utf8toString(arr)`\n\nDecode UTF-8 bytes to a string (strict mode)\n\nThrows on invalid UTF-8 byte sequences\n\nThis is similar to `new TextDecoder('utf-8', { fatal: true, ignoreBOM: true }).decode(arr)`,\nbut works on all engines.\n\n#### `utf8toStringLoose(arr)`\n\nDecode UTF-8 bytes to a string (loose mode)\n\nReplaces invalid UTF-8 byte sequences with replacement codepoints `U+FFFD`\nper [WHATWG Encoding](https://encoding.spec.whatwg.org/) specification.\n\n_Such replacement is a non-injective function, is irreversable and causes collisions.\\\nPrefer using strict throwing methods for cryptography applications._\n\nThis is similar to `new TextDecoder('utf-8', { ignoreBOM: true }).decode(arr)`,\nbut works on all engines.\n\n### @exodus/bytes/utf16.js \u003csub\u003e![](https://img.shields.io/bundlejs/size/@exodus/bytes/utf16.js?style=flat-square)\u003c/sub\u003e\n\nUTF-16 encoding/decoding\n\n```js\nimport { utf16fromString, utf16toString } from '@exodus/bytes/utf16.js'\n\n// loose\nimport { utf16fromStringLoose, utf16toStringLoose } from '@exodus/bytes/utf16.js'\n```\n\n_These methods by design encode/decode BOM (codepoint `U+FEFF` Byte Order Mark) as-is._\\\n_If you need BOM handling or detection, use `@exodus/bytes/encoding.js`_\n\n#### `utf16fromString(string, format = 'uint16')`\n\nEncode a string to UTF-16 bytes (strict mode)\n\nThrows on invalid Unicode (unpaired surrogates)\n\n#### `utf16fromStringLoose(string, format = 'uint16')`\n\nEncode a string to UTF-16 bytes (loose mode)\n\nReplaces invalid Unicode (unpaired surrogates) with replacement codepoints `U+FFFD`\nper [WHATWG Encoding](https://encoding.spec.whatwg.org/) specification.\n\n_Such replacement is a non-injective function, is irreversible and causes collisions.\\\nPrefer using strict throwing methods for cryptography applications._\n\n#### `utf16toString(arr, format = 'uint16')`\n\nDecode UTF-16 bytes to a string (strict mode)\n\nThrows on invalid UTF-16 byte sequences\n\nThrows on non-even byte length.\n\n#### `utf16toStringLoose(arr, format = 'uint16')`\n\nDecode UTF-16 bytes to a string (loose mode)\n\nReplaces invalid UTF-16 byte sequences with replacement codepoints `U+FFFD`\nper [WHATWG Encoding](https://encoding.spec.whatwg.org/) specification.\n\n_Such replacement is a non-injective function, is irreversible and causes collisions.\\\nPrefer using strict throwing methods for cryptography applications._\n\nThrows on non-even byte length.\n\n### @exodus/bytes/bigint.js \u003csub\u003e![](https://img.shields.io/bundlejs/size/@exodus/bytes/bigint.js?style=flat-square)\u003c/sub\u003e\n\nConvert between BigInt and Uint8Array\n\n```js\nimport { fromBigInt, toBigInt } from '@exodus/bytes/bigint.js'\n```\n\n#### `fromBigInt(bigint, { length, format = 'uint8' })`\n\nConvert a BigInt to a Uint8Array or Buffer\n\nThe output bytes are in big-endian format.\n\nThrows if the BigInt is negative or cannot fit into the specified length.\n\n#### `toBigInt(arr)`\n\nConvert a Uint8Array or Buffer to a BigInt\n\nThe bytes are interpreted as a big-endian unsigned integer.\n\n### @exodus/bytes/hex.js \u003csub\u003e![](https://img.shields.io/bundlejs/size/@exodus/bytes/hex.js?style=flat-square)\u003c/sub\u003e\n\nImplements Base16 from [RFC4648](https://datatracker.ietf.org/doc/html/rfc4648)\n(no differences from [RFC3548](https://datatracker.ietf.org/doc/html/rfc4648)).\n\n```js\nimport { fromHex, toHex } from '@exodus/bytes/hex.js'\n```\n\n#### `fromHex(string, format = 'uint8')`\n\nDecode a hex string to bytes\n\nUnlike `Buffer.from()`, throws on invalid input\n\n#### `toHex(arr)`\n\nEncode a `Uint8Array` to a lowercase hex string\n\n### @exodus/bytes/base64.js \u003csub\u003e![](https://img.shields.io/bundlejs/size/@exodus/bytes/base64.js?style=flat-square)\u003c/sub\u003e\n\nImplements base64 and base64url from [RFC4648](https://datatracker.ietf.org/doc/html/rfc4648)\n(no differences from [RFC3548](https://datatracker.ietf.org/doc/html/rfc4648)).\n\n```js\nimport { fromBase64, toBase64 } from '@exodus/bytes/base64.js'\nimport { fromBase64url, toBase64url } from '@exodus/bytes/base64.js'\nimport { fromBase64any } from '@exodus/bytes/base64.js'\n```\n\n#### `fromBase64(string, { format = 'uint8', padding = 'both' })`\n\nDecode a base64 string to bytes\n\nOperates in strict mode for last chunk, does not allow whitespace\n\n#### `fromBase64url(string, { format = 'uint8', padding = false })`\n\nDecode a base64url string to bytes\n\nOperates in strict mode for last chunk, does not allow whitespace\n\n#### `fromBase64any(string, { format = 'uint8', padding = 'both' })`\n\nDecode either base64 or base64url string to bytes\n\nAutomatically detects the variant based on characters present\n\n#### `toBase64(arr, { padding = true })`\n\nEncode a `Uint8Array` to a base64 string (RFC 4648)\n\n#### `toBase64url(arr, { padding = false })`\n\nEncode a `Uint8Array` to a base64url string (RFC 4648)\n\n### @exodus/bytes/base32.js \u003csub\u003e![](https://img.shields.io/bundlejs/size/@exodus/bytes/base32.js?style=flat-square)\u003c/sub\u003e\n\nImplements base32 and base32hex from [RFC4648](https://datatracker.ietf.org/doc/html/rfc4648)\n(no differences from [RFC3548](https://datatracker.ietf.org/doc/html/rfc4648)).\n\n```js\nimport { fromBase32, toBase32 } from '@exodus/bytes/base32.js'\nimport { fromBase32hex, toBase32hex } from '@exodus/bytes/base32.js'\n```\n\n#### `fromBase32(string, { format = 'uint8', padding = 'both' })`\n\nDecode a base32 string to bytes\n\nOperates in strict mode for last chunk, does not allow whitespace\n\n#### `fromBase32hex(string, { format = 'uint8', padding = 'both' })`\n\nDecode a base32hex string to bytes\n\nOperates in strict mode for last chunk, does not allow whitespace\n\n#### `fromBase32crockford(string, options)`\n\nDecode a Crockford base32 string to bytes\n\nOperates in strict mode for last chunk, does not allow whitespace\n\nCrockford base32 decoding follows extra mapping per spec: `LIli -\u003e 1, Oo -\u003e 0`\n\n#### `toBase32(arr, { padding = false })`\n\nEncode a `Uint8Array` to a base32 string (RFC 4648)\n\n#### `toBase32hex(arr, { padding = false })`\n\nEncode a `Uint8Array` to a base32hex string (RFC 4648)\n\n#### `toBase32crockford(arr, options)`\n\nEncode a `Uint8Array` to a Crockford base32 string\n\n### @exodus/bytes/bech32.js \u003csub\u003e![](https://img.shields.io/bundlejs/size/@exodus/bytes/bech32.js?style=flat-square)\u003c/sub\u003e\n\nImplements bech32 and bech32m from\n[BIP-0173](https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki#specification)\nand [BIP-0350](https://github.com/bitcoin/bips/blob/master/bip-0350.mediawiki#specification).\n\n```js\nimport { fromBech32, toBech32 } from '@exodus/bytes/bech32.js'\nimport { fromBech32m, toBech32m } from '@exodus/bytes/bech32.js'\nimport { getPrefix } from '@exodus/bytes/bech32.js'\n```\n\n#### `getPrefix(string, limit = 90)`\n\nExtract the prefix from a bech32 or bech32m string without full validation\n\nThis is a quick check that skips most validation.\n\n#### `fromBech32(string, limit = 90)`\n\nDecode a bech32 string to bytes\n\n#### `toBech32(prefix, bytes, limit = 90)`\n\nEncode bytes to a bech32 string\n\n#### `fromBech32m(string, limit = 90)`\n\nDecode a bech32m string to bytes\n\n#### `toBech32m(prefix, bytes, limit = 90)`\n\nEncode bytes to a bech32m string\n\n### @exodus/bytes/base58.js \u003csub\u003e![](https://img.shields.io/bundlejs/size/@exodus/bytes/base58.js?style=flat-square)\u003c/sub\u003e\n\nImplements [base58](https://www.ietf.org/archive/id/draft-msporny-base58-03.txt) encoding.\n\nSupports both standard base58 and XRP variant alphabets.\n\n```js\nimport { fromBase58, toBase58 } from '@exodus/bytes/base58.js'\nimport { fromBase58xrp, toBase58xrp } from '@exodus/bytes/base58.js'\n```\n\n#### `fromBase58(string, format = 'uint8')`\n\nDecode a base58 string to bytes\n\nUses the standard Bitcoin base58 alphabet\n\n#### `toBase58(arr)`\n\nEncode a `Uint8Array` to a base58 string\n\nUses the standard Bitcoin base58 alphabet\n\n#### `fromBase58xrp(string, format = 'uint8')`\n\nDecode a base58 string to bytes using XRP alphabet\n\nUses the XRP variant base58 alphabet\n\n#### `toBase58xrp(arr)`\n\nEncode a `Uint8Array` to a base58 string using XRP alphabet\n\nUses the XRP variant base58 alphabet\n\n### @exodus/bytes/base58check.js \u003csub\u003e![](https://img.shields.io/bundlejs/size/@exodus/bytes/base58check.js?style=flat-square)\u003c/sub\u003e\n\nImplements [base58check](https://en.bitcoin.it/wiki/Base58Check_encoding) encoding.\n\n```js\nimport { fromBase58check, toBase58check } from '@exodus/bytes/base58check.js'\nimport { fromBase58checkSync, toBase58checkSync } from '@exodus/bytes/base58check.js'\nimport { makeBase58check } from '@exodus/bytes/base58check.js'\n```\n\nOn non-Node.js, requires peer dependency [@noble/hashes](https://www.npmjs.com/package/@noble/hashes) to be installed.\n\n#### `async fromBase58check(string, format = 'uint8')`\n\nDecode a base58check string to bytes asynchronously\n\nValidates the checksum using double SHA-256\n\n#### `async toBase58check(arr)`\n\nEncode bytes to base58check string asynchronously\n\nUses double SHA-256 for checksum calculation\n\n#### `fromBase58checkSync(string, format = 'uint8')`\n\nDecode a base58check string to bytes synchronously\n\nValidates the checksum using double SHA-256\n\n#### `toBase58checkSync(arr)`\n\nEncode bytes to base58check string synchronously\n\nUses double SHA-256 for checksum calculation\n\n#### `makeBase58check(hashAlgo, hashAlgoSync)`\n\nCreate a base58check encoder/decoder with custom hash functions\n\n### @exodus/bytes/wif.js \u003csub\u003e![](https://img.shields.io/bundlejs/size/@exodus/bytes/wif.js?style=flat-square)\u003c/sub\u003e\n\nWallet Import Format (WIF) encoding and decoding.\n\n```js\nimport { fromWifString, toWifString } from '@exodus/bytes/wif.js'\nimport { fromWifStringSync, toWifStringSync } from '@exodus/bytes/wif.js'\n```\n\nOn non-Node.js, requires peer dependency [@noble/hashes](https://www.npmjs.com/package/@noble/hashes) to be installed.\n\n#### `async fromWifString(string[, version])`\n\nDecode a WIF string to WIF data\n\nReturns a promise that resolves to an object with `{ version, privateKey, compressed }`.\n\nThe optional `version` parameter validates the version byte.\n\nThrows if the WIF string is invalid or version doesn't match.\n\n#### `fromWifStringSync(string[, version])`\n\nDecode a WIF string to WIF data (synchronous)\n\nReturns an object with `{ version, privateKey, compressed }`.\n\nThe optional `version` parameter validates the version byte.\n\nThrows if the WIF string is invalid or version doesn't match.\n\n#### `async toWifString({ version, privateKey, compressed })`\n\nEncode WIF data to a WIF string\n\n#### `toWifStringSync({ version, privateKey, compressed })`\n\nEncode WIF data to a WIF string (synchronous)\n\n### @exodus/bytes/array.js \u003csub\u003e![](https://img.shields.io/bundlejs/size/@exodus/bytes/array.js?style=flat-square)\u003c/sub\u003e\n\nTypedArray utils and conversions.\n\n```js\nimport { typedCopyBytes, typedView } from '@exodus/bytes/array.js'\n```\n\n#### `typedCopyBytes(arr, format = 'uint8')`\n\nCreate a copy of TypedArray underlying bytes in the specified format (`'uint8'`, `'buffer'`, or `'arraybuffer'`)\n\nThis does not copy _values_, but copies the underlying bytes.\nThe result is similar to that of `typedView()`, but this function provides a copy, not a view of the same memory.\n\n\u003e [!WARNING]\n\u003e Copying underlying bytes from `Uint16Array` (or other with `BYTES_PER_ELEMENT \u003e 1`)\n\u003e is platform endianness-dependent.\n\n\u003e [!NOTE]\n\u003e Buffer might be pooled.\n\u003e Uint8Array return values are not pooled and match their underlying ArrayBuffer.\n\n#### `typedView(arr, format = 'uint8')`\n\nCreate a view of a TypedArray in the specified format (`'uint8'` or `'buffer'`)\n\n\u003e [!IMPORTANT]\n\u003e Does not copy data, returns a view on the same underlying buffer\n\n\u003e [!WARNING]\n\u003e Viewing `Uint16Array` (or other with `BYTES_PER_ELEMENT \u003e 1`) as bytes\n\u003e is platform endianness-dependent.\n\n### @exodus/bytes/encoding.js \u003csub\u003e![](https://img.shields.io/bundlejs/size/@exodus/bytes/encoding.js?style=flat-square)\u003c/sub\u003e\n\nImplements the [Encoding standard](https://encoding.spec.whatwg.org/):\n[TextDecoder](https://encoding.spec.whatwg.org/#interface-textdecoder),\n[TextEncoder](https://encoding.spec.whatwg.org/#interface-textencoder),\n[TextDecoderStream](https://encoding.spec.whatwg.org/#interface-textdecoderstream),\n[TextEncoderStream](https://encoding.spec.whatwg.org/#interface-textencoderstream),\nsome [hooks](https://encoding.spec.whatwg.org/#specification-hooks).\n\n```js\nimport { TextDecoder, TextEncoder } from '@exodus/bytes/encoding.js'\nimport { TextDecoderStream, TextEncoderStream } from '@exodus/bytes/encoding.js' // Requires Streams\nimport { isomorphicDecode, isomorphicEncode } from '@exodus/bytes/encoding.js'\n\n// Hooks for standards\nimport { getBOMEncoding, legacyHookDecode, labelToName, normalizeEncoding } from '@exodus/bytes/encoding.js'\n```\n\n#### `new TextDecoder(label = 'utf-8', { fatal = false, ignoreBOM = false })`\n\n[TextDecoder](https://encoding.spec.whatwg.org/#interface-textdecoder) implementation/polyfill.\n\nDecode bytes to strings according to [WHATWG Encoding](https://encoding.spec.whatwg.org) specification.\n\n#### `new TextEncoder()`\n\n[TextEncoder](https://encoding.spec.whatwg.org/#interface-textencoder) implementation/polyfill.\n\nEncode strings to UTF-8 bytes according to [WHATWG Encoding](https://encoding.spec.whatwg.org) specification.\n\n#### `new TextDecoderStream(label = 'utf-8', { fatal = false, ignoreBOM = false })`\n\n[TextDecoderStream](https://encoding.spec.whatwg.org/#interface-textdecoderstream) implementation/polyfill.\n\nA [Streams](https://streams.spec.whatwg.org/) wrapper for `TextDecoder`.\n\nRequires [Streams](https://streams.spec.whatwg.org/) to be either supported by the platform or\n[polyfilled](https://npmjs.com/package/web-streams-polyfill).\n\n#### `new TextEncoderStream()`\n\n[TextEncoderStream](https://encoding.spec.whatwg.org/#interface-textencoderstream) implementation/polyfill.\n\nA [Streams](https://streams.spec.whatwg.org/) wrapper for `TextEncoder`.\n\nRequires [Streams](https://streams.spec.whatwg.org/) to be either supported by the platform or\n[polyfilled](https://npmjs.com/package/web-streams-polyfill).\n\n#### `isomorphicDecode(input)`\n\nImplements [isomorphic decode](https://infra.spec.whatwg.org/#isomorphic-decode).\n\nGiven a `TypedArray` or an `ArrayBuffer` instance `input`, creates a string of the same length\nas input byteLength, using bytes from input as codepoints.\n\nE.g. for `Uint8Array` input, this is similar to `String.fromCodePoint(...input)`.\n\nWider `TypedArray` inputs, e.g. `Uint16Array`, are interpreted as underlying _bytes_.\n\n#### `isomorphicEncode(str)`\n\nImplements [isomorphic encode](https://infra.spec.whatwg.org/#isomorphic-encode).\n\nGiven a string, creates an `Uint8Array` of the same length with the string codepoints as byte values.\n\nAccepts only [isomorphic string](https://infra.spec.whatwg.org/#isomorphic-string) input\nand asserts that, throwing on any strings containing codepoints higher than `U+00FF`.\n\n#### `labelToName(label)`\n\nImplements [get an encoding from a string `label`](https://encoding.spec.whatwg.org/#concept-encoding-get).\n\nConvert an encoding [label](https://encoding.spec.whatwg.org/#names-and-labels) to its name,\nas a case-sensitive string.\n\nIf an encoding with that label does not exist, returns `null`.\n\nAll encoding names are also valid labels for corresponding encodings.\n\n#### `normalizeEncoding(label)`\n\nConvert an encoding [label](https://encoding.spec.whatwg.org/#names-and-labels) to its name,\nas an ASCII-lowercased string.\n\nIf an encoding with that label does not exist, returns `null`.\n\nThis is the same as [`decoder.encoding` getter](https://encoding.spec.whatwg.org/#dom-textdecoder-encoding),\nexcept that it:\n 1. Supports [`replacement` encoding](https://encoding.spec.whatwg.org/#replacement) and its\n    [labels](https://encoding.spec.whatwg.org/#ref-for-replacement%E2%91%A1)\n 2. Does not throw for invalid labels and instead returns `null`\n\nIt is identical to:\n```js\nlabelToName(label)?.toLowerCase() ?? null\n```\n\nAll encoding names are also valid labels for corresponding encodings.\n\n#### `getBOMEncoding(input)`\n\nImplements [BOM sniff](https://encoding.spec.whatwg.org/#bom-sniff) legacy hook.\n\nGiven a `TypedArray` or an `ArrayBuffer` instance `input`, returns either of:\n- `'utf-8'`, if `input` starts with UTF-8 byte order mark.\n- `'utf-16le'`, if `input` starts with UTF-16LE byte order mark.\n- `'utf-16be'`, if `input` starts with UTF-16BE byte order mark.\n- `null` otherwise.\n\n#### `legacyHookDecode(input, fallbackEncoding = 'utf-8')`\n\nImplements [decode](https://encoding.spec.whatwg.org/#decode) legacy hook.\n\nGiven a `TypedArray` or an `ArrayBuffer` instance `input` and an optional `fallbackEncoding`\nencoding [label](https://encoding.spec.whatwg.org/#names-and-labels),\nsniffs encoding from BOM with `fallbackEncoding` fallback and then\ndecodes the `input` using that encoding, skipping BOM if it was present.\n\nNotes:\n\n- BOM-sniffed encoding takes precedence over `fallbackEncoding` option per spec.\n  Use with care.\n- Always operates in non-fatal [mode](https://encoding.spec.whatwg.org/#textdecoder-error-mode),\n  aka replacement. It can convert different byte sequences to equal strings.\n\nThis method is similar to the following code, except that it doesn't support encoding labels and\nonly expects lowercased encoding name:\n\n```js\nnew TextDecoder(getBOMEncoding(input) ?? fallbackEncoding).decode(input)\n```\n\n### @exodus/bytes/encoding-lite.js \u003csub\u003e![](https://img.shields.io/bundlejs/size/@exodus/bytes/encoding-lite.js?style=flat-square)\u003c/sub\u003e\n\nThe exact same exports as `@exodus/bytes/encoding.js` are also exported as\n`@exodus/bytes/encoding-lite.js`, with the difference that the lite version does not load\nmulti-byte `TextDecoder` encodings by default to reduce bundle size ~12x.\n\n```js\nimport { TextDecoder, TextEncoder } from '@exodus/bytes/encoding-lite.js'\nimport { TextDecoderStream, TextEncoderStream } from '@exodus/bytes/encoding-lite.js' // Requires Streams\nimport { isomorphicDecode, isomorphicEncode } from '@exodus/bytes/encoding-lite.js'\n\n// Hooks for standards\nimport { getBOMEncoding, legacyHookDecode, labelToName, normalizeEncoding } from '@exodus/bytes/encoding-lite.js'\n```\n\nThe only affected encodings are: `gbk`, `gb18030`, `big5`, `euc-jp`, `iso-2022-jp`, `shift_jis`\nand their [labels](https://encoding.spec.whatwg.org/#names-and-labels) when used with `TextDecoder`.\n\nLegacy single-byte encodingds are loaded by default in both cases.\n\n`TextEncoder` and hooks for standards (including `labelToName` / `normalizeEncoding`) do not have any behavior\ndifferences in the lite version and support full range if inputs.\n\nTo avoid inconsistencies, the exported classes and methods are exactly the same objects.\n\n```console\n\u003e lite = require('@exodus/bytes/encoding-lite.js')\n[Module: null prototype] {\n  TextDecoder: [class TextDecoder],\n  TextDecoderStream: [class TextDecoderStream],\n  TextEncoder: [class TextEncoder],\n  TextEncoderStream: [class TextEncoderStream],\n  getBOMEncoding: [Function: getBOMEncoding],\n  labelToName: [Function: labelToName],\n  legacyHookDecode: [Function: legacyHookDecode],\n  normalizeEncoding: [Function: normalizeEncoding]\n}\n\u003e new lite.TextDecoder('big5').decode(Uint8Array.of(0x25))\nUncaught:\nError: Legacy multi-byte encodings are disabled in /encoding-lite.js, use /encoding.js for full encodings range support\n\n\u003e full = require('@exodus/bytes/encoding.js')\n[Module: null prototype] {\n  TextDecoder: [class TextDecoder],\n  TextDecoderStream: [class TextDecoderStream],\n  TextEncoder: [class TextEncoder],\n  TextEncoderStream: [class TextEncoderStream],\n  getBOMEncoding: [Function: getBOMEncoding],\n  labelToName: [Function: labelToName],\n  legacyHookDecode: [Function: legacyHookDecode],\n  normalizeEncoding: [Function: normalizeEncoding]\n}\n\u003e full.TextDecoder === lite.TextDecoder\ntrue\n\u003e new full.TextDecoder('big5').decode(Uint8Array.of(0x25))\n'%'\n\u003e new lite.TextDecoder('big5').decode(Uint8Array.of(0x25))\n'%'\n```\n\n### @exodus/bytes/encoding-browser.js \u003csub\u003e![](https://img.shields.io/bundlejs/size/@exodus/bytes/encoding-browser.js?style=flat-square)\u003c/sub\u003e\n\nSame as `@exodus/bytes/encoding.js`, but in browsers instead of polyfilling just uses whatever the\nbrowser provides, drastically reducing the bundle size (to less than 2 KiB gzipped).\n\nDoes not provide `isomorphicDecode` and `isomorphicEncode` exports.\n\n```js\nimport { TextDecoder, TextEncoder } from '@exodus/bytes/encoding-browser.js'\nimport { TextDecoderStream, TextEncoderStream } from '@exodus/bytes/encoding-browser.js' // Requires Streams\n\n// Hooks for standards\nimport { getBOMEncoding, legacyHookDecode, labelToName, normalizeEncoding } from '@exodus/bytes/encoding-browser.js'\n```\n\nUnder non-browser engines (Node.js, React Native, etc.) a full polyfill is used as those platforms\ndo not provide sufficiently complete / non-buggy `TextDecoder` APIs.\n\n\u003e [!NOTE]\n\u003e Implementations in browsers [have bugs](https://docs.google.com/spreadsheets/d/1pdEefRG6r9fZy61WHGz0TKSt8cO4ISWqlpBN5KntIvQ/edit),\n\u003e but they are fixing them and the expected update window is short.\\\n\u003e If you want to circumvent browser bugs, use full `@exodus/bytes/encoding.js` import.\n\n### @exodus/bytes/whatwg.js \u003csub\u003e![](https://img.shields.io/bundlejs/size/@exodus/bytes/whatwg.js?style=flat-square)\u003c/sub\u003e\n\nWHATWG helpers\n\n```js\nimport '@exodus/bytes/encoding.js' // For full legacy multi-byte encodings support\nimport { percentEncodeAfterEncoding } from '@exodus/bytes/whatwg.js'\n```\n\n#### `percentEncodeAfterEncoding(encoding, input, percentEncodeSet, spaceAsPlus = false)`\n\nImplements [percent-encode after encoding](https://url.spec.whatwg.org/#string-percent-encode-after-encoding)\nper WHATWG URL specification.\n\n\u003e [!IMPORTANT]\n\u003e You must import `@exodus/bytes/encoding.js` for this API to accept legacy multi-byte encodings.\n\nEncodings `utf16-le`, `utf16-be`, and `replacement` are not accepted.\n\n[C0 control percent-encode set](https://url.spec.whatwg.org/#c0-control-percent-encode-set) is\nalways percent-encoded.\n\n`percentEncodeSet` is an addition to that, and must be a string of unique increasing codepoints\nin range 0x20 - 0x7e, e.g. `' \"#\u003c\u003e'`.\n\nThis method accepts [DOMStrings](https://webidl.spec.whatwg.org/#idl-DOMString) and converts them\nto [USVStrings](https://webidl.spec.whatwg.org/#idl-USVString).\nThis is different from e.g. `encodeURI` and `encodeURIComponent` which throw on surrogates:\n```js\n\u003e percentEncodeAfterEncoding('utf8', '\\ud800', ' \"#$%\u0026+,/:;\u003c=\u003e?@[\\\\]^`{|}') // component\n'%EF%BF%BD'\n\u003e encodeURIComponent('\\ud800')\nUncaught URIError: URI malformed\n```\n\n## Changelog\n\nSee [GitHub Releases](https://github.com/ExodusOSS/bytes/releases) tab\n\n## License\n\n[MIT](./LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fexodusoss%2Fbytes","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fexodusoss%2Fbytes","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fexodusoss%2Fbytes/lists"}