@polkadot/types-codec 9.11.3 → 9.13.1
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/LICENSE +201 -0
- package/abstract/Array.d.ts +2 -0
- package/abstract/Base.d.ts +14 -3
- package/abstract/Base.js +16 -4
- package/abstract/Int.d.ts +3 -1
- package/abstract/Int.js +13 -9
- package/abstract/Object.d.ts +67 -0
- package/abstract/Object.js +51 -0
- package/base/Compact.d.ts +2 -1
- package/base/DoNotConstruct.d.ts +1 -0
- package/base/Enum.d.ts +2 -1
- package/base/Null.d.ts +2 -1
- package/base/Null.js +0 -1
- package/base/Option.d.ts +1 -0
- package/base/Option.js +1 -1
- package/base/Tuple.d.ts +0 -1
- package/base/Tuple.js +18 -10
- package/base/Vec.d.ts +1 -2
- package/base/Vec.js +21 -8
- package/base/VecFixed.d.ts +0 -1
- package/cjs/abstract/Base.js +16 -4
- package/cjs/abstract/Int.js +12 -8
- package/cjs/abstract/Object.js +58 -0
- package/cjs/base/Null.js +0 -1
- package/cjs/base/Option.js +1 -1
- package/cjs/base/Tuple.js +18 -10
- package/cjs/base/Vec.js +20 -7
- package/cjs/native/Bool.js +1 -0
- package/cjs/native/Date.js +1 -0
- package/cjs/native/Struct.js +6 -14
- package/cjs/packageInfo.js +1 -1
- package/extended/BTreeSet.d.ts +2 -1
- package/extended/Map.d.ts +2 -1
- package/native/Bool.d.ts +2 -0
- package/native/Bool.js +1 -0
- package/native/Date.d.ts +2 -0
- package/native/Date.js +1 -0
- package/native/Float.d.ts +3 -2
- package/native/Json.d.ts +2 -0
- package/native/Raw.d.ts +3 -2
- package/native/Set.d.ts +2 -0
- package/native/Struct.d.ts +3 -2
- package/native/Struct.js +7 -15
- package/native/Text.d.ts +3 -2
- package/package.json +9 -9
- package/packageInfo.js +1 -1
- package/types/codec.d.ts +32 -12
- package/types/helpers.d.ts +9 -1
- package/types/registry.d.ts +4 -3
- package/abstract/BigInt.d.ts +0 -4
- package/abstract/BigInt.js +0 -14
- package/cjs/abstract/BigInt.js +0 -20
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
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.
|
package/abstract/Array.d.ts
CHANGED
|
@@ -10,6 +10,8 @@ import type { AnyJson, Codec, Inspect, IU8a, IVec, Registry } from '../types';
|
|
|
10
10
|
export declare abstract class AbstractArray<T extends Codec> extends Array<T> implements IVec<T> {
|
|
11
11
|
readonly registry: Registry;
|
|
12
12
|
createdAtHash?: IU8a;
|
|
13
|
+
initialU8aLength?: number;
|
|
14
|
+
isStorageFallback?: boolean;
|
|
13
15
|
/**
|
|
14
16
|
* @description This ensures that operators such as clice, filter, map, etc. return
|
|
15
17
|
* new Array instances (without this we need to apply overrides)
|
package/abstract/Base.d.ts
CHANGED
|
@@ -6,9 +6,10 @@ import type { AnyJson, BareOpts, Codec, Inspect, IU8a, Registry } from '../types
|
|
|
6
6
|
*/
|
|
7
7
|
export declare abstract class AbstractBase<T extends Codec> implements Codec {
|
|
8
8
|
#private;
|
|
9
|
-
createdAtHash?: IU8a;
|
|
10
|
-
readonly initialU8aLength?: number;
|
|
11
9
|
readonly registry: Registry;
|
|
10
|
+
createdAtHash?: IU8a;
|
|
11
|
+
initialU8aLength?: number;
|
|
12
|
+
isStorageFallback?: boolean;
|
|
12
13
|
protected constructor(registry: Registry, value: T, initialU8aLength?: number);
|
|
13
14
|
/**
|
|
14
15
|
* @description The length of the value when encoded as a Uint8Array
|
|
@@ -18,6 +19,9 @@ export declare abstract class AbstractBase<T extends Codec> implements Codec {
|
|
|
18
19
|
* @description returns a hash of the contents
|
|
19
20
|
*/
|
|
20
21
|
get hash(): IU8a;
|
|
22
|
+
/**
|
|
23
|
+
* @description returns the inner (wrapped value)
|
|
24
|
+
*/
|
|
21
25
|
get inner(): T;
|
|
22
26
|
/**
|
|
23
27
|
* @description Checks if the value is an empty value
|
|
@@ -59,6 +63,13 @@ export declare abstract class AbstractBase<T extends Codec> implements Codec {
|
|
|
59
63
|
/**
|
|
60
64
|
* @description Returns the base runtime type name for this instance
|
|
61
65
|
*/
|
|
62
|
-
toRawType(): string;
|
|
66
|
+
abstract toRawType(): string;
|
|
67
|
+
/**
|
|
68
|
+
* @description Returns the inner wrapped value (equivalent to valueOf)
|
|
69
|
+
*/
|
|
63
70
|
unwrap(): T;
|
|
71
|
+
/**
|
|
72
|
+
* @description Returns the inner wrapped value
|
|
73
|
+
*/
|
|
74
|
+
valueOf(): T;
|
|
64
75
|
}
|
package/abstract/Base.js
CHANGED
|
@@ -8,8 +8,8 @@
|
|
|
8
8
|
export class AbstractBase {
|
|
9
9
|
#raw;
|
|
10
10
|
constructor(registry, value, initialU8aLength) {
|
|
11
|
-
this.#raw = value;
|
|
12
11
|
this.initialU8aLength = initialU8aLength;
|
|
12
|
+
this.#raw = value;
|
|
13
13
|
this.registry = registry;
|
|
14
14
|
}
|
|
15
15
|
|
|
@@ -26,6 +26,10 @@ export class AbstractBase {
|
|
|
26
26
|
get hash() {
|
|
27
27
|
return this.registry.hash(this.toU8a());
|
|
28
28
|
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* @description returns the inner (wrapped value)
|
|
32
|
+
*/
|
|
29
33
|
get inner() {
|
|
30
34
|
return this.#raw;
|
|
31
35
|
}
|
|
@@ -97,10 +101,18 @@ export class AbstractBase {
|
|
|
97
101
|
/**
|
|
98
102
|
* @description Returns the base runtime type name for this instance
|
|
99
103
|
*/
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* @description Returns the inner wrapped value (equivalent to valueOf)
|
|
107
|
+
*/
|
|
103
108
|
unwrap() {
|
|
104
109
|
return this.#raw;
|
|
105
110
|
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* @description Returns the inner wrapped value
|
|
114
|
+
*/
|
|
115
|
+
valueOf() {
|
|
116
|
+
return this.#raw;
|
|
117
|
+
}
|
|
106
118
|
}
|
package/abstract/Int.d.ts
CHANGED
|
@@ -11,9 +11,11 @@ export declare const DEFAULT_UINT_BITS = 64;
|
|
|
11
11
|
export declare abstract class AbstractInt extends BN implements INumber {
|
|
12
12
|
#private;
|
|
13
13
|
readonly registry: Registry;
|
|
14
|
-
createdAtHash?: IU8a;
|
|
15
14
|
readonly encodedLength: number;
|
|
16
15
|
readonly isUnsigned: boolean;
|
|
16
|
+
createdAtHash?: IU8a;
|
|
17
|
+
initialU8aLength?: number;
|
|
18
|
+
isStorageFallback?: boolean;
|
|
17
19
|
constructor(registry: Registry, value?: AnyNumber, bitLength?: UIntBitLength, isSigned?: boolean);
|
|
18
20
|
/**
|
|
19
21
|
* @description returns a hash of the contents
|
package/abstract/Int.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// Copyright 2017-2023 @polkadot/types-codec authors & contributors
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
3
|
|
|
4
|
-
import { BN, BN_BILLION, BN_HUNDRED, BN_MILLION, BN_QUINTILL, bnToBn, bnToHex, bnToU8a, formatBalance, formatNumber, hexToBn, isBn, isFunction, isHex, isNumber, isObject, isString, isU8a, u8aToBn, u8aToNumber } from '@polkadot/util';
|
|
4
|
+
import { BN, BN_BILLION, BN_HUNDRED, BN_MILLION, BN_QUINTILL, bnToBn, bnToHex, bnToU8a, formatBalance, formatNumber, hexToBn, isBigInt, isBn, isFunction, isHex, isNumber, isObject, isString, isU8a, u8aToBn, u8aToNumber } from '@polkadot/util';
|
|
5
5
|
export const DEFAULT_UINT_BITS = 64;
|
|
6
6
|
|
|
7
7
|
// Maximum allowed integer for JS is 2^53 - 1, set limit at 52
|
|
@@ -9,6 +9,9 @@ export const DEFAULT_UINT_BITS = 64;
|
|
|
9
9
|
const MAX_NUMBER_BITS = 52;
|
|
10
10
|
const MUL_P = new BN(10000);
|
|
11
11
|
const FORMATTERS = [['Perquintill', BN_QUINTILL], ['Perbill', BN_BILLION], ['Permill', BN_MILLION], ['Percent', BN_HUNDRED]];
|
|
12
|
+
function isToBn(value) {
|
|
13
|
+
return isFunction(value.toBn);
|
|
14
|
+
}
|
|
12
15
|
function toPercentage(value, divisor) {
|
|
13
16
|
return `${(value.mul(MUL_P).div(divisor).toNumber() / 100).toFixed(2)}%`;
|
|
14
17
|
}
|
|
@@ -31,9 +34,13 @@ function decodeAbstractInt(value, isNegative) {
|
|
|
31
34
|
throw new Error('String should not contain decimal points or scientific notation');
|
|
32
35
|
}
|
|
33
36
|
return value;
|
|
34
|
-
} else if (isBn(value)) {
|
|
37
|
+
} else if (isBn(value) || isBigInt(value)) {
|
|
35
38
|
return value.toString();
|
|
36
|
-
} else if (isObject(value)
|
|
39
|
+
} else if (isObject(value)) {
|
|
40
|
+
if (isToBn(value)) {
|
|
41
|
+
return value.toBn().toString();
|
|
42
|
+
}
|
|
43
|
+
|
|
37
44
|
// Allow the construction from an object with a single top-level key. This means that
|
|
38
45
|
// single key objects can be treated equivalently to numbers, assuming they meet the
|
|
39
46
|
// specific requirements. (This is useful in Weights 1.5 where Objects are compact)
|
|
@@ -41,13 +48,9 @@ function decodeAbstractInt(value, isNegative) {
|
|
|
41
48
|
if (keys.length !== 1) {
|
|
42
49
|
throw new Error('Unable to construct number from multi-key object');
|
|
43
50
|
}
|
|
44
|
-
|
|
45
|
-
if (!isString(inner) && !isNumber(inner)) {
|
|
46
|
-
throw new Error('Unable to construct from object with non-string/non-number value');
|
|
47
|
-
}
|
|
48
|
-
return decodeAbstractInt(inner, isNegative);
|
|
51
|
+
return decodeAbstractInt(value[keys[0]], isNegative);
|
|
49
52
|
}
|
|
50
|
-
|
|
53
|
+
throw new Error(`Unable to create BN from unknown type ${typeof value}`);
|
|
51
54
|
}
|
|
52
55
|
|
|
53
56
|
/**
|
|
@@ -72,6 +75,7 @@ export class AbstractInt extends BN {
|
|
|
72
75
|
this.registry = registry;
|
|
73
76
|
this.#bitLength = bitLength;
|
|
74
77
|
this.encodedLength = this.#bitLength / 8;
|
|
78
|
+
this.initialU8aLength = this.#bitLength / 8;
|
|
75
79
|
this.isUnsigned = !isSigned;
|
|
76
80
|
const isNegative = this.isNeg();
|
|
77
81
|
const maxBits = bitLength - (isSigned && !isNegative ? 1 : 0);
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import type { HexString } from '@polkadot/util/types';
|
|
2
|
+
import type { AnyJson, BareOpts, CodecObject, Inspect, IU8a, Registry, ToString } from '../types';
|
|
3
|
+
/**
|
|
4
|
+
* @name Object
|
|
5
|
+
* @description A type extends the Base class, when it holds a value
|
|
6
|
+
*/
|
|
7
|
+
export declare abstract class AbstractObject<T extends ToString> implements CodecObject<T> {
|
|
8
|
+
readonly registry: Registry;
|
|
9
|
+
createdAtHash?: IU8a;
|
|
10
|
+
initialU8aLength?: number;
|
|
11
|
+
isStorageFallback?: boolean;
|
|
12
|
+
readonly $: T;
|
|
13
|
+
protected constructor(registry: Registry, value: T, initialU8aLength?: number);
|
|
14
|
+
/**
|
|
15
|
+
* @description The length of the value when encoded as a Uint8Array
|
|
16
|
+
*/
|
|
17
|
+
get encodedLength(): number;
|
|
18
|
+
/**
|
|
19
|
+
* @description returns a hash of the contents
|
|
20
|
+
*/
|
|
21
|
+
get hash(): IU8a;
|
|
22
|
+
/**
|
|
23
|
+
* @description Checks if the value is an empty value
|
|
24
|
+
*/
|
|
25
|
+
abstract get isEmpty(): boolean;
|
|
26
|
+
/**
|
|
27
|
+
* @description Compares the value of the input to see if there is a match
|
|
28
|
+
*/
|
|
29
|
+
abstract eq(other?: unknown): boolean;
|
|
30
|
+
/**
|
|
31
|
+
* @description Returns a breakdown of the hex encoding for this Codec
|
|
32
|
+
*/
|
|
33
|
+
abstract inspect(): Inspect;
|
|
34
|
+
/**
|
|
35
|
+
* @description Returns a hex string representation of the value. isLe returns a LE (number-only) representation
|
|
36
|
+
*/
|
|
37
|
+
abstract toHex(isLe?: boolean): HexString;
|
|
38
|
+
/**
|
|
39
|
+
* @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
|
|
40
|
+
*/
|
|
41
|
+
abstract toHuman(isExtended?: boolean): AnyJson;
|
|
42
|
+
/**
|
|
43
|
+
* @description Converts the Object to JSON, typically used for RPC transfers
|
|
44
|
+
*/
|
|
45
|
+
abstract toJSON(): AnyJson;
|
|
46
|
+
/**
|
|
47
|
+
* @description Converts the value in a best-fit primitive form
|
|
48
|
+
*/
|
|
49
|
+
abstract toPrimitive(): AnyJson;
|
|
50
|
+
/**
|
|
51
|
+
* @description Returns the string representation of the value
|
|
52
|
+
*/
|
|
53
|
+
toString(): string;
|
|
54
|
+
/**
|
|
55
|
+
* @description Encodes the value as a Uint8Array as per the SCALE specifications
|
|
56
|
+
* @param isBare true when the value has none of the type-specific prefixes (internal)
|
|
57
|
+
*/
|
|
58
|
+
abstract toU8a(isBare?: BareOpts): Uint8Array;
|
|
59
|
+
/**
|
|
60
|
+
* @description Returns the base runtime type name for this instance
|
|
61
|
+
*/
|
|
62
|
+
abstract toRawType(): string;
|
|
63
|
+
/**
|
|
64
|
+
* @description Return the internal value (JS-aligned, same result as $)
|
|
65
|
+
*/
|
|
66
|
+
valueOf(): T;
|
|
67
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
// Copyright 2017-2023 @polkadot/types-codec authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @name Object
|
|
6
|
+
* @description A type extends the Base class, when it holds a value
|
|
7
|
+
*/
|
|
8
|
+
export class AbstractObject {
|
|
9
|
+
constructor(registry, value, initialU8aLength) {
|
|
10
|
+
this.$ = value;
|
|
11
|
+
this.initialU8aLength = initialU8aLength;
|
|
12
|
+
this.registry = registry;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @description The length of the value when encoded as a Uint8Array
|
|
17
|
+
*/
|
|
18
|
+
get encodedLength() {
|
|
19
|
+
return this.toU8a().length;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* @description returns a hash of the contents
|
|
24
|
+
*/
|
|
25
|
+
get hash() {
|
|
26
|
+
return this.registry.hash(this.toU8a());
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* @description Checks if the value is an empty value
|
|
31
|
+
*/
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* @description Returns the string representation of the value
|
|
35
|
+
*/
|
|
36
|
+
toString() {
|
|
37
|
+
return this.$.toString();
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* @description Encodes the value as a Uint8Array as per the SCALE specifications
|
|
42
|
+
* @param isBare true when the value has none of the type-specific prefixes (internal)
|
|
43
|
+
*/
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* @description Return the internal value (JS-aligned, same result as $)
|
|
47
|
+
*/
|
|
48
|
+
valueOf() {
|
|
49
|
+
return this.$;
|
|
50
|
+
}
|
|
51
|
+
}
|
package/base/Compact.d.ts
CHANGED
|
@@ -18,7 +18,8 @@ export declare class Compact<T extends INumber> implements ICompact<T> {
|
|
|
18
18
|
#private;
|
|
19
19
|
readonly registry: Registry;
|
|
20
20
|
createdAtHash?: IU8a;
|
|
21
|
-
|
|
21
|
+
initialU8aLength?: number;
|
|
22
|
+
isStorageFallback?: boolean;
|
|
22
23
|
constructor(registry: Registry, Type: CodecClass<T> | string, value?: Compact<T> | AnyNumber, { definition, setDefinition }?: Options<T>);
|
|
23
24
|
static with<O extends INumber>(Type: CodecClass<O> | string): CodecClass<Compact<O>>;
|
|
24
25
|
/**
|
package/base/DoNotConstruct.d.ts
CHANGED
package/base/Enum.d.ts
CHANGED
|
@@ -27,7 +27,8 @@ export declare class Enum implements IEnum {
|
|
|
27
27
|
#private;
|
|
28
28
|
readonly registry: Registry;
|
|
29
29
|
createdAtHash?: IU8a;
|
|
30
|
-
|
|
30
|
+
initialU8aLength?: number;
|
|
31
|
+
isStorageFallback?: boolean;
|
|
31
32
|
constructor(registry: Registry, Types: Record<string, string | CodecClass> | Record<string, number> | string[], value?: unknown, index?: number, { definition, setDefinition }?: Options);
|
|
32
33
|
static with(Types: Record<string, string | CodecClass> | Record<string, number> | string[]): EnumCodecClass<Enum>;
|
|
33
34
|
/**
|
package/base/Null.d.ts
CHANGED
|
@@ -10,7 +10,8 @@ export declare class Null implements Codec {
|
|
|
10
10
|
readonly isEmpty = true;
|
|
11
11
|
readonly registry: Registry;
|
|
12
12
|
createdAtHash?: IU8a;
|
|
13
|
-
|
|
13
|
+
initialU8aLength: number;
|
|
14
|
+
isStorageFallback?: boolean;
|
|
14
15
|
constructor(registry: Registry);
|
|
15
16
|
/**
|
|
16
17
|
* @description returns a hash of the contents
|
package/base/Null.js
CHANGED
package/base/Option.d.ts
CHANGED
|
@@ -17,6 +17,7 @@ export declare class Option<T extends Codec> implements IOption<T> {
|
|
|
17
17
|
readonly registry: Registry;
|
|
18
18
|
createdAtHash?: IU8a;
|
|
19
19
|
initialU8aLength?: number;
|
|
20
|
+
isStorageFallback?: boolean;
|
|
20
21
|
constructor(registry: Registry, typeName: CodecClass<T> | string, value?: unknown, { definition, setDefinition }?: Options<T>);
|
|
21
22
|
static with<O extends Codec>(Type: CodecClass<O> | string): CodecClass<Option<O>>;
|
|
22
23
|
/**
|
package/base/Option.js
CHANGED
|
@@ -63,7 +63,7 @@ export class Option {
|
|
|
63
63
|
this.registry = registry;
|
|
64
64
|
this.#Type = Type;
|
|
65
65
|
this.#raw = decoded;
|
|
66
|
-
if (decoded && decoded.initialU8aLength) {
|
|
66
|
+
if (decoded != null && decoded.initialU8aLength) {
|
|
67
67
|
this.initialU8aLength = 1 + decoded.initialU8aLength;
|
|
68
68
|
}
|
|
69
69
|
}
|
package/base/Tuple.d.ts
CHANGED
|
@@ -17,7 +17,6 @@ interface Options {
|
|
|
17
17
|
*/
|
|
18
18
|
export declare class Tuple extends AbstractArray<Codec> implements ITuple<Codec[]> {
|
|
19
19
|
#private;
|
|
20
|
-
readonly initialU8aLength?: number;
|
|
21
20
|
constructor(registry: Registry, Types: TupleTypes | TupleType, value?: AnyTupleValue, { definition, setDefinition }?: Options);
|
|
22
21
|
static with(Types: TupleTypes | TupleType): CodecClass<Tuple>;
|
|
23
22
|
/**
|
package/base/Tuple.js
CHANGED
|
@@ -10,19 +10,27 @@ function noopSetDefinition(d) {
|
|
|
10
10
|
|
|
11
11
|
/** @internal */
|
|
12
12
|
function decodeTuple(registry, result, value, Classes) {
|
|
13
|
-
if (
|
|
13
|
+
if (Array.isArray(value)) {
|
|
14
|
+
const Types = Classes[0];
|
|
15
|
+
for (let i = 0; i < Types.length; i++) {
|
|
16
|
+
try {
|
|
17
|
+
const entry = value == null ? void 0 : value[i];
|
|
18
|
+
result[i] = entry instanceof Types[i] ? entry : new Types[i](registry, entry);
|
|
19
|
+
} catch (error) {
|
|
20
|
+
throw new Error(`Tuple: failed on ${i}:: ${error.message}`);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return [result, 0];
|
|
24
|
+
} else if (isHex(value)) {
|
|
14
25
|
return decodeU8a(registry, result, u8aToU8a(value), Classes);
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
const entry = value == null ? void 0 : value[i];
|
|
20
|
-
result[i] = entry instanceof Types[i] ? entry : new Types[i](registry, entry);
|
|
21
|
-
} catch (error) {
|
|
22
|
-
throw new Error(`Tuple: failed on ${i}:: ${error.message}`);
|
|
26
|
+
} else if (!value || !result.length) {
|
|
27
|
+
const Types = Classes[0];
|
|
28
|
+
for (let i = 0; i < Types.length; i++) {
|
|
29
|
+
result[i] = new Types[i](registry);
|
|
23
30
|
}
|
|
31
|
+
return [result, 0];
|
|
24
32
|
}
|
|
25
|
-
|
|
33
|
+
throw new Error(`Expected array input to Tuple decoding, found ${typeof value}: ${stringify(value)}`);
|
|
26
34
|
}
|
|
27
35
|
|
|
28
36
|
/**
|
package/base/Vec.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ interface Options<T> {
|
|
|
5
5
|
definition?: CodecClass<T>;
|
|
6
6
|
setDefinition?: (d: CodecClass<T>) => CodecClass<T>;
|
|
7
7
|
}
|
|
8
|
-
export declare function decodeVec<T extends Codec>(registry: Registry, result: T[], value: Uint8Array | HexString | unknown[], startAt: number, Type: CodecClass<T>): [number, number];
|
|
8
|
+
export declare function decodeVec<T extends Codec>(registry: Registry, result: T[], value: Uint8Array | HexString | unknown[] | null, startAt: number, Type: CodecClass<T>): [number, number];
|
|
9
9
|
/**
|
|
10
10
|
* @name Vec
|
|
11
11
|
* @description
|
|
@@ -15,7 +15,6 @@ export declare function decodeVec<T extends Codec>(registry: Registry, result: T
|
|
|
15
15
|
*/
|
|
16
16
|
export declare class Vec<T extends Codec> extends AbstractArray<T> {
|
|
17
17
|
#private;
|
|
18
|
-
readonly initialU8aLength?: number;
|
|
19
18
|
constructor(registry: Registry, Type: CodecClass<T> | string, value?: Uint8Array | HexString | unknown[], { definition, setDefinition }?: Options<T>);
|
|
20
19
|
static with<O extends Codec>(Type: CodecClass<O> | string): CodecClass<Vec<O>>;
|
|
21
20
|
/**
|