@lucid-evolution/wallet 0.2.0 → 0.2.2

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.
Files changed (3) hide show
  1. package/dist/index.cjs +281 -221
  2. package/dist/index.js +283 -223
  3. package/package.json +4 -4
package/dist/index.cjs CHANGED
@@ -63,237 +63,297 @@ function walletFromSeed(seed, options = {}) {
63
63
  (0, import_core_utils.fromHex)(entropy),
64
64
  options?.password ? new TextEncoder().encode(options.password) : new Uint8Array()
65
65
  );
66
- const accountKey = rootKey.derive(harden(1852)).derive(harden(1815)).derive(harden(accountIndex));
67
- rootKey.free();
68
- const paymentKey = accountKey.derive(0).derive(0).to_raw_key();
69
- const stakeKey = accountKey.derive(2).derive(0).to_raw_key();
70
- const paymentKeyHash = paymentKey.to_public().hash();
71
- const stakeKeyHash = stakeKey.to_public().hash();
72
- const networkId = network === "Mainnet" ? 1 : 0;
73
- const address = addressType === "Base" ? CML.BaseAddress.new(
74
- networkId,
75
- CML.Credential.new_pub_key(paymentKeyHash),
76
- CML.Credential.new_pub_key(stakeKeyHash)
77
- ).to_address().to_bech32(void 0) : CML.EnterpriseAddress.new(
78
- networkId,
79
- CML.Credential.new_pub_key(paymentKeyHash)
80
- ).to_address().to_bech32(void 0);
81
- const rewardAddress = addressType === "Base" ? CML.RewardAddress.new(
82
- networkId,
83
- CML.Credential.new_pub_key(stakeKeyHash)
84
- ).to_address().to_bech32(void 0) : null;
85
- return {
86
- address,
87
- rewardAddress,
88
- paymentKey: paymentKey.to_bech32(),
89
- stakeKey: addressType === "Base" ? stakeKey.to_bech32() : null
90
- };
66
+ return (0, import_core_utils.withCMLScope)((own) => {
67
+ own(rootKey);
68
+ const accountKey = own(
69
+ own(own(rootKey.derive(harden(1852))).derive(harden(1815))).derive(
70
+ harden(accountIndex)
71
+ )
72
+ );
73
+ const paymentKey = own(
74
+ own(own(accountKey.derive(0)).derive(0)).to_raw_key()
75
+ );
76
+ const stakeKey = own(own(own(accountKey.derive(2)).derive(0)).to_raw_key());
77
+ const paymentKeyHash = own(own(paymentKey.to_public()).hash());
78
+ const stakeKeyHash = own(own(stakeKey.to_public()).hash());
79
+ const networkId = network === "Mainnet" ? 1 : 0;
80
+ const address = addressType === "Base" ? own(
81
+ own(
82
+ CML.BaseAddress.new(
83
+ networkId,
84
+ own(CML.Credential.new_pub_key(paymentKeyHash)),
85
+ own(CML.Credential.new_pub_key(stakeKeyHash))
86
+ )
87
+ ).to_address()
88
+ ).to_bech32(void 0) : own(
89
+ own(
90
+ CML.EnterpriseAddress.new(
91
+ networkId,
92
+ own(CML.Credential.new_pub_key(paymentKeyHash))
93
+ )
94
+ ).to_address()
95
+ ).to_bech32(void 0);
96
+ const rewardAddress = addressType === "Base" ? own(
97
+ own(
98
+ CML.RewardAddress.new(
99
+ networkId,
100
+ own(CML.Credential.new_pub_key(stakeKeyHash))
101
+ )
102
+ ).to_address()
103
+ ).to_bech32(void 0) : null;
104
+ return {
105
+ address,
106
+ rewardAddress,
107
+ paymentKey: paymentKey.to_bech32(),
108
+ stakeKey: addressType === "Base" ? stakeKey.to_bech32() : null
109
+ };
110
+ });
91
111
  }
