mtg

package
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: Jan 25, 2024 License: Apache-2.0 Imports: 21 Imported by: 11

README

MTG

This module can bootstrap a MTG application very effortlessly, it's as simple as a few lines of code.

func (rw *RefundWorker) ProcessOutput(ctx context.Context, out *mtg.Output) {
	receivers := []string{out.Sender}
	traceId := mixin.UniqueConversationID(out.UTXOID, "refund")
	err := rw.grp.BuildTransaction(ctx, out.AssetID, receivers, 1, out.Amount.String(), "refund", traceId)
	if err != nil {
		panic(err)
	}
}

group, _ := mtg.BuildGroup(ctx, db, conf)
rw := NewRefundrWorker(ctx, group, conf)
group.AddWorker(rw)
group.Run(ctx)

The group will call every workers added, and the worker just needs to implement the ProcessOutput interface. The code above is a very simple worker that refunds all the payments received.

Documentation

Index

Constants

View Source
const (
	ActionStateInitial = 10
	ActionStateDone    = 11
)
View Source
const (
	CollectibleMetaTokenId  = "2f8aa18a-3cb8-31d5-95bc-5a4f2e25dc2f"
	CollectibleMixinAssetId = "1700941284a95f31b25ec8c546008f208f88eee4419ccdcdbe6e3195e60128ca"
)
View Source
const (
	IterationActionAdd    = 11
	IterationActionRemove = 12
)
View Source
const (
	NMPrefix  = "NFO"
	NMVersion = 0x00
)
View Source
const (
	OutputStateUnspent = 10
	OutputStateSigned  = 11
	OutputStateSpent   = 12

	OutputTypeMultisig    = "multisig_utxo"
	OutputTypeCollectible = "non_fungible_output"
)
View Source
const (
	TransactionStateInitial  = 10
	TransactionStateSigning  = 11
	TransactionStateSigned   = 12
	TransactionStateSnapshot = 13

	OutputsBatchSize          = 36
	CompactionTransactionMemo = "COMPACTION"
	StorageAssetId            = "c94ac88f-4671-3976-b60a-09064f1811e8"
	StorageReceiverId         = "773e5e77-4107-45c2-b648-8fc722ed77f5"
)

Variables

View Source
var (
	CompressionVersionZero   = []byte{0, 0, 0, 0}
	CompressionVersionLatest = CompressionVersionZero
)
View Source
var (
	NMDefaultCollectionKey = uuid.Nil.Bytes()

	NMDefaultChain uuid.UUID
	NMDefaultClass []byte
)

Functions

func BuildExtraNFO

func BuildExtraNFO(extra []byte) []byte

func BuildMintNFO

func BuildMintNFO(collection string, token []byte, hash crypto.Hash) []byte

func CompressMsgpackMarshalPanic added in v0.5.0

func CompressMsgpackMarshalPanic(val any) []byte

func DecodeMixinExtra

func DecodeMixinExtra(memo string) *mixinExtraPack

func DecompressMsgpackUnmarshal added in v0.5.0

func DecompressMsgpackUnmarshal(data []byte, val any) error

func EncodeMixinExtra

func EncodeMixinExtra(groupId, traceId, memo string) string

func MsgpackMarshalPanic

func MsgpackMarshalPanic(val any) []byte

func MsgpackUnmarshal

func MsgpackUnmarshal(data []byte, val any) error

Types

type Action

type Action struct {
	UTXOID    string
	CreatedAt time.Time
	State     int
}

type Clock

type Clock struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func NewClock

func NewClock(store Store) (*Clock, error)

func (*Clock) Now

func (c *Clock) Now() time.Time

type CollectibleOutput

type CollectibleOutput struct {
	Type               string
	UserId             string
	OutputId           string
	TokenId            string
	TransactionHash    crypto.Hash
	OutputIndex        int
	Amount             decimal.Decimal
	SendersThreshold   int64
	Senders            []string
	ReceiversThreshold int64
	Receivers          []string
	Memo               string
	CreatedAt          time.Time
	UpdatedAt          time.Time
	SignedBy           string
	SignedTx           string
	State              int
}

func (*CollectibleOutput) StateName

func (out *CollectibleOutput) StateName() string

func (*CollectibleOutput) Unified

func (o *CollectibleOutput) Unified() *UnifiedOutput

type CollectibleTransaction

