@mysten/pas 0.1.4 → 0.2.0

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/CHANGELOG.md CHANGED
@@ -1,5 +1,22 @@
1
1
  # @mysten/pas
2
2
 
3
+ ## 0.2.0
4
+
5
+ ### Minor Changes
6
+
7
+ - bbf63cb: Add `typeTag` and `resolveTypeTag` methods to the generated `MoveStruct`, `MoveEnum`, and
8
+ `MoveTuple` classes.
9
+
10
+ - `typeTag(options?)` builds the type tag string for a generated type. `typeArguments` is the full
11
+ positional list of type arguments in Move declaration order; each entry is a type tag string,
12
+ another `typeTag()` result, or a BCS type (its name is used). Types with unfilled phantom
13
+ parameters require `typeArguments` at compile time, and argument arity is validated at runtime.
14
+ - `resolveTypeTag({ client, ... })` builds the tag, resolves MVR names through
15
+ `client.core.mvr.resolveType`, and returns the normalized address-only form suitable for queries
16
+ and comparisons against on-chain data.
17
+
18
+ - bbf63cb: Updated dependencies
19
+
3
20
  ## 0.1.4
4
21
 
5
22
  ### Patch Changes
@@ -1,12 +1,78 @@
1
1
  import { PASClientError } from "../../error.mjs";
2
- import { BcsEnum, BcsStruct, BcsTuple, bcs } from "@mysten/sui/bcs";
3
- import { normalizeSuiAddress } from "@mysten/sui/utils";
2
+ import { BcsEnum, BcsStruct, BcsTuple, TypeTagSerializer, bcs } from "@mysten/sui/bcs";
3
+ import { normalizeStructTag, normalizeSuiAddress } from "@mysten/sui/utils";
4
4
  import "@mysten/sui/transactions";
5
5
 
6
6
  //#region src/contracts/utils/index.ts
7
7
  const MOVE_STDLIB_ADDRESS = normalizeSuiAddress("0x1");
8
8
  const SUI_FRAMEWORK_ADDRESS = normalizeSuiAddress("0x2");
