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

how to sign transaction offline #3732

Open
dev-dantealighieri opened this issue May 7, 2024 · 5 comments
Open

how to sign transaction offline #3732

dev-dantealighieri opened this issue May 7, 2024 · 5 comments

Comments

@dev-dantealighieri
Copy link

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

@kajoseph
Copy link
Collaborator

kajoseph commented May 8, 2024

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.

@dev-dantealighieri
Copy link
Author

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

@kajoseph
Copy link
Collaborator

kajoseph commented May 8, 2024

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.

@dev-dantealighieri
Copy link
Author

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
Copy link
Author

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

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

2 participants