Nxt Forum

Nxt Discussion => Nxt General Discussion => Topic started by: box1413 on January 21, 2017, 06:14:04 am

Title: How to sign a transaction locally and then send it to a server?
Post by: box1413 on January 21, 2017, 06:14:04 am
Does anyone know which api involves signing a transaction locally then sending it to a server without revealing your password?

Which api's do I have to use?

And is there a code example of this in action?

There's so many API's I just don't know which one to use or how to combine them.
Title: Re: How to sign a transaction locally and then send it to a server?
Post by: lurker10 on January 21, 2017, 07:06:45 am
Any API with a broadcast=false will give you transaction bytes for that transaction. After that use broadcastTransaction API to send it to the network without revealing the passphrase.
Title: Re: How to sign a transaction locally and then send it to a server?
Post by: ScripterRon on January 21, 2017, 04:29:25 pm
Take a look at https://github.com/ScripterRon/NxtCore to see how to sign a transaction.  The Transaction() class creates a signed or unsigned transaction.  The relevant code is the following:
Code: [Select]
  //
  // Sign the transaction
  //
  byte[] txBytes = getBytes(true);
  this.signature = Crypto.sign(txBytes, passPhrase);
  this.signatureHash = Crypto.singleDigest(signature);
The unsigned bytes returned by the server with broadcast=false contain 64 bytes of zero in place of the signature.  Replace those bytes with the signature returned by Crypto.sign() and then broadcast the transaction using the broadcastTransaction API.  The signature bytes are at offset 95 in the transaction (see Transaction.getBytes()).

If you are using Java, you can use the crypto routines in NxtCore.  Otherwise, you will need to provide the crypto routines in your language.  Take a look at the Crypto() class to see what you need to do.

I took a different approach for Ardor which might be simpler to understand.  Take a look at https://github.com/ScripterRon/Nxt2API.  Look at Nxt.broadcastTransaction() which takes the unsigned bytes returned by the server, signs the transaction, and then broadcasts the transaction.  While this library is for use with Ardor, transaction signing is the same as Nxt (the transaction offset of the signature is different).
elective-stereophonic
elective-stereophonic
assembly
assembly