-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
83 lines (67 loc) · 2.31 KB
/
index.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
const bitcoin = require('bitcoinjs-lib');
const ECPairFactory = require('ecpair').default;
const ecc = require('tiny-secp256k1');
const fs = require('fs');
const axios = require('axios');
const ECPair = ECPairFactory(ecc);
const network = bitcoin.networks.testnet; // Otherwise, bitcoin = mainnet and regnet = local
const baseUrl = "https://blockstream.info/testnet/api/"
// Bitcoin: https://blockstream.info/api/
// Bitcoin Testnet: https://blockstream.info/testnet/api/
async function createP2PKHwallet() {
try {
const keyPair = ECPair.makeRandom({ network: network });
const { address } = bitcoin.payments.p2pkh({
pubkey: keyPair.publicKey,
network: network,
});
const privateKey = keyPair.toWIF()
console.log(`| Public Address | ${address} |`)
console.log(`| Private Key | ${privateKey} |`)
const wallet = {
address: address,
privateKey: privateKey
};
const walletJSON = JSON.stringify(wallet, null, 4);
fs.writeFileSync('wallet.json', walletJSON);
console.log(`Wallet created and saved to wallet.json`);
// Check balance of the generated address
let balance = await checkBalance(address)
console.log("Balance:", balance)
let BlockNumber = await getLatestBlockNumber()
console.log("BlockNumber:", BlockNumber)
} catch (error) {
console.log(error)
}
}
async function checkBalance(address) {
try {
const response = await axios.get(`${baseUrl}/address/${address}/utxo`);
const utxos = response.data;
console.log(await Promise.all(utxos))
let balance = 0;
utxos.forEach(utxo => {
balance += utxo.value;
});
return balance;
} catch (err) {
console.log("err:", err?.response?.data)
}
}
async function getLatestBlockNumber() {
try{
const response = await axios.get(`${baseUrl}/blocks/tip/height`);
return response.data;
}catch(err){
console.log("err:", err?.response?.data)
}
}
async function broadcastTransaction(txHex) {
try {
const response = await axios.post(`${baseUrl}/tx`, txHex);
return response.data;
} catch (err) {
console.log("err:", err?.response?.data)
}
}
createP2PKHwallet();