This repository has been archived by the owner on Aug 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from eqlabs/latenssi/encryption
Data encryption and refactor
- Loading branch information
Showing
24 changed files
with
574 additions
and
475 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package gorm | ||
|
||
import ( | ||
"github.com/eqlabs/flow-nft-wallet-service/data" | ||
"gorm.io/gorm" | ||
) | ||
|
||
type AccountStore struct { | ||
db *gorm.DB | ||
} | ||
|
||
func newAccountStore(db *gorm.DB) *AccountStore { | ||
db.AutoMigrate(&data.Account{}, &data.Key{}) | ||
return &AccountStore{db} | ||
} | ||
|
||
// List all accounts | ||
func (s *AccountStore) Accounts() (accounts []data.Account, err error) { | ||
err = s.db.Select("address").Find(&accounts).Error | ||
return | ||
} | ||
|
||
// Insert new account | ||
func (s *AccountStore) InsertAccount(account data.Account) error { | ||
return s.db.Create(&account).Error | ||
} | ||
|
||
// Get account details | ||
func (s *AccountStore) Account(address string) (account data.Account, err error) { | ||
err = s.db.Preload("Keys").First(&account, "address = ?", address).Error | ||
return | ||
} | ||
|
||
// Get account key with index | ||
func (s *AccountStore) AccountKey(address string, index int) (key data.Key, err error) { | ||
err = s.db.Where("account_address = ? AND index = ?", address, index).First(&key).Error | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package gorm | ||
|
||
import ( | ||
"github.com/eqlabs/flow-nft-wallet-service/data" | ||
"gorm.io/gorm" | ||
) | ||
|
||
type Store struct { | ||
data.AccountStore | ||
} | ||
|
||
func NewStore(dialector gorm.Dialector) (*Store, error) { | ||
db, err := gorm.Open(dialector, &gorm.Config{}) | ||
if err != nil { | ||
return &Store{}, err | ||
} | ||
return &Store{ | ||
AccountStore: newAccountStore(db), | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package data | ||
|
||
import ( | ||
"time" | ||
|
||
"gorm.io/gorm" | ||
) | ||
|
||
const ( | ||
DB_TYPE_POSTGRESQL = "psql" | ||
DB_TYPE_MYSQL = "mysql" | ||
DB_TYPE_SQLITE = "sqlite" | ||
) | ||
|
||
type Store interface { | ||
AccountStore | ||
} | ||
|
||
type AccountStore interface { | ||
Accounts() ([]Account, error) | ||
InsertAccount(a Account) error | ||
Account(address string) (Account, error) | ||
AccountKey(address string, index int) (Key, error) | ||
} | ||
|
||
// Storable account | ||
type Account struct { | ||
Address string `json:"address" gorm:"primaryKey"` | ||
Keys []Key `json:"keys" gorm:"foreignKey:AccountAddress;references:Address;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"` | ||
CreatedAt time.Time `json:"-"` | ||
UpdatedAt time.Time `json:"-"` | ||
DeletedAt gorm.DeletedAt `json:"-" gorm:"index"` | ||
} | ||
|
||
// Storable account key | ||
type Key struct { | ||
ID int `json:"-" gorm:"primaryKey"` | ||
AccountAddress string `json:"-" gorm:"index"` | ||
Index int `json:"index" gorm:"index"` | ||
Type string `json:"type"` | ||
Value []byte `json:"-"` | ||
CreatedAt time.Time `json:"-"` | ||
UpdatedAt time.Time `json:"-"` | ||
DeletedAt gorm.DeletedAt `json:"-" gorm:"index"` | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package google | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/onflow/flow-go-sdk/crypto/cloudkms" | ||
|
||
kms "cloud.google.com/go/kms/apiv1" | ||
kmspb "google.golang.org/genproto/googleapis/cloud/kms/v1" | ||
) | ||
|
||
// Creates a new asymmetric signing key in Google KMS and returns a cloudkms.Key (the "raw" result isn't needed) | ||
func AsymKey(ctx context.Context, parent, id string) (createdKey cloudkms.Key, err error) { | ||
kmsClient, err := kms.NewKeyManagementClient(ctx) | ||
if err != nil { | ||
return | ||
} | ||
|
||
req := &kmspb.CreateCryptoKeyRequest{ | ||
Parent: parent, | ||
CryptoKeyId: id, | ||
CryptoKey: &kmspb.CryptoKey{ | ||
Purpose: kmspb.CryptoKey_ASYMMETRIC_SIGN, | ||
VersionTemplate: &kmspb.CryptoKeyVersionTemplate{ | ||
Algorithm: kmspb.CryptoKeyVersion_EC_SIGN_P256_SHA256, | ||
}, | ||
// TODO: Set relevant labels at creation, update post-creation if necessary | ||
Labels: map[string]string{ | ||
"service": "flow-nft-wallet-service", | ||
"account_address": "", | ||
"chain_id": "", | ||
"environment": "development", | ||
}, | ||
}, | ||
} | ||
|
||
googleKey, err := kmsClient.CreateCryptoKey(ctx, req) | ||
if err != nil { | ||
return | ||
} | ||
|
||
// Append cryptoKeyVersions so that we can utilize the KeyFromResourceID method | ||
createdKey, err = cloudkms.KeyFromResourceID(fmt.Sprintf("%s/cryptoKeyVersions/1", googleKey.Name)) | ||
if err != nil { | ||
fmt.Println("Could not create cloudkms.Key from ResourceId:", googleKey.Name) | ||
return | ||
} | ||
|
||
// Validate key name | ||
if !strings.HasPrefix(createdKey.ResourceID(), googleKey.Name) { | ||
fmt.Println("WARNING: created Google KMS key name does not match the expected", createdKey.ResourceID(), " vs ", googleKey.Name) | ||
// TODO: Handle scenario | ||
} | ||
|
||
return | ||
} |
Oops, something went wrong.