@lucid-evolution/wallet 0.1.4 → 0.1.5
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 +217 -29
- package/dist/index.d.cts +14 -2
- package/dist/index.d.ts +14 -2
- package/dist/index.js +219 -29
- package/package.json +3 -3
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
|
}
|
|
@@ -197,8 +215,178 @@ function discoverOwnUsedTxKeyHashes(tx, ownKeyHashes, ownUtxos) {
|
|
|
197
215
|
}
|
|
198
216
|
return usedKeyHashes.filter((k) => ownKeyHashes.includes(k));
|
|
199
217
|
}
|
|
218
|
+
|
|
219
|
+
// src/wallet_selection.ts
|
|
220
|
+
var import_core_utils2 = require("@lucid-evolution/core-utils");
|
|
221
|
+
var import_utils2 = require("@lucid-evolution/utils");
|
|
222
|
+
var CML2 = __toESM(require("@dcspark/cardano-multiplatform-lib-nodejs"), 1);
|
|
223
|
+
var import_sign_data = require("@lucid-evolution/sign_data");
|
|
224
|
+
var makeWalletFromSeed = (provider, network, seed, options) => {
|
|
225
|
+
const { address, rewardAddress, paymentKey, stakeKey } = walletFromSeed(
|
|
226
|
+
seed,
|
|
227
|
+
{
|
|
228
|
+
addressType: options?.addressType || "Base",
|
|
229
|
+
accountIndex: options?.accountIndex || 0,
|
|
230
|
+
password: options?.password,
|
|
231
|
+
network
|
|
232
|
+
}
|
|
233
|
+
);
|
|
234
|
+
const paymentKeyHash = CML2.PrivateKey.from_bech32(paymentKey).to_public().hash().to_hex();
|
|
235
|
+
const stakeKeyHash = stakeKey ? CML2.PrivateKey.from_bech32(stakeKey).to_public().hash().to_hex() : "";
|
|
236
|
+
const privKeyHashMap = {
|
|
237
|
+
[paymentKeyHash]: paymentKey,
|
|
238
|
+
[stakeKeyHash]: stakeKey
|
|
239
|
+
};
|
|
240
|
+
return {
|
|
241
|
+
address: async () => address,
|
|
242
|
+
rewardAddress: async () => rewardAddress || null,
|
|
243
|
+
getUtxos: async () => provider.getUtxos((0, import_utils2.paymentCredentialOf)(address)),
|
|
244
|
+
getUtxosCore: async () => {
|
|
245
|
+
const utxos = await provider.getUtxos((0, import_utils2.paymentCredentialOf)(address));
|
|
246
|
+
const coreUtxos = [];
|
|
247
|
+
utxos.forEach((utxo) => coreUtxos.push((0, import_utils2.utxoToCore)(utxo)));
|
|
248
|
+
return coreUtxos;
|
|
249
|
+
},
|
|
250
|
+
getDelegation: async () => {
|
|
251
|
+
return rewardAddress ? await provider.getDelegation(rewardAddress) : { poolId: null, rewards: 0n };
|
|
252
|
+
},
|
|
253
|
+
signTx: async (tx) => {
|
|
254
|
+
const utxos = await provider.getUtxos(address);
|
|
255
|
+
const ownKeyHashes = [paymentKeyHash, stakeKeyHash];
|
|
256
|
+
const usedKeyHashes = discoverOwnUsedTxKeyHashes(tx, ownKeyHashes, utxos);
|
|
257
|
+
const txWitnessSetBuilder = CML2.TransactionWitnessSetBuilder.new();
|
|
258
|
+
usedKeyHashes.forEach((keyHash) => {
|
|
259
|
+
const priv = CML2.PrivateKey.from_bech32(privKeyHashMap[keyHash]);
|
|
260
|
+
const witness = CML2.make_vkey_witness(
|
|
261
|
+
CML2.hash_transaction(tx.body()),
|
|
262
|
+
// toCMLTransactionHash(tx.body()),
|
|
263
|
+
priv
|
|
264
|
+
);
|
|
265
|
+
txWitnessSetBuilder.add_vkey(witness);
|
|
266
|
+
});
|
|
267
|
+
return txWitnessSetBuilder.build();
|
|
268
|
+
},
|
|
269
|
+
signMessage: async (address2, payload) => {
|
|
270
|
+
const {
|
|
271
|
+
paymentCredential,
|
|
272
|
+
stakeCredential,
|
|
273
|
+
address: { hex: hexAddress }
|
|
274
|
+
} = (0, import_utils2.getAddressDetails)(address2);
|
|
275
|
+
const keyHash = paymentCredential?.hash || stakeCredential?.hash;
|
|
276
|
+
const privateKey = privKeyHashMap[keyHash];
|
|
277
|
+
if (!privateKey) {
|
|
278
|
+
throw new Error(`Cannot sign message for address: ${address2}.`);
|
|
279
|
+
}
|
|
280
|
+
return (0, import_sign_data.signData)(hexAddress, payload, privateKey);
|
|
281
|
+
},
|
|
282
|
+
submitTx: async (tx) => {
|
|
283
|
+
return await provider.submitTx(tx);
|
|
284
|
+
}
|
|
285
|
+
};
|
|
286
|
+
};
|
|
287
|
+
var makeWalletFromPrivateKey = (provider, network, privateKey) => {
|
|
288
|
+
const priv = CML2.PrivateKey.from_bech32(privateKey);
|
|
289
|
+
const pubKeyHash = priv.to_public().hash();
|
|
290
|
+
const address = CML2.EnterpriseAddress.new(
|
|
291
|
+
network === "Mainnet" ? 1 : 0,
|
|
292
|
+
CML2.Credential.new_pub_key(pubKeyHash)
|
|
293
|
+
).to_address().to_bech32(void 0);
|
|
294
|
+
return {
|
|
295
|
+
address: async () => address,
|
|
296
|
+
rewardAddress: async () => null,
|
|
297
|
+
getUtxos: async () => {
|
|
298
|
+
return await provider.getUtxos((0, import_utils2.paymentCredentialOf)(address));
|
|
299
|
+
},
|
|
300
|
+
getUtxosCore: async () => {
|
|
301
|
+
const utxos = await provider.getUtxos((0, import_utils2.paymentCredentialOf)(address));
|
|
302
|
+
const coreUtxos = [];
|
|
303
|
+
utxos.forEach((utxo) => {
|
|
304
|
+
coreUtxos.push((0, import_utils2.utxoToCore)(utxo));
|
|
305
|
+
});
|
|
306
|
+
return coreUtxos;
|
|
307
|
+
},
|
|
308
|
+
getDelegation: async () => {
|
|
309
|
+
return { poolId: null, rewards: 0n };
|
|
310
|
+
},
|
|
311
|
+
signTx: async (tx) => {
|
|
312
|
+
const signed = priv.sign(tx.to_cbor_bytes());
|
|
313
|
+
const witness = CML2.Vkeywitness.new(priv.to_public(), signed);
|
|
314
|
+
const txWitnessSetBuilder = CML2.TransactionWitnessSetBuilder.new();
|
|
315
|
+
txWitnessSetBuilder.add_vkey(witness);
|
|
316
|
+
return txWitnessSetBuilder.build();
|
|
317
|
+
},
|
|
318
|
+
signMessage: async (address2, payload) => {
|
|
319
|
+
const {
|
|
320
|
+
paymentCredential,
|
|
321
|
+
address: { hex: hexAddress }
|
|
322
|
+
} = (0, import_utils2.getAddressDetails)(address2);
|
|
323
|
+
const keyHash = paymentCredential?.hash;
|
|
324
|
+
const originalKeyHash = pubKeyHash.to_hex();
|
|
325
|
+
if (!keyHash || keyHash !== originalKeyHash) {
|
|
326
|
+
throw new Error(`Cannot sign message for address: ${address2}.`);
|
|
327
|
+
}
|
|
328
|
+
return (0, import_sign_data.signData)(hexAddress, payload, privateKey);
|
|
329
|
+
},
|
|
330
|
+
submitTx: async (tx) => {
|
|
331
|
+
return await provider.submitTx(tx);
|
|
332
|
+
}
|
|
333
|
+
};
|
|
334
|
+
};
|
|
335
|
+
var makeWalletFromAPI = (provider, api) => {
|
|
336
|
+
const getAddressHex = async () => {
|
|
337
|
+
const [addressHex] = await api.getUsedAddresses();
|
|
338
|
+
if (addressHex)
|
|
339
|
+
return addressHex;
|
|
340
|
+
const [unusedAddressHex] = await api.getUnusedAddresses();
|
|
341
|
+
return unusedAddressHex;
|
|
342
|
+
};
|
|
343
|
+
const getRewardAddress = async () => {
|
|
344
|
+
const [rewardAddressHex] = await api.getRewardAddresses();
|
|
345
|
+
const rewardAddress = rewardAddressHex ? CML2.RewardAddress.from_address(CML2.Address.from_hex(rewardAddressHex)).to_address().to_bech32(void 0) : null;
|
|
346
|
+
return rewardAddress;
|
|
347
|
+
};
|
|
348
|
+
return {
|
|
349
|
+
address: async () => CML2.Address.from_hex(await getAddressHex()).to_bech32(void 0),
|
|
350
|
+
rewardAddress: async () => getRewardAddress(),
|
|
351
|
+
getUtxos: async () => {
|
|
352
|
+
const utxos = (await api.getUtxos() || []).map((utxo) => {
|
|
353
|
+
const parsedUtxo = CML2.TransactionUnspentOutput.from_cbor_bytes(
|
|
354
|
+
(0, import_core_utils2.fromHex)(utxo)
|
|
355
|
+
);
|
|
356
|
+
return (0, import_utils2.coreToUtxo)(parsedUtxo);
|
|
357
|
+
});
|
|
358
|
+
return utxos;
|
|
359
|
+
},
|
|
360
|
+
getUtxosCore: async () => {
|
|
361
|
+
const utxos = [];
|
|
362
|
+
(await api.getUtxos() || []).forEach((utxo) => {
|
|
363
|
+
utxos.push(CML2.TransactionUnspentOutput.from_cbor_hex(utxo));
|
|
364
|
+
});
|
|
365
|
+
return utxos;
|
|
366
|
+
},
|
|
367
|
+
getDelegation: async () => {
|
|
368
|
+
const rewardAddr = await getRewardAddress();
|
|
369
|
+
return rewardAddr ? await provider.getDelegation(rewardAddr) : { poolId: null, rewards: 0n };
|
|
370
|
+
},
|
|
371
|
+
signTx: async (tx) => {
|
|
372
|
+
const witnessSet = await api.signTx((0, import_core_utils2.toHex)(tx.to_cbor_bytes()), true);
|
|
373
|
+
return CML2.TransactionWitnessSet.from_cbor_hex(witnessSet);
|
|
374
|
+
},
|
|
375
|
+
signMessage: async (address, payload) => {
|
|
376
|
+
const hexAddress = (0, import_core_utils2.toHex)(CML2.Address.from_bech32(address).to_raw_bytes());
|
|
377
|
+
return await api.signData(hexAddress, payload);
|
|
378
|
+
},
|
|
379
|
+
submitTx: async (tx) => {
|
|
380
|
+
const txHash = await api.submitTx(tx);
|
|
381
|
+
return txHash;
|
|
382
|
+
}
|
|
383
|
+
};
|
|
384
|
+
};
|
|
200
385
|
// Annotate the CommonJS export names for ESM import in node:
|
|
201
386
|
0 && (module.exports = {
|
|
202
387
|
discoverOwnUsedTxKeyHashes,
|
|
388
|
+
makeWalletFromAPI,
|
|
389
|
+
makeWalletFromPrivateKey,
|
|
390
|
+
makeWalletFromSeed,
|
|
203
391
|
walletFromSeed
|
|
204
392
|
});
|
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
|
}
|
|
@@ -160,7 +175,182 @@ function discoverOwnUsedTxKeyHashes(tx, ownKeyHashes, ownUtxos) {
|
|
|
160
175
|
}
|
|
161
176
|
return usedKeyHashes.filter((k) => ownKeyHashes.includes(k));
|
|
162
177
|
}
|
|
178
|
+
|
|
179
|
+
// src/wallet_selection.ts
|
|
180
|
+
import { fromHex as fromHex2, toHex } from "@lucid-evolution/core-utils";
|
|
181
|
+
import {
|
|
182
|
+
coreToUtxo,
|
|
183
|
+
getAddressDetails as getAddressDetails2,
|
|
184
|
+
paymentCredentialOf,
|
|
185
|
+
utxoToCore
|
|
186
|
+
} from "@lucid-evolution/utils";
|
|
187
|
+
import * as CML2 from "@dcspark/cardano-multiplatform-lib-nodejs";
|
|
188
|
+
import { signData } from "@lucid-evolution/sign_data";
|
|
189
|
+
var makeWalletFromSeed = (provider, network, seed, options) => {
|
|
190
|
+
const { address, rewardAddress, paymentKey, stakeKey } = walletFromSeed(
|
|
191
|
+
seed,
|
|
192
|
+
{
|
|
193
|
+
addressType: options?.addressType || "Base",
|
|
194
|
+
accountIndex: options?.accountIndex || 0,
|
|
195
|
+
password: options?.password,
|
|
196
|
+
network
|
|
197
|
+
}
|
|
198
|
+
);
|
|
199
|
+
const paymentKeyHash = CML2.PrivateKey.from_bech32(paymentKey).to_public().hash().to_hex();
|
|
200
|
+
const stakeKeyHash = stakeKey ? CML2.PrivateKey.from_bech32(stakeKey).to_public().hash().to_hex() : "";
|
|
201
|
+
const privKeyHashMap = {
|
|
202
|
+
[paymentKeyHash]: paymentKey,
|
|
203
|
+
[stakeKeyHash]: stakeKey
|
|
204
|
+
};
|
|
205
|
+
return {
|
|
206
|
+
address: async () => address,
|
|
207
|
+
rewardAddress: async () => rewardAddress || null,
|
|
208
|
+
getUtxos: async () => provider.getUtxos(paymentCredentialOf(address)),
|
|
209
|
+
getUtxosCore: async () => {
|
|
210
|
+
const utxos = await provider.getUtxos(paymentCredentialOf(address));
|
|
211
|
+
const coreUtxos = [];
|
|
212
|
+
utxos.forEach((utxo) => coreUtxos.push(utxoToCore(utxo)));
|
|
213
|
+
return coreUtxos;
|
|
214
|
+
},
|
|
215
|
+
getDelegation: async () => {
|
|
216
|
+
return rewardAddress ? await provider.getDelegation(rewardAddress) : { poolId: null, rewards: 0n };
|
|
217
|
+
},
|
|
218
|
+
signTx: async (tx) => {
|
|
219
|
+
const utxos = await provider.getUtxos(address);
|
|
220
|
+
const ownKeyHashes = [paymentKeyHash, stakeKeyHash];
|
|
221
|
+
const usedKeyHashes = discoverOwnUsedTxKeyHashes(tx, ownKeyHashes, utxos);
|
|
222
|
+
const txWitnessSetBuilder = CML2.TransactionWitnessSetBuilder.new();
|
|
223
|
+
usedKeyHashes.forEach((keyHash) => {
|
|
224
|
+
const priv = CML2.PrivateKey.from_bech32(privKeyHashMap[keyHash]);
|
|
225
|
+
const witness = CML2.make_vkey_witness(
|
|
226
|
+
CML2.hash_transaction(tx.body()),
|
|
227
|
+
// toCMLTransactionHash(tx.body()),
|
|
228
|
+
priv
|
|
229
|
+
);
|
|
230
|
+
txWitnessSetBuilder.add_vkey(witness);
|
|
231
|
+
});
|
|
232
|
+
return txWitnessSetBuilder.build();
|
|
233
|
+
},
|
|
234
|
+
signMessage: async (address2, payload) => {
|
|
235
|
+
const {
|
|
236
|
+
paymentCredential,
|
|
237
|
+
stakeCredential,
|
|
238
|
+
address: { hex: hexAddress }
|
|
239
|
+
} = getAddressDetails2(address2);
|
|
240
|
+
const keyHash = paymentCredential?.hash || stakeCredential?.hash;
|
|
241
|
+
const privateKey = privKeyHashMap[keyHash];
|
|
242
|
+
if (!privateKey) {
|
|
243
|
+
throw new Error(`Cannot sign message for address: ${address2}.`);
|
|
244
|
+
}
|
|
245
|
+
return signData(hexAddress, payload, privateKey);
|
|
246
|
+
},
|
|
247
|
+
submitTx: async (tx) => {
|
|
248
|
+
return await provider.submitTx(tx);
|
|
249
|
+
}
|
|
250
|
+
};
|
|
251
|
+
};
|
|
252
|
+
var makeWalletFromPrivateKey = (provider, network, privateKey) => {
|
|
253
|
+
const priv = CML2.PrivateKey.from_bech32(privateKey);
|
|
254
|
+
const pubKeyHash = priv.to_public().hash();
|
|
255
|
+
const address = CML2.EnterpriseAddress.new(
|
|
256
|
+
network === "Mainnet" ? 1 : 0,
|
|
257
|
+
CML2.Credential.new_pub_key(pubKeyHash)
|
|
258
|
+
).to_address().to_bech32(void 0);
|
|
259
|
+
return {
|
|
260
|
+
address: async () => address,
|
|
261
|
+
rewardAddress: async () => null,
|
|
262
|
+
getUtxos: async () => {
|
|
263
|
+
return await provider.getUtxos(paymentCredentialOf(address));
|
|
264
|
+
},
|
|
265
|
+
getUtxosCore: async () => {
|
|
266
|
+
const utxos = await provider.getUtxos(paymentCredentialOf(address));
|
|
267
|
+
const coreUtxos = [];
|
|
268
|
+
utxos.forEach((utxo) => {
|
|
269
|
+
coreUtxos.push(utxoToCore(utxo));
|
|
270
|
+
});
|
|
271
|
+
return coreUtxos;
|
|
272
|
+
},
|
|
273
|
+
getDelegation: async () => {
|
|
274
|
+
return { poolId: null, rewards: 0n };
|
|
275
|
+
},
|
|
276
|
+
signTx: async (tx) => {
|
|
277
|
+
const signed = priv.sign(tx.to_cbor_bytes());
|
|
278
|
+
const witness = CML2.Vkeywitness.new(priv.to_public(), signed);
|
|
279
|
+
const txWitnessSetBuilder = CML2.TransactionWitnessSetBuilder.new();
|
|
280
|
+
txWitnessSetBuilder.add_vkey(witness);
|
|
281
|
+
return txWitnessSetBuilder.build();
|
|
282
|
+
},
|
|
283
|
+
signMessage: async (address2, payload) => {
|
|
284
|
+
const {
|
|
285
|
+
paymentCredential,
|
|
286
|
+
address: { hex: hexAddress }
|
|
287
|
+
} = getAddressDetails2(address2);
|
|
288
|
+
const keyHash = paymentCredential?.hash;
|
|
289
|
+
const originalKeyHash = pubKeyHash.to_hex();
|
|
290
|
+
if (!keyHash || keyHash !== originalKeyHash) {
|
|
291
|
+
throw new Error(`Cannot sign message for address: ${address2}.`);
|
|
292
|
+
}
|
|
293
|
+
return signData(hexAddress, payload, privateKey);
|
|
294
|
+
},
|
|
295
|
+
submitTx: async (tx) => {
|
|
296
|
+
return await provider.submitTx(tx);
|
|
297
|
+
}
|
|
298
|
+
};
|
|
299
|
+
};
|
|
300
|
+
var makeWalletFromAPI = (provider, api) => {
|
|
301
|
+
const getAddressHex = async () => {
|
|
302
|
+
const [addressHex] = await api.getUsedAddresses();
|
|
303
|
+
if (addressHex)
|
|
304
|
+
return addressHex;
|
|
305
|
+
const [unusedAddressHex] = await api.getUnusedAddresses();
|
|
306
|
+
return unusedAddressHex;
|
|
307
|
+
};
|
|
308
|
+
const getRewardAddress = async () => {
|
|
309
|
+
const [rewardAddressHex] = await api.getRewardAddresses();
|
|
310
|
+
const rewardAddress = rewardAddressHex ? CML2.RewardAddress.from_address(CML2.Address.from_hex(rewardAddressHex)).to_address().to_bech32(void 0) : null;
|
|
311
|
+
return rewardAddress;
|
|
312
|
+
};
|
|
313
|
+
return {
|
|
314
|
+
address: async () => CML2.Address.from_hex(await getAddressHex()).to_bech32(void 0),
|
|
315
|
+
rewardAddress: async () => getRewardAddress(),
|
|
316
|
+
getUtxos: async () => {
|
|
317
|
+
const utxos = (await api.getUtxos() || []).map((utxo) => {
|
|
318
|
+
const parsedUtxo = CML2.TransactionUnspentOutput.from_cbor_bytes(
|
|
319
|
+
fromHex2(utxo)
|
|
320
|
+
);
|
|
321
|
+
return coreToUtxo(parsedUtxo);
|
|
322
|
+
});
|
|
323
|
+
return utxos;
|
|
324
|
+
},
|
|
325
|
+
getUtxosCore: async () => {
|
|
326
|
+
const utxos = [];
|
|
327
|
+
(await api.getUtxos() || []).forEach((utxo) => {
|
|
328
|
+
utxos.push(CML2.TransactionUnspentOutput.from_cbor_hex(utxo));
|
|
329
|
+
});
|
|
330
|
+
return utxos;
|
|
331
|
+
},
|
|
332
|
+
getDelegation: async () => {
|
|
333
|
+
const rewardAddr = await getRewardAddress();
|
|
334
|
+
return rewardAddr ? await provider.getDelegation(rewardAddr) : { poolId: null, rewards: 0n };
|
|
335
|
+
},
|
|
336
|
+
signTx: async (tx) => {
|
|
337
|
+
const witnessSet = await api.signTx(toHex(tx.to_cbor_bytes()), true);
|
|
338
|
+
return CML2.TransactionWitnessSet.from_cbor_hex(witnessSet);
|
|
339
|
+
},
|
|
340
|
+
signMessage: async (address, payload) => {
|
|
341
|
+
const hexAddress = toHex(CML2.Address.from_bech32(address).to_raw_bytes());
|
|
342
|
+
return await api.signData(hexAddress, payload);
|
|
343
|
+
},
|
|
344
|
+
submitTx: async (tx) => {
|
|
345
|
+
const txHash = await api.submitTx(tx);
|
|
346
|
+
return txHash;
|
|
347
|
+
}
|
|
348
|
+
};
|
|
349
|
+
};
|
|
163
350
|
export {
|
|
164
351
|
discoverOwnUsedTxKeyHashes,
|
|
352
|
+
makeWalletFromAPI,
|
|
353
|
+
makeWalletFromPrivateKey,
|
|
354
|
+
makeWalletFromSeed,
|
|
165
355
|
walletFromSeed
|
|
166
356
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lucid-evolution/wallet",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.5",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -23,10 +23,10 @@
|
|
|
23
23
|
"license": "MIT",
|
|
24
24
|
"dependencies": {
|
|
25
25
|
"@dcspark/cardano-multiplatform-lib-nodejs": "^5.2.0",
|
|
26
|
-
"@lucid-evolution/bip39": "0.2.
|
|
26
|
+
"@lucid-evolution/bip39": "0.2.4",
|
|
27
27
|
"@lucid-evolution/core-types": "0.1.3",
|
|
28
28
|
"@lucid-evolution/core-utils": "0.1.3",
|
|
29
|
-
"@lucid-evolution/utils": "0.1.
|
|
29
|
+
"@lucid-evolution/utils": "0.1.5",
|
|
30
30
|
"@lucid-evolution/sign_data": "0.1.3"
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|