shared

package
v0.0.0-...-64df52d Latest Latest
Warning

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

Go to latest
Published: Dec 8, 2024 License: MIT Imports: 34 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var AllUTXOs []UTXO

Functions

func ConvertLocalTransactionToThrylosTransaction

func ConvertLocalTransactionToThrylosTransaction(tx Transaction) (*thrylos.Transaction, error)

Hypothetical conversion function from your local Transaction type to *thrylos.Transaction ConvertLocalTransactionToThrylosTransaction converts your local Transaction type to *thrylos.Transaction

func ConvertSharedToThrylos

func ConvertSharedToThrylos(tx *Transaction) (*thrylos.Transaction, error)

ConvertSharedToThrylos converts a shared.Transaction to a thrylos.Transaction.

func ConvertSharedUTXOToProto

func ConvertSharedUTXOToProto(u UTXO) *thrylos.UTXO

ConvertSharedUTXOToProto converts a shared.UTXO to a protobuf UTXO message.

func ConvertToProtoTransaction

func ConvertToProtoTransaction(tx *Transaction) (*thrylos.Transaction, error)

func CreateThrylosTransaction

func CreateThrylosTransaction(id int) *thrylos.Transaction

func DebugSignTransaction

func DebugSignTransaction(tx *thrylos.Transaction, privateKey ed25519.PrivateKey) error

DebugSignTransaction wraps the signing process with detailed logging

func DebugSignatureVerification

func DebugSignatureVerification(tx *thrylos.Transaction, publicKey ed25519.PublicKey) error

DebugSignatureVerification wraps the verification process with detailed logging

func DecodePrivateKey

func DecodePrivateKey(encodedKey []byte) (ed25519.PrivateKey, error)

func DecryptTransactionData

func DecryptTransactionData(encryptedData, encryptedKey []byte, recipientPrivateKey *rsa.PrivateKey) ([]byte, error)

DecryptTransactionData function should be already defined and be similar to this

func DecryptWithAES

func DecryptWithAES(key, ciphertext []byte) ([]byte, error)

DecryptWithAES decrypts data using AES-256-CBC.

func EncryptAESKey

func EncryptAESKey(aesKey []byte, recipientPublicKey *rsa.PublicKey) ([]byte, error)

func EncryptWithAES

func EncryptWithAES(key, plaintext []byte) ([]byte, error)

EncryptWithAES encrypts data using AES-256-CBC.

func GenerateAESKey

func GenerateAESKey() ([]byte, error)

GenerateAESKey generates a new AES-256 symmetric key.

func GenerateEd25519Keys

func GenerateEd25519Keys() (ed25519.PublicKey, ed25519.PrivateKey, string, error)

GenerateEd25519Keys generates a new Ed25519 public/private key pair derived from a mnemonic seed phrase.

func GenerateTransactionID

func GenerateTransactionID(inputs []UTXO, outputs []UTXO, address string, amount, gasFee int) (string, error)

GenerateTransactionID creates a unique identifier for a transaction based on its contents.

func GetAllUTXOs

func GetAllUTXOs() map[string]UTXO

func HashData

func HashData(data []byte) []byte

func MarkUTXOAsSpent

func MarkUTXOAsSpent(utxoID string, allUTXOs map[string]UTXO)

MarkUTXOAsSpent removes a UTXO from the set of available UTXOs, effectively marking it as spent. This operation is critical in preventing double-spending within the blockchain system.

func ProcessTransactionsBatch

func ProcessTransactionsBatch(transactions []*Transaction, db BlockchainDBInterface) error

func PublicKeyToAddress

func PublicKeyToAddress(pubKey ed25519.PublicKey) string

PublicKeyToAddress generates a public address from an Ed25519 public key using SHA-256 and then BLAKE2b-256.

func PublicKeyToAddressWithCache

func PublicKeyToAddressWithCache(pubKey ed25519.PublicKey) string

func SanitizeAndFormatAddress

func SanitizeAndFormatAddress(address string) (string, error)

SanitizeAndFormatAddress cleans and validates blockchain addresses.

func SerializeTransactionForSigning

func SerializeTransactionForSigning(tx *Transaction) ([]byte, error)

