├── .github
└── workflows
│ └── main.yml
├── .gitignore
├── LICENSE
├── README.md
├── as-pect.config.js
├── assembly
├── AbstractInt.ts
├── Arrays
│ ├── AbstractArray.ts
│ ├── BoolArray.ts
│ ├── ByteArray.ts
│ ├── IntArray.ts
│ ├── StringArray.ts
│ ├── UInt128Array.ts
│ └── index.ts
├── Bool.ts
├── Byte.ts
├── BytesReader.ts
├── Hash.ts
├── Int
│ ├── CompactInt.ts
│ ├── Int16.ts
│ ├── Int32.ts
│ ├── Int64.ts
│ ├── Int8.ts
│ └── index.ts
├── ScaleMap.ts
├── ScaleString.ts
├── UInt
│ ├── UInt128.ts
│ ├── UInt16.ts
│ ├── UInt32.ts
│ ├── UInt64.ts
│ ├── UInt8.ts
│ └── index.ts
├── __tests__
│ ├── Arrays
│ │ ├── BoolArray.spec.ts
│ │ ├── ByteArray.spec.ts
│ │ ├── IntArray.spec.ts
│ │ ├── StringArray.spec.ts
│ │ └── UInt128Array.spec.ts
│ ├── Bool.spec.ts
│ ├── Byte.spec.ts
│ ├── BytesReader.spec.ts
│ ├── Hash.spec.ts
│ ├── Int.spec.ts
│ ├── ScaleMap.spec.ts
│ ├── ScaleString.spec.ts
│ ├── UInt.spec.ts
│ └── as-pect.d.ts
├── index.ts
├── interfaces
│ ├── Codec.ts
│ ├── DecodedData.ts
│ ├── UnwrappableCodec.ts
│ └── index.ts
├── tsconfig.json
└── utils
│ ├── Arrays.ts
│ ├── Bytes.ts
│ └── BytesBuffer.ts
├── example
├── README.md
├── as-pect.config.js
├── assembly
│ ├── __tests__
│ │ └── as-pect.d.ts
│ ├── index.ts
│ └── tsconfig.json
├── index.js
├── package-lock.json
└── package.json
├── index.js
├── package-lock.json
├── package.json
└── web3_badge_black.png
/.github/workflows/main.yml:
--------------------------------------------------------------------------------
1 | name: Tests
2 | on: [push]
3 | jobs:
4 | tests:
5 | runs-on: ubuntu-latest
6 | steps:
7 | - uses: actions/checkout@v2
8 | - name: run-tests
9 | run: |
10 | npm install
11 | npm run test:ci
12 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules/
2 | build/
3 | .idea/
4 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "[]"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright [yyyy] [name of copyright owner]
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
AssemblyScript SCALE Codec
2 |
3 |
4 | [](https://opensource.org/licenses/Apache-2.0)
5 | 
6 | [](https://img.shields.io/npm/v/as-scale-codec?color=light-green&label=npm%20package)
7 | 
8 |
9 | **as-scale-codec** is AssemblyScript implementation of Polkadot SCALE Codec. The codec is used as a communication mechanism between Polkadot Hosts and Polkadot Runtimes.
10 |
11 | More detailed information about the SCALE codec specification can be found [here](https://substrate.dev/docs/en/knowledgebase/advanced/codec).
12 |
13 | This 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:
14 | 
15 | # Supported types
16 | The following table shows the status of the types and their arrays:
17 |
18 | | Type | Support | Array Support |
19 | |----------------------|:--------------------:|:------:|
20 | | `Fixed width number` | ✅ | ✅ |
21 | | `Compact Int` | ✅ |✅ |
22 | | `Big Integer` | :small_orange_diamond: *Limited Support* | :small_orange_diamond: *Limited Support* |
23 | | `Byte` |✅ |✅ |
24 | | `Bool` | ✅| ✅|
25 | | `Hash` |✅ | :heavy_minus_sign: |
26 | | `String` | ✅|✅ |
27 | | `Map` |✅| :heavy_minus_sign: |
28 |
29 | The following table shows the status of the fixed width numbers:
30 |
31 | | Тype | `8` | `16` | `32` | `64` | `128` | `256` |
32 | |--|:--:|:--:|:--:|:--:|:--:|:--:|
33 | | `int` | ✅ | ✅| ✅|✅ | :heavy_minus_sign:| :heavy_minus_sign:|
34 | | `uint` | ✅ | ✅| ✅|✅ |✅ |:heavy_minus_sign:|
35 |
36 |
37 | ## Special Types
38 |
39 | - **Compact Int** - [Documentation](https://substrate.dev/docs/en/knowledgebase/advanced/codec#compactgeneral-integers)
40 |
41 | ## **Getting Started**
42 | *You can find more information on AssemblyScript and how to get started with it in the AssemblyScript docs -> [https://www.assemblyscript.org/introduction.html](https://www.assemblyscript.org/introduction.html)*
43 |
44 | 1. In your AssemblyScript project execute:
45 |
46 | ```bash
47 | npm install as-scale-codec
48 | ```
49 | 2. 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`.
50 |
51 | Detailed examples of the exported by the library types are listed below:
52 |
53 |
54 | ## Types
55 |
56 | ### Encoding
57 |
58 | Every type has а **toU8a** function. It encodes type value into an array of bytes
59 |
60 | ```jsx
61 | import { Bool, Byte, ScaleString, Hash, CompactInt } from "as-scale-codec"
62 | import { Int8, Int16, Int32, Int64 } from "as-scale-codec"
63 | import { UInt8, UInt16, UInt32, UInt64, UInt128 } from "as-scale-codec"
64 | // ScaleMap
65 | const scaleMap = new ScaleMap();
66 | scaleMap.set(new Int32(1), new Bool(false));
67 | scaleMap.toU8a() // => [4, 1, 0, 0, 0, 0];
68 |
69 | // Bool
70 | const scaleBool = new Bool(true);
71 | scaleBool.toU8a() // => [0x01]
72 |
73 | // Byte
74 | const scaleByte = new Byte(0x01);
75 | scaleByte.toU8a() // => [0x01]
76 |
77 | // String
78 | const scaleString = new ScaleString("a");
79 | scaleString.toU8a() // => [0x04, 0x61]
80 |
81 | // Hash
82 | const scaleHash = new Hash([0xff, 0x00, 0xab]);
83 | scaleHash.toU8a() // => [0xff, 0x00, 0xab, 0x00, ... 0x00] (32 bytes long)
84 |
85 | // Compact Int
86 | const scaleCompactInt = new CompactInt(1);
87 | scaleCompactInt.toU8a() // => [0x04]
88 |
89 | // Int
90 | const scaleInt8 = new Int8(-1);
91 | scaleInt8.toU8a() // => [0xff]
92 |
93 | const scaleInt16 = new Int16(-1);
94 | scaleInt16.toU8a() // => [0xff, 0xff]
95 |
96 | const scaleInt32 = new Int32(-1);
97 | scaleInt32.toU8a() // => [0xff, 0xff, 0xff, 0xff]
98 |
99 | const scaleInt64 = new Int64(-1);
100 | scaleInt64.toU8a() // => [0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]
101 |
102 | // UInt
103 | const scaleUInt8 = new UInt8(1);
104 | scaleUInt8.toU8a() // => [0x01]
105 |
106 | const scaleUInt16 = new UInt16(1);
107 | scaleUInt16.toU8a() // => [0x01, 0x00]
108 |
109 | const scaleUInt32 = new UInt32(1);
110 | scaleUInt32.toU8a() // => [0x01, 0x00, 0x00, 0x00]
111 |
112 | const scaleUInt64 = new UInt64(1);
113 | scaleUInt64.toU8a() // => [0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
114 |
115 | const scaleUInt128 = new UInt128(u128.fromU64(18446744073709551615));
116 | scaleUInt128.toU8a() // => [0x13, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]
117 | ```
118 |
119 | ### Decoding
120 |
121 | Every type has a **static** function **fromU8a**. It decodes an array of bytes to the desired type
122 |
123 | ```jsx
124 | import { ScaleMap, Bool, Byte, ScaleString, Hash, CompactInt } from "as-scale-codec"
125 | import { Int8, Int16, Int32, Int64 } from "as-scale-codec"
126 | import { UInt8, UInt16, UInt32, UInt64, UInt128 } from "as-scale-codec"
127 |
128 | // Bool
129 | Bool.fromU8a([0x01]); // => new Bool(true)
130 |
131 | // Byte
132 | Byte.fromU8a([0x01]); // => new Byte(0x01)
133 |
134 | // String
135 | Byte.fromU8a([0x04, 0x61]); // => new ScaleString('a')
136 |
137 | // Hash
138 | Hash.fromU8a([0xff, 0x00, 0xab]);
139 | // => [0xff, 0x00, 0xab, 0x00, ... 0x00] (32 bytes long)
140 |
141 | ScaleMap.fromU8a([4, 1, 0, 0, 0, 0]);
142 | // => const scaleMap = new ScaleMap()
143 | // => scaleMap.set(new Int32(1), new Bool(false))
144 |
145 | // Compact Int
146 | CompactInt.fromU8a([0x04]); // => new CompactInt(1)
147 |
148 | // Int
149 | Int8.fromU8a([0xff]); // => new Int8(-1)
150 | Int16.fromU8a([0xff, 0xff]); // => new Int16(-1)
151 | Int32.fromU8a([0xff, 0xff, 0xff, 0xff]); // => new Int32(-1)
152 | Int64.fromU8a([0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]); // => new Int64(-1)
153 |
154 | // UInt
155 | UInt8.fromU8a([0x01]); // => new UInt8(1)
156 | UInt16.fromU8a([0x01, 0x00]); // => new UInt16(1)
157 | UInt32.fromU8a([0x01, 0x00, 0x00, 0x00]); // => new UInt32(1)
158 | UInt64.fromU8a([0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]); // => new UInt64(1)
159 | UInt128.fromU8a([0x33, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]);
160 | // => 340282366920938463444927863358058659840
161 | ```
162 |
163 | ## Arrays
164 |
165 | ### Encoding
166 |
167 | Every array has **toU8a** function. It encodes the array values into an array of SCALE encoded bytes.
168 |
169 | ```jsx
170 | import { BoolArray, ByteArray, IntArray, StringArray } from "as-scale-codec"
171 |
172 | // Bool Array
173 | const boolArray = new BoolArray([true, false, true]);
174 | boolArray.toU8a(); // => [0x0c, 0x01, 0x00, 0x01]
175 |
176 | // Byte Array
177 | const byteArray = new ByteArray([0x01, 0x01, 0x01]);
178 | byteArray.toU8a(); // => [0x0c, 0x01, 0x01, 0x01]
179 |
180 | // Int Array
181 | const intArray = new IntArray([16384, 2, 3, 4]);
182 | intArray.toU8a() // => [0x10, 0x02, 0x00, 0x01, 0x00, 0x08, 0x0c, 0x10]
183 |
184 | // String Array
185 | const stringArray = new StringArray(["hello", "world"]);
186 | stringArray.toU8a() // => [0x08, 0x14, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x14, 0x77, 0x6f, 0x72, 0x6c, 0x64]
187 | ```
188 |
189 | ### Decoding
190 |
191 | Every array has a **static** function **fromU8a**. It decodes an array of SCALE encoded bytes and creates an array instance of the desired type.
192 |
193 | ```jsx
194 | import { BoolArray, ByteArray, IntArray, StringArray } from "as-scale-codec"
195 |
196 | // Bool Array
197 | BoolArray.fromU8a([0x0c, 0x01, 0x00, 0x01]);
198 | // => new BoolArray([true, false, true])
199 |
200 | // Byte Array
201 | ByteArray.fromU8a([0x0c, 0x01, 0x01, 0x01])
202 | // => new ByteArray([0x01, 0x01, 0x01])
203 |
204 | // Int Array
205 | IntArray.fromU8a([0x10, 0x02, 0x00, 0x01, 0x00, 0x08, 0x0c, 0x10])
206 | // => new IntArray([16384, 2, 3, 4])
207 |
208 | // String Array
209 | StringArray.fromU8a([0x08, 0x14, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x14, 0x77, 0x6f, 0x72, 0x6c, 0x64])
210 | // => new StringArray(["hello", "world"])
211 | ```
212 |
213 | # BytesReader
214 |
215 | If you have an array of arbitrary SCALE encoded bytes that you need to decode, `BytesReader` class is a preferred way to do it:
216 |
217 | ```jsx
218 | import { BytesReader } from 'as-scale-codec';
219 | // Arbitrary SCALE encoded bytes
220 | const bytes: u8[] = [
221 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
222 | 69, 0, 0, 0,
223 | 110, 125, 239, 2,
224 | 56, 97, 115, 45, 115, 99, 97, 108, 101, 45, 99, 111, 100, 101, 99,
225 | 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,
226 | 0
227 | ];
228 | // Instantiate BytesReader instance with SCALE encoded bytes
229 | const bytesReader = new BytesReader(bytes);
230 |
231 | // Read Int64
232 | bytesReader.readInto();
233 | // => new Int(-1)
234 |
235 | // Read UInt32
236 | bytesReader.readInto();
237 | // => new UInt32(69)
238 |
239 | // Read CompactInt
240 | bytesReader.readInto();
241 | // => new CompactInt(12312411)
242 |
243 | // Read ScaleString
244 | bytesReader.readInto();
245 | // => new ScaleString("as-scale-codec")
246 |
247 | // Read Hash
248 | bytesReader.readInto();
249 | // => 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])
250 |
251 | // Read Bool
252 | bytesReader.readInto();
253 | // => new Bool(false)
254 |
255 | // If you have single SCALE encoded type, you can use static decodeInto() function of BytesReader
256 |
257 | const uInt64Bytes: u8[] = [1, 0, 0, 0, 0, 0, 0];
258 | // Read UInt64
259 | BytesReader.decodeInto(uInt64Bytes);
260 | // => new UInt64(1)
261 |
262 | const hashBytes: u8[] = [0xff, 0x00, 0xab];
263 | // Read Hash
264 | BytesReader.decodeInto(hashBytes);
265 | // new Hash([0xff, 0x00, 0xab])
266 |
267 | const mapBytes: u8[] = [2, 1, 0, 1, 0, 0, 0, 3, 0, 3, 0, 0, 0];
268 | // Read ScaleMap
269 | BytesReader.decodeInto>(mapBytes);
270 | // => const scaleMap = new ScaleMap();
271 | // => scaleMap.set(new UInt16(1), new UInt32(1))
272 | // => scaleMap.set(new UInt16(3), new UInt32(3))
273 |
274 | const cmpBytes: u8[] = [169, 2];
275 | // Read CompactInt
276 | BytesReader.decodeInto(cmpBytes);
277 | // new CompactInt(170)
278 |
279 | const int8Bytes: u8[] = [0xff];
280 | // Read Int8
281 | BytesReader.decodeInto(int8Bytes);
282 | // new Int8(-1)
283 |
284 | ```
285 |
286 | # Miscellaneous
287 |
288 | ### Convert bytes to hash
289 |
290 | ```jsx
291 | Hash.bytesToHash([0xff, 0x00, 0xab]);
292 | // => [0x00, ... , 0x00, 0xff, 0x00, 0xab] (32 bytes long)
293 |
294 | Hash.bytesToHash([0xff, 0x00, ..., 0x00]); // (32 bytes long)
295 | // => [0xff, ... , 0x00] (32 bytes long)
296 | ```
297 |
298 | # **Tests**
299 |
300 | In order to run the unit tests, one must perform:
301 |
302 | ```bash
303 | npm run test
304 | ```
305 |
306 | # **License**
307 | This repository is licensed under [Apache 2.0 license](https://github.com/LimeChain/as-scale-codec/blob/master/LICENSE)
308 |
--------------------------------------------------------------------------------
/as-pect.config.js:
--------------------------------------------------------------------------------
1 | module.exports = {
2 | /**
3 | * A set of globs passed to the glob package that qualify typescript files for testing.
4 | */
5 | include: ["assembly/__tests__/**/*.spec.ts"],
6 | /**
7 | * A set of globs passed to the glob package that quality files to be added to each test.
8 | */
9 | add: ["assembly/__tests__/**/*.include.ts"],
10 | /**
11 | * All the compiler flags needed for this test suite. Make sure that a binary file is output.
12 | */
13 | flags: {
14 | /** To output a wat file, uncomment the following line. */
15 | // "--textFile": ["output.wat"],
16 | /** A runtime must be provided here. */
17 | "--runtime": ["stub"], // Acceptable values are: full, half, stub (arena), and none
18 | },
19 | /**
20 | * A set of regexp that will disclude source files from testing.
21 | */
22 | disclude: [/node_modules/],
23 | /**
24 | * Add your required AssemblyScript imports here.
25 | */
26 | imports (memory, createImports, instantiateSync, binary) {
27 | let instance; // Imports can reference this
28 | const myImports = {
29 | // put your web assembly imports here, and return the module
30 | };
31 | instance = instantiateSync(binary, createImports(myImports));
32 | return instance;
33 | },
34 | /**
35 | * Add a custom reporter here if you want one. The following example is in typescript.
36 | *
37 | * @example
38 | * import { TestReporter, TestGroup, TestResult, TestContext } from "as-pect";
39 | *
40 | * export class CustomReporter extends TestReporter {
41 | * // implement each abstract method here
42 | * public abstract onStart(suite: TestContext): void;
43 | * public abstract onGroupStart(group: TestGroup): void;
44 | * public abstract onGroupFinish(group: TestGroup): void;
45 | * public abstract onTestStart(group: TestGroup, result: TestResult): void;
46 | * public abstract onTestFinish(group: TestGroup, result: TestResult): void;
47 | * public abstract onFinish(suite: TestContext): void;
48 | * }
49 | */
50 | // reporter: new CustomReporter(),
51 | /**
52 | * Specify if the binary wasm file should be written to the file system.
53 | */
54 | outputBinary: false,
55 | };
56 |
--------------------------------------------------------------------------------
/assembly/AbstractInt.ts:
--------------------------------------------------------------------------------
1 | // Copyright 2020 LimeChain Ltd.
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | import { UnwrappableCodec } from "./interfaces/UnwrappableCodec";
16 | import { Bytes } from "./utils/Bytes";
17 |
18 | /** Representation for a Int value in the system. */
19 | export abstract class AbstractInt implements UnwrappableCodec {
20 |
21 | protected bitLength: i32;
22 | private _value: T;
23 |
24 | constructor (value: T, bitLength: i32) {
25 | this._value = value;
26 | this.bitLength = bitLength;
27 | }
28 |
29 | /**
30 | * @description Returns the inner native value
31 | */
32 | public unwrap(): T{
33 | return this._value;
34 | }
35 |
36 | /** Encodes the value as u8[] as per the SCALE codec specification */
37 | public toU8a (): u8[] {
38 | let bytesEncoded = new Array(this.bitLength);
39 | Bytes.putUint(bytesEncoded, this.unwrap(), this.bitLength);
40 | return bytesEncoded;
41 | }
42 |
43 | public eq(other: AbstractInt): bool{
44 | return this.unwrap() == other.unwrap();
45 | }
46 | public notEq(other: AbstractInt): bool{
47 | return this.unwrap() != other.unwrap();
48 | }
49 |
50 | /**
51 | * @description Non-static constructor method used to populate defined properties of the model
52 | * @param bytes SCALE encoded bytes
53 | * @param index index to start decoding the bytes from
54 | */
55 | public populateFromBytes(bytes: u8[], index: i32 = 0): void {
56 | assert(bytes.length - index > 0, "AbstractInt: Invalid bytes provided");
57 | this._value = Bytes.toUint(bytes, this.bitLength, index);
58 | }
59 |
60 | /**
61 | * @description Returns the string representation of the value
62 | */
63 | toString (): string {
64 | return this.unwrap().toString();
65 | }
66 | /**
67 | * @description The length of Uint8Array when the value is encoded
68 | */
69 | public encodedLength (): i32 {
70 | return this.bitLength;
71 | }
72 | }
73 |
--------------------------------------------------------------------------------
/assembly/Arrays/AbstractArray.ts:
--------------------------------------------------------------------------------
1 | // Copyright 2020 LimeChain Ltd.
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | import { Codec } from "../interfaces/Codec";
16 | import { DecodedData } from "../interfaces/DecodedData";
17 | import { UnwrappableCodec } from "../interfaces/UnwrappableCodec";
18 | import { ArrayUtils } from "../utils/Arrays";
19 | import { Bytes } from "../utils/Bytes";
20 | import { BytesBuffer } from "../utils/BytesBuffer";
21 |
22 | export abstract class AbstractArray implements UnwrappableCodec>{
23 |
24 | public values: Array;
25 | constructor(input: NativeType[] = []) {
26 | this.values = new Array(input.length);
27 | Bytes.copy(input, this.values);
28 | }
29 |
30 | /**
31 | * @description Returns the inner native value
32 | */
33 | public unwrap(): Array{
34 | return this.values;
35 | }
36 |
37 | public eq(other: AbstractArray): bool{
38 | return ArrayUtils.areArraysEqual(this.values, other.values);
39 | };
40 |
41 | public notEq(other: AbstractArray): bool{
42 | return !ArrayUtils.areArraysEqual(this.values, other.values);
43 | };
44 |
45 | /**
46 | * @description Encodes values of all elements in u8[] successively as per the SCALE codec specification
47 | */
48 | public toU8a (): u8[] {
49 | const bytesBuffer = new BytesBuffer();
50 | bytesBuffer.encodeCompactInt(this.values.length);
51 |
52 | for (let i = 0; i < this.values.length; i++) {
53 | const element = instantiate(this.values[i]);
54 | bytesBuffer.write(element.toU8a());
55 | }
56 |
57 | return bytesBuffer.bytes;
58 | }
59 |
60 | /**
61 | * @description Returns encoded byte length of the type
62 | */
63 | abstract encodedLength(): i32;
64 |
65 | /**
66 | * @description Non-static constructor method used to populate defined properties of the model
67 | * @param bytes SCALE encoded bytes
68 | * @param index index to start decoding the bytes from
69 | */
70 | abstract populateFromBytes(bytes: u8[], index: i32): void;
71 |
72 | /**
73 | * @description Each child class has to provide decryption implementation for elements
74 | */
75 | public abstract decodeElement (value: u8[]): DecodedData;
76 |
77 | /**
78 | * @description Instantiates type of ScaleArray from u8[] SCALE encoded bytes (Decode)
79 | */
80 | static fromU8a (input: u8[]): TypeOfScaleArray {
81 | const data = Bytes.decodeCompactInt(input);
82 | let bytes = input.slice(data.decBytes);
83 |
84 | const scaleArray = instantiate([]);
85 |
86 | for (let i: u64 = 0; i < data.value; i++) {
87 | const element = scaleArray.decodeElement(bytes);
88 | scaleArray.values.push(element.value);
89 |
90 | bytes = bytes.slice(element.decBytes);
91 | }
92 |
93 | return scaleArray;
94 | }
95 | }
96 |
--------------------------------------------------------------------------------
/assembly/Arrays/BoolArray.ts:
--------------------------------------------------------------------------------
1 | // Copyright 2020 LimeChain Ltd.
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | import { BytesReader, CompactInt } from "..";
16 | import { DecodedData } from "../interfaces/DecodedData";
17 | import { Bool } from "./../Bool";
18 | import { AbstractArray } from "./AbstractArray";
19 |
20 |
21 | // @ts-ignore
22 | export class BoolArray extends AbstractArray {
23 |
24 | /**
25 | * @description BoolArray elements decryption implementation
26 | */
27 | public decodeElement (value: u8[]): DecodedData {
28 | const scaleBool = Bool.fromU8a([value[0]]);
29 |
30 | return new DecodedData(
31 | scaleBool.unwrap(),
32 | scaleBool.encodedLength()
33 | )
34 | }
35 |
36 | /**
37 | * @description Returns encoded byte length of the type
38 | */
39 | public encodedLength(): i32{
40 | return (new CompactInt(this.values.length).encodedLength()) + super.values.length;
41 | }
42 |
43 | /**
44 | * @description Non-static constructor method used to populate defined properties of the model
45 | * @param bytes SCALE encoded bytes
46 | * @param index index to start decoding the bytes from
47 | */
48 | populateFromBytes(bytes: u8[], index: i32 = 0): void{
49 | const bytesReader = new BytesReader(bytes.slice(index));
50 | const data = bytesReader.readInto();
51 |
52 | for(let i: i32 = 0; i < data.unwrap() - index; i++){
53 | const element = bytesReader.readInto();
54 | this.values.push(element.unwrap());
55 | }
56 | }
57 | /**
58 | * @description Instantiates ScaleBoolArray from u8[] SCALE encoded bytes (Decode)
59 | */
60 | static fromU8a (input: u8[]): BoolArray {
61 | return AbstractArray.fromU8a(input);
62 | }
63 |
64 | @inline @operator('==')
65 | static eq(a: BoolArray, b: BoolArray): bool {
66 | return a.eq(b);
67 | }
68 |
69 | @inline @operator('!=')
70 | static notEq(a: BoolArray, b: BoolArray): bool {
71 | return a.notEq(b);
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/assembly/Arrays/ByteArray.ts:
--------------------------------------------------------------------------------
1 | // Copyright 2020 LimeChain Ltd.
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | import { BytesReader } from "..";
16 | import { Byte } from "../Byte";
17 | import { CompactInt } from "../Int";
18 | import { DecodedData } from "../interfaces/DecodedData";
19 | import { AbstractArray } from "./AbstractArray";
20 |
21 | // @ts-ignore
22 | export class ByteArray extends AbstractArray {
23 |
24 | /**
25 | * @description Return hex representation of ByteArray
26 | */
27 | public toHexString (): string {
28 | let result = "0x";
29 | for (let i = 0; i < super.values.length; i++) {
30 | const str = super.values[i].toString(16);
31 | if (str.length == 1) {
32 | result += "0";
33 | }
34 |
35 | result += str;
36 | }
37 |
38 | return result;
39 | }
40 | /**
41 | * @description BoolArray elements decryption implementation
42 | */
43 | public decodeElement(value: u8[]): DecodedData {
44 | const scaleByte = Byte.fromU8a([value[0]]);
45 |
46 | return new DecodedData(
47 | scaleByte.unwrap(),
48 | scaleByte.encodedLength()
49 | )
50 | }
51 |
52 | /**
53 | * @description Returns encoded byte length of the type
54 | */
55 | public encodedLength(): i32{
56 | return (new CompactInt(this.values.length).encodedLength()) + super.values.length;
57 | }
58 |
59 | /**
60 | * @description Non-static constructor method used to populate defined properties of the model
61 | * @param bytes SCALE encoded bytes
62 | * @param index index to start decoding the bytes from
63 | */
64 | populateFromBytes(bytes: u8[], index: i32 = 0): void {
65 | const bytesReader = new BytesReader(bytes.slice(index));
66 | const data = bytesReader.readInto();
67 |
68 | for(let i: i32 = 0; i < data.unwrap(); i++){
69 | const element: Byte = bytesReader.readInto();
70 | this.values.push(element.unwrap());
71 | }
72 | }
73 | /**
74 | * @description Instantiates ScaleByteArray from u8[] SCALE encoded bytes (Decode)
75 | */
76 | static fromU8a(input: u8[], index: i32 = 0): ByteArray {
77 | return AbstractArray.fromU8a(input.slice(index));
78 | }
79 |
80 | @inline @operator('==')
81 | static eq(a: ByteArray, b: ByteArray): bool {
82 | return a.eq(b);
83 | }
84 |
85 | @inline @operator('!=')
86 | static notEq(a: ByteArray, b: ByteArray): bool {
87 | return a.notEq(b);
88 | }
89 | }
90 |
91 |
--------------------------------------------------------------------------------
/assembly/Arrays/IntArray.ts:
--------------------------------------------------------------------------------
1 | // Copyright 2020 LimeChain Ltd.
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | import { BytesReader } from "..";
16 | import { CompactInt } from "../Int/CompactInt";
17 | import { DecodedData } from "../interfaces/DecodedData";
18 | import { AbstractArray } from "./AbstractArray";
19 |
20 |
21 | // @ts-ignore
22 | export class IntArray extends AbstractArray {
23 |
24 | /**
25 | * @description BoolArray elements decryption implementation
26 | */
27 | public decodeElement (value: u8[]): DecodedData {
28 | const compactInt = CompactInt.fromU8a(value);
29 |
30 | return new DecodedData(
31 | compactInt.unwrap(),
32 | compactInt.encodedLength()
33 | )
34 | }
35 |
36 | /**
37 | * @description Returns encoded byte length of the type
38 | */
39 | public encodedLength(): i32{
40 | let len: i32 = new CompactInt(this.values.length).encodedLength();
41 | for (let i: i32 = 0; i < this.values.length; i++){
42 | const value = new CompactInt(this.values[i]);
43 | len += value.encodedLength();
44 | }
45 | return len;
46 | }
47 | /**
48 | * @description Non-static constructor method used to populate defined properties of the model
49 | * @param bytes SCALE encoded bytes
50 | * @param index index to start decoding the bytes from
51 | */
52 | populateFromBytes(bytes: u8[], index: i32 = 0): void {
53 | const bytesReader = new BytesReader(bytes.slice(index));
54 | const data = bytesReader.readInto();
55 |
56 | for(let i: i32 = 0; i < data.unwrap(); i++){
57 | const element: CompactInt = bytesReader.readInto();
58 | this.values.push(element.unwrap());
59 | }
60 | }
61 | /**
62 | * @description Instantiates ScaleIntArray from u8[] SCALE encoded bytes (Decode)
63 | */
64 | static fromU8a (input: u8[]): IntArray {
65 | return AbstractArray.fromU8a(input);
66 | }
67 |
68 | @inline @operator('==')
69 | static eq(a: IntArray, b: IntArray): bool {
70 | return a.eq(b);
71 | }
72 |
73 | @inline @operator('!=')
74 | static notEq(a: IntArray, b: IntArray): bool {
75 | return a.notEq(b);
76 | }
77 | }
78 |
79 |
--------------------------------------------------------------------------------
/assembly/Arrays/StringArray.ts:
--------------------------------------------------------------------------------
1 | // Copyright 2020 LimeChain Ltd.
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | import { BytesReader, CompactInt } from "..";
16 | import { DecodedData } from "../interfaces/DecodedData";
17 | import { ScaleString } from "../ScaleString";
18 | import { Bytes } from "../utils/Bytes";
19 | import { AbstractArray } from "./AbstractArray";
20 |
21 |
22 | // @ts-ignore
23 | export class StringArray extends AbstractArray{
24 |
25 | /**
26 | * @description BoolArray elements decryption implementation
27 | */
28 | public decodeElement (value: u8[]): DecodedData {
29 | const stringLength = Bytes.decodeCompactInt(value);
30 | const encodedStringLength = i32(stringLength.decBytes + stringLength.value);
31 |
32 | return new DecodedData(
33 | ScaleString.fromU8a(value.slice(0, encodedStringLength)).toString(),
34 | encodedStringLength
35 | )
36 | }
37 |
38 | /**
39 | * @description Returns encoded byte length of the type
40 | */
41 | public encodedLength(): i32{
42 | let len: i32 = new CompactInt(this.values.length).encodedLength();
43 | for (let i: i32 = 0; i < this.values.length; i++){
44 | const value = new ScaleString(this.values[i]);
45 | len += value.encodedLength();
46 | }
47 | return len;
48 | }
49 |
50 | /**
51 | * @description Non-static constructor method used to populate defined properties of the model
52 | * @param bytes SCALE encoded bytes
53 | * @param index index to start decoding the bytes from
54 | */
55 | populateFromBytes(bytes: u8[], index: i32 = 0): void {
56 | const bytesReader = new BytesReader(bytes.slice(index));
57 | const data = bytesReader.readInto();
58 |
59 | for(let i: i32 = 0; i < data.unwrap(); i++){
60 | const element: ScaleString = bytesReader.readInto();
61 | this.values.push(element.toString());
62 | }
63 | }
64 |
65 | /**
66 | * @description Instantiates ScaleStringArray from u8[] SCALE encoded bytes (Decode)
67 | */
68 | static fromU8a (input: u8[]): StringArray {
69 | return AbstractArray.fromU8a(input);
70 | }
71 |
72 | @inline @operator('==')
73 | static eq(a: StringArray, b: StringArray): bool {
74 | return a.eq(b);
75 | }
76 |
77 | @inline @operator('!=')
78 | static notEq(a: StringArray, b: StringArray): bool {
79 | return a.notEq(b);
80 | }
81 | }
--------------------------------------------------------------------------------
/assembly/Arrays/UInt128Array.ts:
--------------------------------------------------------------------------------
1 | // Copyright 2020 LimeChain Ltd.
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | import { u128 } from "as-bignum";
16 | import { BytesReader, CompactInt } from "..";
17 | import { DecodedData } from "../interfaces/DecodedData";
18 | import { UInt128 } from "../UInt/UInt128";
19 | import { BIT_LENGTH } from "../utils/Bytes";
20 | import { AbstractArray } from "./AbstractArray";
21 |
22 | // @ts-ignore
23 | export class UInt128Array extends AbstractArray {
24 |
25 | /**
26 | * @description BoolArray elements decryption implementation
27 | */
28 | public decodeElement(value: u8[]): DecodedData {
29 | const u128Instance = UInt128.fromU8a(value.slice(0, BIT_LENGTH.INT_128));
30 |
31 | return new DecodedData(
32 | u128Instance.unwrap(),
33 | u128Instance.encodedLength()
34 | )
35 | }
36 |
37 | /**
38 | * @description Non-static constructor method used to populate defined properties of the model
39 | * @param bytes SCALE encoded bytes
40 | * @param index index to start decoding the bytes from
41 | */
42 | populateFromBytes(bytes: u8[], index: i32 = 0): void {
43 | const bytesReader = new BytesReader(bytes.slice(index));
44 | const data = bytesReader.readInto();
45 | for(let i: i32 = 0; i < data.unwrap(); i++){
46 | const element: UInt128 = BytesReader.decodeInto(bytesReader.readBytes(BIT_LENGTH.INT_128));
47 | this.values.push(element.unwrap());
48 | }
49 | }
50 |
51 | /**
52 | * @description Instantiates ScaleIntArray from u8[] SCALE encoded bytes (Decode)
53 | */
54 | static fromU8a (input: u8[]): UInt128Array {
55 | return AbstractArray.fromU8a(input);
56 | }
57 |
58 | /**
59 | * @description Returns encoded byte length of the type
60 | */
61 | public encodedLength(): i32{
62 | return (new CompactInt(this.values.length).encodedLength()) + super.values.length * BIT_LENGTH.INT_128;
63 | }
64 |
65 | @inline @operator('==')
66 | static eq(a: UInt128Array, b: UInt128Array): bool {
67 | return a.eq(b);
68 | }
69 |
70 | @inline @operator('!=')
71 | static notEq(a: UInt128Array, b: UInt128Array): bool {
72 | return a.notEq(b);
73 | }
74 | }
75 |
76 |
--------------------------------------------------------------------------------
/assembly/Arrays/index.ts:
--------------------------------------------------------------------------------
1 | export * from "./BoolArray";
2 | export * from "./ByteArray";
3 | export * from "./IntArray";
4 | export * from "./StringArray";
5 | export * from "./UInt128Array";
6 |
--------------------------------------------------------------------------------
/assembly/Bool.ts:
--------------------------------------------------------------------------------
1 | // Copyright 2020 LimeChain Ltd.
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | import { UnwrappableCodec } from "./interfaces/UnwrappableCodec";
16 |
17 | /** Representation for a boolean value in the system. */
18 | export class Bool implements UnwrappableCodec {
19 |
20 | private _value: bool;
21 |
22 | constructor (value: bool = false) {
23 | this._value = value;
24 | }
25 |
26 | /**
27 | * @description Returns the inner native value
28 | */
29 | public unwrap(): bool{
30 | return this._value;
31 | }
32 |
33 | /** Encodes the value as u8[] as per the SCALE codec specification
34 | * true -> [1]
35 | * false -> [0]
36 | */
37 | toU8a (): u8[] {
38 | let bytesEncoded = new Array(1);
39 | bytesEncoded[0] = this._value ? 0x01 : 0x00;
40 | return bytesEncoded;
41 | }
42 |
43 | /**
44 | * @description Non-static constructor method used to populate defined properties of the model.
45 | * @param bytes SCALE encoded bytes
46 | * @param index index to start decoding the bytes from
47 | */
48 | public populateFromBytes(bytes: u8[], index: i32 = 0): void{
49 | assert(bytes.length > 0 && (bytes[index] == 1 || bytes[index] == 0), 'Bool: Cannot decode invalid input');
50 | this._value = bytes[index] == 1;
51 | }
52 |
53 | /**
54 | * @description Returns the string representation of the value
55 | */
56 | toString (): string {
57 | return this._value.toString();
58 | }
59 |
60 | eq(other: Bool): bool {
61 | return this._value == other.unwrap();
62 | }
63 |
64 | notEq(other: Bool): bool {
65 | return this._value != other.unwrap();
66 | }
67 | /**
68 | * @description The length of Uint8Array when the value is encoded
69 | */
70 | public encodedLength (): i32 {
71 | return 1;
72 | }
73 |
74 | /** Instantiates new Bool from u8[] SCALE encoded bytes */
75 | static fromU8a (value: u8[], index: i32 = 0): Bool {
76 | assert(value.length - index > 0 && (value[index] == 1 || value[index] == 0), 'Bool: Cannot decode invalid input');
77 |
78 | return new Bool(value[index] == 1);
79 | }
80 |
81 | @inline @operator('==')
82 | static eq(a: Bool, b: Bool): bool {
83 | return a.eq(b);
84 | }
85 |
86 | @inline @operator('!=')
87 | static notEq(a: Bool, b: Bool): bool {
88 | return a.notEq(b);
89 | }
90 | }
91 |
--------------------------------------------------------------------------------
/assembly/Byte.ts:
--------------------------------------------------------------------------------
1 | // Copyright 2020 LimeChain Ltd.
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | import { UnwrappableCodec } from "./interfaces/UnwrappableCodec";
16 | import { BIT_LENGTH } from "./utils/Bytes";
17 |
18 | export class Byte implements UnwrappableCodec{
19 |
20 | private _value: u8;
21 | protected bitLength: i32;
22 |
23 | constructor (value: u8 = 0) {
24 | this._value = value;
25 | this.bitLength = BIT_LENGTH.INT_8;
26 | }
27 |
28 | /**
29 | * @description Returns the inner native value
30 | */
31 | public unwrap(): u8{
32 | return this._value;
33 | }
34 |
35 | /**
36 | * @description Encodes Byte as u8[] as per the SCALE codec specification
37 | */
38 | public toU8a (): u8[] {
39 | return [this._value];
40 | }
41 | /**
42 | * @description Non-static constructor method used to populate defined properties of the model
43 | * @param bytes SCALE encoded bytes
44 | * @param index index to start decoding the bytes from
45 | */
46 | public populateFromBytes(bytes: u8[], index: i32 = 0): void{
47 | assert(bytes.length - index > 0, 'Bool: Cannot decode invalid input');
48 | this._value = bytes[index];
49 | }
50 |
51 | eq(other: Byte): bool {
52 | return this._value == other.unwrap();
53 | }
54 |
55 | notEq(other: Byte): bool {
56 | return this._value != other.unwrap();
57 | }
58 |
59 | /**
60 | * @description The length of Byte when the value is encoded
61 | */
62 | public encodedLength (): i32 {
63 | return this.bitLength;
64 | }
65 |
66 | /** Instantiates new Byte from u8[] SCALE encoded bytes */
67 | static fromU8a (value: u8[], index: i32 = 0): Byte {
68 | assert(value.length - index > 0, 'Byte: cannot decode invalid type');
69 | return new Byte(value[index]);
70 | }
71 |
72 | @inline @operator('==')
73 | static eq(a: Byte, b: Byte): bool {
74 | return a.eq(b);
75 | }
76 |
77 | @inline @operator('!=')
78 | static notEq(a: Byte, b: Byte): bool {
79 | return a.notEq(b);
80 | }
81 | }
--------------------------------------------------------------------------------
/assembly/BytesReader.ts:
--------------------------------------------------------------------------------
1 | // Copyright 2020 LimeChain Ltd.
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | import { Codec } from '.';
16 |
17 | /**
18 | * @description BytesReader class that helps reading bytes into SCALE Codec types
19 | */
20 | export class BytesReader{
21 | /**
22 | * u8 bytes
23 | */
24 | public readonly bytes: u8[];
25 | /**
26 | * Current index to start decoding from
27 | */
28 | private index: i32 = 0;
29 |
30 | constructor(bytes: u8[]){
31 | this.bytes = bytes;
32 | }
33 |
34 | /**
35 | * @description Reads bytes into a given Type
36 | */
37 | readInto(): T{
38 | const instance: T = BytesReader.decodeInto(this.bytes, this.index);
39 | this.index += instance.encodedLength();
40 | return instance;
41 | }
42 | /**
43 | * Returns the unread bytes from the reader
44 | */
45 | getLeftoverBytes(): u8[]{
46 | return this.bytes.slice(this.index);
47 | }
48 | /**
49 | * Read custom sized array of raw bytes
50 | * @param size byte array size
51 | */
52 | readBytes(size: i32): u8[]{
53 | const bytes: u8[] = this.bytes.slice(this.index, this.index + size);
54 | this.index += size;
55 | return bytes;
56 | }
57 | /**
58 | * @description Static variant of readInto() method
59 | * @param bytes
60 | * @param index
61 | */
62 | static decodeInto(bytes: u8[], index: i32 = 0): T{
63 | const instance: T = instantiate();
64 | instance.populateFromBytes(bytes, index);
65 | return instance;
66 | }
67 | }
--------------------------------------------------------------------------------
/assembly/Hash.ts:
--------------------------------------------------------------------------------
1 | // Copyright 2020 LimeChain Ltd.
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | import { UnwrappableCodec } from './interfaces/UnwrappableCodec';
16 | import { Bytes } from './utils/Bytes';
17 |
18 | export class Hash implements UnwrappableCodec> {
19 |
20 | private _values: Array;
21 |
22 | constructor(value: u8[] = []) {
23 | this._values = new Array(32);
24 | Bytes.copy(value, this._values);
25 | }
26 |
27 | /**
28 | * @description Returns the inner native value
29 | */
30 | public unwrap(): Array{
31 | return this._values;
32 | }
33 |
34 | /**
35 | * @description Encodes Hash as u8[] as per the SCALE codec specification
36 | */
37 | public toU8a (): u8[] {
38 | const result: u8[] = new Array(this.encodedLength());
39 | Bytes.copy(this._values, result);
40 |
41 | return result;
42 | }
43 |
44 | /**
45 | * @description Non-static constructor method used to populate defined properties of the model.
46 | * @param bytes SCALE encoded bytes
47 | * @param index index to start decoding the bytes from
48 | */
49 | public populateFromBytes(bytes: u8[], index: i32 = 0): void{
50 | assert(bytes.length - index >= 0, "Hash: Empty bytes array provided");
51 | this._values = new Array(32);
52 | Bytes.copy(bytes, this._values, 0, index);
53 | }
54 |
55 | /**
56 | * @description Return string representation of Hash
57 | */
58 | public toString (): string {
59 | return "0x" + this._values.join('');
60 | }
61 |
62 | /**
63 | * @description Instantiate Hash from bytes cropped from the left.
64 | */
65 | static bytesToHash (bytes: u8[]): Hash {
66 | let hash = new Hash([]);
67 | if (bytes.length > 32) {
68 | bytes = bytes.slice(bytes.length - 32);
69 | }
70 |
71 | const position: i32 = 32 - bytes.length;
72 | Bytes.copy(bytes, hash.unwrap(), position);
73 | return hash;
74 | }
75 |
76 | /**
77 | * @description The length of encoded Hash
78 | */
79 | public encodedLength (): i32 {
80 | return 32;
81 | }
82 |
83 | /**
84 | * @description Instantiates Hash from u8[] SCALE encoded bytes
85 | * @param input SCALE encoded bytes
86 | * @param index an index of input to start decoding from
87 | */
88 | static fromU8a (input: u8[], index: i32 = 0): Hash {
89 | assert(input.length - index >= 0, "Hash: Empty bytes array provided");
90 | return new Hash(input.slice(index));
91 | }
92 |
93 | eq(other: Hash): bool {
94 | let areEqual = true;
95 | for (let i = 0; i < this.unwrap().length; i++) {
96 | if (this.unwrap()[i] != other.unwrap()[i]) {
97 | areEqual = false;
98 | break;
99 | }
100 | }
101 | return areEqual;
102 | }
103 |
104 | notEq(other: Hash): bool {
105 | return !this.eq(other);
106 | }
107 |
108 | @inline @operator('==')
109 | static eq(a: Hash, b: Hash): bool {
110 | return a.eq(b);
111 | }
112 |
113 | @inline @operator('!=')
114 | static notEq(a: Hash, b: Hash): bool {
115 | return a.notEq(b);
116 | }
117 | }
118 |
--------------------------------------------------------------------------------
/assembly/Int/CompactInt.ts:
--------------------------------------------------------------------------------
1 | // Copyright 2020 LimeChain Ltd.
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | import { UnwrappableCodec } from "../interfaces/UnwrappableCodec";
16 | import { BIT_LENGTH, Bytes } from "../utils/Bytes";
17 | import { BytesBuffer } from "../utils/BytesBuffer";
18 |
19 | /**
20 | * @description Representation for a CompactInt value in the system.
21 | */
22 | export class CompactInt implements UnwrappableCodec {
23 |
24 | private _value: i64;
25 | protected bitLength: i32;
26 |
27 | constructor (value: i64 = 0) {
28 | this._value = value;
29 | this.bitLength = CompactInt._computeBitLength(value);
30 | }
31 |
32 | /**
33 | * @description Return inner native value
34 | */
35 | unwrap(): i64{
36 | return this._value;
37 | }
38 |
39 | /**
40 | * @description Encodes the value as u8[] as per the SCALE codec specification
41 | */
42 | public toU8a (): u8[] {
43 | const bytesBuffer = new BytesBuffer();
44 | bytesBuffer.encodeCompactInt(this._value);
45 |
46 | return bytesBuffer.bytes;
47 | }
48 | /**
49 | * @description Non-static constructor method used to populate defined properties of the model
50 | * @param bytes SCALE encoded bytes
51 | * @param index index to start decoding the bytes from
52 | */
53 | public populateFromBytes(bytes: u8[], index: i32 = 0): void{
54 | assert(bytes.length - index > 0, "CompactInt: Empty bytes array provided");
55 | const decodedData = Bytes.decodeCompactInt(bytes, index);
56 | this._value = decodedData.value;
57 | this.bitLength = CompactInt._computeBitLength(decodedData.value);
58 | }
59 | /**
60 | * @description Returns the string representation of the value
61 | */
62 | toString (): string {
63 | return this._value.toString();
64 | }
65 |
66 | /**
67 | * Internal private function to compute bit length of the value
68 | * @param value
69 | */
70 | static _computeBitLength(value: u64): i32 {
71 | if (value < 1 << 6) return BIT_LENGTH.INT_8;
72 | else if (value < 1 << 14) return BIT_LENGTH.INT_16;
73 | else if (value < 1 << 30) return BIT_LENGTH.INT_32;
74 | else {
75 | return BIT_LENGTH.INT_64;
76 | }
77 | }
78 | /**
79 | * @description The length of Int when the value is encoded
80 | */
81 | public encodedLength (): i32 {
82 | return this.bitLength;
83 | }
84 |
85 |
86 | eq(other: CompactInt): bool {
87 | return this._value == other.unwrap();
88 | }
89 |
90 | notEq(other: CompactInt): bool {
91 | return this._value != other.unwrap();
92 | }
93 |
94 | /**
95 | * @description Instantiates Compact Int from u8[] SCALE encoded bytes
96 | * Compact Int decodes int8, int16, int32, int64 size correctly
97 | * @param input SCALE encoded bytes
98 | * @param index an index of input to start decoding from
99 | */
100 | static fromU8a (value: u8[], index: i32 = 0): CompactInt {
101 | assert(value.length - index > 0, "CompactInt: Empty bytes array provided");
102 | const decodedData = Bytes.decodeCompactInt(value, index);
103 | return new CompactInt(decodedData.value);
104 | }
105 |
106 | @inline @operator('==')
107 | static eq(a: CompactInt, b: CompactInt): bool {
108 | return a.eq(b);
109 | }
110 |
111 | @inline @operator('!=')
112 | static notEq(a: CompactInt, b: CompactInt): bool {
113 | return a.notEq(b);
114 | }
115 | }
116 |
--------------------------------------------------------------------------------
/assembly/Int/Int16.ts:
--------------------------------------------------------------------------------
1 | // Copyright 2020 LimeChain Ltd.
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | import { AbstractInt } from "../AbstractInt";
16 | import { BIT_LENGTH, Bytes } from "../utils/Bytes";
17 |
18 | /** Representation for a Int16 value in the system. */
19 | export class Int16 extends AbstractInt {
20 |
21 | constructor (value: i16 = 0) {
22 | super(value, BIT_LENGTH.INT_16)
23 | }
24 |
25 | /**
26 | * @description Instantiates new Int16 from u8[] SCALE encoded bytes
27 | * NOTE: if the length of the provided value is less than the byte length of the Int16,
28 | * it is filled with 0 bytes
29 | * */
30 | static fromU8a (value: u8[], index:i32 = 0): Int16 {
31 | assert(value.length - index > 0, 'Int16: Empty bytes array provided');
32 | var res = Bytes.toUint(value, BIT_LENGTH.INT_16, index);
33 | return new Int16(res);
34 | }
35 |
36 | @inline @operator('==')
37 | static eq(a: Int16, b: Int16): bool {
38 | return a.eq(b);
39 | }
40 |
41 | @inline @operator('!=')
42 | static notEq(a: Int16, b: Int16): bool {
43 | return a.notEq(b);
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/assembly/Int/Int32.ts:
--------------------------------------------------------------------------------
1 | // Copyright 2020 LimeChain Ltd.
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | import { AbstractInt } from "../AbstractInt";
16 | import { BIT_LENGTH, Bytes } from "../utils/Bytes";
17 |
18 | /** Representation for a Int32 value in the system. */
19 | export class Int32 extends AbstractInt {
20 |
21 | constructor (value: i32 = 0) {
22 | super(value, BIT_LENGTH.INT_32)
23 | }
24 |
25 | /**
26 | * @description Instantiates new Int32 from u8[] SCALE encoded bytes
27 | * NOTE: if the length of the provided value is less than the byte length of the Int32,
28 | * it is filled with 0 bytes
29 | */
30 | static fromU8a (value: u8[], index: i32 = 0): Int32 {
31 | assert(value.length - index > 0, 'Int32: Empty bytes array provided');
32 | var res = Bytes.toUint(value, BIT_LENGTH.INT_32, index);
33 | return new Int32(res);
34 | }
35 |
36 | @inline @operator('==')
37 | static eq(a: Int32, b: Int32): bool {
38 | return a.eq(b);
39 | }
40 |
41 | @inline @operator('!=')
42 | static notEq(a: Int32, b: Int32): bool {
43 | return a.notEq(b);
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/assembly/Int/Int64.ts:
--------------------------------------------------------------------------------
1 | // Copyright 2020 LimeChain Ltd.
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | import { AbstractInt } from "../AbstractInt";
16 | import { BIT_LENGTH, Bytes } from "../utils/Bytes";
17 |
18 | /** Representation for a Int64 value in the system. */
19 | export class Int64 extends AbstractInt {
20 |
21 | constructor (value: i64 = 0) {
22 | super(value, BIT_LENGTH.INT_64)
23 | }
24 |
25 | /**
26 | * @description Instantiates new Int64 from u8[] SCALE encoded bytes
27 | * NOTE: if the length of the provided value is less than the byte length of the Int64,
28 | * it is filled with 0 bytes
29 | */
30 | static fromU8a (value: u8[], index: i32 = 0): Int64 {
31 | assert(value.length - index > 0, "Int64: Empty bytes array provided");
32 | var res = Bytes.toUint(value, BIT_LENGTH.INT_64, index);
33 | return new Int64(res);
34 | }
35 |
36 | @inline @operator('==')
37 | static eq(a: Int64, b: Int64): bool {
38 | return a.eq(b);
39 | }
40 |
41 | @inline @operator('!=')
42 | static notEq(a: Int64, b: Int64): bool {
43 | return a.notEq(b);
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/assembly/Int/Int8.ts:
--------------------------------------------------------------------------------
1 | // Copyright 2020 LimeChain Ltd.
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | import { AbstractInt } from "../AbstractInt";
16 | import { BIT_LENGTH } from "../utils/Bytes";
17 |
18 | /** Representation for a Int8 value in the system. */
19 | export class Int8 extends AbstractInt {
20 |
21 | constructor (value: i8 = 0) {
22 | super(value, BIT_LENGTH.INT_8)
23 | }
24 |
25 | /**
26 | * @description Instantiates new Int8 from u8[] SCALE encoded bytes
27 | * NOTE: if the length of the provided value is less than the byte length of the Int8,
28 | * it is filled with 0 bytes
29 | * */
30 | static fromU8a (value: u8[], index: i32 = 0): Int8 {
31 | assert(value.length - index > 0, 'Int8: empty bytes array provided');
32 | return new Int8(value[index]);
33 | }
34 |
35 | @inline @operator('==')
36 | static eq(a: Int8, b: Int8): bool {
37 | return a.eq(b);
38 | }
39 |
40 | @inline @operator('!=')
41 | static notEq(a: Int8, b: Int8): bool {
42 | return a.notEq(b);
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/assembly/Int/index.ts:
--------------------------------------------------------------------------------
1 | export * from "./CompactInt";
2 | export * from "./Int8";
3 | export * from "./Int16";
4 | export * from "./Int32";
5 | export * from "./Int64";
6 |
--------------------------------------------------------------------------------
/assembly/ScaleMap.ts:
--------------------------------------------------------------------------------
1 | import { CompactInt } from ".";
2 | import { BytesReader } from "./BytesReader";
3 | import { Codec } from "./interfaces/Codec";
4 | import { UnwrappableCodec } from "./interfaces/UnwrappableCodec";
5 |
6 | /**
7 | * @description SCALE Codec support for native Map type
8 | */
9 | export class ScaleMap implements UnwrappableCodec