{"id":13652442,"url":"https://github.com/LimeChain/as-scale-codec","last_synced_at":"2025-04-23T03:30:50.586Z","repository":{"id":54175335,"uuid":"261099915","full_name":"LimeChain/as-scale-codec","owner":"LimeChain","description":"AssemblyScript implementation of the SCALE codec used in the Parity Substrate framework.","archived":false,"fork":false,"pushed_at":"2023-07-12T06:12:11.000Z","size":341,"stargazers_count":20,"open_issues_count":5,"forks_count":6,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-04-10T02:44:15.414Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/LimeChain.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":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2020-05-04T06:45:41.000Z","updated_at":"2025-03-26T10:11:23.000Z","dependencies_parsed_at":"2024-01-03T05:15:08.957Z","dependency_job_id":"a61ceed7-abf4-4a58-98b2-f3adfc7d8353","html_url":"https://github.com/LimeChain/as-scale-codec","commit_stats":{"total_commits":173,"total_committers":6,"mean_commits":"28.833333333333332","dds":0.5780346820809248,"last_synced_commit":"9982786da2f97f7aea5900643c05d112dc0ae32d"},"previous_names":["limechain/assemblyscript-scale-codec"],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LimeChain%2Fas-scale-codec","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LimeChain%2Fas-scale-codec/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LimeChain%2Fas-scale-codec/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LimeChain%2Fas-scale-codec/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/LimeChain","download_url":"https://codeload.github.com/LimeChain/as-scale-codec/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249737714,"owners_count":21318494,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":[],"created_at":"2024-08-02T02:00:59.390Z","updated_at":"2025-04-23T03:30:48.194Z","avatar_url":"https://github.com/LimeChain.png","language":"TypeScript","funding_links":[],"categories":["SCALE Codec"],"sub_categories":[],"readme":"\u003ch2 align=\"center\"\u003eAssemblyScript SCALE Codec\u003c/h2\u003e\n\n\n[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)\n![Tests](https://github.com/LimeChain/as-scale-codec/workflows/Tests/badge.svg)\n[![npm version](https://img.shields.io/npm/v/as-scale-codec?color=light-green\u0026label=npm%20package)](https://img.shields.io/npm/v/as-scale-codec?color=light-green\u0026label=npm%20package)\n![GitHub release (latest by date)](https://img.shields.io/github/v/release/LimeChain/as-scale-codec)\n\n**as-scale-codec** is AssemblyScript implementation of Polkadot SCALE Codec. The codec is used as a communication mechanism between Polkadot Hosts and Polkadot Runtimes.\n\nMore detailed information about the SCALE codec specification can be found [here](https://substrate.dev/docs/en/knowledgebase/advanced/codec).\n\nThis AssemblyScript implementation of the codec is funded by [Web3 Foundation](https://web3.foundation/) via their [Open Grants Program](https://github.com/w3f/Open-Grants-Program)! :pray:\n![WEB3 Badge](./web3_badge_black.png)\n# Supported types\nThe following table shows the status of the types and their arrays:\n\n|       Type        |       Support      | Array Support | \n|----------------------|:--------------------:|:------:|\n| `Fixed width number` | ✅ | ✅ |\n| `Compact Int`        | ✅ |✅ |\n| `Big Integer` | :small_orange_diamond: *Limited Support* | :small_orange_diamond: *Limited Support* |\n| `Byte` |✅ |✅ | \n| `Bool` | ✅| ✅|\n| `Hash` |✅ | :heavy_minus_sign: |\n| `String` | ✅|✅ | \n| `Map` |✅| :heavy_minus_sign: | \n\nThe following table shows the status of the fixed width numbers:\n\n| Тype | `8` | `16` | `32` | `64` | `128` | `256` | \n|--|:--:|:--:|:--:|:--:|:--:|:--:|\n| `int` | ✅ | ✅| ✅|✅ | :heavy_minus_sign:|  :heavy_minus_sign:|\n| `uint` | ✅ | ✅| ✅|✅ |✅ |:heavy_minus_sign:|\n\n\n## Special Types\n\n- **Compact Int** - [Documentation](https://substrate.dev/docs/en/knowledgebase/advanced/codec#compactgeneral-integers)\n\n## **Getting Started**  \n*You can find more information on AssemblyScript and how to get started with it in the AssemblyScript docs -\u003e [https://www.assemblyscript.org/introduction.html](https://www.assemblyscript.org/introduction.html)*\n\n1. In your AssemblyScript project execute:\n\n    ```bash\n    npm install as-scale-codec\n    ```\n2. Once you have the library installed in your AssemblyScript project you can use it in your `assembly` files by importing the files from `as-scale-codec`.\n \nDetailed examples of the exported by the library types are listed below:\n  \n\n## Types\n\n### Encoding\n\nEvery type has а **toU8a** function. It encodes type value into an array of bytes\n\n```jsx\nimport { Bool, Byte, ScaleString, Hash, CompactInt } from \"as-scale-codec\"\nimport { Int8, Int16, Int32, Int64 } from \"as-scale-codec\"\nimport { UInt8, UInt16, UInt32, UInt64, UInt128 } from \"as-scale-codec\"\n// ScaleMap\nconst scaleMap = new ScaleMap\u003cInt32, Bool\u003e();\nscaleMap.set(new Int32(1), new Bool(false));\nscaleMap.toU8a() // =\u003e [4, 1, 0, 0, 0, 0];\n\n// Bool\nconst scaleBool = new Bool(true);\nscaleBool.toU8a() // =\u003e [0x01]\n\n// Byte\nconst scaleByte = new Byte(0x01);\nscaleByte.toU8a() // =\u003e [0x01]\n\n// String\nconst scaleString = new ScaleString(\"a\");\nscaleString.toU8a() // =\u003e [0x04, 0x61] \n\n// Hash\nconst scaleHash = new Hash([0xff, 0x00, 0xab]);\nscaleHash.toU8a() // =\u003e [0xff, 0x00, 0xab, 0x00, ... 0x00] (32 bytes long)\n\n// Compact Int\nconst scaleCompactInt = new CompactInt(1);\nscaleCompactInt.toU8a() // =\u003e [0x04]\n\n// Int\nconst scaleInt8 = new Int8(-1);\nscaleInt8.toU8a() // =\u003e [0xff]\n\nconst scaleInt16 = new Int16(-1);\nscaleInt16.toU8a() // =\u003e [0xff, 0xff]\n\nconst scaleInt32 = new Int32(-1);\nscaleInt32.toU8a() // =\u003e [0xff, 0xff, 0xff, 0xff]\n\nconst scaleInt64 = new Int64(-1);\nscaleInt64.toU8a() // =\u003e [0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]\n\n// UInt\nconst scaleUInt8 = new UInt8(1);\nscaleUInt8.toU8a() // =\u003e [0x01]\n\nconst scaleUInt16 = new UInt16(1);\nscaleUInt16.toU8a() // =\u003e [0x01, 0x00]\n\nconst scaleUInt32 = new UInt32(1);\nscaleUInt32.toU8a() // =\u003e [0x01, 0x00, 0x00, 0x00]\n\nconst scaleUInt64 = new UInt64(1);\nscaleUInt64.toU8a() // =\u003e [0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]\n\nconst scaleUInt128 = new UInt128(u128.fromU64(18446744073709551615));\nscaleUInt128.toU8a() // =\u003e [0x13, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]\n```\n\n### Decoding\n\nEvery type has a **static** function **fromU8a**. It decodes an array of bytes to the desired type\n\n```jsx\nimport { ScaleMap, Bool, Byte, ScaleString, Hash, CompactInt } from \"as-scale-codec\"\nimport { Int8, Int16, Int32, Int64 } from \"as-scale-codec\"\nimport { UInt8, UInt16, UInt32, UInt64, UInt128 } from \"as-scale-codec\"\n\n// Bool\nBool.fromU8a([0x01]); // =\u003e new Bool(true)\n\n// Byte\nByte.fromU8a([0x01]); // =\u003e new Byte(0x01)\n\n// String\nByte.fromU8a([0x04, 0x61]); // =\u003e new ScaleString('a')\n\n// Hash\nHash.fromU8a([0xff, 0x00, 0xab]); \n// =\u003e [0xff, 0x00, 0xab, 0x00, ... 0x00] (32 bytes long)\n\nScaleMap\u003cInt32, Bool\u003e.fromU8a([4, 1, 0, 0, 0, 0]);\n// =\u003e const scaleMap = new ScaleMap\u003cInt32, Bool\u003e()\n// =\u003e scaleMap.set(new Int32(1), new Bool(false))\n\n// Compact Int\nCompactInt.fromU8a([0x04]); // =\u003e new CompactInt(1)\n\n// Int\nInt8.fromU8a([0xff]); // =\u003e new Int8(-1)\nInt16.fromU8a([0xff, 0xff]); // =\u003e new Int16(-1)\nInt32.fromU8a([0xff, 0xff, 0xff, 0xff]); // =\u003e new Int32(-1)\nInt64.fromU8a([0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]); // =\u003e new Int64(-1)\n\n// UInt\nUInt8.fromU8a([0x01]); // =\u003e new UInt8(1)\nUInt16.fromU8a([0x01, 0x00]); // =\u003e new UInt16(1)\nUInt32.fromU8a([0x01, 0x00, 0x00, 0x00]); // =\u003e new UInt32(1)\nUInt64.fromU8a([0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]); // =\u003e new UInt64(1)\nUInt128.fromU8a([0x33, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]);\n// =\u003e 340282366920938463444927863358058659840\n```\n\n## Arrays\n\n### Encoding\n\nEvery array has **toU8a** function. It encodes the array values into an array of SCALE encoded bytes.\n\n```jsx\nimport { BoolArray, ByteArray, IntArray, StringArray } from \"as-scale-codec\"\n\n// Bool Array\nconst boolArray = new BoolArray([true, false, true]);\nboolArray.toU8a(); // =\u003e [0x0c, 0x01, 0x00, 0x01]\n\n// Byte Array\nconst byteArray = new ByteArray([0x01, 0x01, 0x01]);\nbyteArray.toU8a(); // =\u003e [0x0c, 0x01, 0x01, 0x01]\n\n// Int Array\nconst intArray = new IntArray([16384, 2, 3, 4]);\nintArray.toU8a() // =\u003e [0x10, 0x02, 0x00, 0x01, 0x00, 0x08, 0x0c, 0x10]\n\n// String Array\nconst stringArray = new StringArray([\"hello\", \"world\"]);\nstringArray.toU8a() // =\u003e [0x08, 0x14, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x14, 0x77, 0x6f, 0x72, 0x6c, 0x64]\n```\n\n### Decoding\n\nEvery array has a **static** function **fromU8a**. It decodes an array of SCALE encoded bytes and creates an array instance of the desired type.\n\n```jsx\nimport { BoolArray, ByteArray, IntArray, StringArray } from \"as-scale-codec\"\n\n// Bool Array\nBoolArray.fromU8a([0x0c, 0x01, 0x00, 0x01]); \n// =\u003e new BoolArray([true, false, true])\n\n// Byte Array\nByteArray.fromU8a([0x0c, 0x01, 0x01, 0x01])\n// =\u003e new ByteArray([0x01, 0x01, 0x01])\n\n// Int Array\nIntArray.fromU8a([0x10, 0x02, 0x00, 0x01, 0x00, 0x08, 0x0c, 0x10])\n// =\u003e new IntArray([16384, 2, 3, 4])\n\n// String Array\nStringArray.fromU8a([0x08, 0x14, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x14, 0x77, 0x6f, 0x72, 0x6c, 0x64])\n// =\u003e new StringArray([\"hello\", \"world\"])\n```\n\n# BytesReader\n\nIf you have an array of arbitrary SCALE encoded bytes that you need to decode, `BytesReader` class is a preferred way to do it:\n\n```jsx\nimport { BytesReader } from 'as-scale-codec';\n// Arbitrary SCALE encoded bytes\nconst bytes: u8[] = [\n    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,\n    69, 0, 0, 0,\n    110, 125, 239, 2,\n    56, 97, 115, 45, 115, 99, 97, 108, 101, 45, 99, 111, 100, 101, 99,\n    128, 1, 10, 0, 0, 0, 2, 2, 1, 123, 33, 3, 1, 35, 34, 5, 8, 22, 52, 1, 0, 0, 0, 1, 1, 1, 56, 21, 142, 13, 13, 1,\n    0\n];\n// Instantiate BytesReader instance with SCALE encoded bytes\nconst bytesReader = new BytesReader(bytes);\n\n// Read Int64\nbytesReader.readInto\u003cInt64\u003e();\n// =\u003e new Int(-1)\n\n// Read UInt32\nbytesReader.readInto\u003cUInt32\u003e();\n// =\u003e new UInt32(69)\n\n// Read CompactInt\nbytesReader.readInto\u003cCompactInt\u003e();\n// =\u003e new CompactInt(12312411)\n\n// Read ScaleString\nbytesReader.readInto\u003cScaleString\u003e();\n// =\u003e new ScaleString(\"as-scale-codec\")\n\n// Read Hash\nbytesReader.readInto\u003cHash\u003e();\n// =\u003e new Hash([128, 1, 10, 0, 0, 0, 2, 2, 1, 123, 33, 3, 1, 35, 34, 5, 8, 22, 52, 1, 0, 0, 0, 1, 1, 1, 56, 21, 142, 13, 13, 1])\n\n// Read Bool\nbytesReader.readInto\u003cBool\u003e();\n// =\u003e new Bool(false)\n\n// If you have single SCALE encoded type, you can use static decodeInto\u003cT\u003e() function of BytesReader\n\nconst uInt64Bytes: u8[] = [1, 0, 0, 0, 0, 0, 0];\n// Read UInt64\nBytesReader.decodeInto\u003cUInt64\u003e(uInt64Bytes);\n// =\u003e new UInt64(1)\n\nconst hashBytes: u8[] = [0xff, 0x00, 0xab];\n// Read Hash\nBytesReader.decodeInto\u003cHash\u003e(hashBytes);\n// new Hash([0xff, 0x00, 0xab])\n\nconst mapBytes: u8[] = [2, 1, 0, 1, 0, 0, 0, 3, 0, 3, 0, 0, 0];\n// Read ScaleMap\nBytesReader.decodeInto\u003cScaleMap\u003cUInt16, UInt32\u003e\u003e(mapBytes);\n// =\u003e const scaleMap = new ScaleMap\u003cUInt16, UInt32\u003e();\n// =\u003e scaleMap.set(new UInt16(1), new UInt32(1))\n// =\u003e scaleMap.set(new UInt16(3), new UInt32(3))\n\nconst cmpBytes: u8[] = [169, 2];\n// Read CompactInt\nBytesReader.decodeInto\u003cCompactInt\u003e(cmpBytes);\n// new CompactInt(170)\n\nconst int8Bytes: u8[] = [0xff];\n// Read Int8\nBytesReader.decodeInto\u003cInt8\u003e(int8Bytes);\n// new Int8(-1)\n\n```\n\n# Miscellaneous\n\n### Convert bytes to hash\n\n```jsx\nHash.bytesToHash([0xff, 0x00, 0xab]); \n// =\u003e [0x00, ... , 0x00, 0xff, 0x00, 0xab] (32 bytes long)\n\nHash.bytesToHash([0xff, 0x00, ..., 0x00]); // (32 bytes long)\n// =\u003e [0xff, ... , 0x00] (32 bytes long)\n```\n\n# **Tests**\n\nIn order to run the unit tests, one must perform:\n\n```bash\nnpm run test\n```\n\n# **License**\nThis repository is licensed under [Apache 2.0 license](https://github.com/LimeChain/as-scale-codec/blob/master/LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FLimeChain%2Fas-scale-codec","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FLimeChain%2Fas-scale-codec","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FLimeChain%2Fas-scale-codec/lists"}