Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Enhance readUtxos to support txHashStr in both hex and base64 f… #52

Merged
merged 1 commit into from
Nov 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 31 additions & 6 deletions examples/query/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"encoding/base64"
"encoding/hex"
"fmt"
"log"
Expand Down Expand Up @@ -59,25 +60,46 @@ func readParams(ctx context.Context, client *utxorpc.UtxorpcClient) {
}

func readUtxos(ctx context.Context, client *utxorpc.UtxorpcClient, txHashStr string, txIndex uint32) {
txHash, err := hex.DecodeString(txHashStr)
if err != nil {
log.Fatalf("failed to decode hex string: %v", err)
var txHashBytes []byte
var err error

// Attempt to decode the input as hex
txHashBytes, err = hex.DecodeString(txHashStr)
if err == nil {
log.Printf("Input txHashStr decoded from hex.")
} else {
// If not hex, attempt to decode as Base64
txHashBytes, err = base64.StdEncoding.DecodeString(txHashStr)
if err == nil {
log.Printf("Input txHashStr decoded from Base64.")
} else {
log.Printf("Input txHashStr is neither valid hex nor Base64.")
fmt.Println("Error: txHashStr must be a valid hexadecimal or Base64 string.")
return
}
}

// Create TxoRef with the decoded hash bytes
txoRef := &query.TxoRef{
Hash: txHash,
Hash: txHashBytes, // Use the decoded []byte
Index: txIndex,
}

// Prepare the request
req := connect.NewRequest(&query.ReadUtxosRequest{
Keys: []*query.TxoRef{txoRef},
})
client.AddHeadersToRequest(req)
fmt.Println("connecting to utxorpc host:", client.URL())
fmt.Println("Connecting to utxorpc host:", client.URL())

// Send the request
resp, err := client.Query.ReadUtxos(ctx, req)
if err != nil {
utxorpc.HandleError(err)
return
}

// Process the response
fmt.Printf("Response: %+v\n", resp)

if resp.Msg.LedgerTip != nil {
Expand All @@ -91,8 +113,11 @@ func readUtxos(ctx context.Context, client *utxorpc.UtxorpcClient, txHashStr str
fmt.Printf(" Native Bytes: %x\n", item.NativeBytes)
if cardano := item.GetCardano(); cardano != nil {
fmt.Println(" Cardano UTxO:")
fmt.Printf(" Address: %s\n", cardano.Address)
fmt.Printf(" Address: %x\n", cardano.Address)
fmt.Printf(" Coin: %d\n", cardano.Coin)
if cardano.Datum != nil {
fmt.Printf(" Datum Hash: %x\n", cardano.Datum.Hash)
}
}
}
}
Expand Down