@twin.org/dlt-iota 0.0.2-next.9 → 0.0.3-next.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -81
- package/dist/es/index.js +19 -0
- package/dist/es/index.js.map +1 -0
- package/dist/es/iota.js +508 -0
- package/dist/es/iota.js.map +1 -0
- package/dist/es/iotaSmartContractUtils.js +413 -0
- package/dist/es/iotaSmartContractUtils.js.map +1 -0
- package/dist/es/models/IAdminCapFields.js +4 -0
- package/dist/es/models/IAdminCapFields.js.map +1 -0
- package/dist/es/models/IContractData.js +4 -0
- package/dist/es/models/IContractData.js.map +1 -0
- package/dist/es/models/IGasReservationResult.js +2 -0
- package/dist/es/models/IGasReservationResult.js.map +1 -0
- package/dist/es/models/IGasStationConfig.js +4 -0
- package/dist/es/models/IGasStationConfig.js.map +1 -0
- package/dist/es/models/IGasStationExecuteResponse.js +4 -0
- package/dist/es/models/IGasStationExecuteResponse.js.map +1 -0
- package/dist/es/models/IGasStationReserveGasResponse.js +2 -0
- package/dist/es/models/IGasStationReserveGasResponse.js.map +1 -0
- package/dist/es/models/IGasStationReserveGasResult.js +2 -0
- package/dist/es/models/IGasStationReserveGasResult.js.map +1 -0
- package/dist/es/models/IIotaConfig.js +2 -0
- package/dist/es/models/IIotaConfig.js.map +1 -0
- package/dist/es/models/IIotaDryRun.js +2 -0
- package/dist/es/models/IIotaDryRun.js.map +1 -0
- package/dist/es/models/IIotaResponseOptions.js +2 -0
- package/dist/es/models/IIotaResponseOptions.js.map +1 -0
- package/dist/es/models/IMigrationStateFields.js +4 -0
- package/dist/es/models/IMigrationStateFields.js.map +1 -0
- package/dist/es/models/ISmartContractDeployments.js +2 -0
- package/dist/es/models/ISmartContractDeployments.js.map +1 -0
- package/dist/es/models/ISmartContractObject.js +4 -0
- package/dist/es/models/ISmartContractObject.js.map +1 -0
- package/dist/es/models/networkTypes.js +21 -0
- package/dist/es/models/networkTypes.js.map +1 -0
- package/dist/types/index.d.ts +16 -16
- package/dist/types/iota.d.ts +8 -4
- package/dist/types/iotaSmartContractUtils.d.ts +2 -2
- package/dist/types/models/IGasStationReserveGasResponse.d.ts +1 -1
- package/dist/types/models/IIotaConfig.d.ts +1 -1
- package/dist/types/models/ISmartContractDeployments.d.ts +2 -2
- package/docs/changelog.md +44 -0
- package/docs/reference/classes/Iota.md +11 -3
- package/docs/reference/classes/IotaSmartContractUtils.md +3 -3
- package/docs/reference/interfaces/IGasStationExecuteResponse.md +1 -1
- package/docs/reference/interfaces/IGasStationReserveGasResponse.md +1 -1
- package/locales/en.json +6 -2
- package/package.json +21 -12
- package/dist/cjs/index.cjs +0 -946
- package/dist/esm/index.mjs +0 -942
package/dist/esm/index.mjs
DELETED
|
@@ -1,942 +0,0 @@
|
|
|
1
|
-
import { toB64, bcs } from '@iota/bcs';
|
|
2
|
-
import { IotaClient } from '@iota/iota-sdk/client';
|
|
3
|
-
import { Ed25519Keypair } from '@iota/iota-sdk/keypairs/ed25519';
|
|
4
|
-
import { Transaction } from '@iota/iota-sdk/transactions';
|
|
5
|
-
import { Guards, Is, GeneralError, Converter, BaseError, StringHelper } from '@twin.org/core';
|
|
6
|
-
import { Bip44, KeyType, Bip39 } from '@twin.org/crypto';
|
|
7
|
-
import { FetchHelper, HttpMethod } from '@twin.org/web';
|
|
8
|
-
|
|
9
|
-
// Copyright 2024 IOTA Stiftung.
|
|
10
|
-
// SPDX-License-Identifier: Apache-2.0.
|
|
11
|
-
/**
|
|
12
|
-
* Class for performing operations on IOTA.
|
|
13
|
-
*/
|
|
14
|
-
class Iota {
|
|
15
|
-
/**
|
|
16
|
-
* Default name for the mnemonic secret.
|
|
17
|
-
*/
|
|
18
|
-
static DEFAULT_MNEMONIC_SECRET_NAME = "mnemonic";
|
|
19
|
-
/**
|
|
20
|
-
* Default name for the seed secret.
|
|
21
|
-
*/
|
|
22
|
-
static DEFAULT_SEED_SECRET_NAME = "seed";
|
|
23
|
-
/**
|
|
24
|
-
* Default coin type.
|
|
25
|
-
*/
|
|
26
|
-
static DEFAULT_COIN_TYPE = 4218;
|
|
27
|
-
/**
|
|
28
|
-
* Default scan range.
|
|
29
|
-
*/
|
|
30
|
-
static DEFAULT_SCAN_RANGE = 1000;
|
|
31
|
-
/**
|
|
32
|
-
* Default inclusion timeout.
|
|
33
|
-
*/
|
|
34
|
-
static DEFAULT_INCLUSION_TIMEOUT = 60;
|
|
35
|
-
/**
|
|
36
|
-
* Runtime name for the class.
|
|
37
|
-
* @internal
|
|
38
|
-
*/
|
|
39
|
-
static _CLASS_NAME = "Iota";
|
|
40
|
-
/**
|
|
41
|
-
* Create a new IOTA client.
|
|
42
|
-
* @param config The configuration.
|
|
43
|
-
* @returns The client instance.
|
|
44
|
-
*/
|
|
45
|
-
static createClient(config) {
|
|
46
|
-
Guards.object(Iota._CLASS_NAME, "config", config);
|
|
47
|
-
Guards.object(Iota._CLASS_NAME, "config.clientOptions", config.clientOptions);
|
|
48
|
-
Guards.string(Iota._CLASS_NAME, "config.clientOptions.url", config.clientOptions.url);
|
|
49
|
-
return new IotaClient(config.clientOptions);
|
|
50
|
-
}
|
|
51
|
-
/**
|
|
52
|
-
* Create configuration using defaults where necessary.
|
|
53
|
-
* @param config The configuration to populate.
|
|
54
|
-
*/
|
|
55
|
-
static populateConfig(config) {
|
|
56
|
-
Guards.object(Iota._CLASS_NAME, "config.clientOptions", config.clientOptions);
|
|
57
|
-
config.vaultMnemonicId ??= Iota.DEFAULT_MNEMONIC_SECRET_NAME;
|
|
58
|
-
config.vaultSeedId ??= Iota.DEFAULT_SEED_SECRET_NAME;
|
|
59
|
-
config.coinType ??= Iota.DEFAULT_COIN_TYPE;
|
|
60
|
-
config.inclusionTimeoutSeconds ??= Iota.DEFAULT_INCLUSION_TIMEOUT;
|
|
61
|
-
}
|
|
62
|
-
/**
|
|
63
|
-
* Get addresses for the identity.
|
|
64
|
-
* @param seed The seed to use for generating addresses.
|
|
65
|
-
* @param coinType The coin type to use.
|
|
66
|
-
* @param accountIndex The account index to get the addresses for.
|
|
67
|
-
* @param startAddressIndex The start index for the addresses.
|
|
68
|
-
* @param count The number of addresses to generate.
|
|
69
|
-
* @param isInternal Whether the addresses are internal.
|
|
70
|
-
* @returns The list of addresses.
|
|
71
|
-
*/
|
|
72
|
-
static getAddresses(seed, coinType, accountIndex, startAddressIndex, count, isInternal) {
|
|
73
|
-
Guards.integer(Iota._CLASS_NAME, "coinType", coinType);
|
|
74
|
-
Guards.integer(Iota._CLASS_NAME, "accountIndex", accountIndex);
|
|
75
|
-
Guards.integer(Iota._CLASS_NAME, "startAddressIndex", startAddressIndex);
|
|
76
|
-
Guards.integer(Iota._CLASS_NAME, "count", count);
|
|
77
|
-
const addresses = [];
|
|
78
|
-
for (let i = startAddressIndex; i < startAddressIndex + count; i++) {
|
|
79
|
-
// Derive the keypair using the seed
|
|
80
|
-
const keyPair = Bip44.keyPair(seed, KeyType.Ed25519, coinType ?? Iota.DEFAULT_COIN_TYPE, accountIndex, isInternal ?? false, i);
|
|
81
|
-
const keypair = Ed25519Keypair.fromSecretKey(keyPair.privateKey);
|
|
82
|
-
addresses.push(keypair.getPublicKey().toIotaAddress());
|
|
83
|
-
}
|
|
84
|
-
return addresses;
|
|
85
|
-
}
|
|
86
|
-
/**
|
|
87
|
-
* Get a key pair for the specified index.
|
|
88
|
-
* @param seed The seed to use for generating the key pair.
|
|
89
|
-
* @param coinType The coin type to use.
|
|
90
|
-
* @param accountIndex The account index to get the key pair for.
|
|
91
|
-
* @param addressIndex The address index to get the key pair for.
|
|
92
|
-
* @param isInternal Whether the address is internal.
|
|
93
|
-
* @returns The key pair containing private key and public key.
|
|
94
|
-
*/
|
|
95
|
-
static getKeyPair(seed, coinType, accountIndex, addressIndex, isInternal) {
|
|
96
|
-
Guards.integer(Iota._CLASS_NAME, "coinType", coinType);
|
|
97
|
-
Guards.integer(Iota._CLASS_NAME, "accountIndex", accountIndex);
|
|
98
|
-
Guards.integer(Iota._CLASS_NAME, "addressIndex", addressIndex);
|
|
99
|
-
const keyPair = Bip44.keyPair(seed, KeyType.Ed25519, coinType ?? Iota.DEFAULT_COIN_TYPE, accountIndex, isInternal ?? false, addressIndex);
|
|
100
|
-
return keyPair;
|
|
101
|
-
}
|
|
102
|
-
/**
|
|
103
|
-
* Prepare and post a transaction.
|
|
104
|
-
* @param config The configuration.
|
|
105
|
-
* @param vaultConnector The vault connector.
|
|
106
|
-
* @param logging The logging component.
|
|
107
|
-
* @param identity The identity of the user to access the vault keys.
|
|
108
|
-
* @param client The client instance.
|
|
109
|
-
* @param source The source address.
|
|
110
|
-
* @param amount The amount to transfer.
|
|
111
|
-
* @param recipient The recipient address.
|
|
112
|
-
* @param options The transaction options.
|
|
113
|
-
* @returns The transaction result.
|
|
114
|
-
*/
|
|
115
|
-
static async prepareAndPostValueTransaction(config, vaultConnector, logging, identity, client, source, amount, recipient, options) {
|
|
116
|
-
try {
|
|
117
|
-
const txb = new Transaction();
|
|
118
|
-
const [coin] = txb.splitCoins(txb.gas, [txb.pure.u64(amount)]);
|
|
119
|
-
txb.transferObjects([coin], txb.pure.address(recipient));
|
|
120
|
-
// Check if gas station configuration is present
|
|
121
|
-
if (Is.object(config.gasStation)) {
|
|
122
|
-
return await this.prepareAndPostGasStationTransaction(config, vaultConnector, identity, client, source, txb);
|
|
123
|
-
}
|
|
124
|
-
const result = await this.prepareAndPostTransaction(config, vaultConnector, logging, identity, client, source, txb, options);
|
|
125
|
-
return result;
|
|
126
|
-
}
|
|
127
|
-
catch (error) {
|
|
128
|
-
throw new GeneralError(Iota._CLASS_NAME, "valueTransactionFailed", undefined, Iota.extractPayloadError(error));
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
/**
|
|
132
|
-
* Prepare and post a transaction.
|
|
133
|
-
* @param config The configuration.
|
|
134
|
-
* @param vaultConnector The vault connector.
|
|
135
|
-
* @param logging The logging component.
|
|
136
|
-
* @param identity The identity of the user to access the vault keys.
|
|
137
|
-
* @param client The client instance.
|
|
138
|
-
* @param owner The owner of the address.
|
|
139
|
-
* @param transaction The transaction to execute.
|
|
140
|
-
* @param options The transaction options.
|
|
141
|
-
* @returns The transaction response.
|
|
142
|
-
*/
|
|
143
|
-
static async prepareAndPostTransaction(config, vaultConnector, logging, identity, client, owner, transaction, options) {
|
|
144
|
-
// Check if gas station configuration is present
|
|
145
|
-
if (Is.object(config.gasStation)) {
|
|
146
|
-
return this.prepareAndPostGasStationTransaction(config, vaultConnector, identity, client, owner, transaction, options);
|
|
147
|
-
}
|
|
148
|
-
// Traditional transaction flow
|
|
149
|
-
// Dry run the transaction if cost logging is enabled to get the gas and storage costs
|
|
150
|
-
if (Is.stringValue(options?.dryRunLabel)) {
|
|
151
|
-
await Iota.dryRunTransaction(client, logging, transaction, owner, options.dryRunLabel);
|
|
152
|
-
}
|
|
153
|
-
const seed = await this.getSeed(config, vaultConnector, identity);
|
|
154
|
-
const addressKeyPair = Iota.findAddress(config.maxAddressScanRange ?? Iota.DEFAULT_SCAN_RANGE, config.coinType ?? Iota.DEFAULT_COIN_TYPE, seed, owner);
|
|
155
|
-
const keypair = Ed25519Keypair.fromSecretKey(addressKeyPair.privateKey);
|
|
156
|
-
try {
|
|
157
|
-
const response = await client.signAndExecuteTransaction({
|
|
158
|
-
transaction,
|
|
159
|
-
signer: keypair,
|
|
160
|
-
requestType: "WaitForLocalExecution",
|
|
161
|
-
options: {
|
|
162
|
-
showEffects: options?.showEffects ?? true,
|
|
163
|
-
showEvents: options?.showEvents ?? true,
|
|
164
|
-
showObjectChanges: options?.showObjectChanges ?? true
|
|
165
|
-
}
|
|
166
|
-
});
|
|
167
|
-
if (options?.waitForConfirmation ?? true) {
|
|
168
|
-
// Wait for transaction to be indexed and available over API
|
|
169
|
-
const confirmedTransaction = await Iota.waitForTransactionConfirmation(client, response.digest, config, {
|
|
170
|
-
showEffects: options?.showEffects ?? true,
|
|
171
|
-
showEvents: options?.showEvents ?? true,
|
|
172
|
-
showObjectChanges: options?.showObjectChanges ?? true
|
|
173
|
-
});
|
|
174
|
-
return confirmedTransaction;
|
|
175
|
-
}
|
|
176
|
-
return response;
|
|
177
|
-
}
|
|
178
|
-
catch (error) {
|
|
179
|
-
throw new GeneralError(Iota._CLASS_NAME, "transactionFailed", undefined, Iota.extractPayloadError(error));
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
/**
|
|
183
|
-
* Get the seed from the vault.
|
|
184
|
-
* @param config The configuration to use.
|
|
185
|
-
* @param vaultConnector The vault connector to use.
|
|
186
|
-
* @param identity The identity of the user to access the vault keys.
|
|
187
|
-
* @returns The seed.
|
|
188
|
-
*/
|
|
189
|
-
static async getSeed(config, vaultConnector, identity) {
|
|
190
|
-
try {
|
|
191
|
-
const seedBase64 = await vaultConnector.getSecret(Iota.buildSeedKey(identity, config.vaultSeedId));
|
|
192
|
-
return Converter.base64ToBytes(seedBase64);
|
|
193
|
-
}
|
|
194
|
-
catch { }
|
|
195
|
-
const mnemonic = await vaultConnector.getSecret(Iota.buildMnemonicKey(identity, config.vaultMnemonicId));
|
|
196
|
-
return Bip39.mnemonicToSeed(mnemonic);
|
|
197
|
-
}
|
|
198
|
-
/**
|
|
199
|
-
* Find the address in the seed.
|
|
200
|
-
* @param maxScanRange The maximum range to scan.
|
|
201
|
-
* @param coinType The coin type to use.
|
|
202
|
-
* @param seed The seed to use.
|
|
203
|
-
* @param address The address to find.
|
|
204
|
-
* @returns The address key pair.
|
|
205
|
-
* @throws Error if the address is not found.
|
|
206
|
-
*/
|
|
207
|
-
static findAddress(maxScanRange, coinType, seed, address) {
|
|
208
|
-
for (let i = 0; i < maxScanRange; i++) {
|
|
209
|
-
const addressKeyPair = Bip44.address(seed, KeyType.Ed25519, coinType, 0, false, i);
|
|
210
|
-
if (addressKeyPair.address === address) {
|
|
211
|
-
return addressKeyPair;
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
throw new GeneralError(Iota._CLASS_NAME, "addressNotFound", { address });
|
|
215
|
-
}
|
|
216
|
-
/**
|
|
217
|
-
* Extract error from SDK payload.
|
|
218
|
-
* Errors from the IOTA SDK are usually not JSON strings but objects.
|
|
219
|
-
* @param error The error to extract.
|
|
220
|
-
* @returns The extracted error.
|
|
221
|
-
*/
|
|
222
|
-
static extractPayloadError(error) {
|
|
223
|
-
if (Is.object(error)) {
|
|
224
|
-
if (!Is.empty(error.inner)) {
|
|
225
|
-
error.inner = Iota.extractPayloadError(error.inner);
|
|
226
|
-
}
|
|
227
|
-
if (error.code === "InsufficientGas") {
|
|
228
|
-
return new GeneralError(Iota._CLASS_NAME, "insufficientFunds");
|
|
229
|
-
}
|
|
230
|
-
else if (error.message?.startsWith("ErrorObject")) {
|
|
231
|
-
const msg = /message: "(.*)"/.exec(error.message);
|
|
232
|
-
if (msg && msg.length > 1) {
|
|
233
|
-
error = msg[1];
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
const baseError = BaseError.fromError(error);
|
|
238
|
-
if (baseError.name === "Base" && !Is.stringValue(baseError.source)) {
|
|
239
|
-
baseError.name = "IOTA";
|
|
240
|
-
baseError.source = Iota._CLASS_NAME;
|
|
241
|
-
}
|
|
242
|
-
return baseError;
|
|
243
|
-
}
|
|
244
|
-
/**
|
|
245
|
-
* Get the key for storing the mnemonic.
|
|
246
|
-
* @param identity The identity to use.
|
|
247
|
-
* @param vaultMnemonicId The mnemonic ID to use.
|
|
248
|
-
* @returns The mnemonic key.
|
|
249
|
-
*/
|
|
250
|
-
static buildMnemonicKey(identity, vaultMnemonicId) {
|
|
251
|
-
return `${identity}/${vaultMnemonicId ?? Iota.DEFAULT_MNEMONIC_SECRET_NAME}`;
|
|
252
|
-
}
|
|
253
|
-
/**
|
|
254
|
-
* Get the key for storing the seed.
|
|
255
|
-
* @param identity The identity to use.
|
|
256
|
-
* @param vaultSeedId The seed ID to use.
|
|
257
|
-
* @returns The seed key.
|
|
258
|
-
*/
|
|
259
|
-
static buildSeedKey(identity, vaultSeedId) {
|
|
260
|
-
return `${identity}/${vaultSeedId ?? Iota.DEFAULT_SEED_SECRET_NAME}`;
|
|
261
|
-
}
|
|
262
|
-
/**
|
|
263
|
-
* Check if the package exists on the network.
|
|
264
|
-
* @param client The client to use.
|
|
265
|
-
* @param packageId The package ID to check.
|
|
266
|
-
* @returns True if the package exists, false otherwise.
|
|
267
|
-
*/
|
|
268
|
-
static async packageExistsOnNetwork(client, packageId) {
|
|
269
|
-
try {
|
|
270
|
-
const packageObject = await client.getObject({
|
|
271
|
-
id: packageId,
|
|
272
|
-
options: {
|
|
273
|
-
showType: true
|
|
274
|
-
}
|
|
275
|
-
});
|
|
276
|
-
if ("error" in packageObject) {
|
|
277
|
-
if (packageObject?.error?.code === "notExists") {
|
|
278
|
-
return false;
|
|
279
|
-
}
|
|
280
|
-
throw new GeneralError(Iota._CLASS_NAME, "packageObjectError", {
|
|
281
|
-
packageId,
|
|
282
|
-
error: packageObject.error
|
|
283
|
-
});
|
|
284
|
-
}
|
|
285
|
-
return true;
|
|
286
|
-
}
|
|
287
|
-
catch (error) {
|
|
288
|
-
throw new GeneralError(Iota._CLASS_NAME, "packageNotFoundOnNetwork", {
|
|
289
|
-
packageId,
|
|
290
|
-
error: Iota.extractPayloadError(error)
|
|
291
|
-
});
|
|
292
|
-
}
|
|
293
|
-
}
|
|
294
|
-
/**
|
|
295
|
-
* Dry run a transaction and log the results.
|
|
296
|
-
* @param client The IOTA client.
|
|
297
|
-
* @param logging The logging component.
|
|
298
|
-
* @param txb The transaction to dry run.
|
|
299
|
-
* @param sender The sender address.
|
|
300
|
-
* @param operation The operation to log.
|
|
301
|
-
* @returns void.
|
|
302
|
-
*/
|
|
303
|
-
static async dryRunTransaction(client, logging, txb, sender, operation) {
|
|
304
|
-
try {
|
|
305
|
-
txb.setSender(sender);
|
|
306
|
-
const builtTx = await txb.build({
|
|
307
|
-
client,
|
|
308
|
-
onlyTransactionKind: false
|
|
309
|
-
});
|
|
310
|
-
const dryRunResult = await client.dryRunTransactionBlock({
|
|
311
|
-
transactionBlock: builtTx
|
|
312
|
-
});
|
|
313
|
-
if (dryRunResult.effects.status?.status !== "success") {
|
|
314
|
-
throw new GeneralError(this._CLASS_NAME, "dryRunFailed", {
|
|
315
|
-
error: dryRunResult.effects?.status?.error
|
|
316
|
-
});
|
|
317
|
-
}
|
|
318
|
-
const result = {
|
|
319
|
-
status: dryRunResult.effects.status.status,
|
|
320
|
-
costs: {
|
|
321
|
-
computationCost: dryRunResult.effects.gasUsed.computationCost,
|
|
322
|
-
computationCostBurned: dryRunResult.effects.gasUsed.computationCostBurned,
|
|
323
|
-
storageCost: dryRunResult.effects.gasUsed.storageCost,
|
|
324
|
-
storageRebate: dryRunResult.effects.gasUsed.storageRebate,
|
|
325
|
-
nonRefundableStorageFee: dryRunResult.effects.gasUsed.nonRefundableStorageFee
|
|
326
|
-
},
|
|
327
|
-
events: dryRunResult.events ?? [],
|
|
328
|
-
balanceChanges: dryRunResult.balanceChanges ?? [],
|
|
329
|
-
objectChanges: dryRunResult.objectChanges ?? []
|
|
330
|
-
};
|
|
331
|
-
if (logging) {
|
|
332
|
-
await logging.log({
|
|
333
|
-
level: "info",
|
|
334
|
-
source: Iota._CLASS_NAME,
|
|
335
|
-
ts: Date.now(),
|
|
336
|
-
message: "transactionCosts",
|
|
337
|
-
data: {
|
|
338
|
-
operation,
|
|
339
|
-
...result
|
|
340
|
-
}
|
|
341
|
-
});
|
|
342
|
-
}
|
|
343
|
-
return result;
|
|
344
|
-
}
|
|
345
|
-
catch (error) {
|
|
346
|
-
if (error instanceof GeneralError) {
|
|
347
|
-
throw error;
|
|
348
|
-
}
|
|
349
|
-
throw new GeneralError(Iota._CLASS_NAME, "dryRunFailed", undefined, Iota.extractPayloadError(error));
|
|
350
|
-
}
|
|
351
|
-
}
|
|
352
|
-
/**
|
|
353
|
-
* Wait for a transaction to be indexed and available over the API.
|
|
354
|
-
* @param client The IOTA client instance.
|
|
355
|
-
* @param digest The digest of the transaction to wait for.
|
|
356
|
-
* @param config The IOTA configuration.
|
|
357
|
-
* @param options Additional options for the transaction query.
|
|
358
|
-
* @param options.showEffects Whether to show effects.
|
|
359
|
-
* @param options.showEvents Whether to show events.
|
|
360
|
-
* @param options.showObjectChanges Whether to show object changes.
|
|
361
|
-
* @returns The confirmed transaction response.
|
|
362
|
-
*/
|
|
363
|
-
static async waitForTransactionConfirmation(client, digest, config, options) {
|
|
364
|
-
const timeoutMs = (config.inclusionTimeoutSeconds ?? Iota.DEFAULT_INCLUSION_TIMEOUT) * 1000;
|
|
365
|
-
return client.waitForTransaction({
|
|
366
|
-
digest,
|
|
367
|
-
timeout: timeoutMs,
|
|
368
|
-
options: {
|
|
369
|
-
showEffects: options?.showEffects ?? true,
|
|
370
|
-
showEvents: options?.showEvents ?? true,
|
|
371
|
-
showObjectChanges: options?.showObjectChanges ?? true
|
|
372
|
-
}
|
|
373
|
-
});
|
|
374
|
-
}
|
|
375
|
-
/**
|
|
376
|
-
* Check if the error is an abort error.
|
|
377
|
-
* @param error The error to check.
|
|
378
|
-
* @param code The error code to check for.
|
|
379
|
-
* @returns True if the error is an abort error, false otherwise.
|
|
380
|
-
*/
|
|
381
|
-
static isAbortError(error, code) {
|
|
382
|
-
const err = BaseError.fromError(error);
|
|
383
|
-
if (Is.stringValue(err.properties?.error) && err.properties.error.startsWith("MoveAbort")) {
|
|
384
|
-
if (Is.number(code)) {
|
|
385
|
-
return err.properties.error.includes(code.toString());
|
|
386
|
-
}
|
|
387
|
-
return true;
|
|
388
|
-
}
|
|
389
|
-
return false;
|
|
390
|
-
}
|
|
391
|
-
/**
|
|
392
|
-
* Prepare and post a transaction using gas station sponsoring.
|
|
393
|
-
* @param config The configuration.
|
|
394
|
-
* @param vaultConnector The vault connector.
|
|
395
|
-
* @param identity The identity of the user to access the vault keys.
|
|
396
|
-
* @param client The client instance.
|
|
397
|
-
* @param owner The owner of the address.
|
|
398
|
-
* @param transaction The transaction to execute.
|
|
399
|
-
* @param options Response options including confirmation behavior.
|
|
400
|
-
* @returns The transaction response.
|
|
401
|
-
*/
|
|
402
|
-
static async prepareAndPostGasStationTransaction(config, vaultConnector, identity, client, owner, transaction, options) {
|
|
403
|
-
Guards.object(this._CLASS_NAME, "config.gasStation", config.gasStation);
|
|
404
|
-
try {
|
|
405
|
-
// Reserve gas from the gas station
|
|
406
|
-
const gasBudget = config.gasBudget ?? 50000000;
|
|
407
|
-
const gasReservation = await this.reserveGas(config, gasBudget);
|
|
408
|
-
// Set transaction parameters for sponsoring
|
|
409
|
-
transaction.setSender(owner);
|
|
410
|
-
transaction.setGasOwner(gasReservation.sponsorAddress);
|
|
411
|
-
transaction.setGasPayment(gasReservation.gasCoins);
|
|
412
|
-
transaction.setGasBudget(gasBudget);
|
|
413
|
-
// Build and sign transaction
|
|
414
|
-
const unsignedTxBytes = await transaction.build({ client });
|
|
415
|
-
// Sign the transaction with the user's private key
|
|
416
|
-
const seed = await this.getSeed(config, vaultConnector, identity);
|
|
417
|
-
const addressKeyPair = Iota.findAddress(config.maxAddressScanRange ?? Iota.DEFAULT_SCAN_RANGE, config.coinType ?? Iota.DEFAULT_COIN_TYPE, seed, owner);
|
|
418
|
-
const keypair = Ed25519Keypair.fromSecretKey(addressKeyPair.privateKey);
|
|
419
|
-
const signature = await keypair.signTransaction(unsignedTxBytes);
|
|
420
|
-
return await this.executeAndConfirmGasStationTransaction(config, client, gasReservation.reservationId, unsignedTxBytes, signature.signature, options);
|
|
421
|
-
}
|
|
422
|
-
catch (error) {
|
|
423
|
-
throw new GeneralError(Iota._CLASS_NAME, "gasStationTransactionFailed", undefined, Iota.extractPayloadError(error));
|
|
424
|
-
}
|
|
425
|
-
}
|
|
426
|
-
/**
|
|
427
|
-
* Reserve gas from the gas station.
|
|
428
|
-
* @param config The configuration containing gas station settings.
|
|
429
|
-
* @param gasBudget The gas budget to reserve.
|
|
430
|
-
* @returns The gas reservation result.
|
|
431
|
-
*/
|
|
432
|
-
static async reserveGas(config, gasBudget) {
|
|
433
|
-
Guards.object(this._CLASS_NAME, "config.gasStation", config.gasStation);
|
|
434
|
-
const requestData = {
|
|
435
|
-
// eslint-disable-next-line camelcase
|
|
436
|
-
gas_budget: gasBudget,
|
|
437
|
-
// eslint-disable-next-line camelcase
|
|
438
|
-
reserve_duration_secs: 30
|
|
439
|
-
};
|
|
440
|
-
const baseUrl = StringHelper.trimTrailingSlashes(config.gasStation.gasStationUrl);
|
|
441
|
-
const result = await FetchHelper.fetchJson(this._CLASS_NAME, `${baseUrl}/v1/reserve_gas`, HttpMethod.POST, requestData, {
|
|
442
|
-
headers: {
|
|
443
|
-
Authorization: `Bearer ${config.gasStation.gasStationAuthToken}`
|
|
444
|
-
}
|
|
445
|
-
});
|
|
446
|
-
const apiResponse = result.result;
|
|
447
|
-
return {
|
|
448
|
-
sponsorAddress: apiResponse.sponsor_address,
|
|
449
|
-
reservationId: apiResponse.reservation_id,
|
|
450
|
-
gasCoins: apiResponse.gas_coins
|
|
451
|
-
};
|
|
452
|
-
}
|
|
453
|
-
/**
|
|
454
|
-
* Execute a sponsored transaction through the gas station.
|
|
455
|
-
* @param config The configuration containing gas station settings.
|
|
456
|
-
* @param reservationId The reservation ID from gas reservation.
|
|
457
|
-
* @param transactionBytes The unsigned transaction bytes.
|
|
458
|
-
* @param userSignature The user's signature.
|
|
459
|
-
* @returns The transaction response.
|
|
460
|
-
*/
|
|
461
|
-
static async executeGasStationTransaction(config, reservationId, transactionBytes, userSignature) {
|
|
462
|
-
Guards.object(this._CLASS_NAME, "config.gasStation", config.gasStation);
|
|
463
|
-
const requestData = {
|
|
464
|
-
// eslint-disable-next-line camelcase
|
|
465
|
-
reservation_id: reservationId,
|
|
466
|
-
// eslint-disable-next-line camelcase
|
|
467
|
-
tx_bytes: toB64(transactionBytes),
|
|
468
|
-
// eslint-disable-next-line camelcase
|
|
469
|
-
user_sig: userSignature
|
|
470
|
-
};
|
|
471
|
-
const baseUrl = StringHelper.trimTrailingSlashes(config.gasStation.gasStationUrl);
|
|
472
|
-
const result = await FetchHelper.fetchJson(this._CLASS_NAME, `${baseUrl}/v1/execute_tx`, HttpMethod.POST, requestData, {
|
|
473
|
-
headers: {
|
|
474
|
-
Authorization: `Bearer ${config.gasStation.gasStationAuthToken}`
|
|
475
|
-
}
|
|
476
|
-
});
|
|
477
|
-
const effectsData = result.effects;
|
|
478
|
-
// Match IotaTransactionBlockResponse format
|
|
479
|
-
return {
|
|
480
|
-
digest: effectsData.transactionDigest,
|
|
481
|
-
effects: effectsData,
|
|
482
|
-
events: [],
|
|
483
|
-
objectChanges: [],
|
|
484
|
-
confirmedLocalExecution: true
|
|
485
|
-
};
|
|
486
|
-
}
|
|
487
|
-
/**
|
|
488
|
-
* Execute and confirm a gas station transaction.
|
|
489
|
-
* @param config The configuration containing gas station settings.
|
|
490
|
-
* @param client The IOTA client for confirmation.
|
|
491
|
-
* @param reservationId The reservation ID from gas reservation.
|
|
492
|
-
* @param transactionBytes The unsigned transaction bytes.
|
|
493
|
-
* @param userSignature The user's signature.
|
|
494
|
-
* @param options Response options including confirmation behavior.
|
|
495
|
-
* @returns The transaction response (confirmed if waitForConfirmation is true).
|
|
496
|
-
*/
|
|
497
|
-
static async executeAndConfirmGasStationTransaction(config, client, reservationId, transactionBytes, userSignature, options) {
|
|
498
|
-
Guards.object(this._CLASS_NAME, "config.gasStation", config.gasStation);
|
|
499
|
-
const response = await this.executeGasStationTransaction(config, reservationId, transactionBytes, userSignature);
|
|
500
|
-
if (options?.waitForConfirmation ?? true) {
|
|
501
|
-
const confirmedTransaction = await Iota.waitForTransactionConfirmation(client, response.digest, config, {
|
|
502
|
-
showEffects: options?.showEffects ?? true,
|
|
503
|
-
showEvents: options?.showEvents ?? true,
|
|
504
|
-
showObjectChanges: options?.showObjectChanges ?? true
|
|
505
|
-
});
|
|
506
|
-
return confirmedTransaction;
|
|
507
|
-
}
|
|
508
|
-
return response;
|
|
509
|
-
}
|
|
510
|
-
}
|
|
511
|
-
|
|
512
|
-
// Copyright 2024 IOTA Stiftung.
|
|
513
|
-
// SPDX-License-Identifier: Apache-2.0.
|
|
514
|
-
/**
|
|
515
|
-
* Utility class providing common smart contract operations for IOTA-based contracts.
|
|
516
|
-
* This class uses composition pattern to provide shared functionality without inheritance complexity.
|
|
517
|
-
*/
|
|
518
|
-
class IotaSmartContractUtils {
|
|
519
|
-
/**
|
|
520
|
-
* Runtime name for the class.
|
|
521
|
-
*/
|
|
522
|
-
static CLASS_NAME = "IotaSmartContractUtils";
|
|
523
|
-
/**
|
|
524
|
-
* Migrate a smart contract object to the current version using admin privileges.
|
|
525
|
-
* This is a generic migration method that works with any IOTA smart contract.
|
|
526
|
-
* @param config The IOTA configuration.
|
|
527
|
-
* @param client The IOTA client instance.
|
|
528
|
-
* @param vaultConnector The vault connector for key management.
|
|
529
|
-
* @param walletConnector The wallet connector for address generation.
|
|
530
|
-
* @param logging Optional logging component.
|
|
531
|
-
* @param gasBudget The gas budget for the transaction.
|
|
532
|
-
* @param identity The identity of the controller with admin privileges.
|
|
533
|
-
* @param objectId The ID of the object to migrate.
|
|
534
|
-
* @param namespace The contract namespace (e.g., "nft", "verifiable_storage").
|
|
535
|
-
* @param packageId The deployed package ID for the contract.
|
|
536
|
-
* @param deploymentConfig The deployment configuration containing object IDs.
|
|
537
|
-
* @param walletAddressIndex Optional wallet address index for the controller.
|
|
538
|
-
* @returns Promise that resolves when migration is complete.
|
|
539
|
-
*/
|
|
540
|
-
static async migrateSmartContract(config, client, vaultConnector, walletConnector, logging, gasBudget, identity, objectId, namespace, packageId, deploymentConfig, walletAddressIndex) {
|
|
541
|
-
try {
|
|
542
|
-
const txb = new Transaction();
|
|
543
|
-
txb.setGasBudget(gasBudget);
|
|
544
|
-
const moduleName = this.getModuleName(namespace);
|
|
545
|
-
// Get admin address for the transaction
|
|
546
|
-
const adminAddress = await this.getPackageControllerAddress(walletConnector, identity, walletAddressIndex);
|
|
547
|
-
// Get the required object IDs from deployment config
|
|
548
|
-
const { adminCapId, migrationStateId } = await this.getContractObjectIds(client, namespace, config.network, deploymentConfig, packageId, adminAddress);
|
|
549
|
-
txb.moveCall({
|
|
550
|
-
target: `${packageId}::${moduleName}::migrate_${moduleName}`,
|
|
551
|
-
arguments: [txb.object(adminCapId), txb.object(migrationStateId), txb.object(objectId)]
|
|
552
|
-
});
|
|
553
|
-
const result = await Iota.prepareAndPostTransaction(config, vaultConnector, logging, identity, client, adminAddress, txb, {
|
|
554
|
-
dryRunLabel: config.enableCostLogging ? "migrate_object" : undefined
|
|
555
|
-
});
|
|
556
|
-
if (result.effects?.status?.status !== "success") {
|
|
557
|
-
throw new GeneralError(IotaSmartContractUtils.CLASS_NAME, "migrationFailed", {
|
|
558
|
-
error: result.effects?.status?.error,
|
|
559
|
-
objectId
|
|
560
|
-
});
|
|
561
|
-
}
|
|
562
|
-
}
|
|
563
|
-
catch (error) {
|
|
564
|
-
throw new GeneralError(IotaSmartContractUtils.CLASS_NAME, "migrateSmartContractFailed", { objectId }, error);
|
|
565
|
-
}
|
|
566
|
-
}
|
|
567
|
-
/**
|
|
568
|
-
* Enable migration operations using admin privileges.
|
|
569
|
-
* @param config The IOTA configuration.
|
|
570
|
-
* @param client The IOTA client instance.
|
|
571
|
-
* @param vaultConnector The vault connector for key management.
|
|
572
|
-
* @param walletConnector The wallet connector for address generation.
|
|
573
|
-
* @param logging Optional logging component.
|
|
574
|
-
* @param gasBudget The gas budget for the transaction.
|
|
575
|
-
* @param identity The identity of the controller with admin privileges.
|
|
576
|
-
* @param namespace The contract namespace (e.g., "nft", "verifiable_storage").
|
|
577
|
-
* @param packageId The deployed package ID for the contract.
|
|
578
|
-
* @param deploymentConfig The deployment configuration containing object IDs.
|
|
579
|
-
* @param walletAddressIndex Optional wallet address index for the controller.
|
|
580
|
-
* @returns Promise that resolves when migration is enabled.
|
|
581
|
-
*/
|
|
582
|
-
static async enableMigration(config, client, vaultConnector, walletConnector, logging, gasBudget, identity, namespace, packageId, deploymentConfig, walletAddressIndex) {
|
|
583
|
-
try {
|
|
584
|
-
const txb = new Transaction();
|
|
585
|
-
txb.setGasBudget(gasBudget);
|
|
586
|
-
const moduleName = this.getModuleName(namespace);
|
|
587
|
-
// Get admin address for the transaction
|
|
588
|
-
const adminAddress = await this.getPackageControllerAddress(walletConnector, identity, walletAddressIndex);
|
|
589
|
-
// Get the required object IDs from deployment config
|
|
590
|
-
const { adminCapId, migrationStateId } = await this.getContractObjectIds(client, namespace, config.network, deploymentConfig, packageId, adminAddress);
|
|
591
|
-
txb.moveCall({
|
|
592
|
-
target: `${packageId}::${moduleName}::enable_migration`,
|
|
593
|
-
arguments: [txb.object(adminCapId), txb.object(migrationStateId)]
|
|
594
|
-
});
|
|
595
|
-
const result = await Iota.prepareAndPostTransaction(config, vaultConnector, logging, identity, client, adminAddress, txb, {
|
|
596
|
-
dryRunLabel: config.enableCostLogging ? "enable_migration" : undefined
|
|
597
|
-
});
|
|
598
|
-
if (result.effects?.status?.status !== "success") {
|
|
599
|
-
throw new GeneralError(IotaSmartContractUtils.CLASS_NAME, "enableMigrationFailed", {
|
|
600
|
-
error: result.effects?.status?.error
|
|
601
|
-
});
|
|
602
|
-
}
|
|
603
|
-
}
|
|
604
|
-
catch (error) {
|
|
605
|
-
throw new GeneralError(IotaSmartContractUtils.CLASS_NAME, "enableMigrationFailed", undefined, error);
|
|
606
|
-
}
|
|
607
|
-
}
|
|
608
|
-
/**
|
|
609
|
-
* Disable migration operations using admin privileges.
|
|
610
|
-
* @param config The IOTA configuration.
|
|
611
|
-
* @param client The IOTA client instance.
|
|
612
|
-
* @param vaultConnector The vault connector for key management.
|
|
613
|
-
* @param walletConnector The wallet connector for address generation.
|
|
614
|
-
* @param logging Optional logging component.
|
|
615
|
-
* @param gasBudget The gas budget for the transaction.
|
|
616
|
-
* @param identity The identity of the controller with admin privileges.
|
|
617
|
-
* @param namespace The contract namespace (e.g., "nft", "verifiable_storage").
|
|
618
|
-
* @param packageId The deployed package ID for the contract.
|
|
619
|
-
* @param deploymentConfig The deployment configuration containing object IDs.
|
|
620
|
-
* @param walletAddressIndex Optional wallet address index for the controller.
|
|
621
|
-
* @returns Promise that resolves when migration is disabled.
|
|
622
|
-
*/
|
|
623
|
-
static async disableMigration(config, client, vaultConnector, walletConnector, logging, gasBudget, identity, namespace, packageId, deploymentConfig, walletAddressIndex) {
|
|
624
|
-
try {
|
|
625
|
-
const txb = new Transaction();
|
|
626
|
-
txb.setGasBudget(gasBudget);
|
|
627
|
-
const moduleName = this.getModuleName(namespace);
|
|
628
|
-
// Get admin address for the transaction
|
|
629
|
-
const adminAddress = await this.getPackageControllerAddress(walletConnector, identity, walletAddressIndex);
|
|
630
|
-
// Get the required object IDs from deployment config
|
|
631
|
-
const { adminCapId, migrationStateId } = await this.getContractObjectIds(client, namespace, config.network, deploymentConfig, packageId, adminAddress);
|
|
632
|
-
txb.moveCall({
|
|
633
|
-
target: `${packageId}::${moduleName}::disable_migration`,
|
|
634
|
-
arguments: [txb.object(adminCapId), txb.object(migrationStateId)]
|
|
635
|
-
});
|
|
636
|
-
const result = await Iota.prepareAndPostTransaction(config, vaultConnector, logging, identity, client, adminAddress, txb, {
|
|
637
|
-
dryRunLabel: config.enableCostLogging ? "disable_migration" : undefined
|
|
638
|
-
});
|
|
639
|
-
if (result.effects?.status?.status !== "success") {
|
|
640
|
-
throw new GeneralError(IotaSmartContractUtils.CLASS_NAME, "disableMigrationFailed", {
|
|
641
|
-
error: result.effects?.status?.error
|
|
642
|
-
});
|
|
643
|
-
}
|
|
644
|
-
}
|
|
645
|
-
catch (error) {
|
|
646
|
-
throw new GeneralError(IotaSmartContractUtils.CLASS_NAME, "disableMigrationFailed", undefined, error);
|
|
647
|
-
}
|
|
648
|
-
}
|
|
649
|
-
/**
|
|
650
|
-
* Check if migration is currently active for a smart contract.
|
|
651
|
-
* @param config The IOTA configuration.
|
|
652
|
-
* @param client The IOTA client instance.
|
|
653
|
-
* @param namespace The contract namespace (e.g., "nft", "verifiable_storage").
|
|
654
|
-
* @param packageId The deployed package ID for the contract.
|
|
655
|
-
* @param deploymentConfig The deployment configuration containing object IDs.
|
|
656
|
-
* @param identity The identity for MigrationState discovery.
|
|
657
|
-
* @param walletConnector The wallet connector for address generation.
|
|
658
|
-
* @param walletAddressIndex Optional wallet address index.
|
|
659
|
-
* @returns True if migration is enabled, false otherwise.
|
|
660
|
-
*/
|
|
661
|
-
static async isMigrationActive(config, client, namespace, packageId, deploymentConfig, identity, walletConnector, walletAddressIndex) {
|
|
662
|
-
try {
|
|
663
|
-
// Get admin address for discovery
|
|
664
|
-
const adminAddress = await this.getPackageControllerAddress(walletConnector, identity, walletAddressIndex);
|
|
665
|
-
// Get the migration state ID
|
|
666
|
-
const { migrationStateId } = await this.getContractObjectIds(client, namespace, config.network, deploymentConfig, packageId, adminAddress);
|
|
667
|
-
const migrationStateResponse = await client.getObject({
|
|
668
|
-
id: migrationStateId,
|
|
669
|
-
options: {
|
|
670
|
-
showContent: true,
|
|
671
|
-
showType: true
|
|
672
|
-
}
|
|
673
|
-
});
|
|
674
|
-
if (!migrationStateResponse.data?.content) {
|
|
675
|
-
throw new GeneralError(IotaSmartContractUtils.CLASS_NAME, "migrationStateNotReadable", {
|
|
676
|
-
migrationStateId
|
|
677
|
-
});
|
|
678
|
-
}
|
|
679
|
-
const content = migrationStateResponse.data.content;
|
|
680
|
-
if (content.dataType === "moveObject" && Is.objectValue(content.fields)) {
|
|
681
|
-
const fields = content.fields;
|
|
682
|
-
return Is.boolean(fields.enabled) ? fields.enabled : false;
|
|
683
|
-
}
|
|
684
|
-
return false;
|
|
685
|
-
}
|
|
686
|
-
catch (error) {
|
|
687
|
-
throw new GeneralError(IotaSmartContractUtils.CLASS_NAME, "isMigrationActiveFailed", undefined, error);
|
|
688
|
-
}
|
|
689
|
-
}
|
|
690
|
-
/**
|
|
691
|
-
* Get the current contract version from the deployed smart contract.
|
|
692
|
-
* @param config The IOTA configuration.
|
|
693
|
-
* @param client The IOTA client instance.
|
|
694
|
-
* @param namespace The contract namespace (e.g., "nft", "verifiable_storage").
|
|
695
|
-
* @param packageId The deployed package ID for the contract.
|
|
696
|
-
* @param identity The identity for package controller address.
|
|
697
|
-
* @param walletConnector The wallet connector for address generation.
|
|
698
|
-
* @param walletAddressIndex Optional wallet address index.
|
|
699
|
-
* @returns The current version number of the contract.
|
|
700
|
-
*/
|
|
701
|
-
static async getCurrentContractVersion(config, client, namespace, packageId, identity, walletConnector, walletAddressIndex) {
|
|
702
|
-
try {
|
|
703
|
-
const tx = new Transaction();
|
|
704
|
-
const moduleName = this.getModuleName(namespace);
|
|
705
|
-
tx.moveCall({
|
|
706
|
-
target: `${packageId}::${moduleName}::get_current_version`,
|
|
707
|
-
arguments: []
|
|
708
|
-
});
|
|
709
|
-
const controllerAddress = await this.getPackageControllerAddress(walletConnector, identity, walletAddressIndex);
|
|
710
|
-
const result = await client.devInspectTransactionBlock({
|
|
711
|
-
sender: controllerAddress,
|
|
712
|
-
transactionBlock: tx
|
|
713
|
-
});
|
|
714
|
-
if (Is.arrayValue(result.results) &&
|
|
715
|
-
Is.object(result.results[0]) &&
|
|
716
|
-
Is.arrayValue(result.results[0].returnValues)) {
|
|
717
|
-
const versionBytes = result.results[0].returnValues[0];
|
|
718
|
-
// Convert to Uint8Array if it's a regular array
|
|
719
|
-
const byteData = versionBytes[0];
|
|
720
|
-
if (!Is.arrayValue(byteData) && !Is.uint8Array(byteData)) {
|
|
721
|
-
throw new GeneralError(this.CLASS_NAME, "invalidVersionData");
|
|
722
|
-
}
|
|
723
|
-
// Convert to Uint8Array for BCS parsing
|
|
724
|
-
const uint8Data = Is.uint8Array(byteData) ? byteData : new Uint8Array(byteData);
|
|
725
|
-
// The version is returned as a u64, decode it from bytes using BCS
|
|
726
|
-
// IOTA Move contracts return data in BCS format
|
|
727
|
-
const version = Number(bcs.u64().parse(uint8Data));
|
|
728
|
-
return version;
|
|
729
|
-
}
|
|
730
|
-
throw new GeneralError(IotaSmartContractUtils.CLASS_NAME, "getCurrentContractVersionNoData", {
|
|
731
|
-
resultExists: Is.arrayValue(result.results),
|
|
732
|
-
resultLength: Is.arrayValue(result.results) ? result.results.length : 0,
|
|
733
|
-
hasReturnValues: Is.arrayValue(result.results?.[0]?.returnValues)
|
|
734
|
-
});
|
|
735
|
-
}
|
|
736
|
-
catch (error) {
|
|
737
|
-
throw new GeneralError(IotaSmartContractUtils.CLASS_NAME, "getCurrentContractVersionFailed", undefined, error);
|
|
738
|
-
}
|
|
739
|
-
}
|
|
740
|
-
/**
|
|
741
|
-
* Validate that an object version is compatible with the current contract.
|
|
742
|
-
* @param config The IOTA configuration.
|
|
743
|
-
* @param client The IOTA client instance.
|
|
744
|
-
* @param namespace The contract namespace (e.g., "nft", "verifiable_storage").
|
|
745
|
-
* @param packageId The deployed package ID for the contract.
|
|
746
|
-
* @param identity The identity for version checking.
|
|
747
|
-
* @param objectId The object ID to validate.
|
|
748
|
-
* @param walletConnector The wallet connector for address generation.
|
|
749
|
-
* @param versionExtractor Function to extract version from object content.
|
|
750
|
-
* @param walletAddressIndex Optional wallet address index.
|
|
751
|
-
* @returns True if the object version is compatible, false otherwise.
|
|
752
|
-
*/
|
|
753
|
-
static async validateObjectVersion(config, client, namespace, packageId, identity, objectId, walletConnector, versionExtractor, walletAddressIndex) {
|
|
754
|
-
try {
|
|
755
|
-
// Get current contract version
|
|
756
|
-
const currentVersion = await this.getCurrentContractVersion(config, client, namespace, packageId, identity, walletConnector, walletAddressIndex);
|
|
757
|
-
// Get object version
|
|
758
|
-
const objectResponse = await client.getObject({
|
|
759
|
-
id: objectId,
|
|
760
|
-
options: {
|
|
761
|
-
showContent: true,
|
|
762
|
-
showType: true
|
|
763
|
-
}
|
|
764
|
-
});
|
|
765
|
-
if (!objectResponse.data?.content) {
|
|
766
|
-
throw new GeneralError(IotaSmartContractUtils.CLASS_NAME, "objectNotReadable", {
|
|
767
|
-
objectId
|
|
768
|
-
});
|
|
769
|
-
}
|
|
770
|
-
const content = objectResponse.data.content;
|
|
771
|
-
if (content.dataType === "moveObject" && Is.objectValue(content.fields)) {
|
|
772
|
-
const objectVersion = versionExtractor(content);
|
|
773
|
-
return objectVersion <= currentVersion;
|
|
774
|
-
}
|
|
775
|
-
throw new GeneralError(IotaSmartContractUtils.CLASS_NAME, "objectInvalidFormat", {
|
|
776
|
-
objectId,
|
|
777
|
-
content
|
|
778
|
-
});
|
|
779
|
-
}
|
|
780
|
-
catch (error) {
|
|
781
|
-
throw new GeneralError(IotaSmartContractUtils.CLASS_NAME, "validateObjectVersionFailed", { objectId }, error);
|
|
782
|
-
}
|
|
783
|
-
}
|
|
784
|
-
/**
|
|
785
|
-
* Get the module name for a given namespace.
|
|
786
|
-
* @param namespace The contract namespace.
|
|
787
|
-
* @returns The module name in snake_case format.
|
|
788
|
-
* @internal
|
|
789
|
-
*/
|
|
790
|
-
static getModuleName(namespace) {
|
|
791
|
-
// Convert namespace to snake_case for module name
|
|
792
|
-
return StringHelper.snakeCase(namespace);
|
|
793
|
-
}
|
|
794
|
-
/**
|
|
795
|
-
* Get the package controller address for transactions.
|
|
796
|
-
* @param walletConnector The wallet connector for address generation.
|
|
797
|
-
* @param identity The identity to use.
|
|
798
|
-
* @param addressIndex Optional address index to use.
|
|
799
|
-
* @returns The controller address.
|
|
800
|
-
* @internal
|
|
801
|
-
*/
|
|
802
|
-
static async getPackageControllerAddress(walletConnector, identity, addressIndex = 0) {
|
|
803
|
-
const addresses = await walletConnector.getAddresses(identity, 0, addressIndex, 1);
|
|
804
|
-
return addresses[0];
|
|
805
|
-
}
|
|
806
|
-
/**
|
|
807
|
-
* Get contract object IDs (AdminCap and MigrationState) from deployment config with fallback discovery.
|
|
808
|
-
* @param client The IOTA client instance.
|
|
809
|
-
* @param namespace The contract namespace.
|
|
810
|
-
* @param network The network name.
|
|
811
|
-
* @param deploymentConfig The deployment configuration.
|
|
812
|
-
* @param packageId The package ID.
|
|
813
|
-
* @param adminAddress The admin address for object discovery.
|
|
814
|
-
* @returns Object containing adminCapId and migrationStateId.
|
|
815
|
-
* @internal
|
|
816
|
-
*/
|
|
817
|
-
static async getContractObjectIds(client, namespace, network, deploymentConfig, packageId, adminAddress) {
|
|
818
|
-
try {
|
|
819
|
-
// First try to load from deployment JSON
|
|
820
|
-
const networkConfig = deploymentConfig[network];
|
|
821
|
-
if (Is.objectValue(networkConfig)) {
|
|
822
|
-
const migrationStateId = networkConfig.migrationStateId;
|
|
823
|
-
// AdminCap must be discovered from blockchain (not stored in JSON)
|
|
824
|
-
const adminCapId = await this.discoverAdminCap(client, packageId, namespace, adminAddress);
|
|
825
|
-
if (Is.stringValue(migrationStateId) && Is.stringValue(adminCapId)) {
|
|
826
|
-
return { adminCapId, migrationStateId };
|
|
827
|
-
}
|
|
828
|
-
}
|
|
829
|
-
// Fallback: discover both from blockchain
|
|
830
|
-
return await this.discoverContractObjectsFromBlockchain(client, packageId, namespace, adminAddress);
|
|
831
|
-
}
|
|
832
|
-
catch (error) {
|
|
833
|
-
throw new GeneralError(IotaSmartContractUtils.CLASS_NAME, "getContractObjectIdsFailed", { namespace, network, packageId }, error);
|
|
834
|
-
}
|
|
835
|
-
}
|
|
836
|
-
/**
|
|
837
|
-
* Discover AdminCap object from the blockchain.
|
|
838
|
-
* @param client The IOTA client instance.
|
|
839
|
-
* @param packageId The package ID.
|
|
840
|
-
* @param namespace The contract namespace.
|
|
841
|
-
* @param adminAddress The admin address.
|
|
842
|
-
* @returns The AdminCap object ID.
|
|
843
|
-
* @internal
|
|
844
|
-
*/
|
|
845
|
-
static async discoverAdminCap(client, packageId, namespace, adminAddress) {
|
|
846
|
-
const adminCapType = `${packageId}::${this.getModuleName(namespace)}::AdminCap`;
|
|
847
|
-
const adminCapObjects = await client.getOwnedObjects({
|
|
848
|
-
owner: adminAddress,
|
|
849
|
-
filter: {
|
|
850
|
-
StructType: adminCapType
|
|
851
|
-
},
|
|
852
|
-
options: {
|
|
853
|
-
showContent: true,
|
|
854
|
-
showType: true
|
|
855
|
-
}
|
|
856
|
-
});
|
|
857
|
-
if (Is.arrayValue(adminCapObjects.data) && adminCapObjects.data.length > 0) {
|
|
858
|
-
const adminCapObject = adminCapObjects.data[0];
|
|
859
|
-
if (Is.stringValue(adminCapObject.data?.objectId)) {
|
|
860
|
-
return adminCapObject.data.objectId;
|
|
861
|
-
}
|
|
862
|
-
}
|
|
863
|
-
throw new GeneralError(IotaSmartContractUtils.CLASS_NAME, "adminCapNotFound", {
|
|
864
|
-
adminCapType,
|
|
865
|
-
adminAddress
|
|
866
|
-
});
|
|
867
|
-
}
|
|
868
|
-
/**
|
|
869
|
-
* Discover contract objects from blockchain as fallback.
|
|
870
|
-
* @param client The IOTA client instance.
|
|
871
|
-
* @param packageId The package ID.
|
|
872
|
-
* @param namespace The contract namespace.
|
|
873
|
-
* @param adminAddress The admin address.
|
|
874
|
-
* @returns Object containing adminCapId and migrationStateId.
|
|
875
|
-
* @internal
|
|
876
|
-
*/
|
|
877
|
-
static async discoverContractObjectsFromBlockchain(client, packageId, namespace, adminAddress) {
|
|
878
|
-
// Discover AdminCap
|
|
879
|
-
const adminCapId = await this.discoverAdminCap(client, packageId, namespace, adminAddress);
|
|
880
|
-
// Discover MigrationState through transaction history
|
|
881
|
-
const migrationStateType = `${packageId}::${this.getModuleName(namespace)}::MigrationState`;
|
|
882
|
-
const transactions = await client.queryTransactionBlocks({
|
|
883
|
-
filter: {
|
|
884
|
-
FromAddress: adminAddress
|
|
885
|
-
},
|
|
886
|
-
options: {
|
|
887
|
-
showObjectChanges: true,
|
|
888
|
-
showEffects: true
|
|
889
|
-
},
|
|
890
|
-
limit: 20,
|
|
891
|
-
order: "descending"
|
|
892
|
-
});
|
|
893
|
-
// Look for MigrationState object creation in transaction history
|
|
894
|
-
let migrationStateId;
|
|
895
|
-
for (const tx of transactions.data) {
|
|
896
|
-
const objectChanges = tx.objectChanges;
|
|
897
|
-
if (Is.arrayValue(objectChanges)) {
|
|
898
|
-
for (const change of objectChanges) {
|
|
899
|
-
if ((change.type === "created" || change.type === "mutated") &&
|
|
900
|
-
"objectType" in change &&
|
|
901
|
-
change.objectType === migrationStateType) {
|
|
902
|
-
migrationStateId = change.objectId;
|
|
903
|
-
break;
|
|
904
|
-
}
|
|
905
|
-
}
|
|
906
|
-
if (migrationStateId) {
|
|
907
|
-
break;
|
|
908
|
-
}
|
|
909
|
-
}
|
|
910
|
-
}
|
|
911
|
-
if (!migrationStateId) {
|
|
912
|
-
throw new GeneralError(IotaSmartContractUtils.CLASS_NAME, "migrationStateNotFound", {
|
|
913
|
-
migrationStateType,
|
|
914
|
-
adminAddress
|
|
915
|
-
});
|
|
916
|
-
}
|
|
917
|
-
return { adminCapId, migrationStateId };
|
|
918
|
-
}
|
|
919
|
-
}
|
|
920
|
-
|
|
921
|
-
// Copyright 2024 IOTA Stiftung.
|
|
922
|
-
// SPDX-License-Identifier: Apache-2.0.
|
|
923
|
-
/**
|
|
924
|
-
* Network types supported for deployment
|
|
925
|
-
*/
|
|
926
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
927
|
-
const NetworkTypes = {
|
|
928
|
-
/**
|
|
929
|
-
* Testnet.
|
|
930
|
-
*/
|
|
931
|
-
Testnet: "testnet",
|
|
932
|
-
/**
|
|
933
|
-
* Devnet.
|
|
934
|
-
*/
|
|
935
|
-
Devnet: "devnet",
|
|
936
|
-
/**
|
|
937
|
-
* Mainnet.
|
|
938
|
-
*/
|
|
939
|
-
Mainnet: "mainnet"
|
|
940
|
-
};
|
|
941
|
-
|
|
942
|
-
export { Iota, IotaSmartContractUtils, NetworkTypes };
|