type CollectibleTransaction struct {
	TraceId   string
	State     int
	Receivers []string
	Threshold int
	Amount    string
	NFO       []byte
	Raw       []byte
	Hash      crypto.Hash
	UpdatedAt time.Time
	TokenId   string
}

type Configuration

type Configuration struct {
	App struct {
		ClientId   string `toml:"client-id"`
		SessionId  string `toml:"session-id"`
		PrivateKey string `toml:"private-key"`
		PinToken   string `toml:"pin-token"`
		PIN        string `toml:"pin"`
	} `toml:"app"`
	Genesis struct {
		Members   []string `toml:"members"`
		Threshold int      `toml:"threshold"`
		Timestamp int64    `toml:"timestamp"`
	} `toml:"genesis"`
	GroupSize        int   `toml:"group-size"`
	LoopWaitDuration int64 `toml:"loop-wait-duration"`
}

func Setup

func Setup(path string) (*Configuration, error)

type Group

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

func BuildGroup

func BuildGroup(ctx context.Context, store Store, conf *Configuration) (*Group, error)

func (*Group) AddNode

func (grp *Group) AddNode(id string, threshold int, timestamp time.Time) error

func (*Group) AddWorker

func (grp *Group) AddWorker(wkr Worker)

func (*Group) BuildCollectibleMintTransaction

func (grp *Group) BuildCollectibleMintTransaction(ctx context.Context, receivers []string, threshold int, nfo []byte) error

func (*Group) BuildCollectibleTransferTransaction

func (grp *Group) BuildCollectibleTransferTransaction(ctx context.Context, receivers []string, threshold int, memo string, tokenId, traceId string) error

func (*Group) BuildStorageTransaction added in v0.5.0

func (grp *Group) BuildStorageTransaction(ctx context.Context, data []byte, groupId string) (*Transaction, error)

func (*Group) BuildTransaction

func (grp *Group) BuildTransaction(ctx context.Context, assetId string, receivers []string, threshold int, amount, memo string, traceId, groupId string) error

the app should decide a unique trace id so that the MTG will not double spend

func (*Group) BuildTransactionWithReferences added in v0.5.0

func (grp *Group) BuildTransactionWithReferences(ctx context.Context, assetId string, receivers []string, threshold int, amount, memo string, traceId, groupId string, references []crypto.Hash) error

func (*Group) GenesisId

func (grp *Group) GenesisId() string

func (*Group) GetMembers

func (grp *Group) GetMembers() []string

func (*Group) GetThreshold

func (grp *Group) GetThreshold() int

func (*Group) ListActiveNodes

func (grp *Group) ListActiveNodes() ([]string, int, time.Time, error)

func (*Group) ListOutputsForAsset

func (grp *Group) ListOutputsForAsset(groupId, assetId, state string, limit int) ([]*Output, error)

func (*Group) ListOutputsForTransaction added in v0.4.1

func (grp *Group) ListOutputsForTransaction(traceId string) ([]*Output, error)

func (*Group) RemoveNode

func (grp *Group) RemoveNode(id string, threshold int, timestamp time.Time) error

func (*Group) Run

func (grp *Group) Run(ctx context.Context)

func (*Group) SetOutputGrouper

func (grp *Group) SetOutputGrouper(per func(out *Output) string)

func (*Group) Synced added in v0.4.0

func (grp *Group) Synced() (bool, error)

type Iteration

type Iteration struct {
	Action    int
	NodeId    string
	Threshold int
	CreatedAt time.Time
}

a node joins or leaves the group with an iteration this is for the evolution mechanism of MTG TODO not implemented yet

type NFOMemo

type NFOMemo struct {
	Prefix  string
	Version byte

	Mask       uint64
	Chain      uuid.UUID // 16 bytes
	Class      []byte    // 64 bytes contract address
	Collection uuid.UUID // 16 bytes
	Token      []byte    // 64 bytes hash of content

	Extra []byte
}

func DecodeNFOMemo

func DecodeNFOMemo(b []byte) (*NFOMemo, error)

func (*NFOMemo) Encode

func (nm *NFOMemo) Encode() []byte

func (*NFOMemo) Indexes

func (nm *NFOMemo) Indexes() []int

func (*NFOMemo) Mark

func (nm *NFOMemo) Mark(indexes []int)

func (*NFOMemo) WillMint

func (nm *NFOMemo) WillMint() bool

type Output

type Output struct {
	GroupId         string
	UserID          string
	UTXOID          string
	AssetID         string
	TransactionHash crypto.Hash
	OutputIndex     int
	Sender          string
	Amount          decimal.Decimal
	Threshold       uint8
	Members         []string
	Memo            string
	State           int
	CreatedAt       time.Time
	UpdatedAt       time.Time
	SignedBy        string
	SignedTx        string
}

func (*Output) StateName

func (out *Output) StateName() string

func (*Output) Unified

func (o *Output) Unified() *UnifiedOutput

type Store

type Store interface {
	WriteProperty(key, val []byte) error
	ReadProperty(key []byte) ([]byte, error)

	WriteIteration(ir *Iteration) error
	ListIterations() ([]*Iteration, error)

	WriteOutput(utxo *Output, traceId string) error
	WriteOutputs(utxos []*Output, traceId string) error

	ListOutputsForTransaction(traceId string) ([]*Output, error)
	ListOutputsForAsset(groupId string, state, assetId string, limit int) ([]*Output, error)

	WriteAction(act *Action) error
	ListActions(limit int) ([]*UnifiedOutput, error)

	WriteTransaction(tx *Transaction) error
	ReadTransactionByTraceId(traceId string) (*Transaction, error)
	ReadTransactionByHash(hash crypto.Hash) (*Transaction, error)
	ListTransactions(state int, limit int) ([]*Transaction, error)
	DeleteTransaction(tx *Transaction) error

	WriteCollectibleOutput(utxo *CollectibleOutput, traceId string) error
	WriteCollectibleOutputs(utxos []*CollectibleOutput, traceId string) error
	ListCollectibleOutputsForTransaction(traceId string) ([]*CollectibleOutput, error)
	ListCollectibleOutputsForToken(state, tokenId string, limit int) ([]*CollectibleOutput, error)

	WriteCollectibleTransaction(traceId string, tx *CollectibleTransaction) error
	ReadCollectibleTransaction(traceId string) (*CollectibleTransaction, error)
	ReadCollectibleTransactionByHash(hash crypto.Hash) (*CollectibleTransaction, error)
	ListCollectibleTransactions(state int, limit int) ([]*CollectibleTransaction, error)
}

type Transaction

type Transaction struct {
	GroupId    string
	TraceId    string
	State      int
	AssetId    string
	Receivers  []string
	Threshold  int
	Amount     string
	Memo       string
	Raw        []byte
	Hash       crypto.Hash
	References []crypto.Hash
	UpdatedAt  time.Time
}

type UnifiedOutput

type UnifiedOutput struct {
	Type            string          `json:"type"`
	UserId          string          `json:"user_id"`
	TransactionHash crypto.Hash     `json:"transaction_hash"`
	OutputIndex     int             `json:"output_index"`
	Amount          decimal.Decimal `json:"amount"`
	Memo            string          `json:"memo"`
	CreatedAt       time.Time       `json:"created_at"`
	UpdatedAt       time.Time       `json:"updated_at"`
	SignedBy        string          `json:"signed_by"`
	SignedTx        string          `json:"signed_tx"`
	State           string          `json:"state"`

	UnifiedOutputId           string   `json:"output_id"`
	UnifiedTokenId            string   `json:"token_id"`
	UnifiedSendersThreshold   int64    `json:"senders_threshold"`
	UnifiedSenders            []string `json:"senders"`
	UnifiedReceiversThreshold int64    `json:"receivers_threshold"`
	UnifiedReceivers          []string `json:"receivers"`

	UnifiedUTXOID    string   `json:"utxo_id"`
	UnifiedAssetId   string   `json:"asset_id"`
	UnifiedThreshold int64    `json:"threshold"`
	UnifiedMembers   []string `json:"members"`
	UnifiedSender    string   `json:"sender"`
}

func (*UnifiedOutput) AsCollectible

func (o *UnifiedOutput) AsCollectible() *CollectibleOutput

func (*UnifiedOutput) AsMultisig

func (o *UnifiedOutput) AsMultisig() *Output

func (*UnifiedOutput) UniqueId

func (o *UnifiedOutput) UniqueId() string

type Worker

type Worker interface {
	// handle the output and a true return value interrupts workers loop
	ProcessOutput(context.Context, *Output) bool
	ProcessCollectibleOutput(context.Context, *CollectibleOutput) bool
}

Jump to

Keyboard shortcuts

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