6

I'm trying to send over the network a base64 encoded string representing a partly signed envelope. I need to parse the string back to the original javascript object in order to add a new signature to it.

3

The first serialization can be done via:

const base64str = transaction.toEnvelope().toXDR().toString('base64')

Then you have a base64 encoded string that can be safely transmitted over the network.

To restore it back to the original object you can do:

const tx = new Transaction(base64str)
  • You can also do transaction.toEnvelope().toXDR('base64') but the types are wrong. – Jesús Fuentes Apr 20 '18 at 10:08
  • You are right.. the typings are not updated. – phra Apr 20 '18 at 10:09
1

Encoding: You can serialize the transaction by first converting to XDR (which is a binary format) and then converting to a base64 string so it can be sent over the wire.

// sign with empty signature to convert to a transaction envelope
signed, e := txn.Sign()
if e != nil {
    log.Fatal("failed to sign: ", e)
}

// convert to base64 (b64 is a string)
b64, e := signed.Base64()
if e != nil {
    log.Fatal("failed to convert to base64: ", e)
}

Decoding: Then you can decode the base64 string to binary and then build the transaction from the binary.

// decodeFromBase64 decodes the transaction from a base64 string into a TransactionEnvelopeBuilder
func decodeFromBase64(encodedXdr string) *b.TransactionEnvelopeBuilder {
    // Unmarshall from base64 encoded XDR format
    var decoded xdr.TransactionEnvelope
    e := xdr.SafeUnmarshalBase64(encodedXdr, &decoded)
    if e != nil {
        log.Fatal(e)
    }

    // convert to TransactionEnvelopeBuilder
    txEnvelopeBuilder := b.TransactionEnvelopeBuilder{E: &decoded}
    txEnvelopeBuilder.Init()

    return &txEnvelopeBuilder
}

Note: You will need to input the network passphrase when you decode it because it is not transmitted as part of the base64-encoded Transaction XDR

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.