@lucid-evolution/wallet 0.1.4 → 0.1.6
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 +224 -31
- package/dist/index.d.cts +14 -2
- package/dist/index.d.ts +14 -2
- package/dist/index.js +226 -31
- package/package.json +6 -6
package/dist/index.cjs
CHANGED
|
@@ -31,6 +31,9 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
31
31
|
var src_exports = {};
|
|
32
32
|
__export(src_exports, {
|
|
33
33
|
discoverOwnUsedTxKeyHashes: () => discoverOwnUsedTxKeyHashes,
|
|
34
|
+
makeWalletFromAPI: () => makeWalletFromAPI,
|
|
35
|
+
makeWalletFromPrivateKey: () => makeWalletFromPrivateKey,
|
|
36
|
+
makeWalletFromSeed: () => makeWalletFromSeed,
|
|
34
37
|
walletFromSeed: () => walletFromSeed
|
|
35
38
|
});
|
|
36
39
|
module.exports = __toCommonJS(src_exports);
|
|
@@ -98,41 +101,56 @@ function discoverOwnUsedTxKeyHashes(tx, ownKeyHashes, ownUtxos) {
|
|
|
98
101
|
return;
|
|
99
102
|
for (let i = 0; i < certs.len(); i++) {
|
|
100
103
|
const cert = certs.get(i);
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
+
switch (cert.kind()) {
|
|
105
|
+
case 0:
|
|
106
|
+
break;
|
|
107
|
+
case 1: {
|
|
108
|
+
const credential = cert.as_stake_deregistration()?.stake_credential();
|
|
109
|
+
switch (credential?.kind()) {
|
|
110
|
+
case 0:
|
|
111
|
+
usedKeyHashes.push(credential.as_pub_key()?.to_hex());
|
|
112
|
+
break;
|
|
113
|
+
case 1:
|
|
114
|
+
usedKeyHashes.push(credential.as_script()?.to_hex());
|
|
115
|
+
break;
|
|
116
|
+
}
|
|
117
|
+
break;
|
|
104
118
|
}
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
119
|
+
case 2: {
|
|
120
|
+
const credential = cert.as_stake_delegation()?.stake_credential();
|
|
121
|
+
if (credential?.kind() === 0) {
|
|
122
|
+
const keyHash = credential.to_cbor_hex();
|
|
123
|
+
usedKeyHashes.push(keyHash);
|
|
124
|
+
}
|
|
125
|
+
break;
|
|
110
126
|
}
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
127
|
+
case 3: {
|
|
128
|
+
const poolParams = cert.as_pool_registration()?.pool_params();
|
|
129
|
+
const owners = poolParams?.pool_owners();
|
|
130
|
+
if (!owners)
|
|
131
|
+
break;
|
|
132
|
+
for (let i2 = 0; i2 < owners.len(); i2++) {
|
|
133
|
+
const keyHash = owners.get(i2).to_hex();
|
|
134
|
+
usedKeyHashes.push(keyHash);
|
|
135
|
+
}
|
|
136
|
+
const operator = poolParams.operator().to_hex();
|
|
137
|
+
usedKeyHashes.push(operator);
|
|
138
|
+
break;
|
|
116
139
|
}
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
if (!owners)
|
|
140
|
+
case 4: {
|
|
141
|
+
const operator = cert.as_pool_retirement()?.pool().to_hex();
|
|
142
|
+
usedKeyHashes.push(operator);
|
|
121
143
|
break;
|
|
122
|
-
for (let i2 = 0; i2 < owners.len(); i2++) {
|
|
123
|
-
const keyHash = owners.get(i2).to_hex();
|
|
124
|
-
usedKeyHashes.push(keyHash);
|
|
125
144
|
}
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
const credential = cert.as_unreg_cert()?.stake_credential();
|
|
133
|
-
if (credential) {
|
|
134
|
-
usedKeyHashes.push(credential.to_cbor_hex());
|
|
145
|
+
case 6: {
|
|
146
|
+
const credential = cert.as_unreg_cert()?.stake_credential();
|
|
147
|
+
if (credential) {
|
|
148
|
+
usedKeyHashes.push(credential.to_cbor_hex());
|
|
149
|
+
}
|
|
150
|
+
break;
|
|
135
151
|
}
|
|
152
|
+
default:
|
|
153
|
+
break;
|
|
136
154
|
}
|
|
137
155
|
}
|
|
138
156
|
}
|
|
@@ -143,8 +161,13 @@ function discoverOwnUsedTxKeyHashes(tx, ownKeyHashes, ownUtxos) {
|
|
|
143
161
|
const rewardAddresses = withdrawals2.keys();
|
|
144
162
|
for (let i = 0; i < rewardAddresses.len(); i++) {
|
|
145
163
|
const credential = rewardAddresses.get(i).payment();
|
|
146
|
-
|
|
147
|
-
|
|
164
|
+
switch (credential.kind()) {
|
|
165
|
+
case 0:
|
|
166
|
+
usedKeyHashes.push(credential.as_pub_key()?.to_hex());
|
|
167
|
+
break;
|
|
168
|
+
case 1:
|
|
169
|
+
usedKeyHashes.push(credential.as_script()?.to_hex());
|
|
170
|
+
break;
|
|
148
171
|
}
|
|
149
172
|
}
|
|
150
173
|
}
|
|
@@ -197,8 +220,178 @@ function discoverOwnUsedTxKeyHashes(tx, ownKeyHashes, ownUtxos) {
|
|
|
197
220
|
}
|
|
198
221
|
return usedKeyHashes.filter((k) => ownKeyHashes.includes(k));
|
|
199
222
|
}
|
|
223
|
+
|
|
224
|
+
// src/wallet_selection.ts
|
|
225
|
+
var import_core_utils2 = require("@lucid-evolution/core-utils");
|
|
226
|
+
var import_utils2 = require("@lucid-evolution/utils");
|
|
227
|
+
var CML2 = __toESM(require("@dcspark/cardano-multiplatform-lib-nodejs"), 1);
|
|
228
|
+
var import_sign_data = require("@lucid-evolution/sign_data");
|
|
229
|
+
var makeWalletFromSeed = (provider, network, seed, options) => {
|
|
230
|
+
const { address, rewardAddress, paymentKey, stakeKey } = walletFromSeed(
|
|
231
|
+
seed,
|
|
232
|
+
{
|
|
233
|
+
addressType: options?.addressType || "Base",
|
|
234
|
+
accountIndex: options?.accountIndex || 0,
|
|
235
|
+
password: options?.password,
|
|
236
|
+
network
|
|
237
|
+
}
|
|
238
|
+
);
|
|
239
|
+
const paymentKeyHash = CML2.PrivateKey.from_bech32(paymentKey).to_public().hash().to_hex();
|
|
240
|
+
const stakeKeyHash = stakeKey ? CML2.PrivateKey.from_bech32(stakeKey).to_public().hash().to_hex() : "";
|
|
241
|
+
const privKeyHashMap = {
|
|
242
|
+
[paymentKeyHash]: paymentKey,
|
|
243
|
+
[stakeKeyHash]: stakeKey
|
|
244
|
+
};
|
|
245
|
+
return {
|
|
246
|
+
address: async () => address,
|
|
247
|
+
rewardAddress: async () => rewardAddress || null,
|
|
248
|
+
getUtxos: async () => provider.getUtxos((0, import_utils2.paymentCredentialOf)(address)),
|
|
249
|
+
getUtxosCore: async () => {
|
|
250
|
+
const utxos = await provider.getUtxos((0, import_utils2.paymentCredentialOf)(address));
|
|
251
|
+
const coreUtxos = [];
|
|
252
|
+
utxos.forEach((utxo) => coreUtxos.push((0, import_utils2.utxoToCore)(utxo)));
|
|
253
|
+
return coreUtxos;
|
|
254
|
+
},
|
|
255
|
+
getDelegation: async () => {
|
|
256
|
+
return rewardAddress ? await provider.getDelegation(rewardAddress) : { poolId: null, rewards: 0n };
|
|
257
|
+
},
|
|
258
|
+
signTx: async (tx) => {
|
|
259
|
+
const utxos = await provider.getUtxos(address);
|
|
260
|
+
const ownKeyHashes = [paymentKeyHash, stakeKeyHash];
|
|
261
|
+
const usedKeyHashes = discoverOwnUsedTxKeyHashes(tx, ownKeyHashes, utxos);
|
|
262
|
+
const txWitnessSetBuilder = CML2.TransactionWitnessSetBuilder.new();
|
|
263
|
+
usedKeyHashes.forEach((keyHash) => {
|
|
264
|
+
const priv = CML2.PrivateKey.from_bech32(privKeyHashMap[keyHash]);
|
|
265
|
+
const witness = CML2.make_vkey_witness(
|
|
266
|
+
CML2.hash_transaction(tx.body()),
|
|
267
|
+
// toCMLTransactionHash(tx.body()),
|
|
268
|
+
priv
|
|
269
|
+
);
|
|
270
|
+
txWitnessSetBuilder.add_vkey(witness);
|
|
271
|
+
});
|
|
272
|
+
return txWitnessSetBuilder.build();
|
|
273
|
+
},
|
|
274
|
+
signMessage: async (address2, payload) => {
|
|
275
|
+
const {
|
|
276
|
+
paymentCredential,
|
|
277
|
+
stakeCredential,
|
|
278
|
+
address: { hex: hexAddress }
|
|
279
|
+
} = (0, import_utils2.getAddressDetails)(address2);
|
|
280
|
+
const keyHash = paymentCredential?.hash || stakeCredential?.hash;
|
|
281
|
+
const privateKey = privKeyHashMap[keyHash];
|
|
282
|
+
if (!privateKey) {
|
|
283
|
+
throw new Error(`Cannot sign message for address: ${address2}.`);
|
|
284
|
+
}
|
|
285
|
+
return (0, import_sign_data.signData)(hexAddress, payload, privateKey);
|
|
286
|
+
},
|
|
287
|
+
submitTx: async (tx) => {
|
|
288
|
+
return await provider.submitTx(tx);
|
|
289
|
+
}
|
|
290
|
+
};
|
|
291
|
+
};
|
|
292
|
+
var makeWalletFromPrivateKey = (provider, network, privateKey) => {
|
|
293
|
+
const priv = CML2.PrivateKey.from_bech32(privateKey);
|
|
294
|
+
const pubKeyHash = priv.to_public().hash();
|
|
295
|
+
const address = CML2.EnterpriseAddress.new(
|
|
296
|
+
network === "Mainnet" ? 1 : 0,
|
|
297
|
+
CML2.Credential.new_pub_key(pubKeyHash)
|
|
298
|
+
).to_address().to_bech32(void 0);
|
|
299
|
+
return {
|
|
300
|
+
address: async () => address,
|
|
301
|
+
rewardAddress: async () => null,
|
|
302
|
+
getUtxos: async () => {
|
|
303
|
+
return await provider.getUtxos((0, import_utils2.paymentCredentialOf)(address));
|
|
304
|
+
},
|
|
305
|
+
getUtxosCore: async () => {
|
|
306
|
+
const utxos = await provider.getUtxos((0, import_utils2.paymentCredentialOf)(address));
|
|
307
|
+
const coreUtxos = [];
|
|
308
|
+
utxos.forEach((utxo) => {
|
|
309
|
+
coreUtxos.push((0, import_utils2.utxoToCore)(utxo));
|
|
310
|
+
});
|
|
311
|
+
return coreUtxos;
|
|
312
|
+
},
|
|
313
|
+
getDelegation: async () => {
|
|
314
|
+
return { poolId: null, rewards: 0n };
|
|
315
|
+
},
|
|
316
|
+
signTx: async (tx) => {
|
|
317
|
+
const signed = priv.sign(tx.to_cbor_bytes());
|
|
318
|
+
const witness = CML2.Vkeywitness.new(priv.to_public(), signed);
|
|
319
|
+
const txWitnessSetBuilder = CML2.TransactionWitnessSetBuilder.new();
|
|
320
|
+
txWitnessSetBuilder.add_vkey(witness);
|
|
321
|
+
return txWitnessSetBuilder.build();
|
|
322
|
+
},
|
|
323
|
+
signMessage: async (address2, payload) => {
|
|
324
|
+
const {
|
|
325
|
+
paymentCredential,
|
|
326
|
+
address: { hex: hexAddress }
|
|
327
|
+
} = (0, import_utils2.getAddressDetails)(address2);
|
|
328
|
+
const keyHash = paymentCredential?.hash;
|
|
329
|
+
const originalKeyHash = pubKeyHash.to_hex();
|
|
330
|
+
if (!keyHash || keyHash !== originalKeyHash) {
|
|
331
|
+
throw new Error(`Cannot sign message for address: ${address2}.`);
|
|
332
|
+
}
|
|
333
|
+
return (0, import_sign_data.signData)(hexAddress, payload, privateKey);
|
|
334
|
+
},
|
|
335
|
+
submitTx: async (tx) => {
|
|
336
|
+
return await provider.submitTx(tx);
|
|
337
|
+
}
|
|
338
|
+
};
|
|
339
|
+
};
|
|
340
|
+
var makeWalletFromAPI = (provider, api) => {
|
|
341
|
+
const getAddressHex = async () => {
|
|
342
|
+
const [addressHex] = await api.getUsedAddresses();
|
|
343
|
+
if (addressHex)
|
|
344
|
+
return addressHex;
|
|
345
|
+
const [unusedAddressHex] = await api.getUnusedAddresses();
|
|
346
|
+
return unusedAddressHex;
|
|
347
|
+
};
|
|
348
|
+
const getRewardAddress = async () => {
|
|
349
|
+
const [rewardAddressHex] = await api.getRewardAddresses();
|
|
350
|
+
const rewardAddress = rewardAddressHex ? CML2.RewardAddress.from_address(CML2.Address.from_hex(rewardAddressHex)).to_address().to_bech32(void 0) : null;
|
|
351
|
+
return rewardAddress;
|
|
352
|
+
};
|
|
353
|
+
return {
|
|
354
|
+
address: async () => CML2.Address.from_hex(await getAddressHex()).to_bech32(void 0),
|
|
355
|
+
rewardAddress: async () => getRewardAddress(),
|
|
356
|
+
getUtxos: async () => {
|
|
357
|
+
const utxos = (await api.getUtxos() || []).map((utxo) => {
|
|
358
|
+
const parsedUtxo = CML2.TransactionUnspentOutput.from_cbor_bytes(
|
|
359
|
+
(0, import_core_utils2.fromHex)(utxo)
|
|
360
|
+
);
|
|
361
|
+
return (0, import_utils2.coreToUtxo)(parsedUtxo);
|
|
362
|
+
});
|
|
363
|
+
return utxos;
|
|
364
|
+
},
|
|
365
|
+
getUtxosCore: async () => {
|
|
366
|
+
const utxos = [];
|
|
367
|
+
(await api.getUtxos() || []).forEach((utxo) => {
|
|
368
|
+
utxos.push(CML2.TransactionUnspentOutput.from_cbor_hex(utxo));
|
|
369
|
+
});
|
|
370
|
+
return utxos;
|
|
371
|
+
},
|
|
372
|
+
getDelegation: async () => {
|
|
373
|
+
const rewardAddr = await getRewardAddress();
|
|
374
|
+
return rewardAddr ? await provider.getDelegation(rewardAddr) : { poolId: null, rewards: 0n };
|
|
375
|
+
},
|
|
376
|
+
signTx: async (tx) => {
|
|
377
|
+
const witnessSet = await api.signTx((0, import_core_utils2.toHex)(tx.to_cbor_bytes()), true);
|
|
378
|
+
return CML2.TransactionWitnessSet.from_cbor_hex(witnessSet);
|
|
379
|
+
},
|
|
380
|
+
signMessage: async (address, payload) => {
|
|
381
|
+
const hexAddress = (0, import_core_utils2.toHex)(CML2.Address.from_bech32(address).to_raw_bytes());
|
|
382
|
+
return await api.signData(hexAddress, payload);
|
|
383
|
+
},
|
|
384
|
+
submitTx: async (tx) => {
|
|
385
|
+
const txHash = await api.submitTx(tx);
|
|
386
|
+
return txHash;
|
|
387
|
+
}
|
|
388
|
+
};
|
|
389
|
+
};
|
|
200
390
|
// Annotate the CommonJS export names for ESM import in node:
|
|
201
391
|
0 && (module.exports = {
|
|
202
392
|
discoverOwnUsedTxKeyHashes,
|
|
393
|
+
makeWalletFromAPI,
|
|
394
|
+
makeWalletFromPrivateKey,
|
|
395
|
+
makeWalletFromSeed,
|
|
203
396
|
walletFromSeed
|
|
204
397
|
});
|
package/dist/index.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Network, KeyHash, UTxO, Address, RewardAddress, PrivateKey } from '@lucid-evolution/core-types';
|
|
1
|
+
import { Network, KeyHash, UTxO, Address, RewardAddress, PrivateKey, Provider, Wallet, WalletApi } from '@lucid-evolution/core-types';
|
|
2
2
|
import * as CML from '@dcspark/cardano-multiplatform-lib-nodejs';
|
|
3
3
|
|
|
4
4
|
type FromSeed = {
|
|
@@ -15,4 +15,16 @@ declare function walletFromSeed(seed: string, options?: {
|
|
|
15
15
|
}): FromSeed;
|
|
16
16
|
declare function discoverOwnUsedTxKeyHashes(tx: CML.Transaction, ownKeyHashes: Array<KeyHash>, ownUtxos: Array<UTxO>): Array<KeyHash>;
|
|
17
17
|
|
|
18
|
-
|
|
18
|
+
declare const makeWalletFromSeed: (provider: Provider, network: Network, seed: string, options?: {
|
|
19
|
+
addressType?: "Base" | "Enterprise";
|
|
20
|
+
accountIndex?: number;
|
|
21
|
+
password?: string;
|
|
22
|
+
}) => Wallet;
|
|
23
|
+
/**
|
|
24
|
+
* Cardano Private key in bech32; not the BIP32 private key or any key that is not fully derived.
|
|
25
|
+
* Only an Enteprise address (without stake credential) is derived.
|
|
26
|
+
*/
|
|
27
|
+
declare const makeWalletFromPrivateKey: (provider: Provider, network: Network, privateKey: PrivateKey) => Wallet;
|
|
28
|
+
declare const makeWalletFromAPI: (provider: Provider, api: WalletApi) => Wallet;
|
|
29
|
+
|
|
30
|
+
export { discoverOwnUsedTxKeyHashes, makeWalletFromAPI, makeWalletFromPrivateKey, makeWalletFromSeed, walletFromSeed };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Network, KeyHash, UTxO, Address, RewardAddress, PrivateKey } from '@lucid-evolution/core-types';
|
|
1
|
+
import { Network, KeyHash, UTxO, Address, RewardAddress, PrivateKey, Provider, Wallet, WalletApi } from '@lucid-evolution/core-types';
|
|
2
2
|
import * as CML from '@dcspark/cardano-multiplatform-lib-nodejs';
|
|
3
3
|
|
|
4
4
|
type FromSeed = {
|
|
@@ -15,4 +15,16 @@ declare function walletFromSeed(seed: string, options?: {
|
|
|
15
15
|
}): FromSeed;
|
|
16
16
|
declare function discoverOwnUsedTxKeyHashes(tx: CML.Transaction, ownKeyHashes: Array<KeyHash>, ownUtxos: Array<UTxO>): Array<KeyHash>;
|
|
17
17
|
|
|
18
|
-
|
|
18
|
+
declare const makeWalletFromSeed: (provider: Provider, network: Network, seed: string, options?: {
|
|
19
|
+
addressType?: "Base" | "Enterprise";
|
|
20
|
+
accountIndex?: number;
|
|
21
|
+
password?: string;
|
|
22
|
+
}) => Wallet;
|
|
23
|
+
/**
|
|
24
|
+
* Cardano Private key in bech32; not the BIP32 private key or any key that is not fully derived.
|
|
25
|
+
* Only an Enteprise address (without stake credential) is derived.
|
|
26
|
+
*/
|
|
27
|
+
declare const makeWalletFromPrivateKey: (provider: Provider, network: Network, privateKey: PrivateKey) => Wallet;
|
|
28
|
+
declare const makeWalletFromAPI: (provider: Provider, api: WalletApi) => Wallet;
|
|
29
|
+
|
|
30
|
+
export { discoverOwnUsedTxKeyHashes, makeWalletFromAPI, makeWalletFromPrivateKey, makeWalletFromSeed, walletFromSeed };
|
package/dist/index.js
CHANGED
|
@@ -61,41 +61,56 @@ function discoverOwnUsedTxKeyHashes(tx, ownKeyHashes, ownUtxos) {
|
|
|
61
61
|
return;
|
|
62
62
|
for (let i = 0; i < certs.len(); i++) {
|
|
63
63
|
const cert = certs.get(i);
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
64
|
+
switch (cert.kind()) {
|
|
65
|
+
case 0:
|
|
66
|
+
break;
|
|
67
|
+
case 1: {
|
|
68
|
+
const credential = cert.as_stake_deregistration()?.stake_credential();
|
|
69
|
+
switch (credential?.kind()) {
|
|
70
|
+
case 0:
|
|
71
|
+
usedKeyHashes.push(credential.as_pub_key()?.to_hex());
|
|
72
|
+
break;
|
|
73
|
+
case 1:
|
|
74
|
+
usedKeyHashes.push(credential.as_script()?.to_hex());
|
|
75
|
+
break;
|
|
76
|
+
}
|
|
77
|
+
break;
|
|
67
78
|
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
79
|
+
case 2: {
|
|
80
|
+
const credential = cert.as_stake_delegation()?.stake_credential();
|
|
81
|
+
if (credential?.kind() === 0) {
|
|
82
|
+
const keyHash = credential.to_cbor_hex();
|
|
83
|
+
usedKeyHashes.push(keyHash);
|
|
84
|
+
}
|
|
85
|
+
break;
|
|
73
86
|
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
87
|
+
case 3: {
|
|
88
|
+
const poolParams = cert.as_pool_registration()?.pool_params();
|
|
89
|
+
const owners = poolParams?.pool_owners();
|
|
90
|
+
if (!owners)
|
|
91
|
+
break;
|
|
92
|
+
for (let i2 = 0; i2 < owners.len(); i2++) {
|
|
93
|
+
const keyHash = owners.get(i2).to_hex();
|
|
94
|
+
usedKeyHashes.push(keyHash);
|
|
95
|
+
}
|
|
96
|
+
const operator = poolParams.operator().to_hex();
|
|
97
|
+
usedKeyHashes.push(operator);
|
|
98
|
+
break;
|
|
79
99
|
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
if (!owners)
|
|
100
|
+
case 4: {
|
|
101
|
+
const operator = cert.as_pool_retirement()?.pool().to_hex();
|
|
102
|
+
usedKeyHashes.push(operator);
|
|
84
103
|
break;
|
|
85
|
-
for (let i2 = 0; i2 < owners.len(); i2++) {
|
|
86
|
-
const keyHash = owners.get(i2).to_hex();
|
|
87
|
-
usedKeyHashes.push(keyHash);
|
|
88
104
|
}
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
const credential = cert.as_unreg_cert()?.stake_credential();
|
|
96
|
-
if (credential) {
|
|
97
|
-
usedKeyHashes.push(credential.to_cbor_hex());
|
|
105
|
+
case 6: {
|
|
106
|
+
const credential = cert.as_unreg_cert()?.stake_credential();
|
|
107
|
+
if (credential) {
|
|
108
|
+
usedKeyHashes.push(credential.to_cbor_hex());
|
|
109
|
+
}
|
|
110
|
+
break;
|
|
98
111
|
}
|
|
112
|
+
default:
|
|
113
|
+
break;
|
|
99
114
|
}
|
|
100
115
|
}
|
|
101
116
|
}
|
|
@@ -106,8 +121,13 @@ function discoverOwnUsedTxKeyHashes(tx, ownKeyHashes, ownUtxos) {
|
|
|
106
121
|
const rewardAddresses = withdrawals2.keys();
|
|
107
122
|
for (let i = 0; i < rewardAddresses.len(); i++) {
|
|
108
123
|
const credential = rewardAddresses.get(i).payment();
|
|
109
|
-
|
|
110
|
-
|
|
124
|
+
switch (credential.kind()) {
|
|
125
|
+
case 0:
|
|
126
|
+
usedKeyHashes.push(credential.as_pub_key()?.to_hex());
|
|
127
|
+
break;
|
|
128
|
+
case 1:
|
|
129
|
+
usedKeyHashes.push(credential.as_script()?.to_hex());
|
|
130
|
+
break;
|
|
111
131
|
}
|
|
112
132
|
}
|
|
113
133
|
}
|
|
@@ -160,7 +180,182 @@ function discoverOwnUsedTxKeyHashes(tx, ownKeyHashes, ownUtxos) {
|
|
|
160
180
|
}
|
|
161
181
|
return usedKeyHashes.filter((k) => ownKeyHashes.includes(k));
|
|
162
182
|
}
|
|
183
|
+
|
|
184
|
+
// src/wallet_selection.ts
|
|
185
|
+
import { fromHex as fromHex2, toHex } from "@lucid-evolution/core-utils";
|
|
186
|
+
import {
|
|
187
|
+
coreToUtxo,
|
|
188
|
+
getAddressDetails as getAddressDetails2,
|
|
189
|
+
paymentCredentialOf,
|
|
190
|
+
utxoToCore
|
|
191
|
+
} from "@lucid-evolution/utils";
|
|
192
|
+
import * as CML2 from "@dcspark/cardano-multiplatform-lib-nodejs";
|
|
193
|
+
import { signData } from "@lucid-evolution/sign_data";
|
|
194
|
+
var makeWalletFromSeed = (provider, network, seed, options) => {
|
|
195
|
+
const { address, rewardAddress, paymentKey, stakeKey } = walletFromSeed(
|
|
196
|
+
seed,
|
|
197
|
+
{
|
|
198
|
+
addressType: options?.addressType || "Base",
|
|
199
|
+
accountIndex: options?.accountIndex || 0,
|
|
200
|
+
password: options?.password,
|
|
201
|
+
network
|
|
202
|
+
}
|
|
203
|
+
);
|
|
204
|
+
const paymentKeyHash = CML2.PrivateKey.from_bech32(paymentKey).to_public().hash().to_hex();
|
|
205
|
+
const stakeKeyHash = stakeKey ? CML2.PrivateKey.from_bech32(stakeKey).to_public().hash().to_hex() : "";
|
|
206
|
+
const privKeyHashMap = {
|
|
207
|
+
[paymentKeyHash]: paymentKey,
|
|
208
|
+
[stakeKeyHash]: stakeKey
|
|
209
|
+
};
|
|
210
|
+
return {
|
|
211
|
+
address: async () => address,
|
|
212
|
+
rewardAddress: async () => rewardAddress || null,
|
|
213
|
+
getUtxos: async () => provider.getUtxos(paymentCredentialOf(address)),
|
|
214
|
+
getUtxosCore: async () => {
|
|
215
|
+
const utxos = await provider.getUtxos(paymentCredentialOf(address));
|
|
216
|
+
const coreUtxos = [];
|
|
217
|
+
utxos.forEach((utxo) => coreUtxos.push(utxoToCore(utxo)));
|
|
218
|
+
return coreUtxos;
|
|
219
|
+
},
|
|
220
|
+
getDelegation: async () => {
|
|
221
|
+
return rewardAddress ? await provider.getDelegation(rewardAddress) : { poolId: null, rewards: 0n };
|
|
222
|
+
},
|
|
223
|
+
signTx: async (tx) => {
|
|
224
|
+
const utxos = await provider.getUtxos(address);
|
|
225
|
+
const ownKeyHashes = [paymentKeyHash, stakeKeyHash];
|
|
226
|
+
const usedKeyHashes = discoverOwnUsedTxKeyHashes(tx, ownKeyHashes, utxos);
|
|
227
|
+
const txWitnessSetBuilder = CML2.TransactionWitnessSetBuilder.new();
|
|
228
|
+
usedKeyHashes.forEach((keyHash) => {
|
|
229
|
+
const priv = CML2.PrivateKey.from_bech32(privKeyHashMap[keyHash]);
|
|
230
|
+
const witness = CML2.make_vkey_witness(
|
|
231
|
+
CML2.hash_transaction(tx.body()),
|
|
232
|
+
// toCMLTransactionHash(tx.body()),
|
|
233
|
+
priv
|
|
234
|
+
);
|
|
235
|
+
txWitnessSetBuilder.add_vkey(witness);
|
|
236
|
+
});
|
|
237
|
+
return txWitnessSetBuilder.build();
|
|
238
|
+
},
|
|
239
|
+
signMessage: async (address2, payload) => {
|
|
240
|
+
const {
|
|
241
|
+
paymentCredential,
|
|
242
|
+
stakeCredential,
|
|
243
|
+
address: { hex: hexAddress }
|
|
244
|
+
} = getAddressDetails2(address2);
|
|
245
|
+
const keyHash = paymentCredential?.hash || stakeCredential?.hash;
|
|
246
|
+
const privateKey = privKeyHashMap[keyHash];
|
|
247
|
+
if (!privateKey) {
|
|
248
|
+
throw new Error(`Cannot sign message for address: ${address2}.`);
|
|
249
|
+
}
|
|
250
|
+
return signData(hexAddress, payload, privateKey);
|
|
251
|
+
},
|
|
252
|
+
submitTx: async (tx) => {
|
|
253
|
+
return await provider.submitTx(tx);
|
|
254
|
+
}
|
|
255
|
+
};
|
|
256
|
+
};
|
|
257
|
+
var makeWalletFromPrivateKey = (provider, network, privateKey) => {
|
|
258
|
+
const priv = CML2.PrivateKey.from_bech32(privateKey);
|
|
259
|
+
const pubKeyHash = priv.to_public().hash();
|
|
260
|
+
const address = CML2.EnterpriseAddress.new(
|
|
261
|
+
network === "Mainnet" ? 1 : 0,
|
|
262
|
+
CML2.Credential.new_pub_key(pubKeyHash)
|
|
263
|
+
).to_address().to_bech32(void 0);
|
|
264
|
+
return {
|
|
265
|
+
address: async () => address,
|
|
266
|
+
rewardAddress: async () => null,
|
|
267
|
+
getUtxos: async () => {
|
|
268
|
+
return await provider.getUtxos(paymentCredentialOf(address));
|
|
269
|
+
},
|
|
270
|
+
getUtxosCore: async () => {
|
|
271
|
+
const utxos = await provider.getUtxos(paymentCredentialOf(address));
|
|
272
|
+
const coreUtxos = [];
|
|
273
|
+
utxos.forEach((utxo) => {
|
|
274
|
+
coreUtxos.push(utxoToCore(utxo));
|
|
275
|
+
});
|
|
276
|
+
return coreUtxos;
|
|
277
|
+
},
|
|
278
|
+
getDelegation: async () => {
|
|
279
|
+
return { poolId: null, rewards: 0n };
|
|
280
|
+
},
|
|
281
|
+
signTx: async (tx) => {
|
|
282
|
+
const signed = priv.sign(tx.to_cbor_bytes());
|
|
283
|
+
const witness = CML2.Vkeywitness.new(priv.to_public(), signed);
|
|
284
|
+
const txWitnessSetBuilder = CML2.TransactionWitnessSetBuilder.new();
|
|
285
|
+
txWitnessSetBuilder.add_vkey(witness);
|
|
286
|
+
return txWitnessSetBuilder.build();
|
|
287
|
+
},
|
|
288
|
+
signMessage: async (address2, payload) => {
|
|
289
|
+
const {
|
|
290
|
+
paymentCredential,
|
|
291
|
+
address: { hex: hexAddress }
|
|
292
|
+
} = getAddressDetails2(address2);
|
|
293
|
+
const keyHash = paymentCredential?.hash;
|
|
294
|
+
const originalKeyHash = pubKeyHash.to_hex();
|
|
295
|
+
if (!keyHash || keyHash !== originalKeyHash) {
|
|
296
|
+
throw new Error(`Cannot sign message for address: ${address2}.`);
|
|
297
|
+
}
|
|
298
|
+
return signData(hexAddress, payload, privateKey);
|
|
299
|
+
},
|
|
300
|
+
submitTx: async (tx) => {
|
|
301
|
+
return await provider.submitTx(tx);
|
|
302
|
+
}
|
|
303
|
+
};
|
|
304
|
+
};
|
|
305
|
+
var makeWalletFromAPI = (provider, api) => {
|
|
306
|
+
const getAddressHex = async () => {
|
|
307
|
+
const [addressHex] = await api.getUsedAddresses();
|
|
308
|
+
if (addressHex)
|
|
309
|
+
return addressHex;
|
|
310
|
+
const [unusedAddressHex] = await api.getUnusedAddresses();
|
|
311
|
+
return unusedAddressHex;
|
|
312
|
+
};
|
|
313
|
+
const getRewardAddress = async () => {
|
|
314
|
+
const [rewardAddressHex] = await api.getRewardAddresses();
|
|
315
|
+
const rewardAddress = rewardAddressHex ? CML2.RewardAddress.from_address(CML2.Address.from_hex(rewardAddressHex)).to_address().to_bech32(void 0) : null;
|
|
316
|
+
return rewardAddress;
|
|
317
|
+
};
|
|
318
|
+
return {
|
|
319
|
+
address: async () => CML2.Address.from_hex(await getAddressHex()).to_bech32(void 0),
|
|
320
|
+
rewardAddress: async () => getRewardAddress(),
|
|
321
|
+
getUtxos: async () => {
|
|
322
|
+
const utxos = (await api.getUtxos() || []).map((utxo) => {
|
|
323
|
+
const parsedUtxo = CML2.TransactionUnspentOutput.from_cbor_bytes(
|
|
324
|
+
fromHex2(utxo)
|
|
325
|
+
);
|
|
326
|
+
return coreToUtxo(parsedUtxo);
|
|
327
|
+
});
|
|
328
|
+
return utxos;
|
|
329
|
+
},
|
|
330
|
+
getUtxosCore: async () => {
|
|
331
|
+
const utxos = [];
|
|
332
|
+
(await api.getUtxos() || []).forEach((utxo) => {
|
|
333
|
+
utxos.push(CML2.TransactionUnspentOutput.from_cbor_hex(utxo));
|
|
334
|
+
});
|
|
335
|
+
return utxos;
|
|
336
|
+
},
|
|
337
|
+
getDelegation: async () => {
|
|
338
|
+
const rewardAddr = await getRewardAddress();
|
|
339
|
+
return rewardAddr ? await provider.getDelegation(rewardAddr) : { poolId: null, rewards: 0n };
|
|
340
|
+
},
|
|
341
|
+
signTx: async (tx) => {
|
|
342
|
+
const witnessSet = await api.signTx(toHex(tx.to_cbor_bytes()), true);
|
|
343
|
+
return CML2.TransactionWitnessSet.from_cbor_hex(witnessSet);
|
|
344
|
+
},
|
|
345
|
+
signMessage: async (address, payload) => {
|
|
346
|
+
const hexAddress = toHex(CML2.Address.from_bech32(address).to_raw_bytes());
|
|
347
|
+
return await api.signData(hexAddress, payload);
|
|
348
|
+
},
|
|
349
|
+
submitTx: async (tx) => {
|
|
350
|
+
const txHash = await api.submitTx(tx);
|
|
351
|
+
return txHash;
|
|
352
|
+
}
|
|
353
|
+
};
|
|
354
|
+
};
|
|
163
355
|
export {
|
|
164
356
|
discoverOwnUsedTxKeyHashes,
|
|
357
|
+
makeWalletFromAPI,
|
|
358
|
+
makeWalletFromPrivateKey,
|
|
359
|
+
makeWalletFromSeed,
|
|
165
360
|
walletFromSeed
|
|
166
361
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lucid-evolution/wallet",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.6",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -23,11 +23,11 @@
|
|
|
23
23
|
"license": "MIT",
|
|
24
24
|
"dependencies": {
|
|
25
25
|
"@dcspark/cardano-multiplatform-lib-nodejs": "^5.2.0",
|
|
26
|
-
"@lucid-evolution/bip39": "0.2.
|
|
27
|
-
"@lucid-evolution/core-types": "0.1.
|
|
28
|
-
"@lucid-evolution/core-utils": "0.1.
|
|
29
|
-
"@lucid-evolution/utils": "0.1.
|
|
30
|
-
"@lucid-evolution/sign_data": "0.1.
|
|
26
|
+
"@lucid-evolution/bip39": "0.2.5",
|
|
27
|
+
"@lucid-evolution/core-types": "0.1.4",
|
|
28
|
+
"@lucid-evolution/core-utils": "0.1.4",
|
|
29
|
+
"@lucid-evolution/utils": "0.1.6",
|
|
30
|
+
"@lucid-evolution/sign_data": "0.1.4"
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|
|
33
33
|
"@types/node": "^20.11.24",
|