@pezkuwi/types-create 16.5.16 → 16.5.18

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.
Files changed (168) hide show
  1. package/build/LICENSE +201 -0
  2. package/build/README.md +3 -0
  3. package/build/bundle.js +3 -0
  4. package/build/cjs/bundle.d.ts +3 -0
  5. package/build/cjs/bundle.js +9 -0
  6. package/build/cjs/create/class.js +165 -0
  7. package/build/cjs/create/index.js +5 -0
  8. package/build/cjs/create/type.js +66 -0
  9. package/build/cjs/exports.js +5 -0
  10. package/build/cjs/index.js +5 -0
  11. package/build/cjs/packageInfo.js +4 -0
  12. package/build/cjs/types/augmentRegistry.js +3 -0
  13. package/build/cjs/types/index.js +6 -0
  14. package/build/cjs/types/lookup.js +2 -0
  15. package/build/cjs/types/types.js +29 -0
  16. package/build/cjs/util/encodeTypes.js +128 -0
  17. package/build/cjs/util/getTypeDef.js +195 -0
  18. package/build/cjs/util/index.js +7 -0
  19. package/build/cjs/util/typeSplit.js +60 -0
  20. package/build/cjs/util/xcm.js +9 -0
  21. package/build/create/class.d.ts +5 -0
  22. package/build/create/class.js +160 -0
  23. package/build/create/index.d.ts +2 -0
  24. package/build/create/index.js +2 -0
  25. package/build/create/type.d.ts +3 -0
  26. package/build/create/type.js +63 -0
  27. package/build/exports.d.ts +2 -0
  28. package/build/exports.js +2 -0
  29. package/build/index.d.ts +2 -0
  30. package/build/index.js +2 -0
  31. package/build/package.json +291 -0
  32. package/build/packageDetect.d.ts +1 -0
  33. package/build/packageDetect.js +4 -0
  34. package/build/packageInfo.d.ts +6 -0
  35. package/build/packageInfo.js +1 -0
  36. package/build/types/augmentRegistry.d.ts +11 -0
  37. package/build/types/augmentRegistry.js +1 -0
  38. package/build/types/index.d.ts +4 -0
  39. package/build/types/index.js +3 -0
  40. package/build/types/lookup.d.ts +14 -0
  41. package/build/types/lookup.js +1 -0
  42. package/build/types/types.d.ts +43 -0
  43. package/build/types/types.js +26 -0
  44. package/build/util/encodeTypes.d.ts +11 -0
  45. package/build/util/encodeTypes.js +123 -0
  46. package/build/util/getTypeDef.d.ts +8 -0
  47. package/build/util/getTypeDef.js +192 -0
  48. package/build/util/index.d.ts +4 -0
  49. package/build/util/index.js +4 -0
  50. package/build/util/typeSplit.d.ts +1 -0
  51. package/build/util/typeSplit.js +57 -0
  52. package/build/util/xcm.d.ts +2 -0
  53. package/build/util/xcm.js +5 -0
  54. package/build-deno/README.md +3 -0
  55. package/build-deno/bundle.ts +5 -0
  56. package/build-deno/create/class.ts +252 -0
  57. package/build-deno/create/index.ts +3 -0
  58. package/build-deno/create/type.ts +85 -0
  59. package/build-deno/exports.ts +3 -0
  60. package/build-deno/index.ts +4 -0
  61. package/build-deno/mod.ts +2 -0
  62. package/build-deno/packageDetect.ts +8 -0
  63. package/build-deno/packageInfo.ts +3 -0
  64. package/build-deno/types/augmentRegistry.ts +15 -0
  65. package/build-deno/types/index.ts +7 -0
  66. package/build-deno/types/lookup.ts +16 -0
  67. package/build-deno/types/types.ts +45 -0
  68. package/build-deno/util/encodeTypes.ts +201 -0
  69. package/build-deno/util/getTypeDef.ts +267 -0
  70. package/build-deno/util/index.ts +5 -0
  71. package/build-deno/util/typeSplit.ts +53 -0
  72. package/build-deno/util/xcm.ts +10 -0
  73. package/build-tsc-cjs/packageDetect.js +6 -0
  74. package/{cjs → build-tsc-cjs}/packageInfo.js +1 -1
  75. package/{packageInfo.js → build-tsc-esm/packageInfo.js} +1 -1
  76. package/package.json +11 -11
  77. package/src/bundle.ts +9 -0
  78. package/src/create/class.ts +257 -0
  79. package/src/create/index.ts +5 -0
  80. package/src/create/type.ts +94 -0
  81. package/src/exports.ts +6 -0
  82. package/src/index.ts +6 -0
  83. package/src/mod.ts +4 -0
  84. package/src/packageDetect.ts +12 -0
  85. package/src/packageInfo.ts +6 -0
  86. package/src/types/augmentRegistry.ts +19 -0
  87. package/src/types/index.ts +11 -0
  88. package/src/types/lookup.ts +21 -0
  89. package/src/types/types.ts +47 -0
  90. package/src/util/encodeTypes.spec.ts +217 -0
  91. package/src/util/encodeTypes.ts +205 -0
  92. package/src/util/getTypeDef.spec.ts +704 -0
  93. package/src/util/getTypeDef.ts +279 -0
  94. package/src/util/index.ts +7 -0
  95. package/src/util/typeSplit.spec.ts +50 -0
  96. package/src/util/typeSplit.ts +56 -0
  97. package/src/util/xcm.ts +12 -0
  98. package/tsconfig.build.json +15 -0
  99. package/tsconfig.build.tsbuildinfo +1 -0
  100. package/tsconfig.spec.json +17 -0
  101. package/tsconfig.spec.tsbuildinfo +1 -0
  102. /package/{cjs → build}/bundle.d.ts +0 -0
  103. /package/{create → build/cjs/create}/class.d.ts +0 -0
  104. /package/{create → build/cjs/create}/index.d.ts +0 -0
  105. /package/{create → build/cjs/create}/type.d.ts +0 -0
  106. /package/{exports.d.ts → build/cjs/exports.d.ts} +0 -0
  107. /package/{index.d.ts → build/cjs/index.d.ts} +0 -0
  108. /package/{cjs → build/cjs}/package.json +0 -0
  109. /package/{packageDetect.d.ts → build/cjs/packageDetect.d.ts} +0 -0
  110. /package/{cjs → build/cjs}/packageDetect.js +0 -0
  111. /package/{packageInfo.d.ts → build/cjs/packageInfo.d.ts} +0 -0
  112. /package/{types → build/cjs/types}/augmentRegistry.d.ts +0 -0
  113. /package/{types → build/cjs/types}/index.d.ts +0 -0
  114. /package/{types → build/cjs/types}/lookup.d.ts +0 -0
  115. /package/{types → build/cjs/types}/types.d.ts +0 -0
  116. /package/{util → build/cjs/util}/encodeTypes.d.ts +0 -0
  117. /package/{util → build/cjs/util}/getTypeDef.d.ts +0 -0
  118. /package/{util → build/cjs/util}/index.d.ts +0 -0
  119. /package/{util → build/cjs/util}/typeSplit.d.ts +0 -0
  120. /package/{util → build/cjs/util}/xcm.d.ts +0 -0
  121. /package/{bundle.d.ts → build-tsc/bundle.d.ts} +0 -0
  122. /package/{cjs → build-tsc}/create/class.d.ts +0 -0
  123. /package/{cjs → build-tsc}/create/index.d.ts +0 -0
  124. /package/{cjs → build-tsc}/create/type.d.ts +0 -0
  125. /package/{cjs → build-tsc}/exports.d.ts +0 -0
  126. /package/{cjs → build-tsc}/index.d.ts +0 -0
  127. /package/{cjs → build-tsc}/packageDetect.d.ts +0 -0
  128. /package/{cjs → build-tsc}/packageInfo.d.ts +0 -0
  129. /package/{cjs → build-tsc}/types/augmentRegistry.d.ts +0 -0
  130. /package/{cjs → build-tsc}/types/index.d.ts +0 -0
  131. /package/{cjs → build-tsc}/types/lookup.d.ts +0 -0
  132. /package/{cjs → build-tsc}/types/types.d.ts +0 -0
  133. /package/{cjs → build-tsc}/util/encodeTypes.d.ts +0 -0
  134. /package/{cjs → build-tsc}/util/getTypeDef.d.ts +0 -0
  135. /package/{cjs → build-tsc}/util/index.d.ts +0 -0
  136. /package/{cjs → build-tsc}/util/typeSplit.d.ts +0 -0
  137. /package/{cjs → build-tsc}/util/xcm.d.ts +0 -0
  138. /package/{cjs → build-tsc-cjs}/bundle.js +0 -0
  139. /package/{cjs → build-tsc-cjs}/create/class.js +0 -0
  140. /package/{cjs → build-tsc-cjs}/create/index.js +0 -0
  141. /package/{cjs → build-tsc-cjs}/create/type.js +0 -0
  142. /package/{cjs → build-tsc-cjs}/exports.js +0 -0
  143. /package/{cjs → build-tsc-cjs}/index.js +0 -0
  144. /package/{cjs → build-tsc-cjs}/types/augmentRegistry.js +0 -0
  145. /package/{cjs → build-tsc-cjs}/types/index.js +0 -0
  146. /package/{cjs → build-tsc-cjs}/types/lookup.js +0 -0
  147. /package/{cjs → build-tsc-cjs}/types/types.js +0 -0
  148. /package/{cjs → build-tsc-cjs}/util/encodeTypes.js +0 -0
  149. /package/{cjs → build-tsc-cjs}/util/getTypeDef.js +0 -0
  150. /package/{cjs → build-tsc-cjs}/util/index.js +0 -0
  151. /package/{cjs → build-tsc-cjs}/util/typeSplit.js +0 -0
  152. /package/{cjs → build-tsc-cjs}/util/xcm.js +0 -0
  153. /package/{bundle.js → build-tsc-esm/bundle.js} +0 -0
  154. /package/{create → build-tsc-esm/create}/class.js +0 -0
  155. /package/{create → build-tsc-esm/create}/index.js +0 -0
  156. /package/{create → build-tsc-esm/create}/type.js +0 -0
  157. /package/{exports.js → build-tsc-esm/exports.js} +0 -0
  158. /package/{index.js → build-tsc-esm/index.js} +0 -0
  159. /package/{packageDetect.js → build-tsc-esm/packageDetect.js} +0 -0
  160. /package/{types → build-tsc-esm/types}/augmentRegistry.js +0 -0
  161. /package/{types → build-tsc-esm/types}/index.js +0 -0
  162. /package/{types → build-tsc-esm/types}/lookup.js +0 -0
  163. /package/{types → build-tsc-esm/types}/types.js +0 -0
  164. /package/{util → build-tsc-esm/util}/encodeTypes.js +0 -0
  165. /package/{util → build-tsc-esm/util}/getTypeDef.js +0 -0
  166. /package/{util → build-tsc-esm/util}/index.js +0 -0
  167. /package/{util → build-tsc-esm/util}/typeSplit.js +0 -0
  168. /package/{util → build-tsc-esm/util}/xcm.js +0 -0
@@ -0,0 +1,16 @@
1
+
2
+ import type { Option, Text } from 'https://deno.land/x/pezkuwi/types-codec/mod.ts';
3
+ import type { ICompact, INumber, LookupString } from 'https://deno.land/x/pezkuwi/types-codec/types/index.ts';
4
+ import type { TypeDef } from './types.ts';
5
+
6
+ interface SiTypeBase {
7
+ def: {
8
+ asTuple: ICompact<INumber>[]
9
+ }
10
+ }
11
+
12
+ export interface ILookup {
13
+ getSiType (lookupId: ICompact<INumber> | LookupString | number): SiTypeBase;
14
+ getTypeDef (lookupId: ICompact<INumber> | LookupString | number): TypeDef;
15
+ sanitizeField (name: Option<Text>): [string | null, string | null];
16
+ }
@@ -0,0 +1,45 @@
1
+
2
+ export enum TypeDefInfo {
3
+ BTreeMap,
4
+ BTreeSet,
5
+ Compact,
6
+ DoNotConstruct,
7
+ Enum,
8
+ HashMap,
9
+ Int,
10
+ Linkage,
11
+ Null,
12
+ Option,
13
+ Plain,
14
+ Range,
15
+ RangeInclusive,
16
+ Result,
17
+ Set,
18
+ Si,
19
+ Struct,
20
+ Tuple,
21
+ UInt,
22
+ Vec,
23
+ VecFixed,
24
+ WrapperKeepOpaque,
25
+ WrapperOpaque
26
+ }
27
+
28
+ export interface TypeDef {
29
+ alias?: Map<string, string> | undefined;
30
+ displayName?: string | undefined;
31
+ docs?: string[] | undefined;
32
+ fallbackType?: string | undefined;
33
+ info: TypeDefInfo;
34
+ index?: number;
35
+ isFromSi?: boolean;
36
+ length?: number;
37
+ lookupIndex?: number;
38
+ lookupName?: string | undefined;
39
+ lookupNameRoot?: string | undefined;
40
+ name?: string | undefined;
41
+ namespace?: string | undefined;
42
+ sub?: TypeDef | TypeDef[];
43
+ type: string;
44
+ typeName?: string | undefined;
45
+ }
@@ -0,0 +1,201 @@
1
+
2
+ import type { Registry } from 'https://deno.land/x/pezkuwi/types-codec/types/index.ts';
3
+ import type { TypeDef } from 'https://deno.land/x/pezkuwi/types-create/types/index.ts';
4
+
5
+ import { isNumber, isUndefined, objectSpread, stringify } from 'https://deno.land/x/pezkuwi/util/mod.ts';
6
+
7
+ import { TypeDefInfo } from '../types/index.ts';
8
+
9
+ interface ToString { toString: () => string }
10
+
11
+ const stringIdentity = <T extends ToString> (value: T): string => value.toString();
12
+
13
+ const INFO_WRAP = ['BTreeMap', 'BTreeSet', 'Compact', 'HashMap', 'Option', 'Result', 'Vec'];
14
+
15
+ export function paramsNotation <T extends ToString> (outer: string, inner?: T | T[], transform: (_: T) => string = stringIdentity): string {
16
+ return `${outer}${
17
+ inner
18
+ ? `<${(Array.isArray(inner) ? inner : [inner]).map(transform).join(', ')}>`
19
+ : ''
20
+ }`;
21
+ }
22
+
23
+ function encodeWithParams (registry: Registry, typeDef: TypeDef, outer: string): string {
24
+ const { info, sub } = typeDef;
25
+
26
+ switch (info) {
27
+ case TypeDefInfo.BTreeMap:
28
+ case TypeDefInfo.BTreeSet:
29
+ case TypeDefInfo.Compact:
30
+ case TypeDefInfo.HashMap:
31
+ case TypeDefInfo.Linkage:
32
+ case TypeDefInfo.Option:
33
+ case TypeDefInfo.Range:
34
+ case TypeDefInfo.RangeInclusive:
35
+ case TypeDefInfo.Result:
36
+ case TypeDefInfo.Vec:
37
+ case TypeDefInfo.WrapperKeepOpaque:
38
+ case TypeDefInfo.WrapperOpaque:
39
+ return paramsNotation(outer, sub, (p) => encodeTypeDef(registry, p));
40
+ }
41
+
42
+ throw new Error(`Unable to encode ${stringify(typeDef)} with params`);
43
+ }
44
+
45
+ function encodeSubTypes (registry: Registry, sub: TypeDef[], asEnum?: boolean, extra?: Record<string, unknown>): string {
46
+ const names = sub.map(({ name }) => name);
47
+
48
+ if (!names.every((n) => !!n)) {
49
+ throw new Error(`Subtypes does not have consistent names, ${names.join(', ')}`);
50
+ }
51
+
52
+ const inner: Record<string, string> = objectSpread({}, extra);
53
+
54
+ for (let i = 0, count = sub.length; i < count; i++) {
55
+ const def = sub[i];
56
+
57
+ if (!def.name) {
58
+ throw new Error(`No name found in ${stringify(def)}`);
59
+ }
60
+
61
+ inner[def.name] = encodeTypeDef(registry, def);
62
+ }
63
+
64
+ return stringify(
65
+ asEnum
66
+ ? { _enum: inner }
67
+ : inner
68
+ );
69
+ }
70
+
71
+ const encoders: Record<TypeDefInfo, (registry: Registry, typeDef: TypeDef) => string> = {
72
+ [TypeDefInfo.BTreeMap]: (registry: Registry, typeDef: TypeDef) =>
73
+ encodeWithParams(registry, typeDef, 'BTreeMap'),
74
+
75
+ [TypeDefInfo.BTreeSet]: (registry: Registry, typeDef: TypeDef) =>
76
+ encodeWithParams(registry, typeDef, 'BTreeSet'),
77
+
78
+ [TypeDefInfo.Compact]: (registry: Registry, typeDef: TypeDef) =>
79
+ encodeWithParams(registry, typeDef, 'Compact'),
80
+
81
+ [TypeDefInfo.DoNotConstruct]: (registry: Registry, { displayName, lookupIndex, lookupName }: TypeDef) =>
82
+ `DoNotConstruct<${lookupName || displayName || (isUndefined(lookupIndex) ? 'Unknown' : registry.createLookupType(lookupIndex))}>`,
83
+
84
+ [TypeDefInfo.Enum]: (registry: Registry, { sub }: TypeDef): string => {
85
+ if (!Array.isArray(sub)) {
86
+ throw new Error('Unable to encode Enum type');
87
+ }
88
+
89
+ // c-like enums have all Null entries
90
+ // TODO We need to take the disciminant into account and auto-add empty entries
91
+ return sub.every(({ type }) => type === 'Null')
92
+ ? stringify({ _enum: sub.map(({ name }, index) => `${name || `Empty${index}`}`) })
93
+ : encodeSubTypes(registry, sub, true);
94
+ },
95
+
96
+ [TypeDefInfo.HashMap]: (registry: Registry, typeDef: TypeDef) =>
97
+ encodeWithParams(registry, typeDef, 'HashMap'),
98
+
99
+ [TypeDefInfo.Int]: (_registry: Registry, { length = 32 }: TypeDef) =>
100
+ `Int<${length}>`,
101
+
102
+ [TypeDefInfo.Linkage]: (registry: Registry, typeDef: TypeDef) =>
103
+ encodeWithParams(registry, typeDef, 'Linkage'),
104
+
105
+ [TypeDefInfo.Null]: (_registry: Registry, _typeDef: TypeDef) =>
106
+ 'Null',
107
+
108
+ [TypeDefInfo.Option]: (registry: Registry, typeDef: TypeDef) =>
109
+ encodeWithParams(registry, typeDef, 'Option'),
110
+
111
+ [TypeDefInfo.Plain]: (_registry: Registry, { displayName, type }: TypeDef) =>
112
+ displayName || type,
113
+
114
+ [TypeDefInfo.Range]: (registry: Registry, typeDef: TypeDef) =>
115
+ encodeWithParams(registry, typeDef, 'Range'),
116
+
117
+ [TypeDefInfo.RangeInclusive]: (registry: Registry, typeDef: TypeDef) =>
118
+ encodeWithParams(registry, typeDef, 'RangeInclusive'),
119
+
120
+ [TypeDefInfo.Result]: (registry: Registry, typeDef: TypeDef) =>
121
+ encodeWithParams(registry, typeDef, 'Result'),
122
+
123
+ [TypeDefInfo.Set]: (_registry: Registry, { length = 8, sub }: TypeDef): string => {
124
+ if (!Array.isArray(sub)) {
125
+ throw new Error('Unable to encode Set type');
126
+ }
127
+
128
+ return stringify({
129
+ _set: sub.reduce((all, { index, name }, count) =>
130
+ objectSpread(all, { [`${name || `Unknown${index || count}`}`]: index || count }),
131
+ { _bitLength: length || 8 })
132
+ });
133
+ },
134
+
135
+ [TypeDefInfo.Si]: (_registry: Registry, { lookupName, type }: TypeDef) =>
136
+ lookupName || type,
137
+
138
+ [TypeDefInfo.Struct]: (registry: Registry, { alias, sub }: TypeDef): string => {
139
+ if (!Array.isArray(sub)) {
140
+ throw new Error('Unable to encode Struct type');
141
+ }
142
+
143
+ return encodeSubTypes(registry, sub, false, alias
144
+ ? {
145
+ _alias: [...alias.entries()].reduce<Record<string, string>>((all, [k, v]) =>
146
+ objectSpread(all, { [k]: v }), {}
147
+ )
148
+ }
149
+ : {}
150
+ );
151
+ },
152
+
153
+ [TypeDefInfo.Tuple]: (registry: Registry, { sub }: TypeDef): string => {
154
+ if (!Array.isArray(sub)) {
155
+ throw new Error('Unable to encode Tuple type');
156
+ }
157
+
158
+ return `(${sub.map((type) => encodeTypeDef(registry, type)).join(',')})`;
159
+ },
160
+
161
+ [TypeDefInfo.UInt]: (_registry: Registry, { length = 32 }: TypeDef) =>
162
+ `UInt<${length}>`,
163
+
164
+ [TypeDefInfo.Vec]: (registry: Registry, typeDef: TypeDef) =>
165
+ encodeWithParams(registry, typeDef, 'Vec'),
166
+
167
+ [TypeDefInfo.VecFixed]: (_registry: Registry, { length, sub }: TypeDef): string => {
168
+ if (!isNumber(length) || !sub || Array.isArray(sub)) {
169
+ throw new Error('Unable to encode VecFixed type');
170
+ }
171
+
172
+ return `[${sub.type};${length}]`;
173
+ },
174
+
175
+ [TypeDefInfo.WrapperKeepOpaque]: (registry: Registry, typeDef: TypeDef) =>
176
+ encodeWithParams(registry, typeDef, 'WrapperKeepOpaque'),
177
+
178
+ [TypeDefInfo.WrapperOpaque]: (registry: Registry, typeDef: TypeDef) =>
179
+ encodeWithParams(registry, typeDef, 'WrapperOpaque')
180
+ };
181
+
182
+ function encodeType (registry: Registry, typeDef: TypeDef, withLookup = true): string {
183
+ return withLookup && typeDef.lookupName
184
+ ? typeDef.lookupName
185
+ : encoders[typeDef.info](registry, typeDef);
186
+ }
187
+
188
+ export function encodeTypeDef (registry: Registry, typeDef: TypeDef): string {
189
+ // In the case of contracts we do have the unfortunate situation where the displayName would
190
+ // refer to "Option" when it is an option. For these, string it out, only using when actually
191
+ // not a top-level element to be used
192
+ return (typeDef.displayName && !INFO_WRAP.some((i) => typeDef.displayName === i))
193
+ ? typeDef.displayName
194
+ : encodeType(registry, typeDef);
195
+ }
196
+
197
+ export function withTypeString (registry: Registry, typeDef: Omit<TypeDef, 'type'> & { type?: string }): TypeDef {
198
+ return objectSpread({}, typeDef, {
199
+ type: encodeType(registry, typeDef as TypeDef, false)
200
+ });
201
+ }
@@ -0,0 +1,267 @@
1
+
2
+ import type { AnyString } from 'https://deno.land/x/pezkuwi/types-codec/types/index.ts';
3
+ import type { TypeDef } from 'https://deno.land/x/pezkuwi/types-create/types/index.ts';
4
+
5
+ import { sanitize } from 'https://deno.land/x/pezkuwi/types-codec/mod.ts';
6
+ import { isNumber, isString, objectSpread, stringify } from 'https://deno.land/x/pezkuwi/util/mod.ts';
7
+
8
+ import { TypeDefInfo } from '../types/index.ts';
9
+ import { typeSplit } from './typeSplit.ts';
10
+
11
+ interface TypeDefOptions {
12
+ name?: string;
13
+ displayName?: string;
14
+ }
15
+
16
+ interface SetDetails {
17
+ _bitLength: number;
18
+ index: number;
19
+
20
+ [key: string]: number;
21
+ }
22
+
23
+ interface ParsedDef {
24
+ _alias: string;
25
+ _enum?: string[];
26
+ _fallback?: string;
27
+ _set?: SetDetails;
28
+
29
+ [key: string]: unknown;
30
+ }
31
+
32
+ const KNOWN_INTERNALS = ['_alias', '_fallback'];
33
+
34
+ function getTypeString (typeOrObj: any): string {
35
+ return isString(typeOrObj)
36
+ ? typeOrObj.toString()
37
+ : stringify(typeOrObj);
38
+ }
39
+
40
+ function isRustEnum (details: Record<string, string> | Record<string, number>): details is Record<string, string> {
41
+ const values = Object.values(details);
42
+
43
+ if (values.some((v) => isNumber(v))) {
44
+ if (!values.every((v) => isNumber(v) && v >= 0 && v <= 255)) {
45
+ throw new Error('Invalid number-indexed enum definition');
46
+ }
47
+
48
+ return false;
49
+ }
50
+
51
+ return true;
52
+ }
53
+
54
+ function _decodeEnum (value: TypeDef, details: string[] | Record<string, string> | Record<string, number>, count: number, fallbackType?: string): TypeDef {
55
+ value.info = TypeDefInfo.Enum;
56
+ value.fallbackType = fallbackType;
57
+
58
+ // not as pretty, but remain compatible with oo7 for both struct and Array types
59
+ if (Array.isArray(details)) {
60
+ value.sub = details.map((name, index): TypeDef => ({
61
+ index,
62
+ info: TypeDefInfo.Plain,
63
+ name,
64
+ type: 'Null'
65
+ }));
66
+ } else if (isRustEnum(details)) {
67
+ value.sub = Object.entries(details).map(([name, typeOrObj], index): TypeDef =>
68
+ objectSpread({}, getTypeDef(getTypeString(typeOrObj || 'Null'), { name }, count), { index })
69
+ );
70
+ } else {
71
+ value.sub = Object.entries(details).map(([name, index]): TypeDef => ({
72
+ index,
73
+ info: TypeDefInfo.Plain,
74
+ name,
75
+ type: 'Null'
76
+ }));
77
+ }
78
+
79
+ return value;
80
+ }
81
+
82
+ function _decodeSet (value: TypeDef, details: SetDetails, fallbackType: string | undefined): TypeDef {
83
+ value.info = TypeDefInfo.Set;
84
+ value.fallbackType = fallbackType;
85
+ value.length = details._bitLength;
86
+ value.sub = Object
87
+ .entries(details)
88
+ .filter(([name]): boolean => !name.startsWith('_'))
89
+ .map(([name, index]): TypeDef => ({
90
+ index,
91
+ info: TypeDefInfo.Plain,
92
+ name,
93
+ type: 'Null'
94
+ }));
95
+
96
+ return value;
97
+ }
98
+
99
+ function _decodeStruct (value: TypeDef, type: string, _: string, count: number): TypeDef {
100
+ const parsed = JSON.parse(type) as ParsedDef;
101
+ const keys = Object.keys(parsed);
102
+
103
+ if (parsed._enum) {
104
+ return _decodeEnum(value, parsed._enum, count, parsed._fallback);
105
+ } else if (parsed._set) {
106
+ return _decodeSet(value, parsed._set, parsed._fallback);
107
+ }
108
+
109
+ value.alias = parsed._alias
110
+ ? new Map(Object.entries(parsed._alias))
111
+ : undefined;
112
+ value.fallbackType = parsed._fallback;
113
+ value.sub = keys
114
+ .filter((name) => !KNOWN_INTERNALS.includes(name))
115
+ .map((name) =>
116
+ getTypeDef(getTypeString(parsed[name]), { name }, count)
117
+ );
118
+
119
+ return value;
120
+ }
121
+
122
+ function _decodeFixedVec (value: TypeDef, type: string, _: string, count: number): TypeDef {
123
+ const max = type.length - 1;
124
+ let index = -1;
125
+ let inner = 0;
126
+
127
+ for (let i = 1; (i < max) && (index === -1); i++) {
128
+ switch (type[i]) {
129
+ case ';': {
130
+ if (inner === 0) {
131
+ index = i;
132
+ }
133
+
134
+ break;
135
+ }
136
+
137
+ case '[':
138
+ case '(':
139
+ case '<':
140
+ inner++;
141
+ break;
142
+
143
+ case ']':
144
+ case ')':
145
+ case '>':
146
+ inner--;
147
+ break;
148
+ }
149
+ }
150
+
151
+ if (index === -1) {
152
+ throw new Error(`${type}: Unable to extract location of ';'`);
153
+ }
154
+
155
+ const vecType = type.substring(1, index);
156
+ const [strLength, displayName] = type.substring(index + 1, max).split(';');
157
+ const length = parseInt(strLength.trim(), 10);
158
+
159
+ if (length > 2048) {
160
+ throw new Error(`${type}: Only support for [Type; <length>], where length <= 2048`);
161
+ }
162
+
163
+ value.displayName = displayName;
164
+ value.length = length;
165
+ value.sub = getTypeDef(vecType, {}, count);
166
+
167
+ return value;
168
+ }
169
+
170
+ function _decodeTuple (value: TypeDef, _: string, subType: string, count: number): TypeDef {
171
+ value.sub = subType.length === 0
172
+ ? []
173
+ : typeSplit(subType).map((inner) => getTypeDef(inner, {}, count));
174
+
175
+ return value;
176
+ }
177
+
178
+ function _decodeAnyInt (value: TypeDef, type: string, _: string, clazz: 'Int' | 'UInt'): TypeDef {
179
+ const [strLength, displayName] = type.substring(clazz.length + 1, type.length - 1).split(',');
180
+ const length = parseInt(strLength.trim(), 10);
181
+
182
+ if ((length > 8192) || (length % 8)) {
183
+ throw new Error(`${type}: Only support for ${clazz}<bitLength>, where length <= 8192 and a power of 8, found ${length}`);
184
+ }
185
+
186
+ value.displayName = displayName;
187
+ value.length = length;
188
+
189
+ return value;
190
+ }
191
+
192
+ function _decodeInt (value: TypeDef, type: string, subType: string): TypeDef {
193
+ return _decodeAnyInt(value, type, subType, 'Int');
194
+ }
195
+
196
+ function _decodeUInt (value: TypeDef, type: string, subType: string): TypeDef {
197
+ return _decodeAnyInt(value, type, subType, 'UInt');
198
+ }
199
+
200
+ function _decodeDoNotConstruct (value: TypeDef, type: string, _: string): TypeDef {
201
+ const NAME_LENGTH = 'DoNotConstruct'.length;
202
+
203
+ value.displayName = type.substring(NAME_LENGTH + 1, type.length - 1);
204
+
205
+ return value;
206
+ }
207
+
208
+ function hasWrapper (type: string, [start, end]: [string, string, TypeDefInfo, any?]): boolean {
209
+ return (type.startsWith(start)) && (type.slice(-1 * end.length) === end);
210
+ }
211
+
212
+ const nestedExtraction: [string, string, TypeDefInfo, (value: TypeDef, type: string, subType: string, count: number) => TypeDef][] = [
213
+ ['[', ']', TypeDefInfo.VecFixed, _decodeFixedVec],
214
+ ['{', '}', TypeDefInfo.Struct, _decodeStruct],
215
+ ['(', ')', TypeDefInfo.Tuple, _decodeTuple],
216
+ // the inner for these are the same as tuple, multiple values
217
+ ['BTreeMap<', '>', TypeDefInfo.BTreeMap, _decodeTuple],
218
+ ['HashMap<', '>', TypeDefInfo.HashMap, _decodeTuple],
219
+ ['Int<', '>', TypeDefInfo.Int, _decodeInt],
220
+ ['Result<', '>', TypeDefInfo.Result, _decodeTuple],
221
+ ['UInt<', '>', TypeDefInfo.UInt, _decodeUInt],
222
+ ['DoNotConstruct<', '>', TypeDefInfo.DoNotConstruct, _decodeDoNotConstruct]
223
+ ];
224
+
225
+ const wrappedExtraction: [string, string, TypeDefInfo][] = [
226
+ ['BTreeSet<', '>', TypeDefInfo.BTreeSet],
227
+ ['Compact<', '>', TypeDefInfo.Compact],
228
+ ['Linkage<', '>', TypeDefInfo.Linkage],
229
+ ['Opaque<', '>', TypeDefInfo.WrapperOpaque],
230
+ ['Option<', '>', TypeDefInfo.Option],
231
+ ['Range<', '>', TypeDefInfo.Range],
232
+ ['RangeInclusive<', '>', TypeDefInfo.RangeInclusive],
233
+ ['Vec<', '>', TypeDefInfo.Vec],
234
+ ['WrapperKeepOpaque<', '>', TypeDefInfo.WrapperKeepOpaque],
235
+ ['WrapperOpaque<', '>', TypeDefInfo.WrapperOpaque]
236
+ ];
237
+
238
+ function extractSubType (type: string, [start, end]: [string, string, TypeDefInfo, any?]): string {
239
+ return type.substring(start.length, type.length - end.length);
240
+ }
241
+
242
+ export function getTypeDef (_type: AnyString, { displayName, name }: TypeDefOptions = {}, count = 0): TypeDef {
243
+ // create the type via Type, allowing types to be sanitized
244
+ const type = sanitize(_type);
245
+ const value: TypeDef = { displayName, info: TypeDefInfo.Plain, name, type };
246
+
247
+ if (++count > 64) {
248
+ throw new Error('getTypeDef: Maximum nested limit reached');
249
+ }
250
+
251
+ const nested = nestedExtraction.find((nested) => hasWrapper(type, nested));
252
+
253
+ if (nested) {
254
+ value.info = nested[2];
255
+
256
+ return nested[3](value, type, extractSubType(type, nested), count);
257
+ }
258
+
259
+ const wrapped = wrappedExtraction.find((wrapped) => hasWrapper(type, wrapped));
260
+
261
+ if (wrapped) {
262
+ value.info = wrapped[2];
263
+ value.sub = getTypeDef(extractSubType(type, wrapped), {}, count);
264
+ }
265
+
266
+ return value;
267
+ }
@@ -0,0 +1,5 @@
1
+
2
+ export * from './encodeTypes.ts';
3
+ export * from './getTypeDef.ts';
4
+ export * from './typeSplit.ts';
5
+ export * from './xcm.ts';
@@ -0,0 +1,53 @@
1
+
2
+ export function typeSplit (type: string): string[] {
3
+ const result: string[] = [];
4
+
5
+ // these are the depths of the various tokens: <, [, {, (
6
+ let c = 0;
7
+ let f = 0;
8
+ let s = 0;
9
+ let t = 0;
10
+
11
+ // current start position
12
+ let start = 0;
13
+
14
+ for (let i = 0, count = type.length; i < count; i++) {
15
+ switch (type[i]) {
16
+ // if we are not nested, add the type
17
+ case ',': {
18
+ if (!(c || f || s || t)) {
19
+ result.push(type.substring(start, i).trim());
20
+ start = i + 1;
21
+ }
22
+
23
+ break;
24
+ }
25
+
26
+ // adjust compact/vec (and friends) depth
27
+ case '<': c++; break;
28
+ case '>': c--; break;
29
+
30
+ // adjust fixed vec depths
31
+ case '[': f++; break;
32
+ case ']': f--; break;
33
+
34
+ // adjust struct depth
35
+ case '{': s++; break;
36
+ case '}': s--; break;
37
+
38
+ // adjust tuple depth
39
+ case '(': t++; break;
40
+ case ')': t--; break;
41
+ }
42
+ }
43
+
44
+ // ensure we have all the terminators taken care of
45
+ if (c || f || s || t) {
46
+ throw new Error(`Invalid definition (missing terminators) found in ${type}`);
47
+ }
48
+
49
+ // the final leg of the journey
50
+ result.push(type.substring(start, type.length).trim());
51
+
52
+ return result;
53
+ }
@@ -0,0 +1,10 @@
1
+
2
+ import { objectSpread } from 'https://deno.land/x/pezkuwi/util/mod.ts';
3
+
4
+ export const XCM_MAPPINGS = ['AssetInstance', 'Fungibility', 'Junction', 'Junctions', 'MultiAsset', 'MultiAssetFilter', 'MultiLocation', 'Response', 'WildFungibility', 'WildMultiAsset', 'Xcm', 'XcmError'];
5
+
6
+ export function mapXcmTypes (version: 'V0' | 'V1' | 'V2' | 'V3' | 'V4' | 'V5'): Record<string, string> {
7
+ return XCM_MAPPINGS.reduce<Record<string, string>>((all, key) =>
8
+ objectSpread(all, { [key]: `${key}${version}` }), {}
9
+ );
10
+ }
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const packageInfo_1 = require("@pezkuwi/types-codec/packageInfo");
4
+ const util_1 = require("@pezkuwi/util");
5
+ const packageInfo_js_1 = require("./packageInfo.js");
6
+ (0, util_1.detectPackage)(packageInfo_js_1.packageInfo, null, [packageInfo_1.packageInfo]);
@@ -1,4 +1,4 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.packageInfo = void 0;
4
- exports.packageInfo = { name: '@pezkuwi/types-create', path: typeof __dirname === 'string' ? __dirname : 'auto', type: 'cjs', version: '16.5.16' };
4
+ exports.packageInfo = { name: '@pezkuwi/types-create', path: typeof __dirname === 'string' ? __dirname : 'auto', type: 'cjs', version: '16.5.18' };
@@ -1 +1 @@
1
- export const packageInfo = { name: '@pezkuwi/types-create', path: (import.meta && import.meta.url) ? new URL(import.meta.url).pathname.substring(0, new URL(import.meta.url).pathname.lastIndexOf('/') + 1) : 'auto', type: 'esm', version: '16.5.16' };
1
+ export const packageInfo = { name: '@pezkuwi/types-create', path: (import.meta && import.meta.url) ? new URL(import.meta.url).pathname.substring(0, new URL(import.meta.url).pathname.lastIndexOf('/') + 1) : 'auto', type: 'esm', version: '16.5.18' };
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "author": "Dijital Kurdistan Tech Institute <dev@pezkuwichain.io>",
2
+ "author": "Jaco Greeff <jacogr@gmail.com>",
3
3
  "bugs": "https://github.com/pezkuwichain/pezkuwi-api/issues",
