Skip to main content

Proof of Existence of a Transaction

Transaction proof example

Below is a simple code snippet that shows how to use the ledger API to generate a transaction existence proof:

Please note: For presentation purposes, all results and error handling, except proofs, were eliminated. Ledger connectivity proof is eliminated here as well. For details and an example, see here and here.

    db, err := bcdb.Create(conConf)
session, err := db.Session(&c.SessionConfig)
ledger, err := session.Ledger()

// Load tx content from file
txBytes, err := ioutil.ReadFile(txFile)
tx := &types.DataTxEnvelope{}
err = proto.Unmarshal(txBytes, tx)
txId := tx.Payload.TxId

// Get transaction receipt for txId
txReceipt, err := ledger.GetTransactionReceipt(txId)

// Check if tx is valid
if txReceipt.Header.ValidationInfo[txReceipt.TxIndex].Flag != types.Flag_VALID {
fmt.Printf("Transaction number %s validation flag is invalid: %s\n", txId, txReceipt.Header.ValidationInfo[txReceipt.TxIndex].Flag)
}

// Get proof for tx existence and verify it
txProof, err := ledger.GetTransactionProof(txReceipt.Header.BaseHeader.Number, txReceipt.TxIndex)
// To validate data tx, you need its content, maybe stored somewhere
res, err := txProof.Verify(txReceipt, tx)
if !res {
fmt.Printf("Transaction %s is not part of block %d\n", tx.Payload.TxId, txReceipt.Header.BaseHeader.Number)
}