//TODO: test func DecodeTx(tx []byte) (*Tx, []byte) { //log.Printf("DecodeTx - %X", tx) //4+1+41+1+8+4=59 if len(tx) < 4+1+41+1+8+4 { return nil, tx } answer := new(Tx) /* Version [4]byte TxInCount mymath.VarInt TxIn []*TxIn//[]TxIn TxOutCount mymath.VarInt TxOut []*TxOut//[]TxOut LockTime [4]byte */ copy(answer.Version[:], tx[0:4]) var tmpbytes []byte //log.Printf("Before ins: %X", tx[4:]) answer.TxInCount, tmpbytes = mymath.DecodeVarIntGiveRest(tx[4:]) answer.TxIn, tmpbytes = DecodeMultipleTxIns(tmpbytes, int(answer.TxInCount)) //log.Printf("Before outs: %X", tmpbytes) answer.TxOutCount, tmpbytes = mymath.DecodeVarIntGiveRest(tmpbytes) answer.TxOut, tmpbytes = DecodeMultipleTxOuts(tmpbytes, int(answer.TxOutCount)) //log.Printf("After outs: %X", tmpbytes) if len(tmpbytes) < 4 { return nil, tx } copy(answer.LockTime[:], tmpbytes[0:4]) return answer, tmpbytes[4:] }
//TODO: test func DecodeTxOut(b []byte) (*TxOut, []byte) { if len(b) < 8+1 { return nil, b } /* Value [8]byte PkScriptLength mymath.VarInt PkScript []byte//uchar[] */ answer := new(TxOut) copy(answer.Value[:], b[0:8]) var tmpbytes []byte answer.PkScriptLength, tmpbytes = mymath.DecodeVarIntGiveRest(b[8:]) if len(tmpbytes) < int(answer.PkScriptLength) { log.Printf("b - %X", b) log.Printf("len(tmpbytes)=%d", len(tmpbytes)) log.Printf("PkScriptLength=%d", answer.PkScriptLength) return nil, b } answer.PkScript = make([]byte, answer.PkScriptLength) copy(answer.PkScript, tmpbytes[0:answer.PkScriptLength]) //log.Printf("PkScript\n%X", answer.PkScript) return answer, tmpbytes[answer.PkScriptLength:] }
//TODO: do func DecodeBlock(blck []byte) *Block { blk := new(Block) //log.Print("DecodeBlock") if len(blck) < 4+32+32+4+4+4 { return nil } copy(blk.Version[:], blck[0:4]) copy(blk.PrevBlock[:], blck[4:4+32]) copy(blk.MerkleRoot[:], blck[4+32:4+32+32]) copy(blk.Timestamp[:], blck[4+32+32:4+32+32+4]) copy(blk.Bits[:], blck[4+32+32+4:4+32+32+4+4]) copy(blk.Nonce[:], blck[4+32+32+4+4:4+32+32+4+4+4]) /*for i:=0;i<4;i++{ blk.Version[i]=block[i] } for i:=0;i<32;i++{ blk.PrevBlock[i]=block[4+i] } for i:=0;i<32;i++{ blk.MerkleRoot[i]=block[4+32+i] } for i:=0;i<4;i++{ blk.Timestamp[i]=block[4+32+32+i] } for i:=0;i<4;i++{ blk.Bits[i]=block[4+32+32+4+i] } for i:=0;i<4;i++{ blk.Nonce[i]=block[4+32+32+4+4+i] }*/ //4+32+32+4+4+4=80 var remainingBits []byte blk.TxnCount, remainingBits = mymath.DecodeVarIntGiveRest(blck[80:]) //log.Printf("decode block remaining bits - %X", remainingBits) blk.Txns, remainingBits = DecodeMultipleTxs(remainingBits, int(blk.TxnCount)) //log.Printf("decode block remaining bits 2 - %X", remainingBits) return blk }
//TODO: test func DecodeTxIn(b []byte) (*TxIn, []byte) { if len(b) < 36+1+4 { return nil, b } //log.Printf("txin b - %X", b) answer := new(TxIn) answer.PreviousOutput.Hash = NewHash(b[0:32]) copy(answer.PreviousOutput.Index[:], b[32:36]) var tmpbytes []byte answer.ScriptLength, tmpbytes = mymath.DecodeVarIntGiveRest(b[36:]) if len(tmpbytes) < int(answer.ScriptLength)+4 { return nil, b } answer.SignatureScript = tmpbytes[0:answer.ScriptLength] copy(answer.Sequence[:], tmpbytes[answer.ScriptLength:answer.ScriptLength+4]) //log.Printf("txin bytes - %X", tmpbytes[answer.ScriptLength+4:]) return answer, tmpbytes[answer.ScriptLength+4:] }