Skip to content

Commit

Permalink
OG now can connect to mariadb (#1521)
Browse files Browse the repository at this point in the history
* OG now can connect to mariadb

* fix string

* fix port

* lint

* add flag
  • Loading branch information
otherview authored Sep 15, 2023
1 parent 8768381 commit 40d58aa
Show file tree
Hide file tree
Showing 9 changed files with 165 additions and 11 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/manual-deploy-obscuro-gateway.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ jobs:
location: 'uksouth'
restart-policy: 'Never'
environment-variables: PORT=80
command-line: ./wallet_extension_linux -host=0.0.0.0 -port=80 -portWS=81 -nodeHost=${{ env.OBSCURO_GATEWAY_NODE_HOST }}
ports: 81 80
command-line: ./wallet_extension_linux -host=0.0.0.0 -port=80 -portWS=81 -nodeHost=${{ env.OBSCURO_GATEWAY_NODE_HOST }} -dbType=mariaDB -dbConnectionURL=obscurouser:${{ secrets.OBSCURO_GATEWAY_MARIADB_USER_PWD }}@tcp(obscurogateway-mariadb-${{ github.event.inputs.testnet_type }}.uksouth.cloudapp.azure.com:3306)/ogdb
ports: 80 81
cpu: 2
memory: 2
2 changes: 2 additions & 0 deletions tools/walletextension/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ type Config struct {
LogPath string
DBPathOverride string // Overrides the database file location. Used in tests.
VerboseFlag bool
DBType string
DBConnectionURL string
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func NewWalletExtensionContainerFromConfig(config config.Config, logger gethlog.
userAccountManager := useraccountmanager.NewUserAccountManager(unAuthedClient, logger)

// start the database
databaseStorage, err := storage.New(config.DBPathOverride)
databaseStorage, err := storage.New(config.DBType, config.DBConnectionURL, config.DBPathOverride)
if err != nil {
logger.Crit("unable to create database to store viewing keys ", log.ErrKey, err)
}
Expand Down
12 changes: 12 additions & 0 deletions tools/walletextension/main/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ const (
verboseFlagName = "verbose"
verboseFlagDefault = false
verboseFlagUsage = "Flag to enable verbose logging of wallet extension traffic"

dbTypeFlagName = "dbType"
dbTypeFlagDefault = "sqlite"
dbTypeFlagUsage = "Defined the db type (sqlite or mariaDB)"

dbConnectionURLFlagName = "dbConnectionURL"
dbConnectionURLFlagDefault = ""
dbConnectionURLFlagUsage = "If dbType is set to mariaDB, this must be set. ex: obscurouser:password@tcp(127.0.0.1:3306)/ogdb"
)

func parseCLIArgs() config.Config {
Expand All @@ -55,6 +63,8 @@ func parseCLIArgs() config.Config {
logPath := flag.String(logPathName, logPathDefault, logPathUsage)
databasePath := flag.String(databasePathName, databasePathDefault, databasePathUsage)
verboseFlag := flag.Bool(verboseFlagName, verboseFlagDefault, verboseFlagUsage)
dbType := flag.String(dbTypeFlagName, dbTypeFlagDefault, dbTypeFlagUsage)
dbConnectionURL := flag.String(dbConnectionURLFlagName, dbConnectionURLFlagDefault, dbConnectionURLFlagUsage)
flag.Parse()

return config.Config{
Expand All @@ -66,5 +76,7 @@ func parseCLIArgs() config.Config {
LogPath: *logPath,
DBPathOverride: *databasePath,
VerboseFlag: *verboseFlag,
DBType: *dbType,
DBConnectionURL: *dbConnectionURL,
}
}
10 changes: 5 additions & 5 deletions tools/walletextension/storage/database/001_init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ USE ogdb;
GRANT SELECT, INSERT, UPDATE, DELETE ON ogdb.* TO 'obscurouser';

CREATE TABLE IF NOT EXISTS ogdb.users (
user_id binary(32) PRIMARY KEY,
private_key binary(32)
user_id varbinary(32) PRIMARY KEY,
private_key varbinary(32)
);
CREATE TABLE IF NOT EXISTS ogdb.accounts (
user_id binary(32),
account_address binary(20),
signature binary(65),
user_id varbinary(32),
account_address varbinary(20),
signature varbinary(65),
FOREIGN KEY(user_id) REFERENCES users(user_id) ON DELETE CASCADE
);
130 changes: 130 additions & 0 deletions tools/walletextension/storage/database/mariadb.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package database

import (
"database/sql"
"fmt"

_ "github.com/go-sql-driver/mysql" // Importing MariaDB driver
"github.com/obscuronet/go-obscuro/go/common/errutil"
"github.com/obscuronet/go-obscuro/tools/walletextension/common"
)

type MariaDB struct {
db *sql.DB
}

// NewMariaDB creates a new MariaDB connection instance
func NewMariaDB(dbURL string) (*MariaDB, error) {
db, err := sql.Open("mysql", dbURL)
if err != nil {
return nil, fmt.Errorf("failed to connect to database: %w", err)
}

return &MariaDB{db: db}, nil
}

func (m *MariaDB) AddUser(userID []byte, privateKey []byte) error {
stmt, err := m.db.Prepare("REPLACE INTO users(user_id, private_key) VALUES (?, ?)")
if err != nil {
return err
}
defer stmt.Close()

_, err = stmt.Exec(userID, privateKey)
if err != nil {
return err
}

return nil
}

func (m *MariaDB) DeleteUser(userID []byte) error {
stmt, err := m.db.Prepare("DELETE FROM users WHERE user_id = ?")
if err != nil {
return err
}
defer stmt.Close()

_, err = stmt.Exec(userID)
if err != nil {
return err
}

return nil
}

func (m *MariaDB) GetUserPrivateKey(userID []byte) ([]byte, error) {
var privateKey []byte
err := m.db.QueryRow("SELECT private_key FROM users WHERE user_id = ?", userID).Scan(&privateKey)
if err != nil {
if err == sql.ErrNoRows {
// No rows found for the given userID
return nil, errutil.ErrNotFound
}
return nil, err
}

return privateKey, nil
}

func (m *MariaDB) AddAccount(userID []byte, accountAddress []byte, signature []byte) error {
stmt, err := m.db.Prepare("INSERT INTO accounts(user_id, account_address, signature) VALUES (?, ?, ?)")
if err != nil {
return err
}
defer stmt.Close()

res, err := stmt.Exec(userID, accountAddress, signature)
if err != nil {
return err
}
fmt.Println(res)

return nil
}

func (m *MariaDB) GetAccounts(userID []byte) ([]common.AccountDB, error) {
rows, err := m.db.Query("SELECT account_address, signature FROM accounts WHERE user_id = ?", userID)
if err != nil {
return nil, err
}
defer rows.Close()

var accounts []common.AccountDB
for rows.Next() {
var account common.AccountDB
if err := rows.Scan(&account.AccountAddress, &account.Signature); err != nil {
return nil, err
}
accounts = append(accounts, account)
}
if err := rows.Err(); err != nil {
return nil, err
}

return accounts, nil
}

func (m *MariaDB) GetAllUsers() ([]common.UserDB, error) {
rows, err := m.db.Query("SELECT user_id, private_key FROM users")
if err != nil {
return nil, err
}
defer rows.Close()

var users []common.UserDB
for rows.Next() {
var user common.UserDB
err = rows.Scan(&user.UserID, &user.PrivateKey)
if err != nil {
return nil, err
}
users = append(users, user)
}

if err = rows.Err(); err != nil {
return nil, err
}

return users, nil
}
12 changes: 10 additions & 2 deletions tools/walletextension/storage/storage.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package storage

import (
"fmt"

"github.com/obscuronet/go-obscuro/tools/walletextension/common"
"github.com/obscuronet/go-obscuro/tools/walletextension/storage/database"
)
Expand All @@ -14,6 +16,12 @@ type Storage interface {
GetAllUsers() ([]common.UserDB, error)
}

func New(dbPath string) (Storage, error) {
return database.NewSqliteDatabase(dbPath)
func New(dbType string, dbConnectionURL, dbPath string) (Storage, error) {
switch dbType {
case "mariaDB":
return database.NewMariaDB(dbConnectionURL)
case "sqlite":
return database.NewSqliteDatabase(dbPath)
}
return nil, fmt.Errorf("unknown db %s", dbType)
}
3 changes: 2 additions & 1 deletion tools/walletextension/storage/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ var tests = map[string]func(storage Storage, t *testing.T){
func TestSQLiteGatewayDB(t *testing.T) {
for name, test := range tests {
t.Run(name, func(t *testing.T) {
storage, err := New("")
// storage, err := New("mariaDB", "obscurouser:password@tcp(127.0.0.1:3306)/ogdb", "") allows to run tests against a local instance of MariaDB
storage, err := New("sqlite", "", "")
require.NoError(t, err)

test(storage, t)
Expand Down
1 change: 1 addition & 0 deletions tools/walletextension/test/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func createWalExtCfg(connectPort, wallHTTPPort, wallWSPort int) *config.Config {
DBPathOverride: testDBPath.Name(),
WalletExtensionPortHTTP: wallHTTPPort,
WalletExtensionPortWS: wallWSPort,
DBType: "sqlite",
}
}

Expand Down

0 comments on commit 40d58aa

Please sign in to comment.