Bitcoinjs-lib: how can i sign raw serialized transaction

Created on 20 May 2021  路  2Comments  路  Source: bitcoinjs/bitcoinjs-lib

i generated raw unsigned dogecoin transaction using third party api and now i got this:

0100000001eeb5498b1adbf77d39f1f55b3cd943b9da9f192e444504a968e3b4eb02bb4a850100000000ffffffff010052230b390000001976a914c09113adb3c0dffce410382e0ef2e04d3617047d88ac00000000

i am doing like this but it is not working:

var bitcoin = require('bitcoinjs-lib')
var bitcoin_message = require('bitcoinjs-message')
var DOGECOIN =   {
    messagePrefix: '\x19Dogecoin Signed Message:\n',
    bip32: {
        public: 0x02facafd,
        private: 0x02fac398
    },
    pubKeyHash: 0x1e,
    scriptHash: 0x16,
    wif: 0x9e
}
var keyPair = bitcoin.ECPair.fromWIF(Buffer.from('__myWIF__'), DOGECOIN);
var privateKey = keyPair.__d;
var message = '0100000001eeb5498b1adbf77d39f1f55b3cd943b9da9f192e444504a968e3b4eb02bb4a850100000000ffffffff010052230b390000001976a914c09113adb3c0dffce410382e0ef2e04d3617047d88ac00000000';

var signature = bitcoin_message.sign(message, privateKey, keyPair.compressed);
console.log(signature.toString('base64'));

Most helpful comment

const bitcoin = require('bitcoinjs-lib')
const DOGECOIN = {
    messagePrefix: '\x19Dogecoin Signed Message:\n',
    bip32: {
        public: 0x02facafd,
        private: 0x02fac398
    },
    pubKeyHash: 0x1e,
    scriptHash: 0x16,
    wif: 0x9e
}

// myWif is a string, fromWIF takes a string not a Buffer
const keyPair = bitcoin.ECPair.fromWIF('__myWIF__', DOGECOIN);

const txHex = '0100000001eeb5498b1adbf77d39f1f55b3cd943b9da9f192e444504a968e3b4eb02bb4a850100000000ffffffff010052230b390000001976a914c09113adb3c0dffce410382e0ef2e04d3617047d88ac00000000';
const tx = bitcoin.Transaction.fromHex(txHex);
const txb = bitcoin.TransactionBuilder.fromTransaction(tx, DOGECOIN);

// This assumes all inputs are spending utxos sent to the same Dogecoin P2PKH address (starts with D)
for (let i = 0; i < tx.ins.length; i++) {
  txb.sign(i, keyPair);
}

const signedTxHex = txb.build().toHex();
// Broadcast this signed raw transaction
console.log(signedTxHex)

All 2 comments

  1. If your API is using raw serialized transactions and not PSBT, it's a very old API. But seeing as this is for Dogecoin, which is based on an old version of bitcoin, and dogecoin has almost no developers to keep it up to date with the latest updates, it would probably be best to use TransactionBuilder.
  2. Using Transaction's fromHex and then TransactionBuilder's fromTransaction will allow you to create a TransactionBuilder and sign the inputs using that.
  3. Examples: https://github.com/bitcoinjs/bitcoinjs-lib/blob/6c08a0be40a4d382f64b7385531407372a7e3b91/test/integration/transactions.spec.ts
const bitcoin = require('bitcoinjs-lib')
const DOGECOIN = {
    messagePrefix: '\x19Dogecoin Signed Message:\n',
    bip32: {
        public: 0x02facafd,
        private: 0x02fac398
    },
    pubKeyHash: 0x1e,
    scriptHash: 0x16,
    wif: 0x9e
}

// myWif is a string, fromWIF takes a string not a Buffer
const keyPair = bitcoin.ECPair.fromWIF('__myWIF__', DOGECOIN);

const txHex = '0100000001eeb5498b1adbf77d39f1f55b3cd943b9da9f192e444504a968e3b4eb02bb4a850100000000ffffffff010052230b390000001976a914c09113adb3c0dffce410382e0ef2e04d3617047d88ac00000000';
const tx = bitcoin.Transaction.fromHex(txHex);
const txb = bitcoin.TransactionBuilder.fromTransaction(tx, DOGECOIN);

// This assumes all inputs are spending utxos sent to the same Dogecoin P2PKH address (starts with D)
for (let i = 0; i < tx.ins.length; i++) {
  txb.sign(i, keyPair);
}

const signedTxHex = txb.build().toHex();
// Broadcast this signed raw transaction
console.log(signedTxHex)
Was this page helpful?
0 / 5 - 0 ratings

Related issues

Mr-Mondragon picture Mr-Mondragon  路  3Comments

silence-may picture silence-may  路  3Comments

yakitorifoodie picture yakitorifoodie  路  3Comments

dcousens picture dcousens  路  3Comments

LeonYanghaha picture LeonYanghaha  路  3Comments