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

[AppGate] Implement the MVP AppGateServer #108

Merged
merged 42 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
cec3ef4
feat: implement app client
red-0ne Oct 27, 2023
d312423
Merge remote-tracking branch 'origin/main' into feat/app-client
red-0ne Nov 6, 2023
c4fe2da
chore: address review comments
red-0ne Nov 7, 2023
a6edf61
Merge remote-tracking branch 'origin/main' into feat/app-client
red-0ne Nov 7, 2023
aab9e17
fix: remove signature field before signing
red-0ne Nov 7, 2023
0f2a53f
chore: go.mod
h5law Nov 7, 2023
65c524f
Merge branch 'main' into feat/app-client
h5law Nov 7, 2023
6e265c2
feat: add ring signatures
h5law Nov 7, 2023
a966b66
Merge branch 'main' into feat/app-client
h5law Nov 7, 2023
80af5fb
Merge branch 'main' into feat/app-client
h5law Nov 9, 2023
c1f6115
chore: remove mock files
h5law Nov 9, 2023
c681484
chore: fix spelling errors
h5law Nov 9, 2023
c787d80
fixup: spelling mistake
h5law Nov 9, 2023
65e9bee
feat: add command to start the appgateserver
h5law Nov 9, 2023
c22dc9c
chore: add debug lines
h5law Nov 9, 2023
c14a8b6
Merge branch 'main' into feat/app-client
h5law Nov 9, 2023
ec57a8e
chore: debugging
h5law Nov 9, 2023
ba3bd8f
Merge branch 'main' into feat/app-client
h5law Nov 9, 2023
781ee13
chore: go.mod
h5law Nov 9, 2023
078be29
chore: close websocket connections
h5law Nov 9, 2023
00211a9
Merge branch 'main' into feat/app-client
h5law Nov 9, 2023
12d97d6
chore: add ws todo
h5law Nov 9, 2023
2591e19
Merge remote-tracking branch 'origin/main' into feat/app-client
red-0ne Nov 10, 2023
3f2cb90
chore: Use depinject for AppGateServer
red-0ne Nov 10, 2023
a857e5f
fix: Get appAddress from url query when appAddress is empty
red-0ne Nov 10, 2023
bc2efb3
feat: address comments
h5law Nov 10, 2023
76a31c0
chore: fix signing key field
h5law Nov 10, 2023
09c843c
chore: defer cancelling ctx
h5law Nov 10, 2023
151313e
chore: cleanup log lines
h5law Nov 10, 2023
2cd04e7
chore: address comments
h5law Nov 10, 2023
5daed4a
chore: address comments
h5law Nov 10, 2023
d8c668a
Merge branch 'main' into feat/app-client
h5law Nov 10, 2023
118d26d
chore: update comments and naming
h5law Nov 10, 2023
ce91371
chore: fix missing if
h5law Nov 10, 2023
969bf0f
chore: add signed relay received debug log
h5law Nov 10, 2023
211df7f
chore: cleanup comments
h5law Nov 10, 2023
89786c3
chore: comments comments comments
h5law Nov 10, 2023
b6f7ef5
chore: comments comments comments
h5law Nov 10, 2023
1d9c233
chore: update ring comments
h5law Nov 10, 2023
89688ab
feat: refactor appgateserver creation with depinject supplier functio…
h5law Nov 10, 2023
683b285
chore: re-add missing signing information check
h5law Nov 10, 2023
35936ac
Merge branch 'main' into feat/app-client
h5law Nov 10, 2023
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
11 changes: 0 additions & 11 deletions pkg/appgateserver/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,6 @@ func runAppGateServer(cmd *cobra.Command, _ []string) error {
return fmt.Errorf("failed to parse listening endpoint: %w", err)
}

