All checks were successful
CI / lint-and-test (push) Successful in 9m52s
Co-authored-by: Cursor <cursoragent@cursor.com>
21 lines
522 B
TypeScript
21 lines
522 B
TypeScript
import { hexToString, stringToHex } from "viem";
|
|
|
|
export function encodeMemoData(memo: string): `0x${string}` {
|
|
const trimmed = memo.trim();
|
|
if (!trimmed) return "0x";
|
|
return stringToHex(trimmed);
|
|
}
|
|
|
|
export function decodeMemoData(data: string | null | undefined): string | null {
|
|
if (!data || data === "0x") return null;
|
|
try {
|
|
const text = hexToString(data as `0x${string}`);
|
|
if (text && /^[\x20-\x7E\s]+$/.test(text)) {
|
|
return text.trim();
|
|
}
|
|
} catch {
|
|
return null;
|
|
}
|
|
return null;
|
|
}
|