-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathone.js
123 lines (93 loc) · 4.09 KB
/
one.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import { Lucid, Blockfrost } from "lucid-cardano";
const initializeLucid = async() => {
try {
// Initialize Lucid
const lucid = await Lucid.new(
new Blockfrost(
"https://cardano-preprod.blockfrost.io/api/v0",
"preprodN1EZYj11zL89jJeaAjeRybxYMLp7grmn"
),
"Preprod"
);
return lucid;
}catch(e){
console.error("Error initializing Lucid:", e);
}
}
async function main() {
try {
// Wait for Lucid initialization
const lucid = await initializeLucid();
// Generate a private key
const privateKey = lucid.utils.generatePrivateKey(); // Bech32 encoded private key
console.log("Generated Private Key (Bech32):", privateKey);
// Generae Seed Phase
const seedPhrase = lucid.utils.generateSeedPhrase()
console.log("Seed phrase:", seedPhrase);
// Now we select a private key wallet with our Lucid instance
const wallet = lucid.selectWalletFromPrivateKey(privateKey);
console.log(wallet);
// Get wallet address
// const address=await wallet.address();
const address = await lucid.wallet.address(); // Bech32 address
console.log("Address:", address);
} catch (error) {
console.error("Error in main function:", error);
}
}
const getBalance = async () => {
const lucid = await initializeLucid();
// first fetch exsisting wallet
// from private key
lucid.selectWalletFromPrivateKey(
"ed25519_sk1wwm0dtvkq87gg9a2lldzrukyn6a5lemekdtc78em8g8xnwxsthrsesn3na"
);
// then from that wallet we get
// same generated wallet address
const address = await lucid.wallet.address(); // Bech32 address
console.log("Address from private_key:", address);
// we can also import exsisting wallet
// from seed phrase
lucid.selectWalletFromSeed("tone side reject sport priority rug bachelor prefer blanket table blush supreme finish blame play rib chimney friend such keep gentle man approve ritual");
const anotherAddress = await lucid.wallet.address();
console.log("Address from seed:", anotherAddress);
// import wallet address from nami wallet seed phrase
lucid.selectWalletFromSeed("chronic negative aisle sugar mother tobacco elephant pitch useless swift wear seven obvious purchase impose nature chapter nuclear trap setup dumb core hybrid slight");
const newAddress = await lucid.wallet.address();
console.log("Address from nami_Seed:", newAddress);
// const api = await window.cardano.nami.enable();
// lucid.selectWallet(api);
// Query UTxOs
const utxos = await lucid.wallet.getUtxos();
console.log("Utxos of nami_wallet:", utxos);
// Utxos of nami_wallet: [
// {
// txHash: '9647332851ec9202afa6a131f94c5f2c5d045a94da906d03662da5baf2ee29b7',
// outputIndex: 0,
// assets: { lovelace: 100000000n },
// address: 'addr_test1qrd45jy5ztsnq6037jnw9emu0d8yzcx7dzvkd6pgmsg3s989f4d58eywysu4egmv88de9ydewcefsdm867rgr28mt3xqqxgyds',
// datumHash: undefined,
// datum: undefined,
// scriptRef: undefined
// }
// ]
// Simple ADA payment working ---------------------------------------
// const tx = await lucid.newTx()
// .payToAddress("addr_test1qznl6ehymnju4m5lrykhstychgfcnpf0yymwn4yslzv96whv0dpa5rfnaxu5dtunn7a2ynkzgmp82v76kamfw5j478wqpkhv50", { lovelace: 500000n })
// .complete();
// const signedTx = await tx.sign().complete();
// const txHash = await signedTx.submit();
// console.log(txHash);
// Multiple recipients --------------------------------------------------------
// Each payToAddress call creates new UTxO, also for same addresses.
// Lucid takes the order of outputs into account.
const tx = await lucid.newTx()
.payToAddress("addr_testa...", { lovelace: 5000000n })
.payToAddress("addr_testb...", { lovelace: 5000000n })
.payToAddress("addr_testc...", { lovelace: 5000000n })
.complete();
const signedTx = await tx.sign().complete();
const txHash = await signedTx.submit();
}
// 1. main();
getBalance();