@typemove/move 1.2.4 → 1.2.5

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 (85) hide show
  1. package/dist/cjs/abstract-codegen.d.ts +50 -0
  2. package/dist/cjs/abstract-codegen.d.ts.map +1 -0
  3. package/dist/cjs/abstract-codegen.js +417 -0
  4. package/dist/cjs/abstract-codegen.js.map +1 -0
  5. package/dist/cjs/abstract-move-coder.d.ts +26 -0
  6. package/dist/cjs/abstract-move-coder.d.ts.map +1 -0
  7. package/dist/cjs/abstract-move-coder.js +260 -0
  8. package/dist/cjs/abstract-move-coder.js.map +1 -0
  9. package/dist/cjs/account.d.ts +16 -0
  10. package/dist/cjs/account.d.ts.map +1 -0
  11. package/dist/cjs/account.js +76 -0
  12. package/dist/cjs/account.js.map +1 -0
  13. package/dist/cjs/chain-adapter.d.ts +13 -0
  14. package/dist/cjs/chain-adapter.d.ts.map +1 -0
  15. package/dist/cjs/chain-adapter.js +7 -0
  16. package/dist/cjs/chain-adapter.js.map +1 -0
  17. package/dist/cjs/index.d.ts +8 -0
  18. package/dist/cjs/index.d.ts.map +1 -0
  19. package/dist/cjs/index.js +11 -0
  20. package/dist/cjs/index.js.map +1 -0
  21. package/dist/cjs/internal-models.d.ts +36 -0
  22. package/dist/cjs/internal-models.d.ts.map +1 -0
  23. package/dist/cjs/internal-models.js +10 -0
  24. package/dist/cjs/internal-models.js.map +1 -0
  25. package/dist/cjs/ts-type.test.d.ts +2 -0
  26. package/dist/cjs/ts-type.test.d.ts.map +1 -0
  27. package/dist/cjs/ts-type.test.js.map +1 -0
  28. package/dist/cjs/types.d.ts +46 -0
  29. package/dist/cjs/types.d.ts.map +1 -0
  30. package/dist/cjs/types.js +250 -0
  31. package/dist/cjs/types.js.map +1 -0
  32. package/dist/cjs/utils.d.ts +17 -0
  33. package/dist/cjs/utils.d.ts.map +1 -0
  34. package/dist/cjs/utils.js +82 -0
  35. package/dist/cjs/utils.js.map +1 -0
  36. package/dist/cjs/utils.test.d.ts +2 -0
  37. package/dist/cjs/utils.test.d.ts.map +1 -0
  38. package/dist/cjs/utils.test.js.map +1 -0
  39. package/dist/esm/abstract-codegen.d.ts +50 -0
  40. package/dist/esm/abstract-codegen.d.ts.map +1 -0
  41. package/dist/esm/abstract-codegen.js +411 -0
  42. package/dist/esm/abstract-codegen.js.map +1 -0
  43. package/dist/esm/abstract-move-coder.d.ts +26 -0
  44. package/dist/esm/abstract-move-coder.d.ts.map +1 -0
  45. package/dist/esm/abstract-move-coder.js +256 -0
  46. package/dist/esm/abstract-move-coder.js.map +1 -0
  47. package/dist/esm/account.d.ts +16 -0
  48. package/dist/esm/account.d.ts.map +1 -0
  49. package/dist/esm/account.js +71 -0
  50. package/dist/esm/account.js.map +1 -0
  51. package/dist/esm/chain-adapter.d.ts +13 -0
  52. package/dist/esm/chain-adapter.d.ts.map +1 -0
  53. package/dist/esm/chain-adapter.js +3 -0
  54. package/dist/esm/chain-adapter.js.map +1 -0
  55. package/dist/esm/index.d.ts +8 -0
  56. package/dist/esm/index.d.ts.map +1 -0
  57. package/dist/esm/index.js +8 -0
  58. package/dist/esm/index.js.map +1 -0
  59. package/dist/esm/internal-models.d.ts +36 -0
  60. package/dist/esm/internal-models.d.ts.map +1 -0
  61. package/dist/esm/internal-models.js +7 -0
  62. package/dist/esm/internal-models.js.map +1 -0
  63. package/dist/esm/ts-type.test.d.ts +2 -0
  64. package/dist/esm/ts-type.test.d.ts.map +1 -0
  65. package/dist/esm/ts-type.test.js.map +1 -0
  66. package/dist/esm/types.d.ts +46 -0
  67. package/dist/esm/types.d.ts.map +1 -0
  68. package/dist/esm/types.js +243 -0
  69. package/dist/esm/types.js.map +1 -0
  70. package/dist/esm/utils.d.ts +17 -0
  71. package/dist/esm/utils.d.ts.map +1 -0
  72. package/dist/esm/utils.js +69 -0
  73. package/dist/esm/utils.js.map +1 -0
  74. package/dist/esm/utils.test.d.ts +2 -0
  75. package/dist/esm/utils.test.d.ts.map +1 -0
  76. package/dist/esm/utils.test.js.map +1 -0
  77. package/package.json +1 -1
  78. package/src/abstract-codegen.ts +504 -0
  79. package/src/abstract-move-coder.ts +288 -0
  80. package/src/account.ts +93 -0
  81. package/src/chain-adapter.ts +26 -0
  82. package/src/index.ts +7 -0
  83. package/src/internal-models.ts +41 -0
  84. package/src/types.ts +284 -0
  85. package/src/utils.ts +91 -0
