database

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 9, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const EstimatedInputSizeForP2PKH = 148

EstimatedInputSizeForP2PKH is the estimated size increase when adding and unlocking P2PKH input to transaction. 32 bytes txID + 4 bytes vout index + 1 byte script length + 107 bytes script pub key + 4 bytes nSequence

Variables

This section is empty.

Functions

func Models

func Models() []any

Models returns a list of all models, e.g. for migrations.

Types

type Address

type Address struct {
	Address string `gorm:"type:char(34);primaryKey"`

	CreatedAt time.Time
	UpdatedAt time.Time
	DeletedAt gorm.DeletedAt `gorm:"index"`

	CustomInstructions datatypes.JSONSlice[bsv.CustomInstruction]

	UserID string
	User   *User `gorm:"foreignKey:UserID"`
}

Address represents a user's (bitcoin) addresses.

type Data

type Data struct {
	TxID string `gorm:"primaryKey"`
	Vout uint32 `gorm:"primaryKey"`

	UserID string

	Blob []byte
}

Data holds the data stored in outputs.

func (*Data) Outpoint

func (o *Data) Outpoint() *bsv.Outpoint

Outpoint returns bsv.Outpoint object which identifies the data-output.

type Operation

type Operation struct {
	TxID   string `gorm:"primaryKey"`
	UserID string `gorm:"primaryKey"`

	CreatedAt time.Time

	Counterparty string
	Type         string
	Value        int64

	User        *User               `gorm:"foreignKey:UserID"`
	Transaction *TrackedTransaction `gorm:"foreignKey:TxID"`
}

Operation represents a user's operation on a transaction.

type Paymail

type Paymail struct {
	gorm.Model

	Alias  string `gorm:"uniqueIndex:idx_alias_domain"`
	Domain string `gorm:"uniqueIndex:idx_alias_domain"`

	PublicName string
	Avatar     string

	UserID string
}

Paymail represents a paymail address

type TrackedOutput

type TrackedOutput struct {
	TxID       string `gorm:"primaryKey"`
	Vout       uint32 `gorm:"primaryKey"`
	SpendingTX string `gorm:"type:char(64)"`

	UserID string

	Satoshis bsv.Satoshis

	CreatedAt time.Time
	UpdatedAt time.Time
}

TrackedOutput represents an output of a transaction.

func (*TrackedOutput) AfterFind

func (o *TrackedOutput) AfterFind(_ *gorm.DB) (err error)

AfterFind is a GORM hook that is called after retrieving the record from the database.

type TrackedTransaction

type TrackedTransaction struct {
	ID       string `gorm:"type:char(64);primaryKey"`
	TxStatus string

	CreatedAt time.Time
	UpdatedAt time.Time

	BlockHeight *int64
	BlockHash   *string

	Data []*Data `gorm:"-"` // Don't auto-save, handled in AfterCreate hook

	Inputs  []*TrackedOutput `gorm:"foreignKey:SpendingTX"`
	Outputs []*TrackedOutput `gorm:"foreignKey:TxID"`

	BeefHex        *string   `gorm:"column:beef_hex"`
	RawHex         *string   `gorm:"column:raw_hex"`
	SourceTxInputs []TxInput `gorm:"foreignKey:TxID;constraint:OnDelete:CASCADE;"`
	// contains filtered or unexported fields
}

TrackedTransaction represents a transaction in the database.

func (*TrackedTransaction) AfterCreate

func (t *TrackedTransaction) AfterCreate(tx *gorm.DB) error

