From 1adedce6f2a8fc5c188e310b0b718b4d9f7b7754 Mon Sep 17 00:00:00 2001 From: jchappelow Date: Sun, 26 Jan 2025 21:57:23 -0600 Subject: [PATCH] dbid => namespace, remove nep413 authr (#1292) * dbid => namespace, remove nep413 authr * taskfile * missing golang.org/x/term require * kwil-cli --- .golangci.yml | 1 + Taskfile.yml | 8 +- app/shared/display/message_test.go | 4 +- cmd/kwil-cli/cmds/database/batch.go | 24 ++- cmd/kwil-cli/cmds/database/call.go | 36 +++-- cmd/kwil-cli/cmds/database/cmd.go | 4 +- cmd/kwil-cli/cmds/database/execute.go | 8 +- cmd/kwil-cli/cmds/database/message.go | 2 +- cmd/kwil-cli/cmds/database/message_test.go | 55 +++---- cmd/kwil-cli/cmds/database/query.go | 8 +- cmd/kwil-cli/cmds/root.go | 8 +- cmd/kwil-cli/cmds/utils/dbid.go | 73 --------- cmd/kwil-cli/cmds/utils/utils.go | 1 - contrib/scripts/build/.go_variables | 2 +- core/client/client.go | 10 +- core/gatewayclient/client.go | 6 +- core/rpc/json/user/commands.go | 2 +- core/types/message.go | 8 +- core/types/message_test.go | 8 +- core/types/payloads.go | 28 ++-- core/types/payloads_test.go | 36 ++--- core/types/types.go | 6 +- core/utils/dbid.go | 17 --- core/utils/utils_test.go | 21 --- extensions/auth/nep413.go | 170 --------------------- extensions/auth/nep413_test.go | 109 ------------- go.mod | 2 +- go.sum | 4 +- node/engine/functions.go | 21 --- node/engine/planner/logical/nodes.go | 2 +- node/services/jsonrpc/usersvc/service.go | 7 +- node/txapp/routes.go | 2 +- test/_old/driver/cli_driver.go | 6 +- test/go.mod | 3 +- test/go.sum | 2 - 35 files changed, 134 insertions(+), 570 deletions(-) delete mode 100644 cmd/kwil-cli/cmds/utils/dbid.go delete mode 100644 core/utils/dbid.go delete mode 100644 core/utils/utils_test.go delete mode 100644 extensions/auth/nep413.go delete mode 100644 extensions/auth/nep413_test.go diff --git a/.golangci.yml b/.golangci.yml index c06996f07..48b883406 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -29,6 +29,7 @@ linters: - durationcheck - canonicalheader - copyloopvar + - exptostd - fatcontext - gocheckcompilerdirectives - gochecksumtype diff --git a/Taskfile.yml b/Taskfile.yml index 37f0f8b27..3c62fbb7c 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -14,9 +14,11 @@ tasks: tools: aliases: [install:deps] + deps: + - task: linter desc: Install tools required to build this app cmds: - - go install "golang.org/x/tools/cmd/goimports@v0.27.0" + - go install "golang.org/x/tools/cmd/goimports@v0.29.0" fmt: desc: Format the code @@ -36,7 +38,7 @@ tasks: - | (cd core; go mod tidy) go mod tidy - # (cd test; go mod tidy) + (cd test; go mod tidy) # (cd core/client/example; go mod tidy) # (cd core/gatewayclient/example; go mod tidy) @@ -49,7 +51,7 @@ tasks: linter: desc: Install the linter # not for CI which has an action for this cmds: - - go install "github.com/golangci/golangci-lint/cmd/golangci-lint@v1.62.2" + - go install "github.com/golangci/golangci-lint/cmd/golangci-lint@v1.63.4" build: desc: Build kwil-cli and kwild diff --git a/app/shared/display/message_test.go b/app/shared/display/message_test.go index f61a1834c..cac58af5b 100644 --- a/app/shared/display/message_test.go +++ b/app/shared/display/message_test.go @@ -88,8 +88,8 @@ func getExampleTxQueryResponse() *types.TxQueryResponse { } rawPayload := types.ActionExecution{ - DBID: "xf617af1ca774ebbd6d23e8fe12c56d41d25a22d81e88f67c6c6ee0d4", - Action: "create_user", + Namespace: "xf617af1ca774ebbd6d23e8fe12c56d41d25a22d81e88f67c6c6ee0d4", + Action: "create_user", Arguments: [][]*types.EncodedValue{ { { diff --git a/cmd/kwil-cli/cmds/database/batch.go b/cmd/kwil-cli/cmds/database/batch.go index 401810994..7225a8838 100644 --- a/cmd/kwil-cli/cmds/database/batch.go +++ b/cmd/kwil-cli/cmds/database/batch.go @@ -23,16 +23,14 @@ var ( var ( batchLong = `Batch executes an action or procedure on a database using inputs from a CSV file. -To map a CSV column name to a procedure input, use the ` + "`" + `--map-inputs` + "`" + ` flag. -The format is ` + "`" + `--map-inputs ":,:"` + "`" + `. If the ` + "`" + `--map-inputs` + "`" + ` flag is not passed, -the CSV column name will be used as the procedure input name. +To map a CSV column name to a procedure input, use the ` + "`--map-inputs`" + ` flag. +The format is ` + "`--map-inputs :,:`." + + ` If the ` + "`--map-inputs`" + ` flag is not passed, the CSV column name will be used as the procedure input name. You can also specify the input values directly using the ` + "`" + `--values` + "`" + ` flag, delimited by a colon. These values will apply to all inserted rows, and will override the CSV column mappings. -You can either specify the database to execute this against with the ` + "`" + `--name` + "`" + ` and ` + "`" + `--owner` + "`" + ` -flags, or you can specify the database by passing the database id with the ` + "`" + `--dbid` + "`" + ` flag. If a ` + "`" + `--name` + "`" + ` -flag is passed and no ` + "`" + `--owner` + "`" + ` flag is passed, the owner will be inferred from your configured wallet.` +You can specify the namespace with the ` + "`--namespace`" + ` flag.` batchExample = `# Given a CSV file with the following contents: # id,name,age @@ -40,8 +38,8 @@ flag is passed and no ` + "`" + `--owner` + "`" + ` flag is passed, the owner wi # 2,jane,30 # 3,jack,35 -# Executing the ` + "`" + `create_user($user_id, $username, $user_age, $created_at)` + "`" + ` action on the "mydb" database -kwil-cli database batch create_user --path ./users.csv --name mydb --owner 0x9228624C3185FCBcf24c1c9dB76D8Bef5f5DAd64 --map-inputs "id:user_id,name:username,age:user_age" --values created_at:$(date +%s)` +# Executing the ` + "`" + `create_user($user_id, $username, $user_age, $created_at)` + "`" + ` action in the "mydb" database namespace +kwil-cli database batch create_user --path ./users.csv --namespace mydb --map-inputs "id:user_id,name:username,age:user_age" --values created_at:$(date +%s)` ) // batch is used for batch operations on databases @@ -59,9 +57,9 @@ func batchCmd() *cobra.Command { Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { return client.DialClient(cmd.Context(), cmd, 0, func(ctx context.Context, cl clientType.Client, conf *config.KwilCliConfig) error { - dbid, _, err := getSelectedNamespace(cmd) + namespace, _, err := getSelectedNamespace(cmd) if err != nil { - return display.PrintErr(cmd, fmt.Errorf("error getting selected dbid from CLI flags: %w", err)) + return display.PrintErr(cmd, fmt.Errorf("error getting selected namespace from CLI flags: %w", err)) } action, _, err := getSelectedAction(cmd, args) @@ -88,12 +86,12 @@ func batchCmd() *cobra.Command { return display.PrintErr(cmd, fmt.Errorf("error building inputs: %w", err)) } - tuples, err := buildExecutionInputs(ctx, cl, dbid, action, inputs) + tuples, err := buildExecutionInputs(ctx, cl, namespace, action, inputs) if err != nil { return display.PrintErr(cmd, fmt.Errorf("error creating action inputs: %w", err)) } - txHash, err := cl.Execute(ctx, dbid, strings.ToLower(action), tuples, + txHash, err := cl.Execute(ctx, namespace, strings.ToLower(action), tuples, clientType.WithNonce(nonceOverride), clientType.WithSyncBroadcast(syncBcast)) if err != nil { return display.PrintErr(cmd, fmt.Errorf("error executing action: %w", err)) @@ -112,7 +110,7 @@ func batchCmd() *cobra.Command { }, } - bindFlagsTargetingAction(cmd) + bindFlagsTargetingAction(cmd) // --namespace/-n , --action/-a cmd.Flags().StringSliceVarP(&csvColumnMappings, "map-inputs", "m", []string{}, "csv column to action parameter mappings (e.g. csv_id:user_id, csv_name:user_name)") cmd.Flags().StringSliceVarP(&inputValueMappings, "values", "v", []string{}, "action parameter mappings applied to all executions (e.g. id:123, name:john)") cmd.Flags().StringVarP(&filePath, "path", "p", "", "path to the CSV file to use") diff --git a/cmd/kwil-cli/cmds/database/call.go b/cmd/kwil-cli/cmds/database/call.go index 6e38fd51a..a3a2ca0ab 100644 --- a/cmd/kwil-cli/cmds/database/call.go +++ b/cmd/kwil-cli/cmds/database/call.go @@ -19,29 +19,27 @@ import ( ) var ( - callLong = `Call a ` + "`" + `view` + "`" + ` procedure or action, returning the result. + callLong = `Call a ` + "`view`" + ` procedure or action, returning the result. -` + "`" + `view` + "`" + ` procedure are read-only procedure that do not require gas to execute. They are -the primary way to query the state of a database. The ` + "`" + `call` + "`" + ` command is used to call -a ` + "`" + `view` + "`" + ` procedure on a database. It takes the procedure name as the first positional +` + "`view`" + ` procedure are read-only procedure that do not require gas to execute. They are +the primary way to query the state of a database. The ` + "`call`" + ` command is used to call +a ` + "`view`" + ` procedure on a database. It takes the procedure name as the first positional argument, and the procedure inputs as all subsequent arguments. To specify a procedure input, you first need to specify the input name, then the input value, delimited by a colon. -For example, for procedure ` + "`" + `get_user($username)` + "`" + `, you would specify the procedure as follows: +For example, for procedure ` + "`get_user($username)`" + `, you would specify the procedure as follows: -` + "`" + `call get_user username:satoshi` + "`" + ` +` + "`call get_user username:satoshi`" + ` -You can either specify the database to execute this against with the ` + "`" + `--name` + "`" + ` and ` + "`" + `--owner` + "`" + ` -flags, or you can specify the database by passing the database id with the ` + "`" + `--dbid` + "`" + ` flag. If a ` + "`" + `--name` + "`" + ` -flag is passed and no ` + "`" + `--owner` + "`" + ` flag is passed, the owner will be inferred from your configured wallet. +You specify the database namespace in which to execute this with the ` + "`--namespace` flag. " + ` -If you are interacting with a Kwil gateway, you can also pass the ` + "`" + `--authenticate` + "`" + ` flag to authenticate the call with your private key.` +If you are interacting with a Kwil gateway, you can also pass the ` + "`--authenticate`" + ` flag to authenticate the call with your private key.` - callExample = `# Calling the ` + "`" + `get_user($username)` + "`" + ` procedure on the "mydb" database -kwil-cli database call get_user --name mydb --owner 0x9228624C3185FCBcf24c1c9dB76D8Bef5f5DAd64 username:satoshi + callExample = `# Calling the ` + "`get_user($username)`" + ` procedure on the "somedb" namespace +kwil-cli database call get_user --namespace somedb username:satoshi -# Calling the ` + "`" + `get_user($username)` + "`" + ` procedure on a database using a dbid, authenticating with a private key -kwil-cli database call get_user --dbid 0x9228624C3185FCBcf24c1c9dB76D8Bef5f5DAd64 username:satoshi --authenticate` +# Calling the ` + "`get_user($username)`" + ` procedure on a database using a namespace, authenticating with a private key +kwil-cli database call get_user --namespace somedb username:satoshi --authenticate` ) func callCmd() *cobra.Command { @@ -63,9 +61,9 @@ func callCmd() *cobra.Command { } return client.DialClient(cmd.Context(), cmd, dialFlags, func(ctx context.Context, clnt clientType.Client, conf *config.KwilCliConfig) error { - dbid, _, err := getSelectedNamespace(cmd) + namespace, _, err := getSelectedNamespace(cmd) if err != nil { - return display.PrintErr(cmd, fmt.Errorf("error getting selected dbid from CLI flags: %w", err)) + return display.PrintErr(cmd, fmt.Errorf("error getting selected namespace from CLI flags: %w", err)) } action, args, err := getSelectedAction(cmd, args) @@ -78,7 +76,7 @@ func callCmd() *cobra.Command { return display.PrintErr(cmd, fmt.Errorf("error getting inputs: %w", err)) } - tuples, err := buildExecutionInputs(ctx, clnt, dbid, action, []map[string]string{inputs}) + tuples, err := buildExecutionInputs(ctx, clnt, namespace, action, []map[string]string{inputs}) if err != nil { return display.PrintErr(cmd, fmt.Errorf("error creating action/procedure inputs: %w", err)) } @@ -90,7 +88,7 @@ func callCmd() *cobra.Command { return display.PrintErr(cmd, errors.New("only one set of inputs can be provided to call")) } - data, err := clnt.Call(ctx, dbid, action, tuples[0]) + data, err := clnt.Call(ctx, namespace, action, tuples[0]) if err != nil { return display.PrintErr(cmd, fmt.Errorf("error calling action/procedure: %w", err)) } @@ -107,7 +105,7 @@ func callCmd() *cobra.Command { }, } - bindFlagsTargetingAction(cmd) + bindFlagsTargetingAction(cmd) // --namespace/-n , --action/-a cmd.Flags().BoolVar(&gwAuth, "authenticate", false, "authenticate signals that the call is being made to a gateway and should be authenticated with the private key") cmd.Flags().BoolVar(&logs, "logs", false, "result will include logs from notices raised during the call") return cmd diff --git a/cmd/kwil-cli/cmds/database/cmd.go b/cmd/kwil-cli/cmds/database/cmd.go index 054a701f6..fe9f34885 100644 --- a/cmd/kwil-cli/cmds/database/cmd.go +++ b/cmd/kwil-cli/cmds/database/cmd.go @@ -8,8 +8,8 @@ var ( dbCmd = &cobra.Command{ Use: "database", Aliases: []string{"db"}, - Short: "The database command is a parent command containing subcommands for interacting with databases.", - Long: "The database command is a parent command containing subcommands for interacting with databases.", + Short: "The database command is a parent command containing subcommands for interacting with the database.", + Long: "The database command is a parent command containing subcommands for interacting with the database.", } nonceOverride int64 diff --git a/cmd/kwil-cli/cmds/database/execute.go b/cmd/kwil-cli/cmds/database/execute.go index ddc1ac1f9..f7e3b1fc3 100644 --- a/cmd/kwil-cli/cmds/database/execute.go +++ b/cmd/kwil-cli/cmds/database/execute.go @@ -27,11 +27,11 @@ statement "SELECT * FROM users WHERE age > 25", you would specify the following: To specify an action to execute, you can pass the action name as the first positional argument, or as the --action flag. The action name is specified as the first positional argument, and the action parameters as all subsequent arguments.` - executeExample = `# Executing a CREATE TABLE statement on the "mydb" database + executeExample = `# Executing a CREATE TABLE statement on the "somedb" database namespace kwil-cli database execute --sql "CREATE TABLE users (id UUID, name TEXT, age INT8);" --namespace mydb -# Executing the ` + "`" + `create_user($username, $age)` + "`" + ` procedure on the "mydb" database -kwil-cli database execute --action create_user username:satoshi age:32 --namespace mydb +# Executing the ` + "`" + `create_user($username, $age)` + "`" + ` procedure on the "somedb" database +kwil-cli database execute --action create_user username:satoshi age:32 --namespace somedb ` ) @@ -167,7 +167,7 @@ func executeCmd() *cobra.Command { }, } - bindFlagsTargetingAction(cmd) + bindFlagsTargetingAction(cmd) // --namespace/-n , --action/-a cmd.Flags().StringVarP(&sqlStmt, "sql", "s", "", "the SQL statement to execute") cmd.Flags().StringVarP(&sqlFilepath, "sql-file", "f", "", "the file containing the SQL statement to execute") return cmd diff --git a/cmd/kwil-cli/cmds/database/message.go b/cmd/kwil-cli/cmds/database/message.go index e1c2e225f..3eda069e3 100644 --- a/cmd/kwil-cli/cmds/database/message.go +++ b/cmd/kwil-cli/cmds/database/message.go @@ -40,7 +40,7 @@ func (d *respDBList) MarshalText() ([]byte, error) { msg.WriteString(fmt.Sprintf("Databases belonging to '%x':\n", d.owner)) } for i, db := range d.Info { - msg.WriteString(fmt.Sprintf(" DBID: %s\n", db.DBID)) + msg.WriteString(fmt.Sprintf(" Namespace: %s\n", db.Namespace)) msg.WriteString(fmt.Sprintf(" Name: %s\n", db.Name)) msg.WriteString(fmt.Sprintf(" Owner: %x", db.Owner)) if i != len(d.Info)-1 { diff --git a/cmd/kwil-cli/cmds/database/message_test.go b/cmd/kwil-cli/cmds/database/message_test.go index 95b03f186..6db0e1a00 100644 --- a/cmd/kwil-cli/cmds/database/message_test.go +++ b/cmd/kwil-cli/cmds/database/message_test.go @@ -1,15 +1,13 @@ package database import ( - "encoding/hex" - "github.com/kwilteam/kwil-db/app/shared/display" "github.com/kwilteam/kwil-db/core/types" ) func Example_respDBlist_text_0() { display.Print( - &respDBList{Info: []*types.DatasetIdentifier{}, owner: mustDecodeHex("6f776e6572")}, + &respDBList{Info: []*types.DatasetIdentifier{}, owner: testOwner}, nil, "text") // Output: // No databases found for '6f776e6572'. @@ -19,57 +17,44 @@ func Example_respDBlist_text() { display.Print( &respDBList{Info: []*types.DatasetIdentifier{ { - Name: "db_a", - Owner: mustDecodeHex("6f776e6572"), - DBID: "xabc", + Name: "db_a", + Owner: testOwner, + Namespace: "one", }, { - Name: "db_b", - Owner: mustDecodeHex("6f776e6572"), - DBID: "xdef", + Name: "db_b", + Owner: testOwner, + Namespace: "two", }, }, - owner: mustDecodeHex("6f776e6572")}, + owner: testOwner}, nil, "text") // Output: // Databases belonging to '6f776e6572': - // DBID: xabc + // Namespace: one // Name: db_a // Owner: 6f776e6572 - // DBID: xdef + // Namespace: two // Name: db_b // Owner: 6f776e6572 } -func mustDecodeHex(s string) []byte { - b, err := hex.DecodeString(s) - if err != nil { - panic(err) - } - return b -} - -// func mustDecodeBase64(s string) []byte { -// b, err := base64.StdEncoding.DecodeString(s) -// if err != nil { -// panic(err) -// } -// return b -// } +// hex.DecodeString("6f776e6572") +var testOwner = []byte{0x6f, 0x77, 0x6e, 0x65, 0x72} func Example_respDBlist_json() { display.Print( &respDBList{Info: []*types.DatasetIdentifier{ { - Name: "db_a", - Owner: mustDecodeHex("6f776e6572"), - DBID: "xabc", + Name: "db_a", + Owner: testOwner, + Namespace: "one", }, { - Name: "db_b", - Owner: mustDecodeHex("6f776e6572"), - DBID: "xdef", + Name: "db_b", + Owner: testOwner, + Namespace: "two", }, }}, nil, "json") @@ -80,12 +65,12 @@ func Example_respDBlist_json() { // { // "name": "db_a", // "owner": "6f776e6572", - // "dbid": "xabc" + // "namespace": "one" // }, // { // "name": "db_b", // "owner": "6f776e6572", - // "dbid": "xdef" + // "namespace": "two" // } // ], // "error": "" diff --git a/cmd/kwil-cli/cmds/database/query.go b/cmd/kwil-cli/cmds/database/query.go index e34fef661..b4ea77784 100644 --- a/cmd/kwil-cli/cmds/database/query.go +++ b/cmd/kwil-cli/cmds/database/query.go @@ -17,15 +17,13 @@ var ( Requires a SQL SELECT statement as an argument. -You can either specify the database to execute this against with the ` + "`" + `--name` + "`" + ` and ` + "`" + `--owner` + "`" + ` -flags, or you can specify the database by passing the database id with the ` + "`" + `--dbid` + "`" + ` flag. If a ` + "`" + `--name` + "`" + ` -flag is passed and no ` + "`" + `--owner` + "`" + ` flag is passed, the owner will be inferred from your configured wallet. +You specify the database namespace to execute this against with the ` + "`--namespace` flag." + ` Note that ad-hoc queries will be rejected on RPC servers that are operating with authenticated call requests enabled.` - queryExample = `# Querying the "users" table in the "mydb" database -kwil-cli database query "SELECT * FROM users WHERE age > 25" --name mydb --owner 0x9228624C3185FCBcf24c1c9dB76D8Bef5f5DAd64` + queryExample = `# Querying the "users" table in the "somedb" database namespace +kwil-cli database query "SELECT * FROM users WHERE age > 25" --namespace somedb` ) func queryCmd() *cobra.Command { diff --git a/cmd/kwil-cli/cmds/root.go b/cmd/kwil-cli/cmds/root.go index 1b3ef668f..2f11f427f 100644 --- a/cmd/kwil-cli/cmds/root.go +++ b/cmd/kwil-cli/cmds/root.go @@ -17,12 +17,12 @@ import ( "github.com/spf13/cobra" ) -var longDesc = `Command line interface client for using %s. +var longDesc = `Command line interface client for using ` + "`%s`." + ` -` + "`" + `%s` + "`" + ` is a command line interface for interacting with %s. It can be used to deploy, update, and query databases. +` + "`%s`" + ` is a command line interface for interacting with %s. It can be used to deploy, update, and query databases. -` + "`" + `%s` + "`" + ` can be configured with a persistent configuration file. This file can be configured with the '%s configure' command. -` + "`" + `%s` + "`" + ` will look for a configuration file at ` + "`" + `$HOME/.kwil-cli/config.json` + "`" + `.` +` + "`%s`" + ` can be configured with a persistent configuration file. This file can be configured with the '%s configure' command. +` + "`%s`" + ` will look for a configuration file at ` + "`$HOME/.kwil-cli/config.json`." func NewRootCmd() *cobra.Command { // The basis for ActiveConfig starts with defaults defined in DefaultKwilCliPersistedConfig. diff --git a/cmd/kwil-cli/cmds/utils/dbid.go b/cmd/kwil-cli/cmds/utils/dbid.go deleted file mode 100644 index 70640be73..000000000 --- a/cmd/kwil-cli/cmds/utils/dbid.go +++ /dev/null @@ -1,73 +0,0 @@ -package utils - -import ( - "encoding/hex" - "encoding/json" - "fmt" - "strings" - - "github.com/spf13/cobra" - - "github.com/kwilteam/kwil-db/app/shared/display" - "github.com/kwilteam/kwil-db/core/utils" -) - -var ( - dbidLong = "`" + `dbid` + "`" + ` generates a dbid for a given schema name and deployer.` - - dbidExample = `# Generate a dbid for a schema and deployer -kwil-cli utils dbid --schema=myschema --deployer=0x1234567890abcdef - -# Maintain the exact deployer address, and do not trim off the 0x prefix -kwil-cli utils dbid --schema=myschema --deployer=0xnot_an_eth_address --no-trim` -) - -func dbidCmd() *cobra.Command { - var schema, deployer string - var noTrim bool - - cmd := &cobra.Command{ - Use: "generate-dbid", - Short: dbidLong, - Long: dbidLong, - Example: dbidExample, - Args: cobra.NoArgs, - RunE: func(cmd *cobra.Command, args []string) error { - deployerBts := []byte(deployer) - if strings.HasPrefix(deployer, "0x") && !noTrim { - deployer = deployer[2:] - var err error - deployerBts, err = hex.DecodeString(deployer) - if err != nil { - return display.PrintErr(cmd, fmt.Errorf(`deployer address "%s" has 0x prefix but is not a valid hex string. try using the --no-trim flag`, deployer)) - } - } - - dbid := utils.GenerateDBID(schema, deployerBts) - return display.PrintCmd(cmd, &dbidOutput{DBID: dbid}) - }, - } - - cmd.Flags().StringVar(&schema, "schema", "", "Schema name") - cmd.Flags().StringVar(&deployer, "deployer", "", "Deployer address") - cmd.Flags().BoolVar(&noTrim, "no-trim", false, "Do not trim off the 0x prefix of the deployer address") - // mark required flags - cmd.MarkFlagRequired("schema") - cmd.MarkFlagRequired("deployer") - - return cmd -} - -type dbidOutput struct { - DBID string `json:"dbid"` -} - -func (d *dbidOutput) MarshalJSON() ([]byte, error) { - // Alias is used to avoid infinite recursion when calling json.Marshal - type Alias dbidOutput - return json.Marshal((Alias)(*d)) -} - -func (d *dbidOutput) MarshalText() (text []byte, err error) { - return []byte("dbid: " + d.DBID), nil -} diff --git a/cmd/kwil-cli/cmds/utils/utils.go b/cmd/kwil-cli/cmds/utils/utils.go index d9dbd1e2d..d450523ac 100644 --- a/cmd/kwil-cli/cmds/utils/utils.go +++ b/cmd/kwil-cli/cmds/utils/utils.go @@ -21,7 +21,6 @@ func NewCmdUtils() *cobra.Command { chainInfoCmd(), kgwAuthnCmd(), testCmd(), - dbidCmd(), generateKeyCmd(), ) diff --git a/contrib/scripts/build/.go_variables b/contrib/scripts/build/.go_variables index cc185e072..8efdf63c1 100644 --- a/contrib/scripts/build/.go_variables +++ b/contrib/scripts/build/.go_variables @@ -33,7 +33,7 @@ if [ "$GO_LINKMODE" = "static" ]; then GO_BUILDTAGS="$GO_BUILDTAGS osusergo netgo" fi -GO_BUILDTAGS="$GO_BUILDTAGS auth_nep413 auth_ed25519_sha256" +GO_BUILDTAGS="$GO_BUILDTAGS auth_ed25519_sha256" GO_LDFLAGS="$GO_LDFLAGS -s -w" diff --git a/core/client/client.go b/core/client/client.go index 8439835df..20333091f 100644 --- a/core/client/client.go +++ b/core/client/client.go @@ -242,7 +242,7 @@ func (c *Client) ChainInfo(ctx context.Context) (*types.ChainInfo, error) { // It returns the receipt, as well as outputs which is the decoded body of the receipt. // It can take any number of inputs, and if multiple tuples of inputs are passed, // it will execute them in the same transaction. -func (c *Client) Execute(ctx context.Context, dbid string, procedure string, tuples [][]any, opts ...clientType.TxOpt) (types.Hash, error) { +func (c *Client) Execute(ctx context.Context, namespace string, procedure string, tuples [][]any, opts ...clientType.TxOpt) (types.Hash, error) { encodedTuples := make([][]*types.EncodedValue, len(tuples)) for i, tuple := range tuples { encoded, err := encodeTuple(tuple) @@ -254,7 +254,7 @@ func (c *Client) Execute(ctx context.Context, dbid string, procedure string, tup executionBody := &types.ActionExecution{ Action: procedure, - DBID: dbid, + Namespace: namespace, Arguments: encodedTuples, } @@ -265,7 +265,7 @@ func (c *Client) Execute(ctx context.Context, dbid string, procedure string, tup } c.logger.Debug("execute action", - "DBID", dbid, "action", procedure, + "Namespace", namespace, "action", procedure, "signature_type", tx.Signature.Type, "signature", base64.StdEncoding.EncodeToString(tx.Signature.Data), "fee", tx.Body.Fee.String(), "nonce", tx.Body.Nonce) @@ -306,14 +306,14 @@ func (c *Client) ExecuteSQL(ctx context.Context, stmt string, params map[string] } // Call calls a procedure or action. It returns the result records. -func (c *Client) Call(ctx context.Context, dbid string, procedure string, inputs []any) (*types.CallResult, error) { +func (c *Client) Call(ctx context.Context, namespace string, procedure string, inputs []any) (*types.CallResult, error) { encoded, err := encodeTuple(inputs) if err != nil { return nil, err } payload := &types.ActionCall{ - DBID: dbid, + Namespace: namespace, Action: procedure, Arguments: encoded, } diff --git a/core/gatewayclient/client.go b/core/gatewayclient/client.go index 9f1f16d4b..9357c27bb 100644 --- a/core/gatewayclient/client.go +++ b/core/gatewayclient/client.go @@ -142,10 +142,10 @@ func NewClient(ctx context.Context, target string, opts *GatewayOptions) (*Gatew // Call call an action. It returns the result records. If authentication is needed, // it will call the gatewaySigner to sign the authentication message. -func (c *GatewayClient) Call(ctx context.Context, dbid string, action string, inputs []any) (*types.CallResult, error) { +func (c *GatewayClient) Call(ctx context.Context, namespace string, action string, inputs []any) (*types.CallResult, error) { // we will try to call with the current cookies set. If we receive an error and it is an auth error, // we will re-auth and retry. We will only retry once. - res, err := c.Client.Call(ctx, dbid, action, inputs) + res, err := c.Client.Call(ctx, namespace, action, inputs) if err == nil { return res, nil } @@ -164,7 +164,7 @@ func (c *GatewayClient) Call(ctx context.Context, dbid string, action string, in } // retry the call - return c.Client.Call(ctx, dbid, action, inputs) + return c.Client.Call(ctx, namespace, action, inputs) } // authenticate authenticates the client with the gateway. diff --git a/core/rpc/json/user/commands.go b/core/rpc/json/user/commands.go index f310d96ca..b62dca01f 100644 --- a/core/rpc/json/user/commands.go +++ b/core/rpc/json/user/commands.go @@ -21,7 +21,7 @@ type VersionRequest struct{} // SchemaRequest contains the request parameters for MethodSchema. type SchemaRequest struct { - DBID string `json:"dbid"` + Namespace string `json:"namespace"` } // AccountRequest contains the request parameters for MethodAccount. diff --git a/core/types/message.go b/core/types/message.go index ea7729ca6..f832f84c1 100644 --- a/core/types/message.go +++ b/core/types/message.go @@ -44,15 +44,15 @@ type CallMessage struct { const callMsgToSignTmplV0 = `Kwil view call. -DBID: %s +Namespace: %s Method: %s Digest: %x Challenge: %x ` -func CallSigText(dbid, action string, payload []byte, challenge []byte) string { +func CallSigText(namespace, action string, payload []byte, challenge []byte) string { digest := sha256.Sum256(payload) - return fmt.Sprintf(callMsgToSignTmplV0, dbid, action, digest[:20], challenge) + return fmt.Sprintf(callMsgToSignTmplV0, namespace, action, digest[:20], challenge) } const stmtMsgToSignTmplV0 = `Kwil SQL statement. @@ -93,7 +93,7 @@ func CreateCallMessage(ac *ActionCall, challenge []byte, signer auth.Signer) (*C } if len(challenge) > 0 { - sigText := CallSigText(ac.DBID, ac.Action, bts, challenge) + sigText := CallSigText(ac.Namespace, ac.Action, bts, challenge) sig, err := signer.Sign([]byte(sigText)) if err != nil { return nil, err diff --git a/core/types/message_test.go b/core/types/message_test.go index e3d0f00a5..320611d92 100644 --- a/core/types/message_test.go +++ b/core/types/message_test.go @@ -12,7 +12,7 @@ import ( func TestCreateCallMessage(t *testing.T) { t.Run("create call message without signer", func(t *testing.T) { ac := &ActionCall{ - DBID: "testdb", + Namespace: "testdb", Action: "testaction", Arguments: nil, } @@ -30,7 +30,7 @@ func TestCreateCallMessage(t *testing.T) { t.Run("create call message with signer but no challenge", func(t *testing.T) { ac := &ActionCall{ - DBID: "testdb", + Namespace: "testdb", Action: "testaction", Arguments: nil, } @@ -50,7 +50,7 @@ func TestCreateCallMessage(t *testing.T) { t.Run("create call message with invalid action call", func(t *testing.T) { ac := &ActionCall{ - DBID: "", + Namespace: "", Action: "", Arguments: nil, } @@ -62,7 +62,7 @@ func TestCreateCallMessage(t *testing.T) { t.Run("create call message with signer and challenge", func(t *testing.T) { ac := &ActionCall{ - DBID: "testdb", + Namespace: "testdb", Action: "testaction", Arguments: nil, } diff --git a/core/types/payloads.go b/core/types/payloads.go index 235bd435f..e2da1a7ad 100644 --- a/core/types/payloads.go +++ b/core/types/payloads.go @@ -275,7 +275,7 @@ func (r RawStatement) Type() PayloadType { // ActionExecution is the payload that is used to execute an action type ActionExecution struct { - DBID string + Namespace string Action string Arguments [][]*EncodedValue } @@ -292,7 +292,7 @@ const aeVersion = 0 // all cases): // // - Two bytes for version (uint16), which is presently 0 (aeVersion). -// - The DBID string is written according to WriteString, which has a +// - The namespace string is written according to WriteString, which has a // 4 byte length prefix followed by the bytes of the utf8 string. // - The Action string is written according to WriteString. // - The number of batched calls is written as a uint16. @@ -307,8 +307,8 @@ func (a ActionExecution) MarshalBinary() ([]byte, error) { if err := binary.Write(buf, SerializationByteOrder, uint16(aeVersion)); err != nil { return nil, err } - // dbid - err := WriteString(buf, a.DBID) + // namespace + err := WriteString(buf, a.Namespace) if err != nil { return nil, err } @@ -353,8 +353,8 @@ func (a *ActionExecution) UnmarshalBinary(b []byte) error { if version != aeVersion { return fmt.Errorf("unsupported version %d", version) } - // dbid - dbid, err := ReadString(rd) + // namespace + namespace, err := ReadString(rd) if err != nil { return err } @@ -392,7 +392,7 @@ func (a *ActionExecution) UnmarshalBinary(b []byte) error { } a.Action = action - a.DBID = dbid + a.Namespace = namespace a.Arguments = args // ensure all args[i] have same length here or in caller? @@ -405,7 +405,7 @@ func (a *ActionExecution) UnmarshalBinary(b []byte) error { // transactions.ActionExecution for the transaction payload used for executing // an action. type ActionCall struct { - DBID string // TODO: rename to Namespace + Namespace string Action string Arguments []*EncodedValue } @@ -419,7 +419,7 @@ const acVersion = 0 // all cases): // // - Two bytes for version (uint16), which is presently 0 (acVersion). -// - The DBID string is written according to WriteString, which has a +// - The namespace string is written according to WriteString, which has a // 4 byte length prefix followed by the bytes of the utf8 string. // - The Action string is written according to WriteString. // - The number of arguments is written as a uint16. @@ -432,8 +432,8 @@ func (ac ActionCall) MarshalBinary() ([]byte, error) { if err := binary.Write(buf, SerializationByteOrder, uint16(acVersion)); err != nil { return nil, err } - // dbid - err := WriteString(buf, ac.DBID) + // namespace + err := WriteString(buf, ac.Namespace) if err != nil { return nil, err } @@ -471,8 +471,8 @@ func (ac *ActionCall) UnmarshalBinary(b []byte) error { if version != acVersion { return fmt.Errorf("unsupported version %d", version) } - // dbid - dbid, err := ReadString(rd) + // namespace + namespace, err := ReadString(rd) if err != nil { return err } @@ -502,7 +502,7 @@ func (ac *ActionCall) UnmarshalBinary(b []byte) error { } ac.Action = action - ac.DBID = dbid + ac.Namespace = namespace ac.Arguments = args return nil diff --git a/core/types/payloads_test.go b/core/types/payloads_test.go index 4ad0b076b..c4d94359c 100644 --- a/core/types/payloads_test.go +++ b/core/types/payloads_test.go @@ -795,8 +795,8 @@ func TestRawStatement_MarshalUnmarshal(t *testing.T) { func TestActionExecution_MarshalUnmarshal(t *testing.T) { t.Run("valid action execution with multiple calls", func(t *testing.T) { original := ActionExecution{ - DBID: "testdb", - Action: "test_action", + Namespace: "testdb", + Action: "test_action", Arguments: [][]*EncodedValue{ { {Type: DataType{Name: TextType.Name}, Data: [][]byte{[]byte("arg1")}}, @@ -815,7 +815,7 @@ func TestActionExecution_MarshalUnmarshal(t *testing.T) { var decoded ActionExecution err = decoded.UnmarshalBinary(data) require.NoError(t, err) - require.Equal(t, original.DBID, decoded.DBID) + require.Equal(t, original.Namespace, decoded.Namespace) require.Equal(t, original.Action, decoded.Action) require.Equal(t, len(original.Arguments), len(decoded.Arguments)) require.Equal(t, original.Arguments, decoded.Arguments) @@ -823,7 +823,7 @@ func TestActionExecution_MarshalUnmarshal(t *testing.T) { t.Run("empty arguments array", func(t *testing.T) { original := ActionExecution{ - DBID: "testdb", + Namespace: "testdb", Action: "empty_action", Arguments: [][]*EncodedValue{}, } @@ -834,15 +834,15 @@ func TestActionExecution_MarshalUnmarshal(t *testing.T) { var decoded ActionExecution err = decoded.UnmarshalBinary(data) require.NoError(t, err) - require.Equal(t, original.DBID, decoded.DBID) + require.Equal(t, original.Namespace, decoded.Namespace) require.Equal(t, original.Action, decoded.Action) require.Empty(t, decoded.Arguments) }) - t.Run("empty strings for DBID and Action", func(t *testing.T) { + t.Run("empty strings for Namespace and Action", func(t *testing.T) { original := ActionExecution{ - DBID: "", - Action: "", + Namespace: "", + Action: "", Arguments: [][]*EncodedValue{ { {Type: DataType{Name: TextType.Name}, Data: [][]byte{[]byte("test")}}, @@ -856,7 +856,7 @@ func TestActionExecution_MarshalUnmarshal(t *testing.T) { var decoded ActionExecution err = decoded.UnmarshalBinary(data) require.NoError(t, err) - require.Empty(t, decoded.DBID) + require.Empty(t, decoded.Namespace) require.Empty(t, decoded.Action) require.Len(t, decoded.Arguments, 1) }) @@ -914,8 +914,8 @@ func TestActionExecution_MarshalUnmarshal(t *testing.T) { func TestActionCall_MarshalUnmarshal(t *testing.T) { t.Run("valid action call with multiple arguments", func(t *testing.T) { original := ActionCall{ - DBID: "testdb", - Action: "test_action", + Namespace: "testdb", + Action: "test_action", Arguments: []*EncodedValue{ { Type: DataType{Name: TextType.Name}, @@ -934,7 +934,7 @@ func TestActionCall_MarshalUnmarshal(t *testing.T) { var decoded ActionCall err = decoded.UnmarshalBinary(data) require.NoError(t, err) - require.Equal(t, original.DBID, decoded.DBID) + require.Equal(t, original.Namespace, decoded.Namespace) require.Equal(t, original.Action, decoded.Action) require.Len(t, decoded.Arguments, len(original.Arguments)) for i, arg := range original.Arguments { @@ -943,9 +943,9 @@ func TestActionCall_MarshalUnmarshal(t *testing.T) { } }) - t.Run("empty dbid and action with no arguments", func(t *testing.T) { + t.Run("empty namespace and action with no arguments", func(t *testing.T) { original := ActionCall{ - DBID: "", + Namespace: "", Action: "", Arguments: []*EncodedValue{}, } @@ -956,7 +956,7 @@ func TestActionCall_MarshalUnmarshal(t *testing.T) { var decoded ActionCall err = decoded.UnmarshalBinary(data) require.NoError(t, err) - require.Equal(t, original.DBID, decoded.DBID) + require.Equal(t, original.Namespace, decoded.Namespace) require.Equal(t, original.Action, decoded.Action) require.Empty(t, decoded.Arguments) }) @@ -971,7 +971,7 @@ func TestActionCall_MarshalUnmarshal(t *testing.T) { } original := ActionCall{ - DBID: "testdb", + Namespace: "testdb", Action: "bulk_action", Arguments: args, } @@ -982,7 +982,7 @@ func TestActionCall_MarshalUnmarshal(t *testing.T) { var decoded ActionCall err = decoded.UnmarshalBinary(data) require.NoError(t, err) - require.Equal(t, original.DBID, decoded.DBID) + require.Equal(t, original.Namespace, decoded.Namespace) require.Equal(t, original.Action, decoded.Action) require.Len(t, decoded.Arguments, len(original.Arguments)) }) @@ -1009,7 +1009,7 @@ func TestActionCall_MarshalUnmarshal(t *testing.T) { require.Error(t, err) }) - t.Run("truncated data after dbid", func(t *testing.T) { + t.Run("truncated data after namespace", func(t *testing.T) { buf := &bytes.Buffer{} binary.Write(buf, SerializationByteOrder, uint16(acVersion)) WriteString(buf, "testdb") diff --git a/core/types/types.go b/core/types/types.go index f67405ccb..e2d0eb2d3 100644 --- a/core/types/types.go +++ b/core/types/types.go @@ -188,9 +188,9 @@ func (v *Validator) UnmarshalJSON(b []byte) error { // DatasetIdentifier contains the information required to identify a dataset. type DatasetIdentifier struct { - Name string `json:"name"` - Owner HexBytes `json:"owner"` - DBID string `json:"dbid"` + Name string `json:"name"` + Owner HexBytes `json:"owner"` + Namespace string `json:"namespace"` } // VotableEventID returns the ID of an event that can be voted on. This may be diff --git a/core/utils/dbid.go b/core/utils/dbid.go deleted file mode 100644 index f7372967d..000000000 --- a/core/utils/dbid.go +++ /dev/null @@ -1,17 +0,0 @@ -package utils - -import ( - "crypto/sha256" - "encoding/hex" - "strings" -) - -// DB ID is a convention. This is likely to change: -// https://github.com/kwilteam/kwil-db/issues/332 - -func GenerateDBID(name string, ownerID []byte) string { - h := sha256.New224() - h.Write([]byte(strings.ToLower(name))) - h.Write(ownerID) - return "x" + hex.EncodeToString(h.Sum(nil)) -} diff --git a/core/utils/utils_test.go b/core/utils/utils_test.go deleted file mode 100644 index e0f8f63cb..000000000 --- a/core/utils/utils_test.go +++ /dev/null @@ -1,21 +0,0 @@ -package utils_test - -import ( - "strings" - "testing" - - "github.com/stretchr/testify/assert" - - "github.com/kwilteam/kwil-db/core/utils" -) - -// testing dbid is case insensitive -func Test_DBID(t *testing.T) { - owner := []byte("owner") - name := "name" - - dbid1 := utils.GenerateDBID(name, owner) - dbid2 := utils.GenerateDBID(strings.ToUpper(name), owner) - - assert.Equal(t, dbid1, dbid2) -} diff --git a/extensions/auth/nep413.go b/extensions/auth/nep413.go deleted file mode 100644 index 5058631e0..000000000 --- a/extensions/auth/nep413.go +++ /dev/null @@ -1,170 +0,0 @@ -//go:build auth_nep413 || ext_test - -package auth - -import ( - "crypto/ed25519" - "crypto/sha256" - "encoding/hex" - "errors" - "fmt" - - "github.com/kwilteam/kwil-db/core/crypto" - borsch "github.com/near/borsh-go" -) - -func init() { - err := RegisterAuthenticator(ModAdd, nep413Name, Nep413Authenticator{ - MsgEncoder: func(bts []byte) string { - return string(bts) - }, - }) - if err != nil { - panic(err) - } -} - -const nep413Name = "nep413" - -/* - - This implements the NEP-413 standard as a Kwil authentication driver. - - NEP-413 is a standard for authentication developed by NEAR protocol. It utilizes - borsch serialization and ed25519 signatures to authenticate users. - - More info can be found here: https://github.com/near/NEPs/blob/master/neps/nep-0413.md. - - In order to implement this, the incoming `Signature` field needs to contain more data that just the ed25519 signature. - It needs to contain the following: - - the length of the first struct (the nep413 `Payload`, without the Tag and Message) - - the first struct (the nep413 `Payload`, without the Tag and Message), serialized with borsch - - the signature generated from the nep413 `AuthenticationToken.signature` - - The first struct is the nep413 `Payload` (https://github.com/near/NEPs/blob/master/neps/nep-0413.md#structure). - All fields of the payload EXCEPT the message and tag are included here (the message field - is populated by the serialized Kwil transaction, and the tag is standard in nep413). - Below, the first struct is identified by the struct type `nep413Payload`. - - The signature is the `signature` field returned from the nep413 signer function (https://github.com/near/NEPs/blob/master/neps/nep-0413.md#output-interface). - This does not contain the the other fields, because they are not needed. - - The signature bytes are as follows: - 1. [0-1]: uint16 length of the first struct (big endian) - 2. [2-2+length]: payload struct - 3. [2+length:]: signature -*/ - -// Nep413Payload is the message sent to the NEP-413 signer. -// It utilizes borsch for deterministic serialization -type Nep413Payload struct { - // Tag is some NEAR specific thing that is not really explained anywhere, - // but should always be the number 2^31+413, or 2147484061 - // https://github.com/near/NEPs/blob/master/neps/nep-0413.md#example - Tag uint32 - - // Message is the plaintext message - Message string - - // Nonce is the 32 byte nonce of the message - Nonce [32]byte - - // Recipient is the string identifier of the recipient (e.g. satoshi.near) - Recipient string - - // CallbackUrl is the url to call when the signature is ready - CallbackUrl *string -} - -type Nep413Authenticator struct { - // MsgEncoder is a function that encodes a message into a string. - // For production, this should be base64.StdEncoding.EncodeToString, - // but for testing, it can be a different encoding. - MsgEncoder func([]byte) string -} - -func (n Nep413Authenticator) Verify(sender []byte, msg []byte, signature []byte) error { - if len(sender) != ed25519.PublicKeySize { - return fmt.Errorf("invalid ed25519 public key size when verifying signature %d", len(sender)) - } - - // deserialize the signature - payload, sig, err := deserializeSignature(signature) - if err != nil { - return err - } - - // add the tag and message to the payload - payload.Tag = 2147484061 - payload.Message = n.MsgEncoder(msg) - - // serialize the payload - // it is critical that the payload is not a pointer, - // since it gives a different result. - payloadBytes, err := borsch.Serialize(*payload) - if err != nil { - return err - } - - // hash the payload - hash := sha256.Sum256(payloadBytes) - - // verify the signature - if !ed25519.Verify(sender, hash[:], sig) { - return crypto.ErrInvalidSignature - } - - return nil -} - -// Identifier generates a NEAR implicit address from a public key, -// which is simply the hex-encoded public key. -func (n Nep413Authenticator) Identifier(sender []byte) (string, error) { - if len(sender) != ed25519.PublicKeySize { - return "", fmt.Errorf("invalid ed25519 public key size for generating near address: %d", len(sender)) - } - - return hex.EncodeToString(sender), nil -} - -// deserializeSignature deserializes the payload and the signature from the signature bytes -func deserializeSignature(signatureBts []byte) (*Nep413Payload, []byte, error) { - if len(signatureBts) < 2 { - return nil, nil, errors.New("invalid signature length") - } - - // get the length of the first struct - payloadLength := int(signatureBts[0])<<8 | int(signatureBts[1]) // big endian - - // check that the length is valid - if len(signatureBts) < 2+payloadLength { - return nil, nil, errors.New("invalid signature length") - } - - struct1Bts := signatureBts[2 : 2+payloadLength] - - // deserialize the first struct - payload := &Nep413Payload{} - err := borsch.Deserialize(payload, struct1Bts) - if err != nil { - return nil, nil, err - } - - // borsch-go has a weird bug where it will deserialize a nil string ((*string)(nil)) - // as an empty string (""). When serializing, it does NOT - // serialize an empty string as a nil string. This accounts for that. - if payload.CallbackUrl != nil && *payload.CallbackUrl == "" { - payload.CallbackUrl = nil - } - - signature := signatureBts[2+payloadLength:] - if len(signature) != ed25519.SignatureSize { - return nil, nil, errors.New("invalid ed25519 signature length") - } - - return payload, signature, nil -} - -func (n Nep413Authenticator) KeyType() crypto.KeyType { - return crypto.KeyTypeEd25519 -} diff --git a/extensions/auth/nep413_test.go b/extensions/auth/nep413_test.go deleted file mode 100644 index 11f6df4de..000000000 --- a/extensions/auth/nep413_test.go +++ /dev/null @@ -1,109 +0,0 @@ -//go:build auth_nep413 || ext_test - -package auth_test - -import ( - "encoding/base64" - "encoding/hex" - "testing" - - "github.com/kwilteam/kwil-db/extensions/auth" - - borsch "github.com/near/borsh-go" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" -) - -var zeroNonce = [32]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} - -func Test_Nep413(t *testing.T) { - type testCase struct { - name string - message string - nonce [32]byte - recipient string - callbackUrl string - signature string - hexPubkey string - encodeFn func([]byte) string - decodeFn func(string) ([]byte, error) - } - - testCases := []testCase{ - { - name: "meteor wallet", - message: "KlNlZSB5b3VyIGlkT1MgcHJvZmlsZSBJRCoKCkRCSUQ6IHg2MjVhODMyYzg0ZjAyZmJlYmIyMjllZTNiNWU2NmI2NzY3ODAyYjI5ZDg3YWNmNzJiOGRkMDVkMQpBY3Rpb246IGdldF93YWxsZXRfaHVtYW5faWQKUGF5bG9hZERpZ2VzdDogZTkxMjJhNjFlMmY4Njg0NmMyM2ViYjc4ZTQ3OGI0ZjNhMjU3NTRjYgoKS3dpbCDwn5aLCg==", - nonce: zeroNonce, - recipient: "idos.network", - signature: "FtSnvFmYDTYk5nuMo9W0AfPsyIy1Pl4pyttvDWmLBsUH2J1SJU6s1JoJvzjKVf95MRby2kc8+vjvQNLAYRpwCQ==", - hexPubkey: "bcb7c8d4ae100a39d8d39be9443b96e14dcc3764e682ae9fb004afecc1cba33d", // DhgBrU3N1n36MV9rENSaQgc4xprMgh7N2vY4th8kLRZN - }, - { - name: "mynearwallet", - message: "KlNlZSB5b3VyIGlkT1MgcHJvZmlsZSBJRCoKCkRCSUQ6IHg2MjVhODMyYzg0ZjAyZmJlYmIyMjllZTNiNWU2NmI2NzY3ODAyYjI5ZDg3YWNmNzJiOGRkMDVkMQpBY3Rpb246IGdldF93YWxsZXRfaHVtYW5faWQKUGF5bG9hZERpZ2VzdDogZTkxMjJhNjFlMmY4Njg0NmMyM2ViYjc4ZTQ3OGI0ZjNhMjU3NTRjYgoKS3dpbCDwn5aLCg==", - nonce: zeroNonce, - recipient: "idos.network", - callbackUrl: `http://localhost:5173/#accountId=juliosantos-staging.testnet&signature=PYogCMrEnbAr7LSVoOYFAz9wZu1IL4Wtj5TL1A%2BJAa05q4RGhtKX8IpghYvFPIkCbcGjeBe%2Fd7INxpfgFaEcDw%3D%3D&publicKey=ed25519%3ADhgBrU3N1n36MV9rENSaQgc4xprMgh7N2vY4th8kLRZN&`, - signature: "Jf9lg+2ikw+Xnp6pR74K/kazF+KLPzT5QGb+nualZOGZcDXEcC7cRjsN9iUwdtVDWELJaIh1BYVMHmVYC78iAw==", - hexPubkey: "bcb7c8d4ae100a39d8d39be9443b96e14dcc3764e682ae9fb004afecc1cba33d", // DhgBrU3N1n36MV9rENSaQgc4xprMgh7N2vY4th8kLRZN - //encodeFn: func(bts []byte) string { return string(bts) }, - }, - { - name: "plaintext", - message: "*See your idOS profile ID*\n\nDBID: x625a832c84f02fbebb229ee3b5e66b6767802b29d87acf72b8dd05d1\nAction: get_wallet_human_id\nPayloadDigest: e9122a61e2f86846c23ebb78e478b4f3a25754cb\n\nKwil \n", - nonce: zeroNonce, - recipient: "idos.network", - callbackUrl: "http://localhost:5173/", - signature: "PCvQ1VOrm2uZl2gcE9JPni/3j/C4ZU2kTlFZeofWMCRybT+rLuQ2Zuft3Vv5DKise7/zNqzZIA9noGJQ8RHZBg==", - hexPubkey: "bcb7c8d4ae100a39d8d39be9443b96e14dcc3764e682ae9fb004afecc1cba33d", // DhgBrU3N1n36MV9rENSaQgc4xprMgh7N2vY4th8kLRZN - encodeFn: strEncode, - decodeFn: strDecode, - }, - } - - for _, tc := range testCases { - t.Run(tc.name, func(t *testing.T) { - // converting data - sig, err := base64.StdEncoding.DecodeString(tc.signature) - require.NoError(t, err) - - if tc.encodeFn == nil { - tc.encodeFn = base64.StdEncoding.EncodeToString - } - if tc.decodeFn == nil { - tc.decodeFn = base64.StdEncoding.DecodeString - } - - pubKey, err := hex.DecodeString(tc.hexPubkey) - require.NoError(t, err) - - msgBts, err := tc.decodeFn(tc.message) - require.NoError(t, err) - - serializedPayload, err := borsch.Serialize(auth.Nep413Payload{ - Nonce: tc.nonce, - Recipient: tc.recipient, - CallbackUrl: &tc.callbackUrl, - }) - require.NoError(t, err) - - payloadLen := len(serializedPayload) - // convert to uint16 - payloadLenBytes := []byte{byte(payloadLen >> 8), byte(payloadLen)} - - // test - err = auth.Nep413Authenticator{ - MsgEncoder: tc.encodeFn, - }.Verify(pubKey, msgBts, append(payloadLenBytes, append(serializedPayload, sig...)...)) - assert.NoError(t, err, "signature should be valid") - }) - } -} - -func strEncode(bts []byte) string { - return string(bts) -} - -func strDecode(str string) ([]byte, error) { - return []byte(str), nil -} diff --git a/go.mod b/go.mod index 0370b6d5c..5eafd6b86 100644 --- a/go.mod +++ b/go.mod @@ -25,7 +25,6 @@ require ( github.com/manifoldco/promptui v0.9.0 github.com/multiformats/go-multiaddr v0.14.0 github.com/multiformats/go-multistream v0.6.0 - github.com/near/borsh-go v0.3.1 github.com/olekukonko/tablewriter v0.0.5 github.com/pelletier/go-toml/v2 v2.2.3 github.com/prometheus/client_golang v1.20.5 @@ -33,6 +32,7 @@ require ( github.com/spf13/pflag v1.0.5 github.com/stretchr/testify v1.10.0 golang.org/x/sync v0.10.0 + golang.org/x/term v0.26.0 golang.org/x/time v0.8.0 ) diff --git a/go.sum b/go.sum index 382e6c879..cfcfecd2d 100644 --- a/go.sum +++ b/go.sum @@ -429,8 +429,6 @@ github.com/multiformats/go-varint v0.0.7 h1:sWSGR+f/eu5ABZA2ZpYKBILXTTs9JWpdEM/n github.com/multiformats/go-varint v0.0.7/go.mod h1:r8PUYw/fD/SjBCiKOoDlGF6QawOELpZAu9eioSos/OU= github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA= github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ= -github.com/near/borsh-go v0.3.1 h1:ukNbhJlPKxfua0/nIuMZhggSU8zvtRP/VyC25LLqPUA= -github.com/near/borsh-go v0.3.1/go.mod h1:NeMochZp7jN/pYFuxLkrZtmLqbADmnp/y1+/dL+AsyQ= github.com/neelance/astrewrite v0.0.0-20160511093645-99348263ae86/go.mod h1:kHJEU3ofeGjhHklVoIGuVj85JJwZ6kWPaJwCIxgnFmo= github.com/neelance/sourcemap v0.0.0-20151028013722-8c68805598ab/go.mod h1:Qr6/a/Q4r9LP1IltGz7tA7iOK1WonHEYhu1HRBA7ZiM= github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec= @@ -751,6 +749,8 @@ golang.org/x/term v0.7.0/go.mod h1:P32HKFT3hSsZrRxla30E9HqToFYAQPCMs/zFMBUFqPY= golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo= golang.org/x/term v0.11.0/go.mod h1:zC9APTIj3jG3FdV/Ons+XE1riIZXG4aZ4GTHiPZJPIU= golang.org/x/term v0.16.0/go.mod h1:yn7UURbUtPyrVJPGPq404EukNFxcm/foM+bV/bfcDsY= +golang.org/x/term v0.26.0 h1:WEQa6V3Gja/BhNxg540hBip/kkaYtRg3cxg4oXSw4AU= +golang.org/x/term v0.26.0/go.mod h1:Si5m1o57C5nBNQo5z1iq+XDijt21BDBDp2bK0QI8e3E= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= diff --git a/node/engine/functions.go b/node/engine/functions.go index 40ade78ef..2cfaa997d 100644 --- a/node/engine/functions.go +++ b/node/engine/functions.go @@ -189,27 +189,6 @@ var ( }, PGFormatFunc: defaultFormat("digest"), }, - "generate_dbid": &ScalarFunctionDefinition{ - ValidateArgsFunc: func(args []*types.DataType) (*types.DataType, error) { - // first should be text, second should be blob - if len(args) != 2 { - return nil, wrapErrArgumentNumber(2, len(args)) - } - - if !args[0].Equals(types.TextType) { - return nil, wrapErrArgumentType(types.TextType, args[0]) - } - - if !args[1].Equals(types.ByteaType) { - return nil, wrapErrArgumentType(types.ByteaType, args[1]) - } - - return types.TextType, nil - }, - PGFormatFunc: func(inputs []string) (string, error) { - return fmt.Sprintf(`(select 'x' || encode(sha224(lower(%s)::bytea || %s), 'hex'))`, inputs[0], inputs[1]), nil - }, - }, // array functions "array_append": &ScalarFunctionDefinition{ ValidateArgsFunc: func(args []*types.DataType) (*types.DataType, error) { diff --git a/node/engine/planner/logical/nodes.go b/node/engine/planner/logical/nodes.go index 25ccb3b7e..05df4c060 100644 --- a/node/engine/planner/logical/nodes.go +++ b/node/engine/planner/logical/nodes.go @@ -178,7 +178,7 @@ func (f *ProcedureScanSource) FormatScan() string { str.WriteString(strconv.FormatBool(f.IsForeign)) str.WriteString("] ") if f.IsForeign { - str.WriteString("[dbid=") + str.WriteString("[namespace=") str.WriteString(f.ContextualArgs[0].String()) str.WriteString("] ") str.WriteString("[proc=") diff --git a/node/services/jsonrpc/usersvc/service.go b/node/services/jsonrpc/usersvc/service.go index 672020939..8b51de1c9 100644 --- a/node/services/jsonrpc/usersvc/service.go +++ b/node/services/jsonrpc/usersvc/service.go @@ -618,9 +618,6 @@ func unmarshalActionCall(req *userjson.CallRequest) (*types.ActionCall, *types.C cm := *req - // sigtxt := transactions.CallSigText(actionPayload.DBID, actionPayload.Action, - // req.Body.Payload, req.Body.Challenge) - return &actionPayload, &cm, nil } @@ -651,7 +648,7 @@ func (svc *Service) Call(ctx context.Context, req *userjson.CallRequest) (*userj return nil, jsonrpc.NewError(jsonrpc.ErrorInvalidParams, "failed to convert action call: "+err.Error(), nil) } - if jsonRPCErr := svc.authenticate(msg, types.CallSigText(body.DBID, body.Action, + if jsonRPCErr := svc.authenticate(msg, types.CallSigText(body.Namespace, body.Action, msg.Body.Payload, msg.Body.Challenge)); jsonRPCErr != nil { return nil, jsonRPCErr } @@ -682,7 +679,7 @@ func (svc *Service) Call(ctx context.Context, req *userjson.CallRequest) (*userj defer readTx.Rollback(ctx) r := &rowReader{} - callRes, err := svc.engine.Call(&common.EngineContext{TxContext: txContext}, readTx, body.DBID, body.Action, args, r.read) + callRes, err := svc.engine.Call(&common.EngineContext{TxContext: txContext}, readTx, body.Namespace, body.Action, args, r.read) if err != nil { return nil, engineError(err) } diff --git a/node/txapp/routes.go b/node/txapp/routes.go index 2500afa5c..07e1beb46 100644 --- a/node/txapp/routes.go +++ b/node/txapp/routes.go @@ -291,7 +291,7 @@ func (d *executeActionRoute) PreTx(ctx *common.TxContext, svc *common.Service, t } d.action = action.Action - d.namespace = action.DBID + d.namespace = action.Namespace // here, we decode the [][]types.EncodedTypes into [][]any d.args = make([][]any, len(action.Arguments)) diff --git a/test/_old/driver/cli_driver.go b/test/_old/driver/cli_driver.go index 11cfe09f4..f73ce868f 100644 --- a/test/_old/driver/cli_driver.go +++ b/test/_old/driver/cli_driver.go @@ -209,18 +209,18 @@ func (d *KwilCliDriver) prepareCliActionParams(ctx context.Context, namespace st return args, nil } -func (d *KwilCliDriver) Execute(ctx context.Context, dbid string, action string, inputs ...[]any) (types.Hash, error) { +func (d *KwilCliDriver) Execute(ctx context.Context, namespace string, action string, inputs ...[]any) (types.Hash, error) { if len(inputs) > 1 { return types.Hash{}, fmt.Errorf("kwil-cli does not support batched inputs") } // NOTE: kwil-cli does not support batched inputs - actionInputs, err := d.prepareCliActionParams(ctx, dbid, action, inputs[0]) + actionInputs, err := d.prepareCliActionParams(ctx, namespace, action, inputs[0]) if err != nil { return types.Hash{}, fmt.Errorf("failed to get action params: %w", err) } - args := []string{"database", "execute", action, "--dbid", dbid} + args := []string{"database", "execute", action, "--namespace", namespace} args = append(args, actionInputs...) cmd := d.newKwilCliCmd(args...) diff --git a/test/go.mod b/test/go.mod index 3f857a591..cf06e973a 100644 --- a/test/go.mod +++ b/test/go.mod @@ -15,7 +15,6 @@ require ( github.com/kwilteam/kwil-db v0.0.0-00010101000000-000000000000 github.com/kwilteam/kwil-db/core v0.3.1-0.20241212163115-7353f2761884 github.com/stretchr/testify v1.10.0 - github.com/spf13/cobra v1.8.1 github.com/testcontainers/testcontainers-go v0.34.0 github.com/testcontainers/testcontainers-go/modules/compose v0.34.0 ) @@ -234,7 +233,6 @@ require ( github.com/multiformats/go-varint v0.0.7 // indirect github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f // indirect - github.com/near/borsh-go v0.3.1 // indirect github.com/olekukonko/tablewriter v0.0.5 // indirect github.com/onsi/ginkgo/v2 v2.20.2 // indirect github.com/opencontainers/go-digest v1.0.0 // indirect @@ -284,6 +282,7 @@ require ( github.com/sirupsen/logrus v1.9.3 // indirect github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966 // indirect github.com/spaolacci/murmur3 v1.1.0 // indirect + github.com/spf13/cobra v1.8.1 // indirect github.com/spf13/pflag v1.0.5 // indirect github.com/supranational/blst v0.3.13 // indirect github.com/theupdateframework/notary v0.7.0 // indirect diff --git a/test/go.sum b/test/go.sum index cb5db9030..f6db379ec 100644 --- a/test/go.sum +++ b/test/go.sum @@ -706,8 +706,6 @@ github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8m github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f h1:y5//uYreIhSUg3J1GEMiLbxo1LJaP8RfCpH6pymGZus= github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f/go.mod h1:ZdcZmHo+o7JKHSa8/e818NopupXU1YMK5fe1lsApnBw= -github.com/near/borsh-go v0.3.1 h1:ukNbhJlPKxfua0/nIuMZhggSU8zvtRP/VyC25LLqPUA= -github.com/near/borsh-go v0.3.1/go.mod h1:NeMochZp7jN/pYFuxLkrZtmLqbADmnp/y1+/dL+AsyQ= github.com/neelance/astrewrite v0.0.0-20160511093645-99348263ae86/go.mod h1:kHJEU3ofeGjhHklVoIGuVj85JJwZ6kWPaJwCIxgnFmo= github.com/neelance/sourcemap v0.0.0-20151028013722-8c68805598ab/go.mod h1:Qr6/a/Q4r9LP1IltGz7tA7iOK1WonHEYhu1HRBA7ZiM= github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=