func SerializeUTXOs

func SerializeUTXOs(utxos []UTXO) ([]byte, error)

This utilizes the custom MarshalJSON method defined in the UTXO struct if present.

func SharedToThrylos

func SharedToThrylos(tx *Transaction) *thrylos.Transaction

func SharedToThrylosInputs

func SharedToThrylosInputs(inputs []UTXO, txSender string) []*thrylos.UTXO

func SharedToThrylosOutputs

func SharedToThrylosOutputs(outputs []UTXO) []*thrylos.UTXO

func SignTransaction

func SignTransaction(tx *thrylos.Transaction, ed25519PrivateKey ed25519.PrivateKey) error

Used only in the CLI Signer (not in the blockchain!)

func SignTransactionData

func SignTransactionData(tx *Transaction, privateKeyBytes []byte) ([]byte, error)

func UTXOToMap

func UTXOToMap(utxo *UTXO) map[string]interface{}

Additional helper functions for converting between types

func UTXOsToMapSlice

func UTXOsToMapSlice(utxos []*UTXO) []map[string]interface{}

func ValidateAndConvertTransaction

func ValidateAndConvertTransaction(tx *thrylos.Transaction, db BlockchainDBInterface, publicKey ed25519.PublicKey, estimator GasEstimator, balance int64) error

func ValidateTransaction

func ValidateTransaction(tx Transaction, availableUTXOs map[string][]UTXO) bool

ValidateTransaction checks the internal consistency of a transaction, ensuring that the sum of inputs matches the sum of outputs. It is a crucial part of ensuring no value is created out of thin air within the blockchain system. ValidateTransaction checks the internal consistency of a transaction, ensuring that the sum of inputs matches the sum of outputs.

func VerifyTransaction

func VerifyTransaction(tx *thrylos.Transaction, utxos map[string][]*thrylos.UTXO, getPublicKeyFunc func(address string) (ed25519.PublicKey, error)) (bool, error)

VerifyTransaction ensures the overall validity of a transaction, including the correctness of its signature, the existence and ownership of UTXOs in its inputs, and the equality of input and output values.

func VerifyTransactionData

func VerifyTransactionData(tx *thrylos.Transaction, utxos map[string][]*thrylos.UTXO, getPublicKeyFunc GetPublicKeyFunc) (bool, error)

func VerifyTransactionSignature

func VerifyTransactionSignature(tx *thrylos.Transaction, ed25519PublicKey ed25519.PublicKey) error

VerifyTransactionSignature verifies both the Ed25519 of a given transaction.

Types

type BlockchainDBInterface

type BlockchainDBInterface interface {
	Bech32AddressExists(bech32Address string) (bool, error)
	GetBalance(address string, utxos map[string][]UTXO) (int64, error)
	SendTransaction(fromAddress, toAddress string, amount int, privKey *rsa.PrivateKey) (bool, error)
	SanitizeAndFormatAddress(address string) (string, error)
	InsertBlock(data []byte, blockNumber int) error
	GetLastBlockData() ([]byte, int, error)
	RetrievePublicKeyFromAddress(address string) (ed25519.PublicKey, error)
	PublicKeyExists(address string) (bool, error) // Added new method to the interface
	AddTransaction(tx *thrylos.Transaction) error
	UpdateUTXOs(inputs []UTXO, outputs []UTXO) error
	CreateUTXO(id, txID string, index int, address string, amount int64) (UTXO, error)
	GetUTXOsForUser(address string) ([]UTXO, error)
	GetUTXOs(address string) (map[string][]UTXO, error)
	CreateAndSignTransaction(txID string, inputs, outputs []UTXO, privKey *rsa.PrivateKey) (Transaction, error)
	InsertOrUpdateEd25519PublicKey(address string, ed25519PublicKey []byte) error
	RetrieveEd25519PublicKey(address string) (ed25519.PublicKey, error)
	StoreBlock(data []byte, blockNumber int) error
	RetrieveBlock(blockNumber int) ([]byte, error)
	BeginTransaction() (*TransactionContext, error)
	CommitTransaction(txn *TransactionContext) error
	RollbackTransaction(txn *TransactionContext) error
	SetTransaction(txn *TransactionContext, key []byte, value []byte) error
	GetUTXOsByAddress(address string) (map[string][]UTXO, error)
	GetAllUTXOs() (map[string][]UTXO, error)
	GetUTXOsForAddress(address string) ([]UTXO, error)
	AddUTXO(utxo UTXO) error // Add this line
	TransactionExists(txn *TransactionContext, txID string) (bool, error)
	RetrieveValidatorPublicKey(validatorAddress string) ([]byte, error)
	StoreValidatorPublicKey(validatorAddress string, publicKey []byte) error
	MarkUTXOAsSpent(txn *TransactionContext, utxo UTXO) error
	AddNewUTXO(txn *TransactionContext, utxo UTXO) error
	GetAllValidatorPublicKeys() (map[string]ed25519.PublicKey, error)
}

