bitcore icon indicating copy to clipboard operation
bitcore copied to clipboard

how to sign transaction offline

Open dev-dantealighieri opened this issue 9 months ago • 5 comments

I have two questions

  1. I want to send to multiple addresses, is this tx.to usage correct?
  2. How can I sign this transaction offline, can i serialize without sign and sign that offline?
    let tx = new Transaction().from(utxos);

    tx = tx.to(address1, amount1);
    tx = tx.to(address2, amount2); // is this valid?

    tx = tx.fee(fee);
    tx = tx.sign('private key'); // how to do this online
    
    return tx.serialize();

thanks, dante

dev-dantealighieri avatar May 07 '24 14:05 dev-dantealighieri

Your code is correct. If you want to clean it up a bit, you actually don't need to re-assign tx as all of those will modify tx in place.

As for signing, your private keys need to be the corresponding private keys for the utxos. You don't need to be online for any of it. The whole thing can be done offline. Serialize will then return a hex string of the signed tx that you can broadcast from any device.

kajoseph avatar May 08 '24 01:05 kajoseph

thanks for the reply @kajoseph!

I have an external service to sign, which does not use bitcore-lib, instead it uses elliptic. How should I sign these utxos separately(which are owned by different addresses and private keys), should i create a transaction object for each of these utxos, then serialize them, and sign the hex output? (bitcores sign function only takes 1 private key, thats what confused me)

If its this way, how should i construct the final transaction object before broadcasting? If my way is not correct, whats the correct way?

thanks, dante

dev-dantealighieri avatar May 08 '24 07:05 dev-dantealighieri

The sign function can take an array of private keys to sign all utxos, or you can call tx.sign multiple times.

If you're only trying to use elliptic, then you'll need to reimplement the sign function on your own either by copying the code out of bitcore-lib or following some other docs. How the signatures get applied depends on the utxo type.

I would recommend using bitcore-lib in your signing service if you can, then do something like this:

...
tx.fee(fee);
return tx.uncheckedSerialize(); // unsigned tx hex string to send to signer

Signer:

const tx = new Transaction(unsignedTxHex);
tx.sign([privateKey1, privateKey2, ...]);
return tx.serialize(); // signed tx hex

Then broadcast the signed tx.

kajoseph avatar May 08 '24 10:05 kajoseph

thanks for the reply, sorry for my questions, im not familiar with bitcoin.

I don't get how should i create data to sign for each utxo, and merge them after signing.

what should i do for each addresses utxo's? should i create a Transaction object containing multiple addresses utxos, or should i do a different thing for each utxo, sign them separately, and create a Transaction object later.

it would be great if you provide a working example, or types that i shall use

thanks, dante

dev-dantealighieri avatar May 08 '24 14:05 dev-dantealighieri

@kajoseph hello, can you provide information on how I can sign utxos with different private keys on same transaction?

dev-dantealighieri avatar May 14 '24 06:05 dev-dantealighieri