Beispiel #1
0
def get_address_from_pubkey(hex_pubkey):
    """ get bitcoin address from pub key
    """

    pubkey = BitcoinPublicKey(hex_pubkey)

    return pubkey.address()
def get_address_from_pubkey(hex_pubkey):
    """ get bitcoin address from pub key
    """

    pubkey = BitcoinPublicKey(hex_pubkey)

    return pubkey.address()
class AuthResponseTest(unittest.TestCase):
    def setUp(self):
        self.private_key_hex = str(PRIVATE_KEY)
        self.public_key_hex = str(PUBLIC_KEY)
        self.private_key = BitcoinPrivateKey(self.private_key_hex)
        self.public_key = BitcoinPublicKey(self.public_key_hex)
        self.profile = RYAN_PROFILE
        self.username = '******'
        self.sample_encoded_token = RESPONSE_SAMPLE_ENCODED_TOKEN
        self.sample_decoded_token = RESPONSE_SAMPLE_DECODED_TOKEN

    def tearDown(self):
        pass

    def test_auth_response_token_encoding(self):
        # without username, testing basics
        auth_response = AuthResponse(self.private_key_hex, RYAN_PROFILE)
        auth_response_token = auth_response.token()

        decoded_token = AuthResponse.decode(auth_response_token)
        payload = decoded_token['payload']
        self.assertEqual(payload['public_keys'][0], self.public_key_hex)
        self.assertEqual(get_address_from_did(payload['iss']),
                         self.public_key.address())
        self.assertEqual(payload['profile'], self.profile)
        self.assertEqual(payload['username'], None)

        self.assertTrue(AuthResponse.verify(auth_response_token))

        # with username
        with requests_mock.mock() as m:
            m.get(LOCALHOST_CORE_API + NAME_LOOKUP_URL + self.username,
                  text=json.dumps({'address': self.public_key.address()}))
            auth_response = AuthResponse(self.private_key_hex, RYAN_PROFILE,
                                         self.username)
            auth_response_token = auth_response.token()

            self.assertTrue(
                do_public_keys_match_username(
                    auth_response_token, Tokenizer(),
                    AuthResponse.decode(auth_response_token)))
            self.assertTrue(AuthResponse.verify(auth_response_token))

    def test_auth_response_token_decoding(self):
        decoded_token = AuthResponse.decode(self.sample_encoded_token)
        self.assertEqual(decoded_token, self.sample_decoded_token)
Beispiel #4
0
def broadcast(name, data_hash, consensus_hash, private_key, blockchain_client, blockchain_broadcaster=None, tx_only=False, user_public_key=None, testset=False):
    """
    Write a name update into the blockchain.
    Returns a JSON object with 'data' set to the nulldata and 'transaction_hash' set to the transaction hash on success.
    """
    
    # sanity check 
    pay_fee = True
    if user_public_key is not None:
        pay_fee = False
        tx_only = True

    if user_public_key is None and private_key is None:
        raise Exception("Missing both public and private key")
    
    if not tx_only and private_key is None:
        raise Exception("Need private key for broadcasting")
    
    if blockchain_broadcaster is None:
        blockchain_broadcaster = blockchain_client 
    
    from_address = None 
    inputs = None
    private_key_obj = None
    
    if user_public_key is not None:
        # subsidizing 
        pubk = BitcoinPublicKey( user_public_key )
        from_address = pubk.address()
        
        # get inputs from utxo provider 
        inputs = get_unspents( from_address, blockchain_client )

    elif private_key is not None:
        # ordering directly
        pubk = BitcoinPrivateKey( private_key ).public_key()
        public_key = pubk.to_hex()
        
        # get inputs and from address using private key
        private_key_obj, from_address, inputs = analyze_private_key(private_key, blockchain_client)
        
    nulldata = build(name, consensus_hash, data_hash=data_hash, testset=testset)
    outputs = make_outputs( nulldata, inputs, from_address, pay_fee=pay_fee )
    
    if tx_only:
       
        unsigned_tx = serialize_transaction( inputs, outputs )
        return {'unsigned_tx': unsigned_tx}

    else:
       
        signed_tx = tx_serialize_and_sign( inputs, outputs, private_key_obj )
        response = broadcast_transaction( signed_tx, blockchain_broadcaster )
        response.update({'data': nulldata})
        return response
