示例#1
0
    def __init__(self, from_addr, to_addr, amount, utxo_set):
        inputs = []
        outputs = []

        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:
            print('Not enough funds')
            sys.exit()

        # inputs
        for tx_id, outs in valid_outputs.items():
            for out in outs:
                input = TXInput(tx_id, out, None, wallet.public_key)
                inputs.append(input)

        # outputs
        outputs.append(TXOutput(amount, to_addr))
        if acc > amount:
            outputs.append(TXOutput(acc - amount, from_addr))

        self._tx = Transaction(None, inputs, outputs).set_id()
        self._utxo_set = utxo_set

        self._sign_utxo(wallet.private_key)
        print('Signing complete\n')
示例#2
0
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
示例#3
0
    def __init__(self, to, data=None):
        if not data:
            data = 'Reward to {0}'.format(to)

        tx_id = None
        vin = [TXInput('', -1, None, data)]
        vout = [TXOutput(TXOutput.subsidy, to)]
        self._tx = Transaction(tx_id, vin, vout).set_id()
示例#4
0
    def __init__(self, to_account, data=None):
        if data == None:
            data = ''.join(
                random.sample(string.ascii_letters + string.digits, 20))

        vins = [TXInput(None, -1, data)]
        vouts = [TXOutput(to_account, CoinbaseTx.subsidy)]
        super(CoinbaseTx, self).__init__(vins, vouts)
示例#5
0
    def __init__(self, to, data=None):
        if not data:
            # data = 'Reward to {0}'.format(to)
            data = ''.join(random.choice(
                string.ascii_uppercase + string.digits) for _ in range(20))

        tx_id = None
        vin = [TXInput('', -1, None, data)]
        vout = [TXOutput(TXOutput.subsidy, to)]
        self._tx = Transaction(tx_id, vin, vout).set_id()
示例#6
0
    def _trimmed_copy(self):
        inputs = []
        outputs = []

        for vin in self.vin:
            inputs.append(TXInput(vin.tx_id, vin.vout, None, None))

        for vout in self.vout:
            outputs.append(TXOutput(vout.value, vout.address))

        return Transaction(self.ID, inputs, outputs)
示例#7
0
    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)
示例#8
0
    def __init__(self, from_account, to_account, amount, blockchain):
        inputs = []
        outputs = []

        acc, valid_outputs = blockchain.find_spendable_outputs(
            from_account, amount)
        if acc < amount:
            raise NotEnoughFundsError()

        # 构建交易输入列表
        for tx_id, outs in valid_outputs.items():
            for idx, out in enumerate(outs):
                input = TXInput(tx_id, idx, from_account)
                inputs.append(input)

        # 构建交易输出列表
        outputs.append(TXOutput(to_account, amount))

        # 找零
        if acc > amount:
            outputs.append(TXOutput(from_account, acc - amount))

        super(UTXOTransaction, self).__init__(inputs, outputs)
示例#9
0
    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
示例#10
0
 def __init__(self, tx_id=None, vin=None, vout=None):
     self._id = tx_id
     self._vin = [TXInput()]
     self._vout = [TXOutput()]