@polkadot/types-create 9.14.2 → 10.0.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.
@@ -1,168 +1,124 @@
1
- // Copyright 2017-2023 @polkadot/types-create authors & contributors
2
- // SPDX-License-Identifier: Apache-2.0
3
-
4
1
  import { isNumber, isUndefined, objectSpread, stringify } from '@polkadot/util';
5
- import { TypeDefInfo } from "../types/index.js";
6
- const stringIdentity = value => value.toString();
2
+ import { TypeDefInfo } from '../types/index.js';
3
+ const stringIdentity = (value) => value.toString();
7
4
  const INFO_WRAP = ['BTreeMap', 'BTreeSet', 'Compact', 'HashMap', 'Option', 'Result', 'Vec'];
8
5
  export function paramsNotation(outer, inner, transform = stringIdentity) {
9
- return `${outer}${inner ? `<${(Array.isArray(inner) ? inner : [inner]).map(transform).join(', ')}>` : ''}`;
6
+ return `${outer}${inner
7
+ ? `<${(Array.isArray(inner) ? inner : [inner]).map(transform).join(', ')}>`
8
+ : ''}`;
10
9
  }
11
10
  function encodeWithParams(registry, typeDef, outer) {
12
- const {
13
- info,
14
- sub
15
- } = typeDef;
16
- switch (info) {
17
- case TypeDefInfo.BTreeMap:
18
- case TypeDefInfo.BTreeSet:
19
- case TypeDefInfo.Compact:
20
- case TypeDefInfo.HashMap:
21
- case TypeDefInfo.Linkage:
22
- case TypeDefInfo.Option:
23
- case TypeDefInfo.Range:
24
- case TypeDefInfo.RangeInclusive:
25
- case TypeDefInfo.Result:
26
- case TypeDefInfo.Vec:
27
- case TypeDefInfo.WrapperKeepOpaque:
28
- case TypeDefInfo.WrapperOpaque:
29
- return paramsNotation(outer, sub, p => encodeTypeDef(registry, p));
30
- }
31
- throw new Error(`Unable to encode ${stringify(typeDef)} with params`);
11
+ const { info, sub } = typeDef;
12
+ switch (info) {
13
+ case TypeDefInfo.BTreeMap:
14
+ case TypeDefInfo.BTreeSet:
15
+ case TypeDefInfo.Compact:
16
+ case TypeDefInfo.HashMap:
17
+ case TypeDefInfo.Linkage:
18
+ case TypeDefInfo.Option:
19
+ case TypeDefInfo.Range:
20
+ case TypeDefInfo.RangeInclusive:
21
+ case TypeDefInfo.Result:
22
+ case TypeDefInfo.Vec:
23
+ case TypeDefInfo.WrapperKeepOpaque:
24
+ case TypeDefInfo.WrapperOpaque:
25
+ return paramsNotation(outer, sub, (p) => encodeTypeDef(registry, p));
26
+ }
27
+ throw new Error(`Unable to encode ${stringify(typeDef)} with params`);
32
28
  }
