Exemplo n.º 1
0
	def getMemPoolTransactions(self):
		mempool = self._rpcHost.call('getrawmempool')
		result = []
		for txHash in mempool:
			txHex = self._rpcHost.call('getrawtransaction', txHash)
			result.append((txHash, RawTransaction.FromHex(txHex)))
		return result
Exemplo n.º 2
0
 def signAndSend(self, unsignedTransactionHex, privateKeys,
                 maximumSignedSize):
     unsignedSize = len(unsignedTransactionHex) / 2
     signedSize = unsignedSize * 30 // 25  # arbitrary increase here to simulate an increase in size when signing transactions
     if signedSize > maximumSignedSize:
         raise Host.MaximumSignedSizeExceeded()
     unsignedTransactionBytes = RawTransaction.FromHex(
         unsignedTransactionHex)
     decoded, scriptPubKeys = RawTransaction.Decode(
         unsignedTransactionBytes)
     sumOfInputs = 0
     pubKeyHashesToBeSigned = []
     for i in range(decoded.numberOfInputs()):
         amount, pubKeyHashToBeSigned = self._consumeUnspent(
             decoded.inputTXID(i), decoded.inputVOut(i))
         sumOfInputs += amount
         if pubKeyHashToBeSigned is not None:
             pubKeyHashesToBeSigned.append(pubKeyHashToBeSigned)
     if len(pubKeyHashesToBeSigned) != len(privateKeys):
         raise Exception(
             'number of supplied private keys does not match number of required private keys'
         )
     for pubKeyHash in pubKeyHashesToBeSigned:
         privateKeys = MatchPubKeyHashAndRemovePrivateKey(
             self._keyGenerator, pubKeyHash, privateKeys)
     self._nextTXID += 1
     txid = MakeTXID(self._nextTXID)
     outputAmounts = []
     for vout in range(decoded.numberOfOutputs()):
         toAdd = {'txid': txid, 'vout': vout}
         scriptPubKey = scriptPubKeys[vout]
         pubKeyHash = decoded.outputPubKeyHash(vout)
         assert pubKeyHash == RawTransaction.PubKeyHashForScriptPubKey(
             scriptPubKey)
         toAdd['scriptPubKey'] = scriptPubKey
         toAdd['address'] = pubKeyHash
         toAdd['amount'] = decoded.outputAmount(vout)
         assert decoded.outputAmount(vout) >= TransactionFee.dustLimit
         self._unspent.append(toAdd)
         outputAmounts.append(decoded.outputAmount(vout))
     requiredFee = TransactionFee.CalculateRequired_FromSizeAndOutputs(
         signedSize, outputAmounts)
     paidFee = sumOfInputs - sum(outputAmounts)
     assert paidFee >= requiredFee
     assert paidFee == requiredFee  # can overspend, in theory, but would like to then see the actual repeat case for this
     self._addTransaction(txid, unsignedTransactionHex)
     return txid
Exemplo n.º 3
0
	def getBlockTransactions(self, blockHash):
		result = []
		block = self._getBlock_Cached(blockHash)
		transactionIDs = block['tx']
		blockData = self._getBlockData_Cached(blockHash)
		rawTransactions = RawTransaction.GetTransactionsInBlock(blockData)
		assert len(rawTransactions) == len(transactionIDs)
		assert len(transactionIDs) >= 1
		for i in range(len(rawTransactions) - 1):
			result.append((transactionIDs[i + 1], rawTransactions[i + 1]))
		return result
Exemplo n.º 4
0
 def _addUnspent(self, amount):
     self._nextTXID += 1
     txid = MakeTXID(self._nextTXID)
     vout = 7
     toAdd = {'txid': txid, 'vout': vout}
     pubKeyHash = self.getNewNonSwapBillAddress()
     scriptPubKey = RawTransaction.ScriptPubKeyForPubKeyHash(pubKeyHash)
     toAdd['scriptPubKey'] = scriptPubKey
     toAdd['address'] = pubKeyHash
     toAdd['amount'] = amount
     self._unspent.append(toAdd)
Exemplo n.º 5
0
def _processTransactions(state, wallet, ownedAccounts, transactions,
                         applyToState, reportPrefix, out):
    for txID, hostTXBytes in transactions:
        hostTX, scriptPubKeys = RawTransaction.Decode(hostTXBytes)
        if RawTransaction.UnexpectedFormat_Fast(hostTXBytes,
                                                ControlAddressPrefix.prefix):
            continue
        inputsReport = ownedAccounts.updateForSpent(hostTX, state)
        try:
            transactionType, sourceAccounts, outputs, transactionDetails = TransactionEncoding.ToStateTransaction(
                hostTX)
            appliedSuccessfully = True
        except (TransactionEncoding.NotValidSwapBillTransaction,
                TransactionEncoding.UnsupportedTransaction):
            if inputsReport != '':
                print(reportPrefix + ': <invalid transaction>', file=out)
                print(inputsReport, end="", file=out)
            continue
        if inputsReport != '':
            print(reportPrefix + ': ' + transactionType, file=out)
            print(inputsReport, end="", file=out)
        if not applyToState:
            continue
        #inBetweenReport = ownedAccounts.checkForTradeOfferChanges(state)
        #assert inBetweenReport == ''
        error = state.applyTransaction(transactionType,
                                       txID,
                                       sourceAccounts=sourceAccounts,
                                       transactionDetails=transactionDetails,
                                       outputs=outputs)
        outputsReport = ownedAccounts.checkForTradeOfferChanges(state)
        outputsReport += ownedAccounts.updateForNewOutputs(
            wallet, state, txID, hostTX, outputs, scriptPubKeys)
        if outputsReport:
            if inputsReport == '':
                # didn't print this line yet
                print(reportPrefix + ': ' + transactionType, file=out)
            print(outputsReport, end="", file=out)
        if (outputsReport or inputsReport) and error is not None:
            print(' * failed:', error, file=out)
Exemplo n.º 6
0
	def _getBlockData_Cached(self, blockHash):
		if self._cachedBlockDataHash != blockHash:
			self._cachedBlockData = RawTransaction.FromHex(self._rpcHost.call('getblock', blockHash, False))
			self._cachedBlockDataHash = blockHash
		return self._cachedBlockData
Exemplo n.º 7
0
 def _addTransaction(self, txid, unsignedTransactionHex):
     self._memPool.append(
         (txid, RawTransaction.FromHex(unsignedTransactionHex)))