def broadcast(name, destination_address, keepdata, consensus_hash, private_key, blockchain_client, blockchain_broadcaster=None, tx_only=False, user_public_key=None, testset=False):
    
    # sanity check 
    pay_fee = True
    if user_public_key is not None:
        pay_fee = False
        tx_only = True 

    if user_public_key is None and private_key is None:
        raise Exception("Missing both public and private key")
    
    if not tx_only and private_key is None:
        raise Exception("Need private key for broadcasting")
    
    if blockchain_broadcaster is None:
        blockchain_broadcaster = blockchain_client 
    
    from_address = None 
    inputs = None
    private_key_obj = None
    
    if user_public_key is not None:
        # subsidizing 
        pubk = BitcoinPublicKey( user_public_key )

        from_address = pubk.address()
        inputs = get_unspents( from_address, blockchain_client )

    elif private_key is not None:
        # ordering directly 
        pubk = BitcoinPrivateKey( private_key ).public_key()
        public_key = pubk.to_hex()
        
        # get inputs and from address using private key
        private_key_obj, from_address, inputs = analyze_private_key(private_key, blockchain_client)
        
    nulldata = build(name, keepdata, consensus_hash, testset=testset)
    outputs = make_outputs(nulldata, inputs, destination_address, from_address, pay_fee=pay_fee, format='hex')
    
    if tx_only:
    
        unsigned_tx = serialize_transaction( inputs, outputs )
        return {"unsigned_tx": unsigned_tx}
    
    else:
        # serialize, sign, and broadcast the tx
        response = serialize_sign_and_broadcast(inputs, outputs, private_key_obj, blockchain_broadcaster)
        response.update({'data': nulldata})
        return response
class AuthRequestTest(unittest.TestCase):
    def setUp(self):
        self.private_key_hex = str(PRIVATE_KEY)
        self.public_key_hex = str(PUBLIC_KEY)
        self.domain_name = 'localhost:3000'
        self.private_key = BitcoinPrivateKey(self.private_key_hex)
        self.public_key = BitcoinPublicKey(self.public_key_hex)
        self.sample_encoded_token = REQUEST_SAMPLE_ENCODED_TOKEN
        self.sample_decoded_token = REQUEST_SAMPLE_DECODED_TOKEN
        self.maxDiff = None

    def tearDown(self):
        pass

    def test_auth_request_token_encoding(self):
        # valid AuthRequest
        auth_request = AuthRequest(self.private_key_hex, self.domain_name)
        auth_request_token = auth_request.token()

        decoded_token = AuthRequest.decode(auth_request_token)
        payload = decoded_token['payload']
        self.assertEqual(payload['public_keys'][0], self.public_key_hex)
        self.assertEqual(get_address_from_did(payload['iss']),
                         self.public_key.address())
        self.assertEqual(payload['scopes'], [])
        self.assertEqual(payload['manifest_uri'],
                         self.domain_name + '/manifest.json')

        self.assertTrue(AuthRequest.verify(auth_request_token))

        # invalid AuthRequest
        auth_request = AuthRequest(self.private_key_hex, self.domain_name)
        auth_request_token = auth_request.token()[:-1]

        self.assertFalse(AuthRequest.verify(auth_request_token))

    def test_auth_request_token_decoding(self):
        decoded_token = AuthRequest.decode(self.sample_encoded_token)
        self.assertEqual(decoded_token, self.sample_decoded_token)

    def test_custom_openssl_backend(self):
        auth_request = AuthRequest(self.private_key_hex,
                                   self.domain_name,
                                   crypto_backend=openssl_backend)
        auth_request_token = auth_request.token()
        self.assertTrue(AuthRequest.verify(auth_request_token))
