txbuilder

package
v0.0.0-...-4e0ae02 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2016 License: AGPL-3.0 Imports: 17 Imported by: 0

Documentation

Overview

Package txbuilder builds a Chain Protocol transaction from a list of actions.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrRejected means the network rejected a tx (as a double-spend)
	ErrRejected = errors.New("transaction rejected")

	ErrMissingRawTx        = errors.New("missing raw tx")
	ErrBadInstructionCount = errors.New("too many signing instructions in template")
)
View Source
var (
	ErrBadRefData          = errors.New("transaction reference data does not match previous template's reference data")
	ErrBadTxInputIdx       = errors.New("unsigned tx missing input")
	ErrBadWitnessComponent = errors.New("invalid witness component")
	ErrBadAmount           = errors.New("bad asset amount")
	ErrBlankCheck          = errors.New("unsafe transaction: leaves assets free to control")
	ErrAction              = errors.New("errors occurred in one or more actions")
	ErrMissingFields       = errors.New("required field is missing")
)
View Source
var ErrEmptyProgram = errors.New("empty signature program")
View Source
var ErrNoTxSighashCommitment = errors.New("no commitment to tx sighash")

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).

View Source
var Generator *rpc.Client

Functions

func FinalizeTx

func FinalizeTx(ctx context.Context, c *protocol.Chain, tx *bc.Tx) error

FinalizeTx validates a transaction signature template, assembles a fully signed tx, and stores the effects of its changes on the UTXO set.

func MissingFieldsError

func MissingFieldsError(name ...string) error

MissingFieldsError returns a wrapped error ErrMissingFields with a data item containing the given field names.

func Sign

func Sign(ctx context.Context, tpl *Template, xpubs []string, signFn SignFunc) error

Types

type Action

type Action interface {
	// TODO(bobg, jeffomatic): see if there is a way to remove the maxTime
	// parameter from the build call. One possibility would be to treat TTL as
	// a transaction-wide default parameter that gets folded into actions that
	// care about it. This could happen when the build request is being
	// deserialized.
	Build(context.Context, time.Time, *TemplateBuilder) error
}

func DecodeControlProgramAction

func DecodeControlProgramAction(data []byte) (Action, error)

func DecodeSetTxRefDataAction

func DecodeSetTxRefDataAction(data []byte) (Action, error)

type KeyID

type KeyID struct {
	XPub           string               `json:"xpub"`
	DerivationPath []chainjson.HexBytes `json:"derivation_path"`
}

func KeyIDs

func KeyIDs(xpubs []chainkd.XPub, path [][]byte) []KeyID

KeyIDs produces KeyIDs from a list of xpubs and a derivation path (applied to all the xpubs).

type SignFunc

type SignFunc func(context.Context, string, [][]byte, [32]byte) ([]byte, error)

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"`
}

func (SignatureWitness) MarshalJSON

func (sw SignatureWitness) MarshalJSON() ([]byte, error)

func (SignatureWitness) Materialize

func (sw SignatureWitness) Materialize(tpl *Template, index int, args *[][]byte) error

func (*SignatureWitness) Sign

func (sw *SignatureWitness) Sign(ctx context.Context, tpl *Template, index int, xpubs []string, signFn SignFunc) error

Sign populates sw.Sigs with as many signatures of the predicate in sw.Program as it can from the overlapping set of keys in sw.Keys and xpubs.

If sw.Program is empty, it is populated with an _inferred_ predicate: a program committing to aspects of the current transaction. Specifically, the program commits to:

  • the mintime and maxtime of the transaction (if non-zero)
  • the outpoint and (if non-empty) reference data of the current input
  • the assetID, amount, control program, and (if non-empty) reference data of each output.

type SigningInstruction

type SigningInstruction struct {
	Position int `json:"position"`
	bc.AssetAmount
	WitnessComponents []WitnessComponent `json:"witness_components,omitempty"`
}

SigningInstruction gives directions for signing inputs in a TxTemplate.

func (*SigningInstruction) AddWitnessKeys

func (si *SigningInstruction) AddWitnessKeys(keys []KeyID, quorum int)

func (*SigningInstruction) UnmarshalJSON

func (si *SigningInstruction) UnmarshalJSON(b []byte) error

type Template

type Template struct {
	Transaction         *bc.TxData            `json:"raw_transaction"`
	SigningInstructions []*SigningInstruction `json:"signing_instructions"`

	// Local indicates that all inputs to the transaction are signed
	// exclusively by keys managed by this Core. Whenever accepting
	// a template from an external Core, `Local` should be set to
	// false.
	Local bool `json:"local"`

	// 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"`
	// contains filtered or unexported fields
}

Template represents a partially- or fully-signed transaction.

func Build

func Build(ctx context.Context, tx *bc.TxData, actions []Action, maxTime time.Time) (*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.

func (*Template) Hash

func (t *Template) Hash(idx int) bc.Hash

type TemplateBuilder

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

func (*TemplateBuilder) AddInput

func (b *TemplateBuilder) AddInput(in *bc.TxInput, sigInstruction *SigningInstruction) error

func (*TemplateBuilder) AddOutput

func (b *TemplateBuilder) AddOutput(o *bc.TxOutput) error

func (*TemplateBuilder) Build

func (b *TemplateBuilder) Build() (*Template, error)

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) RestrictMinTimeMS

func (b *TemplateBuilder) RestrictMinTimeMS(ms uint64)

type WitnessComponent

type WitnessComponent interface {
	// Sign is called to add signatures. Actual signing is delegated to
	// a callback function.
	Sign(context.Context, *Template, int, []string, SignFunc) error

	// Materialize is called to turn the component into a vector of
	// arguments for the input witness.
	Materialize(*Template, int, *[][]byte) error
}

WitnessComponent encodes instructions for finalizing a transaction by populating its InputWitness fields. Each WitnessComponent object produces zero or more items for the InputWitness of the txinput it corresponds to.

Jump to

Keyboard shortcuts

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