type DefaultNetwork

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

func NewDefaultNetwork

func NewDefaultNetwork() *DefaultNetwork

func (*DefaultNetwork) AddPeer

func (n *DefaultNetwork) AddPeer(address string)

func (*DefaultNetwork) BroadcastMessage

func (n *DefaultNetwork) BroadcastMessage(message []byte) error

func (*DefaultNetwork) GetPeerAddresses

func (n *DefaultNetwork) GetPeerAddresses() []string

func (*DefaultNetwork) IsConnected

func (n *DefaultNetwork) IsConnected(nodeAddress string) bool

func (*DefaultNetwork) RemovePeer

func (n *DefaultNetwork) RemovePeer(address string)

func (*DefaultNetwork) SendMessage

func (n *DefaultNetwork) SendMessage(nodeAddress string, message []byte) error

type GasEstimator

type GasEstimator interface {
	FetchGasEstimate(dataSize int, balance int64) (int, error)
}

GasEstimator defines an interface for fetching gas estimates.

type GetPublicKeyFunc

type GetPublicKeyFunc func(address string) (ed25519.PublicKey, error)

type NetworkInterface

type NetworkInterface interface {
	SendMessage(nodeAddress string, message []byte) error
	BroadcastMessage(message []byte) error
	GetPeerAddresses() []string
	IsConnected(nodeAddress string) bool
	AddPeer(address string)
	RemovePeer(address string)
}

NetworkInterface defines the methods required for network communication

type NetworkMessage

type NetworkMessage struct {
	Type      string      `json:"type"`
	Data      interface{} `json:"data"`
	Timestamp time.Time   `json:"timestamp"`
}

NetworkMessage represents a message sent between nodes

type Transaction

type Transaction struct {
	ID               string   `json:"id"`
	Timestamp        int64    `json:"timestamp"`
	Inputs           []UTXO   `json:"inputs"`
	Outputs          []UTXO   `json:"outputs"`
	EncryptedInputs  []byte   `json:"encryptedinputs,omitempty"`
	EncryptedOutputs []byte   `json:"encryptedoutputs,omitempty"`
	Signature        string   `json:"signature"`
	EncryptedAESKey  []byte   `json:"encryptedaeskey,omitempty"`
	PreviousTxIds    []string `json:"previoustxids,omitempty"`
	Sender           string   `json:"sender"`
	GasFee           int      `json:"gasfee"`
	Status           string   `json:"status,omitempty"`
	BlockHash        string   `json:"blockHash,omitempty"`
}

Transaction defines the structure for blockchain transactions, including its inputs, outputs, a unique identifier, and an optional signature. Transactions are the mechanism through which value is transferred within the blockchain.

func ConvertThrylosTransactionToLocal

func ConvertThrylosTransactionToLocal(tx *thrylos.Transaction) (Transaction, error)

ConvertThrylosTransactionToLocal converts a thrylos.Transaction back to your local Transaction type Updated

func CreateAndSignTransaction

func CreateAndSignTransaction(id string, sender string, inputs []UTXO, outputs []UTXO, ed25519PrivateKey ed25519.PrivateKey, aesKey []byte, estimator GasEstimator) (*Transaction, error)

Used only in the CLI Signer (not in the blockchain!)

func NewTransaction

func NewTransaction(id string, inputs []UTXO, outputs []UTXO) Transaction

