Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX in testnet #8

Open
tonycesar opened this issue Sep 14, 2022 · 1 comment
Open

FIX in testnet #8

tonycesar opened this issue Sep 14, 2022 · 1 comment

Comments

@tonycesar
Copy link
Author

import bitcore from "bitcore-lib";
import axios from 'axios';

const is_mainnet = false;
const btcApi =  is_mainnet ? 'https://api.bitcore.io/api/BTC/mainnet': 'https://api.bitcore.io/api/BTC/testnet';
if (is_mainnet) {
    bitcore.Networks.defaultNetwork = bitcore.Networks.testnet;
}

const createWallet = () => {
    var privateKey = new bitcore.PrivateKey();
    var address = privateKey.toAddress();
    return { address: address.toString(), privateKey: privateKey.toString() };
};


/**
 * 
 * @param {*} wallet 
 * @returns array of UTOXs
 */
const getUTXOs = async (wallet) => {
    const resposeUTOXs = await axios.get(btcApi+ '/address/' +wallet + '/?unspent=true');
    return resposeUTOXs.data.map(utox => ({
        address: utox.address,
        txId: utox.mintTxid,
        satoshis: +utox.value,
        script: utox.script,
        outputIndex: utox.mintIndex,
    }));
}

const buildTransaction = async ({ from, to, data, value }) => {
    let utxos = await getUTXOs(from);
    return new bitcore.Transaction()
    .from(utxos)
    .to(to, value)
    .change(from);
}

const sendSignedTransaction = async (transaction, privateKey) => {
    const pvtKey = new bitcore.PrivateKey(privateKey);
    const tx = transaction.sign(pvtKey);
    const txHex = tx.serialize();
    return await  axios.post(btcApi + '/tx/send', {rawTx: txHex})
}

const walletMain = 'miniiesTdDvmpLZ3uwdxpiAEYkCA3zp7Sf'
const walletMainKey = 'e6989d6eee921104087ce358469ad61ec5025b35208e9573d1d794e2308b7379';
const walletSecond = 'mhRxrgcTskzRVJda6M3METXaNmB9rmsSs3';

const transaction = await buildTransaction({
            from: walletMain,
            to: walletSecond,
            value: 1000
        });
const result = await sendSignedTransaction(transaction, walletMainKey);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant