Files

46 lines
824 B
Go

package chain
import (
"crypto/ed25519"
"encoding/hex"
"maps"
"strconv"
)
type CoinID struct {
TxHash string
Index int
}
func (id CoinID) Key() string { return id.TxHash + ":" + strconv.Itoa(id.Index) }
type Coin struct {
Amount int
Owner ed25519.PublicKey
}
func (c Coin) OwnerHex() string { return hex.EncodeToString(c.Owner) }
type UnspentCoin struct {
Coin
CoinID CoinID
}
type CoinStore map[string]*UnspentCoin
func (s CoinStore) Add(txHash string, idx int, coin Coin) {
id := CoinID{TxHash: txHash, Index: idx}
s[id.Key()] = &UnspentCoin{Coin: coin, CoinID: id}
}
func (s CoinStore) Spend(id CoinID) { delete(s, id.Key()) }
func (s CoinStore) Get(id CoinID) *UnspentCoin { return s[id.Key()] }
func (s CoinStore) Clone() CoinStore {
c := make(CoinStore, len(s))
maps.Copy(c, s)
return c
}