2022-05-20 10:18:59 +01:00
|
|
|
// Copyright (C) 2022 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package satellitedb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"storj.io/common/uuid"
|
|
|
|
"storj.io/storj/private/blockchain"
|
|
|
|
"storj.io/storj/satellite/payments/storjscan"
|
|
|
|
"storj.io/storj/satellite/satellitedb/dbx"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ensure that storjscanWalletsDB implements storjscan.WalletsDB.
|
|
|
|
var _ storjscan.WalletsDB = (*storjscanWalletsDB)(nil)
|
|
|
|
|
|
|
|
// storjscanWalletsDB is Storjscan wallets DB.
|
|
|
|
//
|
|
|
|
// architecture: Database
|
|
|
|
type storjscanWalletsDB struct {
|
|
|
|
db *satelliteDB
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add creates new user/wallet association record.
|
|
|
|
func (walletsDB storjscanWalletsDB) Add(ctx context.Context, userID uuid.UUID, walletAddress blockchain.Address) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
return walletsDB.db.CreateNoReturn_StorjscanWallet(ctx,
|
|
|
|
dbx.StorjscanWallet_UserId(userID[:]),
|
|
|
|
dbx.StorjscanWallet_WalletAddress(walletAddress.Bytes()))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get returns with thw wallet associated with the user.
|
|
|
|
func (walletsDB storjscanWalletsDB) Get(ctx context.Context, userID uuid.UUID) (_ blockchain.Address, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
wallet, err := walletsDB.db.Get_StorjscanWallet_WalletAddress_By_UserId(ctx, dbx.StorjscanWallet_UserId(userID[:]))
|
|
|
|
if err != nil {
|
|
|
|
return blockchain.Address{}, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
address, err := blockchain.BytesToAddress(wallet.WalletAddress)
|
|
|
|
if err != nil {
|
|
|
|
return blockchain.Address{}, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
return address, nil
|
|
|
|
}
|
|
|
|
|
2022-05-10 20:19:53 +01:00
|
|
|
// GetAll returns all saved wallet entries.
|
|
|
|
func (walletsDB storjscanWalletsDB) GetAll(ctx context.Context) (_ []storjscan.Wallet, err error) {
|
2022-05-20 10:18:59 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2022-05-10 20:19:53 +01:00
|
|
|
entries, err := walletsDB.db.All_StorjscanWallet(ctx)
|
2022-05-20 10:18:59 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
2022-05-10 20:19:53 +01:00
|
|
|
var wallets []storjscan.Wallet
|
|
|
|
for _, entry := range entries {
|
|
|
|
userID, err := uuid.FromBytes(entry.UserId)
|
2022-05-20 10:18:59 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
2022-05-10 20:19:53 +01:00
|
|
|
address, err := blockchain.BytesToAddress(entry.WalletAddress)
|
|
|
|
if err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
wallets = append(wallets, storjscan.Wallet{UserID: userID, Address: address})
|
2022-05-20 10:18:59 +01:00
|
|
|
}
|
2022-05-10 20:19:53 +01:00
|
|
|
return wallets, nil
|
2022-05-20 10:18:59 +01:00
|
|
|
}
|