@polkadot/types-create 9.5.2 → 9.6.2

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/package.json CHANGED
@@ -20,7 +20,7 @@
20
20
  "./cjs/detectPackage.js"
21
21
  ],
22
22
  "type": "module",
23
- "version": "9.5.2",
23
+ "version": "9.6.2",
24
24
  "main": "./cjs/index.js",
25
25
  "module": "./index.js",
26
26
  "types": "./index.d.ts",
@@ -120,8 +120,8 @@
120
120
  }
121
121
  },
122
122
  "dependencies": {
123
- "@babel/runtime": "^7.19.4",
124
- "@polkadot/types-codec": "9.5.2",
123
+ "@babel/runtime": "^7.20.0",
124
+ "@polkadot/types-codec": "9.6.2",
125
125
  "@polkadot/util": "^10.1.11"
126
126
  }
127
127
  }
package/packageInfo.js CHANGED
@@ -1,9 +1,11 @@
1
1
  // Copyright 2017-2022 @polkadot/types-create authors & contributors
2
2
  // SPDX-License-Identifier: Apache-2.0
3
+
3
4
  // Do not edit, auto-generated by @polkadot/dev
5
+
4
6
  export const packageInfo = {
5
7
  name: '@polkadot/types-create',
6
8
  path: (import.meta && import.meta.url) ? new URL(import.meta.url).pathname.substring(0, new URL(import.meta.url).pathname.lastIndexOf('/') + 1) : 'auto',
7
9
  type: 'esm',
8
- version: '9.5.2'
10
+ version: '9.6.2'
9
11
  };
@@ -1,5 +1,6 @@
1
1
  // Copyright 2017-2022 @polkadot/types-create authors & contributors
2
2
  // SPDX-License-Identifier: Apache-2.0
3
+
3
4
  // import type lookup before we augment - in some environments
4
5
  // this is required to allow for ambient/previous definitions
5
6
  import '@polkadot/types-codec/types/registry';
package/types/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  // Copyright 2017-2022 @polkadot/types-create authors & contributors
2
2
  // SPDX-License-Identifier: Apache-2.0
3
+
3
4
  import "./augmentRegistry.js";
4
5
  export * from "./lookup.js";
5
6
  export * from "./types.js";
package/types/types.js CHANGED
@@ -1,7 +1,7 @@
1
1
  // Copyright 2017-2022 @polkadot/types-create authors & contributors
2
2
  // SPDX-License-Identifier: Apache-2.0
3
- export let TypeDefInfo;
4
3
 
