Skip to content

Instantly share code, notes, and snippets.

@schlingel
Created December 10, 2019 09:36
Show Gist options
  • Save schlingel/8fc203cae75663e8592e8a6831b4199f to your computer and use it in GitHub Desktop.
Save schlingel/8fc203cae75663e8592e8a6831b4199f to your computer and use it in GitHub Desktop.

Raw Transactions with BSV and Sending over RPC

Code
const bitcoinRpc = require('node-bitcoin-rpc');
const bsv = require('bsv');

bitcoinRpc.init('localhost', 18445, 'user', 'pwd');

(async () => {
    "use strict";

    async function listUnspent() {
        return new Promise((resolve, reject) => {
            bitcoinRpc.call('listunspent', [], (err, res) => {
                if (!!err) {
                    reject(err);
                } else {
                    resolve(res.result);
                }
            });
        });
    }

    async function getPrivateKey(address) {
        return new Promise((resolve, reject) => {
            bitcoinRpc.call('dumpprivkey', [ address ], (err, res) => {
                if (!!err) {
                    reject(err);
                } else {
                    resolve(res.result);
                }
            });
        });
    }

    async function sendRawTransaction(rawTransaction) {
        return new Promise((resolve, reject) => {
            bitcoinRpc.call('sendrawtransaction', [ rawTransaction ], (err, res) => {
                if (!!err) {
                    reject(err);
                } else {
                    resolve(res.result);
                }
            });
        });
    }

    try {
        const utxos = await listUnspent();

        console.log('picking first spendable utxo with more than 1 BTC');
        const utxo = utxos.find(u => u.spendable && u.amount > 1);
        console.log('found', utxo);

        console.log('Fetching private key');
        const privKeyToken = await getPrivateKey(utxo.address);
        const privKey = new bsv.PrivateKey(privKeyToken);
        console.log('found', privKey.inspect());

        const moneyOutput = new bsv.Transaction.UnspentOutput(utxo);
        console.log(moneyOutput.inspect());

        console.log('Transforming to outgoing output for new transaction');

        const transaction = new bsv.Transaction()
            .from([ utxo ])
            .to('mwaNghmBoUbzxrQfJagwY3C4zAqnVWRJaF', 100) // 100 Satoshis
            .change('mnZuPaoi6ZEdtA8Bak2ELV63XGkWvGgme2')
            .addData([ 'Hello', 'World', 'It', 'is', 'me!' ])
            .sign(privKey);

        console.log('Created and signed transaction', transaction.inspect());
        console.log('Serializing to raw transaction format');
        const rawTransaction = transaction.serialize(true);
        console.log('raw transaction', rawTransaction);

        const result = await sendRawTransaction(rawTransaction);

        console.log('sended raw transaction', result);
    } catch(e) {
        console.error('meh!');
        console.error(e);
    }
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment