Skip to content

Commit

Permalink
Merge pull request #2167 from stakwork/update/invite_connection
Browse files Browse the repository at this point in the history
chore: ensure we pass inviter pubkey and inviter route hint when crea…
  • Loading branch information
humansinstitute authored Dec 13, 2024
2 parents 87be43e + 71705fd commit 13f580b
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 9 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/frontend-e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ 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
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/prjob_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ 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;
Expand All @@ -31,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
Expand Down
5 changes: 4 additions & 1 deletion db/structsv2.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
26 changes: 22 additions & 4 deletions handlers/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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{
Expand All @@ -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))
Expand Down
7 changes: 5 additions & 2 deletions handlers/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

Expand Down
13 changes: 11 additions & 2 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
90 changes: 90 additions & 0 deletions utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
}

0 comments on commit 13f580b

Please sign in to comment.