4
+ export let TypeDefInfo;
5
5
  (function (TypeDefInfo) {
6
6
  TypeDefInfo[TypeDefInfo["BTreeMap"] = 0] = "BTreeMap";
7
7
  TypeDefInfo[TypeDefInfo["BTreeSet"] = 1] = "BTreeSet";
@@ -1,21 +1,18 @@
1
1
  // Copyright 2017-2022 @polkadot/types-create authors & contributors
2
2
  // SPDX-License-Identifier: Apache-2.0
3
+
3
4
  import { isNumber, isUndefined, objectSpread, stringify } from '@polkadot/util';
4
5
  import { TypeDefInfo } from "../types/index.js";
5
-
6
6
  const stringIdentity = value => value.toString();
7
-
8
7
  const INFO_WRAP = ['BTreeMap', 'BTreeSet', 'Compact', 'HashMap', 'Option', 'Result', 'Vec'];
9
8
  export function paramsNotation(outer, inner, transform = stringIdentity) {
10
9
  return `${outer}${inner ? `<${(Array.isArray(inner) ? inner : [inner]).map(transform).join(', ')}>` : ''}`;
11
10
  }
12
-
13
11
  function encodeWithParams(registry, typeDef, outer) {
14
12
  const {
15
13
  info,
16
14
  sub
17
15
  } = typeDef;
18
-
19
16
  switch (info) {
20
17
  case TypeDefInfo.BTreeMap:
21
18
  case TypeDefInfo.BTreeSet:
@@ -31,33 +28,27 @@ function encodeWithParams(registry, typeDef, outer) {
31
28
  case TypeDefInfo.WrapperOpaque:
32
29
  return paramsNotation(outer, sub, p => encodeTypeDef(registry, p));
33
30
  }
34
-
35
31
  throw new Error(`Unable to encode ${stringify(typeDef)} with params`);
36
32
  }
37
-
38
33
  function encodeSubTypes(registry, sub, asEnum, extra) {
39
34
  const names = sub.map(({
40
35
  name
41
36
  }) => name);
42
-
43
37
  if (!names.every(n => !!n)) {
44
38
  throw new Error(`Subtypes does not have consistent names, ${names.join(', ')}`);
45
39
  }
46
-
47
40
  const inner = objectSpread({}, extra);
48
-
49
41
  for (let i = 0; i < sub.length; i++) {
50
42
  const def = sub[i];
51
43
  inner[def.name] = encodeTypeDef(registry, def);
52
44
  }
53
-
54
45
  return stringify(asEnum ? {
55
46
  _enum: inner
56
47
  } : inner);
57
- } // We setup a record here to ensure we have comprehensive coverage (any item not covered will result
58
- // in a compile-time error with the missing index)
59
-
48
+ }
60
49
 
50
+ // We setup a record here to ensure we have comprehensive coverage (any item not covered will result
51
+ // in a compile-time error with the missing index)
61
52
  const encoders = {
62
53
  [TypeDefInfo.BTreeMap]: (registry, typeDef) => encodeWithParams(registry, typeDef, 'BTreeMap'),
63
54
  [TypeDefInfo.BTreeSet]: (registry, typeDef) => encodeWithParams(registry, typeDef, 'BTreeSet'),
@@ -72,10 +63,10 @@ const encoders = {
72
63
  }) => {
73
64
  if (!Array.isArray(sub)) {
74
65
  throw new Error('Unable to encode Enum type');
75
- } // c-like enums have all Null entries
76
- // TODO We need to take the disciminant into account and auto-add empty entries
77
-
66
+ }
78
67
 
68
+ // c-like enums have all Null entries
69
+ // TODO We need to take the disciminant into account and auto-add empty entries
79
70
  return sub.every(({
80
71
  type
81
72
  }) => type === 'Null') ? stringify({
@@ -108,7 +99,6 @@ const encoders = {
108
99
  if (!Array.isArray(sub)) {
109
100
  throw new Error('Unable to encode Set type');
110
101
  }
111
-
112
102
  return stringify({
113
103
  _set: sub.reduce((all, {
114
104
  index,
@@ -132,7 +122,6 @@ const encoders = {
132
122
  if (!Array.isArray(sub)) {
133
123
  throw new Error('Unable to encode Struct type');
134
124
  }
135
-
136
125
  return encodeSubTypes(registry, sub, false, alias ? {
137
126
  _alias: [...alias.entries()].reduce((all, [k, v]) => objectSpread(all, {
138
127
  [k]: v
@@ -145,7 +134,6 @@ const encoders = {
145
134
  if (!Array.isArray(sub)) {
146
135
  throw new Error('Unable to encode Tuple type');
147
136
  }
148
-
149
137
  return `(${sub.map(type => encodeTypeDef(registry, type)).join(',')})`;
150
138
  },
151
139
  [TypeDefInfo.UInt]: (registry, {
@@ -159,17 +147,14 @@ const encoders = {
159
147
  if (!isNumber(length) || !sub || Array.isArray(sub)) {
160
148
  throw new Error('Unable to encode VecFixed type');
161
149
  }
162
-
163
150
  return `[${sub.type};${length}]`;
164
151
  },
165
152
  [TypeDefInfo.WrapperKeepOpaque]: (registry, typeDef) => encodeWithParams(registry, typeDef, 'WrapperKeepOpaque'),
166
153
  [TypeDefInfo.WrapperOpaque]: (registry, typeDef) => encodeWithParams(registry, typeDef, 'WrapperOpaque')
167
154
  };
168
-
169
155
  function encodeType(registry, typeDef, withLookup = true) {
170
156
  return withLookup && typeDef.lookupName ? typeDef.lookupName : encoders[typeDef.info](registry, typeDef);
171
157
  }
172
-
173
158
  export function encodeTypeDef(registry, typeDef) {
174
159
  // In the case of contracts we do have the unfortunate situation where the displayName would
175
160
  // refer to "Option" when it is an option. For these, string it out, only using when actually
@@ -1,37 +1,34 @@
1
1
  // Copyright 2017-2022 @polkadot/types-create authors & contributors
2
2
  // SPDX-License-Identifier: Apache-2.0
3
+
3
4
  import { sanitize } from '@polkadot/types-codec';
4
5
  import { isNumber, isString, objectSpread } from '@polkadot/util';
5
6
  import { TypeDefInfo } from "../types/index.js";
6
7
  import { typeSplit } from "./typeSplit.js";
7
8
  const KNOWN_INTERNALS = ['_alias', '_fallback'];
8
-
9
9
  function getTypeString(typeOrObj) {
10
10
  return isString(typeOrObj) ? typeOrObj.toString() : JSON.stringify(typeOrObj);
11
11
  }
12
-
13
12
  function isRustEnum(details) {
14
13
  const values = Object.values(details);
15
-
16
14
  if (values.some(v => isNumber(v))) {
17
15
  if (!values.every(v => isNumber(v) && v >= 0 && v <= 255)) {
18
16
  throw new Error('Invalid number-indexed enum definition');
19
17
  }
20
-
21
18
  return false;
22
19
  }
23
-
24
20
  return true;
25
- } // decode an enum of either of the following forms
21
+ }
22
+
23
+ // decode an enum of either of the following forms
26
24
  // { _enum: ['A', 'B', 'C'] }
27
25
  // { _enum: { A: AccountId, B: Balance, C: u32 } }
28
26
  // { _enum: { A: 1, B: 2 } }
29
-
30
-
31
27
  function _decodeEnum(value, details, count, fallbackType) {
32
28
  value.info = TypeDefInfo.Enum;
33
- value.fallbackType = fallbackType; // not as pretty, but remain compatible with oo7 for both struct and Array types
29
+ value.fallbackType = fallbackType;
34
30
 
31
+ // not as pretty, but remain compatible with oo7 for both struct and Array types
35
32
  if (Array.isArray(details)) {
36
33
  value.sub = details.map((name, index) => ({
37
34
  index,
@@ -53,12 +50,11 @@ function _decodeEnum(value, details, count, fallbackType) {
53
50
  type: 'Null'
54
51
  }));
55
52
  }
56
-
57
53
  return value;
58
- } // decode a set of the form
59
- // { _set: { A: 0b0001, B: 0b0010, C: 0b0100 } }
60
-
54
+ }
61
55
 
56
+ // decode a set of the form
57
+ // { _set: { A: 0b0001, B: 0b0010, C: 0b0100 } }
62
58
  function _decodeSet(value, details, fallbackType) {
63
59
  value.info = TypeDefInfo.Set;
64
60
  value.fallbackType = fallbackType;
@@ -70,35 +66,32 @@ function _decodeSet(value, details, fallbackType) {
70
66
  type: 'Null'
71
67
  }));
72
68
  return value;
73
- } // decode a struct, set or enum
74
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
75
-
69
+ }
76
70
 
71
+ // decode a struct, set or enum
72
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
77
73
  function _decodeStruct(value, type, _, count) {
78
74
  const parsed = JSON.parse(type);
79
75
  const keys = Object.keys(parsed);
80
-
81
76
  if (keys.includes('_enum')) {
82
77
  return _decodeEnum(value, parsed._enum, count, parsed._fallback);
83
78
  } else if (keys.includes('_set')) {
84
79
  return _decodeSet(value, parsed._set, parsed._fallback);
85
80
  }
86
-
87
81
  value.alias = parsed._alias ? new Map(Object.entries(parsed._alias)) : undefined;
88
82
  value.fallbackType = parsed._fallback;
89
83
  value.sub = keys.filter(name => !KNOWN_INTERNALS.includes(name)).map(name => getTypeDef(getTypeString(parsed[name]), {
90
84
  name
91
85
  }, count));
92
86
  return value;
93
- } // decode a fixed vector, e.g. [u8;32]
94
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
95
-
87
+ }
96
88
 
89
+ // decode a fixed vector, e.g. [u8;32]
90
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
97
91
  function _decodeFixedVec(value, type, _, count) {
98
92
  const max = type.length - 1;
99
93
  let index = -1;
100
94
  let inner = 0;
101
-
102
95
  for (let i = 1; i < max && index === -1; i++) {
103
96
  switch (type[i]) {
104
97
  case ';':
@@ -106,16 +99,13 @@ function _decodeFixedVec(value, type, _, count) {
106
99
  if (inner === 0) {
107
100
  index = i;
108
101
  }
109
-
110
102
  break;
111
103
  }
112
-
113
104
  case '[':
114
105
  case '(':
115
106
  case '<':
116
107
  inner++;
117
108
  break;
118
-
119
109
  case ']':
120
110
  case ')':
121
111
  case '>':
@@ -123,73 +113,62 @@ function _decodeFixedVec(value, type, _, count) {
123
113
  break;
124
114
  }
125
115
  }
126
-
127
116
  if (index === -1) {
128
117
  throw new Error(`${type}: Unable to extract location of ';'`);
129
118
  }
130
-
131
119
  const vecType = type.substring(1, index);
132
120
  const [strLength, displayName] = type.substring(index + 1, max).split(';');
133
121
  const length = parseInt(strLength.trim(), 10);
134
-
135
122
  if (length > 2048) {
136
123
  throw new Error(`${type}: Only support for [Type; <length>], where length <= 2048`);
137
124
  }
138
-
139
125
  value.displayName = displayName;
140
126
  value.length = length;
141
127
  value.sub = getTypeDef(vecType, {}, count);
142
128
  return value;
143
- } // decode a tuple
144
-
129
+ }
145
130
 
131
+ // decode a tuple
146
132
  function _decodeTuple(value, _, subType, count) {
147
133
  value.sub = subType.length === 0 ? [] : typeSplit(subType).map(inner => getTypeDef(inner, {}, count));
148
134
  return value;
149
- } // decode a Int/UInt<bitLength[, name]>
150
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
151
-
135
+ }
152
136
 
137
+ // decode a Int/UInt<bitLength[, name]>
138
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
153
139
  function _decodeAnyInt(value, type, _, clazz) {
154
140
  const [strLength, displayName] = type.substring(clazz.length + 1, type.length - 1).split(',');
155
141
  const length = parseInt(strLength.trim(), 10);
156
-
157
142
  if (length > 8192 || length % 8) {
158
143
  throw new Error(`${type}: Only support for ${clazz}<bitLength>, where length <= 8192 and a power of 8, found ${length}`);
159
144
  }
160
-
161
145
  value.displayName = displayName;
162
146
  value.length = length;
163
147
  return value;
164
148
  }
165
-
166
149
  function _decodeInt(value, type, subType) {
167
150
  return _decodeAnyInt(value, type, subType, 'Int');
168
151
  }
169
-
170
152
  function _decodeUInt(value, type, subType) {
171
153
  return _decodeAnyInt(value, type, subType, 'UInt');
172
- } // eslint-disable-next-line @typescript-eslint/no-unused-vars
173
-
154
+ }
174
155
 
156
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
175
157
  function _decodeDoNotConstruct(value, type, _) {
176
158
  const NAME_LENGTH = 'DoNotConstruct'.length;
177
159
  value.displayName = type.substring(NAME_LENGTH + 1, type.length - 1);
178
160
  return value;
179
161
  }
180
-
181
162
  function hasWrapper(type, [start, end]) {
182
163
  return type.substring(0, start.length) === start && type.slice(-1 * end.length) === end;
183
164
  }
184
-
185
- const nestedExtraction = [['[', ']', TypeDefInfo.VecFixed, _decodeFixedVec], ['{', '}', TypeDefInfo.Struct, _decodeStruct], ['(', ')', TypeDefInfo.Tuple, _decodeTuple], // the inner for these are the same as tuple, multiple values
165
+ const nestedExtraction = [['[', ']', TypeDefInfo.VecFixed, _decodeFixedVec], ['{', '}', TypeDefInfo.Struct, _decodeStruct], ['(', ')', TypeDefInfo.Tuple, _decodeTuple],
166
+ // the inner for these are the same as tuple, multiple values
186
167
  ['BTreeMap<', '>', TypeDefInfo.BTreeMap, _decodeTuple], ['HashMap<', '>', TypeDefInfo.HashMap, _decodeTuple], ['Int<', '>', TypeDefInfo.Int, _decodeInt], ['Result<', '>', TypeDefInfo.Result, _decodeTuple], ['UInt<', '>', TypeDefInfo.UInt, _decodeUInt], ['DoNotConstruct<', '>', TypeDefInfo.DoNotConstruct, _decodeDoNotConstruct]];
187
168
  const wrappedExtraction = [['BTreeSet<', '>', TypeDefInfo.BTreeSet], ['Compact<', '>', TypeDefInfo.Compact], ['Linkage<', '>', TypeDefInfo.Linkage], ['Opaque<', '>', TypeDefInfo.WrapperOpaque], ['Option<', '>', TypeDefInfo.Option], ['Range<', '>', TypeDefInfo.Range], ['RangeInclusive<', '>', TypeDefInfo.RangeInclusive], ['Vec<', '>', TypeDefInfo.Vec], ['WrapperKeepOpaque<', '>', TypeDefInfo.WrapperKeepOpaque], ['WrapperOpaque<', '>', TypeDefInfo.WrapperOpaque]];
188
-
189
169
  function extractSubType(type, [start, end]) {
190
170
  return type.substring(start.length, type.length - end.length);
191
171
  }
192
-
193
172
  export function getTypeDef(_type, {
194
173
  displayName,
195
174
  name
@@ -202,24 +181,18 @@ export function getTypeDef(_type, {
202
181
  name,
203
182
  type
204
183
  };
205
-
206
184
  if (++count > 64) {
207
185
  throw new Error('getTypeDef: Maximum nested limit reached');
208
186
  }
209
-
210
187
  const nested = nestedExtraction.find(nested => hasWrapper(type, nested));
211
-
212
188
  if (nested) {
213
189
  value.info = nested[2];
214
190
  return nested[3](value, type, extractSubType(type, nested), count);
215
191
  }
216
-
217
192
  const wrapped = wrappedExtraction.find(wrapped => hasWrapper(type, wrapped));
218
-
219
193
  if (wrapped) {
220
194
  value.info = wrapped[2];
221
195
  value.sub = getTypeDef(extractSubType(type, wrapped), {}, count);
222
196
  }
223
-
224
197
  return value;
225
198
  }
package/util/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  // Copyright 2017-2022 @polkadot/types-create authors & contributors
2
2
  // SPDX-License-Identifier: Apache-2.0
3
+
3
4
  export * from "./encodeTypes.js";
4
5
  export * from "./getTypeDef.js";
5
6
  export * from "./typeSplit.js";
package/util/typeSplit.js CHANGED
@@ -1,16 +1,18 @@
1
1
  // Copyright 2017-2022 @polkadot/types-create authors & contributors
2
2
  // SPDX-License-Identifier: Apache-2.0
3
+
3
4
  // safely split a string on ', ' while taking care of any nested occurences
4
5
  export function typeSplit(type) {
5
- const result = []; // these are the depths of the various tokens: <, [, {, (
6
+ const result = [];
6
7
 
8
+ // these are the depths of the various tokens: <, [, {, (
7
9
  let c = 0;
8
10
  let f = 0;
9
11
  let s = 0;
10
- let t = 0; // current start position
12
+ let t = 0;
11
13
 
14
+ // current start position
12
15
  let start = 0;
13
-
14
16
  for (let i = 0; i < type.length; i++) {
15
17
  switch (type[i]) {
16
18
  // if we are not nested, add the type
@@ -20,54 +22,49 @@ export function typeSplit(type) {
20
22
  result.push(type.substring(start, i).trim());
21
23
  start = i + 1;
22
24
  }
23
-
24
25
  break;
25
26
  }
26
- // adjust compact/vec (and friends) depth
27
27
 
28
+ // adjust compact/vec (and friends) depth
28
29
  case '<':
29
30
  c++;
30
31
  break;
31
-
32
32
  case '>':
33
33
  c--;
34
34
  break;
35
- // adjust fixed vec depths
36
35
 
36
+ // adjust fixed vec depths
37
37
  case '[':
38
38
  f++;
39
39
  break;
40
-
41
40
  case ']':
42
41
  f--;
43
42
  break;
44
- // adjust struct depth
45
43
 
44
+ // adjust struct depth
46
45
  case '{':
47
46
  s++;
48
47
  break;
49
-
50
48
  case '}':
51
49
  s--;
52
50
  break;
53
- // adjust tuple depth
54
51
 
52
+ // adjust tuple depth
55
53
  case '(':
56
54
  t++;
57
55
  break;
58
-
59
56
  case ')':
60
57
  t--;
61
58
  break;
62
59
  }
63
- } // ensure we have all the terminators taken care of
64
-
60
+ }
65
61
 
62
+ // ensure we have all the terminators taken care of
66
63
  if (c || f || s || t) {
67
64
  throw new Error(`Invalid definition (missing terminators) found in ${type}`);
68
- } // the final leg of the journey
69
-
65
+ }
70
66
 
67
+ // the final leg of the journey
71
68
  result.push(type.substring(start, type.length).trim());
72
69
  return result;
73
70
  }
package/util/xcm.js CHANGED
@@ -1,5 +1,6 @@
1
1
  // Copyright 2017-2022 @polkadot/types-create authors & contributors
2
2
  // SPDX-License-Identifier: Apache-2.0
3
+
3
4
  import { objectSpread } from '@polkadot/util';
4
5
  export const XCM_MAPPINGS = ['AssetInstance', 'Fungibility', 'Junction', 'Junctions', 'MultiAsset', 'MultiAssetFilter', 'MultiLocation', 'Response', 'WildFungibility', 'WildMultiAsset', 'Xcm', 'XcmError', 'XcmOrder'];
5
6
  export function mapXcmTypes(version) {