storj/satellite/payments/storjscan/service.go
dlamarmorgan 270204f352 satellite/{payments/storjscan,satellitedb}: Add wallet implementation
Add storjscan wallets implementation to the satellite. The wallets interface allows you to add and claim new wallets as called by the API. The storjscan specific implementation of this interface uses a wallets DB to associate the user to a wallet address, as well as a storjscan client to request and associate new wallets to the satellite.

Change-Id: I54081edb5545d4e3ee07cf1cce3d3e87cc00c4a1
2022-06-03 11:45:47 +00:00

65 lines
1.7 KiB
Go

// 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"
"storj.io/storj/satellite/payments/coinpayments"
)
var (
// Error defines storjscan service error.
Error = errs.Class("storjscan service")
)
// ensures that Wallets implements payments.Wallets.
var _ payments.DepositWallets = (*Service)(nil)
// Config stores needed information for storjscan service initialization.
type Config struct {
Credentials coinpayments.Credentials
Endpoint string
}
// 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 {
return blockchain.Address{}, err
}
err = service.walletsDB.Add(ctx, userID, address)
return address, err
}
// 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)
return service.walletsDB.Get(ctx, userID)
}