@scallop-io/sui-kit 2.1.0 → 2.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/README.md +6 -6
- package/dist/index.cjs +11 -11
- package/dist/index.d.cts +69 -69
- package/dist/index.d.ts +69 -69
- package/dist/index.js +7 -7
- package/package.json +163 -164
- package/src/index.ts +11 -11
- package/src/libs/multiSig/client.ts +35 -35
- package/src/libs/multiSig/index.ts +1 -1
- package/src/libs/multiSig/publickey.ts +8 -8
- package/src/libs/suiAccountManager/crypto.ts +4 -4
- package/src/libs/suiAccountManager/index.ts +82 -82
- package/src/libs/suiAccountManager/keypair.ts +13 -13
- package/src/libs/suiAccountManager/util.ts +20 -20
- package/src/libs/suiInteractor/index.ts +6 -6
- package/src/libs/suiInteractor/suiInteractor.ts +279 -272
- package/src/libs/suiInteractor/util.ts +6 -6
- package/src/libs/suiModel/index.ts +2 -2
- package/src/libs/suiModel/suiOwnedObject.ts +57 -57
- package/src/libs/suiModel/suiSharedObject.ts +27 -27
- package/src/libs/suiTxBuilder/index.ts +303 -302
- package/src/libs/suiTxBuilder/util.ts +185 -183
- package/src/suiKit.ts +422 -410
- package/src/types/index.ts +71 -76
|
@@ -1,69 +1,69 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
normalizeSuiAddress,
|
|
4
|
-
isValidSuiObjectId,
|
|
5
|
-
isValidSuiAddress,
|
|
6
|
-
} from '@mysten/sui/utils';
|
|
7
|
-
import { Inputs, getPureBcsSchema } from '@mysten/sui/transactions';
|
|
8
|
-
import { SerializedBcs, bcs, isSerializedBcs } from '@mysten/bcs';
|
|
1
|
+
import { bcs, isSerializedBcs, SerializedBcs } from "@mysten/bcs";
|
|
2
|
+
import type { SuiClientTypes } from "@mysten/sui/client";
|
|
9
3
|
import type {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
} from
|
|
14
|
-
import
|
|
4
|
+
Transaction,
|
|
5
|
+
TransactionArgument,
|
|
6
|
+
TransactionObjectArgument,
|
|
7
|
+
} from "@mysten/sui/transactions";
|
|
8
|
+
import { getPureBcsSchema, Inputs } from "@mysten/sui/transactions";
|
|
9
|
+
import {
|
|
10
|
+
isValidSuiAddress,
|
|
11
|
+
isValidSuiObjectId,
|
|
12
|
+
normalizeSuiAddress,
|
|
13
|
+
normalizeSuiObjectId,
|
|
14
|
+
} from "@mysten/sui/utils";
|
|
15
15
|
import type {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
} from
|
|
16
|
+
SuiAddressArg,
|
|
17
|
+
SuiAmountsArg,
|
|
18
|
+
SuiInputTypes,
|
|
19
|
+
SuiObjectArg,
|
|
20
|
+
SuiTxArg,
|
|
21
|
+
SuiVecTxArg,
|
|
22
|
+
} from "../../types/index.js";
|
|
23
23
|
|
|
24
24
|
// Object reference type
|
|
25
25
|
interface SuiObjectRef {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
26
|
+
objectId: string;
|
|
27
|
+
version: number | string;
|
|
28
|
+
digest: string;
|
|
29
29
|
}
|
|
30
30
|
|
|
31
31
|
// Simple types that can be converted to OpenSignatureBody
|
|
32
32
|
const SIMPLE_BCS_TYPES = [
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
33
|
+
"u8",
|
|
34
|
+
"u16",
|
|
35
|
+
"u32",
|
|
36
|
+
"u64",
|
|
37
|
+
"u128",
|
|
38
|
+
"u256",
|
|
39
|
+
"bool",
|
|
40
|
+
"address",
|
|
41
41
|
] as const;
|
|
42
42
|
|
|
43
43
|
type SimpleBcsType = (typeof SIMPLE_BCS_TYPES)[number];
|
|
44
44
|
|
|
45
45
|
// Convert simple type string to OpenSignatureBody
|
|
46
46
|
function toOpenSignatureBody(type: string): SuiClientTypes.OpenSignatureBody {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
47
|
+
if (!SIMPLE_BCS_TYPES.includes(type as SimpleBcsType)) {
|
|
48
|
+
throw new Error(`Invalid SimpleBcsType: ${type}`);
|
|
49
|
+
}
|
|
50
|
+
return { $kind: type } as SuiClientTypes.OpenSignatureBody;
|
|
51
51
|
}
|
|
52
52
|
|
|
53
53
|
// TODO: unclear why we need this function and types
|
|
54
54
|
export const getDefaultSuiInputType = (
|
|
55
|
-
|
|
56
|
-
):
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
55
|
+
value: SuiTxArg,
|
|
56
|
+
): "u64" | "bool" | "object" | undefined => {
|
|
57
|
+
if (typeof value === "string" && isValidSuiObjectId(value)) {
|
|
58
|
+
return "object";
|
|
59
|
+
}
|
|
60
|
+
if (typeof value === "number" || typeof value === "bigint") {
|
|
61
|
+
return "u64";
|
|
62
|
+
}
|
|
63
|
+
if (typeof value === "boolean") {
|
|
64
|
+
return "bool";
|
|
65
|
+
}
|
|
66
|
+
return undefined;
|
|
67
67
|
};
|
|
68
68
|
|
|
69
69
|
// =========== TYPE GUARD ============
|
|
@@ -74,11 +74,13 @@ export const getDefaultSuiInputType = (
|
|
|
74
74
|
* @returns boolean.
|
|
75
75
|
*/
|
|
76
76
|
function isAmountArg(arg: any): arg is bigint | number | string {
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
77
|
+
return (
|
|
78
|
+
typeof arg === "number" ||
|
|
79
|
+
typeof arg === "bigint" ||
|
|
80
|
+
(typeof arg === "string" &&
|
|
81
|
+
!isValidSuiAddress(arg) &&
|
|
82
|
+
!Number.isNaN(Number(arg)))
|
|
83
|
+
);
|
|
82
84
|
}
|
|
83
85
|
|
|
84
86
|
/**
|
|
@@ -88,19 +90,19 @@ function isAmountArg(arg: any): arg is bigint | number | string {
|
|
|
88
90
|
* @returns boolean.
|
|
89
91
|
*/
|
|
90
92
|
function isMoveVecArg(
|
|
91
|
-
|
|
93
|
+
arg: SuiTxArg | SuiVecTxArg | SuiObjectArg | SuiAmountsArg,
|
|
92
94
|
): arg is SuiVecTxArg {
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
95
|
+
if (
|
|
96
|
+
arg !== null &&
|
|
97
|
+
typeof arg === "object" &&
|
|
98
|
+
"vecType" in arg &&
|
|
99
|
+
"value" in arg
|
|
100
|
+
) {
|
|
101
|
+
return true;
|
|
102
|
+
} else if (Array.isArray(arg)) {
|
|
103
|
+
return true;
|
|
104
|
+
}
|
|
105
|
+
return false;
|
|
104
106
|
}
|
|
105
107
|
|
|
106
108
|
/**
|
|
@@ -109,12 +111,12 @@ function isMoveVecArg(
|
|
|
109
111
|
* @returns boolean
|
|
110
112
|
*/
|
|
111
113
|
function isObjectRef(arg: SuiObjectArg): arg is SuiObjectRef {
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
114
|
+
return (
|
|
115
|
+
typeof arg === "object" &&
|
|
116
|
+
"digest" in arg &&
|
|
117
|
+
"version" in arg &&
|
|
118
|
+
"objectId" in arg
|
|
119
|
+
);
|
|
118
120
|
}
|
|
119
121
|
|
|
120
122
|
/**
|
|
@@ -123,14 +125,14 @@ function isObjectRef(arg: SuiObjectArg): arg is SuiObjectRef {
|
|
|
123
125
|
* @returns
|
|
124
126
|
*/
|
|
125
127
|
function isSharedObjectRef(
|
|
126
|
-
|
|
128
|
+
arg: SuiObjectArg,
|
|
127
129
|
): arg is Parameters<typeof Inputs.SharedObjectRef>[0] {
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
130
|
+
return (
|
|
131
|
+
typeof arg === "object" &&
|
|
132
|
+
"objectId" in arg &&
|
|
133
|
+
"initialSharedVersion" in arg &&
|
|
134
|
+
"mutable" in arg
|
|
135
|
+
);
|
|
134
136
|
}
|
|
135
137
|
// ===================================
|
|
136
138
|
|
|
@@ -148,45 +150,45 @@ function isSharedObjectRef(
|
|
|
148
150
|
* @param type 'address' | 'bool' | 'u8' | 'u16' | 'u32' | 'u64' | 'u128' | 'u256' | 'signer' | 'object' | string
|
|
149
151
|
*/
|
|
150
152
|
export function makeVecParam(
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
153
|
+
txBlock: Transaction,
|
|
154
|
+
args: SuiTxArg[],
|
|
155
|
+
type?: SuiInputTypes,
|
|
154
156
|
): TransactionArgument {
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
157
|
+
if (args.length === 0)
|
|
158
|
+
throw new Error("Transaction builder error: Empty array is not allowed");
|
|
159
|
+
// Using first element value as default type
|
|
160
|
+
// TODO: unclear why we need this function and types
|
|
161
|
+
const defaultSuiType = getDefaultSuiInputType(args[0]);
|
|
162
|
+
const VECTOR_REGEX = /^vector<(.+)>$/;
|
|
163
|
+
const STRUCT_REGEX = /^([^:]+)::([^:]+)::([^<]+)(<(.+)>)?/;
|
|
162
164
|
|
|
163
|
-
|
|
165
|
+
type = type || defaultSuiType;
|
|
164
166
|
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
167
|
+
if (type === "object") {
|
|
168
|
+
const elements = args.map((arg) =>
|
|
169
|
+
typeof arg === "string" && isValidSuiObjectId(arg)
|
|
170
|
+
? txBlock.object(normalizeSuiObjectId(arg))
|
|
171
|
+
: convertObjArg(txBlock, arg as SuiObjectArg),
|
|
172
|
+
);
|
|
173
|
+
return txBlock.makeMoveVec({ elements });
|
|
174
|
+
} else if (
|
|
175
|
+
typeof type === "string" &&
|
|
176
|
+
!VECTOR_REGEX.test(type) &&
|
|
177
|
+
!STRUCT_REGEX.test(type)
|
|
178
|
+
) {
|
|
179
|
+
// Convert simple type to OpenSignatureBody for BCS schema
|
|
180
|
+
const signatureBody = toOpenSignatureBody(type as SimpleBcsType);
|
|
181
|
+
const bcsSchema = getPureBcsSchema(signatureBody);
|
|
182
|
+
if (!bcsSchema) {
|
|
183
|
+
throw new Error(`Unknown type: ${type}`);
|
|
184
|
+
}
|
|
185
|
+
return txBlock.pure(bcs.vector(bcsSchema).serialize(args));
|
|
186
|
+
} else {
|
|
187
|
+
const elements = args.map((arg) =>
|
|
188
|
+
convertObjArg(txBlock, arg as SuiObjectArg),
|
|
189
|
+
);
|
|
190
|
+
return txBlock.makeMoveVec({ elements, type: type as string });
|
|
191
|
+
}
|
|
190
192
|
}
|
|
191
193
|
|
|
192
194
|
/**
|
|
@@ -197,27 +199,27 @@ export function makeVecParam(
|
|
|
197
199
|
* @returns The converted array of TransactionArgument.
|
|
198
200
|
*/
|
|
199
201
|
export function convertArgs(
|
|
200
|
-
|
|
201
|
-
|
|
202
|
+
txBlock: Transaction,
|
|
203
|
+
args: (SuiTxArg | SuiVecTxArg | SuiObjectArg | SuiAmountsArg)[],
|
|
202
204
|
): TransactionArgument[] {
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
205
|
+
return args.map((arg) => {
|
|
206
|
+
if (arg instanceof SerializedBcs || isSerializedBcs(arg)) {
|
|
207
|
+
return txBlock.pure(arg);
|
|
208
|
+
}
|
|
207
209
|
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
210
|
+
if (isMoveVecArg(arg)) {
|
|
211
|
+
const vecType = "vecType" in arg;
|
|
212
|
+
return vecType
|
|
213
|
+
? makeVecParam(txBlock, arg.value, arg.vecType)
|
|
214
|
+
: makeVecParam(txBlock, arg);
|
|
215
|
+
}
|
|
214
216
|
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
217
|
+
if (isAmountArg(arg)) {
|
|
218
|
+
return convertAmounts(txBlock, [arg as unknown as SuiAmountsArg])[0];
|
|
219
|
+
}
|
|
218
220
|
|
|
219
|
-
|
|
220
|
-
|
|
221
|
+
return convertObjArg(txBlock, arg as SuiObjectArg);
|
|
222
|
+
});
|
|
221
223
|
}
|
|
222
224
|
|
|
223
225
|
/**
|
|
@@ -228,14 +230,14 @@ export function convertArgs(
|
|
|
228
230
|
* @returns The converted TransactionArgument.
|
|
229
231
|
*/
|
|
230
232
|
export function convertAddressArg(
|
|
231
|
-
|
|
232
|
-
|
|
233
|
+
txBlock: Transaction,
|
|
234
|
+
arg: SuiAddressArg,
|
|
233
235
|
): SuiTxArg {
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
236
|
+
if (typeof arg === "string" && isValidSuiAddress(arg)) {
|
|
237
|
+
return txBlock.pure.address(normalizeSuiAddress(arg));
|
|
238
|
+
} else {
|
|
239
|
+
return convertArgs(txBlock, [arg])[0];
|
|
240
|
+
}
|
|
239
241
|
}
|
|
240
242
|
|
|
241
243
|
/**
|
|
@@ -246,64 +248,64 @@ export function convertAddressArg(
|
|
|
246
248
|
* @returns The converted TransactionArgument.
|
|
247
249
|
*/
|
|
248
250
|
export function convertObjArg(
|
|
249
|
-
|
|
250
|
-
|
|
251
|
+
txb: Transaction,
|
|
252
|
+
arg: SuiObjectArg,
|
|
251
253
|
): TransactionObjectArgument {
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
254
|
+
if (typeof arg === "string") {
|
|
255
|
+
return txb.object(arg);
|
|
256
|
+
}
|
|
255
257
|
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
258
|
+
if (isObjectRef(arg)) {
|
|
259
|
+
return txb.objectRef(arg);
|
|
260
|
+
}
|
|
259
261
|
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
262
|
+
if (isSharedObjectRef(arg)) {
|
|
263
|
+
return txb.sharedObjectRef(arg);
|
|
264
|
+
}
|
|
263
265
|
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
266
|
+
if ("Object" in arg) {
|
|
267
|
+
if ("ImmOrOwnedObject" in arg.Object) {
|
|
268
|
+
return txb.object(Inputs.ObjectRef(arg.Object.ImmOrOwnedObject));
|
|
269
|
+
} else if ("SharedObject" in arg.Object) {
|
|
270
|
+
return txb.object(Inputs.SharedObjectRef(arg.Object.SharedObject));
|
|
271
|
+
} else {
|
|
272
|
+
throw new Error("Invalid argument type");
|
|
273
|
+
}
|
|
274
|
+
}
|
|
273
275
|
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
276
|
+
if (typeof arg === "function") {
|
|
277
|
+
return arg;
|
|
278
|
+
}
|
|
277
279
|
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
280
|
+
if (
|
|
281
|
+
"GasCoin" in arg ||
|
|
282
|
+
"Input" in arg ||
|
|
283
|
+
"Result" in arg ||
|
|
284
|
+
"NestedResult" in arg
|
|
285
|
+
) {
|
|
286
|
+
return arg;
|
|
287
|
+
}
|
|
286
288
|
|
|
287
|
-
|
|
289
|
+
throw new Error("Invalid argument type");
|
|
288
290
|
}
|
|
289
291
|
|
|
290
292
|
export function convertAmounts(
|
|
291
|
-
|
|
292
|
-
|
|
293
|
+
txBlock: Transaction,
|
|
294
|
+
amounts: SuiAmountsArg[],
|
|
293
295
|
): TransactionArgument[] {
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
296
|
+
return amounts.map((amount) => {
|
|
297
|
+
if (isAmountArg(amount)) {
|
|
298
|
+
return txBlock.pure.u64(amount);
|
|
299
|
+
} else {
|
|
300
|
+
return convertArgs(txBlock, [amount])[0];
|
|
301
|
+
}
|
|
302
|
+
});
|
|
301
303
|
}
|
|
302
304
|
|
|
303
305
|
export const partitionArray = <T>(array: T[], chunkSize: number) => {
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
306
|
+
const result: T[][] = [];
|
|
307
|
+
for (let i = 0; i < array.length; i += chunkSize) {
|
|
308
|
+
result.push(array.slice(i, i + chunkSize));
|
|
309
|
+
}
|
|
310
|
+
return result;
|
|
309
311
|
};
|