AfterCreate is a hook that is called after creating the transaction. It is responsible for adding new (User's) UTXOs, Data outputs, and removing spent UTXOs.

func (*TrackedTransaction) CreateDataOutput

func (t *TrackedTransaction) CreateDataOutput(data *Data)

CreateDataOutput prepares a new Data output and adds it to the transaction.

func (*TrackedTransaction) CreateUTXO

func (t *TrackedTransaction) CreateUTXO(
	output *TrackedOutput,
	bucket string,
	estimatedInputSize uint64,
	customInstructions bsv.CustomInstructions,
)

CreateUTXO prepares a new UTXO and adds it to the transaction.

func (*TrackedTransaction) HasBeefHex

func (t *TrackedTransaction) HasBeefHex() bool

HasBeefHex checks if the tracked transaction record contains a non-empty BeefHex attribute.

func (*TrackedTransaction) HasRawHex

func (t *TrackedTransaction) HasRawHex() bool

HasRawHex checks if the tracked transaction record does not contain a BeefHex attribute.

func (*TrackedTransaction) ToTxQueryResult

func (t *TrackedTransaction) ToTxQueryResult() *beef.TxQueryResult

ToTxQueryResult converts a TrackedTransaction into a TxQueryResult.

type TxInput

type TxInput struct {
	TxID       string `gorm:"type:char(64);primaryKey"` // ID of the spending transaction
	SourceTxID string `gorm:"type:char(64);primaryKey"` // ID of the source transaction
}

TxInput represents a transaction input in the database. This struct is used to store the relationship between a transaction (TxID) and its source transaction (SourceTxID). It helps track transaction ancestry by linking an input to the transaction that created the output it spends.

type User

type User struct {
	ID        string `gorm:"type:char(34);primaryKey"`
	CreatedAt time.Time
	UpdatedAt time.Time

	PubKey string `gorm:"index;unique;not null"`

	Paymails  []*Paymail     `gorm:"foreignKey:UserID"`
	Addresses []*Address     `gorm:"foreignKey:UserID"`
	Contacts  []*UserContact `gorm:"foreignKey:UserID"`
}

User represents a user in the database

func (*User) BeforeCreate

func (u *User) BeforeCreate(_ *gorm.DB) (err error)

BeforeCreate is a gorm hook that is called before creating a new user

func (*User) PubKeyObj

func (u *User) PubKeyObj() (*primitives.PublicKey, error)

PubKeyObj returns the go-sdk primitives.PublicKey object from the user's PubKey string

type UserContact

type UserContact struct {
	gorm.Model

	FullName string
	Status   string
	Paymail  string
	PubKey   string

	UserID string
	User   *User `gorm:"foreignKey:UserID"`
}

UserContact represents a contact between two users but is assigned to one.

type UserUTXO

type UserUTXO struct {
	UserID   string `gorm:"primaryKey;uniqueIndex:idx_window,sort:asc,priority:1"`
	TxID     string `gorm:"primaryKey;uniqueIndex:idx_window,sort:asc,priority:4"`
	Vout     uint32 `gorm:"primaryKey;uniqueIndex:idx_window,sort:asc,priority:5"`
	Satoshis uint64
	// EstimatedInputSize is the estimated size increase when adding and unlocking this UTXO to a transaction.
	EstimatedInputSize uint64
	Bucket             string    `gorm:"check:chk_not_data_bucket,bucket <> 'data'"`
	CreatedAt          time.Time `gorm:"uniqueIndex:idx_window,sort:asc,priority:3"`
	// TouchedAt is the time when the UTXO was last touched (selected for preparing transaction outline) - used for prioritizing UTXO selection.
	TouchedAt time.Time `gorm:"uniqueIndex:idx_window,sort:asc,priority:2"`
	// CustomInstructions is the list of instructions for unlocking given UTXO (it should be understood by client).
	CustomInstructions datatypes.JSONSlice[bsv.CustomInstruction]
}

UserUTXO is a table holding user's Unspent Transaction Outputs (UTXOs).

func NewUTXO

func NewUTXO(
	output *TrackedOutput,
	bucket string,
	estimatedInputSize uint64,
	customInstructions bsv.CustomInstructions,
) *UserUTXO

NewUTXO creates a new UserUTXO from the given TrackedOutput and additional data.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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