db

package
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 10, 2024 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const (
	VASPS_FILE        = "vasps.json"
	WALLETS_FILE      = "wallets.json"
	TRANSACTIONS_FILE = "transactions.json"
)

Variables

This section is empty.

Functions

func LoadWallets

func LoadWallets(fixturesPath string) (wallets []Wallet, accounts []Account, err error)

Load wallets from the fixtures directory as a slice of Wallet objects. Also returns a slice of Account objects which are derived from the wallet values.

func MigrateDB

func MigrateDB(gdb *gorm.DB) (err error)

MigrateDB the schema based on the models defined above.

func OpenDB

func OpenDB(conf *config.Config) (db *gorm.DB, err error)

OpenDB opens a connection to a database with retries and returns the gorm database pointer.

func ResetDB

func ResetDB(gdb *gorm.DB, fixturesPath string) (err error)

ResetDB resets the database using the JSON fixtures.

Types

type Account

type Account struct {
	gorm.Model
	Name          string          `gorm:"not null"`
	Email         string          `gorm:"uniqueIndex;not null"`
	WalletAddress string          `gorm:"uniqueIndex;not null;column:wallet_address"`
	Wallet        Wallet          `gorm:"foreignKey:WalletAddress;references:Address"`
	Balance       decimal.Decimal `gorm:"type:numeric(15,2);default:0.0"`
	Completed     uint64          `gorm:"not null;default:0"`
	Pending       uint64          `gorm:"not null;default:0"`
	IVMS101       string          `gorm:"column:ivms101;not null"`
	VaspID        uint            `gorm:"not null"`
	Vasp          VASP            `gorm:"foreignKey:VaspID"`
}

Account contains details about the transactions that are served by the local VASP. It also contains the IVMS 101 data for KYC verification, in this table it is just stored as a JSON string rather than breaking it down to the field level. Only customers of the VASP have accounts.

func (Account) BalanceFloat

func (a Account) BalanceFloat() float32

BalanceFloat converts the balance decimal into an exact two precision float32 for use with the protocol buffers.

func (Account) GetVASP

func (a Account) GetVASP(d *DB) (vasp *VASP, err error)

Return the VASP associated with the account.

func (Account) GetWallet

func (a Account) GetWallet(db *DB) (wallet *Wallet, err error)

Return the wallet associated with the account.

func (Account) LoadIdentity

func (a Account) LoadIdentity() (person *ivms101.Person, err error)

LoadIdentity returns the ivms101.Person for the Account.

func (Account) TableName

func (Account) TableName() string

TableName explicitly defines the name of the table for the model

func (Account) Transactions

func (a Account) Transactions(db *DB) (records []Transaction, err error)

Transactions returns an ordered list of transactions associated with the account ordered by the timestamp of the transaction, listing any pending transactions at the top. This function may also support pagination and limiting functions, which is why we're using it rather than having a direct relationship on the model.

type DB

type DB struct {
	// contains filtered or unexported fields
}

DB is a wrapper around a gorm.DB instance that restricts query results to a single VASP.

func NewDB

func NewDB(conf *config.Config) (d *DB, err error)

func NewDBMock

func NewDBMock(vasp string) (d *DB, mock sqlmock.Sqlmock, err error)

NewDBMock creates a new mocked database object which uses an underlying SQL mock. The mock can be safely passed as a real database object to high-level functions which interact with gorm.DB objects to enable testing.

func (*DB) Create

func (d *DB) Create(value interface{}) *gorm.DB

func (*DB) GetDB

func (d *DB) GetDB() *gorm.DB

func (*DB) GetVASP

func (d *DB) GetVASP() VASP

func (*DB) LookupAccount

func (d *DB) LookupAccount(account string) *gorm.DB

LookupAccount by email address or wallet address.

func (*DB) LookupAnyAccount

func (d *DB) LookupAnyAccount(account string) *gorm.DB

LookupAnyAccount by email address or wallet address, not restricted to the local rVASP.

func (*DB) LookupAnyBeneficiary

func (d *DB) LookupAnyBeneficiary(beneficiary string) *gorm.DB

LookupAnyBeneficiary by email address or wallet address, not restricted to the local rVASP.

func (*DB) LookupBeneficiary

func (d *DB) LookupBeneficiary(beneficiary string) *gorm.DB

LookupBeneficiary by email address or wallet address.

func (*DB) LookupIdentity

func (d *DB) LookupIdentity(walletAddress string) *gorm.DB

LookupIdentity by wallet address.

func (*DB) LookupPending

func (d *DB) LookupPending() *gorm.DB

LookupPending returns the pending transactions.

func (*DB) LookupTransaction

func (d *DB) LookupTransaction(envelope string) *gorm.DB

LookupTransaction by envelope ID.

func (*DB) LookupWallet

func (d *DB) LookupWallet(address string) *gorm.DB

LookupWallet by wallet address.

func (*DB) MakeTransaction

func (d *DB) MakeTransaction(originator string, beneficiary string) (*Transaction, error)

MakeTransaction returns a new Transaction from the originator and beneficiary wallet addresses. Note: this does not store the transaction in the database to allow the caller to modify the transaction fields before storage.

func (*DB) Query

func (d *DB) Query() *gorm.DB

func (*DB) Save

func (d *DB) Save(value interface{}) *gorm.DB

type Identity

type Identity struct {
	gorm.Model
	WalletAddress string `gorm:"not null;column:wallet_address;index:wallet_index,unique"`
	Email         string `gorm:"not null"`
	Provider      string `gorm:"not null"`
	VaspID        uint   `gorm:"not null;index:wallet_index,unique"`
	Vasp          VASP   `gorm:"foreignKey:VaspID"`
}

Identity holds raw data for an originator or a beneficiary that was sent as part of the transaction process. This should not be stored in the wallet since the wallet is a representation of the local VASPs knowledge about customers and because the identity information could change between transactions. This intermediate table is designed to more closely mimic data storage as part of a blockchain transaction.

func (Identity) TableName

func (Identity) TableName() string

TableName explicitly defines the name of the table for the model

type PolicyType

type PolicyType string
const (
	SendPartial PolicyType = "SendPartial"
	SendFull    PolicyType = "SendFull"
	SendError   PolicyType = "SendError"
	SyncRepair  PolicyType = "SyncRepair"
	SyncRequire PolicyType = "SyncRequire"
	AsyncRepair PolicyType = "AsyncRepair"
	AsyncReject PolicyType = "AsyncReject"
)

type Transaction

type Transaction struct {
	gorm.Model
	Envelope      string              `gorm:"not null"`
	AccountID     uint                `gorm:"not null"`
	Account       Account             `gorm:"foreignKey:AccountID"`
	OriginatorID  uint                `gorm:"column:originator_id;not null"`
	Originator    Identity            `gorm:"foreignKey:OriginatorID"`
	BeneficiaryID uint                `gorm:"column:beneficiary_id;not null"`
	Beneficiary   Identity            `gorm:"foreignKey:BeneficiaryID"`
	Amount        decimal.Decimal     `gorm:"type:decimal(15,8)"`
	AssetType     string              `gorm:"not null"`
	Debit         bool                `gorm:"not null"`
	State         pb.TransactionState `gorm:"not null;default:0"`
	StateString   string              `gorm:"column:state_string;not null"`
	Timestamp     time.Time           `gorm:"not null"`
	NotBefore     time.Time           `gorm:"not null"`
	NotAfter      time.Time           `gorm:"not null"`
	Identity      string              `gorm:"not null"`
	Transaction   string              `gorm:"not null"`
	VaspID        uint                `gorm:"not null"`
	Vasp          VASP                `gorm:"foreignKey:VaspID"`
}

Transaction holds exchange information to send money from one account to another. It also contains the decrypted identity payload that was sent as part of the TRISA protocol and the envelope ID that uniquely identifies the message chain. TODO: Add a field for the transaction payload marshaled as a string.

func (Transaction) AmountFloat

func (t Transaction) AmountFloat() float32

AmountFloat converts the amount decimal into an exact two precision float32 for use with the protocol buffers.

func (Transaction) GetAccount

func (t Transaction) GetAccount(db *DB) (account *Account, err error)

Return the account associated with the transaction.

func (Transaction) GetBeneficiary

func (t Transaction) GetBeneficiary(db *DB) (identity *Identity, err error)

Return the originator address associated with the transaction.

func (Transaction) GetOriginator

func (t Transaction) GetOriginator(db *DB) (identity *Identity, err error)

Return the originator associated with the transaction.

func (Transaction) Proto

func (t Transaction) Proto() *pb.Transaction

Proto converts the transaction into a protocol buffer transaction

func (*Transaction) SetState added in v1.0.2

func (t *Transaction) SetState(state pb.TransactionState)

Set the transaction state to a new value

func (Transaction) TableName

func (Transaction) TableName() string

TableName explicitly defines the name of the table for the model

type VASP

type VASP struct {
	gorm.Model
	Name      string     `gorm:"uniqueIndex;size:255;not null"`
	LegalName *string    `gorm:"column:legal_name;null"`
	URL       *string    `gorm:"null"`
	Country   *string    `gorm:"null"`
	Endpoint  *string    `gorm:"null"`
	PubKey    *string    `gorm:"null"`
	NotAfter  *time.Time `gorm:"null"`
	IVMS101   string     `gorm:"column:ivms101"`
}

VASP is a record of known partner VASPs and caches TRISA protocol information. This table also contains IVMS101 data that identifies the VASP (but only for the local VASP - we assume that VASPs do not have IVMS101 data on each other and have to use the directory service for that). TODO: modify VASP ID to a GUID

func LoadVASPs

func LoadVASPs(fixturesPath string) (vasps []VASP, err error)

Load VASPS from the fixtures directory as a slice of VASP objects.

func (VASP) LoadIdentity

func (v VASP) LoadIdentity() (person *ivms101.Person, err error)

LoadIdentity returns the ivms101.Person for the VASP.

func (VASP) TableName

func (VASP) TableName() string

TableName explicitly defines the name of the table for the model

type Wallet

type Wallet struct {
	gorm.Model
	Address           string     `gorm:"uniqueIndex"`
	Email             string     `gorm:"uniqueIndex"`
	OriginatorPolicy  PolicyType `gorm:"column:originator_policy"`
	BeneficiaryPolicy PolicyType `gorm:"column:beneficiary_policy"`
	ProviderID        uint       `gorm:"not null"`
	Provider          VASP       `gorm:"foreignKey:ProviderID"`
	VaspID            uint       `gorm:"not null"`
	Vasp              VASP       `gorm:"foreignKey:VaspID"`
}

Wallet is a mapping of wallet IDs to VASPs to determine where to send transactions. Provider lookups can happen by wallet address or by email.

func (Wallet) TableName

func (Wallet) TableName() string

TableName explicitly defines the name of the table for the model

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL