示例#1
0
def get_radd_from_pub(pub):
    try:
        taker_addr = str(P2PKHBitcoinAddress.from_pubkey(x("02"+pub)))
    except:
        try:
            taker_addr = str(P2PKHBitcoinAddress.from_pubkey(x("03"+pub)))
        except:
            pass
        taker_addr = pub
        pass
    return str(taker_addr)
示例#2
0
def get_radd_from_pub(pub):
    try:
        taker_addr = str(P2PKHBitcoinAddress.from_pubkey(x("02"+pub)))
    except Exception as e:
        print(e)
        try:
            taker_addr = str(P2PKHBitcoinAddress.from_pubkey(x("03"+pub)))
        except Exception as e:
            print(e)
            taker_addr = pub
            pass
        pass
    return str(taker_addr)
示例#3
0
def get_radd_from_pub(pub):
    try:
        addr = str(P2PKHBitcoinAddress.from_pubkey(x(pub)))
    except Exception as e:
        try:
            addr = str(P2PKHBitcoinAddress.from_pubkey(x(pub)))
        except Exception as e:
            print(colorize(str(e) + ": " + pub, 'red'))
            print(e)
            addr = pub
            pass
        pass
    return str(addr)
示例#4
0
def VerifyMessage(address, message, sig):
    sig = base64.b64decode(sig)
    hash = message.GetHash()

    pubkey = CPubKey.recover_compact(hash, sig)

    return str(P2PKHBitcoinAddress.from_pubkey(pubkey)) == str(address)
示例#5
0
 def get_wallet_unspent_fastgraph_transactions(self, address):
     result = [
         x for x in self.mongo.db.fastgraph_transactions.find(
             {'txn.outputs.to': address})
     ]
     reverse_public_key = None
     for x in result:
         xaddress = str(
             P2PKHBitcoinAddress.from_pubkey(bytes.fromhex(
                 x['public_key'])))
         if xaddress == address:
             reverse_public_key = x['public_key']
             break
     if not reverse_public_key:
         for x in result:
             yield x['txn']
         return
     for x in result:
         spent_on_fastgraph = self.mongo.db.fastgraph_transactions.find({
             'public_key':
             reverse_public_key,
             'txn.inputs.id':
             x['id']
         })
         spent_on_blockchain = self.mongo.db.blocks.find({
             'public_key':
             reverse_public_key,
             'transactions.inputs.id':
             x['id']
         })
         if not spent_on_fastgraph.count(
         ) and not spent_on_blockchain.count():
             # x['txn']['height'] = x['height'] # TODO: make height work for frastgraph transactions so we can order messages etc.
             yield x['txn']
示例#6
0
    def __init__(self, config):
        self.seed = config.get('seed', '')
        self.xprv = config.get('xprv', '')
        self.username = config.get('username', '')
        self.public_key = config.get('public_key')
        self.address = str(
            P2PKHBitcoinAddress.from_pubkey(bytes.fromhex(self.public_key)))

        self.private_key = config.get('private_key')
        self.wif = self.to_wif(self.private_key)
        self.bulletin_secret = self.inst_get_bulletin_secret()

        self.mongodb_host = config.get('mongodb_host')
        self.database = config.get('database')
        self.site_database = config.get('site_database')
        self.web_server_host = config.get('web_server_host')
        self.web_server_port = config.get('web_server_port')
        if config.get('peer_host') == '0.0.0.0' or config.get(
                'peer_host') == 'localhost':
            raise Exception(
                "cannot use localhost or 0.0.0.0, must specify public ipv4 address"
            )
        if config.get('peer_host') == '[my public ip]':
            raise Exception(
                "please configure your peer_post to your public ipv4 address")
        self.peer_host = config.get('peer_host')
        self.peer_port = config.get('peer_port')
        self.serve_host = config.get('serve_host')
        self.serve_port = config.get('serve_port')
        self.callbackurl = config.get('callbackurl')
        self.fcm_key = config.get('fcm_key')
示例#7
0
def calc_addr_from_pubkey(coin, pubkey):
    bitcoin.params = COIN_PARAMS[coin]
    try:
        return str(P2PKHBitcoinAddress.from_pubkey(x(pubkey)))
    except Exception as e:
        logger.error(f"[calc_addr_from_pubkey] Exception: {e}")
        return {"error": str(e)}
示例#8
0
    def from_dict(cls, config):
        cls.seed = config.get('seed', '')
        cls.xprv = config.get('xprv', '')
        cls.username = config.get('username', '')
        cls.public_key = config.get('public_key')
        cls.address = str(
            P2PKHBitcoinAddress.from_pubkey(bytes.fromhex(cls.public_key)))

        cls.private_key = config.get('private_key')
        cls.wif = cls.to_wif(cls.private_key)
        cls.bulletin_secret = cls.get_bulletin_secret()

        cls.mongodb_host = config.get('mongodb_host')
        cls.database = config.get('database')
        cls.site_database = config.get('site_database')
        cls.web_server_host = config.get('web_server_host')
        cls.web_server_port = config.get('web_server_port')
        if config.get('peer_host') == '0.0.0.0' or config.get(
                'peer_host') == 'localhost':
            raise Exception(
                "cannot use localhost or 0.0.0.0, must specify public ipv4 address"
            )
        if config.get('peer_host') == '[my public ip]':
            raise Exception(
                "please configure your peer_post to your public ipv4 address")
        cls.peer_host = config.get('peer_host')
        cls.peer_port = config.get('peer_port')
        cls.serve_host = config.get('serve_host')
        cls.serve_port = config.get('serve_port')
        cls.callbackurl = config.get('callbackurl')
        cls.fcm_key = config.get('fcm_key')
