// Package tx decodes transactions as well as it encodes them. package tx import ( "encoding/binary" "errors" "fmt" ) // ErrTruncated is a transaction whose fields run past the end of the input. var ErrTruncated = errors.New("tx: truncated transaction") // ErrTrailing is a transaction followed by bytes that are not part of it. var ErrTrailing = errors.New("tx: trailing bytes after the transaction") // MaxItems bounds the input and output counts before anything is allocated. const MaxItems = 100000 // Decode parses a transaction, with or without witnesses. // // It is strict: a field that runs past the end is ErrTruncated, bytes left over after the // locktime are ErrTrailing, and a count larger than MaxItems is refused before it is used to // size anything. A decoder that guesses is a decoder that accepts a different transaction from // the one the bytes describe. func Decode(raw []byte) (*Tx, error) { c := &cursor{raw: raw} version, err := c.u32() if err != nil { return nil, err } transaction := &Tx{Version: version} // A zero input count is the segwit marker when a 0x01 flag follows. first, err := c.u8() if err != nil { return nil, err } withWitness := false if first == 0x00 { flag, err := c.u8() if err != nil { return nil, err } if flag != 0x01 { return nil, fmt.Errorf("tx: unsupported segwit flag 0x%02x", flag) } withWitness = true if first, err = c.u8(); err != nil { return nil, err } } count, err := c.count(first) if err != nil { return nil, err } if count == 0 { return nil, ErrNoInputs } for i := 0; i < count; i++ { var input Input if input.Outpoint, err = c.outpoint(); err != nil { return nil, err } if input.ScriptSig, err = c.bytes(); err != nil { return nil, err } if input.Sequence, err = c.u32(); err != nil { return nil, err } transaction.Inputs = append(transaction.Inputs, input) } first, err = c.u8() if err != nil { return nil, err } count, err = c.count(first) if err != nil { return nil, err } if count == 0 { return nil, ErrNoOutputs } for i := 0; i < count; i++ { value, err := c.u64() if err != nil { return nil, err } script, err := c.bytes() if err != nil { return nil, err } transaction.Outputs = append(transaction.Outputs, Output{Value: value, Script: script}) } if withWitness { for i := range transaction.Inputs { items, err := c.compactSize() if err != nil { return nil, err } if items > MaxItems { return nil, fmt.Errorf("tx: %d witness items", items) } for j := 0; j < items; j++ { item, err := c.bytes() if err != nil { return nil, err } transaction.Inputs[i].Witness = append(transaction.Inputs[i].Witness, item) } } } if transaction.LockTime, err = c.u32(); err != nil { return nil, err } if c.at != len(raw) { return nil, fmt.Errorf("%w: %d bytes", ErrTrailing, len(raw)-c.at) } return transaction, nil } // cursor reads fields from a slice and never reads past the end. type cursor struct { raw []byte at int } func (c *cursor) take(n int) ([]byte, error) { if n < 0 || c.at+n > len(c.raw) { return nil, fmt.Errorf("%w: %d bytes at offset %d", ErrTruncated, n, c.at) } out := c.raw[c.at : c.at+n] c.at += n return out, nil } func (c *cursor) u8() (byte, error) { b, err := c.take(1) if err != nil { return 0, err } return b[0], nil } func (c *cursor) u32() (uint32, error) { b, err := c.take(4) if err != nil { return 0, err } return binary.LittleEndian.Uint32(b), nil } func (c *cursor) u64() (uint64, error) { b, err := c.take(8) if err != nil { return 0, err } return binary.LittleEndian.Uint64(b), nil } func (c *cursor) outpoint() (Outpoint, error) { hash, err := c.take(32) if err != nil { return Outpoint{}, err } index, err := c.u32() if err != nil { return Outpoint{}, err } var outpoint Outpoint copy(outpoint.Hash[:], hash) outpoint.Index = index return outpoint, nil } // bytes reads a compact-size length and that many bytes. func (c *cursor) bytes() ([]byte, error) { length, err := c.compactSize() if err != nil { return nil, err } raw, err := c.take(length) if err != nil { return nil, err } return append([]byte(nil), raw...), nil } // compactSize reads one size, and reads the first byte of it if first is non-negative. func (c *cursor) compactSize() (int, error) { first, err := c.u8() if err != nil { return 0, err } return c.count(first) } // count reads the rest of a compact size whose first byte is already read. func (c *cursor) count(first byte) (int, error) { switch { case first < 0xfd: return int(first), nil case first == 0xfd: b, err := c.take(2) if err != nil { return 0, err } value := int(binary.LittleEndian.Uint16(b)) if value < 0xfd { return 0, fmt.Errorf("tx: non-minimal compact size") } return value, nil case first == 0xfe: b, err := c.take(4) if err != nil { return 0, err } value := binary.LittleEndian.Uint32(b) if value < 0x10000 { return 0, fmt.Errorf("tx: non-minimal compact size") } if value > MaxItems { return 0, fmt.Errorf("tx: %d items", value) } return int(value), nil default: b, err := c.take(8) if err != nil { return 0, err } value := binary.LittleEndian.Uint64(b) if value < 0x100000000 || value > MaxItems { return 0, fmt.Errorf("tx: %d items", value) } return int(value), nil } }