Skip to content

7. Appendix: Transaction signing

Steven Yuan edited this page Dec 9, 2021 · 4 revisions

After sending your payload to the /construction/payloads endpoint, you will receive back something that looks like this:

{
    "unsigned_transaction": "wgFeCiEBDNLFrIGRmHw4MzWZCnFvxkM2iOh6/Pz7ehY4BNUMHuoSMgohAc+To+IRIepBvkKuIrZ88ta1UY4CCLr5/oEEU/IzNAaoEICt4gQYsODAgYOGjJgwGLiRAiCdAQ==",
    "payloads": [
        {
            "hex_bytes": "0a21010cd2c5ac8192987c383335999e716fc6433688e87afcfcfb12163804d40c1eea12320a2101cf93a3e2011dea41be42ae22b67cf2dab5518e0208baf9fe810453f2333406a81080ade20418b0e0c08183868c983018b89102209e01"
        }
    ]
}
  • unsigned_transaction is the base64 encoded version of the unsigned transaction that the helium-js library can understand.
  • ⚠️ hex_bytes is NOT the hex equivalent of the unsigned_transaction. THIS IS IMPORTANT. If you wish to use hex_bytes, this is the raw protobuf message to be signed with your private key using a raw crypto library like libsodium.

Signing an unsigned transaction with Helium is greatly simplified with the helium-js library. However, if you wish to sign the raw payload without the help of the helium library you can still do that.

After you've signed the transaction, the resulting signature is what can be passed into the /construction/combine endpoint to create a signed transaction ready for submission.

How to sign a transaction

1. The Easy Way (helium-js)

Example: Signing a PaymentV2 transaction from unsigned_transaction:

const paymentTxn = PaymentV2.fromString({{unsigned_txn_string}});

// This is only one of many ways to derive a keypair
const keypair = await Keypair.fromWords({{seed_phrase}});

const signedPaymentTxn = await paymentTxn.sign({payer: keypair});
const hexSignature = Buffer.from(signedPaymentTxn.signature).toString('hex'));

// Use the hex representation of the signature for /construction/combine
console.log(hexSignature);

2. The Hard Way

If you wish to not use helium-js for signing, you must have your private key readily available. To sign the payload this way, take hex_bytes, and convert it into a buffer. You can then use a library such as libsodium and sign the payload using a method like crypto_sign_detached.

The resulting signature then need to be converted back into hex format to be passed into /construction/combine