Skip to content

Commit 87661bd

Browse files
Merge pull request #69 from oraichain/feat/implement-indexer
feat: add check utf8 when insert data to atrributes table
2 parents aefe0cd + ac70e41 commit 87661bd

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

indexer/sink/psql/utils.go

+14-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ package psql
22

33
import (
44
"database/sql"
5+
"encoding/base64"
56
"fmt"
67
"strings"
78
"unicode"
9+
"unicode/utf8"
810

911
cfg "github.com/CosmWasm/wasmd/indexer/config"
1012
abci "github.com/cometbft/cometbft/abci/types"
@@ -100,7 +102,14 @@ func insertEvents(dbtx *sql.DB, blockID, txID uint32, evts []abci.Event) error {
100102
if len(attr.Value) > 8191 {
101103
continue
102104
}
103-
attrValue := attr.Value
105+
isUtf8 := isUTF8Valid(attr.Value)
106+
var attrValue string
107+
108+
if isUtf8 {
109+
attrValue = attr.Value
110+
} else {
111+
attrValue = base64.StdEncoding.EncodeToString([]byte(attr.Value))
112+
}
104113
if hasNonPrintableChars(attr.Value) {
105114
// convert to hex to safely store the value
106115
attrValue = fmt.Sprintf("%x\n", []byte(attr.Value))
@@ -141,3 +150,7 @@ func hasNonPrintableChars(s string) bool {
141150
}
142151
return false // All characters are printable
143152
}
153+
154+
func isUTF8Valid(s string) bool {
155+
return utf8.ValidString(s)
156+
}

0 commit comments

Comments
 (0)