def send(from_addr, to_addr, amount): bc = Blockchain() utxo_set = UTXOSet(bc) tx = UTXOTx(from_addr, to_addr, amount, utxo_set) inputs = [] outputs = [] for vin in tx.vin: ctxin = TXInput() ctxin._tx_id = vin.tx_id ctxin._vin = vin.vout ctxin._sig = vin.signature ctxin._public_key = vin.public_key inputs.append(ctxin) for vout in tx.vout: outputs.append(TXOutput(vout.value, vout.address)) txnew = Transaction() txnew._id = tx.ID txnew._vin = inputs txnew._vout = outputs return txnew
def __init__(self, from_addr, to_addr, amount, utxo_set): inputs = [] outputs = [] # log('UTXOTx') wallets = ws.Wallets() wallet = wallets.get_wallet(from_addr) pubkey_hash = utils.hash_public_key(wallet.public_key) acc, valid_outputs = utxo_set.find_spendable_outputs( pubkey_hash, amount) if acc < amount: # log.error('Not enough funds') utils.logg('Not enough funds') sys.exit() # Build a list of inputs for tx_id, outs in valid_outputs.items(): for out in outs: ctxin = TXInput() ctxin._tx_id = tx_id ctxin._vout = out ctxin._signature = None ctxin._public_key = wallet.public_key inputs.append(ctxin) # Build a list of outputs outputs.append(TXOutput(amount, to_addr)) if acc > amount: # A change outputs.append(TXOutput(acc-amount, from_addr)) self._tx = Transaction() self._tx.vin = inputs self._tx.vout = outputs self._tx.set_id() self._utxo_set = utxo_set self._sign_utxo(wallet.private_key)
def _trimmed_copy(self): inputs = [] outputs = [] for vin in self.vin: ctxin = TXInput() ctxin._tx_id = vin.tx_id ctxin._vin = vin.vout ctxin._sig = None ctxin._public_key = None inputs.append(ctxin) for vout in self.vout: outputs.append(TXOutput(vout.value, vout.address)) txnew = Transaction() txnew._id = self.ID txnew._vin = inputs txnew._vout = outputs return txnew