示例#9
0
def privkey_to_address(privkey):
    try:
        key = CBitcoinSecret(privkey)
        address = str(P2PKHBitcoinAddress.from_pubkey(key.pub))
    except:
        return False
    return address
示例#10
0
    async def collect_needed_inputs(self, input_obj, input_txn, my_address,
                                    input_sum, inputs, outputs_and_fee_total):

        if isinstance(input_obj, ExternalInput):
            await input_txn.verify()
            address = str(
                P2PKHBitcoinAddress.from_pubkey(
                    bytes.fromhex(input_txn.public_key)))
        else:
            address = my_address

        for txn_output in input_txn.outputs:
            if txn_output.to == address and float(txn_output.value) > 0.0:
                fix1 = fix_float1(txn_output.value)
                fix2 = fix_float2(txn_output.value)
                fixtotal1 = fix_float1(outputs_and_fee_total)
                fixtotal2 = fix_float2(outputs_and_fee_total)
                if (self.exact_match and fix1 != fixtotal1
                        and fix2 != fixtotal2):
                    continue
                input_sum += txn_output.value

                if input_txn not in inputs:
                    inputs.append(input_obj)

                if input_sum >= outputs_and_fee_total:
                    return input_sum
        return input_sum
示例#11
0
def calc_addr_tool(pubkey, pubtype, p2shtype, wiftype):
    class CoinParams(CoreMainParams):
        MESSAGE_START = b'\x24\xe9\x27\x64'
        DEFAULT_PORT = 7770
        BASE58_PREFIXES = {
            'PUBKEY_ADDR': int(pubtype),
            'SCRIPT_ADDR': int(p2shtype),
            'SECRET_KEY': int(wiftype)
        }

    bitcoin.params = CoinParams

    try:
        address = str(P2PKHBitcoinAddress.from_pubkey(x(pubkey)))
        return {
            "pubkey": pubkey,
            "pubtype": pubtype,
            "p2shtype": p2shtype,
            "wiftype": wiftype,
            "address": address
        }

    except Exception as e:
        logger.error(f"[calc_addr_tool] Exception: {e}")
        return {"error": str(e)}
示例#12
0
def get_public_addresses(key_file):
    keys = read_keys(key_file)
    bitcoin_secrets = [CBitcoinSecret(key) for key in keys]
    bitcoin_addresses = [
        P2PKHBitcoinAddress.from_pubkey(key.pub) for key in bitcoin_secrets
    ]
    return bitcoin_addresses
示例#13
0
    def from_dict(cls, config):
        cls.public_key = config['public_key']
        cls.address = str(
            P2PKHBitcoinAddress.from_pubkey(cls.public_key.decode('hex')))

        cls.private_key = config['private_key']
        cls.username = config['username']
        cls.wif = cls.to_wif()
        cipher = Crypt(str(cls.private_key))
        cls.bulletin_secret = cls.get_bulletin_secret()

        cls.mongodb_host = config['mongodb_host']
        cls.database = config['database']
        cls.site_database = config['site_database']
        cls.web_server_host = config['web_server_host']
        cls.web_server_port = config['web_server_port']
        if config['peer_host'] == '0.0.0.0' or config[
                'peer_host'] == 'localhost':
            raise Exception(
                "cannot use localhost or 0.0.0.0, must specify public ipv4 address"
            )
        if config['peer_host'] == '[my public ip]':
            raise Exception(
                "please configure your peer_post to your public ipv4 address")
        cls.peer_host = config['peer_host']
        cls.peer_port = config['peer_port']
        cls.serve_host = config['serve_host']
        cls.serve_port = config['serve_port']
        cls.callbackurl = config['callbackurl']
        cls.fcm_key = config['fcm_key']
示例#14
0
    async def from_dict(cls, block):
        transactions = []
        for txn in block.get('transactions'):
            # TODO: do validity checking for coinbase transactions
            if str(P2PKHBitcoinAddress.from_pubkey(bytes.fromhex(block.get('public_key')))) in [x['to'] for x in txn.get('outputs', '')] and len(txn.get('outputs', '')) == 1 and not txn.get('inputs') and not txn.get('relationship'):
                txn['coinbase'] = True  
            else:
                txn['coinbase'] = False
            transactions.append(Transaction.from_dict(txn))

        if block.get('special_target', 0) == 0:
            block['special_target'] = block.get('target')

        return await cls.init_async(
            version=block.get('version'),
            block_time=block.get('time'),
            block_index=block.get('index'),
            public_key=block.get('public_key'),
            prev_hash=block.get('prevHash'),
            nonce=block.get('nonce'),
            transactions=transactions,
            block_hash=block.get('hash'),
            merkle_root=block.get('merkleRoot'),
            signature=block.get('id'),
            special_min=block.get('special_min'),
            header=block.get('header', ''),
            target=int(block.get('target'), 16),
            special_target=int(block.get('special_target', 0), 16)
        )
