2022-04-28 03:54:56 +01:00
|
|
|
// Copyright (C) 2022 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package storjscan
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
|
|
|
|
"storj.io/common/uuid"
|
|
|
|
"storj.io/storj/private/blockchain"
|
|
|
|
"storj.io/storj/satellite/payments"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// Error defines storjscan service error.
|
|
|
|
Error = errs.Class("storjscan service")
|
|
|
|
)
|
|
|
|
|
|
|
|
// ensures that Wallets implements payments.Wallets.
|
|
|
|
var _ payments.DepositWallets = (*Service)(nil)
|
|
|
|
|
|
|
|
// Service is an implementation for payment service via Stripe and Coinpayments.
|
|
|
|
//
|
|
|
|
// architecture: Service
|
|
|
|
type Service struct {
|
|
|
|
walletsDB WalletsDB
|
|
|
|
storjscanClient *Client
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewService creates a Service instance.
|
|
|
|
func NewService(db DB, storjscanClient *Client) (*Service, error) {
|
|
|
|
return &Service{
|
|
|
|
walletsDB: db.Wallets(),
|
|
|
|
storjscanClient: storjscanClient,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Claim gets a new crypto wallet and associates it with a user.
|
|
|
|
func (service *Service) Claim(ctx context.Context, userID uuid.UUID) (_ blockchain.Address, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
address, err := service.storjscanClient.ClaimNewEthAddress(ctx)
|
|
|
|
if err != nil {
|
2022-06-06 13:42:45 +01:00
|
|
|
return blockchain.Address{}, Error.Wrap(err)
|
2022-04-28 03:54:56 +01:00
|
|
|
}
|
|
|
|
err = service.walletsDB.Add(ctx, userID, address)
|
2022-06-06 13:42:45 +01:00
|
|
|
if err != nil {
|
|
|
|
return blockchain.Address{}, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return address, nil
|
2022-04-28 03:54:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get returns the crypto wallet address associated with the given user.
|
|
|
|
func (service *Service) Get(ctx context.Context, userID uuid.UUID) (_ blockchain.Address, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2022-06-06 13:42:45 +01:00
|
|
|
address, err := service.walletsDB.Get(ctx, userID)
|
|
|
|
return address, Error.Wrap(err)
|
2022-04-28 03:54:56 +01:00
|
|
|
}
|