-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeneratekeys.js
78 lines (66 loc) · 2.13 KB
/
generatekeys.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
import bitcoin from "bitcoinjs-lib";
import ECPairFactory from "ecpair";
import * as ecc from "tiny-secp256k1";
import prompt from "prompt-sync";
import readline from "node:readline";
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
function createAddress() {
//const prompt = require("prompt-sync")({ sigint: true });
//const readline = require("readline");
// const rl = readline.createInterface({
// input: process.stdin,
// output: process.stdout,
// });
// let txid;
// rl.question("What is your transactionId? ", function (val) {
// console.log(`Transaction ID is ${val}`);
// txid = val;
// //console.log(txid);
// rl.close();
// });
const network = bitcoin.networks.testnet;
console.log(network);
const ECPair = ECPairFactory.ECPairFactory(ecc);
const keyPair = ECPair.makeRandom({ network });
//console.log(keyPair);
const { address } = bitcoin.payments.p2pkh({
pubkey: keyPair.publicKey,
network: network,
});
console.log(address);
console.log(keyPair);
return keyPair;
}
function question(query) {
return new Promise((resolve) => {
rl.question(query, resolve);
});
}
async function sendFunds() {
const keyPair = createAddress();
const txid = await question("What is your transactionId? ");
const amount = await question("How much do you have? ");
const destinationAddress = await question(
"What address are you sending to? "
);
const outputNumber = await question("What is the output number?");
console.log(`txid: ${txid}!`);
console.log(`Amount: ${amount}!`);
// const outputNumber = 0;
// const txid = "736884387c55a6c415de583f96b8fe621589dee9944648219a9eef4483ee0804";
// const amount = 0.0008913;
const txb = new bitcoin.Psbt();
txb.network = bitcoin.networks.testnet;
txb.addInput(txid, outputNumber);
//const destinationAddress = "mv4rnyY3Su5gjcDNzbMLKBQkBicCtHUtFB";
const minerFee = 10000;
const outputAmount = amount * 1e8 - minerFee;
txb.addOutput(destinationAddress, outputAmount);
txb.signInput(outputNumber, keyPair);
console.log(txb.build().toHex());
rl.close();
}
sendFunds();