示例#15
0
    def from_dict(cls, config):
        from transactionutils import TU
        cls.seed = config.get('seed', '')
        cls.xprv = config.get('xprv', '')
        cls.username = config.get('username', '')
        cls.network = config.get('network', 'mainnet')
        cls.public_key = config['public_key']
        cls.address = str(P2PKHBitcoinAddress.from_pubkey(cls.public_key.decode('hex')))

        cls.private_key = config['private_key']
        cls.wif = cls.generate_wif(cls.private_key)
        cls.bulletin_secret = TU.generate_deterministic_signature(config, config['username'], config['private_key'])

        cls.mongodb_host = config['mongodb_host']
        cls.database = config['database']
        cls.site_database = config['site_database']
        cls.web_server_host = config['web_server_host']
        cls.web_server_port = config['web_server_port']
        if config['peer_host'] == '0.0.0.0' or config['peer_host'] == 'localhost':
            raise Exception("cannot use localhost or 0.0.0.0, must specify public ipv4 address")
        if config['peer_host'] == '[my public ip]':
            raise Exception("please configure your peer_post to your public ipv4 address")
        cls.peer_host = config['peer_host']
        cls.peer_port = config['peer_port']
        cls.serve_host = config['serve_host']
        cls.serve_port = config['serve_port']
        cls.callbackurl = config['callbackurl']
        cls.fcm_key = config['fcm_key']
示例#16
0
文件: block.py 项目: icook/yadacoin
    def from_dict(cls, config, mongo, block):
        transactions = []
        for txn in block.get('transactions'):
            # TODO: do validify checking for coinbase transactions
            if str(
                    P2PKHBitcoinAddress.from_pubkey(
                        block.get('public_key').decode('hex'))) in [
                            x['to'] for x in txn.get('outputs', '')
                        ] and len(
                            txn.get('outputs',
                                    '')) == 1 and not txn.get('relationship'):
                txn['coinbase'] = True
            else:
                txn['coinbase'] = False
            if 'signatures' in txn:
                transactions.append(FastGraph.from_dict(config, mongo, txn))
            else:
                transactions.append(Transaction.from_dict(config, mongo, txn))

        return cls(config=config,
                   mongo=mongo,
                   version=block.get('version'),
                   block_time=block.get('time'),
                   block_index=block.get('index'),
                   public_key=block.get('public_key'),
                   prev_hash=block.get('prevHash'),
                   nonce=block.get('nonce'),
                   transactions=transactions,
                   block_hash=block.get('hash'),
                   merkle_root=block.get('merkleRoot'),
                   signature=block.get('id'),
                   special_min=block.get('special_min'),
                   target=int(block.get('target'), 16))
示例#17
0
def privkey_to_address(privkey):
    try:
        key = CBitcoinSecret(privkey)
        address = str(P2PKHBitcoinAddress.from_pubkey(key.pub))
    except:
        return False
    return address
示例#18
0
文件: block.py 项目: icook/yadacoin
 def save(self):
     self.verify()
     for txn in self.transactions:
         if txn.inputs:
             address = str(
                 P2PKHBitcoinAddress.from_pubkey(
                     txn.public_key.decode('hex')))
             unspent = BU.get_wallet_unspent_transactions(
                 self.config, self.mongo, address,
                 [x.id for x in txn.inputs])
             unspent_ids = [x['id'] for x in unspent]
             failed = False
             used_ids_in_this_txn = []
             for x in txn.inputs:
                 if x.id not in unspent_ids:
                     failed = True
                 if x.id in used_ids_in_this_txn:
                     failed = True
                 used_ids_in_this_txn.append(x.id)
             if failed:
                 raise BaseException('double spend',
                                     [x.id for x in txn.inputs])
     res = self.mongo.db.blocks.find({"index": (int(self.index) - 1)})
     if res.count() and res[0]['hash'] == self.prev_hash or self.index == 0:
         self.mongo.db.blocks.insert(self.to_dict())
     else:
         print "CRITICAL: block rejected..."
示例#19
0
 async def recover_missing_transaction(self, txn_id, exclude_ids=[]):
     return False
     if await self.config.mongo.async_db.failed_recoveries.find_one({'txn_id': txn_id}):
         return False
     self.app_log.warning("recovering missing transaction input: {}".format(txn_id))
     address = str(P2PKHBitcoinAddress.from_pubkey(bytes.fromhex(self.public_key)))
     missing_txns = self.config.mongo.async_db.blocks.aggregate([
         {
             '$unwind': '$transactions'
         },
         {
             '$project': {
                 'transaction': '$transactions',
                 'index': '$index'
             }
         }
     ], allowDiskUse=True)
     async for missing_txn in missing_txns:
         self.app_log.warning('recovery searching block index: {}'.format(missing_txn['index']))
         try:
             result = verify_signature(base64.b64decode(txn_id), missing_txn['transaction']['hash'].encode(),
                                     bytes.fromhex(self.public_key))
             if result:
                 block_index = await self.find_unspent_missing_index(missing_txn['transaction']['hash'], exclude_ids)
                 if block_index:
                     await self.replace_missing_transaction_input(
                         block_index,
                         missing_txn['transaction']['hash'],
                         txn_id
                     )
                     return True
             else:
                 if len(base64.b64decode(txn_id)) != 65:
                     continue
                 result = VerifyMessage(
                     address,
                     BitcoinMessage(missing_txn['transaction']['hash'], magic=''),
                     txn_id
                 )
                 if result:
                     block_index = await self.find_unspent_missing_index(missing_txn['transaction']['hash'], exclude_ids)
                     if block_index:
                         await self.replace_missing_transaction_input(
                             block_index,
                             missing_txn['transaction']['hash'],
                             txn_id
                         )
                         return True
         except:
             continue
     await self.config.mongo.async_db.failed_recoveries.update_one({
         'txn_id': txn_id
     },
     {
         '$set': {
             'txn_id': txn_id
         }
     }, upsert=True)
     return False
示例#20
0
文件: block.py 项目: icook/yadacoin
 def get_coinbase(self):
     for txn in self.transactions:
         if str(
                 P2PKHBitcoinAddress.from_pubkey(
                     self.public_key.decode('hex'))) in [
                         x.to for x in txn.outputs
                     ] and len(txn.outputs) == 1 and not txn.relationship:
             return txn
示例#21
0
    def verify(self):
        super(FastGraph, self).verify()
        result = self.mongo.db.fastgraph_transactions.find_one({
            'txn.hash': self.hash
        })
        
        if not self.signatures:
            raise InvalidFastGraphTransactionException('no signatures were provided')

        xaddress = str(P2PKHBitcoinAddress.from_pubkey(self.public_key.decode('hex')))
        unspent = [x['id'] for x in BU.get_wallet_unspent_transactions(self.config, self.mongo, xaddress)]
        unspent_fastgraph = [x['id'] for x in BU.get_wallet_unspent_fastgraph_transactions(self.config, self.mongo, xaddress)]
        inputs = [x.id for x in self.inputs]
        if len(set(inputs) & set(unspent)) != len(inputs) and len(set(inputs) & set(unspent_fastgraph)) != len(inputs):
            raise InvalidFastGraphTransactionException('Input not found in unspent')

        txn_for_rids = self.get_origin_relationship()
        if not txn_for_rids:
            raise InvalidFastGraphTransactionException('no origin transactions found')
        public_key = txn_for_rids['public_key']

        for signature in self.signatures:
            signature.passed = False
            signed = verify_signature(
                base64.b64decode(signature.signature),
                self.hash,
                public_key.decode('hex')
            )
            if signed:
                signature.passed = True

            """
            # This is for a later fork to include a wider consensus area for a larger spending group
            else:
                mutual_friends = [x for x in BU.get_transactions_by_rid(self.config, self.mongo, self.rid, self.config.bulletin_secret, raw=True, rid=True, lt_block_height=highest_height)]
                for mutual_friend in mutual_friends:
                    mutual_friend = Transaction.from_dict(self.config, self.mongo, mutual_friend)
                    if isinstance(mutual_friend.relationship, Relationship) and signature.bulletin_secret == mutual_friend.relationship.their_bulletin_secret:
                        other_mutual_friend = mutual_friend
                for mutual_friend in mutual_friends:
                    mutual_friend = Transaction.from_dict(self.config, self.mongo, mutual_friend)
                    if mutual_friend.public_key != self.config.public_key:
                        identity = verify_signature(
                            base64.b64decode(other_mutual_friend.relationship.their_bulletin_secret),
                            other_mutual_friend.relationship.their_username,
                            mutual_friend.public_key.decode('hex')
                        )
                        signed = verify_signature(
                            base64.b64decode(signature.signature),
                            self.hash,
                            mutual_friend.public_key.decode('hex')
                        )
                        if identity and signed:
                            signature.passed = True
            """
        for signature in self.signatures:
            if not signature.passed:
                raise InvalidFastGraphTransactionException('not all signatures verified')
示例#22
0
    def verify(self):
        getcontext().prec = 8
        if int(self.version) != int(CHAIN.get_version_for_height(self.index)):
            raise Exception("Wrong version for block height", self.version, CHAIN.get_version_for_height(self.index))

        txns = self.get_transaction_hashes()
        verify_merkle_root = self.get_merkle_root(txns)
        if verify_merkle_root != self.merkle_root:
            raise Exception("Invalid block merkle root")

        header = self.generate_header()
        hashtest = self.generate_hash_from_header(self.index, header, str(self.nonce))
        # print("header", header, "nonce", self.nonce, "hashtest", hashtest)
        if self.hash != hashtest:
            getLogger("tornado.application").warning("Verify error hashtest {} header {} nonce {}".format(hashtest, header, self.nonce))
            raise Exception('Invalid block hash')

        address = P2PKHBitcoinAddress.from_pubkey(bytes.fromhex(self.public_key))
        try:
            # print("address", address, "sig", self.signature, "pubkey", self.public_key)
            result = verify_signature(base64.b64decode(self.signature), self.hash.encode('utf-8'), bytes.fromhex(self.public_key))
            if not result:
                raise Exception("block signature1 is invalid")
        except:
            try:
                result = VerifyMessage(address, BitcoinMessage(self.hash.encode('utf-8'), magic=''), self.signature)
                if not result:
                    raise
            except:
                raise Exception("block signature2 is invalid")

        # verify reward
        coinbase_sum = 0
        for txn in self.transactions:
            if int(self.index) > CHAIN.CHECK_TIME_FROM and (int(txn.time) > int(self.time) + CHAIN.TIME_TOLERANCE):
                #yadacoin.core.config.CONFIG.mongo.db.miner_transactions.remove({'id': txn.transaction_signature}, multi=True)
                #raise Exception("Block embeds txn too far in the future")
                pass

            if txn.coinbase:
                for output in txn.outputs:
                    coinbase_sum += float(output.value)

        fee_sum = 0.0
        for txn in self.transactions:
            if not txn.coinbase:
                fee_sum += float(txn.fee)
        reward = CHAIN.get_block_reward(self.index)

        #if Decimal(str(fee_sum)[:10]) != Decimal(str(coinbase_sum)[:10]) - Decimal(str(reward)[:10]):
        """
        KO for block 13949
        0.02099999 50.021 50.0
        Integrate block error 1 ('Coinbase output total does not equal block reward + transaction fees', 0.020999999999999998, 0.021000000000000796)
        """
        if quantize_eight(fee_sum) != quantize_eight(coinbase_sum - reward):
            print(fee_sum, coinbase_sum, reward)
            raise Exception("Coinbase output total does not equal block reward + transaction fees", fee_sum, (coinbase_sum - reward))
示例#23
0
def make_self_transaction():
	words = get_randomness('keys.txt')
	# seckey = CBitcoinSecret.from_secret_bytes(our_keys[1])
	h = hashlib.sha256(words).digest()
	seckey = CBitcoinSecret.from_secret_bytes(h)
	input_hashes = [('08f7e2c1238cc9b918649e40d72815f32be6dc1ad538cb25331bd1f1c58a5f46',0),
	('8642baa47de6ece50c2800221e5bc7eefd7adf4158f24af31fdcfa185cb54fce', 1)]
	address = P2PKHBitcoinAddress.from_pubkey(seckey.pub)  # "1F26pNMrywyZJdr22jErtKcjF8R3Ttt55G"
	return make_transaction(0.00092, input_hashes, address, seckey)
示例#24
0
文件: block.py 项目: icook/yadacoin
    def verify(self):
        getcontext().prec = 8
        if int(self.version) != int(BU.get_version_for_height(self.index)):
            raise BaseException("Wrong version for block height", self.version,
                                BU.get_version_for_height(self.index))
        try:
            txns = self.get_transaction_hashes()
            self.set_merkle_root(txns)
            if self.verify_merkle_root != self.merkle_root:
                raise BaseException("Invalid block")
        except:
            raise

        try:
            header = BlockFactory.generate_header(self)
            hashtest = BlockFactory.generate_hash_from_header(
                header, str(self.nonce))
            if self.hash != hashtest:
                raise BaseException('Invalid block')
        except:
            raise

        address = P2PKHBitcoinAddress.from_pubkey(
            self.public_key.decode('hex'))
        try:
            result = verify_signature(base64.b64decode(self.signature),
                                      self.hash, self.public_key.decode('hex'))
            if not result:
                raise Exception("block signature is invalid")
        except:
            try:
                result = VerifyMessage(address,
                                       BitcoinMessage(self.hash, magic=''),
                                       self.signature)
                if not result:
                    raise
            except:
                raise BaseException("block signature is invalid")

        # verify reward
        coinbase_sum = 0
        for txn in self.transactions:
            if txn.coinbase:
                for output in txn.outputs:
                    coinbase_sum += float(output.value)

        fee_sum = 0.0
        for txn in self.transactions:
            if not txn.coinbase:
                fee_sum += float(txn.fee)
        reward = BU.get_block_reward(self.config, self.mongo, self)

        if Decimal(str(fee_sum)[:10]) != (Decimal(str(coinbase_sum)[:10]) -
                                          Decimal(str(reward)[:10])):
            raise BaseException(
                "Coinbase output total does not equal block reward + transaction fees",
                fee_sum, (coinbase_sum - reward))
示例#25
0
def fetch_key_for_address(key_file, address):
    keys = read_keys(key_file)
    bitcoin_secrets = [CBitcoinSecret(key) for key in keys]
    for key in bitcoin_secrets:
        addr = P2PKHBitcoinAddress.from_pubkey(key.pub)
        if str(addr) == address:
            return key

    return None
示例#26
0
    def create_checksum_signature(self):
        key = CBitcoinSecret(self.primary_private_key.wif())
        address = P2PKHBitcoinAddress.from_pubkey(key.pub)

        signature = None

        print(self.primary_private_key.wif(), str(address))

        return str(address), signature
示例#27
0
 def parse_secret(self, txid):
     raw = zcashd.gettransaction(txid, True)['hex']
     decoded = zcashd.call('decoderawtransaction', raw)
     scriptSig = decoded['vin'][0]['scriptSig']
     asm = scriptSig['asm'].split(" ")
     pubkey = asm[1]
     secret = x2s(asm[2])
     redeemPubkey = P2PKHBitcoinAddress.from_pubkey(x(pubkey))
     return secret
示例#28
0
def analyze_tx(tx_hex_string):
    output = {}

    # get op_return from transaction
    hex = unhexlify(tx_hex_string)
    deserializedTransaction = CTransaction.deserialize(hex)
    op_return_vout = deserializedTransaction.vout[1].scriptPubKey

    # get redeem script
    redeem_script = ''
    for i in op_return_vout:
        script = bytes(i).decode('utf8')
        if 'REDEEM' in script:
            redeem_script_string = script.replace('REDEEM SCRIPT ', '')
    output['redeemScript'] = redeem_script_string

    # convert redeem script into list
    redeemScript = CScript(unhexlify(redeem_script_string))
    redeem_script_array = []
    for i in redeemScript:
        redeem_script_array.append(i)

    # get redeem script hash (hodl address)
    p2sh_address = P2SHBitcoinAddress.from_redeemScript(redeemScript)
    output['hodlAddress'] = str(p2sh_address)

    # get nlocktime from redeem script
    nlocktime_hex = b2lx(redeem_script_array[0])
    nlocktime = int(nlocktime_hex, 16)
    output['nLockTime'] = nlocktime

    # get authorized key from redeem script
    pubkey = b2x(redeem_script_array[3])

    # get address from authorized key
    pubkey = unhexlify(pubkey)
    P2PKHBitcoinAddress = bitcoin.wallet.P2PKHBitcoinAddress
    addr = P2PKHBitcoinAddress.from_pubkey(pubkey)
    output['authorizedAddress'] = str(addr)

    # get total sent to hodl address
    locked_satoshis = 0
    for i in deserializedTransaction.vout:
        if i.nValue > 0:
            sPK = i.scriptPubKey
            amount = i.nValue
            try:
                vout_p2sh_addr = P2SHBitcoinAddress.from_scriptPubKey(sPK)
                # rewards only paid to really locked funds
                if str(p2sh_address) == str(vout_p2sh_addr):
                    locked_satoshis += amount
            except:
                pass
    output["lockedSatoshis"] = locked_satoshis

    return (output)
