@lucid-evolution/utils 0.1.73 → 0.1.74
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/dist/index.cjs +355 -288
- package/dist/index.js +356 -285
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -82,6 +82,9 @@ var scriptFromCMLNative = (cmlNative) => {
|
|
|
82
82
|
};
|
|
83
83
|
};
|
|
84
84
|
|
|
85
|
+
// src/address.ts
|
|
86
|
+
import { withCMLScope as withCMLScope2 } from "@lucid-evolution/core-utils";
|
|
87
|
+
|
|
85
88
|
// src/network.ts
|
|
86
89
|
function networkToId(network) {
|
|
87
90
|
switch (network) {
|
|
@@ -191,108 +194,136 @@ import {
|
|
|
191
194
|
UPLCConst,
|
|
192
195
|
UPLCProgram
|
|
193
196
|
} from "@harmoniclabs/uplc";
|
|
194
|
-
import { fromHex as fromHex2, toHex as toHex2 } from "@lucid-evolution/core-utils";
|
|
197
|
+
import { fromHex as fromHex2, toHex as toHex2, withCMLScope } from "@lucid-evolution/core-utils";
|
|
195
198
|
import { decode as decode2 } from "cbor-x";
|
|
196
199
|
import { dataFromCbor } from "@harmoniclabs/plutus-data";
|
|
197
200
|
function validatorToAddress(network, validator, stakeCredential) {
|
|
198
201
|
const validatorHash = validatorToScriptHash(validator);
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
)
|
|
208
|
-
).to_address().to_bech32(void 0);
|
|
209
|
-
} else {
|
|
210
|
-
return CML.EnterpriseAddress.new(
|
|
211
|
-
networkToId(network),
|
|
212
|
-
CML.Credential.new_script(CML.ScriptHash.from_hex(validatorHash))
|
|
213
|
-
).to_address().to_bech32(void 0);
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
function validatorToScriptHash(validator) {
|
|
217
|
-
switch (validator.type) {
|
|
218
|
-
case "Native":
|
|
219
|
-
return CML.NativeScript.from_cbor_hex(validator.script).hash().to_hex();
|
|
220
|
-
case "PlutusV1":
|
|
221
|
-
return CML.PlutusScript.from_v1(
|
|
222
|
-
CML.PlutusV1Script.from_cbor_hex(
|
|
223
|
-
applyDoubleCborEncoding(validator.script)
|
|
224
|
-
)
|
|
225
|
-
).hash().to_hex();
|
|
226
|
-
case "PlutusV2":
|
|
227
|
-
return CML.PlutusScript.from_v2(
|
|
228
|
-
CML.PlutusV2Script.from_cbor_hex(
|
|
229
|
-
applyDoubleCborEncoding(validator.script)
|
|
230
|
-
)
|
|
231
|
-
).hash().to_hex();
|
|
232
|
-
case "PlutusV3":
|
|
233
|
-
return CML.PlutusScript.from_v3(
|
|
234
|
-
CML.PlutusV3Script.from_cbor_hex(
|
|
235
|
-
applyDoubleCborEncoding(validator.script)
|
|
236
|
-
)
|
|
237
|
-
).hash().to_hex();
|
|
238
|
-
default:
|
|
239
|
-
throw new Error("No variant matched");
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
function toScriptRef(script) {
|
|
243
|
-
switch (script.type) {
|
|
244
|
-
case "Native":
|
|
245
|
-
return CML.Script.new_native(
|
|
246
|
-
CML.NativeScript.from_cbor_hex(script.script)
|
|
247
|
-
);
|
|
248
|
-
case "PlutusV1":
|
|
249
|
-
return CML.Script.new_plutus_v1(
|
|
250
|
-
CML.PlutusV1Script.from_cbor_hex(
|
|
251
|
-
applyDoubleCborEncoding(script.script)
|
|
202
|
+
return withCMLScope((own) => {
|
|
203
|
+
const paymentCredential = own(
|
|
204
|
+
CML.Credential.new_script(own(CML.ScriptHash.from_hex(validatorHash)))
|
|
205
|
+
);
|
|
206
|
+
if (stakeCredential) {
|
|
207
|
+
const stake = stakeCredential.type === "Key" ? own(
|
|
208
|
+
CML.Credential.new_pub_key(
|
|
209
|
+
own(CML.Ed25519KeyHash.from_hex(stakeCredential.hash))
|
|
252
210
|
)
|
|
253
|
-
)
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
CML.PlutusV2Script.from_cbor_hex(
|
|
257
|
-
applyDoubleCborEncoding(script.script)
|
|
211
|
+
) : own(
|
|
212
|
+
CML.Credential.new_script(
|
|
213
|
+
own(CML.ScriptHash.from_hex(stakeCredential.hash))
|
|
258
214
|
)
|
|
259
215
|
);
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
CML.PlutusV3Script.from_cbor_hex(
|
|
263
|
-
applyDoubleCborEncoding(script.script)
|
|
264
|
-
)
|
|
216
|
+
const address2 = own(
|
|
217
|
+
CML.BaseAddress.new(networkToId(network), paymentCredential, stake)
|
|
265
218
|
);
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
219
|
+
return own(address2.to_address()).to_bech32(void 0);
|
|
220
|
+
}
|
|
221
|
+
const address = own(
|
|
222
|
+
CML.EnterpriseAddress.new(networkToId(network), paymentCredential)
|
|
223
|
+
);
|
|
224
|
+
return own(address.to_address()).to_bech32(void 0);
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
function validatorToScriptHash(validator) {
|
|
228
|
+
return withCMLScope((own) => {
|
|
229
|
+
switch (validator.type) {
|
|
230
|
+
case "Native":
|
|
231
|
+
return own(
|
|
232
|
+
own(CML.NativeScript.from_cbor_hex(validator.script)).hash()
|
|
233
|
+
).to_hex();
|
|
234
|
+
case "PlutusV1": {
|
|
235
|
+
const script = own(
|
|
236
|
+
CML.PlutusV1Script.from_cbor_hex(
|
|
237
|
+
applyDoubleCborEncoding(validator.script)
|
|
238
|
+
)
|
|
239
|
+
);
|
|
240
|
+
return own(own(CML.PlutusScript.from_v1(script)).hash()).to_hex();
|
|
241
|
+
}
|
|
242
|
+
case "PlutusV2": {
|
|
243
|
+
const script = own(
|
|
244
|
+
CML.PlutusV2Script.from_cbor_hex(
|
|
245
|
+
applyDoubleCborEncoding(validator.script)
|
|
246
|
+
)
|
|
247
|
+
);
|
|
248
|
+
return own(own(CML.PlutusScript.from_v2(script)).hash()).to_hex();
|
|
249
|
+
}
|
|
250
|
+
case "PlutusV3": {
|
|
251
|
+
const script = own(
|
|
252
|
+
CML.PlutusV3Script.from_cbor_hex(
|
|
253
|
+
applyDoubleCborEncoding(validator.script)
|
|
254
|
+
)
|
|
255
|
+
);
|
|
256
|
+
return own(own(CML.PlutusScript.from_v3(script)).hash()).to_hex();
|
|
257
|
+
}
|
|
258
|
+
default:
|
|
259
|
+
throw new Error("No variant matched");
|
|
260
|
+
}
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
function toScriptRef(script) {
|
|
264
|
+
return withCMLScope((own) => {
|
|
265
|
+
switch (script.type) {
|
|
266
|
+
case "Native":
|
|
267
|
+
return CML.Script.new_native(
|
|
268
|
+
own(CML.NativeScript.from_cbor_hex(script.script))
|
|
269
|
+
);
|
|
270
|
+
case "PlutusV1":
|
|
271
|
+
return CML.Script.new_plutus_v1(
|
|
272
|
+
own(
|
|
273
|
+
CML.PlutusV1Script.from_cbor_hex(
|
|
274
|
+
applyDoubleCborEncoding(script.script)
|
|
275
|
+
)
|
|
276
|
+
)
|
|
277
|
+
);
|
|
278
|
+
case "PlutusV2":
|
|
279
|
+
return CML.Script.new_plutus_v2(
|
|
280
|
+
own(
|
|
281
|
+
CML.PlutusV2Script.from_cbor_hex(
|
|
282
|
+
applyDoubleCborEncoding(script.script)
|
|
283
|
+
)
|
|
284
|
+
)
|
|
285
|
+
);
|
|
286
|
+
case "PlutusV3":
|
|
287
|
+
return CML.Script.new_plutus_v3(
|
|
288
|
+
own(
|
|
289
|
+
CML.PlutusV3Script.from_cbor_hex(
|
|
290
|
+
applyDoubleCborEncoding(script.script)
|
|
291
|
+
)
|
|
292
|
+
)
|
|
293
|
+
);
|
|
294
|
+
default:
|
|
295
|
+
throw new Error("No variant matched.");
|
|
296
|
+
}
|
|
297
|
+
});
|
|
269
298
|
}
|
|
270
299
|
function fromScriptRef(scriptRef) {
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
300
|
+
return withCMLScope((own) => {
|
|
301
|
+
const kind = scriptRef.kind();
|
|
302
|
+
switch (kind) {
|
|
303
|
+
case 0:
|
|
304
|
+
return {
|
|
305
|
+
type: "Native",
|
|
306
|
+
script: own(scriptRef.as_native()).to_cbor_hex()
|
|
307
|
+
};
|
|
308
|
+
case 1:
|
|
309
|
+
return {
|
|
310
|
+
type: "PlutusV1",
|
|
311
|
+
script: own(scriptRef.as_plutus_v1()).to_cbor_hex()
|
|
312
|
+
};
|
|
313
|
+
case 2:
|
|
314
|
+
return {
|
|
315
|
+
type: "PlutusV2",
|
|
316
|
+
script: own(scriptRef.as_plutus_v2()).to_cbor_hex()
|
|
317
|
+
};
|
|
318
|
+
case 3:
|
|
319
|
+
return {
|
|
320
|
+
type: "PlutusV3",
|
|
321
|
+
script: own(scriptRef.as_plutus_v3()).to_cbor_hex()
|
|
322
|
+
};
|
|
323
|
+
default:
|
|
324
|
+
throw new Error("No variant matched.");
|
|
325
|
+
}
|
|
326
|
+
});
|
|
296
327
|
}
|
|
297
328
|
function mintingPolicyToId(mintingPolicy) {
|
|
298
329
|
return validatorToScriptHash(mintingPolicy);
|
|
@@ -328,139 +359,151 @@ function addressFromHexOrBech32(address) {
|
|
|
328
359
|
}
|
|
329
360
|
}
|
|
330
361
|
function credentialToRewardAddress(network, stakeCredential) {
|
|
331
|
-
return
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
362
|
+
return withCMLScope2((own) => {
|
|
363
|
+
const credential = stakeCredential.type === "Key" ? own(
|
|
364
|
+
CML.Credential.new_pub_key(
|
|
365
|
+
own(CML.Ed25519KeyHash.from_hex(stakeCredential.hash))
|
|
366
|
+
)
|
|
367
|
+
) : own(
|
|
368
|
+
CML.Credential.new_script(
|
|
369
|
+
own(CML.ScriptHash.from_hex(stakeCredential.hash))
|
|
370
|
+
)
|
|
371
|
+
);
|
|
372
|
+
const rewardAddress = own(
|
|
373
|
+
CML.RewardAddress.new(networkToId(network), credential)
|
|
374
|
+
);
|
|
375
|
+
return own(rewardAddress.to_address()).to_bech32(void 0);
|
|
376
|
+
});
|
|
339
377
|
}
|
|
340
378
|
function validatorToRewardAddress(network, validator) {
|
|
341
379
|
const validatorHash = validatorToScriptHash(validator);
|
|
342
|
-
return
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
380
|
+
return withCMLScope2((own) => {
|
|
381
|
+
const credential = own(
|
|
382
|
+
CML.Credential.new_script(own(CML.ScriptHash.from_hex(validatorHash)))
|
|
383
|
+
);
|
|
384
|
+
const rewardAddress = own(
|
|
385
|
+
CML.RewardAddress.new(networkToId(network), credential)
|
|
386
|
+
);
|
|
387
|
+
return own(rewardAddress.to_address()).to_bech32(void 0);
|
|
388
|
+
});
|
|
346
389
|
}
|
|
390
|
+
var credentialDetails = (credential, own) => credential.kind() === 0 ? { type: "Key", hash: own(credential.as_pub_key()).to_hex() } : { type: "Script", hash: own(credential.as_script()).to_hex() };
|
|
391
|
+
var addressDetails = (address) => ({
|
|
392
|
+
networkId: address.network_id(),
|
|
393
|
+
bech32: address.to_bech32(void 0),
|
|
394
|
+
hex: address.to_hex()
|
|
395
|
+
});
|
|
347
396
|
function getAddressDetails(address) {
|
|
348
397
|
try {
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
},
|
|
373
|
-
paymentCredential,
|
|
374
|
-
stakeCredential
|
|
375
|
-
};
|
|
398
|
+
return withCMLScope2((own) => {
|
|
399
|
+
const parsedAddress = own(
|
|
400
|
+
CML.BaseAddress.from_address(own(addressFromHexOrBech32(address)))
|
|
401
|
+
);
|
|
402
|
+
const paymentCredential = credentialDetails(
|
|
403
|
+
own(parsedAddress.payment()),
|
|
404
|
+
own
|
|
405
|
+
);
|
|
406
|
+
const stakeCredential = credentialDetails(
|
|
407
|
+
own(parsedAddress.stake()),
|
|
408
|
+
own
|
|
409
|
+
);
|
|
410
|
+
const { networkId, ...details } = addressDetails(
|
|
411
|
+
own(parsedAddress.to_address())
|
|
412
|
+
);
|
|
413
|
+
return {
|
|
414
|
+
type: "Base",
|
|
415
|
+
networkId,
|
|
416
|
+
address: details,
|
|
417
|
+
paymentCredential,
|
|
418
|
+
stakeCredential
|
|
419
|
+
};
|
|
420
|
+
});
|
|
376
421
|
} catch (_e) {
|
|
377
422
|
}
|
|
378
423
|
try {
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
424
|
+
return withCMLScope2((own) => {
|
|
425
|
+
const parsedAddress = own(
|
|
426
|
+
CML.EnterpriseAddress.from_address(
|
|
427
|
+
own(addressFromHexOrBech32(address))
|
|
428
|
+
)
|
|
429
|
+
);
|
|
430
|
+
const paymentCredential = credentialDetails(
|
|
431
|
+
own(parsedAddress.payment()),
|
|
432
|
+
own
|
|
433
|
+
);
|
|
434
|
+
const { networkId, ...details } = addressDetails(
|
|
435
|
+
own(parsedAddress.to_address())
|
|
436
|
+
);
|
|
437
|
+
return {
|
|
438
|
+
type: "Enterprise",
|
|
439
|
+
networkId,
|
|
440
|
+
address: details,
|
|
441
|
+
paymentCredential
|
|
442
|
+
};
|
|
443
|
+
});
|
|
398
444
|
} catch (_e) {
|
|
399
445
|
}
|
|
400
446
|
try {
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
}
|
|
418
|
-
|
|
419
|
-
};
|
|
447
|
+
return withCMLScope2((own) => {
|
|
448
|
+
const parsedAddress = own(
|
|
449
|
+
CML.PointerAddress.from_address(own(addressFromHexOrBech32(address)))
|
|
450
|
+
);
|
|
451
|
+
const paymentCredential = credentialDetails(
|
|
452
|
+
own(parsedAddress.payment()),
|
|
453
|
+
own
|
|
454
|
+
);
|
|
455
|
+
const { networkId, ...details } = addressDetails(
|
|
456
|
+
own(parsedAddress.to_address())
|
|
457
|
+
);
|
|
458
|
+
return {
|
|
459
|
+
type: "Pointer",
|
|
460
|
+
networkId,
|
|
461
|
+
address: details,
|
|
462
|
+
paymentCredential
|
|
463
|
+
};
|
|
464
|
+
});
|
|
420
465
|
} catch (_e) {
|
|
421
466
|
}
|
|
422
467
|
try {
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
type: "Reward",
|
|
435
|
-
|
|
436
|
-
address: {
|
|
437
|
-
bech32: parsedAddress.to_address().to_bech32(void 0),
|
|
438
|
-
hex: parsedAddress.to_address().to_hex()
|
|
439
|
-
},
|
|
440
|
-
stakeCredential
|
|
441
|
-
};
|
|
468
|
+
return withCMLScope2((own) => {
|
|
469
|
+
const parsedAddress = own(
|
|
470
|
+
CML.RewardAddress.from_address(own(addressFromHexOrBech32(address)))
|
|
471
|
+
);
|
|
472
|
+
const stakeCredential = credentialDetails(
|
|
473
|
+
own(parsedAddress.payment()),
|
|
474
|
+
own
|
|
475
|
+
);
|
|
476
|
+
const { networkId, ...details } = addressDetails(
|
|
477
|
+
own(parsedAddress.to_address())
|
|
478
|
+
);
|
|
479
|
+
return { type: "Reward", networkId, address: details, stakeCredential };
|
|
480
|
+
});
|
|
442
481
|
} catch (_e) {
|
|
443
482
|
}
|
|
444
483
|
try {
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
484
|
+
return withCMLScope2((own) => {
|
|
485
|
+
const parsedAddress = own(
|
|
486
|
+
((address2) => {
|
|
487
|
+
try {
|
|
488
|
+
return CML.ByronAddress.from_cbor_hex(address2);
|
|
489
|
+
} catch (_e) {
|
|
490
|
+
try {
|
|
491
|
+
return CML.ByronAddress.from_base58(address2);
|
|
492
|
+
} catch (_e2) {
|
|
493
|
+
throw new Error("Could not deserialize address.");
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
})(address)
|
|
497
|
+
);
|
|
498
|
+
return {
|
|
499
|
+
type: "Byron",
|
|
500
|
+
networkId: own(parsedAddress.content()).network_id(),
|
|
501
|
+
address: {
|
|
502
|
+
bech32: "",
|
|
503
|
+
hex: own(parsedAddress.to_address()).to_hex()
|
|
453
504
|
}
|
|
454
|
-
}
|
|
455
|
-
})
|
|
456
|
-
return {
|
|
457
|
-
type: "Byron",
|
|
458
|
-
networkId: parsedAddress.content().network_id(),
|
|
459
|
-
address: {
|
|
460
|
-
bech32: "",
|
|
461
|
-
hex: parsedAddress.to_address().to_hex()
|
|
462
|
-
}
|
|
463
|
-
};
|
|
505
|
+
};
|
|
506
|
+
});
|
|
464
507
|
} catch (_e) {
|
|
465
508
|
}
|
|
466
509
|
throw new Error("No address type matched for: " + address);
|
|
@@ -1274,50 +1317,64 @@ function slotToUnixTime(network, slot) {
|
|
|
1274
1317
|
return slotToBeginUnixTime(slot, SLOT_CONFIG_NETWORK[network]);
|
|
1275
1318
|
}
|
|
1276
1319
|
|
|
1320
|
+
// src/utxo.ts
|
|
1321
|
+
import { withCMLScope as withCMLScope4 } from "@lucid-evolution/core-utils";
|
|
1322
|
+
|
|
1277
1323
|
// src/value.ts
|
|
1278
|
-
import {
|
|
1324
|
+
import {
|
|
1325
|
+
fromHex as fromHex4,
|
|
1326
|
+
toHex as toHex3,
|
|
1327
|
+
withCMLScope as withCMLScope3
|
|
1328
|
+
} from "@lucid-evolution/core-utils";
|
|
1279
1329
|
import { pipe } from "effect";
|
|
1280
1330
|
function valueToAssets(value) {
|
|
1281
1331
|
const assets = {};
|
|
1282
1332
|
assets["lovelace"] = value.coin();
|
|
1283
1333
|
if (value.has_multiassets()) {
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1334
|
+
withCMLScope3((own) => {
|
|
1335
|
+
const ma = own(value.multi_asset());
|
|
1336
|
+
const multiAssets = own(ma.keys());
|
|
1337
|
+
for (let j = 0; j < multiAssets.len(); j++) {
|
|
1338
|
+
const policy = own(multiAssets.get(j));
|
|
1339
|
+
const policyAssets = own(ma.get_assets(policy));
|
|
1340
|
+
const assetNames = own(policyAssets.keys());
|
|
1341
|
+
for (let k = 0; k < assetNames.len(); k++) {
|
|
1342
|
+
const policyAsset = own(assetNames.get(k));
|
|
1343
|
+
const quantity = policyAssets.get(policyAsset);
|
|
1344
|
+
const unit = policy.to_hex() + policyAsset.to_js_value();
|
|
1345
|
+
assets[unit] = quantity;
|
|
1346
|
+
}
|
|
1295
1347
|
}
|
|
1296
|
-
}
|
|
1348
|
+
});
|
|
1297
1349
|
}
|
|
1298
1350
|
return assets;
|
|
1299
1351
|
}
|
|
1300
1352
|
function assetsToValue(assets) {
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1353
|
+
return withCMLScope3((own) => {
|
|
1354
|
+
const multiAsset = own(CML.MultiAsset.new());
|
|
1355
|
+
const lovelace = assets["lovelace"] ? BigInt(assets["lovelace"]) : 0n;
|
|
1356
|
+
const units = Object.keys(assets);
|
|
1357
|
+
const policies = Array.from(
|
|
1358
|
+
new Set(
|
|
1359
|
+
units.filter((unit) => unit !== "lovelace").map((unit) => unit.slice(0, 56))
|
|
1360
|
+
)
|
|
1361
|
+
);
|
|
1362
|
+
for (const policy of policies) {
|
|
1363
|
+
const policyUnits = units.filter((unit) => unit.slice(0, 56) === policy);
|
|
1364
|
+
const assetsValue = own(CML.MapAssetNameToCoin.new());
|
|
1365
|
+
for (const unit of policyUnits) {
|
|
1366
|
+
assetsValue.insert(
|
|
1367
|
+
own(CML.AssetName.from_hex(unit.slice(56))),
|
|
1368
|
+
BigInt(assets[unit])
|
|
1369
|
+
);
|
|
1370
|
+
}
|
|
1371
|
+
multiAsset.insert_assets(
|
|
1372
|
+
own(CML.ScriptHash.from_hex(policy)),
|
|
1373
|
+
assetsValue
|
|
1316
1374
|
);
|
|
1317
1375
|
}
|
|
1318
|
-
|
|
1319
|
-
}
|
|
1320
|
-
return CML.Value.new(lovelace, multiAsset);
|
|
1376
|
+
return CML.Value.new(lovelace, multiAsset);
|
|
1377
|
+
});
|
|
1321
1378
|
}
|
|
1322
1379
|
function fromUnit(unit) {
|
|
1323
1380
|
const policyId = unit.slice(0, 56);
|
|
@@ -1380,23 +1437,23 @@ var sortCanonical = (assets) => pipe(
|
|
|
1380
1437
|
);
|
|
1381
1438
|
|
|
1382
1439
|
// src/utxo.ts
|
|
1383
|
-
var utxoToTransactionOutput = (utxo) => {
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1440
|
+
var utxoToTransactionOutput = (utxo) => withCMLScope4((own) => {
|
|
1441
|
+
const value = own(assetsToValue(utxo.assets));
|
|
1442
|
+
const builder = own(buildOutput(utxo, own).with_value(value));
|
|
1443
|
+
return own(builder.build()).output();
|
|
1444
|
+
});
|
|
1445
|
+
var utxoToTransactionInput = (utxo) => withCMLScope4(
|
|
1446
|
+
(own) => CML.TransactionInput.new(
|
|
1447
|
+
own(CML.TransactionHash.from_hex(utxo.txHash)),
|
|
1389
1448
|
BigInt(utxo.outputIndex)
|
|
1390
|
-
)
|
|
1391
|
-
|
|
1392
|
-
var utxoToCore = (utxo) =>
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
return utxoCore;
|
|
1399
|
-
};
|
|
1449
|
+
)
|
|
1450
|
+
);
|
|
1451
|
+
var utxoToCore = (utxo) => withCMLScope4(
|
|
1452
|
+
(own) => CML.TransactionUnspentOutput.new(
|
|
1453
|
+
own(utxoToTransactionInput(utxo)),
|
|
1454
|
+
own(utxoToTransactionOutput(utxo))
|
|
1455
|
+
)
|
|
1456
|
+
);
|
|
1400
1457
|
function utxosToCores(utxos) {
|
|
1401
1458
|
const result = [];
|
|
1402
1459
|
for (const utxo of utxos) {
|
|
@@ -1405,11 +1462,10 @@ function utxosToCores(utxos) {
|
|
|
1405
1462
|
return result;
|
|
1406
1463
|
}
|
|
1407
1464
|
function coreToUtxo(coreUtxo) {
|
|
1408
|
-
|
|
1409
|
-
...coreToOutRef(coreUtxo.input()),
|
|
1410
|
-
...coreToTxOutput(coreUtxo.output())
|
|
1411
|
-
};
|
|
1412
|
-
return utxo;
|
|
1465
|
+
return withCMLScope4((own) => ({
|
|
1466
|
+
...coreToOutRef(own(coreUtxo.input())),
|
|
1467
|
+
...coreToTxOutput(own(coreUtxo.output()))
|
|
1468
|
+
}));
|
|
1413
1469
|
}
|
|
1414
1470
|
function coresToUtxos(utxos) {
|
|
1415
1471
|
const result = [];
|
|
@@ -1419,10 +1475,10 @@ function coresToUtxos(utxos) {
|
|
|
1419
1475
|
return result;
|
|
1420
1476
|
}
|
|
1421
1477
|
function coreToOutRef(input) {
|
|
1422
|
-
return {
|
|
1423
|
-
txHash: input.transaction_id().to_hex(),
|
|
1478
|
+
return withCMLScope4((own) => ({
|
|
1479
|
+
txHash: own(input.transaction_id()).to_hex(),
|
|
1424
1480
|
outputIndex: parseInt(input.index().toString())
|
|
1425
|
-
};
|
|
1481
|
+
}));
|
|
1426
1482
|
}
|
|
1427
1483
|
function coresToOutRefs(inputs) {
|
|
1428
1484
|
const result = [];
|
|
@@ -1432,13 +1488,17 @@ function coresToOutRefs(inputs) {
|
|
|
1432
1488
|
return result;
|
|
1433
1489
|
}
|
|
1434
1490
|
function coreToTxOutput(output) {
|
|
1435
|
-
return {
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1491
|
+
return withCMLScope4((own) => {
|
|
1492
|
+
const datum = own(output.datum());
|
|
1493
|
+
const scriptRef = own(output.script_ref());
|
|
1494
|
+
return {
|
|
1495
|
+
assets: valueToAssets(own(output.amount())),
|
|
1496
|
+
address: own(output.address()).to_bech32(void 0),
|
|
1497
|
+
datumHash: own(datum?.as_hash())?.to_hex(),
|
|
1498
|
+
datum: own(datum?.as_datum())?.to_cbor_hex(),
|
|
1499
|
+
scriptRef: scriptRef && fromScriptRef(scriptRef)
|
|
1500
|
+
};
|
|
1501
|
+
});
|
|
1442
1502
|
}
|
|
1443
1503
|
function coresToTxOutputs(outputs) {
|
|
1444
1504
|
let result = [];
|
|
@@ -1523,25 +1583,36 @@ function getInputIndices(indexInputs, allInputs, sorted = false) {
|
|
|
1523
1583
|
else return [];
|
|
1524
1584
|
});
|
|
1525
1585
|
}
|
|
1526
|
-
var calculateMinLovelaceFromUTxO = (coinsPerUtxoByte, utxo) =>
|
|
1527
|
-
assetsToValue(utxo.assets)
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
|
|
1586
|
+
var calculateMinLovelaceFromUTxO = (coinsPerUtxoByte, utxo) => withCMLScope4((own) => {
|
|
1587
|
+
const value = own(assetsToValue(utxo.assets));
|
|
1588
|
+
const builder = own(
|
|
1589
|
+
buildOutput(utxo, own).with_asset_and_min_required_coin(
|
|
1590
|
+
own(value.multi_asset()),
|
|
1591
|
+
coinsPerUtxoByte
|
|
1592
|
+
)
|
|
1593
|
+
);
|
|
1594
|
+
const output = own(own(builder.build()).output());
|
|
1595
|
+
return own(output.amount()).coin();
|
|
1596
|
+
});
|
|
1597
|
+
var buildOutput = (utxo, own) => {
|
|
1598
|
+
const address = own(CML.Address.from_bech32(utxo.address));
|
|
1599
|
+
const addressed = own(
|
|
1600
|
+
own(CML.TransactionOutputBuilder.new()).with_address(address)
|
|
1533
1601
|
);
|
|
1534
|
-
|
|
1602
|
+
const builder = buildDatum(utxo, addressed, own);
|
|
1603
|
+
if (!utxo.scriptRef) return own(builder.next());
|
|
1604
|
+
const scriptRef = own(toScriptRef(utxo.scriptRef));
|
|
1605
|
+
return own(own(builder.with_reference_script(scriptRef)).next());
|
|
1535
1606
|
};
|
|
1536
|
-
var buildDatum = (utxo, builder) => {
|
|
1537
|
-
if (utxo.datumHash && utxo.datum)
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
if (utxo.datum)
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
|
|
1607
|
+
var buildDatum = (utxo, builder, own) => {
|
|
1608
|
+
if (utxo.datumHash && utxo.datum) {
|
|
1609
|
+
const datum = own(CML.PlutusData.from_cbor_hex(utxo.datum));
|
|
1610
|
+
return own(builder.with_communication_data(datum));
|
|
1611
|
+
}
|
|
1612
|
+
if (utxo.datum) {
|
|
1613
|
+
const datum = own(CML.PlutusData.from_cbor_hex(utxo.datum));
|
|
1614
|
+
return own(builder.with_data(own(CML.DatumOption.new_datum(datum))));
|
|
1615
|
+
}
|
|
1545
1616
|
return builder;
|
|
1546
1617
|
};
|
|
1547
1618
|
|