From 32832140b0e2ab92608daa47f6684d77ac162b4c Mon Sep 17 00:00:00 2001 From: Oluwatobi Bamidele Date: Thu, 12 Dec 2024 16:51:47 +0100 Subject: [PATCH 1/5] chore: ensure we pass inviter pubkey and inviter route hint when creating connection code --- db/structsv2.go | 5 ++++- handlers/auth.go | 26 ++++++++++++++++++++++---- handlers/auth_test.go | 7 +++++-- utils/utils.go | 13 +++++++++++-- 4 files changed, 42 insertions(+), 9 deletions(-) diff --git a/db/structsv2.go b/db/structsv2.go index 2e68ca46f..6d29aa466 100644 --- a/db/structsv2.go +++ b/db/structsv2.go @@ -80,5 +80,8 @@ type InviteReponse struct { } type InviteBody struct { - Number uint `json:"number"` + Number uint `json:"number"` + Pubkey string `json:"pubkey"` + RouteHint string `json:"route_hint"` + SatsAmount uint64 `json:"sats_amount"` } diff --git a/handlers/auth.go b/handlers/auth.go index 867143e9f..21bb3af4d 100644 --- a/handlers/auth.go +++ b/handlers/auth.go @@ -17,7 +17,7 @@ import ( type authHandler struct { db db.Database - makeConnectionCodeRequest func() string + makeConnectionCodeRequest func(inviter_pubkey string, inviter_route_hint string, msats_amount uint64) string decodeJwt func(token string) (jwt.MapClaims, error) encodeJwt func(pubkey string) (string, error) } @@ -75,8 +75,26 @@ func (ah *authHandler) CreateConnectionCode(w http.ResponseWriter, r *http.Reque return } + if codeBody.Pubkey != "" && codeBody.RouteHint == "" { + fmt.Println("route hint missing") + w.WriteHeader(http.StatusNotAcceptable) + return + } + + if codeBody.RouteHint != "" && codeBody.Pubkey == "" { + fmt.Println("pubkey missing missing") + w.WriteHeader(http.StatusNotAcceptable) + return + } + + if codeBody.SatsAmount == 0 { + codeBody.SatsAmount = 100 + } else { + codeBody.SatsAmount = utils.ConvertSatsToMsats(codeBody.SatsAmount) + } + for i := 0; i < int(codeBody.Number); i++ { - code := ah.makeConnectionCodeRequest() + code := ah.makeConnectionCodeRequest(codeBody.Pubkey, codeBody.RouteHint, codeBody.SatsAmount) if code != "" { newCode := db.ConnectionCodes{ @@ -98,12 +116,12 @@ func (ah *authHandler) CreateConnectionCode(w http.ResponseWriter, r *http.Reque json.NewEncoder(w).Encode("Codes created successfully") } -func MakeConnectionCodeRequest() string { +func MakeConnectionCodeRequest(inviter_pubkey string, inviter_route_hint string, msats_amount uint64) string { url := fmt.Sprintf("%s/invite", config.V2BotUrl) client := http.Client{} // Build v2 keysend payment data - bodyData := utils.BuildV2ConnectionCodes(100, "new_user") + bodyData := utils.BuildV2ConnectionCodes(msats_amount, "new_user", inviter_pubkey, inviter_route_hint) jsonBody := []byte(bodyData) req, _ := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(jsonBody)) diff --git a/handlers/auth_test.go b/handlers/auth_test.go index 46b73bd05..f4a720302 100644 --- a/handlers/auth_test.go +++ b/handlers/auth_test.go @@ -62,10 +62,13 @@ func TestCreateConnectionCode(t *testing.T) { rr := httptest.NewRecorder() handler := http.HandlerFunc(aHandler.CreateConnectionCode) data := db.InviteBody{ - Number: 2, + Number: 2, + Pubkey: "Test_pubkey", + RouteHint: "Test_Route_hint", + SatsAmount: 21, } - aHandler.makeConnectionCodeRequest = func() string { + aHandler.makeConnectionCodeRequest = func(inviter_pubkey string, inviter_route_hint string, msats_amount uint64) string { return "22222222222222222" } diff --git a/utils/utils.go b/utils/utils.go index 820d3dc0f..fd6d63cad 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -91,7 +91,16 @@ func BuildV2KeysendBodyData(amount uint, receiver_pubkey string, route_hint stri return bodyData } -func BuildV2ConnectionCodes(amt_msat uint, alias string) string { - bodyData := fmt.Sprintf(`{"amt_msat": %d, "alias": "%s"}`, amt_msat, alias) +func BuildV2ConnectionCodes(amt_msat uint64, alias string, pubkey string, route_hint string) string { + var bodyData string + if route_hint != "" && pubkey != "" { + bodyData = fmt.Sprintf(`{"amt_msat": %d, "alias": "%s", "inviter_pubkey":"%s", "inviter_route_hint":"%s"}`, amt_msat, alias, pubkey, route_hint) + } else { + bodyData = fmt.Sprintf(`{"amt_msat": %d, "alias": "%s"}`, amt_msat, alias) + } return bodyData } + +func ConvertSatsToMsats(sats uint64) uint64 { + return sats * 1000 +} From 2f1d8b22c980f06f1da576f65b260e8e7d6c1edf Mon Sep 17 00:00:00 2001 From: Oluwatobi Bamidele Date: Thu, 12 Dec 2024 17:16:52 +0100 Subject: [PATCH 2/5] chore: added test for util file --- utils/utils_test.go | 90 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) diff --git a/utils/utils_test.go b/utils/utils_test.go index 33aca6f41..e384ddb1e 100644 --- a/utils/utils_test.go +++ b/utils/utils_test.go @@ -410,3 +410,93 @@ func TestBuildSearchQuery(t *testing.T) { }) } } + +func TestBuildV2ConnectionCodes(t *testing.T) { + tests := []struct { + name string + amtMsat uint64 + alias string + pubkey string + routeHint string + expected string + }{ + { + name: "Standard Input with Route Hint", + amtMsat: 100000, + alias: "new_user", + pubkey: "abcdef123456", + routeHint: "hint123", + expected: `{"amt_msat": 100000, "alias": "new_user", "inviter_pubkey":"abcdef123456", "inviter_route_hint":"hint123"}`, + }, + { + name: "Standard Input without Route Hint", + amtMsat: 100000, + alias: "new_user", + pubkey: "abcdef123456", + routeHint: "", + expected: `{"amt_msat": 100000, "alias": "new_user"}`, + }, + { + name: "Empty Pubkey and Route Hint", + amtMsat: 100000, + alias: "new_user", + pubkey: "", + routeHint: "", + expected: `{"amt_msat": 100000, "alias": "new_user"}`, + }, + { + name: "Long Strings", + amtMsat: 100000, + alias: strings.Repeat("a", 1000), + pubkey: strings.Repeat("b", 1000), + routeHint: strings.Repeat("c", 1000), + expected: fmt.Sprintf(`{"amt_msat": 100000, "alias": "%s", "inviter_pubkey":"%s", "inviter_route_hint":"%s"}`, strings.Repeat("a", 1000), strings.Repeat("b", 1000), strings.Repeat("c", 1000)), + }, + { + name: "Special Characters in Strings", + amtMsat: 100000, + alias: "user!@#", + pubkey: "abc!@#123", + routeHint: "hint$%^", + expected: `{"amt_msat": 100000, "alias": "user!@#", "inviter_pubkey":"abc!@#123", "inviter_route_hint":"hint$%^"}`, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + result := BuildV2ConnectionCodes(tt.amtMsat, tt.alias, tt.pubkey, tt.routeHint) + assert.JSONEq(t, tt.expected, result) + }) + } +} + +func TestConvertSatsToMsats(t *testing.T) { + tests := []struct { + name string + sats uint64 + expected uint64 + }{ + { + name: "Zero Satoshis", + sats: 0, + expected: 0, + }, + { + name: "One Satoshi", + sats: 1, + expected: 1000, + }, + { + name: "Small Amount", + sats: 123, + expected: 123000, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + result := ConvertSatsToMsats(tt.sats) + assert.Equal(t, tt.expected, result) + }) + } +} From 9f4a6f61ebc89e94d0b1a09bb7b6f6b0a17717a6 Mon Sep 17 00:00:00 2001 From: Oluwatobi Bamidele Date: Thu, 12 Dec 2024 18:07:01 +0100 Subject: [PATCH 3/5] test: use a different sphinx bot version for test --- .github/workflows/frontend-e2e.yml | 4 +++- .github/workflows/prjob_tests.yml | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/.github/workflows/frontend-e2e.yml b/.github/workflows/frontend-e2e.yml index a1cbd258a..55ff1bb40 100644 --- a/.github/workflows/frontend-e2e.yml +++ b/.github/workflows/frontend-e2e.yml @@ -23,9 +23,11 @@ jobs: - name: Build Tribes Image run: pwd && ls && docker build -t sphinxlightning/sphinx-tribes:latest . + +# change_v2_ports - name: Clone Stack run: | - git clone --single-branch --branch change_v2_ports https://github.com/stakwork/sphinx-stack.git stack + git clone --single-branch --branch change_sphinx_bot_version https://github.com/stakwork/sphinx-stack.git stack - name: Clone Sphinx Tribes Frontend run: | diff --git a/.github/workflows/prjob_tests.yml b/.github/workflows/prjob_tests.yml index 37e30b96e..25295acc4 100644 --- a/.github/workflows/prjob_tests.yml +++ b/.github/workflows/prjob_tests.yml @@ -15,9 +15,11 @@ jobs: - uses: actions/checkout@v2 + # change_sphinx_bot_version + - name: Clone Stack run: | - git clone --single-branch --branch change_v2_ports https://github.com/stakwork/sphinx-stack.git stackv2; + git clone --single-branch --branch change_sphinx_bot_version https://github.com/stakwork/sphinx-stack.git stackv2; - name: Run Stack V2 uses: nick-fields/retry@v2 From cdc967b0a74ada6c435216e03352baeced1558c5 Mon Sep 17 00:00:00 2001 From: Oluwatobi Bamidele Date: Thu, 12 Dec 2024 19:08:48 +0100 Subject: [PATCH 4/5] chore: log miver --- .github/workflows/prjob_tests.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/prjob_tests.yml b/.github/workflows/prjob_tests.yml index 25295acc4..66d4327a2 100644 --- a/.github/workflows/prjob_tests.yml +++ b/.github/workflows/prjob_tests.yml @@ -33,6 +33,7 @@ jobs: docker logs alice.sphinx docker logs bob.sphinx docker wait stackv2-v2setup-1 + docker logs mixer.sphinx - name: Starting DB run: docker compose -f ./docker/testdb-docker-compose.yml -p test_db up -d From 71705fdadac777cf54732aa58bbdd481f8143a0f Mon Sep 17 00:00:00 2001 From: Oluwatobi Bamidele Date: Thu, 12 Dec 2024 19:36:26 +0100 Subject: [PATCH 5/5] fix: use normal stack branch --- .github/workflows/frontend-e2e.yml | 2 +- .github/workflows/prjob_tests.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/frontend-e2e.yml b/.github/workflows/frontend-e2e.yml index 55ff1bb40..0400af185 100644 --- a/.github/workflows/frontend-e2e.yml +++ b/.github/workflows/frontend-e2e.yml @@ -27,7 +27,7 @@ jobs: # change_v2_ports - name: Clone Stack run: | - git clone --single-branch --branch change_sphinx_bot_version https://github.com/stakwork/sphinx-stack.git stack + git clone --single-branch --branch change_v2_ports https://github.com/stakwork/sphinx-stack.git stack - name: Clone Sphinx Tribes Frontend run: | diff --git a/.github/workflows/prjob_tests.yml b/.github/workflows/prjob_tests.yml index 66d4327a2..f054b78d6 100644 --- a/.github/workflows/prjob_tests.yml +++ b/.github/workflows/prjob_tests.yml @@ -19,7 +19,7 @@ jobs: - name: Clone Stack run: | - git clone --single-branch --branch change_sphinx_bot_version https://github.com/stakwork/sphinx-stack.git stackv2; + git clone --single-branch --branch change_v2_ports https://github.com/stakwork/sphinx-stack.git stackv2; - name: Run Stack V2 uses: nick-fields/retry@v2