solidity-scale-codec 0.3.2 → 0.3.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -0
- package/README.md +1 -1
- package/package.json +2 -2
- package/DEFINITIONS.md +0 -132
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
[Unreleased]
|
|
9
9
|
|
|
10
|
+
## Version 0.3.4
|
|
11
|
+
|
|
12
|
+
## Changed
|
|
13
|
+
|
|
14
|
+
- Move Definitions to website docs - [#f57c914](https://github.com/LucasGrasso/solidity-scale-codec/commit/7e88534930cdb8ac47b5f16a02f2203c588a7e7f)
|
|
15
|
+
|
|
16
|
+
## Version 0.3.3
|
|
17
|
+
|
|
18
|
+
## Changed
|
|
19
|
+
|
|
20
|
+
- Homepage for the docsite - [#fcbac35](https://github.com/LucasGrasso/solidity-scale-codec/commit/757cddcce6d9f7a82f9535471c86f0cffa878956)
|
|
21
|
+
|
|
10
22
|
## Version 0.3.2
|
|
11
23
|
|
|
12
24
|
## Changed
|
package/README.md
CHANGED
|
@@ -22,7 +22,7 @@ This library provides a Highly-Modular implementation of SCALE in solidity.
|
|
|
22
22
|
| `Compact` | A "compact" or general integer encoding is sufficient for encoding large integers (up to 2\*\*536) and is more efficient at encoding most values than the fixed-width version. (Though for single-byte values, the fixed-width integer is never worse.) | `0` | `0x00` |
|
|
23
23
|
| `Arrays` | A collection of same-typed values is encoded, prefixed with a compact encoding of the number of items, followed by each item's encoding concatenated in turn. Currently `[uintN]`,`[intN]`, `[bool]` are supported. | `[1,0]` | `0x080100` |
|
|
24
24
|
|
|
25
|
-
See the [Definitions](
|
|
25
|
+
See the [Definitions](https://lucasgrasso.github.io/solidity-scale-codec/Definitions.html) for more details on the encoding of different types.
|
|
26
26
|
|
|
27
27
|
## Usage
|
|
28
28
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "solidity-scale-codec",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.4",
|
|
4
4
|
"description": "Solidity implementation of scale-codec.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"solidity",
|
|
@@ -47,7 +47,7 @@
|
|
|
47
47
|
"hardhat": "^3.0.15",
|
|
48
48
|
"prettier": "^3.8.1",
|
|
49
49
|
"prettier-plugin-solidity": "^2.2.1",
|
|
50
|
-
"solidity-doc-generator": "^1.
|
|
50
|
+
"solidity-doc-generator": "^1.2.1",
|
|
51
51
|
"typescript": "~5.8.0",
|
|
52
52
|
"viem": "^2.40.3",
|
|
53
53
|
"vitepress": "^1.6.4",
|
package/DEFINITIONS.md
DELETED
|
@@ -1,132 +0,0 @@
|
|
|
1
|
-
> Note: These definitions were taken from https://github.com/w3f/polkadot-spec/blob/main/docs. They are provided here for reference. All credits to respective authors.
|
|
2
|
-
|
|
3
|
-
# Notation
|
|
4
|
-
|
|
5
|
-
- Let $\mathbb{{B}}$ be the set of all byte sequences.
|
|
6
|
-
- For $x \in \mathbb{{B}}$, $x_i$ denotes the $i$-th byte of $x$, and $x_i^j$ denotes the $j$-th bit of the $i$-th byte of $x$.
|
|
7
|
-
|
|
8
|
-
# Definitions
|
|
9
|
-
|
|
10
|
-
## Little Endian
|
|
11
|
-
|
|
12
|
-
By the **little-endian** representation of a non-negative integer, ${I}$, represented as
|
|
13
|
-
|
|
14
|
-
$$
|
|
15
|
-
{I}={\left({B}_{{n}}\ldots{B}_{{0}}\right)}_{{256}}
|
|
16
|
-
$$
|
|
17
|
-
|
|
18
|
-
in base 256, we refer to a byte array ${B}={\left({b}_{{0}},{b}_{{1}},\ldots,{b}_{{n}}\right)}$ such that
|
|
19
|
-
|
|
20
|
-
$$
|
|
21
|
-
{b}_{{i}}\:={B}_{{i}}
|
|
22
|
-
$$
|
|
23
|
-
|
|
24
|
-
Accordingly, we define the function ${\mathsf{\text{Enc}}}_{{{\mathsf{\text{LE}}}}}$:
|
|
25
|
-
|
|
26
|
-
$$
|
|
27
|
-
{\mathsf{\text{Enc}}}_{{{\mathsf{\text{LE}}}}}:{\mathbb{{Z}}}^{+}\rightarrow{\mathbb{{B}}};{\left({B}_{{n}}\ldots{B}_{{0}}\right)}_{{256}}{\mid}\rightarrow{\left({B}_{{{0},}}{B}_{{1}},\ldots,{B}_{{n}}\right)}
|
|
28
|
-
$$
|
|
29
|
-
|
|
30
|
-
## Scale Types
|
|
31
|
-
|
|
32
|
-
### Fixed Length Integers
|
|
33
|
-
|
|
34
|
-
The SCALE codec, $\text{Enc}_{{\text{SC}}}$, for fixed length integers not defined here otherwise, is equal to the little-endian encoding of those values.
|
|
35
|
-
|
|
36
|
-
### Varying Data Type
|
|
37
|
-
|
|
38
|
-
> This library does not provide means for encoding/decoding varying data types, but the definitions are provided here for completeness and reference. The implementation is left to the user of the library.
|
|
39
|
-
|
|
40
|
-
We define a **varying data** type to be an ordered set of data types.
|
|
41
|
-
|
|
42
|
-
$$
|
|
43
|
-
{\mathcal{{T}}}={\left\lbrace{T}_{{1}},\ldots,{T}_{{n}}\right\rbrace}
|
|
44
|
-
$$
|
|
45
|
-
|
|
46
|
-
A value ${A}$ of varying data type is a pair ${\left({A}_{{\text{Type}}},{A}_{{\text{Value}}}\right)}$ where ${A}_{{\text{Type}}}={T}_{{i}}$ for some ${T}_{{i}}\in{\mathcal{{T}}}$ and ${A}_{{\text{Value}}}$ is its value of type ${T}_{{i}}$, which can be empty. We define $\text{idx}{\left({T}_{{i}}\right)}={i}-{1}$, unless it is explicitly defined as another value in the definition of a particular varying data type.
|
|
47
|
-
|
|
48
|
-
The SCALE codec for value ${A}={\left({A}_{{\text{Type}}},{A}_{{\text{Value}}}\right)}$ of varying data type ${\mathcal{{T}}}={\left\lbrace{T}_{{i}},\ldots{T}_{{n}}\right\rbrace}$, formally referred to as $\text{Enc}_{{\text{SC}}}{\left({A}\right)}$ is defined as follows:
|
|
49
|
-
|
|
50
|
-
$$
|
|
51
|
-
\text{Enc}_{{\text{SC}}}{\left({A}\right)}\:=\text{Enc}_{{\text{SC}}}{\left(\text{idx}{\left({A}_{{\text{Type}}}\right)}\text{||}\text{Enc}_{{\text{SC}}}{\left({A}_{{\text{Value}}}\right)}\right)}
|
|
52
|
-
$$
|
|
53
|
-
|
|
54
|
-
The SCALE codec does not encode the correspondence between the value and the data type it represents; the decoder needs prior knowledge of such correspondence to decode the data.
|
|
55
|
-
|
|
56
|
-
### Boolean
|
|
57
|
-
|
|
58
|
-
The SCALE codec for a **boolean value** ${b}$ defined as a byte as follows:
|
|
59
|
-
|
|
60
|
-
$$
|
|
61
|
-
\text{Enc}_{{\text{SC}}}:{\left\lbrace\text{False},\text{True}\right\rbrace}\rightarrow{\mathbb{{B}}}_{{1}}
|
|
62
|
-
$$
|
|
63
|
-
|
|
64
|
-
$$
|
|
65
|
-
{b}\rightarrow{\left\lbrace\begin{matrix}{0}&{b}=\text{False}\\{1}&{b}=\text{True}\end{matrix}\right.}
|
|
66
|
-
$$
|
|
67
|
-
|
|
68
|
-
### Compact
|
|
69
|
-
|
|
70
|
-
**SCALE Length encoding** ${\text{Enc}_{{\text{SC}}}^{{\text{Len}}}}$, also known as a _compact encoding_, of a non-negative number ${n}$ is defined as follows:
|
|
71
|
-
|
|
72
|
-
$$
|
|
73
|
-
{\text{Enc}_{{\text{SC}}}^{{\text{Len}}}}:{\mathbb{{N}}}\rightarrow{\mathbb{{B}}}
|
|
74
|
-
$$
|
|
75
|
-
|
|
76
|
-
$$
|
|
77
|
-
{n}\rightarrow{b}\:={\left\lbrace\begin{matrix}{l}_{{1}}&{0}\le{n}<{2}^{{6}}\\{i}_{{1}}{i}_{{2}}&{2}^{{6}}\le{n}<{2}^{{14}}\\{j}_{{1}}{j}_{{2}}{j}_{{3}}{j}_{{4}}&{2}^{{14}}\le{n}<{2}^{{30}}\\{k}_{{1}}{k}_{{2}}\ldots{k}_{{m}+{1}}&{2}^{{30}}\le{n}\end{matrix}\right.}
|
|
78
|
-
$$
|
|
79
|
-
|
|
80
|
-
$$
|
|
81
|
-
{{l}_{{1}}^{{1}}}{{l}_{{1}}^{{0}}}={00}
|
|
82
|
-
$$
|
|
83
|
-
|
|
84
|
-
$$
|
|
85
|
-
{{i}_{{1}}^{{1}}}{{i}_{{1}}^{{0}}}={01}
|
|
86
|
-
$$
|
|
87
|
-
|
|
88
|
-
$$
|
|
89
|
-
{{j}_{{1}}^{{1}}}{{j}_{{1}}^{{0}}}={10}
|
|
90
|
-
$$
|
|
91
|
-
|
|
92
|
-
$$
|
|
93
|
-
{{k}_{{1}}^{{1}}}{{k}_{{1}}^{{0}}}={11}
|
|
94
|
-
$$
|
|
95
|
-
|
|
96
|
-
and the rest of the bits of ${b}$ store the value of ${n}$ in little-endian format in base-2 as follows:
|
|
97
|
-
|
|
98
|
-
$$
|
|
99
|
-
{n}\:={\left\lbrace\begin{matrix}{{l}_{{1}}^{{7}}}\ldots{{l}_{{1}}^{{3}}}{{l}_{{1}}^{{2}}}&{n}<{2}^{{6}}\\{{i}_{{2}}^{{7}}}\ldots{{i}_{{2}}^{{0}}}{{i}_{{1}}^{{7}}}..{{i}_{{1}}^{{2}}}&{2}^{{6}}\le{n}<{2}^{{14}}\\{{j}_{{4}}^{{7}}}\ldots{{j}_{{4}}^{{0}}}{{j}_{{3}}^{{7}}}\ldots{{j}_{{1}}^{{7}}}\ldots{{j}_{{1}}^{{2}}}&{2}^{{14}}\le{n}<{2}^{{30}}\\{k}_{{2}}+{k}_{{3}}{2}^{{8}}+{k}_{{4}}{2}^{{{2}\times{8}}}+\ldots+{k}_{{m}+{1}}{2}^{{{\left({m}-{1}\right)}{8}}}&{2}^{{30}}\le{n}\end{matrix}\right.}
|
|
100
|
-
$$
|
|
101
|
-
|
|
102
|
-
such that:
|
|
103
|
-
|
|
104
|
-
$$
|
|
105
|
-
{{k}_{{1}}^{{7}}}\ldots{{k}_{{1}}^{{3}}}{{k}_{{1}}^{{2}}}\:={m}-{4}
|
|
106
|
-
$$
|
|
107
|
-
|
|
108
|
-
Note that ${m}$ denotes the length of the original integer being encoded and does not include the extra byte describing the length. The encoding can be used for integers up to: $$2^{(63+4)8} -1 = 2^{536} -1$$
|
|
109
|
-
|
|
110
|
-
### Sequence
|
|
111
|
-
|
|
112
|
-
The **SCALE codec** for **sequence** ${S}$ such that:
|
|
113
|
-
|
|
114
|
-
$$
|
|
115
|
-
{S}\:={A}_{{1}},\ldots{A}_{{n}}
|
|
116
|
-
$$
|
|
117
|
-
|
|
118
|
-
where ${A}_{{i}}$’s are values of **the same type** (and the decoder is unable to infer value of ${n}$ from the context) is defined as:
|
|
119
|
-
|
|
120
|
-
$$
|
|
121
|
-
\text{Enc}_{{\text{SC}}}{\left({S}\right)}\:={\text{Enc}_{{\text{SC}}}^{{\text{Len}}}}{\left({\left|{{S}}\right|}\right)}\text{||}\text{Enc}_{{\text{SC}}}{\left({A}_{{2}}\right)}\text{||}\ldots\text{||}\text{Enc}_{{\text{SC}}}{\left({A}_{{n}}\right)}
|
|
122
|
-
$$
|
|
123
|
-
|
|
124
|
-
where ${\text{Enc}_{{\text{SC}}}^{{\text{Len}}}}$ is defined [here](#compact).
|
|
125
|
-
|
|
126
|
-
In some cases, the length indicator ${\text{Enc}_{{\text{SC}}}^{{\text{Len}}}}{\left({\left|{{S}}\right|}\right)}$ is omitted if the length of the sequence is fixed and known by the decoder upfront. Such cases are explicitly stated by the definition of the corresponding type.
|
|
127
|
-
|
|
128
|
-
### String
|
|
129
|
-
|
|
130
|
-
The SCALE codec for a **string value** is an [encoded sequence](#sequence) consisting of UTF-8 encoded bytes.
|
|
131
|
-
|
|
132
|
-
> This can be achieved via encoding the UTF-8 sequence as a `uint8[]` array, which is supported by this library.
|