@lucid-evolution/utils 0.1.56 → 0.1.58

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
@@ -30,7 +30,6 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
30
30
  // src/index.ts
31
31
  var src_exports = {};
32
32
  __export(src_exports, {
33
- CBOREncodingLevel: () => CBOREncodingLevel,
34
33
  PROTOCOL_PARAMETERS_DEFAULT: () => PROTOCOL_PARAMETERS_DEFAULT,
35
34
  addAssets: () => addAssets,
36
35
  addressFromHexOrBech32: () => addressFromHexOrBech32,
@@ -207,19 +206,6 @@ var applyDoubleCborEncoding = (script) => {
207
206
  }
208
207
  }
209
208
  };
210
- var CBOREncodingLevel = (script) => {
211
- try {
212
- (0, import_cbor_x.decode)((0, import_cbor_x.decode)((0, import_core_utils.fromHex)(script)));
213
- return "double";
214
- } catch (error) {
215
- try {
216
- (0, import_cbor_x.decode)((0, import_core_utils.fromHex)(script));
217
- return "single";
218
- } catch (error2) {
219
- throw new Error("Script is not CBOR-encoded or invalid format.");
220
- }
221
- }
222
- };
223
209
  function datumJsonToCbor(json) {
224
210
  const convert = (json2) => {
225
211
  if (!isNaN(json2.int)) {
@@ -293,15 +279,21 @@ function validatorToScriptHash(validator) {
293
279
  return CML.NativeScript.from_cbor_hex(validator.script).hash().to_hex();
294
280
  case "PlutusV1":
295
281
  return CML.PlutusScript.from_v1(
296
- CML.PlutusV1Script.from_cbor_hex(validator.script)
282
+ CML.PlutusV1Script.from_cbor_hex(
283
+ applyDoubleCborEncoding(validator.script)
284
+ )
297
285
  ).hash().to_hex();
298
286
  case "PlutusV2":
299
287
  return CML.PlutusScript.from_v2(
300
- CML.PlutusV2Script.from_cbor_hex(validator.script)
288
+ CML.PlutusV2Script.from_cbor_hex(
289
+ applyDoubleCborEncoding(validator.script)
290
+ )
301
291
  ).hash().to_hex();
302
292
  case "PlutusV3":
303
293
  return CML.PlutusScript.from_v3(
304
- CML.PlutusV3Script.from_cbor_hex(validator.script)
294
+ CML.PlutusV3Script.from_cbor_hex(
295
+ applyDoubleCborEncoding(validator.script)
296
+ )
305
297
  ).hash().to_hex();
306
298
  default:
307
299
  throw new Error("No variant matched");
@@ -315,15 +307,21 @@ function toScriptRef(script) {
315
307
  );
316
308
  case "PlutusV1":
317
309
  return CML.Script.new_plutus_v1(
318
- CML.PlutusV1Script.from_cbor_hex(script.script)
310
+ CML.PlutusV1Script.from_cbor_hex(
311
+ applyDoubleCborEncoding(script.script)
312
+ )
319
313
  );
320
314
  case "PlutusV2":
321
315
  return CML.Script.new_plutus_v2(
322
- CML.PlutusV2Script.from_cbor_hex(script.script)
316
+ CML.PlutusV2Script.from_cbor_hex(
317
+ applyDoubleCborEncoding(script.script)
318
+ )
323
319
  );
324
320
  case "PlutusV3":
325
321
  return CML.Script.new_plutus_v3(
326
- CML.PlutusV3Script.from_cbor_hex(script.script)
322
+ CML.PlutusV3Script.from_cbor_hex(
323
+ applyDoubleCborEncoding(script.script)
324
+ )
327
325
  );
328
326
  default:
329
327
  throw new Error("No variant matched.");
@@ -360,26 +358,21 @@ function mintingPolicyToId(mintingPolicy) {
360
358
  return validatorToScriptHash(mintingPolicy);
361
359
  }
362
360
  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");
361
+ const program = (0, import_uplc.parseUPLC)(
362
+ (0, import_cbor_x2.decode)((0, import_cbor_x2.decode)((0, import_core_utils2.fromHex)(applyDoubleCborEncoding(plutusScript)))),
363
+ "flat"
364
+ );
365
365
  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
366
+ const appliedProgram = parameters.reduce((body, currentParameter) => {
367
+ const data = import_uplc.UPLCConst.data((0, import_plutus_data.dataFromCbor)(import_plutus.Data.to(currentParameter)));
368
+ const appliedParameter = new import_uplc.Application(body, data);
369
+ return appliedParameter;
370
+ }, program.body);
371
+ return applyDoubleCborEncoding(
372
+ (0, import_core_utils2.toHex)(
373
+ (0, import_uplc.encodeUPLC)(new import_uplc.UPLCProgram(program.version, appliedProgram)).toBuffer().buffer
374
+ )
373
375
  );
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
376
  }
384
377
 
385
378
  // src/address.ts
@@ -1663,7 +1656,6 @@ var fromCMLRedeemerTag = (tag) => {
1663
1656
  };
1664
1657
  // Annotate the CommonJS export names for ESM import in node:
1665
1658
  0 && (module.exports = {
1666
- CBOREncodingLevel,
1667
1659
  PROTOCOL_PARAMETERS_DEFAULT,
1668
1660
  addAssets,
1669
1661
  addressFromHexOrBech32,
package/dist/index.d.cts CHANGED
@@ -66,7 +66,6 @@ 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 CBOREncodingLevel: (script: string) => "double" | "single";
70
69
  declare function datumJsonToCbor(json: DatumJson): Datum;
71
70
 
72
71
  declare function createCostModels(costModels: CostModels): CML.CostModels;
@@ -96,6 +95,8 @@ declare function fromScriptRef(scriptRef: CML.Script): Script;
96
95
  declare function mintingPolicyToId(mintingPolicy: MintingPolicy): PolicyId;
97
96
  /**
98
97
  * Applies a list of parameters, in the form of the `Data` type, to a CBOR encoded script.
98
+ *
99
+ * The `plutusScript` must be double CBOR encoded(bytes). Ensure to use the `applyDoubleCborEncoding` function.
99
100
  */
100
101
  declare function applyParamsToScript<T extends unknown[] = Data[]>(plutusScript: string, params: Exact<[...T]>, type?: T): string;
101
102
 
@@ -192,4 +193,4 @@ declare const stringify: (data: any) => string;
192
193
  declare const toCMLRedeemerTag: (tag: string) => CML.RedeemerTag;
193
194
  declare const fromCMLRedeemerTag: (tag: CML.RedeemerTag) => RedeemerTag;
194
195
 
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 };
196
+ export { 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 };
package/dist/index.d.ts CHANGED
@@ -66,7 +66,6 @@ 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 CBOREncodingLevel: (script: string) => "double" | "single";
70
69
  declare function datumJsonToCbor(json: DatumJson): Datum;
71
70
 
72
71
  declare function createCostModels(costModels: CostModels): CML.CostModels;
@@ -96,6 +95,8 @@ declare function fromScriptRef(scriptRef: CML.Script): Script;
96
95
  declare function mintingPolicyToId(mintingPolicy: MintingPolicy): PolicyId;
97
96
  /**
98
97
  * Applies a list of parameters, in the form of the `Data` type, to a CBOR encoded script.
98
+ *
99
+ * The `plutusScript` must be double CBOR encoded(bytes). Ensure to use the `applyDoubleCborEncoding` function.
99
100
  */
100
101
  declare function applyParamsToScript<T extends unknown[] = Data[]>(plutusScript: string, params: Exact<[...T]>, type?: T): string;
101
102
 
@@ -192,4 +193,4 @@ declare const stringify: (data: any) => string;
192
193
  declare const toCMLRedeemerTag: (tag: string) => CML.RedeemerTag;
193
194
  declare const fromCMLRedeemerTag: (tag: CML.RedeemerTag) => RedeemerTag;
194
195
 
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 };
196
+ export { 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 };
package/dist/index.js CHANGED
@@ -114,19 +114,6 @@ var applyDoubleCborEncoding = (script) => {
114
114
  }
115
115
  }
116
116
  };
117
- var CBOREncodingLevel = (script) => {
118
- try {
119
- decode(decode(fromHex(script)));
120
- return "double";
121
- } catch (error) {
122
- try {
123
- decode(fromHex(script));
124
- return "single";
125
- } catch (error2) {
126
- throw new Error("Script is not CBOR-encoded or invalid format.");
127
- }
128
- }
129
- };
130
117
  function datumJsonToCbor(json) {
131
118
  const convert = (json2) => {
132
119
  if (!isNaN(json2.int)) {
@@ -179,7 +166,7 @@ import {
179
166
  UPLCProgram
180
167
  } from "@harmoniclabs/uplc";
181
168
  import { fromHex as fromHex2, toHex as toHex2 } from "@lucid-evolution/core-utils";
182
- import { decode as decode2, encode as encode2 } from "cbor-x";
169
+ import { decode as decode2 } from "cbor-x";
183
170
  import { dataFromCbor } from "@harmoniclabs/plutus-data";
184
171
  function validatorToAddress(network, validator, stakeCredential) {
185
172
  const validatorHash = validatorToScriptHash(validator);
@@ -206,15 +193,21 @@ function validatorToScriptHash(validator) {
206
193
  return CML.NativeScript.from_cbor_hex(validator.script).hash().to_hex();
207
194
  case "PlutusV1":
208
195
  return CML.PlutusScript.from_v1(
209
- CML.PlutusV1Script.from_cbor_hex(validator.script)
196
+ CML.PlutusV1Script.from_cbor_hex(
197
+ applyDoubleCborEncoding(validator.script)
198
+ )
210
199
  ).hash().to_hex();
211
200
  case "PlutusV2":
212
201
  return CML.PlutusScript.from_v2(
213
- CML.PlutusV2Script.from_cbor_hex(validator.script)
202
+ CML.PlutusV2Script.from_cbor_hex(
203
+ applyDoubleCborEncoding(validator.script)
204
+ )
214
205
  ).hash().to_hex();
215
206
  case "PlutusV3":
216
207
  return CML.PlutusScript.from_v3(
217
- CML.PlutusV3Script.from_cbor_hex(validator.script)
208
+ CML.PlutusV3Script.from_cbor_hex(
209
+ applyDoubleCborEncoding(validator.script)
210
+ )
218
211
  ).hash().to_hex();
219
212
  default:
220
213
  throw new Error("No variant matched");
@@ -228,15 +221,21 @@ function toScriptRef(script) {
228
221
  );
229
222
  case "PlutusV1":
230
223
  return CML.Script.new_plutus_v1(
231
- CML.PlutusV1Script.from_cbor_hex(script.script)
224
+ CML.PlutusV1Script.from_cbor_hex(
225
+ applyDoubleCborEncoding(script.script)
226
+ )
232
227
  );
233
228
  case "PlutusV2":
234
229
  return CML.Script.new_plutus_v2(
235
- CML.PlutusV2Script.from_cbor_hex(script.script)
230
+ CML.PlutusV2Script.from_cbor_hex(
231
+ applyDoubleCborEncoding(script.script)
232
+ )
236
233
  );
237
234
  case "PlutusV3":
238
235
  return CML.Script.new_plutus_v3(
239
- CML.PlutusV3Script.from_cbor_hex(script.script)
236
+ CML.PlutusV3Script.from_cbor_hex(
237
+ applyDoubleCborEncoding(script.script)
238
+ )
240
239
  );
241
240
  default:
242
241
  throw new Error("No variant matched.");
@@ -273,26 +272,21 @@ function mintingPolicyToId(mintingPolicy) {
273
272
  return validatorToScriptHash(mintingPolicy);
274
273
  }
275
274
  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");
275
+ const program = parseUPLC(
276
+ decode2(decode2(fromHex2(applyDoubleCborEncoding(plutusScript)))),
277
+ "flat"
278
+ );
278
279
  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
280
+ const appliedProgram = parameters.reduce((body, currentParameter) => {
281
+ const data = UPLCConst.data(dataFromCbor(Data.to(currentParameter)));
282
+ const appliedParameter = new Application(body, data);
283
+ return appliedParameter;
284
+ }, program.body);
285
+ return applyDoubleCborEncoding(
286
+ toHex2(
287
+ encodeUPLC(new UPLCProgram(program.version, appliedProgram)).toBuffer().buffer
288
+ )
286
289
  );
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
290
  }
297
291
 
298
292
  // src/address.ts
@@ -1579,7 +1573,6 @@ var fromCMLRedeemerTag = (tag) => {
1579
1573
  }
1580
1574
  };
1581
1575
  export {
1582
- CBOREncodingLevel,
1583
1576
  PROTOCOL_PARAMETERS_DEFAULT,
1584
1577
  addAssets,
1585
1578
  addressFromHexOrBech32,
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.58",
4
4
  "description": "",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",