示例#29
0
def print_verbose(signature, key, msg):
    secret = CBitcoinSecret(key)
    address = P2PKHBitcoinAddress.from_pubkey(secret.pub)
    message = BitcoinMessage(msg)
    print('Address: %s' % address)
    print('Message: %s' % msg)
    print('Signature: %s' % signature)
    print('Verified: %s' % VerifyMessage(address, message, signature))
    print('\nTo verify using bitcoin core:')
    print('\n`bitcoin-cli verifymessage %s \'%s\' \'%s\'`\n' % (address, signature.decode('ascii'), msg))
示例#30
0
 def get_coinbase(self):
     for txn in self.transactions:
         if str(
                 P2PKHBitcoinAddress.from_pubkey(
                     bytes.fromhex(self.public_key))) in [
                         x.to for x in txn.outputs
                     ] and len(
                         txn.outputs) == 1 and not txn.relationship and len(
                             txn.inputs) == 0:
             return txn
示例#31
0
def print_verbose(signature, key, msg):
    secret = CBitcoinSecret(key)
    address = P2PKHBitcoinAddress.from_pubkey(secret.pub)
    message = BitcoinMessage(msg)
    print('Address: %s' % address)
    print('Message: %s' % msg)
    print('Signature: %s' % signature)
    print('Verified: %s' % VerifyMessage(address, message, signature))
    print('\nTo verify using bitcoin core:')
    print('\n`bitcoin-cli verifymessage %s \'%s\' \'%s\'`\n' %
          (address, signature.decode('ascii'), msg))
示例#32
0
    def _get_keys(self, txid, block):
        """
            Retrieve all keys from a transaction and construct a list according
            to the db schema.
            -txid:  transaction id
            -block: block number
        """
        tx = self.proxy.getrawtransaction(txid)
        tx_d = self.proxy.decoderawtransaction(tx)

        keys = []
        return_keys = []
        for elt in tx_d['vin']:
            if 'coinbase' in elt.keys():
                continue
            if 'txinwitness' in elt.keys():
                tab = elt['txinwitness']
            elif 'scriptSig' in elt.keys():
                sc = elt['scriptSig']
                tab = [x for x in sc['asm'].split(' ')]

            #If there is a redeem script
            rds = [x for x in tab if x[0] == '5']
            if (len(rds) > 0):
                for x in rds:
                    keys += [p for p in self.proxy.decodescript(x)['asm'].split(' ') if
                            p[0:2] in ['02', '03', '04']]
            else:
                keys += [p for p in tab if p[0:2] in ['02', '03', '04']]

            for key in keys:
                sign = key[:2]
                if sign != "04":
                    #Because sometimes very strange keys, and also because
                    #there are "0"
                    if (len(key) < 60 or len(key) > 70): continue
                    sx = '0x' + key[2:].upper()
                    x = int(sx, 16)
                    y = self.get_y(x, sign)
                    sy = "%X" % y
                    #keys.append([tx, nblock, hash, ])
                else:
                    if (len(key) < 120 or len(key) > 136): continue
                    sx = key[2:66].upper()
                    x = int(sx, 16)
                    sy = key[66:].upper()
                    y = int(sy, 16)

                hash = P2PKHBitcoinAddress.from_pubkey(bytes.fromhex(key), 0)

                return_keys.append([block, txid, str(hash), sx, sy])

            return return_keys
示例#33
0
def lottery_participants(rpc, oracle):
    try:
        mypk = rpc.setpubkey()['pubkey']
    except Exception as e:
        return ('Error: -pubkey is not set' + str(e))

    oraclesinfo = rpc.oraclesinfo(oracle['txid'])

    pubkey_baton = {}
    for reg in oraclesinfo['registered']:
        pubkey_baton[reg['publisher']] = reg['baton']

    participants = []
    for part in pubkey_baton:
        times = []
        time_sample = {}
        pk_addr = P2PKHBitcoinAddress.from_pubkey(x(part))
        orcl_txids = rpc.getaddresstxids(pubkey_baton[part], '1')
        for txid in orcl_txids:
            rawtx = rpc.getrawtransaction(txid, 2)
            try:
                blocktime = rawtx['blocktime']
            except:
                continue
            input_addrs = []
            for vin in rawtx['vin']:
                input_addrs.append(vin['address'])

            # check that someone didn't send an oraclesdata to someone else's baton addr
            if not str(pk_addr) in input_addrs and not pubkey_baton[
                    part] in input_addrs:
                continue

            samples = rpc.oraclessamples(oracle['txid'], pubkey_baton[part],
                                         '0')
            if samples['samples']:
                times.append(blocktime)
                time_sample[blocktime] = samples['samples'][0]['data'][0]
            else:
                continue

        times.sort()
        if times:
            if times[0] < oracle['deadline']:
                try:
                    name_msg = ast.literal_eval(time_sample[times[0]])
                    name = name_msg[0]
                    msg = name_msg[1]
                    participants.append({part: name_msg})
                except:
                    continue

    return (participants)