NewTransaction creates a new Transaction instance with the specified ID, inputs, outputs, and records

func (*Transaction) SerializeWithoutSignature

func (tx *Transaction) SerializeWithoutSignature() ([]byte, error)

SerializeWithoutSignature generates a JSON representation of the transaction without including the signature. This is useful for verifying the transaction signature, as the signature itself cannot be part of the signed data.

func (*Transaction) Validate

func (tx *Transaction) Validate() error

Validate ensures the fields of Transaction are correct. Validate ensures the fields of Transaction are correct.

type TransactionContext

type TransactionContext struct {
	Txn      *badger.Txn
	UTXOs    map[string][]UTXO // Map of address to UTXOs
	Modified map[string]bool   // Track which addresses have modified UTXOs
	// contains filtered or unexported fields
}

TransactionContext wraps a BadgerDB transaction to manage its lifecycle.

func NewTransactionContext

func NewTransactionContext(txn *badger.Txn) *TransactionContext

NewTransactionContext creates a new context for a database transaction.

func (*TransactionContext) GetUTXOs

func (tc *TransactionContext) GetUTXOs(address string) []UTXO

GetUTXOs retrieves UTXOs for a specific address from the transaction context

func (*TransactionContext) MarkModified

func (tc *TransactionContext) MarkModified(address string)

MarkModified marks an address as having modified UTXOs

type TransactionForSigning

type TransactionForSigning struct {
	ID               string   `json:"id"`
	Timestamp        int64    `json:"timestamp"`
	Inputs           []UTXO   `json:"inputs"`
	Outputs          []UTXO   `json:"outputs"`
	EncryptedInputs  []byte   `json:"encryptedInputs,omitempty"`
	EncryptedOutputs []byte   `json:"encryptedOutputs,omitempty"`
	EncryptedAESKey  []byte   `json:"encryptedAESKey,omitempty"`
	PreviousTxIds    []string `json:"previousTxIds,omitempty"`
	Sender           string   `json:"sender"`
	GasFee           int      `json:"gasFee"`
}

type UTXO

type UTXO struct {
	ID            string `json:"id,omitempty"`
	TransactionID string `json:"transaction_id"` // Changed from transactionid
	Index         int    `json:"index"`
	OwnerAddress  string `json:"owner_address"` // Already correct
	Amount        int64  `json:"amount"`
	IsSpent       bool   `json:"is_spent"` // Changed from isspent
}

UTXO represents an Unspent Transaction Output, which is the output of a blockchain transaction that has not been spent and can be used as an input in a new transaction. UTXOs are fundamental to understanding a user's balance within the blockchain.

func CreateUTXO

func CreateUTXO(id, txID string, index int, owner string, amount int64) UTXO

CreateUTXO initializes a new UTXO with the specified attributes. This function is typically called when a transaction is processed, and its outputs are being determined.

func GetUTXO

func GetUTXO(txID string, index int) (*UTXO, error)

GetUTXO retrieves a UTXO by its key.

func GetUTXOsForUser

func GetUTXOsForUser(user string, allUTXOs map[string]UTXO) []UTXO

GetUTXOsForUser scans through all available UTXOs and returns those owned by a specific user. This function is crucial for determining a user's spendable balance.

func (UTXO) MarshalJSON

func (u UTXO) MarshalJSON() ([]byte, error)

MarshalJSON customizes the JSON representation of the UTXO struct. This can be useful for excluding certain fields from the JSON output or adding extra metadata when UTXOs are serialized.

func (*UTXO) ValidateUTXO

func (u *UTXO) ValidateUTXO() error

ValidateUTXO checks for the validity of the UTXO, ensuring its data conforms to expected formats and rules.

type UTXOCache

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

func NewUTXOCache

func NewUTXOCache(size int, bloomSize uint, falsePositiveRate float64) (*UTXOCache, error)

func (*UTXOCache) Add

func (uc *UTXOCache) Add(key string, utxo *UTXO)

func (*UTXOCache) Get

func (uc *UTXOCache) Get(key string) (*UTXO, bool)

func (*UTXOCache) Remove

func (uc *UTXOCache) Remove(key string)

Jump to

Keyboard shortcuts

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