Documentation ¶
Overview ¶
Package txbuilder builds a Chain Protocol transaction from a list of actions.
Index ¶
- Variables
- func FinalizeTx(ctx context.Context, c *protocol.Chain, tx *types.Tx) error
- func MissingFieldsError(name ...string) error
- func Sign(ctx context.Context, tpl *Template, auth string, signFn SignFunc) error
- func SignProgress(txTemplate *Template) bool
- type Action
- type DataWitness
- type RawTxSigWitness
- type Receiver
- type SignFunc
- type SignatureWitness
- type SigningInstruction
- type Template
- type TemplateBuilder
- func (b *TemplateBuilder) AddInput(in *types.TxInput, sigInstruction *SigningInstruction) error
- func (b *TemplateBuilder) AddOutput(o *types.TxOutput) error
- func (b *TemplateBuilder) Build() (*Template, *types.TxData, error)
- func (b *TemplateBuilder) MaxTime() time.Time
- func (b *TemplateBuilder) OnBuild(buildFn func() error)
- func (b *TemplateBuilder) OnRollback(rollbackFn func())
- func (b *TemplateBuilder) RestrictMaxTime(t time.Time)
- func (b *TemplateBuilder) RestrictMinTime(t time.Time)
Constants ¶
This section is empty.
Variables ¶
var ( // ErrRejected means the network rejected a tx (as a double-spend) ErrRejected = errors.New("transaction rejected") // ErrMissingRawTx means missing transaction ErrMissingRawTx = errors.New("missing raw tx") // ErrBadInstructionCount means too many signing instructions compare with inputs ErrBadInstructionCount = errors.New("too many signing instructions in template") )
var ( // ErrNoTxSighashCommitment is returned when no input commits to the // complete transaction. // To permit idempotence of transaction submission, we require at // least one input to commit to the complete transaction (what you get // when you build a transaction with allow_additional_actions=false). ErrNoTxSighashCommitment = errors.New("no commitment to tx sighash") // ErrNoTxSighashAttempt is returned when there was no attempt made to sign // this transaction. ErrNoTxSighashAttempt = errors.New("no tx sighash attempted") // ErrTxSignatureFailure is returned when there was an attempt to sign this // transaction, but it failed. ErrTxSignatureFailure = errors.New("tx signature was attempted but failed") )
var ( //ErrBadRefData means invalid reference data ErrBadRefData = errors.New("transaction reference data does not match previous template's reference data") //ErrBadTxInputIdx means unsigned tx input ErrBadTxInputIdx = errors.New("unsigned tx missing input") //ErrBadWitnessComponent means invalid witness component ErrBadWitnessComponent = errors.New("invalid witness component") //ErrBadAmount means invalid asset amount ErrBadAmount = errors.New("bad asset amount") //ErrBlankCheck means unsafe transaction ErrBlankCheck = errors.New("unsafe transaction: leaves assets free to control") //ErrAction means errors occurred in actions ErrAction = errors.New("errors occurred in one or more actions") //ErrMissingFields means missing required fields ErrMissingFields = errors.New("required field is missing") )
errors
var ErrEmptyProgram = errors.New("empty signature program")
ErrEmptyProgram is a type of error
Functions ¶
func FinalizeTx ¶
FinalizeTx validates a transaction signature template, assembles a fully signed tx, and stores the effects of its changes on the UTXO set.
func MissingFieldsError ¶
MissingFieldsError returns a wrapped error ErrMissingFields with a data item containing the given field names.
func SignProgress ¶
SignProgress check is all the sign requirement are satisfy
Types ¶
type Action ¶
type Action interface {
Build(context.Context, *TemplateBuilder) error
}
Action is a interface
func DecodeControlAddressAction ¶
DecodeControlAddressAction convert input data to action struct
func DecodeControlProgramAction ¶
DecodeControlProgramAction convert input data to action struct
func DecodeControlReceiverAction ¶
DecodeControlReceiverAction convert input data to action struct
func DecodeRetireAction ¶
DecodeRetireAction convert input data to action struct
type DataWitness ¶
DataWitness used sign transaction
func (DataWitness) MarshalJSON ¶
func (dw DataWitness) MarshalJSON() ([]byte, error)
MarshalJSON marshal DataWitness
type RawTxSigWitness ¶
type RawTxSigWitness struct { Quorum int `json:"quorum"` Keys []keyID `json:"keys"` Sigs []chainjson.HexBytes `json:"signatures"` }
RawTxSigWitness is like SignatureWitness but doesn't involve signature programs.
func (RawTxSigWitness) MarshalJSON ¶
func (sw RawTxSigWitness) MarshalJSON() ([]byte, error)
MarshalJSON convert struct to json
type Receiver ¶
type Receiver struct { ControlProgram chainjson.HexBytes `json:"control_program,omitempty"` Address string `json:"address,omitempty"` }
Receiver encapsulates information about where to send assets.
type SignFunc ¶
SignFunc is the function passed into Sign that produces a signature for a given xpub, derivation path, and hash.
type SignatureWitness ¶
type SignatureWitness struct { // Quorum is the number of signatures required. Quorum int `json:"quorum"` // Keys are the identities of the keys to sign with. Keys []keyID `json:"keys"` // Program is the predicate part of the signature program, whose hash is what gets // signed. If empty, it is computed during Sign from the outputs // and the current input of the transaction. Program chainjson.HexBytes `json:"program"` // Sigs are signatures of Program made from each of the Keys // during Sign. Sigs []chainjson.HexBytes `json:"signatures"` }
SignatureWitness is a sign struct
func (SignatureWitness) MarshalJSON ¶
func (sw SignatureWitness) MarshalJSON() ([]byte, error)
MarshalJSON convert struct to json
type SigningInstruction ¶
type SigningInstruction struct { Position uint32 `json:"position"` WitnessComponents []witnessComponent `json:"witness_components,omitempty"` }
SigningInstruction gives directions for signing inputs in a TxTemplate.
func (*SigningInstruction) AddRawWitnessKeys ¶
func (si *SigningInstruction) AddRawWitnessKeys(xpubs []chainkd.XPub, path [][]byte, quorum int)
AddRawWitnessKeys adds a SignatureWitness with the given quorum and list of keys derived by applying the derivation path to each of the xpubs.
func (*SigningInstruction) AddWitnessKeys ¶
func (si *SigningInstruction) AddWitnessKeys(xpubs []chainkd.XPub, path [][]byte, quorum int)
AddWitnessKeys adds a SignatureWitness with the given quorum and list of keys derived by applying the derivation path to each of the xpubs.
func (*SigningInstruction) UnmarshalJSON ¶
func (si *SigningInstruction) UnmarshalJSON(b []byte) error
UnmarshalJSON unmarshal SigningInstruction
type Template ¶
type Template struct { Transaction *types.Tx `json:"raw_transaction"` SigningInstructions []*SigningInstruction `json:"signing_instructions"` // AllowAdditional affects whether Sign commits to the tx sighash or // to individual details of the tx so far. When true, signatures // commit to tx details, and new details may be added but existing // ones cannot be changed. When false, signatures commit to the tx // as a whole, and any change to the tx invalidates the signature. AllowAdditional bool `json:"allow_additional_actions"` }
Template represents a partially- or fully-signed transaction.
func Build ¶
func Build(ctx context.Context, tx *types.TxData, actions []Action, maxTime time.Time, timeRange uint64) (*Template, error)
Build builds or adds on to a transaction. Initially, inputs are left unconsumed, and destinations unsatisfied. Build partners then satisfy and consume inputs and destinations. The final party must ensure that the transaction is balanced before calling finalize.
type TemplateBuilder ¶
type TemplateBuilder struct {
// contains filtered or unexported fields
}
TemplateBuilder is struct of building transactions
func NewBuilder ¶
func NewBuilder(maxTime time.Time) *TemplateBuilder
NewBuilder return new TemplateBuilder instance
func (*TemplateBuilder) AddInput ¶
func (b *TemplateBuilder) AddInput(in *types.TxInput, sigInstruction *SigningInstruction) error
AddInput add inputs of transactions
func (*TemplateBuilder) AddOutput ¶
func (b *TemplateBuilder) AddOutput(o *types.TxOutput) error
AddOutput add outputs of transactions
func (*TemplateBuilder) Build ¶
func (b *TemplateBuilder) Build() (*Template, *types.TxData, error)
Build build transactions with template
func (*TemplateBuilder) MaxTime ¶
func (b *TemplateBuilder) MaxTime() time.Time
MaxTime return maxTime
func (*TemplateBuilder) OnBuild ¶
func (b *TemplateBuilder) OnBuild(buildFn func() error)
OnBuild registers a function that will be run after all actions have been successfully built.
func (*TemplateBuilder) OnRollback ¶
func (b *TemplateBuilder) OnRollback(rollbackFn func())
OnRollback registers a function that can be used to attempt to undo any side effects of building actions. For example, it might cancel any reservations reservations that were made on UTXOs in a spend action. Rollback is a "best-effort" operation and not guaranteed to succeed. Each action's side effects, if any, must be designed with this in mind.
func (*TemplateBuilder) RestrictMaxTime ¶
func (b *TemplateBuilder) RestrictMaxTime(t time.Time)
RestrictMaxTime set maxTime
func (*TemplateBuilder) RestrictMinTime ¶
func (b *TemplateBuilder) RestrictMinTime(t time.Time)
RestrictMinTime set minTime