github.com/myafeier/fabric@v1.0.1-0.20170722181825-3a4b1f2bce86/examples/chaincode/go/utxo/util/util.go (about)

     1  /*
     2  Copyright IBM Corp. 2016 All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  		 http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package util
    18  
    19  import (
    20  	"bytes"
    21  	"encoding/binary"
    22  )
    23  
    24  func decodeInt32(input []byte) (int32, []byte, error) {
    25  	var myint32 int32
    26  	buf1 := bytes.NewBuffer(input[0:4])
    27  	binary.Read(buf1, binary.LittleEndian, &myint32)
    28  	return myint32, input, nil
    29  }
    30  
    31  // ReadVarInt reads an int that is formatted in the Bitcoin style
    32  // variable int format
    33  func ReadVarInt(buffer *bytes.Buffer) uint64 {
    34  	var finalResult uint64
    35  
    36  	var variableLenInt uint8
    37  	binary.Read(buffer, binary.LittleEndian, &variableLenInt)
    38  	if variableLenInt < 253 {
    39  		finalResult = uint64(variableLenInt)
    40  	} else if variableLenInt == 253 {
    41  		var result uint16
    42  		binary.Read(buffer, binary.LittleEndian, &result)
    43  		finalResult = uint64(result)
    44  	} else if variableLenInt == 254 {
    45  		var result uint32
    46  		binary.Read(buffer, binary.LittleEndian, &result)
    47  		finalResult = uint64(result)
    48  	} else if variableLenInt == 255 {
    49  		var result uint64
    50  		binary.Read(buffer, binary.LittleEndian, &result)
    51  		finalResult = result
    52  	}
    53  
    54  	return finalResult
    55  }
    56  
    57  // ParseUTXOBytes parses a bitcoin style transaction
    58  func ParseUTXOBytes(txAsUTXOBytes []byte) *TX {
    59  	buffer := bytes.NewBuffer(txAsUTXOBytes)
    60  	var version int32
    61  	binary.Read(buffer, binary.LittleEndian, &version)
    62  
    63  	inputCount := ReadVarInt(buffer)
    64  
    65  	newTX := &TX{}
    66  
    67  	for i := 0; i < int(inputCount); i++ {
    68  		newTXIN := &TX_TXIN{}
    69  
    70  		newTXIN.SourceHash = buffer.Next(32)
    71  
    72  		binary.Read(buffer, binary.LittleEndian, &newTXIN.Ix)
    73  
    74  		// Parse the script length and script bytes
    75  		scriptLength := ReadVarInt(buffer)
    76  		newTXIN.Script = buffer.Next(int(scriptLength))
    77  
    78  		// Appears to not be used currently
    79  		binary.Read(buffer, binary.LittleEndian, &newTXIN.Sequence)
    80  
    81  		newTX.Txin = append(newTX.Txin, newTXIN)
    82  
    83  	}
    84  
    85  	// Now the outputs
    86  	outputCount := ReadVarInt(buffer)
    87  
    88  	for i := 0; i < int(outputCount); i++ {
    89  		newTXOUT := &TX_TXOUT{}
    90  
    91  		binary.Read(buffer, binary.LittleEndian, &newTXOUT.Value)
    92  
    93  		// Parse the script length and script bytes
    94  		scriptLength := ReadVarInt(buffer)
    95  		newTXOUT.Script = buffer.Next(int(scriptLength))
    96  
    97  		newTX.Txout = append(newTX.Txout, newTXOUT)
    98  
    99  	}
   100  
   101  	binary.Read(buffer, binary.LittleEndian, &newTX.LockTime)
   102  
   103  	return newTX
   104  }