feat: add address to permission (#2571)

This commit is contained in:
Anthony Laibe 2022-03-14 13:38:12 +01:00 committed by GitHub
parent b4fb9be2cb
commit 9c16cedb1e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 544 additions and 417 deletions

View File

@ -1,18 +1,19 @@
// Code generated by go-bindata. // Code generated by go-bindata. DO NOT EDIT.
// sources: // sources:
// 1640111208_dummy.up.sql // 1640111208_dummy.up.sql (258B)
// 1642666031_add_removed_clock_to_bookmarks.up.sql // 1642666031_add_removed_clock_to_bookmarks.up.sql (117B)
// 1643644541_gif_api_key_setting.up.sql // 1643644541_gif_api_key_setting.up.sql (108B)
// 1644188994_recent_stickers.up.sql // 1644188994_recent_stickers.up.sql (79B)
// 1646841105_add_emoji_account.up.sql // 1646659233_add_address_to_dapp_permisssion.up.sql (700B)
// doc.go // 1646841105_add_emoji_account.up.sql (96B)
// DO NOT EDIT! // doc.go (74B)
package migrations package migrations
import ( import (
"bytes" "bytes"
"compress/gzip" "compress/gzip"
"crypto/sha256"
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
@ -25,7 +26,7 @@ import (
func bindataRead(data []byte, name string) ([]byte, error) { func bindataRead(data []byte, name string) ([]byte, error) {
gz, err := gzip.NewReader(bytes.NewBuffer(data)) gz, err := gzip.NewReader(bytes.NewBuffer(data))
if err != nil { if err != nil {
return nil, fmt.Errorf("Read %q: %v", name, err) return nil, fmt.Errorf("read %q: %w", name, err)
} }
var buf bytes.Buffer var buf bytes.Buffer
@ -33,7 +34,7 @@ func bindataRead(data []byte, name string) ([]byte, error) {
clErr := gz.Close() clErr := gz.Close()
if err != nil { if err != nil {
return nil, fmt.Errorf("Read %q: %v", name, err) return nil, fmt.Errorf("read %q: %w", name, err)
} }
if clErr != nil { if clErr != nil {
return nil, err return nil, err
@ -43,8 +44,9 @@ func bindataRead(data []byte, name string) ([]byte, error) {
} }
type asset struct { type asset struct {
bytes []byte bytes []byte
info os.FileInfo info os.FileInfo
digest [sha256.Size]byte
} }
type bindataFileInfo struct { type bindataFileInfo struct {
@ -88,8 +90,8 @@ func _1640111208_dummyUpSql() (*asset, error) {
return nil, err return nil, err
} }
info := bindataFileInfo{name: "1640111208_dummy.up.sql", size: 258, mode: os.FileMode(436), modTime: time.Unix(1646839963, 0)} info := bindataFileInfo{name: "1640111208_dummy.up.sql", size: 258, mode: os.FileMode(0664), modTime: time.Unix(1647246959, 0)}
a := &asset{bytes: bytes, info: info} a := &asset{bytes: bytes, info: info, digest: [32]uint8{0x3e, 0xf0, 0xae, 0x20, 0x6e, 0x75, 0xd1, 0x36, 0x14, 0xf2, 0x40, 0xe5, 0xd6, 0x7a, 0xc4, 0xa5, 0x72, 0xaa, 0xb5, 0x4d, 0x71, 0x97, 0xb8, 0xe8, 0x95, 0x22, 0x95, 0xa2, 0xac, 0xaf, 0x48, 0x58}}
return a, nil return a, nil
} }
@ -108,8 +110,8 @@ func _1642666031_add_removed_clock_to_bookmarksUpSql() (*asset, error) {
return nil, err return nil, err
} }
info := bindataFileInfo{name: "1642666031_add_removed_clock_to_bookmarks.up.sql", size: 117, mode: os.FileMode(436), modTime: time.Unix(1646839963, 0)} info := bindataFileInfo{name: "1642666031_add_removed_clock_to_bookmarks.up.sql", size: 117, mode: os.FileMode(0664), modTime: time.Unix(1647246959, 0)}
a := &asset{bytes: bytes, info: info} a := &asset{bytes: bytes, info: info, digest: [32]uint8{0x84, 0x4e, 0x38, 0x99, 0x7a, 0xc, 0x90, 0x13, 0xec, 0xfe, 0x2f, 0x55, 0xff, 0xb7, 0xb6, 0xaa, 0x96, 0xc6, 0x92, 0x79, 0xcc, 0xee, 0x4e, 0x99, 0x53, 0xfe, 0x1c, 0xbb, 0x32, 0x2, 0xa4, 0x27}}
return a, nil return a, nil
} }
@ -128,8 +130,8 @@ func _1643644541_gif_api_key_settingUpSql() (*asset, error) {
return nil, err return nil, err
} }
info := bindataFileInfo{name: "1643644541_gif_api_key_setting.up.sql", size: 108, mode: os.FileMode(436), modTime: time.Unix(1646839963, 0)} info := bindataFileInfo{name: "1643644541_gif_api_key_setting.up.sql", size: 108, mode: os.FileMode(0664), modTime: time.Unix(1647246959, 0)}
a := &asset{bytes: bytes, info: info} a := &asset{bytes: bytes, info: info, digest: [32]uint8{0x1b, 0x94, 0x28, 0xfb, 0x66, 0xd1, 0x7c, 0xb8, 0x89, 0xe2, 0xb4, 0x71, 0x65, 0x24, 0x57, 0x22, 0x95, 0x38, 0x97, 0x3, 0x9b, 0xc6, 0xa4, 0x41, 0x7b, 0xba, 0xf7, 0xdb, 0x70, 0xf7, 0x20, 0x3a}}
return a, nil return a, nil
} }
@ -148,8 +150,28 @@ func _1644188994_recent_stickersUpSql() (*asset, error) {
return nil, err return nil, err
} }
info := bindataFileInfo{name: "1644188994_recent_stickers.up.sql", size: 79, mode: os.FileMode(436), modTime: time.Unix(1646839963, 0)} info := bindataFileInfo{name: "1644188994_recent_stickers.up.sql", size: 79, mode: os.FileMode(0664), modTime: time.Unix(1647246959, 0)}
a := &asset{bytes: bytes, info: info} a := &asset{bytes: bytes, info: info, digest: [32]uint8{0x1e, 0xad, 0xaa, 0x30, 0xbf, 0x4, 0x7, 0xf8, 0xc3, 0x3, 0xb8, 0x97, 0x23, 0x2b, 0xbd, 0x1c, 0x60, 0x69, 0xb0, 0x42, 0x5e, 0x6b, 0xd, 0xa7, 0xa3, 0x6b, 0x2e, 0xdc, 0x70, 0x13, 0x72, 0x7}}
return a, nil
}
var __1646659233_add_address_to_dapp_permisssionUpSql = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x7c\x51\xcd\x6e\x82\x40\x18\xbc\xf3\x14\x13\x4e\x98\x10\x0f\x5e\x49\x0f\x5b\xfc\x30\x9b\xc2\xae\x5d\x96\x44\x4f\x84\x14\x4c\xb6\x51\xb0\xa2\xef\xdf\xb0\xfc\x08\x36\xe9\x0d\xe6\x9b\x19\x66\x86\x50\x11\xd3\x04\xcd\xde\x63\x02\x8f\x20\xa4\x06\x1d\x78\xaa\x53\x94\xc5\xf5\xda\xe6\x1b\x78\x0e\x00\x98\x12\x5c\x68\xda\x91\xc2\x5e\xf1\x84\xa9\x23\x3e\xe8\x08\x96\x69\xc9\x45\xa8\x28\x21\xa1\xad\x5a\x64\x71\xec\x5b\x49\x5d\x5c\x2a\x68\x3a\xe8\xfe\xb5\x28\xcb\x5b\xd5\xb6\x16\x99\x98\x28\xab\x53\xf1\x38\xdf\xe1\xba\x3d\x2b\x94\x22\xd5\x8a\x71\xa1\xf1\xa8\xcd\xcf\xa3\xca\xfb\x1c\x9d\x59\x3e\x5a\x64\x82\x7f\x66\x04\xaf\x03\xfd\x01\x5c\x39\xab\xc0\x71\xfe\xe9\x73\xad\x6e\x17\xd3\xb6\xa6\xa9\x9f\xad\x3a\xef\xdc\x94\x30\xf5\xfd\x25\xfc\x93\xbd\x0c\xdc\x5f\x23\xa9\x88\xef\x44\x37\x81\x37\x78\xac\xa0\x28\x22\x45\x22\xa4\x69\x3b\xaf\x83\xa5\xc0\x96\x62\xd2\x84\x90\xa5\x21\xdb\x92\x0d\xca\x45\x4a\x4a\x77\x9b\xca\x69\xe9\x94\x62\x0a\x87\xcf\xd8\xf5\x7c\xb8\x2e\x22\x25\x93\x9e\xf2\x22\x5b\x16\x6a\xab\x73\xf5\x75\x1f\xbd\xd6\xa6\xf4\xe7\x84\xf5\xac\xcf\xe9\xd6\x5c\xe6\x37\x7c\x37\xa6\x9e\x42\x34\xd3\xe3\xda\xfe\xc0\xb7\x85\x8d\x2d\xdb\xe1\x81\xe3\x6c\x95\xdc\x0f\x4b\xcf\x28\xc1\x1c\x1f\x63\xb3\x58\x93\xfa\xcb\xcd\x37\x50\x24\x58\x42\x58\xd6\x09\x16\x82\x31\xd9\x93\xda\xdb\xfe\x06\x00\x00\xff\xff\xb0\x41\x9a\x48\xbc\x02\x00\x00")
func _1646659233_add_address_to_dapp_permisssionUpSqlBytes() ([]byte, error) {
return bindataRead(
__1646659233_add_address_to_dapp_permisssionUpSql,
"1646659233_add_address_to_dapp_permisssion.up.sql",
)
}
func _1646659233_add_address_to_dapp_permisssionUpSql() (*asset, error) {
bytes, err := _1646659233_add_address_to_dapp_permisssionUpSqlBytes()
if err != nil {
return nil, err
}
info := bindataFileInfo{name: "1646659233_add_address_to_dapp_permisssion.up.sql", size: 700, mode: os.FileMode(0664), modTime: time.Unix(1647247370, 0)}
a := &asset{bytes: bytes, info: info, digest: [32]uint8{0xed, 0xb0, 0x35, 0xcc, 0x2e, 0x16, 0xe6, 0x15, 0x86, 0x2c, 0x37, 0x80, 0xae, 0xa3, 0xc5, 0x31, 0x78, 0x5, 0x9d, 0xcd, 0x7b, 0xeb, 0x5f, 0xf2, 0xb3, 0x74, 0x72, 0xdf, 0xcf, 0x88, 0xb, 0x40}}
return a, nil return a, nil
} }
@ -168,8 +190,8 @@ func _1646841105_add_emoji_accountUpSql() (*asset, error) {
return nil, err return nil, err
} }
info := bindataFileInfo{name: "1646841105_add_emoji_account.up.sql", size: 96, mode: os.FileMode(436), modTime: time.Unix(1646839402, 0)} info := bindataFileInfo{name: "1646841105_add_emoji_account.up.sql", size: 96, mode: os.FileMode(0664), modTime: time.Unix(1647247370, 0)}
a := &asset{bytes: bytes, info: info} a := &asset{bytes: bytes, info: info, digest: [32]uint8{0xe6, 0x77, 0x29, 0x95, 0x18, 0x64, 0x82, 0x63, 0xe7, 0xaf, 0x6c, 0xa9, 0x15, 0x7d, 0x46, 0xa6, 0xbc, 0xdf, 0xa7, 0xd, 0x2b, 0xd2, 0x2d, 0x97, 0x4d, 0xa, 0x6b, 0xd, 0x6e, 0x90, 0x42, 0x5c}}
return a, nil return a, nil
} }
@ -188,8 +210,8 @@ func docGo() (*asset, error) {
return nil, err return nil, err
} }
info := bindataFileInfo{name: "doc.go", size: 74, mode: os.FileMode(436), modTime: time.Unix(1646818623, 0)} info := bindataFileInfo{name: "doc.go", size: 74, mode: os.FileMode(0664), modTime: time.Unix(1647246959, 0)}
a := &asset{bytes: bytes, info: info} a := &asset{bytes: bytes, info: info, digest: [32]uint8{0xde, 0x7c, 0x28, 0xcd, 0x47, 0xf2, 0xfa, 0x7c, 0x51, 0x2d, 0xd8, 0x38, 0xb, 0xb0, 0x34, 0x9d, 0x4c, 0x62, 0xa, 0x9e, 0x28, 0xc3, 0x31, 0x23, 0xd9, 0xbb, 0x89, 0x9f, 0xa0, 0x89, 0x1f, 0xe8}}
return a, nil return a, nil
} }
@ -197,8 +219,8 @@ func docGo() (*asset, error) {
// It returns an error if the asset could not be found or // It returns an error if the asset could not be found or
// could not be loaded. // could not be loaded.
func Asset(name string) ([]byte, error) { func Asset(name string) ([]byte, error) {
cannonicalName := strings.Replace(name, "\\", "/", -1) canonicalName := strings.Replace(name, "\\", "/", -1)
if f, ok := _bindata[cannonicalName]; ok { if f, ok := _bindata[canonicalName]; ok {
a, err := f() a, err := f()
if err != nil { if err != nil {
return nil, fmt.Errorf("Asset %s can't read by error: %v", name, err) return nil, fmt.Errorf("Asset %s can't read by error: %v", name, err)
@ -208,6 +230,12 @@ func Asset(name string) ([]byte, error) {
return nil, fmt.Errorf("Asset %s not found", name) return nil, fmt.Errorf("Asset %s not found", name)
} }
// AssetString returns the asset contents as a string (instead of a []byte).
func AssetString(name string) (string, error) {
data, err := Asset(name)
return string(data), err
}
// MustAsset is like Asset but panics when Asset would return an error. // MustAsset is like Asset but panics when Asset would return an error.
// It simplifies safe initialization of global variables. // It simplifies safe initialization of global variables.
func MustAsset(name string) []byte { func MustAsset(name string) []byte {
@ -219,12 +247,18 @@ func MustAsset(name string) []byte {
return a return a
} }
// MustAssetString is like AssetString but panics when Asset would return an
// error. It simplifies safe initialization of global variables.
func MustAssetString(name string) string {
return string(MustAsset(name))
}
// AssetInfo loads and returns the asset info for the given name. // AssetInfo loads and returns the asset info for the given name.
// It returns an error if the asset could not be found or // It returns an error if the asset could not be found or
// could not be loaded. // could not be loaded.
func AssetInfo(name string) (os.FileInfo, error) { func AssetInfo(name string) (os.FileInfo, error) {
cannonicalName := strings.Replace(name, "\\", "/", -1) canonicalName := strings.Replace(name, "\\", "/", -1)
if f, ok := _bindata[cannonicalName]; ok { if f, ok := _bindata[canonicalName]; ok {
a, err := f() a, err := f()
if err != nil { if err != nil {
return nil, fmt.Errorf("AssetInfo %s can't read by error: %v", name, err) return nil, fmt.Errorf("AssetInfo %s can't read by error: %v", name, err)
@ -234,6 +268,33 @@ func AssetInfo(name string) (os.FileInfo, error) {
return nil, fmt.Errorf("AssetInfo %s not found", name) return nil, fmt.Errorf("AssetInfo %s not found", name)
} }
// AssetDigest returns the digest of the file with the given name. It returns an
// error if the asset could not be found or the digest could not be loaded.
func AssetDigest(name string) ([sha256.Size]byte, error) {
canonicalName := strings.Replace(name, "\\", "/", -1)
if f, ok := _bindata[canonicalName]; ok {
a, err := f()
if err != nil {
return [sha256.Size]byte{}, fmt.Errorf("AssetDigest %s can't read by error: %v", name, err)
}
return a.digest, nil
}
return [sha256.Size]byte{}, fmt.Errorf("AssetDigest %s not found", name)
}
// Digests returns a map of all known files and their checksums.
func Digests() (map[string][sha256.Size]byte, error) {
mp := make(map[string][sha256.Size]byte, len(_bindata))
for name := range _bindata {
a, err := _bindata[name]()
if err != nil {
return nil, err
}
mp[name] = a.digest
}
return mp, nil
}
// AssetNames returns the names of the assets. // AssetNames returns the names of the assets.
func AssetNames() []string { func AssetNames() []string {
names := make([]string, 0, len(_bindata)) names := make([]string, 0, len(_bindata))
@ -245,14 +306,18 @@ func AssetNames() []string {
// _bindata is a table, holding each asset generator, mapped to its name. // _bindata is a table, holding each asset generator, mapped to its name.
var _bindata = map[string]func() (*asset, error){ var _bindata = map[string]func() (*asset, error){
"1640111208_dummy.up.sql": _1640111208_dummyUpSql, "1640111208_dummy.up.sql": _1640111208_dummyUpSql,
"1642666031_add_removed_clock_to_bookmarks.up.sql": _1642666031_add_removed_clock_to_bookmarksUpSql, "1642666031_add_removed_clock_to_bookmarks.up.sql": _1642666031_add_removed_clock_to_bookmarksUpSql,
"1643644541_gif_api_key_setting.up.sql": _1643644541_gif_api_key_settingUpSql, "1643644541_gif_api_key_setting.up.sql": _1643644541_gif_api_key_settingUpSql,
"1644188994_recent_stickers.up.sql": _1644188994_recent_stickersUpSql, "1644188994_recent_stickers.up.sql": _1644188994_recent_stickersUpSql,
"1646841105_add_emoji_account.up.sql": _1646841105_add_emoji_accountUpSql, "1646659233_add_address_to_dapp_permisssion.up.sql": _1646659233_add_address_to_dapp_permisssionUpSql,
"doc.go": docGo, "1646841105_add_emoji_account.up.sql": _1646841105_add_emoji_accountUpSql,
"doc.go": docGo,
} }
// AssetDebug is true if the assets were built with the debug flag enabled.
const AssetDebug = false
// AssetDir returns the file names below a certain // AssetDir returns the file names below a certain
// directory embedded in the file by go-bindata. // directory embedded in the file by go-bindata.
// For example if you run go-bindata on data/... and data contains the // For example if you run go-bindata on data/... and data contains the
@ -262,15 +327,15 @@ var _bindata = map[string]func() (*asset, error){
// img/ // img/
// a.png // a.png
// b.png // b.png
// then AssetDir("data") would return []string{"foo.txt", "img"} // then AssetDir("data") would return []string{"foo.txt", "img"},
// AssetDir("data/img") would return []string{"a.png", "b.png"} // AssetDir("data/img") would return []string{"a.png", "b.png"},
// AssetDir("foo.txt") and AssetDir("notexist") would return an error // AssetDir("foo.txt") and AssetDir("notexist") would return an error, and
// AssetDir("") will return []string{"data"}. // AssetDir("") will return []string{"data"}.
func AssetDir(name string) ([]string, error) { func AssetDir(name string) ([]string, error) {
node := _bintree node := _bintree
if len(name) != 0 { if len(name) != 0 {
cannonicalName := strings.Replace(name, "\\", "/", -1) canonicalName := strings.Replace(name, "\\", "/", -1)
pathList := strings.Split(cannonicalName, "/") pathList := strings.Split(canonicalName, "/")
for _, p := range pathList { for _, p := range pathList {
node = node.Children[p] node = node.Children[p]
if node == nil { if node == nil {
@ -292,16 +357,18 @@ type bintree struct {
Func func() (*asset, error) Func func() (*asset, error)
Children map[string]*bintree Children map[string]*bintree
} }
var _bintree = &bintree{nil, map[string]*bintree{ var _bintree = &bintree{nil, map[string]*bintree{
"1640111208_dummy.up.sql": &bintree{_1640111208_dummyUpSql, map[string]*bintree{}}, "1640111208_dummy.up.sql": {_1640111208_dummyUpSql, map[string]*bintree{}},
"1642666031_add_removed_clock_to_bookmarks.up.sql": &bintree{_1642666031_add_removed_clock_to_bookmarksUpSql, map[string]*bintree{}}, "1642666031_add_removed_clock_to_bookmarks.up.sql": {_1642666031_add_removed_clock_to_bookmarksUpSql, map[string]*bintree{}},
"1643644541_gif_api_key_setting.up.sql": &bintree{_1643644541_gif_api_key_settingUpSql, map[string]*bintree{}}, "1643644541_gif_api_key_setting.up.sql": {_1643644541_gif_api_key_settingUpSql, map[string]*bintree{}},
"1644188994_recent_stickers.up.sql": &bintree{_1644188994_recent_stickersUpSql, map[string]*bintree{}}, "1644188994_recent_stickers.up.sql": {_1644188994_recent_stickersUpSql, map[string]*bintree{}},
"1646841105_add_emoji_account.up.sql": &bintree{_1646841105_add_emoji_accountUpSql, map[string]*bintree{}}, "1646659233_add_address_to_dapp_permisssion.up.sql": {_1646659233_add_address_to_dapp_permisssionUpSql, map[string]*bintree{}},
"doc.go": &bintree{docGo, map[string]*bintree{}}, "1646841105_add_emoji_account.up.sql": {_1646841105_add_emoji_accountUpSql, map[string]*bintree{}},
"doc.go": {docGo, map[string]*bintree{}},
}} }}
// RestoreAsset restores an asset under the given directory // RestoreAsset restores an asset under the given directory.
func RestoreAsset(dir, name string) error { func RestoreAsset(dir, name string) error {
data, err := Asset(name) data, err := Asset(name)
if err != nil { if err != nil {
@ -319,14 +386,10 @@ func RestoreAsset(dir, name string) error {
if err != nil { if err != nil {
return err return err
} }
err = os.Chtimes(_filePath(dir, name), info.ModTime(), info.ModTime()) return os.Chtimes(_filePath(dir, name), info.ModTime(), info.ModTime())
if err != nil {
return err
}
return nil
} }
// RestoreAssets restores an asset under the given directory recursively // RestoreAssets restores an asset under the given directory recursively.
func RestoreAssets(dir, name string) error { func RestoreAssets(dir, name string) error {
children, err := AssetDir(name) children, err := AssetDir(name)
// File // File
@ -344,7 +407,6 @@ func RestoreAssets(dir, name string) error {
} }
func _filePath(dir, name string) string { func _filePath(dir, name string) string {
cannonicalName := strings.Replace(name, "\\", "/", -1) canonicalName := strings.Replace(name, "\\", "/", -1)
return filepath.Join(append([]string{dir}, strings.Split(cannonicalName, "/")...)...) return filepath.Join(append([]string{dir}, strings.Split(canonicalName, "/")...)...)
} }

View File

@ -0,0 +1,22 @@
CREATE TABLE IF NOT EXISTS dapps_2 (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
name TEXT,
address TEXT NOT NULL default "",
CONSTRAINT unique_dapps_name_address UNIQUE (name,address)
);
CREATE TABLE IF NOT EXISTS permissions_2 (
dapp_id int NOT NULL,
permission TEXT NOT NULL,
FOREIGN KEY(dapp_id) REFERENCES dapps_2(id) ON DELETE CASCADE
);
INSERT INTO dapps_2 SELECT NULL, name, "" FROM dapps;
INSERT INTO permissions_2 select dapps_2.id, permissions.permission from permissions join dapps_2 on dapps_2.name = permissions.dapp_name;
DROP TABLE permissions;
DROP TABLE dapps;
ALTER TABLE permissions_2 RENAME TO permissions;
ALTER TABLE dapps_2 RENAME TO dapps;

File diff suppressed because it is too large Load Diff

View File

@ -22,5 +22,9 @@ func (api *API) GetDappPermissions(ctx context.Context) ([]DappPermissions, erro
} }
func (api *API) DeleteDappPermissions(ctx context.Context, name string) error { func (api *API) DeleteDappPermissions(ctx context.Context, name string) error {
return api.db.DeletePermission(name) return api.db.DeletePermission(name, "")
}
func (api *API) DeleteDappPermissionsByNameAndAddress(ctx context.Context, name string, address string) error {
return api.db.DeletePermission(name, address)
} }

View File

@ -56,7 +56,8 @@ func TestDappPermissionsStored(t *testing.T) {
sort.Slice(rst, func(i, j int) bool { sort.Slice(rst, func(i, j int) bool {
return rst[i].Name < rst[j].Name return rst[i].Name < rst[j].Name
}) })
require.Equal(t, expected, rst) require.Equal(t, expected[0].Name, rst[0].Name)
require.Equal(t, expected[0].Permissions, rst[0].Permissions)
data, err := json.Marshal(rst) data, err := json.Marshal(rst)
require.NoError(t, err) require.NoError(t, err)
@ -77,7 +78,9 @@ func TestDappPermissionsReplacedOnUpdated(t *testing.T) {
rst, err := api.GetDappPermissions(context.TODO()) rst, err := api.GetDappPermissions(context.TODO())
require.NoError(t, err) require.NoError(t, err)
require.Len(t, rst, 1) require.Len(t, rst, 1)
require.Equal(t, perms, rst[0]) require.Equal(t, perms.Name, rst[0].Name)
require.Equal(t, perms.Permissions, rst[0].Permissions)
} }
func TestDappPermissionsDeleted(t *testing.T) { func TestDappPermissionsDeleted(t *testing.T) {

View File

@ -19,8 +19,10 @@ func NewDB(db *sql.DB) *Database {
} }
type DappPermissions struct { type DappPermissions struct {
ID int
Name string `json:"dapp"` Name string `json:"dapp"`
Permissions []string `json:"permissions,omitempty"` Permissions []string `json:"permissions,omitempty"`
Address string `json:"address,omitempty"`
} }
func (db *Database) AddPermissions(perms DappPermissions) (err error) { func (db *Database) AddPermissions(perms DappPermissions) (err error) {
@ -35,13 +37,41 @@ func (db *Database) AddPermissions(perms DappPermissions) (err error) {
} }
_ = tx.Rollback() _ = tx.Rollback()
}() }()
dRows, err := tx.Query("SELECT id FROM dapps where name = ? AND address = ?", perms.Name, perms.Address)
dInsert, err := tx.Prepare("INSERT OR REPLACE INTO dapps(name) VALUES(?)")
if err != nil { if err != nil {
return return
} }
_, err = dInsert.Exec(perms.Name) defer dRows.Close()
dInsert.Close()
var id int64
if dRows.Next() {
err = dRows.Scan(&id)
if err != nil {
return
}
} else {
dInsert, err := tx.Prepare("INSERT INTO dapps(name, address) VALUES(?, ?)")
if err != nil {
return err
}
res, err := dInsert.Exec(perms.Name, perms.Address)
dInsert.Close()
if err != nil {
return err
}
id, err = res.LastInsertId()
if err != nil {
return err
}
}
pDelete, err := tx.Prepare("DELETE FROM permissions WHERE dapp_id = ?")
if err != nil {
return
}
defer pDelete.Close()
_, err = pDelete.Exec(id)
if err != nil { if err != nil {
return return
} }
@ -49,13 +79,14 @@ func (db *Database) AddPermissions(perms DappPermissions) (err error) {
if len(perms.Permissions) == 0 { if len(perms.Permissions) == 0 {
return return
} }
pInsert, err := tx.Prepare("INSERT INTO permissions(dapp_name, permission) VALUES(?, ?)")
pInsert, err := tx.Prepare("INSERT INTO permissions(dapp_id, permission) VALUES(?, ?)")
if err != nil { if err != nil {
return return
} }
defer pInsert.Close() defer pInsert.Close()
for _, perm := range perms.Permissions { for _, perm := range perms.Permissions {
_, err = pInsert.Exec(perms.Name, perm) _, err = pInsert.Exec(id, perm)
if err != nil { if err != nil {
return return
} }
@ -77,36 +108,36 @@ func (db *Database) GetPermissions() (rst []DappPermissions, err error) {
}() }()
// FULL and RIGHT joins are not supported // FULL and RIGHT joins are not supported
dRows, err := tx.Query("SELECT name FROM dapps") dRows, err := tx.Query("SELECT id, name, address FROM dapps")
if err != nil { if err != nil {
return return
} }
defer dRows.Close() defer dRows.Close()
dapps := map[string]*DappPermissions{} dapps := map[int]*DappPermissions{}
for dRows.Next() { for dRows.Next() {
perms := DappPermissions{} perms := DappPermissions{}
err = dRows.Scan(&perms.Name) err = dRows.Scan(&perms.ID, &perms.Name, &perms.Address)
if err != nil { if err != nil {
return nil, err return nil, err
} }
dapps[perms.Name] = &perms dapps[perms.ID] = &perms
} }
pRows, err := tx.Query("SELECT dapp_name, permission from permissions") pRows, err := tx.Query("SELECT dapp_id, permission from permissions")
if err != nil { if err != nil {
return return
} }
defer pRows.Close() defer pRows.Close()
var ( var (
name string id int
permission string permission string
) )
for pRows.Next() { for pRows.Next() {
err = pRows.Scan(&name, &permission) err = pRows.Scan(&id, &permission)
if err != nil { if err != nil {
return return
} }
dapps[name].Permissions = append(dapps[name].Permissions, permission) dapps[id].Permissions = append(dapps[id].Permissions, permission)
} }
rst = make([]DappPermissions, 0, len(dapps)) rst = make([]DappPermissions, 0, len(dapps))
for key := range dapps { for key := range dapps {
@ -116,55 +147,22 @@ func (db *Database) GetPermissions() (rst []DappPermissions, err error) {
return rst, nil return rst, nil
} }
func (db *Database) GetPermissionsByDappName(dappName string) (rst *DappPermissions, err error) { func (db *Database) DeletePermission(name string, address string) error {
tx, err := db.db.Begin() _, err := db.db.Exec("DELETE FROM dapps WHERE name = ? AND address = ?", name, address)
return err
}
func (db *Database) HasPermission(dappName string, address string, permission string) (bool, error) {
var id int64
err := db.db.QueryRow("SELECT id FROM dapps where name = ? AND address = ?", dappName, address).Scan(&id)
if err != nil { if err != nil {
return return false, nil
}
defer func() {
if err == nil {
err = tx.Commit()
return
}
_ = tx.Rollback()
}()
rst = &DappPermissions{
Name: dappName,
} }
pRows, err := tx.Query("SELECT permission from permissions WHERE dapp_name = ?", dappName)
if err == sql.ErrNoRows {
return nil, nil
} else if err != nil {
return nil, err
}
defer pRows.Close()
var permission string
for pRows.Next() {
err = pRows.Scan(&permission)
if err != nil {
return
}
rst.Permissions = append(rst.Permissions, permission)
}
return rst, nil
}
func (db *Database) DeletePermission(name string) error {
_, err := db.db.Exec("DELETE FROM dapps WHERE name = ?", name)
return err
}
func (db *Database) DeleteDappPermission(dappName, permission string) error {
_, err := db.db.Exec("DELETE FROM permissions WHERE dapp_name = ? AND permission = ?", dappName, permission)
return err
}
func (db *Database) HasPermission(dappName string, permission string) (bool, error) {
var count uint64 var count uint64
err := db.db.QueryRow(`SELECT COUNT(1) FROM permissions WHERE dapp_name = ? AND permission = ?`, dappName, permission).Scan(&count) err = db.db.QueryRow(
`SELECT COUNT(1) FROM permissions WHERE dapp_id = ? AND permission = ?`,
id, permission,
).Scan(&count)
return count > 0, err return count > 0, err
} }

View File

@ -83,6 +83,7 @@ type Web3SendAsyncReadOnlyRequest struct {
MessageID interface{} `json:"messageId"` MessageID interface{} `json:"messageId"`
Payload ETHPayload `json:"payload"` Payload ETHPayload `json:"payload"`
Hostname string `json:"hostname"` Hostname string `json:"hostname"`
Address string `json:"address,omitempty"`
} }
type Web3SendAsyncReadOnlyError struct { type Web3SendAsyncReadOnlyError struct {
@ -100,6 +101,7 @@ type Web3SendAsyncReadOnlyResponse struct {
type APIRequest struct { type APIRequest struct {
MessageID interface{} `json:"messageId,omitempty"` MessageID interface{} `json:"messageId,omitempty"`
Address string `json:"address,omitempty"`
Hostname string `json:"hostname"` Hostname string `json:"hostname"`
Permission string `json:"permission"` Permission string `json:"permission"`
} }
@ -300,7 +302,7 @@ func (api *API) web3SignatureResponse(request Web3SendAsyncReadOnlyRequest) (*We
} }
func (api *API) ProcessWeb3ReadOnlyRequest(request Web3SendAsyncReadOnlyRequest) (*Web3SendAsyncReadOnlyResponse, error) { func (api *API) ProcessWeb3ReadOnlyRequest(request Web3SendAsyncReadOnlyRequest) (*Web3SendAsyncReadOnlyResponse, error) {
hasPermission, err := api.s.permissionsDB.HasPermission(request.Hostname, PermissionWeb3) hasPermission, err := api.s.permissionsDB.HasPermission(request.Hostname, request.Address, PermissionWeb3)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -356,8 +358,7 @@ func (api *API) ProcessAPIRequest(request APIRequest) (*APIResponse, error) {
if request.Permission == "" { if request.Permission == "" {
return nil, ErrorInvalidAPIRequest return nil, ErrorInvalidAPIRequest
} }
hasPermission, err := api.s.permissionsDB.HasPermission(request.Hostname, request.Address, request.Permission)
hasPermission, err := api.s.permissionsDB.HasPermission(request.Hostname, request.Permission)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -373,7 +374,6 @@ func (api *API) ProcessAPIRequest(request APIRequest) (*APIResponse, error) {
IsAllowed: false, IsAllowed: false,
}, nil }, nil
} }
var data interface{} var data interface{}
switch request.Permission { switch request.Permission {
case PermissionWeb3: case PermissionWeb3:
@ -393,7 +393,6 @@ func (api *API) ProcessAPIRequest(request APIRequest) (*APIResponse, error) {
default: default:
return nil, ErrorUnknownPermission return nil, ErrorUnknownPermission
} }
return &APIResponse{ return &APIResponse{
ProviderResponse: ProviderResponse{ ProviderResponse: ProviderResponse{
ResponseType: ResponseAPI, ResponseType: ResponseAPI,