def make_address_from_passphrase(passphrase, compressed=True, as_str=True):
    """
    Create a Bitcoin address from a passphrase. The passphrase is hashed and
    then used as the secret bytes to construct the CBitcoinSecret.
    """
    if not isinstance(passphrase, bytes):
        passphrase = bytes(passphrase, "utf-8")
    passphrasehash = hashlib.sha256(passphrase).digest()
    private_key = CBitcoinSecret.from_secret_bytes(passphrasehash, compressed=compressed)
    address = P2PKHBitcoinAddress.from_pubkey(private_key.pub)
    if as_str:
        return str(address)
    else:
        return address
示例#35
0
  def test_build_send_script(self):
    """ Run simple sanity checks on script generation """

    # Set up constants for this test
    sender_private_key = CBitcoinSecret.from_secret_bytes(x('4f65da9b656de4036076911707d2b2dbf065689245b8674faa8790e36d7e5850'))
    sender_public_key = sender_private_key.pub
    recipient_private_key = CBitcoinSecret.from_secret_bytes(x('cfe8e33672f7045f020210f3c7afbca660e053e4c9415c542ff185e97b175cf0'))
    recipient_public_key = recipient_private_key.pub
    send_to_key = CBitcoinSecret.from_secret_bytes(x('15a249b4c09286b877d4708191f1ee8de09903bae034dd9dc8e3286451fa1c80'))
    send_to_address = P2PKHBitcoinAddress.from_pubkey(send_to_key.pub)
    secret = x('88d6e51f777b0b8dc0f429da9f372fbc')
    secret_hash = Hash(secret)
    quantity = 1000

    # Build the send transaction
    txins = [] # TODO: Provide some random inputs
    txouts = [CTxOut(quantity, build_send_out_script(sender_public_key, recipient_public_key, secret_hash))]
    send_tx = CMutableTransaction(txins, txouts)
    send_tx_n = 0 # We're working with the first transaction input

    # Build the refund transaction
    nLockTime = 1422177943
    refund_tx = build_unsigned_refund_tx(send_tx, send_tx_n, send_to_address, nLockTime, CFeeRate(0))

    # Actually verify the signatures
    sighash = SignatureHash(send_tx.vout[0].scriptPubKey, refund_tx, 0, SIGHASH_ALL)
    sender_sig = get_refund_tx_sig(refund_tx, sender_private_key, sender_public_key, recipient_public_key, secret_hash)
    self.assertTrue(sender_public_key.verify(sighash, sender_sig[:-1]))
    recipient_sig = get_refund_tx_sig(refund_tx, recipient_private_key, sender_public_key, recipient_public_key, secret_hash)
    self.assertTrue(recipient_public_key.verify(sighash, recipient_sig[:-1]))

    # Test building a complete refund transaction
    refund_tx = build_signed_refund_tx(send_tx, send_tx_n, refund_tx,
      recipient_sig, recipient_public_key,
        sender_private_key, secret_hash)
    # This throws an exception in case of a problem
    VerifyScript(refund_tx.vin[0].scriptSig,
      send_tx.vout[send_tx_n].scriptPubKey, refund_tx, 0, (SCRIPT_VERIFY_P2SH,))
示例#36
0
#
# This file is part of python-bitcoinlib.
#
# It is subject to the license terms in the LICENSE file found in the top-level
# directory of this distribution.
#
# No part of python-bitcoinlib, including this file, may be copied, modified,
# propagated, or distributed except according to the terms contained in the
# LICENSE file.

from __future__ import absolute_import, division, print_function, unicode_literals

from bitcoin.wallet import CBitcoinSecret, P2PKHBitcoinAddress
from bitcoin.signmessage import BitcoinMessage, VerifyMessage, SignMessage

key = CBitcoinSecret("L4vB5fomsK8L95wQ7GFzvErYGht49JsCPJyJMHpB4xGM6xgi2jvG")
address = P2PKHBitcoinAddress.from_pubkey(key.pub)  # "1F26pNMrywyZJdr22jErtKcjF8R3Ttt55G"
message = "Hey I just met you, and this is crazy, but I'll verify my address, maybe ..."

message = BitcoinMessage(message)

signature = SignMessage(key, message)

print(key, address)
print("Address: %s" % address)
print("Message: %s" % message)
print("\nSignature: %s" % signature)
print("\nVerified: %s" % VerifyMessage(address, message, signature))

print("\nTo verify using bitcoin core;")
print("`bitcoin-cli verifymessage %s \"%s\" \"%s\"`" % (address, signature.decode('ascii'), message))
    return CBitcoinSecret.from_secret_bytes(secret_bytes)


def generate_fake_txin():
    fake_txid = Hash(open("/dev/random", "rb").read(100))
    fake_output_index = 0
    return CMutableTxIn(COutPoint(fake_txid, fake_output_index))


def set_txin_unlocking_script(txin, secret_key, tx):
    txin_locking_script = CScript([OP_DUP, OP_HASH160, Hash160(secret_key.pub), OP_EQUALVERIFY, OP_CHECKSIG])
    sighash = SignatureHash(txin_locking_script, tx, 0, SIGHASH_ALL)
    sig = secret_key.sign(sighash) + bytes([SIGHASH_ALL])
    txin.scriptSig = CScript([sig, secret_key.pub])
    VerifyScript(txin.scriptSig, txin_locking_script, tx, 0)
    return txin


def generate_tx(recipient_address, amount, miner_fee):
    txin_list = [generate_fake_txin() for _ in range(0, 10)]
    txout = CMutableTxOut(amount*COIN - miner_fee*COIN, recipient_address.to_scriptPubKey())
    tx = CMutableTransaction(txin_list, [txout])
    [set_txin_unlocking_script(txin, generate_secret_key(), tx) for txin in txin_list]
    return tx


