@lucid-evolution/wallet 0.2.3 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -44,7 +44,7 @@ var import_utils = require("@lucid-evolution/utils");
44
44
  var import_core_utils = require("@lucid-evolution/core-utils");
45
45
 
46
46
  // src/core.ts
47
- var CML = __toESM(require("@anastasia-labs/cardano-multiplatform-lib-nodejs"), 1);
47
+ var CML = __toESM(require("@lucid-evolution/cml"), 1);
48
48
 
49
49
  // src/wallet.ts
50
50
  var import_bip39 = require("bip39");
@@ -112,15 +112,18 @@ function walletFromSeed(seed, options = {}) {
112
112
  function discoverOwnUsedTxKeyHashes(tx, ownKeyHashes, ownUtxos) {
113
113
  return (0, import_core_utils.withCMLScope)((own) => {
114
114
  const usedKeyHashes = [];
115
+ const ownUtxoByOutRef = /* @__PURE__ */ new Map();
116
+ for (const utxo of ownUtxos) {
117
+ const key = `${utxo.txHash}#${utxo.outputIndex}`;
118
+ if (!ownUtxoByOutRef.has(key)) ownUtxoByOutRef.set(key, utxo);
119
+ }
115
120
  const txBody = own(tx.body());
116
121
  const inputs = own(txBody.inputs());
117
122
  for (let i = 0; i < inputs.len(); i++) {
118
123
  const input = own(inputs.get(i));
119
124
  const txHash = own(input.transaction_id()).to_hex();
120
125
  const outputIndex = Number(input.index());
121
- const utxo = ownUtxos.find(
122
- (utxo2) => utxo2.txHash === txHash && utxo2.outputIndex === outputIndex
123
- );
126
+ const utxo = ownUtxoByOutRef.get(`${txHash}#${outputIndex}`);
124
127
  if (utxo) {
125
128
  const { paymentCredential } = (0, import_utils.getAddressDetails)(utxo.address);
126
129
  usedKeyHashes.push(paymentCredential?.hash);
@@ -343,16 +346,15 @@ function discoverOwnUsedTxKeyHashes(tx, ownKeyHashes, ownUtxos) {
343
346
  const input = own(collateral.get(i));
344
347
  const txHash = own(input.transaction_id()).to_hex();
345
348
  const outputIndex = Number(input.index());
346
- const utxo = ownUtxos.find(
347
- (utxo2) => utxo2.txHash === txHash && utxo2.outputIndex === outputIndex
348
- );
349
+ const utxo = ownUtxoByOutRef.get(`${txHash}#${outputIndex}`);
349
350
  if (utxo) {
350
351
  const { paymentCredential } = (0, import_utils.getAddressDetails)(utxo.address);
351
352
  usedKeyHashes.push(paymentCredential?.hash);
352
353
  }
353
354
  }
354
355
  }
355
- return usedKeyHashes.filter((k) => ownKeyHashes.includes(k));
356
+ const ownKeyHashSet = new Set(ownKeyHashes);
357
+ return usedKeyHashes.filter((k) => ownKeyHashSet.has(k));
356
358
  });
357
359
  }
358
360
 
@@ -553,6 +555,32 @@ var makeWalletFromAPI = (provider, api) => {
553
555
  );
554
556
  return utxos;
555
557
  },
558
+ // Exposed only when the CIP-30 wallet implements getCollateral. With a
559
+ // UTxO override, only candidates in the override are returned, so a
560
+ // collateral UTxO spent by a pending chained transaction is not reused.
561
+ ...typeof api.getCollateral !== "function" ? {} : {
562
+ getCollateral: async (amount) => {
563
+ const params = amount === void 0 ? void 0 : {
564
+ amount: (0, import_core_utils2.withCMLScope)(
565
+ (own) => (0, import_core_utils2.toHex)(own(CML.Value.from_coin(amount)).to_cbor_bytes())
566
+ )
567
+ };
568
+ const hexes = await api.getCollateral(params) || [];
569
+ const candidates = (0, import_core_utils2.withCMLScope)(
570
+ (own) => hexes.map(
571
+ (utxo) => (0, import_utils2.coreToUtxo)(
572
+ own(
573
+ CML.TransactionUnspentOutput.from_cbor_bytes((0, import_core_utils2.fromHex)(utxo))
574
+ )
575
+ )
576
+ )
577
+ );
578
+ const override = config.overriddenUTxOs;
579
+ return override === void 0 ? candidates : candidates.filter(
580
+ (candidate) => override.some((utxo) => (0, import_utils2.isEqualUTxO)(utxo, candidate))
581
+ );
582
+ }
583
+ },
556
584
  getUtxosCore: async () => {
557
585
  const utxos = config.overriddenUTxOs !== void 0 ? config.overriddenUTxOs.map(import_utils2.utxoToCore) : (await api.getUtxos() || []).map(
558
586
  (utxo) => CML.TransactionUnspentOutput.from_cbor_hex(utxo)
package/dist/index.d.cts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { Address, RewardAddress, PrivateKey, KeyHash, UTxO, Network, Provider, WalletApi, Wallet } from '@lucid-evolution/core-types';
2
- import * as CML from '@anastasia-labs/cardano-multiplatform-lib-nodejs';
2
+ import * as CML from '@lucid-evolution/cml';
3
3
 
4
4
  type FromSeed = {
5
5
  address: Address;
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { Address, RewardAddress, PrivateKey, KeyHash, UTxO, Network, Provider, WalletApi, Wallet } from '@lucid-evolution/core-types';
2
- import * as CML from '@anastasia-labs/cardano-multiplatform-lib-nodejs';
2
+ import * as CML from '@lucid-evolution/cml';
3
3
 
4
4
  type FromSeed = {
5
5
  address: Address;
package/dist/index.js CHANGED
@@ -3,7 +3,7 @@ import { getAddressDetails } from "@lucid-evolution/utils";
3
3
  import { fromHex, withCMLScope } from "@lucid-evolution/core-utils";
4
4
 
5
5
  // src/core.ts
6
- import * as CML from "@anastasia-labs/cardano-multiplatform-lib-nodejs";
6
+ import * as CML from "@lucid-evolution/cml";
7
7
 
8
8
  // src/wallet.ts
9
9
  import { mnemonicToEntropy } from "bip39";
@@ -71,15 +71,18 @@ function walletFromSeed(seed, options = {}) {
71
71
  function discoverOwnUsedTxKeyHashes(tx, ownKeyHashes, ownUtxos) {
72
72
  return withCMLScope((own) => {
73
73
  const usedKeyHashes = [];
74
+ const ownUtxoByOutRef = /* @__PURE__ */ new Map();
75
+ for (const utxo of ownUtxos) {
76
+ const key = `${utxo.txHash}#${utxo.outputIndex}`;
77
+ if (!ownUtxoByOutRef.has(key)) ownUtxoByOutRef.set(key, utxo);
78
+ }
74
79
  const txBody = own(tx.body());
75
80
  const inputs = own(txBody.inputs());
76
81
  for (let i = 0; i < inputs.len(); i++) {
77
82
  const input = own(inputs.get(i));
78
83
  const txHash = own(input.transaction_id()).to_hex();
79
84
  const outputIndex = Number(input.index());
80
- const utxo = ownUtxos.find(
81
- (utxo2) => utxo2.txHash === txHash && utxo2.outputIndex === outputIndex
82
- );
85
+ const utxo = ownUtxoByOutRef.get(`${txHash}#${outputIndex}`);
83
86
  if (utxo) {
84
87
  const { paymentCredential } = getAddressDetails(utxo.address);
85
88
  usedKeyHashes.push(paymentCredential?.hash);
@@ -302,16 +305,15 @@ function discoverOwnUsedTxKeyHashes(tx, ownKeyHashes, ownUtxos) {
302
305
  const input = own(collateral.get(i));
303
306
  const txHash = own(input.transaction_id()).to_hex();
304
307
  const outputIndex = Number(input.index());
305
- const utxo = ownUtxos.find(
306
- (utxo2) => utxo2.txHash === txHash && utxo2.outputIndex === outputIndex
307
- );
308
+ const utxo = ownUtxoByOutRef.get(`${txHash}#${outputIndex}`);
308
309
  if (utxo) {
309
310
  const { paymentCredential } = getAddressDetails(utxo.address);
310
311
  usedKeyHashes.push(paymentCredential?.hash);
311
312
  }
312
313
  }
313
314
  }
314
- return usedKeyHashes.filter((k) => ownKeyHashes.includes(k));
315
+ const ownKeyHashSet = new Set(ownKeyHashes);
316
+ return usedKeyHashes.filter((k) => ownKeyHashSet.has(k));
315
317
  });
316
318
  }
317
319
 
@@ -321,6 +323,7 @@ import {
321
323
  coreToUtxo,
322
324
  credentialToRewardAddress,
323
325
  getAddressDetails as getAddressDetails2,
326
+ isEqualUTxO,
324
327
  utxoToCore
325
328
  } from "@lucid-evolution/utils";
326
329
  import { signData } from "@lucid-evolution/sign_data";
@@ -517,6 +520,32 @@ var makeWalletFromAPI = (provider, api) => {
517
520
  );
518
521
  return utxos;
519
522
  },
523
+ // Exposed only when the CIP-30 wallet implements getCollateral. With a
524
+ // UTxO override, only candidates in the override are returned, so a
525
+ // collateral UTxO spent by a pending chained transaction is not reused.
526
+ ...typeof api.getCollateral !== "function" ? {} : {
527
+ getCollateral: async (amount) => {
528
+ const params = amount === void 0 ? void 0 : {
529
+ amount: withCMLScope2(
530
+ (own) => toHex(own(CML.Value.from_coin(amount)).to_cbor_bytes())
531
+ )
532
+ };
533
+ const hexes = await api.getCollateral(params) || [];
534
+ const candidates = withCMLScope2(
535
+ (own) => hexes.map(
536
+ (utxo) => coreToUtxo(
537
+ own(
538
+ CML.TransactionUnspentOutput.from_cbor_bytes(fromHex2(utxo))
539
+ )
540
+ )
541
+ )
542
+ );
543
+ const override = config.overriddenUTxOs;
544
+ return override === void 0 ? candidates : candidates.filter(
545
+ (candidate) => override.some((utxo) => isEqualUTxO(utxo, candidate))
546
+ );
547
+ }
548
+ },
520
549
  getUtxosCore: async () => {
521
550
  const utxos = config.overriddenUTxOs !== void 0 ? config.overriddenUTxOs.map(utxoToCore) : (await api.getUtxos() || []).map(
522
551
  (utxo) => CML.TransactionUnspentOutput.from_cbor_hex(utxo)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lucid-evolution/wallet",
3
- "version": "0.2.3",
3
+ "version": "0.4.0",
4
4
  "description": "",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -21,17 +21,13 @@
21
21
  "keywords": [],
22
22
  "author": "",
23
23
  "license": "MIT",
24
- "browser": {
25
- "@anastasia-labs/cardano-multiplatform-lib-nodejs": "@anastasia-labs/cardano-multiplatform-lib-browser"
26
- },
27
24
  "dependencies": {
28
- "@anastasia-labs/cardano-multiplatform-lib-browser": "6.2.0-1",
29
- "@anastasia-labs/cardano-multiplatform-lib-nodejs": "6.2.0-1",
30
25
  "bip39": "^3.1.0",
31
- "@lucid-evolution/core-types": "0.3.0",
32
- "@lucid-evolution/sign_data": "0.1.32",
33
- "@lucid-evolution/core-utils": "0.1.17",
34
- "@lucid-evolution/utils": "0.1.75"
26
+ "@lucid-evolution/cml": "0.1.0",
27
+ "@lucid-evolution/core-types": "0.5.0",
28
+ "@lucid-evolution/core-utils": "0.2.0",
29
+ "@lucid-evolution/sign_data": "0.2.0",
30
+ "@lucid-evolution/utils": "0.2.0"
35
31
  },
36
32
  "devDependencies": {
37
33
  "@types/node": "^20.12.8",