4
4
  "description": "Type creator helpers",
5
5
  "engines": {
@@ -18,13 +18,11 @@
18
18
  "./cjs/packageDetect.js"
19
19
  ],
20
20
  "type": "module",
21
- "version": "16.5.16",
21
+ "types": "./index.d.ts",
22
+ "version": "16.5.18",
22
23
  "main": "./cjs/index.js",
23
- "module": "./cjs/index.js",
24
- "types": "./cjs/index.d.ts",
24
+ "module": "./index.js",
25
25
  "exports": {
26
- "./cjs/package.json": "./cjs/package.json",
27
- "./cjs/*": "./cjs/*.js",
28
26
  ".": {
29
27
  "module": {
30
28
  "types": "./index.d.ts",
@@ -53,6 +51,8 @@
53
51
  "default": "./bundle.js"
54
52
  }
55
53
  },
54
+ "./cjs/*": "./cjs/*.js",
55
+ "./cjs/package.json": "./cjs/package.json",
56
56
  "./create": {
57
57
  "module": {
58
58
  "types": "./create/index.d.ts",
@@ -127,7 +127,7 @@
127
127
  "default": "./packageDetect.js"
128
128
  }
129
129
  },
130
- "./packageInfo.js": {
130
+ "./packageInfo": {
131
131
  "module": {
132
132
  "types": "./packageInfo.d.ts",
133
133
  "default": "./packageInfo.js"
@@ -141,7 +141,7 @@
141
141
  "default": "./packageInfo.js"
142
142
  }
143
143
  },
144
- "./packageInfo": {
144
+ "./packageInfo.js": {
145
145
  "module": {
146
146
  "types": "./packageInfo.d.ts",
147
147
  "default": "./packageInfo.js"
@@ -283,9 +283,9 @@
283
283
  }
284
284
  },
285
285
  "dependencies": {
286
- "@pezkuwi/types": "16.5.15",
287
- "@pezkuwi/types-codec": "16.5.15",
288
- "@pezkuwi/util": "14.0.12",
286
+ "@pezkuwi/types": "16.5.9",
287
+ "@pezkuwi/types-codec": "16.5.9",
288
+ "@pezkuwi/util": "14.0.11",
289
289
  "tslib": "^2.8.1"
290
290
  }
291
291
  }