92
112
  function discoverOwnUsedTxKeyHashes(tx, ownKeyHashes, ownUtxos) {
93
- const usedKeyHashes = [];
94
- const inputs = tx.body().inputs();
95
- for (let i = 0; i < inputs.len(); i++) {
96
- const input = inputs.get(i);
97
- const txHash = input.transaction_id().to_hex();
98
- const outputIndex = Number(input.index());
99
- const utxo = ownUtxos.find(
100
- (utxo2) => utxo2.txHash === txHash && utxo2.outputIndex === outputIndex
101
- );
102
- if (utxo) {
103
- const { paymentCredential } = (0, import_utils.getAddressDetails)(utxo.address);
104
- usedKeyHashes.push(paymentCredential?.hash);
113
+ return (0, import_core_utils.withCMLScope)((own) => {
114
+ const usedKeyHashes = [];
115
+ const txBody = own(tx.body());
116
+ const inputs = own(txBody.inputs());
117
+ for (let i = 0; i < inputs.len(); i++) {
118
+ const input = own(inputs.get(i));
119
+ const txHash = own(input.transaction_id()).to_hex();
120
+ const outputIndex = Number(input.index());
121
+ const utxo = ownUtxos.find(
122
+ (utxo2) => utxo2.txHash === txHash && utxo2.outputIndex === outputIndex
123
+ );
124
+ if (utxo) {
125
+ const { paymentCredential } = (0, import_utils.getAddressDetails)(utxo.address);
126
+ usedKeyHashes.push(paymentCredential?.hash);
127
+ }
105
128
  }
106
- }
107
- const txBody = tx.body();
108
- function keyHashFromCert(txBody2) {
109
- const certs = txBody2.certs();
110
- if (!certs) return;
111
- for (let i = 0; i < certs.len(); i++) {
112
- const cert = certs.get(i);
113
- switch (cert.kind()) {
114
- case 0:
115
- break;
116
- case 1: {
117
- const credential = cert.as_stake_deregistration()?.stake_credential();
118
- switch (credential?.kind()) {
119
- case 0:
120
- usedKeyHashes.push(credential.as_pub_key()?.to_hex());
121
- break;
122
- case 1:
123
- usedKeyHashes.push(credential.as_script()?.to_hex());
124
- break;
129
+ function keyHashFromCert(txBody2) {
130
+ const certs = own(txBody2.certs());
131
+ if (!certs) return;
132
+ for (let i = 0; i < certs.len(); i++) {
133
+ const cert = own(certs.get(i));
134
+ switch (cert.kind()) {
135
+ case 0:
136
+ break;
137
+ case 1: {
138
+ const credential = own(
139
+ own(cert.as_stake_deregistration())?.stake_credential()
140
+ );
141
+ switch (credential?.kind()) {
142
+ case 0:
143
+ usedKeyHashes.push(own(credential.as_pub_key())?.to_hex());
144
+ break;
145
+ case 1:
146
+ usedKeyHashes.push(own(credential.as_script())?.to_hex());
147
+ break;
148
+ }
149
+ break;
125
150
  }
126
- break;
127
- }
128
- case 2: {
129
- const credential = cert.as_stake_delegation()?.stake_credential();
130
- if (credential?.kind() === 0) {
131
- const keyHash = credential.as_pub_key()?.to_hex();
132
- usedKeyHashes.push(keyHash);
151
+ case 2: {
152
+ const credential = own(
153
+ own(cert.as_stake_delegation())?.stake_credential()
154
+ );
155
+ if (credential?.kind() === 0) {
156
+ const keyHash = own(credential.as_pub_key())?.to_hex();
157
+ usedKeyHashes.push(keyHash);
158
+ }
159
+ break;
133
160
  }
134
- break;
135
- }
136
- case 3: {
137
- const poolParams = cert.as_pool_registration()?.pool_params();
138
- const owners = poolParams?.pool_owners();
139
- if (!owners) break;
140
- for (let i2 = 0; i2 < owners.len(); i2++) {
141
- const keyHash = owners.get(i2).to_hex();
142
- usedKeyHashes.push(keyHash);
161
+ case 3: {
162
+ const poolParams = own(
163
+ own(cert.as_pool_registration())?.pool_params()
164
+ );
165
+ const owners = own(poolParams?.pool_owners());
166
+ if (!owners) break;
167
+ for (let i2 = 0; i2 < owners.len(); i2++) {
168
+ const keyHash = own(owners.get(i2)).to_hex();
169
+ usedKeyHashes.push(keyHash);
170
+ }
171
+ const operator = own(poolParams.operator()).to_hex();
172
+ usedKeyHashes.push(operator);
173
+ break;
143
174
  }
144
- const operator = poolParams.operator().to_hex();
145
- usedKeyHashes.push(operator);
146
- break;
147
- }
148
- case 4: {
149
- const operator = cert.as_pool_retirement()?.pool().to_hex();
150
- usedKeyHashes.push(operator);
151
- break;
152
- }
153
- case 6: {
154
- const credential = cert.as_unreg_cert()?.stake_credential();
155
- if (credential) {
156
- usedKeyHashes.push(credential.as_pub_key()?.to_hex());
175
+ case 4: {
176
+ const operator = own(
177
+ own(cert.as_pool_retirement())?.pool()
178
+ )?.to_hex();
179
+ usedKeyHashes.push(operator);
180
+ break;
157
181
  }
158
- break;
159
- }
160
- case 7: {
161
- const credential = cert.as_vote_deleg_cert()?.stake_credential();
162
- if (credential) {
163
- usedKeyHashes.push(credential.as_pub_key()?.to_hex());
182
+ case 6: {
183
+ const credential = own(
184
+ own(cert.as_unreg_cert())?.stake_credential()
185
+ );
186
+ if (credential) {
187
+ usedKeyHashes.push(own(credential.as_pub_key())?.to_hex());
188
+ }
189
+ break;
164
190
  }
165
- break;
166
- }
167
- case 8: {
168
- const credential = cert.as_stake_vote_deleg_cert()?.stake_credential();
169
- if (credential) {
170
- usedKeyHashes.push(credential.as_pub_key()?.to_hex());
191
+ case 7: {
192
+ const credential = own(
193
+ own(cert.as_vote_deleg_cert())?.stake_credential()
194
+ );
195
+ if (credential) {
196
+ usedKeyHashes.push(own(credential.as_pub_key())?.to_hex());
197
+ }
198
+ break;
171
199
  }
172
- break;
173
- }
174
- case 9: {
175
- const credential = cert.as_stake_reg_deleg_cert()?.stake_credential();
176
- if (credential) {
177
- usedKeyHashes.push(credential.as_pub_key()?.to_hex());
200
+ case 8: {
201
+ const credential = own(
202
+ own(cert.as_stake_vote_deleg_cert())?.stake_credential()
203
+ );
204
+ if (credential) {
205
+ usedKeyHashes.push(own(credential.as_pub_key())?.to_hex());
206
+ }
207
+ break;
178
208
  }
179
- break;
180
- }
181
- case 10: {
182
- const credential = cert.as_vote_reg_deleg_cert()?.stake_credential();
183
- if (credential) {
184
- usedKeyHashes.push(credential.as_pub_key()?.to_hex());
209
+ case 9: {
210
+ const credential = own(
211
+ own(cert.as_stake_reg_deleg_cert())?.stake_credential()
212
+ );
213
+ if (credential) {
214
+ usedKeyHashes.push(own(credential.as_pub_key())?.to_hex());
215
+ }
216
+ break;
185
217
  }
186
- break;
187
- }
188
- case 11: {
189
- const credential = cert.as_stake_vote_reg_deleg_cert()?.stake_credential();
190
- if (credential) {
191
- usedKeyHashes.push(credential.as_pub_key()?.to_hex());
218
+ case 10: {
219
+ const credential = own(
220
+ own(cert.as_vote_reg_deleg_cert())?.stake_credential()
221
+ );
222
+ if (credential) {
223
+ usedKeyHashes.push(own(credential.as_pub_key())?.to_hex());
224
+ }
225
+ break;
192
226
  }
193
- break;
194
- }
195
- case 12: {
196
- const credential = cert.as_auth_committee_hot_cert()?.committee_cold_credential();
197
- if (credential) {
198
- usedKeyHashes.push(credential.as_pub_key()?.to_hex());
227
+ case 11: {
228
+ const credential = own(
229
+ own(cert.as_stake_vote_reg_deleg_cert())?.stake_credential()
230
+ );
231
+ if (credential) {
232
+ usedKeyHashes.push(own(credential.as_pub_key())?.to_hex());
233
+ }
234
+ break;
199
235
  }
200
- break;
201
- }
202
- case 13: {
203
- const credential = cert.as_resign_committee_cold_cert()?.committee_cold_credential();
204
- if (credential) {
205
- usedKeyHashes.push(credential.as_pub_key()?.to_hex());
236
+ case 12: {
237
+ const credential = own(
238
+ own(
239
+ cert.as_auth_committee_hot_cert()
240
+ )?.committee_cold_credential()
241
+ );
242
+ if (credential) {
243
+ usedKeyHashes.push(own(credential.as_pub_key())?.to_hex());
244
+ }
245
+ break;
206
246
  }
207
- break;
208
- }
209
- case 14: {
210
- const credential = cert.as_reg_drep_cert()?.drep_credential();
211
- if (credential) {
212
- usedKeyHashes.push(credential.as_pub_key()?.to_hex());
247
+ case 13: {
248
+ const credential = own(
249
+ own(
250
+ cert.as_resign_committee_cold_cert()
251
+ )?.committee_cold_credential()
252
+ );
253
+ if (credential) {
254
+ usedKeyHashes.push(own(credential.as_pub_key())?.to_hex());
255
+ }
256
+ break;
213
257
  }
214
- break;
215
- }
216
- case 15: {
217
- const credential = cert.as_unreg_drep_cert()?.drep_credential();
218
- if (credential) {
219
- usedKeyHashes.push(credential.as_pub_key()?.to_hex());
258
+ case 14: {
259
+ const credential = own(
260
+ own(cert.as_reg_drep_cert())?.drep_credential()
261
+ );
262
+ if (credential) {
263
+ usedKeyHashes.push(own(credential.as_pub_key())?.to_hex());
264
+ }
265
+ break;
220
266
  }
221
- break;
222
- }
223
- case 16: {
224
- const credential = cert.as_update_drep_cert()?.drep_credential();
225
- if (credential) {
226
- usedKeyHashes.push(credential.as_pub_key()?.to_hex());
267
+ case 15: {
268
+ const credential = own(
269
+ own(cert.as_unreg_drep_cert())?.drep_credential()
270
+ );
271
+ if (credential) {
272
+ usedKeyHashes.push(own(credential.as_pub_key())?.to_hex());
273
+ }
274
+ break;
275
+ }
276
+ case 16: {
277
+ const credential = own(
278
+ own(cert.as_update_drep_cert())?.drep_credential()
279
+ );
280
+ if (credential) {
281
+ usedKeyHashes.push(own(credential.as_pub_key())?.to_hex());
282
+ }
283
+ break;
227
284
  }
228
- break;
285
+ default:
286
+ break;
229
287
  }
230
- default:
231
- break;
232
288
  }
233
289
  }
234
- }
235
- if (txBody.certs()) keyHashFromCert(txBody);
236
- const withdrawals = txBody.withdrawals();
237
- function keyHashFromWithdrawal(withdrawals2) {
238
- const rewardAddresses = withdrawals2.keys();
239
- for (let i = 0; i < rewardAddresses.len(); i++) {
240
- const credential = rewardAddresses.get(i).payment();
241
- switch (credential.kind()) {
242
- case 0:
243
- usedKeyHashes.push(credential.as_pub_key()?.to_hex());
244
- break;
245
- case 1:
246
- usedKeyHashes.push(credential.as_script()?.to_hex());
247
- break;
290
+ keyHashFromCert(txBody);
291
+ const withdrawals = own(txBody.withdrawals());
292
+ function keyHashFromWithdrawal(withdrawals2) {
293
+ const rewardAddresses = own(withdrawals2.keys());
294
+ for (let i = 0; i < rewardAddresses.len(); i++) {
295
+ const credential = own(own(rewardAddresses.get(i)).payment());
296
+ switch (credential.kind()) {
297
+ case 0:
298
+ usedKeyHashes.push(own(credential.as_pub_key())?.to_hex());
299
+ break;
300
+ case 1:
301
+ usedKeyHashes.push(own(credential.as_script())?.to_hex());
302
+ break;
303
+ }
248
304
  }
249
305
  }
250
- }
251
- if (withdrawals) keyHashFromWithdrawal(withdrawals);
252
- const scripts = tx.witness_set().native_scripts();
253
- function keyHashFromScript(scripts2) {
254
- for (let i = 0; i < scripts2.len(); i++) {
255
- const script = scripts2.get(i);
256
- if (script.kind() === 0) {
257
- const keyHash = script.as_script_pubkey()?.ed25519_key_hash().to_hex();
258
- usedKeyHashes.push(keyHash);
259
- }
260
- if (script.kind() === 1) {
261
- keyHashFromScript(script.as_script_all().native_scripts());
262
- return;
263
- }
264
- if (script.kind() === 2) {
265
- keyHashFromScript(script.as_script_any().native_scripts());
266
- return;
267
- }
268
- if (script.kind() === 3) {
269
- keyHashFromScript(script.as_script_n_of_k().native_scripts());
270
- return;
306
+ if (withdrawals) keyHashFromWithdrawal(withdrawals);
307
+ const scripts = own(own(tx.witness_set()).native_scripts());
308
+ function keyHashFromScript(scripts2) {
309
+ for (let i = 0; i < scripts2.len(); i++) {
310
+ const script = own(scripts2.get(i));
311
+ if (script.kind() === 0) {
312
+ const keyHash = own(
313
+ own(script.as_script_pubkey())?.ed25519_key_hash()
314
+ )?.to_hex();
315
+ usedKeyHashes.push(keyHash);
316
+ }
317
+ if (script.kind() === 1) {
318
+ keyHashFromScript(own(own(script.as_script_all()).native_scripts()));
319
+ return;
320
+ }
321
+ if (script.kind() === 2) {
322
+ keyHashFromScript(own(own(script.as_script_any()).native_scripts()));
323
+ return;
324
+ }
325
+ if (script.kind() === 3) {
326
+ keyHashFromScript(
327
+ own(own(script.as_script_n_of_k()).native_scripts())
328
+ );
329
+ return;
330
+ }
271
331
  }
272
332
  }
273
- }
274
- if (scripts) keyHashFromScript(scripts);
275
- const requiredSigners = txBody.required_signers();
276
- if (requiredSigners) {
277
- for (let i = 0; i < requiredSigners.len(); i++) {
278
- usedKeyHashes.push(requiredSigners.get(i).to_hex());
333
+ if (scripts) keyHashFromScript(scripts);
334
+ const requiredSigners = own(txBody.required_signers());
335
+ if (requiredSigners) {
336
+ for (let i = 0; i < requiredSigners.len(); i++) {
337
+ usedKeyHashes.push(own(requiredSigners.get(i)).to_hex());
338
+ }
279
339
  }
280
- }
281
- const collateral = txBody.collateral_inputs();
282
- if (collateral) {
283
- for (let i = 0; i < collateral.len(); i++) {
284
- const input = collateral.get(i);
285
- const txHash = input.transaction_id().to_hex();
286
- const outputIndex = Number(input.index());
287
- const utxo = ownUtxos.find(
288
- (utxo2) => utxo2.txHash === txHash && utxo2.outputIndex === outputIndex
289
- );
290
- if (utxo) {
291
- const { paymentCredential } = (0, import_utils.getAddressDetails)(utxo.address);
292
- usedKeyHashes.push(paymentCredential?.hash);
340
+ const collateral = own(txBody.collateral_inputs());
341
+ if (collateral) {
342
+ for (let i = 0; i < collateral.len(); i++) {
343
+ const input = own(collateral.get(i));
344
+ const txHash = own(input.transaction_id()).to_hex();
345
+ const outputIndex = Number(input.index());
346
+ const utxo = ownUtxos.find(
347
+ (utxo2) => utxo2.txHash === txHash && utxo2.outputIndex === outputIndex
348
+ );
349
+ if (utxo) {
350
+ const { paymentCredential } = (0, import_utils.getAddressDetails)(utxo.address);
351
+ usedKeyHashes.push(paymentCredential?.hash);
352
+ }
293
353
  }
294
354
  }
295
- }
296
- return usedKeyHashes.filter((k) => ownKeyHashes.includes(k));
355
+ return usedKeyHashes.filter((k) => ownKeyHashes.includes(k));
356
+ });
297
357
  }
298
358
 
299
359
  // src/wallet_selection.ts
@@ -362,16 +422,15 @@ var makeWalletFromSeed = (provider, network, seed, options) => {
362
422
  const utxos = config.overriddenUTxOs !== void 0 ? config.overriddenUTxOs : await provider.getUtxos(address);
363
423
  const ownKeyHashes = [paymentKeyHash, stakeKeyHash];
364
424
  const usedKeyHashes = discoverOwnUsedTxKeyHashes(tx, ownKeyHashes, utxos);
365
- const txWitnessSetBuilder = CML.TransactionWitnessSetBuilder.new();
366
- for (const keyHash of usedKeyHashes) {
367
- const priv = CML.PrivateKey.from_bech32(privKeyHashMap[keyHash]);
368
- const witness = CML.make_vkey_witness(
369
- CML.hash_transaction(tx.body()),
370
- priv
371
- );
372
- txWitnessSetBuilder.add_vkey(witness);
373
- }
374
- return txWitnessSetBuilder.build();
425
+ return (0, import_core_utils2.withCMLScope)((own) => {
426
+ const txWitnessSetBuilder = own(CML.TransactionWitnessSetBuilder.new());
427
+ const txHash = own(CML.hash_transaction(own(tx.body())));
428
+ for (const keyHash of usedKeyHashes) {
429
+ const priv = own(CML.PrivateKey.from_bech32(privKeyHashMap[keyHash]));
430
+ txWitnessSetBuilder.add_vkey(own(CML.make_vkey_witness(txHash, priv)));
431
+ }
432
+ return txWitnessSetBuilder.build();
433
+ });
375
434
  };
376
435
  return {
377
436
  overrideUTxOs: (utxos) => config.overriddenUTxOs = utxos,
@@ -420,13 +479,14 @@ var makeWalletFromPrivateKey = (provider, network, privateKey) => {
420
479
  overriddenUTxOs: void 0
421
480
  };
422
481
  const signTx = async (tx) => {
423
- const witness = CML.make_vkey_witness(
424
- CML.hash_transaction(tx.body()),
425
- priv
426
- );
427
- const txWitnessSetBuilder = CML.TransactionWitnessSetBuilder.new();
428
- txWitnessSetBuilder.add_vkey(witness);
429
- return txWitnessSetBuilder.build();
482
+ return (0, import_core_utils2.withCMLScope)((own) => {
483
+ const witness = own(
484
+ CML.make_vkey_witness(own(CML.hash_transaction(own(tx.body()))), priv)
485
+ );
486
+ const txWitnessSetBuilder = own(CML.TransactionWitnessSetBuilder.new());
487
+ txWitnessSetBuilder.add_vkey(witness);
488
+ return txWitnessSetBuilder.build();
489
+ });
430
490
  };
431
491
  return {
432
492
  overrideUTxOs: (utxos) => config.overriddenUTxOs = utxos,
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  // src/wallet.ts
2
2
  import { getAddressDetails } from "@lucid-evolution/utils";
3
- import { fromHex } from "@lucid-evolution/core-utils";
3
+ import { fromHex, withCMLScope } from "@lucid-evolution/core-utils";
4
4
 
5
5
  // src/core.ts
6
6
  import * as CML from "@anastasia-labs/cardano-multiplatform-lib-nodejs";
@@ -22,241 +22,301 @@ function walletFromSeed(seed, options = {}) {
22
22
  fromHex(entropy),
23
23
  options?.password ? new TextEncoder().encode(options.password) : new Uint8Array()
24
24
  );
25
- const accountKey = rootKey.derive(harden(1852)).derive(harden(1815)).derive(harden(accountIndex));
26
- rootKey.free();
27
- const paymentKey = accountKey.derive(0).derive(0).to_raw_key();
28
- const stakeKey = accountKey.derive(2).derive(0).to_raw_key();
29
- const paymentKeyHash = paymentKey.to_public().hash();
30
- const stakeKeyHash = stakeKey.to_public().hash();
31
- const networkId = network === "Mainnet" ? 1 : 0;
32
- const address = addressType === "Base" ? CML.BaseAddress.new(
33
- networkId,
34
- CML.Credential.new_pub_key(paymentKeyHash),
35
- CML.Credential.new_pub_key(stakeKeyHash)
36
- ).to_address().to_bech32(void 0) : CML.EnterpriseAddress.new(
37
- networkId,
38
- CML.Credential.new_pub_key(paymentKeyHash)
39
- ).to_address().to_bech32(void 0);
40
- const rewardAddress = addressType === "Base" ? CML.RewardAddress.new(
41
- networkId,
42
- CML.Credential.new_pub_key(stakeKeyHash)
43
- ).to_address().to_bech32(void 0) : null;
44
- return {
45
- address,
46
- rewardAddress,
47
- paymentKey: paymentKey.to_bech32(),
48
- stakeKey: addressType === "Base" ? stakeKey.to_bech32() : null
49
- };
25
+ return withCMLScope((own) => {
26
+ own(rootKey);
27
+ const accountKey = own(
28
+ own(own(rootKey.derive(harden(1852))).derive(harden(1815))).derive(
29
+ harden(accountIndex)
30
+ )
31
+ );
32
+ const paymentKey = own(
33
+ own(own(accountKey.derive(0)).derive(0)).to_raw_key()
34
+ );
35
+ const stakeKey = own(own(own(accountKey.derive(2)).derive(0)).to_raw_key());
36
+ const paymentKeyHash = own(own(paymentKey.to_public()).hash());
37
+ const stakeKeyHash = own(own(stakeKey.to_public()).hash());
38
+ const networkId = network === "Mainnet" ? 1 : 0;
39
+ const address = addressType === "Base" ? own(
40
+ own(
41
+ CML.BaseAddress.new(
42
+ networkId,
43
+ own(CML.Credential.new_pub_key(paymentKeyHash)),
44
+ own(CML.Credential.new_pub_key(stakeKeyHash))
45
+ )
46
+ ).to_address()
47
+ ).to_bech32(void 0) : own(
48
+ own(
49
+ CML.EnterpriseAddress.new(
50
+ networkId,
51
+ own(CML.Credential.new_pub_key(paymentKeyHash))
52
+ )
53
+ ).to_address()
54
+ ).to_bech32(void 0);
55
+ const rewardAddress = addressType === "Base" ? own(
56
+ own(
57
+ CML.RewardAddress.new(
58
+ networkId,
59
+ own(CML.Credential.new_pub_key(stakeKeyHash))
60
+ )
61
+ ).to_address()
62
+ ).to_bech32(void 0) : null;
63
+ return {
64
+ address,
65
+ rewardAddress,
66
+ paymentKey: paymentKey.to_bech32(),
67
+ stakeKey: addressType === "Base" ? stakeKey.to_bech32() : null
68
+ };
69
+ });
50
70
  }
51
71
  function discoverOwnUsedTxKeyHashes(tx, ownKeyHashes, ownUtxos) {
52
- const usedKeyHashes = [];
53
- const inputs = tx.body().inputs();
54
- for (let i = 0; i < inputs.len(); i++) {
55
- const input = inputs.get(i);
56
- const txHash = input.transaction_id().to_hex();
57
- const outputIndex = Number(input.index());
58
- const utxo = ownUtxos.find(
59
- (utxo2) => utxo2.txHash === txHash && utxo2.outputIndex === outputIndex
60
- );
61
- if (utxo) {
62
- const { paymentCredential } = getAddressDetails(utxo.address);
63
- usedKeyHashes.push(paymentCredential?.hash);
72
+ return withCMLScope((own) => {
73
+ const usedKeyHashes = [];
74
+ const txBody = own(tx.body());
75
+ const inputs = own(txBody.inputs());
76
+ for (let i = 0; i < inputs.len(); i++) {
77
+ const input = own(inputs.get(i));
78
+ const txHash = own(input.transaction_id()).to_hex();
79
+ const outputIndex = Number(input.index());
80
+ const utxo = ownUtxos.find(
81
+ (utxo2) => utxo2.txHash === txHash && utxo2.outputIndex === outputIndex
82
+ );
83
+ if (utxo) {
84
+ const { paymentCredential } = getAddressDetails(utxo.address);
85
+ usedKeyHashes.push(paymentCredential?.hash);
86
+ }
64
87
  }
65
- }
66
- const txBody = tx.body();
67
- function keyHashFromCert(txBody2) {
68
- const certs = txBody2.certs();
69
- if (!certs) return;
70
- for (let i = 0; i < certs.len(); i++) {
71
- const cert = certs.get(i);
72
- switch (cert.kind()) {
73
- case 0:
74
- break;
75
- case 1: {
76
- const credential = cert.as_stake_deregistration()?.stake_credential();
77
- switch (credential?.kind()) {
78
- case 0:
79
- usedKeyHashes.push(credential.as_pub_key()?.to_hex());
80
- break;
81
- case 1:
82
- usedKeyHashes.push(credential.as_script()?.to_hex());
83
- break;
88
+ function keyHashFromCert(txBody2) {
89
+ const certs = own(txBody2.certs());
90
+ if (!certs) return;
91
+ for (let i = 0; i < certs.len(); i++) {
92
+ const cert = own(certs.get(i));
93
+ switch (cert.kind()) {
94
+ case 0:
95
+ break;
96
+ case 1: {
97
+ const credential = own(
98
+ own(cert.as_stake_deregistration())?.stake_credential()
99
+ );
100
+ switch (credential?.kind()) {
101
+ case 0:
102
+ usedKeyHashes.push(own(credential.as_pub_key())?.to_hex());
103
+ break;
104
+ case 1:
105
+ usedKeyHashes.push(own(credential.as_script())?.to_hex());
106
+ break;
107
+ }
108
+ break;
84
109
  }
85
- break;
86
- }
87
- case 2: {
88
- const credential = cert.as_stake_delegation()?.stake_credential();
89
- if (credential?.kind() === 0) {
90
- const keyHash = credential.as_pub_key()?.to_hex();
91
- usedKeyHashes.push(keyHash);
110
+ case 2: {
111
+ const credential = own(
112
+ own(cert.as_stake_delegation())?.stake_credential()
113
+ );
114
+ if (credential?.kind() === 0) {
115
+ const keyHash = own(credential.as_pub_key())?.to_hex();
116
+ usedKeyHashes.push(keyHash);
117
+ }
118
+ break;
92
119
  }
93
- break;
94
- }
95
- case 3: {
96
- const poolParams = cert.as_pool_registration()?.pool_params();
97
- const owners = poolParams?.pool_owners();
98
- if (!owners) break;
99
- for (let i2 = 0; i2 < owners.len(); i2++) {
100
- const keyHash = owners.get(i2).to_hex();
101
- usedKeyHashes.push(keyHash);
120
+ case 3: {
121
+ const poolParams = own(
122
+ own(cert.as_pool_registration())?.pool_params()
123
+ );
124
+ const owners = own(poolParams?.pool_owners());
125
+ if (!owners) break;
126
+ for (let i2 = 0; i2 < owners.len(); i2++) {
127
+ const keyHash = own(owners.get(i2)).to_hex();
128
+ usedKeyHashes.push(keyHash);
129
+ }
130
+ const operator = own(poolParams.operator()).to_hex();
131
+ usedKeyHashes.push(operator);
132
+ break;
102
133
  }
103
- const operator = poolParams.operator().to_hex();
104
- usedKeyHashes.push(operator);
105
- break;
106
- }
107
- case 4: {
108
- const operator = cert.as_pool_retirement()?.pool().to_hex();
109
- usedKeyHashes.push(operator);
110
- break;
111
- }
112
- case 6: {
113
- const credential = cert.as_unreg_cert()?.stake_credential();
114
- if (credential) {
115
- usedKeyHashes.push(credential.as_pub_key()?.to_hex());
134
+ case 4: {
135
+ const operator = own(
136
+ own(cert.as_pool_retirement())?.pool()
137
+ )?.to_hex();
138
+ usedKeyHashes.push(operator);
139
+ break;
116
140
  }
117
- break;
118
- }
119
- case 7: {
120
- const credential = cert.as_vote_deleg_cert()?.stake_credential();
121
- if (credential) {
122
- usedKeyHashes.push(credential.as_pub_key()?.to_hex());
141
+ case 6: {
142
+ const credential = own(
143
+ own(cert.as_unreg_cert())?.stake_credential()
144
+ );
145
+ if (credential) {
146
+ usedKeyHashes.push(own(credential.as_pub_key())?.to_hex());
147
+ }
148
+ break;
123
149
  }
124
- break;
125
- }
126
- case 8: {
127
- const credential = cert.as_stake_vote_deleg_cert()?.stake_credential();
128
- if (credential) {
129
- usedKeyHashes.push(credential.as_pub_key()?.to_hex());
150
+ case 7: {
151
+ const credential = own(
152
+ own(cert.as_vote_deleg_cert())?.stake_credential()
153
+ );
154
+ if (credential) {
155
+ usedKeyHashes.push(own(credential.as_pub_key())?.to_hex());
156
+ }
157
+ break;
130
158
  }
131
- break;
132
- }
133
- case 9: {
134
- const credential = cert.as_stake_reg_deleg_cert()?.stake_credential();
135
- if (credential) {
136
- usedKeyHashes.push(credential.as_pub_key()?.to_hex());
159
+ case 8: {
160
+ const credential = own(
161
+ own(cert.as_stake_vote_deleg_cert())?.stake_credential()
162
+ );
163
+ if (credential) {
164
+ usedKeyHashes.push(own(credential.as_pub_key())?.to_hex());
165
+ }
166
+ break;
137
167
  }
138
- break;
139
- }
140
- case 10: {
141
- const credential = cert.as_vote_reg_deleg_cert()?.stake_credential();
142
- if (credential) {
143
- usedKeyHashes.push(credential.as_pub_key()?.to_hex());
168
+ case 9: {
169
+ const credential = own(
170
+ own(cert.as_stake_reg_deleg_cert())?.stake_credential()
171
+ );
172
+ if (credential) {
173
+ usedKeyHashes.push(own(credential.as_pub_key())?.to_hex());
174
+ }
175
+ break;
144
176
  }
145
- break;
146
- }
147
- case 11: {
148
- const credential = cert.as_stake_vote_reg_deleg_cert()?.stake_credential();
149
- if (credential) {
150
- usedKeyHashes.push(credential.as_pub_key()?.to_hex());
177
+ case 10: {
178
+ const credential = own(
179
+ own(cert.as_vote_reg_deleg_cert())?.stake_credential()
180
+ );
181
+ if (credential) {
182
+ usedKeyHashes.push(own(credential.as_pub_key())?.to_hex());
183
+ }
184
+ break;
151
185
  }
152
- break;
153
- }
154
- case 12: {
155
- const credential = cert.as_auth_committee_hot_cert()?.committee_cold_credential();
156
- if (credential) {
157
- usedKeyHashes.push(credential.as_pub_key()?.to_hex());
186
+ case 11: {
187
+ const credential = own(
188
+ own(cert.as_stake_vote_reg_deleg_cert())?.stake_credential()
189
+ );
190
+ if (credential) {
191
+ usedKeyHashes.push(own(credential.as_pub_key())?.to_hex());
192
+ }
193
+ break;
158
194
  }
159
- break;
160
- }
161
- case 13: {
162
- const credential = cert.as_resign_committee_cold_cert()?.committee_cold_credential();
163
- if (credential) {
164
- usedKeyHashes.push(credential.as_pub_key()?.to_hex());
195
+ case 12: {
196
+ const credential = own(
197
+ own(
198
+ cert.as_auth_committee_hot_cert()
199
+ )?.committee_cold_credential()
200
+ );
201
+ if (credential) {
202
+ usedKeyHashes.push(own(credential.as_pub_key())?.to_hex());
203
+ }
204
+ break;
165
205
  }
166
- break;
167
- }
168
- case 14: {
169
- const credential = cert.as_reg_drep_cert()?.drep_credential();
170
- if (credential) {
171
- usedKeyHashes.push(credential.as_pub_key()?.to_hex());
206
+ case 13: {
207
+ const credential = own(
208
+ own(
209
+ cert.as_resign_committee_cold_cert()
210
+ )?.committee_cold_credential()
211
+ );
212
+ if (credential) {
213
+ usedKeyHashes.push(own(credential.as_pub_key())?.to_hex());
214
+ }
215
+ break;
172
216
  }
173
- break;
174
- }
175
- case 15: {
176
- const credential = cert.as_unreg_drep_cert()?.drep_credential();
177
- if (credential) {
178
- usedKeyHashes.push(credential.as_pub_key()?.to_hex());
217
+ case 14: {
218
+ const credential = own(
219
+ own(cert.as_reg_drep_cert())?.drep_credential()
220
+ );
221
+ if (credential) {
222
+ usedKeyHashes.push(own(credential.as_pub_key())?.to_hex());
223
+ }
224
+ break;
179
225
  }
180
- break;
181
- }
182
- case 16: {
183
- const credential = cert.as_update_drep_cert()?.drep_credential();
184
- if (credential) {
185
- usedKeyHashes.push(credential.as_pub_key()?.to_hex());
226
+ case 15: {
227
+ const credential = own(
228
+ own(cert.as_unreg_drep_cert())?.drep_credential()
229
+ );
230
+ if (credential) {
231
+ usedKeyHashes.push(own(credential.as_pub_key())?.to_hex());
232
+ }
233
+ break;
234
+ }
235
+ case 16: {
236
+ const credential = own(
237
+ own(cert.as_update_drep_cert())?.drep_credential()
238
+ );
239
+ if (credential) {
240
+ usedKeyHashes.push(own(credential.as_pub_key())?.to_hex());
241
+ }
242
+ break;
186
243
  }
187
- break;
244
+ default:
245
+ break;
188
246
  }
189
- default:
190
- break;
191
247
  }
192
248
  }
193
- }
194
- if (txBody.certs()) keyHashFromCert(txBody);
195
- const withdrawals = txBody.withdrawals();
196
- function keyHashFromWithdrawal(withdrawals2) {
197
- const rewardAddresses = withdrawals2.keys();
198
- for (let i = 0; i < rewardAddresses.len(); i++) {
199
- const credential = rewardAddresses.get(i).payment();
200
- switch (credential.kind()) {
201
- case 0:
202
- usedKeyHashes.push(credential.as_pub_key()?.to_hex());
203
- break;
204
- case 1:
205
- usedKeyHashes.push(credential.as_script()?.to_hex());
206
- break;
249
+ keyHashFromCert(txBody);
250
+ const withdrawals = own(txBody.withdrawals());
251
+ function keyHashFromWithdrawal(withdrawals2) {
252
+ const rewardAddresses = own(withdrawals2.keys());
253
+ for (let i = 0; i < rewardAddresses.len(); i++) {
254
+ const credential = own(own(rewardAddresses.get(i)).payment());
255
+ switch (credential.kind()) {
256
+ case 0:
257
+ usedKeyHashes.push(own(credential.as_pub_key())?.to_hex());
258
+ break;
259
+ case 1:
260
+ usedKeyHashes.push(own(credential.as_script())?.to_hex());
261
+ break;
262
+ }
207
263
  }
208
264
  }
209
- }
210
- if (withdrawals) keyHashFromWithdrawal(withdrawals);
211
- const scripts = tx.witness_set().native_scripts();
212
- function keyHashFromScript(scripts2) {
213
- for (let i = 0; i < scripts2.len(); i++) {
214
- const script = scripts2.get(i);
215
- if (script.kind() === 0) {
216
- const keyHash = script.as_script_pubkey()?.ed25519_key_hash().to_hex();
217
- usedKeyHashes.push(keyHash);
218
- }
219
- if (script.kind() === 1) {
220
- keyHashFromScript(script.as_script_all().native_scripts());
221
- return;
222
- }
223
- if (script.kind() === 2) {
224
- keyHashFromScript(script.as_script_any().native_scripts());
225
- return;
226
- }
227
- if (script.kind() === 3) {
228
- keyHashFromScript(script.as_script_n_of_k().native_scripts());
229
- return;
265
+ if (withdrawals) keyHashFromWithdrawal(withdrawals);
266
+ const scripts = own(own(tx.witness_set()).native_scripts());
267
+ function keyHashFromScript(scripts2) {
268
+ for (let i = 0; i < scripts2.len(); i++) {
269
+ const script = own(scripts2.get(i));
270
+ if (script.kind() === 0) {
271
+ const keyHash = own(
272
+ own(script.as_script_pubkey())?.ed25519_key_hash()
273
+ )?.to_hex();
274
+ usedKeyHashes.push(keyHash);
275
+ }
276
+ if (script.kind() === 1) {
277
+ keyHashFromScript(own(own(script.as_script_all()).native_scripts()));
278
+ return;
279
+ }
280
+ if (script.kind() === 2) {
281
+ keyHashFromScript(own(own(script.as_script_any()).native_scripts()));
282
+ return;
283
+ }
284
+ if (script.kind() === 3) {
285
+ keyHashFromScript(
286
+ own(own(script.as_script_n_of_k()).native_scripts())
287
+ );
288
+ return;
289
+ }
230
290
  }
231
291
  }
232
- }
233
- if (scripts) keyHashFromScript(scripts);
234
- const requiredSigners = txBody.required_signers();
235
- if (requiredSigners) {
236
- for (let i = 0; i < requiredSigners.len(); i++) {
237
- usedKeyHashes.push(requiredSigners.get(i).to_hex());
292
+ if (scripts) keyHashFromScript(scripts);
293
+ const requiredSigners = own(txBody.required_signers());
294
+ if (requiredSigners) {
295
+ for (let i = 0; i < requiredSigners.len(); i++) {
296
+ usedKeyHashes.push(own(requiredSigners.get(i)).to_hex());
297
+ }
238
298
  }
239
- }
240
- const collateral = txBody.collateral_inputs();
241
- if (collateral) {
242
- for (let i = 0; i < collateral.len(); i++) {
243
- const input = collateral.get(i);
244
- const txHash = input.transaction_id().to_hex();
245
- const outputIndex = Number(input.index());
246
- const utxo = ownUtxos.find(
247
- (utxo2) => utxo2.txHash === txHash && utxo2.outputIndex === outputIndex
248
- );
249
- if (utxo) {
250
- const { paymentCredential } = getAddressDetails(utxo.address);
251
- usedKeyHashes.push(paymentCredential?.hash);
299
+ const collateral = own(txBody.collateral_inputs());
300
+ if (collateral) {
301
+ for (let i = 0; i < collateral.len(); i++) {
302
+ const input = own(collateral.get(i));
303
+ const txHash = own(input.transaction_id()).to_hex();
304
+ const outputIndex = Number(input.index());
305
+ const utxo = ownUtxos.find(
306
+ (utxo2) => utxo2.txHash === txHash && utxo2.outputIndex === outputIndex
307
+ );
308
+ if (utxo) {
309
+ const { paymentCredential } = getAddressDetails(utxo.address);
310
+ usedKeyHashes.push(paymentCredential?.hash);
311
+ }
252
312
  }
253
313
  }
254
- }
255
- return usedKeyHashes.filter((k) => ownKeyHashes.includes(k));
314
+ return usedKeyHashes.filter((k) => ownKeyHashes.includes(k));
315
+ });
256
316
  }
257
317
 
258
318
  // src/wallet_selection.ts
259
- import { fromHex as fromHex2, toHex } from "@lucid-evolution/core-utils";
319
+ import { fromHex as fromHex2, toHex, withCMLScope as withCMLScope2 } from "@lucid-evolution/core-utils";
260
320
  import {
261
321
  coreToUtxo,
262
322
  credentialToRewardAddress,
@@ -326,16 +386,15 @@ var makeWalletFromSeed = (provider, network, seed, options) => {
326
386
  const utxos = config.overriddenUTxOs !== void 0 ? config.overriddenUTxOs : await provider.getUtxos(address);
327
387
  const ownKeyHashes = [paymentKeyHash, stakeKeyHash];
328
388
  const usedKeyHashes = discoverOwnUsedTxKeyHashes(tx, ownKeyHashes, utxos);
329
- const txWitnessSetBuilder = CML.TransactionWitnessSetBuilder.new();
330
- for (const keyHash of usedKeyHashes) {
331
- const priv = CML.PrivateKey.from_bech32(privKeyHashMap[keyHash]);
332
- const witness = CML.make_vkey_witness(
333
- CML.hash_transaction(tx.body()),
334
- priv
335
- );
336
- txWitnessSetBuilder.add_vkey(witness);
337
- }
338
- return txWitnessSetBuilder.build();
389
+ return withCMLScope2((own) => {
390
+ const txWitnessSetBuilder = own(CML.TransactionWitnessSetBuilder.new());
391
+ const txHash = own(CML.hash_transaction(own(tx.body())));
392
+ for (const keyHash of usedKeyHashes) {
393
+ const priv = own(CML.PrivateKey.from_bech32(privKeyHashMap[keyHash]));
394
+ txWitnessSetBuilder.add_vkey(own(CML.make_vkey_witness(txHash, priv)));
395
+ }
396
+ return txWitnessSetBuilder.build();
397
+ });
339
398
  };
340
399
  return {
341
400
  overrideUTxOs: (utxos) => config.overriddenUTxOs = utxos,
@@ -384,13 +443,14 @@ var makeWalletFromPrivateKey = (provider, network, privateKey) => {
384
443
  overriddenUTxOs: void 0
385
444
  };
386
445
  const signTx = async (tx) => {
387
- const witness = CML.make_vkey_witness(
388
- CML.hash_transaction(tx.body()),
389
- priv
390
- );
391
- const txWitnessSetBuilder = CML.TransactionWitnessSetBuilder.new();
392
- txWitnessSetBuilder.add_vkey(witness);
393
- return txWitnessSetBuilder.build();
446
+ return withCMLScope2((own) => {
447
+ const witness = own(
448
+ CML.make_vkey_witness(own(CML.hash_transaction(own(tx.body()))), priv)
449
+ );
450
+ const txWitnessSetBuilder = own(CML.TransactionWitnessSetBuilder.new());
451
+ txWitnessSetBuilder.add_vkey(witness);
452
+ return txWitnessSetBuilder.build();
453
+ });
394
454
  };
395
455
  return {
396
456
  overrideUTxOs: (utxos) => config.overriddenUTxOs = utxos,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lucid-evolution/wallet",
3
- "version": "0.2.0",
3
+ "version": "0.2.2",
4
4
  "description": "",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -29,9 +29,9 @@
29
29
  "@anastasia-labs/cardano-multiplatform-lib-nodejs": "6.2.0-1",
30
30
  "bip39": "^3.1.0",
31
31
  "@lucid-evolution/core-types": "0.3.0",
32
- "@lucid-evolution/core-utils": "0.1.16",
33
- "@lucid-evolution/sign_data": "0.1.31",
34
- "@lucid-evolution/utils": "0.1.72"
32
+ "@lucid-evolution/core-utils": "0.1.17",
33
+ "@lucid-evolution/sign_data": "0.1.32",
34
+ "@lucid-evolution/utils": "0.1.74"
35
35
  },
36
36
  "devDependencies": {
37
37
  "@types/node": "^20.12.8",