Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(Delegator) Add endpoint to retrieve eoa addr #1497

Merged
merged 1 commit into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading