Skip to content

Commit

Permalink
feat: add endpoint to get payer address
Browse files Browse the repository at this point in the history
test: fix test err

test: fix test err
  • Loading branch information
nick-bisonai committed May 18, 2024
1 parent 0ce7ace commit 624e022
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
29 changes: 29 additions & 0 deletions delegator/sign/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,40 @@ func initialize(c *fiber.Ctx) error {
return c.SendString("Initialized")
}

if strings.HasPrefix(pk, "0x") {
pk = strings.TrimPrefix(pk, "0x")
}

utils.UpdateFeePayer(pk)

return c.SendString("Initialized")
}

func getFeePayerAddress(c *fiber.Ctx) error {
pk, err := utils.GetFeePayer(c)
if err != nil {
return err
}

privateKey, err := crypto.HexToECDSA(pk)
if err != nil {
return err
}

publicKey := privateKey.Public()
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
if !ok {
return errors.New("error casting public key to ECDSA")
}

result := crypto.PubkeyToAddress(*publicKeyECDSA).Hex()
if !strings.HasPrefix(result, "0x") {
result = "0x" + result
}

return c.JSON(result)
}

func insert(c *fiber.Ctx) error {
payload := new(SignInsertPayload)
if err := c.BodyParser(payload); err != nil {
Expand Down
1 change: 1 addition & 0 deletions delegator/sign/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ func Routes(router fiber.Router) {
sign.Post("/volatile", onlySign)

sign.Get("/initialize", initialize)
sign.Get("/feePayer", getFeePayerAddress)
sign.Get("", get)
sign.Get("/:id", getById)
}
26 changes: 24 additions & 2 deletions delegator/tests/sign_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,36 @@ func TestInitialize(t *testing.T) {

readResult, err := utils.GetRequest[utils.FeePayer](appConfig.App, "/readpk", nil)
assert.Nil(t, err)
assert.Equal(t, readResult.PrivateKey, "0x12345")
assert.Equal(t, readResult.PrivateKey, "12345")

err = utils.RawReq(appConfig.App, "GET", "/api/v1/sign/initialize", nil)
assert.Nil(t, err)

readResultRefreshed, err := utils.GetRequest[utils.FeePayer](appConfig.App, "/readpk", nil)
assert.Nil(t, err)
assert.NotEqual(t, readResultRefreshed.PrivateKey, "0x12345")
assert.NotEqual(t, readResultRefreshed.PrivateKey, "12345")
}

func TestGetFeePayerAddress(t *testing.T) {
err := setup()
assert.Nil(t, err)
defer t.Cleanup(cleanup)
defer appConfig.App.Shutdown()

appConfig.App.Get("/readpk", func(c *fiber.Ctx) error {
fp, error := utils.GetFeePayer(c)
if error != nil {
return error
}
return c.JSON(utils.FeePayer{PrivateKey: fp})
})

err = utils.RawReq(appConfig.App, "GET", "/api/v1/sign/initialize?feePayerPrivateKey=0x6014d3aa8be8980fd90146d10176e7ef632bdba96279e8bbe55421e79a979a2e", nil)
assert.Nil(t, err)

readResult, err := utils.GetRequest[string](appConfig.App, "/api/v1/sign/feePayer", nil)
assert.Nil(t, err)
assert.Equal(t, readResult, "0xda2E0E7089a479ef4A75a8c6Cc78426B9270EC08")
}

func TestInsert(t *testing.T) {
Expand Down

0 comments on commit 624e022

Please sign in to comment.