-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
66 lines (53 loc) · 1.83 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
const helpers = require("./utils/helpers");
require('dotenv').config();
const selectedCrypto = "bitcoinTestnet";
const baseUrl =
selectedCrypto === "bitcoinTestnet"
? "https://blockstream.info/testnet"
: "https://blockstream.info";
const path = `m/84'/${selectedCrypto === "bitcoinTestnet" ? "1" : "0"}'/0'/0/0`;
const mnemonic = process.env.MNEMONIC;
const run = async (message) => {
const keyPair = helpers.getKeyPair({
mnemonic,
selectedCrypto,
});
console.log("private key length: ", keyPair.privateKey.length)
const address = helpers.getAddress(keyPair, selectedCrypto, "bech32");
// const seed = bip39.mnemonicToSeedSync(mnemonic, bip39Passphrase);
const scriptHash = helpers.getScriptHash(address, selectedCrypto);
console.log(`${baseUrl}/address/${address}`, "scriptHash", scriptHash);
let res = await helpers.getBalanceFromUtxos({
addresses: [{ address, path, scriptHash }],
changeAddresses: [],
selectedCrypto,
});
// const relayFee = await helpers.wallet.relayFee.default({ selectedCrypto });
// console.log("relayeFee", relayFee);
console.dir(res, { depth: null });
if (!res.data.balance) {
console.log("Balance is 0");
return;
}
if (!res.data.utxos.length) {
setTimeout(run, 3000, message);
return;
}
// create a transaction with a random message. This tx wont send any coins
const { data: txHex } = await helpers.createTransaction({
confirmedBalance: res.data.balance,
changeAddress: address,
mnemonic,
utxos: res.data.utxos,
selectedCrypto,
message,
});
const { data: txHash } = await helpers.wallet.pushtx.default({
rawTx: txHex,
selectedCrypto,
});
if (typeof txHash === "string") {
console.log(`${baseUrl}/tx/${txHash}?expand`);
}
};
run(Buffer.from(process.argv[2] || "68656C6C6F", "hex")); // hello in hex