if __name__ == "__main__":
    address = P2PKHBitcoinAddress.from_pubkey(generate_secret_key().pub)
    transaction = generate_tx(address, 0.1, 0.0001)
    print(b2x(transaction.serialize()))
示例#38
0
def pubkey_to_address(pubkey):
    return str(P2PKHBitcoinAddress.from_pubkey(x(pubkey)))
示例#39
0
  def test_send_script_spend(self):
    """
    Run more in-depth execution checks on the script generated for the send transaction
    """
    sender_private_key = CBitcoinSecret.from_secret_bytes(x('4f65da9b656de4036076911707d2b2dbf065689245b8674faa8790e36d7e5850'))
    sender_public_key = sender_private_key.pub
    recipient_private_key = CBitcoinSecret.from_secret_bytes(x('cfe8e33672f7045f020210f3c7afbca660e053e4c9415c542ff185e97b175cf0'))
    recipient_public_key = recipient_private_key.pub
    secret = x('88d6e51f777b0b8dc0f429da9f372fbc')
    secret_hash = Hash(secret)
    send_to_key = CBitcoinSecret.from_secret_bytes(x('15a249b4c09286b877d4708191f1ee8de09903bae034dd9dc8e3286451fa1c80'))
    send_to_address = P2PKHBitcoinAddress.from_pubkey(send_to_key.pub)
    random_tx_id = x('8390b4c8198198c6447da1a6fad498209436a785459936b95a1e3b63618c1d8a')
    value = 10 * COIN

    send_tx_script_pub_key = build_send_out_script(sender_public_key, recipient_public_key, secret_hash)

    send_txins = [CMutableTxIn(COutPoint(random_tx_id, 0))]
    send_txouts = [CMutableTxOut(value, send_tx_script_pub_key)]
    send_tx = CMutableTransaction(send_txins, send_txouts)

    # Test the standard spend transaction

    txins = [CMutableTxIn(COutPoint(Hash(send_tx.serialize()), 0))]
    txouts = [CMutableTxOut(value, send_to_address.to_scriptPubKey())]
    recv_tx = CMutableTransaction(txins, txouts)

    sighash = SignatureHash(send_tx_script_pub_key, recv_tx, 0, SIGHASH_ALL)
    recipient_sig = recipient_private_key.sign(sighash) + (b'\x01') # bytes([SIGHASH_ALL])
    recv_tx.vin[0].scriptSig = CScript([secret, 0, recipient_sig, recipient_public_key])

    VerifyScript(recv_tx.vin[0].scriptSig, send_tx.vout[0].scriptPubKey, recv_tx, 0, (SCRIPT_VERIFY_P2SH,))

    # Test a refund transaction
    refund_tx = CMutableTransaction(txins, txouts)

    sighash = SignatureHash(send_tx_script_pub_key, refund_tx, 0, SIGHASH_ALL)
    sender_sig = sender_private_key.sign(sighash) + (b'\x01') # bytes([SIGHASH_ALL])
    recipient_sig = recipient_private_key.sign(sighash) + (b'\x01') # bytes([SIGHASH_ALL])
    refund_tx.vin[0].scriptSig = CScript([sender_sig, sender_public_key, 1, recipient_sig, recipient_public_key])

    VerifyScript(refund_tx.vin[0].scriptSig, send_tx_script_pub_key, refund_tx, 0, (SCRIPT_VERIFY_P2SH,))

    # Test invalid transactions are rejected

    invalid_tx = CMutableTransaction(txins, txouts)

    sighash = SignatureHash(send_tx_script_pub_key, invalid_tx, 0, SIGHASH_ALL)
    sender_sig = sender_private_key.sign(sighash) + (b'\x01') # bytes([SIGHASH_ALL])
    recipient_sig = recipient_private_key.sign(sighash) + (b'\x01') # bytes([SIGHASH_ALL])

    invalid_tx.vin[0].scriptSig = CScript([])
    with self.assertRaises(MissingOpArgumentsError):
      VerifyScript(invalid_tx.vin[0].scriptSig, send_tx_script_pub_key, invalid_tx, 0, (SCRIPT_VERIFY_P2SH,))

    invalid_tx.vin[0].scriptSig = CScript([recipient_sig, recipient_public_key, 0])
    with self.assertRaises(VerifyOpFailedError):
      VerifyScript(invalid_tx.vin[0].scriptSig, send_tx_script_pub_key, invalid_tx, 0, (SCRIPT_VERIFY_P2SH,))

    invalid_tx.vin[0].scriptSig = CScript([recipient_sig, recipient_public_key, 1])
    with self.assertRaises(VerifyOpFailedError):
      VerifyScript(invalid_tx.vin[0].scriptSig, send_tx_script_pub_key, invalid_tx, 0, (SCRIPT_VERIFY_P2SH,))

    invalid_tx.vin[0].scriptSig = CScript([recipient_sig, recipient_public_key, 1, recipient_sig, recipient_public_key])
    with self.assertRaises(VerifyOpFailedError):
      VerifyScript(invalid_tx.vin[0].scriptSig, send_tx_script_pub_key, invalid_tx, 0, (SCRIPT_VERIFY_P2SH,))

    invalid_tx.vin[0].scriptSig = CScript([sender_sig, sender_public_key, 1, sender_sig, sender_public_key])
    with self.assertRaises(VerifyOpFailedError):
      VerifyScript(invalid_tx.vin[0].scriptSig, send_tx_script_pub_key, invalid_tx, 0, (SCRIPT_VERIFY_P2SH,))