void TxInRef::unserialize(uint8_t const * ptr, uint32_t nbytes, TxRef* parent, int32_t idx) { parentTx_ = parent; index_ = idx; nBytes_ = (nbytes==0 ? BtcUtils::TxInCalcLength(ptr) : nbytes); self_ = BinaryDataRef(ptr, nBytes_); scriptOffset_ = 36 + BtcUtils::readVarIntLength(getPtr()+36); scriptType_ = BtcUtils::getTxInScriptType(getScriptRef(), BinaryDataRef(getPtr(), 32)); }
BinaryDataRef BinaryData::getSliceRef(int32_t start_pos, uint32_t nChar) const { if(start_pos < 0) start_pos = getSize() + start_pos; if(start_pos + nChar > getSize()) { cerr << "getSliceRef: Invalid BinaryData access" << endl; return BinaryDataRef(); } return BinaryDataRef( getPtr()+start_pos, nChar); }
void TxIn::unserialize_checked(uint8_t const * ptr, uint32_t size, uint32_t nbytes, TxRef parent, uint32_t idx) { parentTx_ = parent; index_ = idx; uint32_t numBytes = (nbytes==0 ? BtcUtils::TxInCalcLength(ptr, size) : nbytes); if (size < numBytes) throw BlockDeserializingException(); dataCopy_.copyFrom(ptr, numBytes); if (dataCopy_.getSize()-36 < 1) throw BlockDeserializingException(); scriptOffset_ = 36 + BtcUtils::readVarIntLength(getPtr()+36); if (dataCopy_.getSize() < 32) throw BlockDeserializingException(); scriptType_ = BtcUtils::getTxInScriptType(getScriptRef(), BinaryDataRef(getPtr(),32)); if(!parentTx_.isInitialized()) { parentHeight_ = UINT32_MAX; parentHash_ = BinaryData(0); } }
BinaryDataRef TransactionVerifier::getOutpoint(unsigned inputID) const { if (inputID > theTx_.txins_.size()) throw runtime_error("invalid txin index"); auto& inputOnS = theTx_.txins_[inputID]; return BinaryDataRef(theTx_.data_ + inputOnS.first, 36); }
BinaryDataRef TransactionVerifier::getWitnessData(unsigned inputId) const { if (inputId >= theTx_.witnesses_.size()) throw runtime_error("invalid witness data id"); auto& witOffsetAndSize = theTx_.witnesses_[inputId]; return BinaryDataRef(theTx_.data_ + witOffsetAndSize.first, witOffsetAndSize.second); }
BinaryDataRef TransactionVerifier::getSerializedOutputScripts(void) const { auto txOutCount = theTx_.txouts_.size(); auto firstTxOutOffset = theTx_.txouts_[0].first; auto lastTxOutOffset = theTx_.txouts_[txOutCount - 1].first + theTx_.txouts_[txOutCount - 1].second; auto txOutsLen = lastTxOutOffset - firstTxOutOffset; return BinaryDataRef(theTx_.data_ + firstTxOutOffset, txOutsLen); }
void TxOutRef::unserialize(uint8_t const * ptr, uint32_t nbytes, TxRef* parent, int32_t idx) { parentTx_ = parent; index_ = idx; nBytes_ = (nbytes==0 ? BtcUtils::TxOutCalcLength(ptr) : nbytes); self_ = BinaryDataRef(ptr, nBytes_); scriptOffset_ = 8 + BtcUtils::readVarIntLength(getPtr()+8); BinaryDataRef scriptRef(self_.getPtr()+scriptOffset_, getScriptSize()); scriptType_ = BtcUtils::getTxOutScriptType(scriptRef); recipientBinAddr20_ = BtcUtils::getTxOutRecipientAddr(scriptRef, scriptType_); }
void BlockHeader::unserialize(uint8_t const * ptr, uint32_t size) { if (size < HEADER_SIZE) throw BlockDeserializingException(); dataCopy_.copyFrom(ptr, HEADER_SIZE); BtcUtils::getHash256(dataCopy_.getPtr(), HEADER_SIZE, thisHash_); difficultyDbl_ = BtcUtils::convertDiffBitsToDouble( BinaryDataRef(dataCopy_.getPtr()+72, 4)); isInitialized_ = true; nextHash_ = BinaryData(0); blockHeight_ = UINT32_MAX; difficultySum_ = -1; isMainBranch_ = false; isOrphan_ = true; numTx_ = UINT32_MAX; }
const BinaryDataRef OutPoint::getDBkey(LMDBBlockDatabase* db) const { if (DBkey_.getSize() == 8) return DBkey_; if (db != nullptr) { if (db->getStoredTx_byHash(txHash_, nullptr, &DBkey_)) { DBkey_.append(WRITE_UINT16_BE((uint16_t)txOutIndex_)); return DBkey_; } } return BinaryDataRef(); }
void BlockHeaderRef::unserialize(uint8_t const * ptr) { self_.setRef(ptr, HEADER_SIZE); BtcUtils::getHash256(self_.getPtr(), HEADER_SIZE, thisHash_); difficultyDbl_ = BtcUtils::convertDiffBitsToDouble( BinaryDataRef(self_.getPtr()+72, 4)); isInitialized_ = true; nextHash_ = BinaryData(0); blockHeight_ = UINT32_MAX; blockNumBytes_ = 0; blkByteLoc_ = 0; difficultySum_ = -1; isMainBranch_ = false; isOrphan_ = true; isFinishedCalc_ = false; isOnDiskYet_ = false; txPtrList_ = vector<TxRef*>(0); }
BinaryDataRef TxOut::getScriptRef(void) { return BinaryDataRef( dataCopy_.getPtr()+scriptOffset_, getScriptSize() ); }
BinaryDataRef TxIn::getScriptRef(void) const { uint32_t scrLen = (uint32_t)BtcUtils::readVarInt(getPtr()+36); return BinaryDataRef(getPtr() + getScriptOffset(), scrLen); }
BinaryDataRef TxOutRef::getScriptRef(void) { return BinaryDataRef( self_.getPtr()+scriptOffset_, getScriptSize() ); }
BinaryDataRef BinaryData::getRef(void) const { return BinaryDataRef(getPtr(), getSize()); }
BinaryDataRef TransactionVerifier::getWitnessData(unsigned inputId) const { auto& witOffsetAndSize = theTx_.witnesses_[inputId]; return BinaryDataRef(theTx_.data_ + witOffsetAndSize.first, witOffsetAndSize.second); }