diff --git a/utils/utils.go b/utils/utils.go index 72ce72165..541367879 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -4,6 +4,7 @@ import ( "fmt" "net/http" "strconv" + "strings" ) func GetPaginationParams(r *http.Request) (int, int, string, string, string) { @@ -65,7 +66,14 @@ func BuildKeysendBodyData(amount uint, receiver_pubkey string, route_hint string } func BuildV2KeysendBodyData(amount uint, receiver_pubkey string, route_hint string, memo string) string { + // convert amount to msat amountMsat := amount * 1000 + + // trim the memo + memo = strings.TrimSpace(memo) + // trim the route hint + route_hint = strings.TrimSpace(route_hint) + var bodyData string if route_hint != "" { bodyData = fmt.Sprintf(`{"amt_msat": %d, "dest": "%s", "route_hint": "%s", "data": "%s", "wait": true}`, amountMsat, receiver_pubkey, route_hint, memo) diff --git a/utils/utils_test.go b/utils/utils_test.go new file mode 100644 index 000000000..7f5cb74dd --- /dev/null +++ b/utils/utils_test.go @@ -0,0 +1,101 @@ +package utils + +import ( + "fmt" + "strings" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestBuildV2KeysendBodyData(t *testing.T) { + tests := []struct { + name string + amount uint + receiverPubkey string + routeHint string + memo string + expected string + expectError bool + }{ + { + name: "Standard Input with Route Hint", + amount: 100, + receiverPubkey: "abcdef123456", + routeHint: "hint123", + memo: "Test transaction", + expected: `{"amt_msat": 100000, "dest": "abcdef123456", "route_hint": "hint123", "data": "Test transaction", "wait": true}`, + }, + { + name: "Standard Input without Route Hint", + amount: 100, + receiverPubkey: "abcdef123456", + routeHint: "", + memo: "Test transaction", + expected: `{"amt_msat": 100000, "dest": "abcdef123456", "route_hint": "", "data": "Test transaction", "wait": true}`, + }, + { + name: "Minimum Amount", + amount: 0, + receiverPubkey: "abcdef123456", + routeHint: "hint123", + memo: "Test transaction", + expected: `{"amt_msat": 0, "dest": "abcdef123456", "route_hint": "hint123", "data": "Test transaction", "wait": true}`, + }, + { + name: "Empty Strings for All String Parameters", + amount: 100, + receiverPubkey: "", + routeHint: "", + memo: "", + expected: `{"amt_msat": 100000, "dest": "", "route_hint": "", "data": "", "wait": true}`, + }, + { + name: "Large Amount", + amount: 4294967295, + receiverPubkey: "abcdef123456", + routeHint: "hint123", + memo: "Test transaction", + expected: `{"amt_msat": 4294967295000, "dest": "abcdef123456", "route_hint": "hint123", "data": "Test transaction", "wait": true}`, + }, + { + name: "Long Strings", + amount: 100, + receiverPubkey: strings.Repeat("a", 1000), + routeHint: strings.Repeat("b", 1000), + memo: strings.Repeat("c", 1000), + expected: fmt.Sprintf(`{"amt_msat": 100000, "dest": "%s", "route_hint": "%s", "data": "%s", "wait": true}`, strings.Repeat("a", 1000), strings.Repeat("b", 1000), strings.Repeat("c", 1000)), + }, + { + name: "Special Characters in Strings", + amount: 100, + receiverPubkey: "abc!@#123", + routeHint: "hint$%^", + memo: "Test &*() transaction", + expected: `{"amt_msat": 100000, "dest": "abc!@#123", "route_hint": "hint$%^", "data": "Test &*() transaction", "wait": true}`, + }, + { + name: "Whitespace in Strings", + amount: 100, + receiverPubkey: "abcdef123456", + routeHint: "hint123 ", + memo: " Test transaction ", + expected: `{"amt_msat": 100000, "dest": "abcdef123456", "route_hint": "hint123", "data": "Test transaction", "wait": true}`, + }, + { + name: "Non-ASCII Characters in Strings", + amount: 100, + receiverPubkey: "abcñ123", + routeHint: "hintü123", + memo: "Test transaction with emoji 😊", + expected: `{"amt_msat": 100000, "dest": "abcñ123", "route_hint": "hintü123", "data": "Test transaction with emoji 😊", "wait": true}`, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + result := BuildV2KeysendBodyData(tt.amount, tt.receiverPubkey, tt.routeHint, tt.memo) + assert.JSONEq(t, tt.expected, result) + }) + } +}