9
+ const HAS_PHANTOM_REGEX = /phantom [A-Za-z_$][A-Za-z0-9_$]*/;
10
+ function splitTopLevelTypeArgs(inner) {
11
+ const parts = [];
12
+ let depth = 0;
13
+ let current = "";
14
+ for (const char of inner) {
15
+ if (char === "," && depth === 0) {
16
+ parts.push(current.trim());
17
+ current = "";
18
+ continue;
19
+ }
20
+ if (char === "<") depth++;
21
+ if (char === ">") depth--;
22
+ current += char;
23
+ }
24
+ if (current) parts.push(current.trim());
25
+ return parts;
26
+ }
27
+ function buildTypeTag(name, options) {
28
+ const lt = name.indexOf("<");
29
+ const base = lt === -1 ? name : name.slice(0, lt);
30
+ if (base.split("::").length !== 3) throw new PASClientError(`${name} is not a top-level Move type`);
31
+ let result = name;
32
+ if (options?.typeArguments) {
33
+ const baked = lt === -1 ? [] : splitTopLevelTypeArgs(name.slice(lt + 1, -1));
34
+ const supplied = options.typeArguments.map((arg) => {
35
+ if (typeof arg === "string") return arg;
36
+ if (arg && typeof arg.serialize === "function" && typeof arg.name === "string") return arg.name;
37
+ throw new PASClientError(`Invalid type argument ${stringify(arg)}`);
38
+ });
39
+ if (supplied.length !== baked.length) throw new PASClientError(`Expected ${baked.length} type arguments for ${base}, got ${supplied.length}`);
40
+ result = supplied.length === 0 ? base : `${base}<${supplied.join(", ")}>`;
41
+ }
42
+ if (HAS_PHANTOM_REGEX.test(result)) throw new PASClientError(options?.typeArguments ? `A type argument contains an unfilled phantom parameter in ${result}` : `Missing type arguments for ${result}`);
43
+ if (options?.package) {
44
+ const [, ...rest] = result.split("::");
45
+ result = [options.package, ...rest].join("::");
46
+ }
47
+ if (!HAS_PHANTOM_REGEX.test(result) && !/[@/]/.test(result)) TypeTagSerializer.parseFromStr(result);
48
+ return result;
49
+ }
50
+ async function resolveBuiltTypeTag(name, options) {
51
+ const { client, ...rest } = options;
52
+ const { type } = await client.core.mvr.resolveType({ type: buildTypeTag(name, rest) });
53
+ return normalizeStructTag(type);
54
+ }
9
55
  var MoveStruct = class extends BcsStruct {
56
+ /**
57
+ * Build the type tag for this struct.
58
+ *
59
+ * `typeArguments` is the full positional list, in Move declaration order, and
60
+ * is required when the struct has unfilled phantom parameters. The result may
61
+ * contain MVR names: those are valid in transaction `typeArguments`, but for
62
+ * queries or comparisons against on-chain data use `resolveTypeTag` instead.
63
+ */
64
+ typeTag(...args) {
65
+ return buildTypeTag(this.name, args[0]);
66
+ }
67
+ /**
68
+ * Build the type tag for this struct, then resolve any MVR names through the
69
+ * client (using its configured overrides and the MVR API) and return the
70
+ * normalized, address-only form suitable for queries and comparisons against
71
+ * on-chain data.
72
+ */
73
+ async resolveTypeTag(options) {
74
+ return resolveBuiltTypeTag(this.name, options);
75
+ }
10
76
  async get({ objectId, ...options }) {
11
77
  const [res] = await this.getMany({
12
78
  ...options,
@@ -31,8 +97,31 @@ var MoveStruct = class extends BcsStruct {
31
97
  });
32
98
  }
33
99
  };
34
- var MoveEnum = class extends BcsEnum {};
35
- var MoveTuple = class extends BcsTuple {};
100
+ var MoveEnum = class extends BcsEnum {
101
+ /** Build the type tag for this enum. See `MoveStruct.typeTag` for semantics. */
102
+ typeTag(...args) {
103
+ return buildTypeTag(this.name, args[0]);
104
+ }
105
+ /** Build and resolve the type tag for this enum. See `MoveStruct.resolveTypeTag`. */
106
+ async resolveTypeTag(options) {
107
+ return resolveBuiltTypeTag(this.name, options);
108
+ }
109
+ };
110
+ var MoveTuple = class extends BcsTuple {
111
+ /** Build the type tag for this struct. See `MoveStruct.typeTag` for semantics. */
112
+ typeTag(...args) {
113
+ return buildTypeTag(this.name, args[0]);
114
+ }
115
+ /** Build and resolve the type tag for this struct. See `MoveStruct.resolveTypeTag`. */
116
+ async resolveTypeTag(options) {
117
+ return resolveBuiltTypeTag(this.name, options);
118
+ }
119
+ };
120
+ function stringify(val) {
121
+ if (typeof val === "object") return JSON.stringify(val, (_key, value) => typeof value === "bigint" ? value.toString() : value);
122
+ if (typeof val === "bigint") return val.toString();
123
+ return val;
124
+ }
36
125
 
37
126
  //#endregion
38
127
  export { MoveEnum, MoveStruct, MoveTuple };
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","names":[],"sources":["../../../src/contracts/utils/index.ts"],"sourcesContent":["import { PASClientError } from '../../error.js';\n\nimport {\n\tbcs,\n\ttype BcsType,\n\ttype TypeTag,\n\tTypeTagSerializer,\n\tBcsStruct,\n\tBcsEnum,\n\tBcsTuple,\n} from '@mysten/sui/bcs';\nimport { normalizeSuiAddress } from '@mysten/sui/utils';\nimport { type TransactionArgument, isArgument } from '@mysten/sui/transactions';\nimport { type ClientWithCoreApi, type SuiClientTypes } from '@mysten/sui/client';\n\nconst MOVE_STDLIB_ADDRESS = normalizeSuiAddress('0x1');\nconst SUI_FRAMEWORK_ADDRESS = normalizeSuiAddress('0x2');\n\nexport type RawTransactionArgument<T> = T | TransactionArgument;\n\nexport type GetOptions<Include extends Omit<SuiClientTypes.ObjectInclude, 'content'> = {}> =\n\tSuiClientTypes.GetObjectOptions<Include> & { client: ClientWithCoreApi };\n\nexport type GetManyOptions<Include extends Omit<SuiClientTypes.ObjectInclude, 'content'> = {}> =\n\tSuiClientTypes.GetObjectsOptions<Include> & { client: ClientWithCoreApi };\n\nexport function getPureBcsSchema(typeTag: string | TypeTag): BcsType<any> | null {\n\tconst parsedTag = typeof typeTag === 'string' ? TypeTagSerializer.parseFromStr(typeTag) : typeTag;\n\n\tif ('u8' in parsedTag) {\n\t\treturn bcs.U8;\n\t} else if ('u16' in parsedTag) {\n\t\treturn bcs.U16;\n\t} else if ('u32' in parsedTag) {\n\t\treturn bcs.U32;\n\t} else if ('u64' in parsedTag) {\n\t\treturn bcs.U64;\n\t} else if ('u128' in parsedTag) {\n\t\treturn bcs.U128;\n\t} else if ('u256' in parsedTag) {\n\t\treturn bcs.U256;\n\t} else if ('address' in parsedTag) {\n\t\treturn bcs.Address;\n\t} else if ('bool' in parsedTag) {\n\t\treturn bcs.Bool;\n\t} else if ('vector' in parsedTag) {\n\t\tconst type = getPureBcsSchema(parsedTag.vector);\n\t\treturn type ? bcs.vector(type) : null;\n\t} else if ('struct' in parsedTag) {\n\t\tconst structTag = parsedTag.struct;\n\t\tconst pkg = normalizeSuiAddress(structTag.address);\n\n\t\tif (pkg === MOVE_STDLIB_ADDRESS) {\n\t\t\tif (\n\t\t\t\t(structTag.module === 'ascii' || structTag.module === 'string') &&\n\t\t\t\tstructTag.name === 'String'\n\t\t\t) {\n\t\t\t\treturn bcs.String;\n\t\t\t}\n\n\t\t\tif (structTag.module === 'option' && structTag.name === 'Option') {\n\t\t\t\tconst inner = structTag.typeParams[0];\n\t\t\t\tconst type = inner ? getPureBcsSchema(inner) : null;\n\t\t\t\treturn type ? bcs.option(type) : null;\n\t\t\t}\n\t\t}\n\n\t\tif (\n\t\t\tpkg === SUI_FRAMEWORK_ADDRESS &&\n\t\t\tstructTag.module === 'object' &&\n\t\t\t(structTag.name === 'ID' || structTag.name === 'UID')\n\t\t) {\n\t\t\treturn bcs.Address;\n\t\t}\n\t}\n\n\treturn null;\n}\n\nexport function normalizeMoveArguments(\n\targs: unknown[] | object,\n\targTypes: readonly (string | null)[],\n\tparameterNames?: string[],\n) {\n\tconst argLen = Array.isArray(args) ? args.length : Object.keys(args).length;\n\tif (parameterNames && argLen !== parameterNames.length) {\n\t\tthrow new PASClientError(\n\t\t\t`Invalid number of arguments, expected ${parameterNames.length}, got ${argLen}`,\n\t\t);\n\t}\n\n\tconst normalizedArgs: TransactionArgument[] = [];\n\n\tlet index = 0;\n\tfor (const argType of argTypes) {\n\t\tif (argType === '0x2::clock::Clock') {\n\t\t\tnormalizedArgs.push((tx) => tx.object.clock());\n\t\t\tcontinue;\n\t\t}\n\n\t\tif (argType === '0x2::random::Random') {\n\t\t\tnormalizedArgs.push((tx) => tx.object.random());\n\t\t\tcontinue;\n\t\t}\n\n\t\tif (argType === '0x2::deny_list::DenyList') {\n\t\t\tnormalizedArgs.push((tx) => tx.object.denyList());\n\t\t\tcontinue;\n\t\t}\n\n\t\tif (argType === '0x3::sui_system::SuiSystemState') {\n\t\t\tnormalizedArgs.push((tx) => tx.object.system());\n\t\t\tcontinue;\n\t\t}\n\n\t\tlet arg;\n\t\tif (Array.isArray(args)) {\n\t\t\tif (index >= args.length) {\n\t\t\t\tthrow new PASClientError(\n\t\t\t\t\t`Invalid number of arguments, expected at least ${index + 1}, got ${args.length}`,\n\t\t\t\t);\n\t\t\t}\n\t\t\targ = args[index];\n\t\t} else {\n\t\t\tif (!parameterNames) {\n\t\t\t\tthrow new PASClientError(`Expected arguments to be passed as an array`);\n\t\t\t}\n\t\t\tconst name = parameterNames[index];\n\t\t\targ = args[name as keyof typeof args];\n\n\t\t\tif (arg === undefined) {\n\t\t\t\tthrow new PASClientError(`Parameter ${name} is required`);\n\t\t\t}\n\t\t}\n\n\t\tindex += 1;\n\n\t\tif (typeof arg === 'function' || isArgument(arg)) {\n\t\t\tnormalizedArgs.push(arg as TransactionArgument);\n\t\t\tcontinue;\n\t\t}\n\n\t\tconst bcsType = argType === null ? null : getPureBcsSchema(argType);\n\n\t\tif (bcsType) {\n\t\t\tconst bytes = bcsType.serialize(arg as never);\n\t\t\tnormalizedArgs.push((tx) => tx.pure(bytes));\n\t\t\tcontinue;\n\t\t}\n\n\t\tif (typeof arg === 'string') {\n\t\t\tnormalizedArgs.push((tx) => tx.object(arg));\n\t\t\tcontinue;\n\t\t}\n\n\t\tthrow new PASClientError(`Invalid argument ${stringify(arg)} for type ${argType}`);\n\t}\n\n\treturn normalizedArgs;\n}\n\nexport class MoveStruct<\n\tT extends Record<string, BcsType<any>>,\n\tconst Name extends string = string,\n> extends BcsStruct<T, Name> {\n\tasync get<Include extends Omit<SuiClientTypes.ObjectInclude, 'content' | 'json'> = {}>({\n\t\tobjectId,\n\t\t...options\n\t}: GetOptions<Include>): Promise<\n\t\tSuiClientTypes.Object<Include & { content: true; json: true }> & {\n\t\t\tjson: BcsStruct<T>['$inferType'];\n\t\t}\n\t> {\n\t\tconst [res] = await this.getMany<Include>({\n\t\t\t...options,\n\t\t\tobjectIds: [objectId],\n\t\t});\n\n\t\tif (!res) {\n\t\t\tthrow new PASClientError(`No object found for id ${objectId}`);\n\t\t}\n\n\t\treturn res;\n\t}\n\n\tasync getMany<Include extends Omit<SuiClientTypes.ObjectInclude, 'content' | 'json'> = {}>({\n\t\tclient,\n\t\t...options\n\t}: GetManyOptions<Include>): Promise<\n\t\tArray<\n\t\t\tSuiClientTypes.Object<Include & { content: true; json: true }> & {\n\t\t\t\tjson: BcsStruct<T>['$inferType'];\n\t\t\t}\n\t\t>\n\t> {\n\t\tconst response = (await client.core.getObjects({\n\t\t\t...options,\n\t\t\tinclude: {\n\t\t\t\t...options.include,\n\t\t\t\tcontent: true,\n\t\t\t},\n\t\t})) as SuiClientTypes.GetObjectsResponse<Include & { content: true }>;\n\n\t\treturn response.objects.map((obj) => {\n\t\t\tif (obj instanceof Error) {\n\t\t\t\tthrow obj;\n\t\t\t}\n\n\t\t\treturn {\n\t\t\t\t...obj,\n\t\t\t\tjson: this.parse(obj.content),\n\t\t\t};\n\t\t});\n\t}\n}\n\nexport class MoveEnum<\n\tT extends Record<string, BcsType<any> | null>,\n\tconst Name extends string,\n> extends BcsEnum<T, Name> {}\n\nexport class MoveTuple<\n\tconst T extends readonly BcsType<any>[],\n\tconst Name extends string,\n> extends BcsTuple<T, Name> {}\n\nfunction stringify(val: unknown) {\n\tif (typeof val === 'object') {\n\t\treturn JSON.stringify(val, (val: unknown) => val);\n\t}\n\tif (typeof val === 'bigint') {\n\t\treturn val.toString();\n\t}\n\n\treturn val;\n}\n"],"mappings":";;;;;;AAeA,MAAM,sBAAsB,oBAAoB,MAAM;AACtD,MAAM,wBAAwB,oBAAoB,MAAM;AAiJxD,IAAa,aAAb,cAGU,UAAmB;CAC5B,MAAM,IAAiF,EACtF,UACA,GAAG,WAKF;EACD,MAAM,CAAC,OAAO,MAAM,KAAK,QAAiB;GACzC,GAAG;GACH,WAAW,CAAC,SAAS;GACrB,CAAC;AAEF,MAAI,CAAC,IACJ,OAAM,IAAI,eAAe,0BAA0B,WAAW;AAG/D,SAAO;;CAGR,MAAM,QAAqF,EAC1F,QACA,GAAG,WAOF;AASD,UARkB,MAAM,OAAO,KAAK,WAAW;GAC9C,GAAG;GACH,SAAS;IACR,GAAG,QAAQ;IACX,SAAS;IACT;GACD,CAAC,EAEc,QAAQ,KAAK,QAAQ;AACpC,OAAI,eAAe,MAClB,OAAM;AAGP,UAAO;IACN,GAAG;IACH,MAAM,KAAK,MAAM,IAAI,QAAQ;IAC7B;IACA;;;AAIJ,IAAa,WAAb,cAGU,QAAiB;AAE3B,IAAa,YAAb,cAGU,SAAkB"}
1
+ {"version":3,"file":"index.mjs","names":[],"sources":["../../../src/contracts/utils/index.ts"],"sourcesContent":["import { PASClientError } from '../../error.js';\n\nimport {\n\tbcs,\n\ttype BcsType,\n\ttype TypeTag,\n\tTypeTagSerializer,\n\tBcsStruct,\n\tBcsEnum,\n\tBcsTuple,\n} from '@mysten/sui/bcs';\nimport { normalizeStructTag, normalizeSuiAddress } from '@mysten/sui/utils';\nimport { type TransactionArgument, isArgument } from '@mysten/sui/transactions';\nimport { type ClientWithCoreApi, type SuiClientTypes } from '@mysten/sui/client';\n\nconst MOVE_STDLIB_ADDRESS = normalizeSuiAddress('0x1');\nconst SUI_FRAMEWORK_ADDRESS = normalizeSuiAddress('0x2');\n\nexport type RawTransactionArgument<T> = T | TransactionArgument;\n\nexport type GetOptions<Include extends Omit<SuiClientTypes.ObjectInclude, 'content'> = {}> =\n\tSuiClientTypes.GetObjectOptions<Include> & { client: ClientWithCoreApi };\n\nexport type GetManyOptions<Include extends Omit<SuiClientTypes.ObjectInclude, 'content'> = {}> =\n\tSuiClientTypes.GetObjectsOptions<Include> & { client: ClientWithCoreApi };\n\nexport function getPureBcsSchema(typeTag: string | TypeTag): BcsType<any> | null {\n\tconst parsedTag = typeof typeTag === 'string' ? TypeTagSerializer.parseFromStr(typeTag) : typeTag;\n\n\tif ('u8' in parsedTag) {\n\t\treturn bcs.U8;\n\t} else if ('u16' in parsedTag) {\n\t\treturn bcs.U16;\n\t} else if ('u32' in parsedTag) {\n\t\treturn bcs.U32;\n\t} else if ('u64' in parsedTag) {\n\t\treturn bcs.U64;\n\t} else if ('u128' in parsedTag) {\n\t\treturn bcs.U128;\n\t} else if ('u256' in parsedTag) {\n\t\treturn bcs.U256;\n\t} else if ('address' in parsedTag) {\n\t\treturn bcs.Address;\n\t} else if ('bool' in parsedTag) {\n\t\treturn bcs.Bool;\n\t} else if ('vector' in parsedTag) {\n\t\tconst type = getPureBcsSchema(parsedTag.vector);\n\t\treturn type ? bcs.vector(type) : null;\n\t} else if ('struct' in parsedTag) {\n\t\tconst structTag = parsedTag.struct;\n\t\tconst pkg = normalizeSuiAddress(structTag.address);\n\n\t\tif (pkg === MOVE_STDLIB_ADDRESS) {\n\t\t\tif (\n\t\t\t\t(structTag.module === 'ascii' || structTag.module === 'string') &&\n\t\t\t\tstructTag.name === 'String'\n\t\t\t) {\n\t\t\t\treturn bcs.String;\n\t\t\t}\n\n\t\t\tif (structTag.module === 'option' && structTag.name === 'Option') {\n\t\t\t\tconst inner = structTag.typeParams[0];\n\t\t\t\tconst type = inner ? getPureBcsSchema(inner) : null;\n\t\t\t\treturn type ? bcs.option(type) : null;\n\t\t\t}\n\t\t}\n\n\t\tif (\n\t\t\tpkg === SUI_FRAMEWORK_ADDRESS &&\n\t\t\tstructTag.module === 'object' &&\n\t\t\t(structTag.name === 'ID' || structTag.name === 'UID')\n\t\t) {\n\t\t\treturn bcs.Address;\n\t\t}\n\t}\n\n\treturn null;\n}\n\nexport function normalizeMoveArguments(\n\targs: unknown[] | object,\n\targTypes: readonly (string | null)[],\n\tparameterNames?: string[],\n) {\n\tconst argLen = Array.isArray(args) ? args.length : Object.keys(args).length;\n\tif (parameterNames && argLen !== parameterNames.length) {\n\t\tthrow new PASClientError(\n\t\t\t`Invalid number of arguments, expected ${parameterNames.length}, got ${argLen}`,\n\t\t);\n\t}\n\n\tconst normalizedArgs: TransactionArgument[] = [];\n\n\tlet index = 0;\n\tfor (const argType of argTypes) {\n\t\tif (argType === '0x2::clock::Clock') {\n\t\t\tnormalizedArgs.push((tx) => tx.object.clock());\n\t\t\tcontinue;\n\t\t}\n\n\t\tif (argType === '0x2::random::Random') {\n\t\t\tnormalizedArgs.push((tx) => tx.object.random());\n\t\t\tcontinue;\n\t\t}\n\n\t\tif (argType === '0x2::deny_list::DenyList') {\n\t\t\tnormalizedArgs.push((tx) => tx.object.denyList());\n\t\t\tcontinue;\n\t\t}\n\n\t\tif (argType === '0x3::sui_system::SuiSystemState') {\n\t\t\tnormalizedArgs.push((tx) => tx.object.system());\n\t\t\tcontinue;\n\t\t}\n\n\t\tlet arg;\n\t\tif (Array.isArray(args)) {\n\t\t\tif (index >= args.length) {\n\t\t\t\tthrow new PASClientError(\n\t\t\t\t\t`Invalid number of arguments, expected at least ${index + 1}, got ${args.length}`,\n\t\t\t\t);\n\t\t\t}\n\t\t\targ = args[index];\n\t\t} else {\n\t\t\tif (!parameterNames) {\n\t\t\t\tthrow new PASClientError(`Expected arguments to be passed as an array`);\n\t\t\t}\n\t\t\tconst name = parameterNames[index];\n\t\t\targ = args[name as keyof typeof args];\n\n\t\t\tif (arg === undefined) {\n\t\t\t\tthrow new PASClientError(`Parameter ${name} is required`);\n\t\t\t}\n\t\t}\n\n\t\tindex += 1;\n\n\t\tif (typeof arg === 'function' || isArgument(arg)) {\n\t\t\tnormalizedArgs.push(arg as TransactionArgument);\n\t\t\tcontinue;\n\t\t}\n\n\t\tconst bcsType = argType === null ? null : getPureBcsSchema(argType);\n\n\t\tif (bcsType) {\n\t\t\tconst bytes = bcsType.serialize(arg as never);\n\t\t\tnormalizedArgs.push((tx) => tx.pure(bytes));\n\t\t\tcontinue;\n\t\t}\n\n\t\tif (typeof arg === 'string') {\n\t\t\tnormalizedArgs.push((tx) => tx.object(arg));\n\t\t\tcontinue;\n\t\t}\n\n\t\tthrow new PASClientError(`Invalid argument ${stringify(arg)} for type ${argType}`);\n\t}\n\n\treturn normalizedArgs;\n}\n\n/* -------------------------- Move type tags -------------------------- */\n\n/** A type argument: a type tag string, or a BCS type whose name is a Move type. */\nexport type TypeArgument = string | BcsType<any>;\n\nexport interface TypeTagOptions {\n\tpackage?: string;\n\ttypeArguments?: readonly TypeArgument[];\n}\n\n/**\n * `typeArguments` is required when the type's name contains unfilled\n * `phantom X` parameters (at any depth). Everything else — argument arity,\n * position contents, and tag validity — is validated at runtime.\n */\ntype TypeTagParams<Name extends string> = Name extends `${string}phantom ${string}`\n\t? [options: TypeTagOptions & { typeArguments: readonly TypeArgument[] }]\n\t: [options?: TypeTagOptions];\n\ntype ResolveTypeTagOptions<Name extends string> = {\n\tclient: ClientWithCoreApi;\n} & (Name extends `${string}phantom ${string}`\n\t? TypeTagOptions & { typeArguments: readonly TypeArgument[] }\n\t: TypeTagOptions);\n\nconst HAS_PHANTOM_REGEX = /phantom [A-Za-z_$][A-Za-z0-9_$]*/;\n\nfunction splitTopLevelTypeArgs(inner: string): string[] {\n\tconst parts: string[] = [];\n\tlet depth = 0;\n\tlet current = '';\n\tfor (const char of inner) {\n\t\tif (char === ',' && depth === 0) {\n\t\t\tparts.push(current.trim());\n\t\t\tcurrent = '';\n\t\t\tcontinue;\n\t\t}\n\t\tif (char === '<') depth++;\n\t\tif (char === '>') depth--;\n\t\tcurrent += char;\n\t}\n\tif (current) parts.push(current.trim());\n\treturn parts;\n}\n\nfunction buildTypeTag(name: string, options: TypeTagOptions | undefined): string {\n\tconst lt = name.indexOf('<');\n\tconst base = lt === -1 ? name : name.slice(0, lt);\n\n\tif (base.split('::').length !== 3) {\n\t\tthrow new PASClientError(`${name} is not a top-level Move type`);\n\t}\n\n\tlet result = name;\n\n\tif (options?.typeArguments) {\n\t\tconst baked = lt === -1 ? [] : splitTopLevelTypeArgs(name.slice(lt + 1, -1));\n\t\tconst supplied = options.typeArguments.map((arg) => {\n\t\t\tif (typeof arg === 'string') {\n\t\t\t\treturn arg;\n\t\t\t}\n\t\t\tif (arg && typeof arg.serialize === 'function' && typeof arg.name === 'string') {\n\t\t\t\treturn arg.name;\n\t\t\t}\n\t\t\tthrow new PASClientError(`Invalid type argument ${stringify(arg)}`);\n\t\t});\n\n\t\tif (supplied.length !== baked.length) {\n\t\t\tthrow new PASClientError(\n\t\t\t\t`Expected ${baked.length} type arguments for ${base}, got ${supplied.length}`,\n\t\t\t);\n\t\t}\n\n\t\tresult = supplied.length === 0 ? base : `${base}<${supplied.join(', ')}>`;\n\t}\n\n\tif (HAS_PHANTOM_REGEX.test(result)) {\n\t\tthrow new PASClientError(\n\t\t\toptions?.typeArguments\n\t\t\t\t? `A type argument contains an unfilled phantom parameter in ${result}`\n\t\t\t\t: `Missing type arguments for ${result}`,\n\t\t);\n\t}\n\n\tif (options?.package) {\n\t\tconst [, ...rest] = result.split('::');\n\t\tresult = [options.package, ...rest].join('::');\n\t}\n\n\t// fully validate address-only tags (MVR names can't be parsed as type tags)\n\tif (!HAS_PHANTOM_REGEX.test(result) && !/[@/]/.test(result)) {\n\t\tTypeTagSerializer.parseFromStr(result);\n\t}\n\n\treturn result;\n}\n\nasync function resolveBuiltTypeTag(\n\tname: string,\n\toptions: { client: ClientWithCoreApi } & TypeTagOptions,\n): Promise<string> {\n\tconst { client, ...rest } = options;\n\tconst { type } = await client.core.mvr.resolveType({\n\t\ttype: buildTypeTag(name, rest),\n\t});\n\treturn normalizeStructTag(type);\n}\n\nexport class MoveStruct<\n\tT extends Record<string, BcsType<any>>,\n\tconst Name extends string = string,\n> extends BcsStruct<T, Name> {\n\t/**\n\t * Build the type tag for this struct.\n\t *\n\t * `typeArguments` is the full positional list, in Move declaration order, and\n\t * is required when the struct has unfilled phantom parameters. The result may\n\t * contain MVR names: those are valid in transaction `typeArguments`, but for\n\t * queries or comparisons against on-chain data use `resolveTypeTag` instead.\n\t */\n\ttypeTag(...args: TypeTagParams<Name>): string {\n\t\treturn buildTypeTag(this.name, args[0] as TypeTagOptions | undefined);\n\t}\n\n\t/**\n\t * Build the type tag for this struct, then resolve any MVR names through the\n\t * client (using its configured overrides and the MVR API) and return the\n\t * normalized, address-only form suitable for queries and comparisons against\n\t * on-chain data.\n\t */\n\tasync resolveTypeTag(options: ResolveTypeTagOptions<Name>): Promise<string> {\n\t\treturn resolveBuiltTypeTag(\n\t\t\tthis.name,\n\t\t\toptions as { client: ClientWithCoreApi } & TypeTagOptions,\n\t\t);\n\t}\n\n\tasync get<Include extends Omit<SuiClientTypes.ObjectInclude, 'content' | 'json'> = {}>({\n\t\tobjectId,\n\t\t...options\n\t}: GetOptions<Include>): Promise<\n\t\tSuiClientTypes.Object<Include & { content: true; json: true }> & {\n\t\t\tjson: BcsStruct<T>['$inferType'];\n\t\t}\n\t> {\n\t\tconst [res] = await this.getMany<Include>({\n\t\t\t...options,\n\t\t\tobjectIds: [objectId],\n\t\t});\n\n\t\tif (!res) {\n\t\t\tthrow new PASClientError(`No object found for id ${objectId}`);\n\t\t}\n\n\t\treturn res;\n\t}\n\n\tasync getMany<Include extends Omit<SuiClientTypes.ObjectInclude, 'content' | 'json'> = {}>({\n\t\tclient,\n\t\t...options\n\t}: GetManyOptions<Include>): Promise<\n\t\tArray<\n\t\t\tSuiClientTypes.Object<Include & { content: true; json: true }> & {\n\t\t\t\tjson: BcsStruct<T>['$inferType'];\n\t\t\t}\n\t\t>\n\t> {\n\t\tconst response = (await client.core.getObjects({\n\t\t\t...options,\n\t\t\tinclude: {\n\t\t\t\t...options.include,\n\t\t\t\tcontent: true,\n\t\t\t},\n\t\t})) as SuiClientTypes.GetObjectsResponse<Include & { content: true }>;\n\n\t\treturn response.objects.map((obj) => {\n\t\t\tif (obj instanceof Error) {\n\t\t\t\tthrow obj;\n\t\t\t}\n\n\t\t\treturn {\n\t\t\t\t...obj,\n\t\t\t\tjson: this.parse(obj.content),\n\t\t\t};\n\t\t});\n\t}\n}\n\nexport class MoveEnum<\n\tT extends Record<string, BcsType<any> | null>,\n\tconst Name extends string,\n> extends BcsEnum<T, Name> {\n\t/** Build the type tag for this enum. See `MoveStruct.typeTag` for semantics. */\n\ttypeTag(...args: TypeTagParams<Name>): string {\n\t\treturn buildTypeTag(this.name, args[0] as TypeTagOptions | undefined);\n\t}\n\n\t/** Build and resolve the type tag for this enum. See `MoveStruct.resolveTypeTag`. */\n\tasync resolveTypeTag(options: ResolveTypeTagOptions<Name>): Promise<string> {\n\t\treturn resolveBuiltTypeTag(\n\t\t\tthis.name,\n\t\t\toptions as { client: ClientWithCoreApi } & TypeTagOptions,\n\t\t);\n\t}\n}\n\nexport class MoveTuple<\n\tconst T extends readonly BcsType<any>[],\n\tconst Name extends string,\n> extends BcsTuple<T, Name> {\n\t/** Build the type tag for this struct. See `MoveStruct.typeTag` for semantics. */\n\ttypeTag(...args: TypeTagParams<Name>): string {\n\t\treturn buildTypeTag(this.name, args[0] as TypeTagOptions | undefined);\n\t}\n\n\t/** Build and resolve the type tag for this struct. See `MoveStruct.resolveTypeTag`. */\n\tasync resolveTypeTag(options: ResolveTypeTagOptions<Name>): Promise<string> {\n\t\treturn resolveBuiltTypeTag(\n\t\t\tthis.name,\n\t\t\toptions as { client: ClientWithCoreApi } & TypeTagOptions,\n\t\t);\n\t}\n}\n\nfunction stringify(val: unknown) {\n\tif (typeof val === 'object') {\n\t\treturn JSON.stringify(val, (_key, value) =>\n\t\t\ttypeof value === 'bigint' ? value.toString() : value,\n\t\t);\n\t}\n\tif (typeof val === 'bigint') {\n\t\treturn val.toString();\n\t}\n\n\treturn val;\n}\n"],"mappings":";;;;;;AAeA,MAAM,sBAAsB,oBAAoB,MAAM;AACtD,MAAM,wBAAwB,oBAAoB,MAAM;AA0KxD,MAAM,oBAAoB;AAE1B,SAAS,sBAAsB,OAAyB;CACvD,MAAM,QAAkB,EAAE;CAC1B,IAAI,QAAQ;CACZ,IAAI,UAAU;AACd,MAAK,MAAM,QAAQ,OAAO;AACzB,MAAI,SAAS,OAAO,UAAU,GAAG;AAChC,SAAM,KAAK,QAAQ,MAAM,CAAC;AAC1B,aAAU;AACV;;AAED,MAAI,SAAS,IAAK;AAClB,MAAI,SAAS,IAAK;AAClB,aAAW;;AAEZ,KAAI,QAAS,OAAM,KAAK,QAAQ,MAAM,CAAC;AACvC,QAAO;;AAGR,SAAS,aAAa,MAAc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QAAO"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mysten/pas",
3
- "version": "0.1.4",
3
+ "version": "0.2.0",
4
4
  "private": false,
5
5
  "description": "Permissioned Assets Standard SDK",
6
6
  "license": "Apache-2.0",
@@ -32,14 +32,14 @@
32
32
  "url": "https://github.com/mystenlabs/ts-sdks/issues"
33
33
  },
34
34
  "devDependencies": {
35
- "@types/node": "^25.0.8",
35
+ "@types/node": "^25.9.3",
36
36
  "ts-retry-promise": "^0.8.1",
37
- "tsdown": "0.20.0-beta.1",
38
- "typescript": "^5.9.3",
39
- "vitest": "^4.0.17"
37
+ "tsdown": "0.22.2",
38
+ "typescript": "^6.0.3",
39
+ "vitest": "^4.1.8"
40
40
  },
41
41
  "peerDependencies": {
42
- "@mysten/sui": "^2.17.0"
42
+ "@mysten/sui": "^2.18.0"
43
43
  },
44
44
  "scripts": {
45
45
  "clean": "rm -rf tsconfig.tsbuildinfo ./dist",
@@ -9,7 +9,7 @@ import {
9
9
  BcsEnum,
10
10
  BcsTuple,
11
11
  } from '@mysten/sui/bcs';
12
- import { normalizeSuiAddress } from '@mysten/sui/utils';
12
+ import { normalizeStructTag, normalizeSuiAddress } from '@mysten/sui/utils';
13
13
  import { type TransactionArgument, isArgument } from '@mysten/sui/transactions';
14
14
  import { type ClientWithCoreApi, type SuiClientTypes } from '@mysten/sui/client';
15
15
 
@@ -159,10 +159,143 @@ export function normalizeMoveArguments(
159
159
  return normalizedArgs;
160
160
  }
161
161
 
162
+ /* -------------------------- Move type tags -------------------------- */
163
+
164
+ /** A type argument: a type tag string, or a BCS type whose name is a Move type. */
165
+ export type TypeArgument = string | BcsType<any>;
166
+
167
+ export interface TypeTagOptions {
168
+ package?: string;
169
+ typeArguments?: readonly TypeArgument[];
170
+ }
171
+
172
+ /**
173
+ * `typeArguments` is required when the type's name contains unfilled
174
+ * `phantom X` parameters (at any depth). Everything else — argument arity,
175
+ * position contents, and tag validity — is validated at runtime.
176
+ */
177
+ type TypeTagParams<Name extends string> = Name extends `${string}phantom ${string}`
178
+ ? [options: TypeTagOptions & { typeArguments: readonly TypeArgument[] }]
179
+ : [options?: TypeTagOptions];
180
+
181
+ type ResolveTypeTagOptions<Name extends string> = {
182
+ client: ClientWithCoreApi;
183
+ } & (Name extends `${string}phantom ${string}`
184
+ ? TypeTagOptions & { typeArguments: readonly TypeArgument[] }
185
+ : TypeTagOptions);
186
+
187
+ const HAS_PHANTOM_REGEX = /phantom [A-Za-z_$][A-Za-z0-9_$]*/;
188
+
189
+ function splitTopLevelTypeArgs(inner: string): string[] {
190
+ const parts: string[] = [];
191
+ let depth = 0;
192
+ let current = '';
193
+ for (const char of inner) {
194
+ if (char === ',' && depth === 0) {
195
+ parts.push(current.trim());
196
+ current = '';
197
+ continue;
198
+ }
199
+ if (char === '<') depth++;
200
+ if (char === '>') depth--;
201
+ current += char;
202
+ }
203
+ if (current) parts.push(current.trim());
204
+ return parts;
205
+ }
206
+
207
+ function buildTypeTag(name: string, options: TypeTagOptions | undefined): string {
208
+ const lt = name.indexOf('<');
209
+ const base = lt === -1 ? name : name.slice(0, lt);
210
+
211
+ if (base.split('::').length !== 3) {
212
+ throw new PASClientError(`${name} is not a top-level Move type`);
213
+ }
214
+
215
+ let result = name;
216
+
217
+ if (options?.typeArguments) {
218
+ const baked = lt === -1 ? [] : splitTopLevelTypeArgs(name.slice(lt + 1, -1));
219
+ const supplied = options.typeArguments.map((arg) => {
220
+ if (typeof arg === 'string') {
221
+ return arg;
222
+ }
223
+ if (arg && typeof arg.serialize === 'function' && typeof arg.name === 'string') {
224
+ return arg.name;
225
+ }
226
+ throw new PASClientError(`Invalid type argument ${stringify(arg)}`);
227
+ });
228
+
229
+ if (supplied.length !== baked.length) {
230
+ throw new PASClientError(
231
+ `Expected ${baked.length} type arguments for ${base}, got ${supplied.length}`,
232
+ );
233
+ }
234
+
235
+ result = supplied.length === 0 ? base : `${base}<${supplied.join(', ')}>`;
236
+ }
237
+
238
+ if (HAS_PHANTOM_REGEX.test(result)) {
239
+ throw new PASClientError(
240
+ options?.typeArguments
241
+ ? `A type argument contains an unfilled phantom parameter in ${result}`
242
+ : `Missing type arguments for ${result}`,
243
+ );
244
+ }
245
+
246
+ if (options?.package) {
247
+ const [, ...rest] = result.split('::');
248
+ result = [options.package, ...rest].join('::');
249
+ }
250
+
251
+ // fully validate address-only tags (MVR names can't be parsed as type tags)
252
+ if (!HAS_PHANTOM_REGEX.test(result) && !/[@/]/.test(result)) {
253
+ TypeTagSerializer.parseFromStr(result);
254
+ }
255
+
256
+ return result;
257
+ }
258
+
259
+ async function resolveBuiltTypeTag(
260
+ name: string,
261
+ options: { client: ClientWithCoreApi } & TypeTagOptions,
262
+ ): Promise<string> {
263
+ const { client, ...rest } = options;
264
+ const { type } = await client.core.mvr.resolveType({
265
+ type: buildTypeTag(name, rest),
266
+ });
267
+ return normalizeStructTag(type);
268
+ }
269
+
162
270
  export class MoveStruct<
163
271
  T extends Record<string, BcsType<any>>,
164
272
  const Name extends string = string,
165
273
  > extends BcsStruct<T, Name> {
274
+ /**
275
+ * Build the type tag for this struct.
276
+ *
277
+ * `typeArguments` is the full positional list, in Move declaration order, and
278
+ * is required when the struct has unfilled phantom parameters. The result may
279
+ * contain MVR names: those are valid in transaction `typeArguments`, but for
280
+ * queries or comparisons against on-chain data use `resolveTypeTag` instead.
281
+ */
282
+ typeTag(...args: TypeTagParams<Name>): string {
283
+ return buildTypeTag(this.name, args[0] as TypeTagOptions | undefined);
284
+ }
285
+
286
+ /**
287
+ * Build the type tag for this struct, then resolve any MVR names through the
288
+ * client (using its configured overrides and the MVR API) and return the
289
+ * normalized, address-only form suitable for queries and comparisons against
290
+ * on-chain data.
291
+ */
292
+ async resolveTypeTag(options: ResolveTypeTagOptions<Name>): Promise<string> {
293
+ return resolveBuiltTypeTag(
294
+ this.name,
295
+ options as { client: ClientWithCoreApi } & TypeTagOptions,
296
+ );
297
+ }
298
+
166
299
  async get<Include extends Omit<SuiClientTypes.ObjectInclude, 'content' | 'json'> = {}>({
167
300
  objectId,
168
301
  ...options
@@ -217,16 +350,44 @@ export class MoveStruct<
217
350
  export class MoveEnum<
218
351
  T extends Record<string, BcsType<any> | null>,
219
352
  const Name extends string,
220
- > extends BcsEnum<T, Name> {}
353
+ > extends BcsEnum<T, Name> {
354
+ /** Build the type tag for this enum. See `MoveStruct.typeTag` for semantics. */
355
+ typeTag(...args: TypeTagParams<Name>): string {
356
+ return buildTypeTag(this.name, args[0] as TypeTagOptions | undefined);
357
+ }
358
+
359
+ /** Build and resolve the type tag for this enum. See `MoveStruct.resolveTypeTag`. */
360
+ async resolveTypeTag(options: ResolveTypeTagOptions<Name>): Promise<string> {
361
+ return resolveBuiltTypeTag(
362
+ this.name,
363
+ options as { client: ClientWithCoreApi } & TypeTagOptions,
364
+ );
365
+ }
366
+ }
221
367
 
222
368
  export class MoveTuple<
223
369
  const T extends readonly BcsType<any>[],
224
370
  const Name extends string,
225
- > extends BcsTuple<T, Name> {}
371
+ > extends BcsTuple<T, Name> {
372
+ /** Build the type tag for this struct. See `MoveStruct.typeTag` for semantics. */
373
+ typeTag(...args: TypeTagParams<Name>): string {
374
+ return buildTypeTag(this.name, args[0] as TypeTagOptions | undefined);
375
+ }
376
+
377
+ /** Build and resolve the type tag for this struct. See `MoveStruct.resolveTypeTag`. */
378
+ async resolveTypeTag(options: ResolveTypeTagOptions<Name>): Promise<string> {
379
+ return resolveBuiltTypeTag(
380
+ this.name,
381
+ options as { client: ClientWithCoreApi } & TypeTagOptions,
382
+ );
383
+ }
384
+ }
226
385
 
227
386
  function stringify(val: unknown) {
228
387
  if (typeof val === 'object') {
229
- return JSON.stringify(val, (val: unknown) => val);
388
+ return JSON.stringify(val, (_key, value) =>
389
+ typeof value === 'bigint' ? value.toString() : value,
390
+ );
230
391
  }
231
392
  if (typeof val === 'bigint') {
232
393
  return val.toString();