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 #7 from eqlabs/latenssi/refactor-params
Refactor param passing and keys package structure
- Loading branch information
Showing
11 changed files
with
319 additions
and
219 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,53 @@ | ||
package gorm | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/caarlos0/env/v6" | ||
"github.com/eqlabs/flow-nft-wallet-service/data" | ||
"gorm.io/driver/mysql" | ||
"gorm.io/driver/postgres" | ||
"gorm.io/driver/sqlite" | ||
"gorm.io/gorm" | ||
) | ||
|
||
type Store struct { | ||
data.AccountStore | ||
} | ||
|
||
func NewStore(dialector gorm.Dialector) (*Store, error) { | ||
type Config struct { | ||
DatabaseDSN string `env:"DB_DSN" envDefault:"wallet.db"` | ||
DatabaseType string `env:"DB_TYPE" envDefault:"sqlite"` | ||
} | ||
|
||
func NewStore(l *log.Logger) (store *Store, err error) { | ||
cfg := Config{} | ||
if err = env.Parse(&cfg); err != nil { | ||
return | ||
} | ||
|
||
var dialector gorm.Dialector | ||
switch cfg.DatabaseType { | ||
case data.DB_TYPE_POSTGRESQL: | ||
dialector = postgres.Open(cfg.DatabaseDSN) | ||
case data.DB_TYPE_MYSQL: | ||
dialector = mysql.Open(cfg.DatabaseDSN) | ||
case data.DB_TYPE_SQLITE: | ||
dialector = sqlite.Open(cfg.DatabaseDSN) | ||
default: | ||
err = fmt.Errorf("database type '%s' not supported", cfg.DatabaseType) | ||
return | ||
} | ||
|
||
db, err := gorm.Open(dialector, &gorm.Config{}) | ||
if err != nil { | ||
return &Store{}, err | ||
return | ||
} | ||
return &Store{ | ||
AccountStore: newAccountStore(db), | ||
}, nil | ||
|
||
store = &Store{ | ||
AccountStore: newAccountStore(l, db), | ||
} | ||
|
||
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
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package simple | ||
package encryption | ||
|
||
import ( | ||
"crypto/aes" | ||
|
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 | ||
} |
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,57 @@ | ||
package local | ||
|
||
import ( | ||
"crypto/rand" | ||
|
||
"github.com/eqlabs/flow-nft-wallet-service/keys" | ||
"github.com/onflow/flow-go-sdk" | ||
"github.com/onflow/flow-go-sdk/crypto" | ||
) | ||
|
||
func Generate( | ||
signAlgo crypto.SignatureAlgorithm, | ||
hashAlgo crypto.HashAlgorithm, | ||
keyIndex, weight int, | ||
) (result keys.Wrapped, err error) { | ||
seed := make([]byte, crypto.MinSeedLength) | ||
_, err = rand.Read(seed) | ||
if err != nil { | ||
return | ||
} | ||
|
||
privateKey, err := crypto.GeneratePrivateKey(signAlgo, seed) | ||
if err != nil { | ||
return | ||
} | ||
|
||
flowKey := flow.NewAccountKey(). | ||
FromPrivateKey(privateKey). | ||
SetHashAlgo(hashAlgo). | ||
SetWeight(weight) | ||
|
||
flowKey.Index = keyIndex | ||
|
||
key := keys.Key{ | ||
Index: keyIndex, | ||
Type: keys.ACCOUNT_KEY_TYPE_LOCAL, | ||
Value: privateKey.String(), | ||
} | ||
|
||
result.AccountKey = key | ||
result.FlowKey = flowKey | ||
|
||
return | ||
} | ||
|
||
func Signer( | ||
signAlgo crypto.SignatureAlgorithm, | ||
hashAlgo crypto.HashAlgorithm, | ||
key keys.Key, | ||
) (result crypto.Signer, err error) { | ||
pk, err := crypto.DecodePrivateKeyHex(signAlgo, key.Value) | ||
if err != nil { | ||
return | ||
} | ||
result = crypto.NewInMemorySigner(pk, hashAlgo) | ||
return | ||
} |
Oops, something went wrong.