@lucid-evolution/utils 0.1.56 → 0.1.59

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 CHANGED
@@ -36,6 +36,7 @@ __export(src_exports, {
36
36
  addressFromHexOrBech32: () => addressFromHexOrBech32,
37
37
  applyDoubleCborEncoding: () => applyDoubleCborEncoding,
38
38
  applyParamsToScript: () => applyParamsToScript,
39
+ applySingleCborEncoding: () => applySingleCborEncoding,
39
40
  assetsToValue: () => assetsToValue,
40
41
  calculateMinLovelaceFromUTxO: () => calculateMinLovelaceFromUTxO,
41
42
  coreToOutRef: () => coreToOutRef,
@@ -207,6 +208,19 @@ var applyDoubleCborEncoding = (script) => {
207
208
  }
208
209
  }
209
210
  };
211
+ var applySingleCborEncoding = (script) => {
212
+ try {
213
+ (0, import_cbor_x.decode)((0, import_cbor_x.decode)((0, import_core_utils.fromHex)(script)));
214
+ return (0, import_core_utils.toHex)((0, import_cbor_x.decode)((0, import_core_utils.fromHex)(script)));
215
+ } catch (error) {
216
+ try {
217
+ (0, import_cbor_x.decode)((0, import_core_utils.fromHex)(script));
218
+ return script;
219
+ } catch (error2) {
220
+ return (0, import_core_utils.toHex)(Uint8Array.from((0, import_cbor_x.encode)((0, import_core_utils.fromHex)(script).buffer)));
221
+ }
222
+ }
223
+ };
210
224
  var CBOREncodingLevel = (script) => {
211
225
  try {
212
226
  (0, import_cbor_x.decode)((0, import_cbor_x.decode)((0, import_core_utils.fromHex)(script)));
@@ -293,15 +307,21 @@ function validatorToScriptHash(validator) {
293
307
  return CML.NativeScript.from_cbor_hex(validator.script).hash().to_hex();
294
308
  case "PlutusV1":
295
309
  return CML.PlutusScript.from_v1(
296
- CML.PlutusV1Script.from_cbor_hex(validator.script)
310
+ CML.PlutusV1Script.from_cbor_hex(
311
+ applyDoubleCborEncoding(validator.script)
312
+ )
297
313
  ).hash().to_hex();
298
314
  case "PlutusV2":
299
315
  return CML.PlutusScript.from_v2(
300
- CML.PlutusV2Script.from_cbor_hex(validator.script)
316
+ CML.PlutusV2Script.from_cbor_hex(
317
+ applyDoubleCborEncoding(validator.script)
318
+ )
301
319
  ).hash().to_hex();
302
320
  case "PlutusV3":
303
321
  return CML.PlutusScript.from_v3(
304
- CML.PlutusV3Script.from_cbor_hex(validator.script)
322
+ CML.PlutusV3Script.from_cbor_hex(
323
+ applyDoubleCborEncoding(validator.script)
324
+ )
305
325
  ).hash().to_hex();
306
326
  default:
307
327
  throw new Error("No variant matched");
@@ -315,15 +335,21 @@ function toScriptRef(script) {
315
335
  );
316
336
  case "PlutusV1":
317
337
  return CML.Script.new_plutus_v1(
318
- CML.PlutusV1Script.from_cbor_hex(script.script)
338
+ CML.PlutusV1Script.from_cbor_hex(
339
+ applyDoubleCborEncoding(script.script)
340
+ )
319
341
  );
320
342
  case "PlutusV2":
321
343
  return CML.Script.new_plutus_v2(
322
- CML.PlutusV2Script.from_cbor_hex(script.script)
344
+ CML.PlutusV2Script.from_cbor_hex(
345
+ applyDoubleCborEncoding(script.script)
346
+ )
323
347
  );
324
348
  case "PlutusV3":
325
349
  return CML.Script.new_plutus_v3(
326
- CML.PlutusV3Script.from_cbor_hex(script.script)
350
+ CML.PlutusV3Script.from_cbor_hex(
351
+ applyDoubleCborEncoding(script.script)
352
+ )
327
353
  );
328
354
  default:
329
355
  throw new Error("No variant matched.");
@@ -360,26 +386,21 @@ function mintingPolicyToId(mintingPolicy) {
360
386
  return validatorToScriptHash(mintingPolicy);
361
387
  }
362
388
  function applyParamsToScript(plutusScript, params, type) {
363
- const encodingLevel = CBOREncodingLevel(plutusScript);
364
- const program = encodingLevel === "double" ? (0, import_uplc.parseUPLC)((0, import_cbor_x2.decode)((0, import_cbor_x2.decode)((0, import_core_utils2.fromHex)(plutusScript))), "flat") : (0, import_uplc.parseUPLC)((0, import_cbor_x2.decode)((0, import_core_utils2.fromHex)(plutusScript)), "flat");
389
+ const program = (0, import_uplc.parseUPLC)(
390
+ (0, import_cbor_x2.decode)((0, import_cbor_x2.decode)((0, import_core_utils2.fromHex)(applyDoubleCborEncoding(plutusScript)))),
391
+ "flat"
392
+ );
365
393
  const parameters = type ? import_plutus.Data.castTo(params, type) : params;
366
- const appliedProgram = parameters.reduce(
367
- (body, currentParameter) => {
368
- const data = import_uplc.UPLCConst.data((0, import_plutus_data.dataFromCbor)(import_plutus.Data.to(currentParameter)));
369
- const appliedParameter = new import_uplc.Application(body, data);
370
- return appliedParameter;
371
- },
372
- program.body
394
+ const appliedProgram = parameters.reduce((body, currentParameter) => {
395
+ const data = import_uplc.UPLCConst.data((0, import_plutus_data.dataFromCbor)(import_plutus.Data.to(currentParameter)));
396
+ const appliedParameter = new import_uplc.Application(body, data);
397
+ return appliedParameter;
398
+ }, program.body);
399
+ return applyDoubleCborEncoding(
400
+ (0, import_core_utils2.toHex)(
401
+ (0, import_uplc.encodeUPLC)(new import_uplc.UPLCProgram(program.version, appliedProgram)).toBuffer().buffer
402
+ )
373
403
  );
374
- const compiledUPLC = (0, import_uplc.encodeUPLC)(
375
- new import_uplc.UPLCProgram(program.version, appliedProgram)
376
- ).toBuffer().buffer.buffer;
377
- switch (encodingLevel) {
378
- case "double":
379
- return (0, import_core_utils2.toHex)((0, import_cbor_x2.encode)((0, import_cbor_x2.encode)(compiledUPLC)));
380
- case "single":
381
- return (0, import_core_utils2.toHex)((0, import_cbor_x2.encode)(compiledUPLC));
382
- }
383
404
  }
384
405
 
385
406
  // src/address.ts
@@ -1669,6 +1690,7 @@ var fromCMLRedeemerTag = (tag) => {
1669
1690
  addressFromHexOrBech32,
1670
1691
  applyDoubleCborEncoding,
1671
1692
  applyParamsToScript,
1693
+ applySingleCborEncoding,
1672
1694
  assetsToValue,
1673
1695
  calculateMinLovelaceFromUTxO,
1674
1696
  coreToOutRef,
package/dist/index.d.cts CHANGED
@@ -66,6 +66,7 @@ declare function validatorToRewardAddress(network: Network, validator: Certifica
66
66
  declare function getAddressDetails(address: string): AddressDetails;
67
67
 
68
68
  declare const applyDoubleCborEncoding: (script: string) => string;
69
+ declare const applySingleCborEncoding: (script: string) => string;
69
70
  declare const CBOREncodingLevel: (script: string) => "double" | "single";
70
71
  declare function datumJsonToCbor(json: DatumJson): Datum;
71
72
 
@@ -96,6 +97,8 @@ declare function fromScriptRef(scriptRef: CML.Script): Script;
96
97
  declare function mintingPolicyToId(mintingPolicy: MintingPolicy): PolicyId;
97
98
  /**
98
99
  * Applies a list of parameters, in the form of the `Data` type, to a CBOR encoded script.
100
+ *
101
+ * The `plutusScript` must be double CBOR encoded(bytes). Ensure to use the `applyDoubleCborEncoding` function.
99
102
  */
100
103
  declare function applyParamsToScript<T extends unknown[] = Data[]>(plutusScript: string, params: Exact<[...T]>, type?: T): string;
101
104
 
@@ -192,4 +195,4 @@ declare const stringify: (data: any) => string;
192
195
  declare const toCMLRedeemerTag: (tag: string) => CML.RedeemerTag;
193
196
  declare const fromCMLRedeemerTag: (tag: CML.RedeemerTag) => RedeemerTag;
194
197
 
195
- export { CBOREncodingLevel, type CMLNative, PROTOCOL_PARAMETERS_DEFAULT, type SortOrder, addAssets, addressFromHexOrBech32, applyDoubleCborEncoding, applyParamsToScript, assetsToValue, calculateMinLovelaceFromUTxO, coreToOutRef, coreToTxOutput, coreToUtxo, coresToOutRefs, coresToTxOutputs, coresToUtxos, createCostModels, credentialToAddress, credentialToRewardAddress, datumJsonToCbor, datumToHash, fromCMLRedeemerTag, fromLabel, fromScriptRef, fromUnit, generatePrivateKey, generateSeedPhrase, getAddressDetails, getInputIndices, getUniqueTokenName, isEqualUTxO, keyHashToCredential, mintingPolicyToId, networkToId, parseCMLNative, paymentCredentialOf, scriptFromCMLNative, scriptFromNative, scriptHashToCredential, selectUTxOs, slotToUnixTime, sortCanonical, sortUTxOs, stakeCredentialOf, stringify, toCMLNativeScript, toCMLRedeemerTag, toLabel, toPublicKey, toScriptRef, toUnit, unixTimeToSlot, utxoToCore, utxoToTransactionInput, utxoToTransactionOutput, utxosToCores, validatorToAddress, validatorToRewardAddress, validatorToScriptHash, valueToAssets };
198
+ export { CBOREncodingLevel, type CMLNative, PROTOCOL_PARAMETERS_DEFAULT, type SortOrder, addAssets, addressFromHexOrBech32, applyDoubleCborEncoding, applyParamsToScript, applySingleCborEncoding, assetsToValue, calculateMinLovelaceFromUTxO, coreToOutRef, coreToTxOutput, coreToUtxo, coresToOutRefs, coresToTxOutputs, coresToUtxos, createCostModels, credentialToAddress, credentialToRewardAddress, datumJsonToCbor, datumToHash, fromCMLRedeemerTag, fromLabel, fromScriptRef, fromUnit, generatePrivateKey, generateSeedPhrase, getAddressDetails, getInputIndices, getUniqueTokenName, isEqualUTxO, keyHashToCredential, mintingPolicyToId, networkToId, parseCMLNative, paymentCredentialOf, scriptFromCMLNative, scriptFromNative, scriptHashToCredential, selectUTxOs, slotToUnixTime, sortCanonical, sortUTxOs, stakeCredentialOf, stringify, toCMLNativeScript, toCMLRedeemerTag, toLabel, toPublicKey, toScriptRef, toUnit, unixTimeToSlot, utxoToCore, utxoToTransactionInput, utxoToTransactionOutput, utxosToCores, validatorToAddress, validatorToRewardAddress, validatorToScriptHash, valueToAssets };
package/dist/index.d.ts CHANGED
@@ -66,6 +66,7 @@ declare function validatorToRewardAddress(network: Network, validator: Certifica
66
66
  declare function getAddressDetails(address: string): AddressDetails;
67
67
 
68
68
  declare const applyDoubleCborEncoding: (script: string) => string;
69
+ declare const applySingleCborEncoding: (script: string) => string;
69
70
  declare const CBOREncodingLevel: (script: string) => "double" | "single";
70
71
  declare function datumJsonToCbor(json: DatumJson): Datum;
71
72
 
@@ -96,6 +97,8 @@ declare function fromScriptRef(scriptRef: CML.Script): Script;
96
97
  declare function mintingPolicyToId(mintingPolicy: MintingPolicy): PolicyId;
97
98
  /**
98
99
  * Applies a list of parameters, in the form of the `Data` type, to a CBOR encoded script.
100
+ *
101
+ * The `plutusScript` must be double CBOR encoded(bytes). Ensure to use the `applyDoubleCborEncoding` function.
99
102
  */
100
103
  declare function applyParamsToScript<T extends unknown[] = Data[]>(plutusScript: string, params: Exact<[...T]>, type?: T): string;
101
104
 
@@ -192,4 +195,4 @@ declare const stringify: (data: any) => string;
192
195
  declare const toCMLRedeemerTag: (tag: string) => CML.RedeemerTag;
193
196
  declare const fromCMLRedeemerTag: (tag: CML.RedeemerTag) => RedeemerTag;
194
197
 
195
- export { CBOREncodingLevel, type CMLNative, PROTOCOL_PARAMETERS_DEFAULT, type SortOrder, addAssets, addressFromHexOrBech32, applyDoubleCborEncoding, applyParamsToScript, assetsToValue, calculateMinLovelaceFromUTxO, coreToOutRef, coreToTxOutput, coreToUtxo, coresToOutRefs, coresToTxOutputs, coresToUtxos, createCostModels, credentialToAddress, credentialToRewardAddress, datumJsonToCbor, datumToHash, fromCMLRedeemerTag, fromLabel, fromScriptRef, fromUnit, generatePrivateKey, generateSeedPhrase, getAddressDetails, getInputIndices, getUniqueTokenName, isEqualUTxO, keyHashToCredential, mintingPolicyToId, networkToId, parseCMLNative, paymentCredentialOf, scriptFromCMLNative, scriptFromNative, scriptHashToCredential, selectUTxOs, slotToUnixTime, sortCanonical, sortUTxOs, stakeCredentialOf, stringify, toCMLNativeScript, toCMLRedeemerTag, toLabel, toPublicKey, toScriptRef, toUnit, unixTimeToSlot, utxoToCore, utxoToTransactionInput, utxoToTransactionOutput, utxosToCores, validatorToAddress, validatorToRewardAddress, validatorToScriptHash, valueToAssets };
198
+ export { CBOREncodingLevel, type CMLNative, PROTOCOL_PARAMETERS_DEFAULT, type SortOrder, addAssets, addressFromHexOrBech32, applyDoubleCborEncoding, applyParamsToScript, applySingleCborEncoding, assetsToValue, calculateMinLovelaceFromUTxO, coreToOutRef, coreToTxOutput, coreToUtxo, coresToOutRefs, coresToTxOutputs, coresToUtxos, createCostModels, credentialToAddress, credentialToRewardAddress, datumJsonToCbor, datumToHash, fromCMLRedeemerTag, fromLabel, fromScriptRef, fromUnit, generatePrivateKey, generateSeedPhrase, getAddressDetails, getInputIndices, getUniqueTokenName, isEqualUTxO, keyHashToCredential, mintingPolicyToId, networkToId, parseCMLNative, paymentCredentialOf, scriptFromCMLNative, scriptFromNative, scriptHashToCredential, selectUTxOs, slotToUnixTime, sortCanonical, sortUTxOs, stakeCredentialOf, stringify, toCMLNativeScript, toCMLRedeemerTag, toLabel, toPublicKey, toScriptRef, toUnit, unixTimeToSlot, utxoToCore, utxoToTransactionInput, utxoToTransactionOutput, utxosToCores, validatorToAddress, validatorToRewardAddress, validatorToScriptHash, valueToAssets };
package/dist/index.js CHANGED
@@ -114,6 +114,19 @@ var applyDoubleCborEncoding = (script) => {
114
114
  }
115
115
  }
116
116
  };
117
+ var applySingleCborEncoding = (script) => {
118
+ try {
119
+ decode(decode(fromHex(script)));
120
+ return toHex(decode(fromHex(script)));
121
+ } catch (error) {
122
+ try {
123
+ decode(fromHex(script));
124
+ return script;
125
+ } catch (error2) {
126
+ return toHex(Uint8Array.from(encode(fromHex(script).buffer)));
127
+ }
128
+ }
129
+ };
117
130
  var CBOREncodingLevel = (script) => {
118
131
  try {
119
132
  decode(decode(fromHex(script)));
@@ -179,7 +192,7 @@ import {
179
192
  UPLCProgram
180
193
  } from "@harmoniclabs/uplc";
181
194
  import { fromHex as fromHex2, toHex as toHex2 } from "@lucid-evolution/core-utils";
182
- import { decode as decode2, encode as encode2 } from "cbor-x";
195
+ import { decode as decode2 } from "cbor-x";
183
196
  import { dataFromCbor } from "@harmoniclabs/plutus-data";
184
197
  function validatorToAddress(network, validator, stakeCredential) {
185
198
  const validatorHash = validatorToScriptHash(validator);
@@ -206,15 +219,21 @@ function validatorToScriptHash(validator) {
206
219
  return CML.NativeScript.from_cbor_hex(validator.script).hash().to_hex();
207
220
  case "PlutusV1":
208
221
  return CML.PlutusScript.from_v1(
209
- CML.PlutusV1Script.from_cbor_hex(validator.script)
222
+ CML.PlutusV1Script.from_cbor_hex(
223
+ applyDoubleCborEncoding(validator.script)
224
+ )
210
225
  ).hash().to_hex();
211
226
  case "PlutusV2":
212
227
  return CML.PlutusScript.from_v2(
213
- CML.PlutusV2Script.from_cbor_hex(validator.script)
228
+ CML.PlutusV2Script.from_cbor_hex(
229
+ applyDoubleCborEncoding(validator.script)
230
+ )
214
231
  ).hash().to_hex();
215
232
  case "PlutusV3":
216
233
  return CML.PlutusScript.from_v3(
217
- CML.PlutusV3Script.from_cbor_hex(validator.script)
234
+ CML.PlutusV3Script.from_cbor_hex(
235
+ applyDoubleCborEncoding(validator.script)
236
+ )
218
237
  ).hash().to_hex();
219
238
  default:
220
239
  throw new Error("No variant matched");
@@ -228,15 +247,21 @@ function toScriptRef(script) {
228
247
  );
229
248
  case "PlutusV1":
230
249
  return CML.Script.new_plutus_v1(
231
- CML.PlutusV1Script.from_cbor_hex(script.script)
250
+ CML.PlutusV1Script.from_cbor_hex(
251
+ applyDoubleCborEncoding(script.script)
252
+ )
232
253
  );
233
254
  case "PlutusV2":
234
255
  return CML.Script.new_plutus_v2(
235
- CML.PlutusV2Script.from_cbor_hex(script.script)
256
+ CML.PlutusV2Script.from_cbor_hex(
257
+ applyDoubleCborEncoding(script.script)
258
+ )
236
259
  );
237
260
  case "PlutusV3":
238
261
  return CML.Script.new_plutus_v3(
239
- CML.PlutusV3Script.from_cbor_hex(script.script)
262
+ CML.PlutusV3Script.from_cbor_hex(
263
+ applyDoubleCborEncoding(script.script)
264
+ )
240
265
  );
241
266
  default:
242
267
  throw new Error("No variant matched.");
@@ -273,26 +298,21 @@ function mintingPolicyToId(mintingPolicy) {
273
298
  return validatorToScriptHash(mintingPolicy);
274
299
  }
275
300
  function applyParamsToScript(plutusScript, params, type) {
276
- const encodingLevel = CBOREncodingLevel(plutusScript);
277
- const program = encodingLevel === "double" ? parseUPLC(decode2(decode2(fromHex2(plutusScript))), "flat") : parseUPLC(decode2(fromHex2(plutusScript)), "flat");
301
+ const program = parseUPLC(
302
+ decode2(decode2(fromHex2(applyDoubleCborEncoding(plutusScript)))),
303
+ "flat"
304
+ );
278
305
  const parameters = type ? Data.castTo(params, type) : params;
279
- const appliedProgram = parameters.reduce(
280
- (body, currentParameter) => {
281
- const data = UPLCConst.data(dataFromCbor(Data.to(currentParameter)));
282
- const appliedParameter = new Application(body, data);
283
- return appliedParameter;
284
- },
285
- program.body
306
+ const appliedProgram = parameters.reduce((body, currentParameter) => {
307
+ const data = UPLCConst.data(dataFromCbor(Data.to(currentParameter)));
308
+ const appliedParameter = new Application(body, data);
309
+ return appliedParameter;
310
+ }, program.body);
311
+ return applyDoubleCborEncoding(
312
+ toHex2(
313
+ encodeUPLC(new UPLCProgram(program.version, appliedProgram)).toBuffer().buffer
314
+ )
286
315
  );
287
- const compiledUPLC = encodeUPLC(
288
- new UPLCProgram(program.version, appliedProgram)
289
- ).toBuffer().buffer.buffer;
290
- switch (encodingLevel) {
291
- case "double":
292
- return toHex2(encode2(encode2(compiledUPLC)));
293
- case "single":
294
- return toHex2(encode2(compiledUPLC));
295
- }
296
316
  }
297
317
 
298
318
  // src/address.ts
@@ -1585,6 +1605,7 @@ export {
1585
1605
  addressFromHexOrBech32,
1586
1606
  applyDoubleCborEncoding,
1587
1607
  applyParamsToScript,
1608
+ applySingleCborEncoding,
1588
1609
  assetsToValue,
1589
1610
  calculateMinLovelaceFromUTxO,
1590
1611
  coreToOutRef,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lucid-evolution/utils",
3
- "version": "0.1.56",
3
+ "version": "0.1.59",
4
4
  "description": "",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",