Skip to content

Commit 5730554

Browse files
authored
feat: view attested result in readable format (#162)
* feat: view attested result in readable human language * chore: apply suggestion * chore: apply suggestion
1 parent b52cab3 commit 5730554

File tree

8 files changed

+1798
-46
lines changed

8 files changed

+1798
-46
lines changed

README.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,63 @@ entries, _ := txActions.ListTransactionFees(ctx, types.ListTransactionFeesInput{
594594

595595
**📖 For complete documentation including parameters, return types, pagination, filtering modes, and real-world examples, see the [Transaction Actions Interface](./docs/api-reference.md#transaction-actions-interface) in the API Reference.**
596596

597+
## Attestation Payload Parsing
598+
599+
Request cryptographically signed attestations of query results from network validators. These signed attestations can be verified on-chain for trustless data verification.
600+
601+
```go
602+
import (
603+
"crypto/sha256"
604+
"github.com/trufnetwork/kwil-db/core/crypto"
605+
"github.com/trufnetwork/sdk-go/core/contractsapi"
606+
)
607+
608+
// Request a signed attestation
609+
attestationActions, _ := tnClient.LoadAttestationActions()
610+
result, _ := attestationActions.RequestAttestation(ctx, types.RequestAttestationInput{
611+
DataProvider: "0x4710a8d8f0d845da110086812a32de6d90d7ff5c",
612+
StreamID: "stai0000000000000000000000000000",
613+
ActionName: "get_record",
614+
Args: args,
615+
MaxFee: "100000000000000000000", // 100 TRUF
616+
})
617+
618+
// Wait and retrieve signed attestation
619+
signed, _ := attestationActions.GetSignedAttestation(ctx, types.GetSignedAttestationInput{
620+
RequestTxID: result.RequestTxID,
621+
})
622+
623+
// Validate payload length before slicing
624+
if len(signed.Payload) < 66 {
625+
log.Fatalf("Payload too short: %d bytes, expected at least 66", len(signed.Payload))
626+
}
627+
628+
// Extract canonical payload and signature (last 65 bytes)
629+
signatureOffset := len(signed.Payload) - 65
630+
canonicalPayload := signed.Payload[:signatureOffset]
631+
signature := signed.Payload[signatureOffset:]
632+
633+
// Verify signature and recover validator address
634+
hash := sha256.Sum256(canonicalPayload)
635+
adjustedSig := make([]byte, 65)
636+
copy(adjustedSig, signature)
637+
if signature[64] >= 27 {
638+
adjustedSig[64] = signature[64] - 27
639+
}
640+
pubKey, _ := crypto.RecoverSecp256k1KeyFromSigHash(hash[:], adjustedSig)
641+
validatorAddr := crypto.EthereumAddressFromPubKey(pubKey)
642+
643+
// Parse attestation payload
644+
parsed, _ := contractsapi.ParseAttestationPayload(canonicalPayload)
645+
fmt.Printf("Validator: 0x%x\n", validatorAddr)
646+
fmt.Printf("Block Height: %d\n", parsed.BlockHeight)
647+
for i, row := range parsed.Result {
648+
fmt.Printf("Row %d: Timestamp=%v, Value=%v\n", i+1, row.Values[0], row.Values[1])
649+
}
650+
```
651+
652+
**📖 For complete documentation including signature verification, payload structure, result decoding, EVM integration examples, and security best practices, see the [Attestation Actions Interface](./docs/api-reference.md#attestation-actions-interface) in the API Reference.**
653+
597654
## Quick Reference
598655

599656
> 🎯 **Full Working Example**: See [`examples/transaction-lifecycle-example/main.go`](./examples/transaction-lifecycle-example/main.go) for complete, runnable code demonstrating all these patterns with proper error handling.
@@ -611,6 +668,9 @@ entries, _ := txActions.ListTransactionFees(ctx, types.ListTransactionFeesInput{
611668
| Destroy stream | `tnClient.DestroyStream(ctx, streamId)` |
612669
| Get transaction event | `txActions.GetTransactionEvent(ctx, input)` |
613670
| List transaction fees | `txActions.ListTransactionFees(ctx, input)` |
671+
| Request attestation | `attestationActions.RequestAttestation(ctx, input)` |
672+
| Get signed attestation | `attestationActions.GetSignedAttestation(ctx, input)` |
673+
| Parse attestation payload | `contractsapi.ParseAttestationPayload(payload)` |
614674

615675
### Safe Operation Patterns
616676

0 commit comments

Comments
 (0)