// Obtain the comet websocket endpoint from the client context.
cometWSUrl, err := url.Parse(clientCtx.NodeURI + "/websocket")
if err != nil {
return fmt.Errorf("failed to parse block query URL: %w", err)
}
cometWSUrl.Scheme = "ws"
// If the comet websocket URL is not provided, use the one from the client context.
if flagCometWebsocketUrl == "" {
flagCometWebsocketUrl = cometWSUrl.String()
}

log.Printf("INFO: Creating block client, using comet websocket URL: %s...", flagCometWebsocketUrl)

// Create the block client with its dependency on the events client.
Expand Down
3 changes: 2 additions & 1 deletion pkg/appgateserver/jsonrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (app *appGateServer) handleJSONRPCRelay(
}

// Perform the HTTP request to the relayer.
log.Printf("DEBUG: Sending relay request to %s", supplierUrl)
log.Printf("DEBUG: Sending signed relay request to %s", supplierUrl)
relayHTTPResponse, err := http.DefaultClient.Do(relayHTTPRequest)
if err != nil {
return err
Expand All @@ -112,6 +112,7 @@ func (app *appGateServer) handleJSONRPCRelay(
// as in some relayer early failures, it may not be signed by the supplier.
// TODO_IMPROVE: Add more logging & telemetry so we can get visibility and signal into
// failed responses.
log.Println("DEBUG: Verifying signed relay response from...")
if err := app.verifyResponse(ctx, supplierAddress, relayResponse); err != nil {
h5law marked this conversation as resolved.
Show resolved Hide resolved
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/appgateserver/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"net/url"
)

// WithSigningInformation sets the signing key and app address for server.
// WithSigningInformation sets the signing key and app address for the server.
func WithSigningInformation(signingInfo *SigningInformation) appGateServerOption {
return func(appGateServer *appGateServer) {
appGateServer.signingInformation = signingInfo
Expand Down
10 changes: 5 additions & 5 deletions pkg/appgateserver/relay_verifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ func (app *appGateServer) verifyResponse(
relayResponse *types.RelayResponse,
) error {
// Get the supplier's public key.
pubKey, err := app.getSupplierPubKeyFromAddress(ctx, supplierAddress)
supplierPubKey, err := app.getSupplierPubKeyFromAddress(ctx, supplierAddress)
if err != nil {
return err
}

// Extract the supplier's signature
signature := relayResponse.Meta.SupplierSignature
supplierSignature := relayResponse.Meta.SupplierSignature

// Get the relay response signable bytes and hash them.
responseBz, err := relayResponse.GetSignableBytes()
Expand All @@ -33,7 +33,7 @@ func (app *appGateServer) verifyResponse(
hash := crypto.Sha256(responseBz)

// Verify the relay response signature.
if !pubKey.VerifySignature(hash, signature) {
if !supplierPubKey.VerifySignature(hash, supplierSignature) {
return ErrAppGateInvalidRelayResponseSignature
h5law marked this conversation as resolved.
Show resolved Hide resolved
}

Expand All @@ -46,9 +46,9 @@ func (app *appGateServer) getSupplierPubKeyFromAddress(
ctx context.Context,
supplierAddress string,
) (cryptotypes.PubKey, error) {
pubKey, ok := app.supplierAccountCache[supplierAddress]
supplierPubKey, ok := app.supplierAccountCache[supplierAddress]
if ok {
return pubKey, nil
return supplierPubKey, nil
}

// Query for the supplier account to get the application's public key
Expand Down
26 changes: 17 additions & 9 deletions pkg/appgateserver/rings.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (app *appGateServer) getRingSingerForAppAddress(ctx context.Context, appAdd
ring, err = newRingFromPoints(points)
}
if err != nil {
log.Printf("ERROR: Unable to get ring for address: %s [%w]", appAddress, err)
log.Printf("ERROR: Unable to get ring for address: %s [%v]", appAddress, err)
return nil, err
}

Expand All @@ -65,8 +65,8 @@ func newRingFromPoints(points []ringtypes.Point) (*ring.Ring, error) {
return ring.NewFixedKeyRingFromPublicKeys(ring_secp256k1.NewCurve(), points)
}

// getDelegatedPubKeysForAddress returns the ring used to sign a message for the given application
// address, by querying the portal module for it's delegated pubkeys
// getDelegatedPubKeysForAddress returns the ring used to sign a message for the given
// application address, by querying the application module for it's delegated pubkeys
func (app *appGateServer) getDelegatedPubKeysForAddress(
ctx context.Context,
appAddress string,
Expand All @@ -84,9 +84,17 @@ func (app *appGateServer) getDelegatedPubKeysForAddress(
// create a slice of addresses for the ring
ringAddresses := make([]string, 0)
ringAddresses = append(ringAddresses, appAddress) // app address is index 0
ringAddresses = append(ringAddresses, appAddress) // add app address twice to make the ring size of mininmum 2
if len(res.Application.DelegateeGatewayAddresses) > 0 {
ringAddresses = append(ringAddresses, res.Application.DelegateeGatewayAddresses...) // delegatee addresses are index 1+
if len(res.Application.DelegateeGatewayAddresses) < 1 {
// add app address twice to make the ring size of mininmum 2
// TODO_TECHDEBT: We are adding the appAddress twice because a ring
h5law marked this conversation as resolved.
Show resolved Hide resolved
// signature requires AT LEAST two pubKeys. When the Application has
// not delegated to any gateways, we add the application's own address
// twice. This is a HACK and should be investigated as to what is the
// best approach to take in this situation.
ringAddresses = append(ringAddresses, appAddress)
} else if len(res.Application.DelegateeGatewayAddresses) > 0 {
// add the delegatee gateway addresses
ringAddresses = append(ringAddresses, res.Application.DelegateeGatewayAddresses...)
}

// get the points on the secp256k1 curve for the addresses
Expand All @@ -102,9 +110,9 @@ func (app *appGateServer) getDelegatedPubKeysForAddress(
return points, nil
}

// addressesToPoints converts a slice of addresses to a slice of points on the secp256k1 curve
// it does so by querying the account module for the public key for each address and converting
// them to the corresponding points on the secp256k1 curve
// addressesToPoints converts a slice of addresses to a slice of points on the
// secp256k1 curve, by querying the account module for the public key for each
// address and converting them to the corresponding points on the secp256k1 curve
func (app *appGateServer) addressesToPoints(ctx context.Context, addresses []string) ([]ringtypes.Point, error) {
curve := ring_secp256k1.NewCurve()
points := make([]ringtypes.Point, len(addresses))
Expand Down
14 changes: 11 additions & 3 deletions pkg/relayer/proxy/rings.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,17 @@ func (rp *relayerProxy) getDelegatedPubKeysForAddress(
// create a slice of addresses for the ring
ringAddresses := make([]string, 0)
ringAddresses = append(ringAddresses, appAddress) // app address is index 0
ringAddresses = append(ringAddresses, appAddress) // add app address twice to make the ring size of mininmum 2
if len(res.Application.DelegateeGatewayAddresses) > 0 {
ringAddresses = append(ringAddresses, res.Application.DelegateeGatewayAddresses...) // delegatee addresses are index 1+
if len(res.Application.DelegateeGatewayAddresses) < 1 {
// add app address twice to make the ring size of mininmum 2
// TODO_TECHDEBT: We are adding the appAddress twice because a ring
// signature requires AT LEAST two pubKeys. When the Application has
// not delegated to any gateways, we add the application's own address
// twice. This is a HACK and should be investigated as to what is the
// best approach to take in this situation.
ringAddresses = append(ringAddresses, appAddress)
} else if len(res.Application.DelegateeGatewayAddresses) > 0 {
// add the delegatee gateway addresses
ringAddresses = append(ringAddresses, res.Application.DelegateeGatewayAddresses...)
}

// get the points on the secp256k1 curve for the addresses
Expand Down