-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: jsonrpc client for function service
- Loading branch information
Showing
7 changed files
with
82 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// package jsonrpc implements a JSON-RPC client for the Kwil function service. | ||
|
||
package jsonrpc | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/url" | ||
|
||
"github.com/kwilteam/kwil-db/core/crypto/auth" | ||
rpcclient "github.com/kwilteam/kwil-db/core/rpc/client" | ||
"github.com/kwilteam/kwil-db/core/rpc/client/function" | ||
jsonFunction "github.com/kwilteam/kwil-db/core/rpc/json/function" | ||
) | ||
|
||
// Client is a JSON-RPC client for the Kwil function service. It uses the | ||
// JSONRPCClient from the rpcclient package for the actual JSON-RPC communication, | ||
// and implements function service methods. | ||
type Client struct { | ||
*rpcclient.JSONRPCClient | ||
} | ||
|
||
// NewClient creates a new json rpc client, target should be the base URL | ||
// of the provider server, and should not include "/rpc/v1" as that is appended | ||
// automatically. | ||
// NOTE: No error will be returned. | ||
func NewClient(target *url.URL, opts ...rpcclient.RPCClientOpts) *Client { | ||
g := &Client{ | ||
JSONRPCClient: rpcclient.NewJSONRPCClient(target, opts...), | ||
} | ||
|
||
return g | ||
} | ||
|
||
var _ function.FunctionServiceClient = (*Client)(nil) | ||
|
||
func (c *Client) VerifySignature(ctx context.Context, sender []byte, signature *auth.Signature, message []byte) error { | ||
result := &jsonFunction.VerifySignatureResponse{} | ||
err := c.CallMethod(ctx, string(jsonFunction.MethodVerifySig), &jsonFunction.VerifySignatureRequest{ | ||
Sender: sender, | ||
Msg: message, | ||
Signature: &jsonFunction.TxSignature{ | ||
SignatureBytes: signature.Signature, | ||
SignatureType: signature.Type, | ||
}, | ||
}, result) | ||
|
||
if err != nil { // protocol/communication level error | ||
return err | ||
} | ||
|
||
if result.Reason != "" { | ||
return fmt.Errorf("%w: %s", function.ErrInvalidSignature, result.Reason) | ||
} | ||
|
||
// NOTE: Forget why I put both `valid` and `error` in the response. | ||
// if `valid` is false, `error` should not be empty. | ||
// This might be not needed, but I just keep it here. | ||
if !result.Valid { | ||
return function.ErrInvalidSignature | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters