dlc

package
v0.0.0-...-3f4333e Latest Latest
Warning

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

Go to latest
Published: Jul 23, 2024 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Addresses

type Addresses map[Contractor]string

Addresses is addresses in string

type Builder

type Builder struct {
	Contract *DLC
	// contains filtered or unexported fields
}

Builder builds DLC by interacting with wallet

func NewBuilder

func NewBuilder(
	p Contractor, w wallet.Wallet, d *DLC) *Builder

NewBuilder createa a builder from DLC

func (*Builder) AcceptAdderss

func (b *Builder) AcceptAdderss(addr btcutil.Address) error

AcceptAdderss accepts address from the counterparty

func (*Builder) AcceptCETxSignatures

func (b *Builder) AcceptCETxSignatures(sigs [][]byte) error

AcceptCETxSignatures accepts CETx signatures received from the counterparty

func (*Builder) AcceptChangeAdderss

func (b *Builder) AcceptChangeAdderss(addr btcutil.Address) error

AcceptChangeAdderss accepts change address from the counterparty

func (*Builder) AcceptFundWitnesses

func (b *Builder) AcceptFundWitnesses(wits []wire.TxWitness)

AcceptFundWitnesses accepts witnesses for fund txins owned by the counerparty

func (*Builder) AcceptPubkey

func (b *Builder) AcceptPubkey(pub []byte) error

AcceptPubkey accepts counter party's public key

func (*Builder) AcceptRefundTxSignature

func (b *Builder) AcceptRefundTxSignature(sig []byte) error

AcceptRefundTxSignature verifies couterparty's given signature is valid and then

func (*Builder) AcceptUtxos

func (b *Builder) AcceptUtxos(utxos []Utxo) error

AcceptUtxos accepts utxos

func (*Builder) Address

func (b *Builder) Address() btcutil.Address

Address returns address to distribute fund

func (*Builder) ChangeAddress

func (b *Builder) ChangeAddress() btcutil.Address

ChangeAddress returns address to send change

func (*Builder) ExecuteContract

func (b *Builder) ExecuteContract() error

ExecuteContract sends CETx and closing tx

func (*Builder) FixDeal

func (b *Builder) FixDeal(fm *oracle.SignedMsg, idxs []int) error

FixDeal fixes a deal by a oracle's signature set by picking up required messages and sigs

func (*Builder) FixedDealAmt

func (b *Builder) FixedDealAmt() (btcutil.Amount, error)

FixedDealAmt returns fixed amt that the party will receive

func (*Builder) FundAmt

func (b *Builder) FundAmt() btcutil.Amount

FundAmt returns fund amount

func (*Builder) PrepareFundTx

func (b *Builder) PrepareFundTx() error

PrepareFundTx prepares fundtx ins and out

func (*Builder) PreparePubkey

func (b *Builder) PreparePubkey() error

PreparePubkey sets fund pubkey

func (*Builder) PublicKey

func (b *Builder) PublicKey() ([]byte, error)

PublicKey returns serialized public key (compressed)

func (*Builder) SendFundTx

func (b *Builder) SendFundTx() error

SendFundTx sends fund tx to the network

func (*Builder) SendRefundTx

func (b *Builder) SendRefundTx() error

SendRefundTx sends refund tx

func (*Builder) SetOraclePubkeySet

func (b *Builder) SetOraclePubkeySet(
	pubset *oracle.PubkeySet, idxs []int) error

SetOraclePubkeySet sets oracle's pubkey set

func (*Builder) SignContractExecutionTx

func (b *Builder) SignContractExecutionTx(deal *Deal, idx int) ([]byte, error)

SignContractExecutionTx signs a contract execution tx for a given party

func (*Builder) SignContractExecutionTxs

func (b *Builder) SignContractExecutionTxs() ([][]byte, error)

SignContractExecutionTxs signs contract execution txs for all deals

func (*Builder) SignFundTx

func (b *Builder) SignFundTx() ([]wire.TxWitness, error)

SignFundTx signs fund tx and return witnesses for the txins owned by the party

func (*Builder) SignRefundTx

func (b *Builder) SignRefundTx() ([]byte, error)

SignRefundTx creates signature for a refund tx, sets it, and returns it

func (*Builder) SignedClosingTx

func (b *Builder) SignedClosingTx(cetx *wire.MsgTx) (*wire.MsgTx, error)

SignedClosingTx constructs a closing tx with witness

func (*Builder) SignedContractExecutionTx

func (b *Builder) SignedContractExecutionTx() (*wire.MsgTx, error)

SignedContractExecutionTx returns a contract execution tx signed by both parties TODO: separate SignedContractExecutionTx and SignContractExecutionTx

func (*Builder) Utxos

func (b *Builder) Utxos() []Utxo

Utxos returns utxos

type ChangeAddressNotExistsError

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

ChangeAddressNotExistsError is raised when change address doesn't exist

type Conditions

type Conditions struct {
	NetParams      *chaincfg.Params              `validate:"required"`
	FixingTime     time.Time                     `validate:"required,gt=time.Now()"`
	FundAmts       map[Contractor]btcutil.Amount `validate:"required,funds,dive,gte=0"`
	FundFeerate    btcutil.Amount                `validate:"required,gt=0"` // fund fee rate (satoshi per byte)
	RedeemFeerate  btcutil.Amount                `validate:"required,gt=0"` // redeem fee rate (satoshi per byte)
	RefundLockTime uint32                        `validate:"required,gt=0"` // refund locktime (block height)
	Deals          []*Deal                       `validate:"required,gt=0,dive,required"`
	PremiumInfo    *PremiumInfo
}

Conditions contains conditions of a contract

func NewConditions

func NewConditions(
	net *chaincfg.Params,
	ftime time.Time,
	famt1, famt2 btcutil.Amount,
	ffeerate, rfeerate btcutil.Amount,
	refundLockTime uint32,
	deals []*Deal,
	info *PremiumInfo,
) (*Conditions, error)

NewConditions creates a new DLC conditions

func (*Conditions) MarshalJSON

func (conds *Conditions) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler

func (*Conditions) UnmarshalJSON

func (conds *Conditions) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler

type ConditionsJSON

type ConditionsJSON struct {
	Net            string             `json:"network"`
	FixingTime     int64              `json:"fixing_time"`
	FundAmts       map[Contractor]int `json:"fund_amts"`
	FundFeerate    int                `json:"fund_feerate"`
	RedeemFeerate  int                `json:"redeem_feerate"`
	RefundLockTime uint32             `json:"refund_locktime"`
	Deals          []*DealJSON        `json:"deals"`
	PremiumInfo    *PremiumInfoJSON   `json:"premium_info"`
}

ConditionsJSON is contract conditions in JSON format

type Contractor

type Contractor int

Contractor represents a contractor type

const (
	// FirstParty is a contractor who creates offer
	FirstParty Contractor = 0
	// SecondParty is a contractor who accepts offer
	SecondParty Contractor = 1
)

func (Contractor) String

func (p Contractor) String() string

String represents contractor in string format

type DLC

type DLC struct {
	Conds       *Conditions
	Oracle      *Oracle
	Pubs        map[Contractor]*btcec.PublicKey // pubkeys used for script and txout
	Addrs       map[Contractor]btcutil.Address  // addresses used to distribute funds after fixing deal
	ChangeAddrs map[Contractor]btcutil.Address  // addresses used to send change
	Utxos       map[Contractor][]*Utxo
	FundWits    map[Contractor][]wire.TxWitness // TODO: change to fund signatures
	RefundSigs  map[Contractor][]byte           // signatures for refund tx
	ExecSigs    [][]byte                        // counterparty's signatures for CETxs
}

DLC contains all information required for DLC contract including FundTx, SettlementTx, RefundTx

func NewDLC

func NewDLC(conds *Conditions) *DLC

NewDLC initializes DLC

func (*DLC) AcceptCETxSignature

func (d *DLC) AcceptCETxSignature(party Contractor, idx int, sig []byte) error

AcceptCETxSignature sets a signature if it's valid for an identified CETx

func (*DLC) Addresses

func (d *DLC) Addresses() Addresses

Addresses converts btcutil.Address to string

func (*DLC) ChangeAddresses

func (d *DLC) ChangeAddresses() Addresses

ChangeAddresses converts btcutil.Address to string

func (*DLC) ClosingTx

func (d *DLC) ClosingTx(
	p Contractor, cetx *wire.MsgTx) (*wire.MsgTx, error)

ClosingTx constructs a tx that redeems a given CET

func (*DLC) ContractAbandonmentTx

func (d *DLC) ContractAbandonmentTx(p Contractor) (*wire.MsgTx, error)

ContractAbandonmentTx creates tx that sends all fund to the counterparty Note: This transaction isn't useful in the realworld, but is necessary for PoC

func (*DLC) ContractExecutionTx

func (d *DLC) ContractExecutionTx(
	party Contractor, deal *Deal, dID int) (*wire.MsgTx, error)

ContractExecutionTx constructs a contract execution tx (CET) using pubkeys and given condition. Both parties have different transactions signed by the other side.

txins:

[0]:fund transaction output[0]

txouts:

[0]:settlement script
[1]:p2wpkh (option)

func (*DLC) ContractID

func (d *DLC) ContractID() (string, error)

ContractID returns contract ID Use fund txid at this moment

func (*DLC) Deal

func (d *DLC) Deal(idx int) (*Deal, error)

Deal gets a deal by id

func (*DLC) DealByMsgs

func (d *DLC) DealByMsgs(msgs [][]byte) (idx int, deal *Deal, err error)

DealByMsgs finds a deal by messages

func (*DLC) DepositAmt

func (d *DLC) DepositAmt(p Contractor) btcutil.Amount

DepositAmt calculates fund amount + fees

func (*DLC) FixDeal

func (d *DLC) FixDeal(msgs [][]byte, sigs [][]byte) error

FixDeal fixes a deal by setting the signature provided by oracle

func (*DLC) FixedContractExecutionTx

func (d *DLC) FixedContractExecutionTx(p Contractor) (*wire.MsgTx, error)

FixedContractExecutionTx returns fixed CETx

func (*DLC) FixedDeal

func (d *DLC) FixedDeal() (idx int, deal *Deal, err error)

FixedDeal returns a fixed deal

func (*DLC) FundTx

func (d *DLC) FundTx() (*wire.MsgTx, error)

FundTx constructs fund tx using prepared fund tx requirements

func (*DLC) HasDealFixed

func (d *DLC) HasDealFixed() bool

HasDealFixed checks if a deal has been fixed

func (*DLC) ParseAddresses

func (d *DLC) ParseAddresses(addrs Addresses) error

ParseAddresses parses address string

func (*DLC) ParseChangeAddresses

func (d *DLC) ParseChangeAddresses(addrs Addresses) error

ParseChangeAddresses parses address string

func (*DLC) ParsePublicKeys

func (d *DLC) ParsePublicKeys(pubs PublicKeys) error

ParsePublicKeys parses public key hex strings

func (*DLC) PrepareOracleCommitments

func (d *DLC) PrepareOracleCommitments(
	V *btcec.PublicKey, Rs []*btcec.PublicKey) error

PrepareOracleCommitments prepares oracle's commitments for all deals

func (*DLC) PublicKeys

func (d *DLC) PublicKeys() PublicKeys

PublicKeys converts btcec.PublicKey to hex string

func (*DLC) RefundTx

func (d *DLC) RefundTx() (*wire.MsgTx, error)

RefundTx creates refund tx refund transaction input:

[0]:fund transaction output[0]
    Sequence (0xfeffffff LE)

output:

[0]:p2wpkh a
[1]:p2wpkh b

locktime:

Value decided by contract.

func (*DLC) SignedFundTx

func (d *DLC) SignedFundTx() (*wire.MsgTx, error)

SignedFundTx constructs signed fundtx

func (*DLC) SignedRefundTx

func (d *DLC) SignedRefundTx() (*wire.MsgTx, error)

SignedRefundTx returns a refund tx with its witness signature

func (*DLC) VerifyRefundTx

func (d *DLC) VerifyRefundTx(sig []byte, pub *btcec.PublicKey) error

VerifyRefundTx verifies the refund transaction signature. Returns nil if the passed signature is valid and corresponds to the passed in public key, and an error if it isnt.

type Deal

type Deal struct {
	Amts map[Contractor]btcutil.Amount `validate:"required,dive,gte=0"`
	Msgs [][]byte                      `validate:"required,gt=0"`
}

Deal contains information about the distributed amounts, commitment messages, and signatures of fixed messages

func NewDeal

func NewDeal(amt1, amt2 btcutil.Amount, msgs [][]byte) *Deal

NewDeal creates a new deal

type DealJSON

type DealJSON struct {
	Amts map[Contractor]int `json:"amts"`
	Msgs [][]byte           `json:"msgs"`
}

DealJSON is DLC deals in JSON format

type InvalidNetworkNameError

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

InvalidNetworkNameError is used when invalid network name is given

type NoFixedDealError

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

NoFixedDealError is an error for a case when no deals has been fixed yet

type NotEnoughFeesError

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

NotEnoughFeesError is an error that txins aren't enough for txouts

type Oracle

type Oracle struct {
	PubkeySet   *oracle.PubkeySet  // Oracle's pubkey set
	Commitments []*btcec.PublicKey // Commitments for deals
	Sig         []byte             // Signature for a fixed deal
	SignedMsgs  [][]byte           // Messages signed by Oracle
}

Oracle contains pubkeys and commitments and signature received from oracle

func NewOracle

func NewOracle(n int) *Oracle

NewOracle initializes oracle

func (*Oracle) MarshalJSON

func (o *Oracle) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler

func (*Oracle) UnmarshalJSON

func (o *Oracle) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler

type OracleJSON

type OracleJSON struct {
	PubkeySet   *oracle.PubkeySetJSON `json:"pubkey"`
	Commitments []string              `json:"commitments"`
	Sig         []byte                `json:"sig"`
	SignedMsgs  [][]byte              `json:"signed_msgs"`
}

OracleJSON is oracle information in JSON format

type PremiumInfo

type PremiumInfo struct {
	PremiumDestAddress btcutil.Address `validate:"required"`
	PremiumAmount      btcutil.Amount  `validate:"required,gt=0"`
	PayingParty        Contractor      `validate:"oneof= 0 1"`
}

func NewPremiumInfo

func NewPremiumInfo(premiumAddress btcutil.Address, premiumAmount btcutil.Amount, payingParty Contractor) (*PremiumInfo, error)

type PremiumInfoJSON

type PremiumInfoJSON struct {
	PremiumDestAddress string `json:"premium_dest_address"`
	PremiumAmount      int    `json:"premium_amount"`
	PayingParty        int    `json:"paying_party"`
}

PremiumInfoJSON is PremiumInfo in JSON format

type PubkeyNotExistsError

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

PubkeyNotExistsError is error when either public key doesn't exist

type PublicKeys

type PublicKeys map[Contractor]string

PublicKeys is public keys in hex string format

type Utxo

Utxo is alias of btcjson.ListUnspentResult

Jump to

Keyboard shortcuts

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