Beispiel #7
0
def broadcast(name,
              destination_address,
              keepdata,
              consensus_hash,
              private_key,
              blockchain_client,
              blockchain_broadcaster=None,
              tx_only=False,
              user_public_key=None,
              testset=False):

    # sanity check
    pay_fee = True
    if user_public_key is not None:
        pay_fee = False
        tx_only = True

    if user_public_key is None and private_key is None:
        raise Exception("Missing both public and private key")

    if not tx_only and private_key is None:
        raise Exception("Need private key for broadcasting")

    if blockchain_broadcaster is None:
        blockchain_broadcaster = blockchain_client

    from_address = None
    inputs = None
    private_key_obj = None

    if user_public_key is not None:
        # subsidizing
        pubk = BitcoinPublicKey(user_public_key)

        from_address = pubk.address()
        inputs = get_unspents(from_address, blockchain_client)

    elif private_key is not None:
        # ordering directly
        pubk = BitcoinPrivateKey(private_key).public_key()
        public_key = pubk.to_hex()

        # get inputs and from address using private key
        private_key_obj, from_address, inputs = analyze_private_key(
            private_key, blockchain_client)

    nulldata = build(name, keepdata, consensus_hash, testset=testset)
    outputs = make_outputs(nulldata,
                           inputs,
                           destination_address,
                           from_address,
                           pay_fee=pay_fee,
                           format='hex')

    if tx_only:

        unsigned_tx = serialize_transaction(inputs, outputs)
        return {"unsigned_tx": unsigned_tx}

    else:
        # serialize, sign, and broadcast the tx
        response = serialize_sign_and_broadcast(inputs, outputs,
                                                private_key_obj,
                                                blockchain_broadcaster)
        response.update({'data': nulldata})
        return response
Beispiel #8
0
def broadcast(name,
              data_hash,
              consensus_hash,
              private_key,
              blockchain_client,
              blockchain_broadcaster=None,
              tx_only=False,
              user_public_key=None,
              testset=False):
    """
    Write a name update into the blockchain.
    Returns a JSON object with 'data' set to the nulldata and 'transaction_hash' set to the transaction hash on success.
    """

    # sanity check
    pay_fee = True
    if user_public_key is not None:
        pay_fee = False
        tx_only = True

    if user_public_key is None and private_key is None:
        raise Exception("Missing both public and private key")

    if not tx_only and private_key is None:
        raise Exception("Need private key for broadcasting")

    if blockchain_broadcaster is None:
        blockchain_broadcaster = blockchain_client

    from_address = None
    inputs = None
    private_key_obj = None

    if user_public_key is not None:
        # subsidizing
        pubk = BitcoinPublicKey(user_public_key)
        from_address = pubk.address()

        # get inputs from utxo provider
        inputs = get_unspents(from_address, blockchain_client)

    elif private_key is not None:
        # ordering directly
        pubk = BitcoinPrivateKey(private_key).public_key()
        public_key = pubk.to_hex()

        # get inputs and from address using private key
        private_key_obj, from_address, inputs = analyze_private_key(
            private_key, blockchain_client)

    nulldata = build(name,
                     consensus_hash,
                     data_hash=data_hash,
                     testset=testset)
    outputs = make_outputs(nulldata, inputs, from_address, pay_fee=pay_fee)

    if tx_only:

        unsigned_tx = serialize_transaction(inputs, outputs)
        return {'unsigned_tx': unsigned_tx}

    else:

        signed_tx = tx_serialize_and_sign(inputs, outputs, private_key_obj)
        response = broadcast_transaction(signed_tx, blockchain_broadcaster)
        response.update({'data': nulldata})
        return response
Beispiel #9
0
    output = open('pickled_tmpmap.dat', 'rb')
    tmpmap = pickle.load(output)  # 'obj_dict' is a dict object
else:
    if os.path.isfile("genesis.json") == False:
        print "Downloading Genesis Block"
        download_file()
        save_file()
    json_file = "genesis.json"
    json_data = open(json_file)
    data = json.load(json_data)
    for x in data:
        try:
            addy = ""
            pub = str(x["owner_pubkey"])
            pbl = BitcoinPublicKey(pub)
            tmpmap[pub] = pbl.address()
            addy = tmpmap[pub]

            if addy not in balmap:
                balmap[addy] = 0
            balmap[addy] += float(x["mir_amount"])

        except Exception as e:
            print e

        output = open('pickled_tmpmap.dat', 'wb+')
        pickle.dump(tmpmap, output)
        output.close()
        output = open('pickled_balmap.dat', 'wb+')
        pickle.dump(balmap, output)
        output.close()