package/src/types.ts ADDED
@@ -0,0 +1,284 @@
1
+ import { accountTypeString, SPLITTER, VECTOR_STR } from './utils.js'
2
+
3
+ export type DecodedStruct<B, T> = B & {
4
+ /**
5
+ * decoded data using ABI, undefined if there is decoding error, usually because the ABI/data mismatch
6
+ */
7
+ data_decoded: T
8
+ type_arguments: string[]
9
+ }
10
+
11
+ export class TypeDescriptor<T = any> {
12
+ qname: string
13
+ reference: boolean
14
+ mutable: boolean
15
+ typeArgs: TypeDescriptor[]
16
+
17
+ constructor(symbol: string, typeParams?: TypeDescriptor[]) {
18
+ this.qname = symbol
19
+ this.reference = false
20
+ this.mutable = false
21
+ this.typeArgs = typeParams || []
22
+ }
23
+
24
+ apply(...typeArgs: TypeDescriptor[]): TypeDescriptor {
25
+ const newObj = this.clone()
26
+ newObj.typeArgs = typeArgs
27
+ return newObj
28
+ }
29
+
30
+ clone(): this {
31
+ const newObj = new TypeDescriptor(this.qname, this.typeArgs)
32
+ newObj.reference = this.reference
33
+ newObj.mutable = this.mutable
34
+ return newObj as any
35
+ }
36
+
37
+ // compare qname without consider case for system type
38
+ compareQname(t: TypeDescriptor): boolean {
39
+ let t1 = this.qname
40
+ if (BUILTIN_TYPES_SET.has(this.qname.toLowerCase())) {
41
+ t1 = this.qname.toLowerCase()
42
+ }
43
+ let t2 = t.qname
44
+ if (BUILTIN_TYPES_SET.has(t.qname.toLowerCase())) {
45
+ t2 = t.qname
46
+ }
47
+ return t1 === t2
48
+ }
49
+
50
+ getSignature(): string {
51
+ if (this.typeArgs.length > 0) {
52
+ return this.qname + '<' + this.typeArgs.map((t) => t.getSignature()).join(', ') + '>'
53
+ }
54
+ return this.qname
55
+ }
56
+
57
+ // Make U8, U16, etc => u8, u16
58
+ getNormalizedSignature(): string {
59
+ let qname = this.qname
60
+ switch (qname) {
61
+ case 'U8':
62
+ case 'U16':
63
+ case 'U32':
64
+ case 'U64':
65
+ case 'U128':
66
+ case 'U256':
67
+ case 'Vector':
68
+ case 'Bool':
69
+ case 'Address':
70
+ qname = qname.toLowerCase()
71
+ }
72
+
73
+ if (this.typeArgs.length > 0) {
74
+ return qname + '<' + this.typeArgs.map((t) => t.getNormalizedSignature()).join(', ') + '>'
75
+ }
76
+ return qname
77
+ }
78
+
79
+ // Replace T0, T1 with more concrete type
80
+ applyTypeArgs(ctx: Map<string, TypeDescriptor>): TypeDescriptor {
81
+ const replace = ctx.get(this.qname)
82
+ if (replace) {
83
+ return replace
84
+ }
85
+ if (ctx.size === 0 || this.typeArgs.length === 0) {
86
+ return this
87
+ }
88
+
89
+ const typeArgs: TypeDescriptor[] = []
90
+ for (const arg of this.typeArgs) {
91
+ const replace = ctx.get(arg.qname)
92
+ if (replace) {
93
+ typeArgs.push(replace)
94
+ } else {
95
+ typeArgs.push(arg.applyTypeArgs(ctx))
96
+ }
97
+ }
98
+ return new TypeDescriptor(this.qname, typeArgs)
99
+ }
100
+
101
+ // all depended types including itself, not include system type
102
+ dependedTypes(): string[] {
103
+ if (this.qname.startsWith('&')) {
104
+ console.error('Not expected &')
105
+ return []
106
+ }
107
+
108
+ if (this.isVector()) {
109
+ return this.typeArgs[0].dependedTypes()
110
+ }
111
+
112
+ if (BUILTIN_TYPES_SET.has(this.qname.toLowerCase())) {
113
+ return []
114
+ }
115
+ switch (this.qname) {
116
+ case 'signer':
117
+ case '0x1::string::String':
118
+ return []
119
+ }
120
+
121
+ // Type parameters are not depended
122
+ if (this.qname.indexOf(SPLITTER) == -1) {
123
+ if (this.qname.startsWith('T')) {
124
+ return []
125
+ }
126
+ }
127
+
128
+ const types = new Set<string>()
129
+ for (const param of this.typeArgs) {
130
+ param.dependedTypes().forEach((t) => types.add(t))
131
+ }
132
+
133
+ types.add(this.qname)
134
+
135
+ return Array.from(types)
136
+ }
137
+
138
+ isVector(): boolean {
139
+ return this.qname.toLowerCase() === VECTOR_STR
140
+ }
141
+
142
+ existAnyType(): boolean {
143
+ if (this.qname === 'any') {
144
+ return true
145
+ }
146
+ for (const param of this.typeArgs) {
147
+ if (param.existAnyType()) {
148
+ return true
149
+ }
150
+ }
151
+ return false
152
+ }
153
+
154
+ name(): string {
155
+ const parts = this.qname.split(SPLITTER)
156
+ return parts[parts.length - 1]
157
+ }
158
+ }
159
+
160
+ export function parseMoveType(type: string): TypeDescriptor {
161
+ const stack: TypeDescriptor[] = [new TypeDescriptor('')]
162
+ let buffer = []
163
+
164
+ // xxx:asdf<g1<a,<c,d>>, b, g2<a,b>, e>
165
+ for (let i = 0; i < type.length; i++) {
166
+ const ch = type[i]
167
+ if (ch === ' ') {
168
+ continue
169
+ }
170
+ if (ch === '<') {
171
+ // const symbol = type.slice(symbolStart, i)
172
+ // symbolStart =
173
+ const symbol = buffer.join('')
174
+ buffer = []
175
+ stack[stack.length - 1].qname = symbol
176
+ adjustType(stack[stack.length - 1])
177
+ stack.push(new TypeDescriptor(''))
178
+ continue
179
+ }
180
+ if (ch === '>' || ch === ',') {
181
+ const typeParam = stack.pop()
182
+ if (!typeParam) {
183
+ throw Error('Unexpected stack size')
184
+ }
185
+ if (buffer.length > 0) {
186
+ typeParam.qname = buffer.join('')
187
+ buffer = []
188
+ }
189
+ adjustType(typeParam)
190
+ stack[stack.length - 1].typeArgs.push(typeParam)
191
+ if (ch === ',') {
192
+ stack.push(new TypeDescriptor(''))
193
+ }
194
+ continue
195
+ }
196
+ buffer.push(ch)
197
+ }
198
+
199
+ if (buffer.length > 0) {
200
+ stack[stack.length - 1].qname = buffer.join('')
201
+ }
202
+ adjustType(stack[stack.length - 1])
203
+
204
+ const res = stack.pop()
205
+ if (!res || stack.length > 0) {
206
+ throw Error('Unexpected stack size')
207
+ }
208
+ return res
209
+ }
210
+
211
+ function adjustType(type: TypeDescriptor) {
212
+ if (type.qname.startsWith('&')) {
213
+ type.reference = true
214
+ type.qname = type.qname.slice(1)
215
+ }
216
+ if (type.qname.startsWith('mut')) {
217
+ type.mutable = true
218
+ type.qname = type.qname.slice(3)
219
+ }
220
+ const parts = type.qname.split(SPLITTER)
221
+ if (parts.length > 1) {
222
+ const [account, module, name] = parts
223
+ type.qname = [accountTypeString(account), module, name].join(SPLITTER)
224
+ }
225
+ }
226
+
227
+ export const ANY_TYPE = new TypeDescriptor<any>('any')
228
+
229
+ export function vectorType<T>(t: TypeDescriptor<T> = ANY_TYPE): TypeDescriptor<T[]> {
230
+ return BUILTIN_TYPES.VECTOR_TYPE_ANY.apply(t)
231
+ }
232
+
233
+ export const BUILTIN_TYPES = {
234
+ ADDRESS_TYPE: new TypeDescriptor<string>('address'),
235
+ // export const Address = new TypeDescriptor<string>("Address")
236
+
237
+ VECTOR_TYPE_ANY: new TypeDescriptor<any[]>('vector'),
238
+ VECTOR_TYPE: vectorType,
239
+
240
+ BOOL_TYPE: new TypeDescriptor<number>('bool'),
241
+
242
+ U8_TYPE: new TypeDescriptor<number>('u8'),
243
+ // export const U8 = new TypeDescriptor<number>("U8")
244
+ U16_TYPE: new TypeDescriptor<number>('u16'),
245
+ // export const U16 = new TypeDescriptor<number>("U16")
246
+ U32_TYPE: new TypeDescriptor<number>('u32'),
247
+ // export const U32 = new TypeDescriptor<number>("U32")
248
+ U64_TYPE: new TypeDescriptor<number>('u64'),
249
+ // export const U64 = new TypeDescriptor<number>("U64")
250
+ U128_TYPE: new TypeDescriptor<number>('u128'),
251
+ // export const U128 = new TypeDescriptor<number>("U128")
252
+ U256_TYPE: new TypeDescriptor<number>('u256')
253
+ // export const U256 = new TypeDescriptor<number>("U256")
254
+ }
255
+
256
+ const BUILTIN_TYPES_SET = new Set(
257
+ Object.values(BUILTIN_TYPES).flatMap((t) => {
258
+ if (typeof t === 'object') {
259
+ return [t.qname.toLowerCase()]
260
+ }
261
+ return []
262
+ })
263
+ )
264
+
265
+ /**
266
+ *
267
+ * @param matcher
268
+ * @param type
269
+ */
270
+ export function matchType(matcher: TypeDescriptor, type: TypeDescriptor): boolean {
271
+ if (matcher.qname === 'any') {
272
+ return true
273
+ }
274
+ if (!matcher.compareQname(type)) {
275
+ return false
276
+ }
277
+ for (const [idx, matcherTarg] of matcher.typeArgs.entries()) {
278
+ const targ = type.typeArgs[idx]
279
+ if (!matchType(matcherTarg, targ)) {
280
+ return false
281
+ }
282
+ }
283
+ return true
284
+ }
package/src/utils.ts ADDED
@@ -0,0 +1,91 @@
1
+ import { InternalMoveModule, InternalMoveStruct } from './internal-models.js'
2
+ import { camel as camelRadash } from 'radash'
3
+
4
+ export const SPLITTER = '::'
5
+
6
+ export const VECTOR_STR = 'vector'
7
+
8
+ export function isFrameworkAccount(account: string) {
9
+ const n = parseInt(account, 16)
10
+ if (Number.isNaN(n)) {
11
+ return false
12
+ }
13
+ return n >= 0 && n < 16
14
+ }
15
+
16
+ // strip any lead 0
17
+ export function accountTypeString(account: string): string {
18
+ const withoutPrefix = account.toLowerCase().replace(/^(0x)/, '')
19
+ return '0x' + withoutPrefix.replace(/^0*/, '')
20
+ }
21
+
22
+ const MOVE_ADDRESS_LENGTH = 32
23
+
24
+ function isHex(value: string): boolean {
25
+ return /^(0x|0X)?[a-fA-F0-9]+$/.test(value)
26
+ }
27
+
28
+ function getHexByteLength(value: string): number {
29
+ return /^(0x|0X)/.test(value) ? (value.length - 2) / 2 : value.length / 2
30
+ }
31
+
32
+ export function isValidMoveAddress(value: string): boolean {
33
+ return isHex(value) && getHexByteLength(value) <= MOVE_ADDRESS_LENGTH
34
+ }
35
+
36
+ // Get full address with 32 bytes
37
+ export function accountAddressString(account: string): string {
38
+ if (!isValidMoveAddress(account)) {
39
+ throw Error('Not valid move address')
40
+ }
41
+
42
+ const address = account.toLowerCase().replace(/^(0x)/, '')
43
+ return `0x${address.padStart(MOVE_ADDRESS_LENGTH * 2, '0')}`
44
+ }
45
+
46
+ const KEYWORDS = new Set([
47
+ 'package',
48
+ 'namespace',
49
+ 'volatile',
50
+ 'object',
51
+ 'string',
52
+ 'number',
53
+ 'bigint',
54
+ 'any',
55
+ 'new',
56
+ 'delete',
57
+ ])
58
+
59
+ export function normalizeToJSName(name: string) {
60
+ if (KEYWORDS.has(name)) {
61
+ return name + '_'
62
+ }
63
+ return name
64
+ }
65
+
66
+ export function moduleQnameForType(type: string): [string, string] {
67
+ const parts = type.split(SPLITTER).slice(0, 2)
68
+ return [parts[0], parts[1]]
69
+ }
70
+
71
+ export function moduleQname(module: { address: string; name: string }): string {
72
+ return accountTypeString(module.address) + SPLITTER + module.name
73
+ }
74
+
75
+ export function structQname(
76
+ module: InternalMoveModule,
77
+ struct: InternalMoveStruct
78
+ ): string {
79
+ return [accountTypeString(module.address), module.name, struct.name].join(
80
+ SPLITTER
81
+ )
82
+ }
83
+
84
+ export function upperFirst(str: string): string {
85
+ return str.charAt(0).toUpperCase() + str.slice(1)
86
+ }
87
+
88
+ export function camel(str: string): string {
89
+ const base = camelRadash(str)
90
+ return str.endsWith('_') ? base + '_' : base
91
+ }