)
const (
- BlockRequestByte = byte(0x10)
- BlockResponseByte = byte(0x11)
- StatusRequestByte = byte(0x20)
+ // BlockRequestByte means block request message
+ BlockRequestByte = byte(0x10)
+ // BlockResponseByte means block response message
+ BlockResponseByte = byte(0x11)
+ // StatusRequestByte means status request message
+ StatusRequestByte = byte(0x20)
+ // StatusResponseByte means status response message
StatusResponseByte = byte(0x21)
+ // NewTransactionByte means transaction notify message
NewTransactionByte = byte(0x30)
)
wire.ConcreteType{&TransactionNotifyMessage{}, NewTransactionByte},
)
+// DecodeMessage decode receive messages
func DecodeMessage(bz []byte) (msgType byte, msg BlockchainMessage, err error) {
msgType = bz[0]
n := int(0)
return
}
+// BlockRequestMessage is block request message struct
type BlockRequestMessage struct {
Height uint64
RawHash [32]byte
}
+// GetHash return block hash
func (m *BlockRequestMessage) GetHash() *bc.Hash {
hash := bc.NewHash(m.RawHash)
return &hash
return fmt.Sprintf("BlockRequestMessage{Hash: %s}", hash.String())
}
+// BlockResponseMessage is block response message struct
type BlockResponseMessage struct {
RawBlock []byte
}
+// NewBlockResponseMessage produce new BlockResponseMessage instance
func NewBlockResponseMessage(block *legacy.Block) (*BlockResponseMessage, error) {
rawBlock, err := block.MarshalText()
if err != nil {
return &BlockResponseMessage{RawBlock: rawBlock}, nil
}
+// GetBlock return block struct
func (m *BlockResponseMessage) GetBlock() *legacy.Block {
block := &legacy.Block{
BlockHeader: legacy.BlockHeader{},
return fmt.Sprintf("BlockResponseMessage{Size: %d}", len(m.RawBlock))
}
+// TransactionNotifyMessage is transaction notify message struct
type TransactionNotifyMessage struct {
RawTx []byte
}
+// NewTransactionNotifyMessage produce new TransactionNotifyMessage instance
func NewTransactionNotifyMessage(tx *legacy.Tx) (*TransactionNotifyMessage, error) {
rawTx, err := tx.TxData.MarshalText()
if err != nil {
return &TransactionNotifyMessage{RawTx: rawTx}, nil
}
+// GetTransaction return Tx struct
func (m *TransactionNotifyMessage) GetTransaction() *legacy.Tx {
tx := &legacy.Tx{}
tx.UnmarshalText(m.RawTx)
return fmt.Sprintf("TransactionNotifyMessage{Size: %d}", len(m.RawTx))
}
+// StatusRequestMessage is status request message struct
type StatusRequestMessage struct{}
func (m *StatusRequestMessage) String() string {
return "StatusRequestMessage"
}
+// StatusResponseMessage is status response message struct
type StatusResponseMessage struct {
Height uint64
RawHash [32]byte
}
+// NewStatusResponseMessage produce new StatusResponseMessage instance
func NewStatusResponseMessage(block *legacy.Block) *StatusResponseMessage {
return &StatusResponseMessage{
Height: block.Height,
}
}
+// GetHash return hash pointer
func (m *StatusResponseMessage) GetHash() *bc.Hash {
hash := bc.NewHash(m.RawHash)
return &hash
errBadAction = errors.New("bad action object")
)
+// BuildRequest is main struct when building transactions
type BuildRequest struct {
Tx *legacy.TxData `json:"base_transaction"`
Actions []map[string]interface{} `json:"actions"`
"github.com/bytom/net"
)
+// ErrNoTLS means that no TLS configuration available
var ErrNoTLS = errors.New("no TLS configuration available")
// TLSConfig returns a TLS config suitable for use
"github.com/bytom/protocol/bc/legacy"
)
+// NewBuilder return new TemplateBuilder instance
func NewBuilder(maxTime time.Time) *TemplateBuilder {
return &TemplateBuilder{maxTime: maxTime}
}
+// TemplateBuilder is struct of building transactions
type TemplateBuilder struct {
base *legacy.TxData
inputs []*legacy.TxInput
callbacks []func() error
}
+// AddInput add inputs of transactions
func (b *TemplateBuilder) AddInput(in *legacy.TxInput, sigInstruction *SigningInstruction) error {
if !in.IsCoinbase() && in.Amount() > math.MaxInt64 {
return errors.WithDetailf(ErrBadAmount, "amount %d exceeds maximum value 2^63", in.Amount())
return nil
}
+// AddOutput add outputs of transactions
func (b *TemplateBuilder) AddOutput(o *legacy.TxOutput) error {
if o.Amount > math.MaxInt64 {
return errors.WithDetailf(ErrBadAmount, "amount %d exceeds maximum value 2^63", o.Amount)
return nil
}
+// RestrictMinTime set minTime
func (b *TemplateBuilder) RestrictMinTime(t time.Time) {
if t.After(b.minTime) {
b.minTime = t
}
}
+// RestrictMaxTime set maxTime
func (b *TemplateBuilder) RestrictMaxTime(t time.Time) {
if t.Before(b.maxTime) {
b.maxTime = t
}
}
+// MaxTime return maxTime
func (b *TemplateBuilder) MaxTime() time.Time {
return b.maxTime
}
}
}
+// Build build transactions with template
func (b *TemplateBuilder) Build() (*Template, *legacy.TxData, error) {
// Run any building callbacks.
for _, cb := range b.callbacks {
chainjson "github.com/bytom/encoding/json"
)
+// DataWitness used sign transaction
type DataWitness chainjson.HexBytes
func (dw DataWitness) materialize(args *[][]byte) error {
return nil
}
+// MarshalJSON marshal DataWitness
func (dw DataWitness) MarshalJSON() ([]byte, error) {
x := struct {
Type string `json:"type"`
var (
// ErrRejected means the network rejected a tx (as a double-spend)
ErrRejected = errors.New("transaction rejected")
-
- ErrMissingRawTx = errors.New("missing raw tx")
+ // 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")
)
si.WitnessComponents = append(si.WitnessComponents, sw)
}
-// AddWitnessKeys adds a SignatureWitness with the given quorum and
+// AddRawWitnessKeys adds a SignatureWitness with the given quorum and
// list of keys derived by applying the derivation path to each of the
// xpubs.
func (si *SigningInstruction) AddRawWitnessKeys(xpubs []chainkd.XPub, path [][]byte, quorum int) {
materialize(*[][]byte) error
}
+// UnmarshalJSON unmarshal SigningInstruction
func (si *SigningInstruction) UnmarshalJSON(b []byte) error {
var pre struct {
Position uint32 `json:"position"`
AllowAdditional bool `json:"allow_additional_actions"`
}
+// Hash return sign hash
func (t *Template) Hash(idx uint32) bc.Hash {
return t.Transaction.SigHash(idx)
}
+// Action is a interface
type Action interface {
Build(context.Context, *TemplateBuilder) error
}
for _, inpID := range tx.Tx.InputIDs {
sp, err := tx.Spend(inpID)
if err != nil {
- log.WithField("err", err).Error("building spend entry type")
continue
}