decode.go raw

   1  // Package tx decodes transactions as well as it encodes them.
   2  package tx
   3  
   4  import (
   5  	"encoding/binary"
   6  	"errors"
   7  	"fmt"
   8  )
   9  
  10  // ErrTruncated is a transaction whose fields run past the end of the input.
  11  var ErrTruncated = errors.New("tx: truncated transaction")
  12  
  13  // ErrTrailing is a transaction followed by bytes that are not part of it.
  14  var ErrTrailing = errors.New("tx: trailing bytes after the transaction")
  15  
  16  // MaxItems bounds the input and output counts before anything is allocated.
  17  const MaxItems = 100000
  18  
  19  // Decode parses a transaction, with or without witnesses.
  20  //
  21  // It is strict: a field that runs past the end is ErrTruncated, bytes left over after the
  22  // locktime are ErrTrailing, and a count larger than MaxItems is refused before it is used to
  23  // size anything. A decoder that guesses is a decoder that accepts a different transaction from
  24  // the one the bytes describe.
  25  func Decode(raw []byte) (*Tx, error) {
  26  	c := &cursor{raw: raw}
  27  
  28  	version, err := c.u32()
  29  
  30  	if err != nil {
  31  		return nil, err
  32  	}
  33  
  34  	transaction := &Tx{Version: version}
  35  
  36  	// A zero input count is the segwit marker when a 0x01 flag follows.
  37  	first, err := c.u8()
  38  
  39  	if err != nil {
  40  		return nil, err
  41  	}
  42  
  43  	withWitness := false
  44  
  45  	if first == 0x00 {
  46  		flag, err := c.u8()
  47  
  48  		if err != nil {
  49  			return nil, err
  50  		}
  51  
  52  		if flag != 0x01 {
  53  			return nil, fmt.Errorf("tx: unsupported segwit flag 0x%02x", flag)
  54  		}
  55  
  56  		withWitness = true
  57  
  58  		if first, err = c.u8(); err != nil {
  59  			return nil, err
  60  		}
  61  	}
  62  
  63  	count, err := c.count(first)
  64  
  65  	if err != nil {
  66  		return nil, err
  67  	}
  68  
  69  	if count == 0 {
  70  		return nil, ErrNoInputs
  71  	}
  72  
  73  	for i := 0; i < count; i++ {
  74  		var input Input
  75  
  76  		if input.Outpoint, err = c.outpoint(); err != nil {
  77  			return nil, err
  78  		}
  79  
  80  		if input.ScriptSig, err = c.bytes(); err != nil {
  81  			return nil, err
  82  		}
  83  
  84  		if input.Sequence, err = c.u32(); err != nil {
  85  			return nil, err
  86  		}
  87  
  88  		transaction.Inputs = append(transaction.Inputs, input)
  89  	}
  90  
  91  	first, err = c.u8()
  92  
  93  	if err != nil {
  94  		return nil, err
  95  	}
  96  
  97  	count, err = c.count(first)
  98  
  99  	if err != nil {
 100  		return nil, err
 101  	}
 102  
 103  	if count == 0 {
 104  		return nil, ErrNoOutputs
 105  	}
 106  
 107  	for i := 0; i < count; i++ {
 108  		value, err := c.u64()
 109  
 110  		if err != nil {
 111  			return nil, err
 112  		}
 113  
 114  		script, err := c.bytes()
 115  
 116  		if err != nil {
 117  			return nil, err
 118  		}
 119  
 120  		transaction.Outputs = append(transaction.Outputs, Output{Value: value, Script: script})
 121  	}
 122  
 123  	if withWitness {
 124  		for i := range transaction.Inputs {
 125  			items, err := c.compactSize()
 126  
 127  			if err != nil {
 128  				return nil, err
 129  			}
 130  
 131  			if items > MaxItems {
 132  				return nil, fmt.Errorf("tx: %d witness items", items)
 133  			}
 134  
 135  			for j := 0; j < items; j++ {
 136  				item, err := c.bytes()
 137  
 138  				if err != nil {
 139  					return nil, err
 140  				}
 141  
 142  				transaction.Inputs[i].Witness = append(transaction.Inputs[i].Witness, item)
 143  			}
 144  		}
 145  	}
 146  
 147  	if transaction.LockTime, err = c.u32(); err != nil {
 148  		return nil, err
 149  	}
 150  
 151  	if c.at != len(raw) {
 152  		return nil, fmt.Errorf("%w: %d bytes", ErrTrailing, len(raw)-c.at)
 153  	}
 154  
 155  	return transaction, nil
 156  }
 157  
 158  // cursor reads fields from a slice and never reads past the end.
 159  type cursor struct {
 160  	raw []byte
 161  	at  int
 162  }
 163  
 164  func (c *cursor) take(n int) ([]byte, error) {
 165  	if n < 0 || c.at+n > len(c.raw) {
 166  		return nil, fmt.Errorf("%w: %d bytes at offset %d", ErrTruncated, n, c.at)
 167  	}
 168  
 169  	out := c.raw[c.at : c.at+n]
 170  	c.at += n
 171  
 172  	return out, nil
 173  }
 174  
 175  func (c *cursor) u8() (byte, error) {
 176  	b, err := c.take(1)
 177  
 178  	if err != nil {
 179  		return 0, err
 180  	}
 181  
 182  	return b[0], nil
 183  }
 184  
 185  func (c *cursor) u32() (uint32, error) {
 186  	b, err := c.take(4)
 187  
 188  	if err != nil {
 189  		return 0, err
 190  	}
 191  
 192  	return binary.LittleEndian.Uint32(b), nil
 193  }
 194  
 195  func (c *cursor) u64() (uint64, error) {
 196  	b, err := c.take(8)
 197  
 198  	if err != nil {
 199  		return 0, err
 200  	}
 201  
 202  	return binary.LittleEndian.Uint64(b), nil
 203  }
 204  
 205  func (c *cursor) outpoint() (Outpoint, error) {
 206  	hash, err := c.take(32)
 207  
 208  	if err != nil {
 209  		return Outpoint{}, err
 210  	}
 211  
 212  	index, err := c.u32()
 213  
 214  	if err != nil {
 215  		return Outpoint{}, err
 216  	}
 217  
 218  	var outpoint Outpoint
 219  
 220  	copy(outpoint.Hash[:], hash)
 221  
 222  	outpoint.Index = index
 223  
 224  	return outpoint, nil
 225  }
 226  
 227  // bytes reads a compact-size length and that many bytes.
 228  func (c *cursor) bytes() ([]byte, error) {
 229  	length, err := c.compactSize()
 230  
 231  	if err != nil {
 232  		return nil, err
 233  	}
 234  
 235  	raw, err := c.take(length)
 236  
 237  	if err != nil {
 238  		return nil, err
 239  	}
 240  
 241  	return append([]byte(nil), raw...), nil
 242  }
 243  
 244  // compactSize reads one size, and reads the first byte of it if first is non-negative.
 245  func (c *cursor) compactSize() (int, error) {
 246  	first, err := c.u8()
 247  
 248  	if err != nil {
 249  		return 0, err
 250  	}
 251  
 252  	return c.count(first)
 253  }
 254  
 255  // count reads the rest of a compact size whose first byte is already read.
 256  func (c *cursor) count(first byte) (int, error) {
 257  	switch {
 258  	case first < 0xfd:
 259  		return int(first), nil
 260  
 261  	case first == 0xfd:
 262  		b, err := c.take(2)
 263  
 264  		if err != nil {
 265  			return 0, err
 266  		}
 267  
 268  		value := int(binary.LittleEndian.Uint16(b))
 269  
 270  		if value < 0xfd {
 271  			return 0, fmt.Errorf("tx: non-minimal compact size")
 272  		}
 273  
 274  		return value, nil
 275  
 276  	case first == 0xfe:
 277  		b, err := c.take(4)
 278  
 279  		if err != nil {
 280  			return 0, err
 281  		}
 282  
 283  		value := binary.LittleEndian.Uint32(b)
 284  
 285  		if value < 0x10000 {
 286  			return 0, fmt.Errorf("tx: non-minimal compact size")
 287  		}
 288  
 289  		if value > MaxItems {
 290  			return 0, fmt.Errorf("tx: %d items", value)
 291  		}
 292  
 293  		return int(value), nil
 294  
 295  	default:
 296  		b, err := c.take(8)
 297  
 298  		if err != nil {
 299  			return 0, err
 300  		}
 301  
 302  		value := binary.LittleEndian.Uint64(b)
 303  
 304  		if value < 0x100000000 || value > MaxItems {
 305  			return 0, fmt.Errorf("tx: %d items", value)
 306  		}
 307  
 308  		return int(value), nil
 309  	}
 310  }
 311