Ejemplo n.º 1
0
    def withdrawfromvault(self, fromvaultaddress, toaddress, amount):
        vault = self.getvault(fromvaultaddress)
        received = self.chaindb.listreceivedbyvault(fromvaultaddress)
        received = received.values()[0]
        if received['value'] < amount + 2 * utils.calculate_fees(None):
            self.logger.warning("Insufficient funds in vault, exiting, return")
            return

        # create transaction
        tx = CTransaction()

        # to the receiver
        txout = CTxOut()
        txout.nValue = amount
        txout.scriptPubKey = utils.address_to_pay_to_pubkey_hash(toaddress)
        tx.vout.append(txout)

        # from the sender
        nValueIn = 0
        nValueOut = amount

        txin = CTxIn()
        txin.prevout = COutPoint()
        txin.prevout.hash = received['txhash']
        txin.prevout.n = received['n']
        txin.scriptSig = received['scriptPubKey']
        tx.vin.append(txin)

        # calculate nValueIn
        nValueIn = received['value']
        # calculate the total excess amount
        excessAmount = nValueIn - nValueOut
        # calculate the fees
        fees = 2 * utils.calculate_fees(tx)
        # create change transaction, if there is any change left
        if excessAmount > fees:
            change_txout = CTxOut()
            change_txout.nValue = excessAmount - fees
            account = self.getaccount()
            changeaddress = fromvaultaddress
            self.logger.debug("Change address: %s" % changeaddress)
            change_txout.scriptPubKey = \
                utils.vault_address_to_pay_to_vault_script(changeaddress)
            tx.vout.append(change_txout)

        # calculate txhash
        tx.calc_sha256()
        txhash = str(tx.sha256)
        key = CKey()
        key.set_pubkey(vault['public_key'])
        key.set_privkey(vault['private_key'])
        signature = key.sign(txhash)
        vaultscript = utils.create_vault_script(vault['address'], \
            vault['master_address'], vault['timeout'], vault['maxfees'])
        scriptSig = chr(OP_VAULT_WITHDRAW) + chr(len(signature)) + signature + \
                chr(len(vaultscript)) + vaultscript
        self.logger.debug("Adding signature: %s" % binascii.hexlify(scriptSig))
        txin.scriptSig = scriptSig
        return tx
Ejemplo n.º 2
0
    def withdrawfromvault(self, fromvaultaddress, toaddress, amount):
        vault = self.getvault(fromvaultaddress)
        received = self.chaindb.listreceivedbyvault(fromvaultaddress)
        received = received.values()[0]
        if received['value'] < amount + 2 * utils.calculate_fees(None):
            self.logger.warning("Insufficient funds in vault, exiting, return")
            return

        # create transaction
        tx = CTransaction()

        # to the receiver
        txout = CTxOut()
        txout.nValue = amount
        txout.scriptPubKey = utils.address_to_pay_to_pubkey_hash(toaddress)
        tx.vout.append(txout)

        # from the sender
        nValueIn = 0
        nValueOut = amount

        txin = CTxIn()
        txin.prevout = COutPoint()
        txin.prevout.hash = received['txhash']
        txin.prevout.n = received['n']
        txin.scriptSig = received['scriptPubKey']
        tx.vin.append(txin)

        # calculate nValueIn
        nValueIn = received['value']
        # calculate the total excess amount
        excessAmount = nValueIn - nValueOut
        # calculate the fees
        fees = 2 *                                                                                                                                                                 utils.calculate_fees(tx)
        # create change transaction, if there is any change left
        if excessAmount > fees:
            change_txout = CTxOut()
            change_txout.nValue = excessAmount - fees
            account = self.getaccount()
            changeaddress = fromvaultaddress
            self.logger.debug("Change address: %s" % changeaddress)
            change_txout.scriptPubKey = \
                utils.vault_address_to_pay_to_vault_script(changeaddress)
            tx.vout.append(change_txout)

        # calculate txhash
        tx.calc_sha256()
        txhash = str(tx.sha256)
        key = CKey()
        key.set_pubkey(vault['public_key'])
        key.set_privkey(vault['private_key'])
        signature = key.sign(txhash)
        vaultscript = utils.create_vault_script(vault['address'], \
            vault['master_address'], vault['timeout'], vault['maxfees'])
        scriptSig = chr(OP_VAULT_WITHDRAW) + chr(len(signature)) + signature + \
                chr(len(vaultscript)) + vaultscript
        self.logger.debug("Adding signature: %s" % binascii.hexlify(scriptSig))
        txin.scriptSig = scriptSig
        return tx
Ejemplo n.º 3
0
    def overridevaulttx(self, fromvaultaddress, toaddress):
        vault = self.getvault(fromvaultaddress)
        # select the input addresses
        received = self.chaindb.listallreceivedbyvault(fromvaultaddress)
        if not received:
            self.logger.warning("Empty vault, exiting, return")
            return None, None
        received = received.values()[0]
        if received['value'] < 2 * utils.calculate_fees(None):
            self.logger.warning("Insufficient funds in vault, exiting, return")
            return None, None
        # calculate remaining amount
        amount = received['value'] - 2 * utils.calculate_fees(None)
        # create transaction
        tx = CTransaction()

        # to the receiver
        txout = CTxOut()
        txout.nValue = amount
        txout.scriptPubKey = utils.address_to_pay_to_pubkey_hash(toaddress)
        tx.vout.append(txout)

        # from the sender
        nValueIn = 0
        nValueOut = amount

        txin = CTxIn()
        txin.prevout = COutPoint()
        txin.prevout.hash = received['txhash']
        txin.prevout.n = received['n']
        txin.scriptSig = received['scriptPubKey']
        tx.vin.append(txin)

        # calculate nValueIn
        nValueIn = received['value']
        # calculate the total excess amount
        excessAmount = nValueIn - nValueOut
        # calculate the fees
        fees = utils.calculate_fees(tx)
        # calculate txhash
        tx.calc_sha256()
        txhash = str(tx.sha256)
        key = CKey()
        key.set_pubkey(vault['public_key'])
        key.set_privkey(vault['private_key'])
        signature = key.sign(txhash)
        # get the script
        vaultscript = utils.create_vault_script(vault['address'], \
                vault['master_address'], vault['timeout'], vault['maxfees'])
        scriptSig = chr(OP_VAULT_OVERRIDE) + chr(len(vault['master_public_key'])) + \
        vault['master_public_key'] + chr(len(signature)) + signature + \
        chr(len(vaultscript)) + vaultscript
        self.logger.debug("Adding signature: %s" % binascii.hexlify(scriptSig))
        txin.scriptSig = scriptSig
        return amount, tx
Ejemplo n.º 4
0
    def overridevaulttx(self, fromvaultaddress, toaddress):
        vault = self.getvault(fromvaultaddress)
        # select the input addresses
        received = self.chaindb.listallreceivedbyvault(fromvaultaddress)
        if not received:
            self.logger.warning("Empty vault, exiting, return")
            return None, None
        received = received.values()[0]
        if received['value'] < 2 * utils.calculate_fees(None):
            self.logger.warning("Insufficient funds in vault, exiting, return")
            return None, None
        # calculate remaining amount
        amount = received['value'] - 2 * utils.calculate_fees(None)
        # create transaction
        tx = CTransaction()

        # to the receiver
        txout = CTxOut()
        txout.nValue = amount
        txout.scriptPubKey = utils.address_to_pay_to_pubkey_hash(toaddress)
        tx.vout.append(txout)

        # from the sender
        nValueIn = 0
        nValueOut = amount

        txin = CTxIn()
        txin.prevout = COutPoint()
        txin.prevout.hash = received['txhash']
        txin.prevout.n = received['n']
        txin.scriptSig = received['scriptPubKey']
        tx.vin.append(txin)

        # calculate nValueIn
        nValueIn = received['value']
        # calculate the total excess amount
        excessAmount = nValueIn - nValueOut
        # calculate the fees
        fees = utils.calculate_fees(tx)
        # calculate txhash
        tx.calc_sha256()
        txhash = str(tx.sha256)
        key = CKey()
        key.set_pubkey(vault['public_key'])
        key.set_privkey(vault['private_key'])
        signature = key.sign(txhash)
        # get the script
        vaultscript = utils.create_vault_script(vault['address'], \
                vault['master_address'], vault['timeout'], vault['maxfees'])
        scriptSig = chr(OP_VAULT_OVERRIDE) + chr(len(vault['master_public_key'])) + \
        vault['master_public_key'] + chr(len(signature)) + signature + \
        chr(len(vaultscript)) + vaultscript
        self.logger.debug("Adding signature: %s" % binascii.hexlify(scriptSig))
        txin.scriptSig = scriptSig
        return amount, tx
Ejemplo n.º 5
0
    def sendtoaddress(self, toaddress, amount):        
        # select the input addresses
        funds = 0
        subaccounts = []
        accounts = self.getaccounts()
        for account in accounts:
            for address, subaccount in account.iteritems():
                if subaccount['balance'] == 0:
                    continue
                else:
                    subaccounts.append(subaccount)
                    # print "got one subaccount", subaccount
                    # print "subaccounts: ", subaccounts
                    funds = funds + subaccount['balance']
                    if funds >= amount + utils.calculate_fees(None):
                        break
        
        # print "subaccounts 2: ", subaccounts
        # incase of insufficient funds, return
        if funds < amount + utils.calculate_fees(None):
            print "In sufficient funds, exiting, return"
            return
            
        # create transaction
        tx = CTransaction()
        
        # print "subaccounts 3: ", subaccounts
        # to the receiver
        txout = CTxOut()
        txout.nValue = amount
        txout.scriptPubKey = utils.address_to_pay_to_pubkey_hash(toaddress)
        tx.vout.append(txout)
        
        # from the sender
        nValueIn = 0
        nValueOut = amount
        public_keys = []
        private_keys = []
        # secrets = []
        # print "subaccounts 4: ", subaccounts
        for subaccount in subaccounts:
            # print "subaccount: ", subaccount
            # get received by from address
            previous_txouts = subaccount['received']
            # print "Previous txouts", previous_txouts
            for received in previous_txouts:
                txin = CTxIn()
                txin.prevout = COutPoint()
                txin.prevout.hash = received['txhash']
                txin.prevout.n = received['n']
                txin.scriptSig = binascii.unhexlify(received['scriptPubKey'])
                tx.vin.append(txin)
                nValueIn = nValueIn + received['value']
                public_keys.append(subaccount['public_key']) 
                private_keys.append(subaccount['private_key'])
                # secrets.append(subaccount['secret'])
                if nValueIn >= amount + utils.calculate_fees(tx):
                    break
            if nValueIn >= amount + utils.calculate_fees(tx):
                break

        # calculate the total excess amount
        excessAmount = nValueIn - nValueOut
        # calculate the fees
        fees = utils.calculate_fees(tx)
        # create change transaction, if there is any change left
        if excessAmount > fees:
            change_txout = CTxOut()
            change_txout.nValue = excessAmount - fees
            changeaddress = subaccounts[0]['address']
            change_txout.scriptPubKey = utils.address_to_pay_to_pubkey_hash(changeaddress)
            tx.vout.append(change_txout)
        
        # calculate txhash
        tx.calc_sha256()
        txhash = str(tx.sha256)
        # sign the transaction
        for public_key, private_key, txin in zip(public_keys, private_keys, tx.vin):
            key = CKey()
            key.set_pubkey(public_key)
            key.set_privkey(private_key)
            signature = key.sign(txhash)
            # scriptSig = chr(len(signature)) + hash_type + signature + chr(len(public_key)) + public_key
            scriptSig = chr(len(signature)) + signature + chr(len(public_key)) + public_key
            print "Adding signature: ", binascii.hexlify(scriptSig)
            txin.scriptSig = scriptSig
            print "Tx Validity: ", tx.is_valid()
        return tx
