4

When you receive a transaction in the http request, you are getting a string with the XRD base64 form of the client signed transaction:

{"transaction": "AAAAAGjeCRajN67nRkVtYO+lpxax9gvitX9FxhZYGXQvs16hAAAAZAAAAAAAAAAAAAAAAQAAAABbcutKAAAAAFty7HYAAAAAAAAAAQAAAAEAAAAAVIx5odtAgqQ+dp4m4QfWntHbOq0hxRCLGI+2Sm0P6EMAAAAKAAAAC01vYml1cyBhdXRoAAAAAAEAAABAG01TL/Ha0YGVrAF6t0UEKP/0Q/NDUymciQBA/CXzYMVlEx2KcHrq3MkpQ9+9sCbCiOYa7wCtusa1tHKygvZRSwAAAAAAAAABL7NeoQAAAEC9v5jdxReIxoCcCXw90dVsIpXwHXkSHUUthCs98D/zpd6TNPvcMgUsQd6cDHzjNk+/00P8M5bHP4rIpFTm7MwN"}

If you are using JS, you can use the following to verify that the transaction was signed by the server PK:

const tx = new Transaction(req.body.transaction);
const op = tx.operations[0];
const { signatures } = tx;
const hash = tx.hash();

// Source account is ours
if (tx.source != SERVER_KEY_PAIR.publicKey()) {
  return res.json({ error: "Invalid source account." });
}

// Challenge transaction was generated by us
if (
  !signatures.some(signature =>
    SERVER_KEY_PAIR.verify(hash, signature.signature())
  )
) {
  return res.json({ error: "Server signature is missing or invalid." });
}

I'm using Golang, but I think I'm using the wrong types and methods because the verification is failing.

var txXDR xdr.TransactionEnvelope
err = xdr.SafeUnmarshalBase64(txeBase64, &txXDR)
if err != nil {
    return fmt.Errorf("Error Unmarhsaling TX: %s", err)
}

The problem using that Transaction Envelope is that I do not see in the Go-SDK the equivalent of

const hash = tx.hash();
SERVER_KEY_PAIR.verify(hash, signat`ure.signature())

when using that XDR Tx.

Can anyone point me to the right direction?

1 Answer 1

6

play.golang.org

package main

import (
  "fmt"

  "github.com/stellar/go/network"
  "github.com/stellar/go/keypair"
  "github.com/stellar/go/xdr"
)

func main() {
  var env xdr.TransactionEnvelope
  err := xdr.SafeUnmarshalBase64("AAAAAE//tVvL/xn3WM2MincerNCe6rekrjZD+f1ACDuU5AxkAAAAZAAAJAEAAuElAAAAAAAAAAAAAAABAAAAAAAAAAMAAAABSFQAAAAAAACbIx6CN2rDLn2KMYzJ36wouCgJ9qAN7GEXXvDJ3frLagAAAAFCVEMAAAAAAJsjHoI3asMufYoxjMnfrCi4KAn2oA3sYRde8Mnd+stqAAAAAAAAAAAAAACpAAWPcAAAAAABmk29AAAAAAAAAAGU5AxkAAAAQFmbZIHErm5yTe9UnrVoRoTtH5aA98Uw1m+H4ZXJs/Y6EIhCGb4e32wijsxMF12HQ73NxkJXE8+fgTSx2x0VIgA=", &env)
  if err != nil {
    panic(err)
  }
  txHash, err := network.HashTransaction(&env.Tx, network.TestNetworkPassphrase)
  if err != nil {
    panic(err)
  }
  kp := keypair.MustParse("GBH77NK3ZP7RT52YZWGIU5Y6VTIJ52VXUSXDMQ7Z7VAAQO4U4QGGIROV")

  err = kp.Verify(txHash[:], env.Signatures[0].Signature)
  fmt.Println("Will be non-nil when sig is invalid:", err)
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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