33
29
  function encodeSubTypes(registry, sub, asEnum, extra) {
34
- const names = sub.map(({
35
- name
36
- }) => name);
37
- if (!names.every(n => !!n)) {
38
- throw new Error(`Subtypes does not have consistent names, ${names.join(', ')}`);
39
- }
40
- const inner = objectSpread({}, extra);
41
- for (let i = 0; i < sub.length; i++) {
42
- const def = sub[i];
43
- inner[def.name] = encodeTypeDef(registry, def);
44
- }
45
- return stringify(asEnum ? {
46
- _enum: inner
47
- } : inner);
48
- }
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)
52
- const encoders = {
53
- [TypeDefInfo.BTreeMap]: (registry, typeDef) => encodeWithParams(registry, typeDef, 'BTreeMap'),
54
- [TypeDefInfo.BTreeSet]: (registry, typeDef) => encodeWithParams(registry, typeDef, 'BTreeSet'),
55
- [TypeDefInfo.Compact]: (registry, typeDef) => encodeWithParams(registry, typeDef, 'Compact'),
56
- [TypeDefInfo.DoNotConstruct]: (registry, {
57
- displayName,
58
- lookupIndex,
59
- lookupName
60
- }) => `DoNotConstruct<${lookupName || displayName || (isUndefined(lookupIndex) ? 'Unknown' : registry.createLookupType(lookupIndex))}>`,
61
- [TypeDefInfo.Enum]: (registry, {
62
- sub
63
- }) => {
64
- if (!Array.isArray(sub)) {
65
- throw new Error('Unable to encode Enum type');
66
- }
67
-
68
- // c-like enums have all Null entries
69
- // TODO We need to take the disciminant into account and auto-add empty entries
70
- return sub.every(({
71
- type
72
- }) => type === 'Null') ? stringify({
73
- _enum: sub.map(({
74
- name
75
- }, index) => `${name || `Empty${index}`}`)
76
- }) : encodeSubTypes(registry, sub, true);
77
- },
78
- [TypeDefInfo.HashMap]: (registry, typeDef) => encodeWithParams(registry, typeDef, 'HashMap'),
79
- [TypeDefInfo.Int]: (registry, {
80
- length = 32
81
- }) => `Int<${length}>`,
82
- [TypeDefInfo.Linkage]: (registry, typeDef) => encodeWithParams(registry, typeDef, 'Linkage'),
83
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
84
- [TypeDefInfo.Null]: (registry, typeDef) => 'Null',
85
- [TypeDefInfo.Option]: (registry, typeDef) => encodeWithParams(registry, typeDef, 'Option'),
86
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
87
- [TypeDefInfo.Plain]: (registry, {
88
- displayName,
89
- type
90
- }) => displayName || type,
91
- [TypeDefInfo.Range]: (registry, typeDef) => encodeWithParams(registry, typeDef, 'Range'),
92
- [TypeDefInfo.RangeInclusive]: (registry, typeDef) => encodeWithParams(registry, typeDef, 'RangeInclusive'),
93
- [TypeDefInfo.Result]: (registry, typeDef) => encodeWithParams(registry, typeDef, 'Result'),
94
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
95
- [TypeDefInfo.Set]: (registry, {
96
- length = 8,
97
- sub
98
- }) => {
99
- if (!Array.isArray(sub)) {
100
- throw new Error('Unable to encode Set type');
101
- }
102
- return stringify({
103
- _set: sub.reduce((all, {
104
- index,
105
- name
106
- }, count) => objectSpread(all, {
107
- [`${name || `Unknown${index || count}`}`]: index || count
108
- }), {
109
- _bitLength: length || 8
110
- })
111
- });
112
- },
113
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
114
- [TypeDefInfo.Si]: (registry, {
115
- lookupName,
116
- type
117
- }) => lookupName || type,
118
- [TypeDefInfo.Struct]: (registry, {
119
- alias,
120
- sub
121
- }) => {
122
- if (!Array.isArray(sub)) {
123
- throw new Error('Unable to encode Struct type');
124
- }
125
- return encodeSubTypes(registry, sub, false, alias ? {
126
- _alias: [...alias.entries()].reduce((all, [k, v]) => objectSpread(all, {
127
- [k]: v
128
- }), {})
129
- } : {});
130
- },
131
- [TypeDefInfo.Tuple]: (registry, {
132
- sub
133
- }) => {
134
- if (!Array.isArray(sub)) {
135
- throw new Error('Unable to encode Tuple type');
30
+ const names = sub.map(({ name }) => name);
31
+ if (!names.every((n) => !!n)) {
32
+ throw new Error(`Subtypes does not have consistent names, ${names.join(', ')}`);
136
33
  }
137
- return `(${sub.map(type => encodeTypeDef(registry, type)).join(',')})`;
138
- },
139
- [TypeDefInfo.UInt]: (registry, {
140
- length = 32
141
- }) => `UInt<${length}>`,
142
- [TypeDefInfo.Vec]: (registry, typeDef) => encodeWithParams(registry, typeDef, 'Vec'),
143
- [TypeDefInfo.VecFixed]: (registry, {
144
- length,
145
- sub
146
- }) => {
147
- if (!isNumber(length) || !sub || Array.isArray(sub)) {
148
- throw new Error('Unable to encode VecFixed type');
34
+ const inner = objectSpread({}, extra);
35
+ for (let i = 0; i < sub.length; i++) {
36
+ const def = sub[i];
37
+ inner[def.name] = encodeTypeDef(registry, def);
149
38
  }
150
- return `[${sub.type};${length}]`;
151
- },
152
- [TypeDefInfo.WrapperKeepOpaque]: (registry, typeDef) => encodeWithParams(registry, typeDef, 'WrapperKeepOpaque'),
153
- [TypeDefInfo.WrapperOpaque]: (registry, typeDef) => encodeWithParams(registry, typeDef, 'WrapperOpaque')
39
+ return stringify(asEnum
40
+ ? { _enum: inner }
41
+ : inner);
42
+ }
43
+ const encoders = {
44
+ [TypeDefInfo.BTreeMap]: (registry, typeDef) => encodeWithParams(registry, typeDef, 'BTreeMap'),
45
+ [TypeDefInfo.BTreeSet]: (registry, typeDef) => encodeWithParams(registry, typeDef, 'BTreeSet'),
46
+ [TypeDefInfo.Compact]: (registry, typeDef) => encodeWithParams(registry, typeDef, 'Compact'),
47
+ [TypeDefInfo.DoNotConstruct]: (registry, { displayName, lookupIndex, lookupName }) => `DoNotConstruct<${lookupName || displayName || (isUndefined(lookupIndex) ? 'Unknown' : registry.createLookupType(lookupIndex))}>`,
48
+ [TypeDefInfo.Enum]: (registry, { sub }) => {
49
+ if (!Array.isArray(sub)) {
50
+ throw new Error('Unable to encode Enum type');
51
+ }
52
+ // c-like enums have all Null entries
53
+ // TODO We need to take the disciminant into account and auto-add empty entries
54
+ return sub.every(({ type }) => type === 'Null')
55
+ ? stringify({ _enum: sub.map(({ name }, index) => `${name || `Empty${index}`}`) })
56
+ : encodeSubTypes(registry, sub, true);
57
+ },
58
+ [TypeDefInfo.HashMap]: (registry, typeDef) => encodeWithParams(registry, typeDef, 'HashMap'),
59
+ [TypeDefInfo.Int]: (registry, { length = 32 }) => `Int<${length}>`,
60
+ [TypeDefInfo.Linkage]: (registry, typeDef) => encodeWithParams(registry, typeDef, 'Linkage'),
61
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
62
+ [TypeDefInfo.Null]: (registry, typeDef) => 'Null',
63
+ [TypeDefInfo.Option]: (registry, typeDef) => encodeWithParams(registry, typeDef, 'Option'),
64
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
65
+ [TypeDefInfo.Plain]: (registry, { displayName, type }) => displayName || type,
66
+ [TypeDefInfo.Range]: (registry, typeDef) => encodeWithParams(registry, typeDef, 'Range'),
67
+ [TypeDefInfo.RangeInclusive]: (registry, typeDef) => encodeWithParams(registry, typeDef, 'RangeInclusive'),
68
+ [TypeDefInfo.Result]: (registry, typeDef) => encodeWithParams(registry, typeDef, 'Result'),
69
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
70
+ [TypeDefInfo.Set]: (registry, { length = 8, sub }) => {
71
+ if (!Array.isArray(sub)) {
72
+ throw new Error('Unable to encode Set type');
73
+ }
74
+ return stringify({
75
+ _set: sub.reduce((all, { index, name }, count) => objectSpread(all, { [`${name || `Unknown${index || count}`}`]: index || count }), { _bitLength: length || 8 })
76
+ });
77
+ },
78
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
79
+ [TypeDefInfo.Si]: (registry, { lookupName, type }) => lookupName || type,
80
+ [TypeDefInfo.Struct]: (registry, { alias, sub }) => {
81
+ if (!Array.isArray(sub)) {
82
+ throw new Error('Unable to encode Struct type');
83
+ }
84
+ return encodeSubTypes(registry, sub, false, alias
85
+ ? {
86
+ _alias: [...alias.entries()].reduce((all, [k, v]) => objectSpread(all, { [k]: v }), {})
87
+ }
88
+ : {});
89
+ },
90
+ [TypeDefInfo.Tuple]: (registry, { sub }) => {
91
+ if (!Array.isArray(sub)) {
92
+ throw new Error('Unable to encode Tuple type');
93
+ }
94
+ return `(${sub.map((type) => encodeTypeDef(registry, type)).join(',')})`;
95
+ },
96
+ [TypeDefInfo.UInt]: (registry, { length = 32 }) => `UInt<${length}>`,
97
+ [TypeDefInfo.Vec]: (registry, typeDef) => encodeWithParams(registry, typeDef, 'Vec'),
98
+ [TypeDefInfo.VecFixed]: (registry, { length, sub }) => {
99
+ if (!isNumber(length) || !sub || Array.isArray(sub)) {
100
+ throw new Error('Unable to encode VecFixed type');
101
+ }
102
+ return `[${sub.type};${length}]`;
103
+ },
104
+ [TypeDefInfo.WrapperKeepOpaque]: (registry, typeDef) => encodeWithParams(registry, typeDef, 'WrapperKeepOpaque'),
105
+ [TypeDefInfo.WrapperOpaque]: (registry, typeDef) => encodeWithParams(registry, typeDef, 'WrapperOpaque')
154
106
  };
155
107
  function encodeType(registry, typeDef, withLookup = true) {
156
- return withLookup && typeDef.lookupName ? typeDef.lookupName : encoders[typeDef.info](registry, typeDef);
108
+ return withLookup && typeDef.lookupName
109
+ ? typeDef.lookupName
110
+ : encoders[typeDef.info](registry, typeDef);
157
111
  }
158
112
  export function encodeTypeDef(registry, typeDef) {
159
- // In the case of contracts we do have the unfortunate situation where the displayName would
160
- // refer to "Option" when it is an option. For these, string it out, only using when actually
161
- // not a top-level element to be used
162
- return typeDef.displayName && !INFO_WRAP.some(i => typeDef.displayName === i) ? typeDef.displayName : encodeType(registry, typeDef);
113
+ // In the case of contracts we do have the unfortunate situation where the displayName would
114
+ // refer to "Option" when it is an option. For these, string it out, only using when actually
115
+ // not a top-level element to be used
116
+ return (typeDef.displayName && !INFO_WRAP.some((i) => typeDef.displayName === i))
117
+ ? typeDef.displayName
118
+ : encodeType(registry, typeDef);
163
119
  }
164
120
  export function withTypeString(registry, typeDef) {
165
- return objectSpread({}, typeDef, {
166
- type: encodeType(registry, typeDef, false)
167
- });
121
+ return objectSpread({}, typeDef, {
122
+ type: encodeType(registry, typeDef, false)
123
+ });
168
124
  }
@@ -1,198 +1,192 @@
1
- // Copyright 2017-2023 @polkadot/types-create authors & contributors
2
- // SPDX-License-Identifier: Apache-2.0
3
-
4
1
  import { sanitize } from '@polkadot/types-codec';
5
2
  import { isNumber, isString, objectSpread } from '@polkadot/util';
6
- import { TypeDefInfo } from "../types/index.js";
7
- import { typeSplit } from "./typeSplit.js";
3
+ import { TypeDefInfo } from '../types/index.js';
4
+ import { typeSplit } from './typeSplit.js';
8
5
  const KNOWN_INTERNALS = ['_alias', '_fallback'];
9
6
  function getTypeString(typeOrObj) {
10
- return isString(typeOrObj) ? typeOrObj.toString() : JSON.stringify(typeOrObj);
7
+ return isString(typeOrObj)
8
+ ? typeOrObj.toString()
9
+ : JSON.stringify(typeOrObj);
11
10
  }
12
11
  function isRustEnum(details) {
13
- const values = Object.values(details);
14
- if (values.some(v => isNumber(v))) {
15
- if (!values.every(v => isNumber(v) && v >= 0 && v <= 255)) {
16
- throw new Error('Invalid number-indexed enum definition');
12
+ const values = Object.values(details);
13
+ if (values.some((v) => isNumber(v))) {
14
+ if (!values.every((v) => isNumber(v) && v >= 0 && v <= 255)) {
15
+ throw new Error('Invalid number-indexed enum definition');
16
+ }
17
+ return false;
17
18
  }
18
- return false;
19
- }
20
- return true;
19
+ return true;
21
20
  }
22
-
23
- // decode an enum of either of the following forms
24
- // { _enum: ['A', 'B', 'C'] }
25
- // { _enum: { A: AccountId, B: Balance, C: u32 } }
26
- // { _enum: { A: 1, B: 2 } }
27
21
  function _decodeEnum(value, details, count, fallbackType) {
28
- value.info = TypeDefInfo.Enum;
29
- value.fallbackType = fallbackType;
30
-
31
- // not as pretty, but remain compatible with oo7 for both struct and Array types
32
- if (Array.isArray(details)) {
33
- value.sub = details.map((name, index) => ({
34
- index,
35
- info: TypeDefInfo.Plain,
36
- name,
37
- type: 'Null'
38
- }));
39
- } else if (isRustEnum(details)) {
40
- value.sub = Object.entries(details).map(([name, typeOrObj], index) => objectSpread({}, getTypeDef(getTypeString(typeOrObj || 'Null'), {
41
- name
42
- }, count), {
43
- index
44
- }));
45
- } else {
46
- value.sub = Object.entries(details).map(([name, index]) => ({
47
- index,
48
- info: TypeDefInfo.Plain,
49
- name,
50
- type: 'Null'
51
- }));
52
- }
53
- return value;
22
+ value.info = TypeDefInfo.Enum;
23
+ value.fallbackType = fallbackType;
24
+ // not as pretty, but remain compatible with oo7 for both struct and Array types
25
+ if (Array.isArray(details)) {
26
+ value.sub = details.map((name, index) => ({
27
+ index,
28
+ info: TypeDefInfo.Plain,
29
+ name,
30
+ type: 'Null'
31
+ }));
32
+ }
33
+ else if (isRustEnum(details)) {
34
+ value.sub = Object.entries(details).map(([name, typeOrObj], index) => objectSpread({}, getTypeDef(getTypeString(typeOrObj || 'Null'), { name }, count), { index }));
35
+ }
36
+ else {
37
+ value.sub = Object.entries(details).map(([name, index]) => ({
38
+ index,
39
+ info: TypeDefInfo.Plain,
40
+ name,
41
+ type: 'Null'
42
+ }));
43
+ }
44
+ return value;
54
45
  }
55
-
56
- // decode a set of the form
57
- // { _set: { A: 0b0001, B: 0b0010, C: 0b0100 } }
58
46
  function _decodeSet(value, details, fallbackType) {
59
- value.info = TypeDefInfo.Set;
60
- value.fallbackType = fallbackType;
61
- value.length = details._bitLength;
62
- value.sub = Object.entries(details).filter(([name]) => !name.startsWith('_')).map(([name, index]) => ({
63
- index,
64
- info: TypeDefInfo.Plain,
65
- name,
66
- type: 'Null'
67
- }));
68
- return value;
47
+ value.info = TypeDefInfo.Set;
48
+ value.fallbackType = fallbackType;
49
+ value.length = details._bitLength;
50
+ value.sub = Object
51
+ .entries(details)
52
+ .filter(([name]) => !name.startsWith('_'))
53
+ .map(([name, index]) => ({
54
+ index,
55
+ info: TypeDefInfo.Plain,
56
+ name,
57
+ type: 'Null'
58
+ }));
59
+ return value;
69
60
  }
70
-
71
- // decode a struct, set or enum
72
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
73
61
  function _decodeStruct(value, type, _, count) {
74
- const parsed = JSON.parse(type);
75
- const keys = Object.keys(parsed);
76
- if (keys.includes('_enum')) {
77
- return _decodeEnum(value, parsed._enum, count, parsed._fallback);
78
- } else if (keys.includes('_set')) {
79
- return _decodeSet(value, parsed._set, parsed._fallback);
80
- }
81
- value.alias = parsed._alias ? new Map(Object.entries(parsed._alias)) : undefined;
82
- value.fallbackType = parsed._fallback;
83
- value.sub = keys.filter(name => !KNOWN_INTERNALS.includes(name)).map(name => getTypeDef(getTypeString(parsed[name]), {
84
- name
85
- }, count));
86
- return value;
62
+ const parsed = JSON.parse(type);
63
+ const keys = Object.keys(parsed);
64
+ if (keys.includes('_enum')) {
65
+ return _decodeEnum(value, parsed._enum, count, parsed._fallback);
66
+ }
67
+ else if (keys.includes('_set')) {
68
+ return _decodeSet(value, parsed._set, parsed._fallback);
69
+ }
70
+ value.alias = parsed._alias
71
+ ? new Map(Object.entries(parsed._alias))
72
+ : undefined;
73
+ value.fallbackType = parsed._fallback;
74
+ value.sub = keys
75
+ .filter((name) => !KNOWN_INTERNALS.includes(name))
76
+ .map((name) => getTypeDef(getTypeString(parsed[name]), { name }, count));
77
+ return value;
87
78
  }
88
-
89
- // decode a fixed vector, e.g. [u8;32]
90
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
91
79
  function _decodeFixedVec(value, type, _, count) {
92
- const max = type.length - 1;
93
- let index = -1;
94
- let inner = 0;
95
- for (let i = 1; i < max && index === -1; i++) {
96
- switch (type[i]) {
97
- case ';':
98
- {
99
- if (inner === 0) {
100
- index = i;
101
- }
102
- break;
80
+ const max = type.length - 1;
81
+ let index = -1;
82
+ let inner = 0;
83
+ for (let i = 1; (i < max) && (index === -1); i++) {
84
+ switch (type[i]) {
85
+ case ';': {
86
+ if (inner === 0) {
87
+ index = i;
88
+ }
89
+ break;
90
+ }
91
+ case '[':
92
+ case '(':
93
+ case '<':
94
+ inner++;
95
+ break;
96
+ case ']':
97
+ case ')':
98
+ case '>':
99
+ inner--;
100
+ break;
103
101
  }
104
- case '[':
105
- case '(':
106
- case '<':
107
- inner++;
108
- break;
109
- case ']':
110
- case ')':
111
- case '>':
112
- inner--;
113
- break;
114
102
  }
115
- }
116
- if (index === -1) {
117
- throw new Error(`${type}: Unable to extract location of ';'`);
118
- }
119
- const vecType = type.substring(1, index);
120
- const [strLength, displayName] = type.substring(index + 1, max).split(';');
121
- const length = parseInt(strLength.trim(), 10);
122
- if (length > 2048) {
123
- throw new Error(`${type}: Only support for [Type; <length>], where length <= 2048`);
124
- }
125
- value.displayName = displayName;
126
- value.length = length;
127
- value.sub = getTypeDef(vecType, {}, count);
128
- return value;
103
+ if (index === -1) {
104
+ throw new Error(`${type}: Unable to extract location of ';'`);
105
+ }
106
+ const vecType = type.substring(1, index);
107
+ const [strLength, displayName] = type.substring(index + 1, max).split(';');
108
+ const length = parseInt(strLength.trim(), 10);
109
+ if (length > 2048) {
110
+ throw new Error(`${type}: Only support for [Type; <length>], where length <= 2048`);
111
+ }
112
+ value.displayName = displayName;
113
+ value.length = length;
114
+ value.sub = getTypeDef(vecType, {}, count);
115
+ return value;
129
116
  }
130
-
131
- // decode a tuple
132
117
  function _decodeTuple(value, _, subType, count) {
133
- value.sub = subType.length === 0 ? [] : typeSplit(subType).map(inner => getTypeDef(inner, {}, count));
134
- return value;
118
+ value.sub = subType.length === 0
119
+ ? []
120
+ : typeSplit(subType).map((inner) => getTypeDef(inner, {}, count));
121
+ return value;
135
122
  }
136
-
137
- // decode a Int/UInt<bitLength[, name]>
138
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
139
123
  function _decodeAnyInt(value, type, _, clazz) {
140
- const [strLength, displayName] = type.substring(clazz.length + 1, type.length - 1).split(',');
141
- const length = parseInt(strLength.trim(), 10);
142
- if (length > 8192 || length % 8) {
143
- throw new Error(`${type}: Only support for ${clazz}<bitLength>, where length <= 8192 and a power of 8, found ${length}`);
144
- }
145
- value.displayName = displayName;
146
- value.length = length;
147
- return value;
124
+ const [strLength, displayName] = type.substring(clazz.length + 1, type.length - 1).split(',');
125
+ const length = parseInt(strLength.trim(), 10);
126
+ if ((length > 8192) || (length % 8)) {
127
+ throw new Error(`${type}: Only support for ${clazz}<bitLength>, where length <= 8192 and a power of 8, found ${length}`);
128
+ }
129
+ value.displayName = displayName;
130
+ value.length = length;
131
+ return value;
148
132
  }
149
133
  function _decodeInt(value, type, subType) {
150
- return _decodeAnyInt(value, type, subType, 'Int');
134
+ return _decodeAnyInt(value, type, subType, 'Int');
151
135
  }
152
136
  function _decodeUInt(value, type, subType) {
153
- return _decodeAnyInt(value, type, subType, 'UInt');
137
+ return _decodeAnyInt(value, type, subType, 'UInt');
154
138
  }
155
-
156
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
157
139
  function _decodeDoNotConstruct(value, type, _) {
158
- const NAME_LENGTH = 'DoNotConstruct'.length;
159
- value.displayName = type.substring(NAME_LENGTH + 1, type.length - 1);
160
- return value;
140
+ const NAME_LENGTH = 'DoNotConstruct'.length;
141
+ value.displayName = type.substring(NAME_LENGTH + 1, type.length - 1);
142
+ return value;
161
143
  }
162
144
  function hasWrapper(type, [start, end]) {
163
- return type.substring(0, start.length) === start && type.slice(-1 * end.length) === end;
145
+ return (type.substring(0, start.length) === start) && (type.slice(-1 * end.length) === end);
164
146
  }
165
- const nestedExtraction = [['[', ']', TypeDefInfo.VecFixed, _decodeFixedVec], ['{', '}', TypeDefInfo.Struct, _decodeStruct], ['(', ')', TypeDefInfo.Tuple, _decodeTuple],
166
- // the inner for these are the same as tuple, multiple values
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]];
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]];
147
+ const nestedExtraction = [
148
+ ['[', ']', TypeDefInfo.VecFixed, _decodeFixedVec],
149
+ ['{', '}', TypeDefInfo.Struct, _decodeStruct],
150
+ ['(', ')', TypeDefInfo.Tuple, _decodeTuple],
151
+ // the inner for these are the same as tuple, multiple values
152
+ ['BTreeMap<', '>', TypeDefInfo.BTreeMap, _decodeTuple],
153
+ ['HashMap<', '>', TypeDefInfo.HashMap, _decodeTuple],
154
+ ['Int<', '>', TypeDefInfo.Int, _decodeInt],
155
+ ['Result<', '>', TypeDefInfo.Result, _decodeTuple],
156
+ ['UInt<', '>', TypeDefInfo.UInt, _decodeUInt],
157
+ ['DoNotConstruct<', '>', TypeDefInfo.DoNotConstruct, _decodeDoNotConstruct]
158
+ ];
159
+ const wrappedExtraction = [
160
+ ['BTreeSet<', '>', TypeDefInfo.BTreeSet],
161
+ ['Compact<', '>', TypeDefInfo.Compact],
162
+ ['Linkage<', '>', TypeDefInfo.Linkage],
163
+ ['Opaque<', '>', TypeDefInfo.WrapperOpaque],
164
+ ['Option<', '>', TypeDefInfo.Option],
165
+ ['Range<', '>', TypeDefInfo.Range],
166
+ ['RangeInclusive<', '>', TypeDefInfo.RangeInclusive],
167
+ ['Vec<', '>', TypeDefInfo.Vec],
168
+ ['WrapperKeepOpaque<', '>', TypeDefInfo.WrapperKeepOpaque],
169
+ ['WrapperOpaque<', '>', TypeDefInfo.WrapperOpaque]
170
+ ];
169
171
  function extractSubType(type, [start, end]) {
170
- return type.substring(start.length, type.length - end.length);
172
+ return type.substring(start.length, type.length - end.length);
171
173
  }
172
- export function getTypeDef(_type, {
173
- displayName,
174
- name
175
- } = {}, count = 0) {
176
- // create the type via Type, allowing types to be sanitized
177
- const type = sanitize(_type);
178
- const value = {
179
- displayName,
180
- info: TypeDefInfo.Plain,
181
- name,
182
- type
183
- };
184
- if (++count > 64) {
185
- throw new Error('getTypeDef: Maximum nested limit reached');
186
- }
187
- const nested = nestedExtraction.find(nested => hasWrapper(type, nested));
188
- if (nested) {
189
- value.info = nested[2];
190
- return nested[3](value, type, extractSubType(type, nested), count);
191
- }
192
- const wrapped = wrappedExtraction.find(wrapped => hasWrapper(type, wrapped));
193
- if (wrapped) {
194
- value.info = wrapped[2];
195
- value.sub = getTypeDef(extractSubType(type, wrapped), {}, count);
196
- }
197
- return value;
174
+ export function getTypeDef(_type, { displayName, name } = {}, count = 0) {
175
+ // create the type via Type, allowing types to be sanitized
176
+ const type = sanitize(_type);
177
+ const value = { displayName, info: TypeDefInfo.Plain, name, type };
178
+ if (++count > 64) {
179
+ throw new Error('getTypeDef: Maximum nested limit reached');
180
+ }
181
+ const nested = nestedExtraction.find((nested) => hasWrapper(type, nested));
182
+ if (nested) {
183
+ value.info = nested[2];
184
+ return nested[3](value, type, extractSubType(type, nested), count);
185
+ }
186
+ const wrapped = wrappedExtraction.find((wrapped) => hasWrapper(type, wrapped));
187
+ if (wrapped) {
188
+ value.info = wrapped[2];
189
+ value.sub = getTypeDef(extractSubType(type, wrapped), {}, count);
190
+ }
191
+ return value;
198
192
  }
package/util/index.js CHANGED
@@ -1,7 +1,4 @@
1
- // Copyright 2017-2023 @polkadot/types-create authors & contributors
2
- // SPDX-License-Identifier: Apache-2.0
3
-
4
- export * from "./encodeTypes.js";
5
- export * from "./getTypeDef.js";
6
- export * from "./typeSplit.js";
7
- export * from "./xcm.js";
1
+ export * from './encodeTypes.js';
2
+ export * from './getTypeDef.js';
3
+ export * from './typeSplit.js';
4
+ export * from './xcm.js';