Ejemplo n.º 6
0
    def sendtovault(self, vault_address, amount):
        # select the input addresses
        funds = 0
        subaccounts = []
        accounts = self.getaccounts()
        for account in accounts:
            for address, subaccount in account.iteritems():
                if subaccount['balance'] == 0:
                    continue
                else:
                    subaccounts.append(subaccount)
                    funds = funds + subaccount['balance']
                    if funds >= amount + utils.calculate_fees(None):
                        break

        # incase of insufficient funds, return
        if funds < amount + utils.calculate_fees(None):
            self.logger.warning("Insufficient funds, exiting, return")
            raise exceptions.InsufficientBalanceException

        # create transaction
        tx = CTransaction()

        # to the receiver
        txout = CTxOut()
        txout.nValue = amount
        txout.scriptPubKey = utils.vault_address_to_pay_to_vault_script(vault_address)
        tx.vout.append(txout)

        # from the sender
        nValueIn = 0
        nValueOut = amount
        public_keys = []
        private_keys = []
        for subaccount in subaccounts:
            # get received by from address
            previous_txouts = subaccount['received']
            for received in previous_txouts:
                txin = CTxIn()
                txin.prevout = COutPoint()
                txin.prevout.hash = received['txhash']
                txin.prevout.n = received['n']
                txin.scriptSig = received['scriptPubKey']
                tx.vin.append(txin)
                nValueIn = nValueIn + received['value']
                public_keys.append(subaccount['public_key'])
                private_keys.append(subaccount['private_key'])
                if nValueIn >= amount + utils.calculate_fees(tx):
                    break
            if nValueIn >= amount + utils.calculate_fees(tx):
                break

        # calculate the total excess amount
        excessAmount = nValueIn - nValueOut
        # calculate the fees
        fees = utils.calculate_fees(tx)
        # create change transaction, if there is any change left
        if excessAmount > fees:
            change_txout = CTxOut()
            change_txout.nValue = excessAmount - fees
            changeaddress = self.getnewaddress()[1]
            change_txout.scriptPubKey = utils.address_to_pay_to_pubkey_hash(changeaddress)
            tx.vout.append(change_txout)

        # calculate txhash
        tx.calc_sha256()
        txhash = str(tx.sha256)
        self.logger.debug("Sending to vault %064x" % tx.sha256)
        # sign the transaction
        for public_key, private_key, txin in zip(public_keys, private_keys, tx.vin):
            key = CKey()
            key.set_pubkey(public_key)
            key.set_privkey(private_key)
            signature = key.sign(txhash)
            # scriptSig = chr(len(signature)) + hash_type + signature + chr(len(public_key)) + public_key
            scriptSig = chr(len(signature)) + signature + chr(len(public_key)) + public_key
            self.logger.debug("Adding signature: %s" % binascii.hexlify(scriptSig))
            txin.scriptSig = scriptSig
            self.logger.debug("Tx Validity: %064x" % tx.is_valid())
        # push data to vault
        tx.calc_sha256()
        self.set(str("vault:" + vault_address), {'txhash': tx.sha256})
        return (vault_address, tx)
Ejemplo n.º 7
0


coinbase = "04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73"
scriptPubKeyHex = "4104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac"

# construct previous out point
previousOut = COutPoint()
previousOut.hash = 0
previousOut.n = 4294967295

# construct txin
txin = CTxIn()
txin.coinbase = binascii.unhexlify(coinbase)
txin.scriptSig = binascii.unhexlify(coinbase)
txin.prevout = previousOut

# construct txout
txout = CTxOut()
txout.nValue = 5000000000
txout.scriptPubKey = binascii.unhexlify(scriptPubKeyHex)

# create transaction
tx = CTransaction()
tx.vin.append(txin)
tx.vout.append(txout)
tx.calc_sha256()
print tx
print "Transaction: ", tx.is_valid()
print "hash: ", hex(tx.sha256)
print "Hash: ", "0x4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b"
Ejemplo n.º 8
0

# from utils
def myhash(s):
    return hashlib.sha256(hashlib.sha256(s).digest()).digest()


previous = COutPoint()
txin = CTxIn()
txout = CTxOut()
tx = CTransaction()

# get the outpoint from which we want to spend
previous.hash = 0xeccf7e3034189b851985d871f91384b8ee357cd47c3024736e5676eb2debb3f2
previous.n = 0x01000000
txin.prevout = previous
txin.scriptSig = binascii.unhexlify(
    "76a914010966776006953d5567439e5e39f86a0d273bee88ac")

# create output transaction
txout.nValue = 0x605af40500000000
txout.scriptPubKey = binascii.unhexlify(
    "76a914097072524438d003d23a2f23edb65aae1bb3e46988ac")

# set inputs and outputs
tx.vin.append(txin)
tx.vout.append(txout)
sertx = tx.serialize() + binascii.unhexlify("01000000")
"""
print sertx[:76]
print sertx[76:152]
Ejemplo n.º 9
0
#!/usr/bin/env python

from bitcoin.core import CBlock
from bitcoin.core import CTxIn, CTxOut, CTransaction

txin = CTxIn()
txout = CTxOut()

txin.scriptSig = 0x04FFFF001D0104455468652054696D65732030332F4A616E2F32303039204368616E63656C6C6F72206F6E206272696E6B206F66207365636F6E64206261696C6F757420666F722062616E6B73
txin.prevout = 0x0000000000000000000000000000000000000000000000000000000000000000FFFFFFFF
print txin, txin.is_valid()
tx.vout.nValue = 5000000000
tx.vout.scriptPubKey = 0x5F1DF16B2B704C8A578D0BBAF74D385CDE12C11EE50455F3C438EF4C3FBCF649B6DE611FEAE06279A60939E028A8D65C10B73071A6F16719274855FEB0FD8A6704

tx = CTransaction()
tx.vin = [txin]
tx.vout = [txout]
"""
tx.vout.scriptPubKey = 0x5F1DF16B2B704C8A578D0BBAF74D385CDE12C11EE50455F3C438EF4C3FBCF649B6DE611FEAE06279A60939E028A8D65C10B73071A6F16719274855FEB0FD8A6704 OP_CHECKSIG
"""

block = CBlock()
block.nVersion = 1
block.hashPrevBlock = 0
block.hashMerkleRoot = 0x4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b
block.nTime    = 1231006505
block.nBits    = 0x1d00ffff
block.nNonce   = 2083236893
block.vtx = [tx]
block.sha256 = 0x000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f
print block.is_valid()
Ejemplo n.º 10
0
    def sendtovault(self, vault_address, amount):
        # select the input addresses
        funds = 0
        subaccounts = []
        accounts = self.getaccounts()
        for account in accounts:
            for address, subaccount in account.iteritems():
                if subaccount['balance'] == 0:
                    continue
                else:
                    subaccounts.append(subaccount)
                    funds = funds + subaccount['balance']
                    if funds >= amount + utils.calculate_fees(None):
                        break

        # incase of insufficient funds, return
        if funds < amount + utils.calculate_fees(None):
            self.logger.warning("Insufficient funds, exiting, return")
            raise exceptions.InsufficientBalanceException

        # create transaction
        tx = CTransaction()

        # to the receiver
        txout = CTxOut()
        txout.nValue = amount
        txout.scriptPubKey = utils.vault_address_to_pay_to_vault_script(
            vault_address)
        tx.vout.append(txout)

        # from the sender
        nValueIn = 0
        nValueOut = amount
        public_keys = []
        private_keys = []
        for subaccount in subaccounts:
            # get received by from address
            previous_txouts = subaccount['received']
            for received in previous_txouts:
                txin = CTxIn()
                txin.prevout = COutPoint()
                txin.prevout.hash = received['txhash']
                txin.prevout.n = received['n']
                txin.scriptSig = received['scriptPubKey']
                tx.vin.append(txin)
                nValueIn = nValueIn + received['value']
                public_keys.append(subaccount['public_key'])
                private_keys.append(subaccount['private_key'])
                if nValueIn >= amount + utils.calculate_fees(tx):
                    break
            if nValueIn >= amount + utils.calculate_fees(tx):
                break

        # calculate the total excess amount
        excessAmount = nValueIn - nValueOut
        # calculate the fees
        fees = utils.calculate_fees(tx)
        # create change transaction, if there is any change left
        if excessAmount > fees:
            change_txout = CTxOut()
            change_txout.nValue = excessAmount - fees
            changeaddress = self.getnewaddress()[1]
            change_txout.scriptPubKey = utils.address_to_pay_to_pubkey_hash(
                changeaddress)
            tx.vout.append(change_txout)

        # calculate txhash
        tx.calc_sha256()
        txhash = str(tx.sha256)
        self.logger.debug("Sending to vault %064x" % tx.sha256)
        # sign the transaction
        for public_key, private_key, txin in zip(public_keys, private_keys,
                                                 tx.vin):
            key = CKey()
            key.set_pubkey(public_key)
            key.set_privkey(private_key)
            signature = key.sign(txhash)
            # scriptSig = chr(len(signature)) + hash_type + signature + chr(len(public_key)) + public_key
            scriptSig = chr(len(signature)) + signature + chr(
                len(public_key)) + public_key
            self.logger.debug("Adding signature: %s" %
                              binascii.hexlify(scriptSig))
            txin.scriptSig = scriptSig
            self.logger.debug("Tx Validity: %064x" % tx.is_valid())
        # push data to vault
        tx.calc_sha256()
        self.set(str("vault:" + vault_address), {'txhash': tx.sha256})
        return (vault_address, tx)
Ejemplo n.º 11
0
    def sendtoaddress(self, toaddress, amount):
        # select the input addresses
        funds = 0
        subaccounts = []
        accounts = self.getaccounts()
        for account in accounts:
            for address, subaccount in account.iteritems():
                if subaccount['balance'] == 0:
                    continue
                else:
                    subaccounts.append(subaccount)
                    funds = funds + subaccount['balance']
                    if funds >= amount + utils.calculate_fees(None):
                        break

        # incase of insufficient funds, return
        if funds < amount + utils.calculate_fees(None):
            self.logger.warning("Insufficient funds, exiting, return")
            return None, None

        # create transaction
        tx = CTransaction()

        # to the receiver
        txout = CTxOut()
        txout.nValue = amount
        txout.scriptPubKey = utils.address_to_pay_to_pubkey_hash(toaddress)
        tx.vout.append(txout)

        # from the sender
        nValueIn = 0
        nValueOut = amount
        public_keys = []
        private_keys = []
        # secrets = []
        for subaccount in subaccounts:
            # get received by from address
            previous_txouts = subaccount['received']
            for received in previous_txouts:
                txin = CTxIn()
                txin.prevout = COutPoint()
                txin.prevout.hash = received['txhash']
                txin.prevout.n = received['n']
                txin.scriptSig = received['scriptPubKey']
                tx.vin.append(txin)
                nValueIn = nValueIn + received['value']
                public_keys.append(subaccount['public_key'])
                private_keys.append(subaccount['private_key'])
                # secrets.append(subaccount['secret'])
                if nValueIn >= amount + utils.calculate_fees(tx):
                    break
            if nValueIn >= amount + utils.calculate_fees(tx):
                break

        # calculate the total excess amount
        excessAmount = nValueIn - nValueOut
        # calculate the fees
        fees = utils.calculate_fees(tx)
        # create change transaction, if there is any change left
        if excessAmount > fees:
            change_txout = CTxOut()
            change_txout.nValue = excessAmount - fees
            changeaddress = subaccounts[0]['address']
            change_txout.scriptPubKey = utils.address_to_pay_to_pubkey_hash(
                changeaddress)
            tx.vout.append(change_txout)

        # calculate txhash
        tx.calc_sha256()
        txhash = str(tx.sha256)
        # sign the transaction
        for public_key, private_key, txin in zip(public_keys, private_keys,
                                                 tx.vin):
            key = CKey()
            key.set_pubkey(public_key)
            key.set_privkey(private_key)
            signature = key.sign(txhash)
            # scriptSig = chr(len(signature)) + hash_type + signature + chr(len(public_key)) + public_key
            scriptSig = chr(len(signature)) + signature + chr(
                len(public_key)) + public_key
            txin.scriptSig = scriptSig
        return amount, tx