Esempio n. 1
0
 def test_valid_address(self):
     """Test whether valid addresses decode to the correct output."""
     for (address, hexscript) in VALID_ADDRESS:
         try:
             scriptpubkey = addr_bech32_to_pubkeyhash(address, include_witver=True)
         except EncodingError:
             scriptpubkey = addr_bech32_to_pubkeyhash(address, prefix='tb', include_witver=True)
         self.assertEqual(scriptpubkey, binascii.unhexlify(hexscript))
         addr = pubkeyhash_to_addr_bech32(scriptpubkey, address[:2].lower())
         self.assertEqual(address.lower(), addr)
Esempio n. 2
0
    def getutxos(self, address, after_txid='', max_txs=10, utxos_per_address=2):
        """
        Dummy method to retreive UTXO's. This method creates a new UTXO for each address provided out of the
        testnet void, which can be used to create test transactions for the bitcoinlib testnet.

        :param address: Address string
        :type address: str
        :param after_txid: Transaction ID of last known transaction. Only check for utxos after given tx id. Default: Leave empty to return all utxos. If used only provide a single address
        :type after_txid: str
        :param max_txs: Maximum number of utxo's to return
        :type max_txs: int

        :return list: The created UTXO set
        """
        utxos = []
        for n in range(utxos_per_address):
            try:
                pkh = str(n).encode() + addr_to_pubkeyhash(address)[1:]
            except:
                pkh = str(n).encode() + addr_bech32_to_pubkeyhash(address)[1:]
            utxos.append(
                {
                    'address': address,
                    'tx_hash': hashlib.sha256(pkh).hexdigest(),
                    'confirmations': 10,
                    'output_n': 0,
                    'index': 0,
                    'value': 1 * self.units,
                    'script': '',
                }
            )
        return utxos
Esempio n. 3
0
    def getutxos(self, addresslist, utxos_per_address=2):
        """
        Dummy method to retreive UTXO's. This method creates a new UTXO for each address provided out of the
        testnet void, which can be used to create test transactions for the bitcoinlib testnet.

        :param addresslist: List of addresses
        :type addresslist: list
        :param utxos_per_address: Number of UTXO's to be created per address
        :type utxos_per_address: int

        :return list: The created UTXO set
        """
        utxos = []
        for n in range(utxos_per_address):
            for address in addresslist:
                try:
                    pkh = str(n).encode() + addr_to_pubkeyhash(address)[1:]
                except:
                    pkh = str(n).encode() + addr_bech32_to_pubkeyhash(
                        address)[1:]
                utxos.append({
                    'address': address,
                    'tx_hash': hashlib.sha256(pkh).hexdigest(),
                    'confirmations': 10,
                    'output_n': 0,
                    'index': 0,
                    'value': 1 * self.units,
                    'script': '',
                })
        return utxos
Esempio n. 4
0
 def test_valid_checksum(self):
     """Test checksum creation and validation."""
     for test in VALID_CHECKSUM:
         pos = test.rfind('1')
         test = test.lower()
         hrp = test[:pos]
         data = _codestring_to_array(test[pos + 1:], 'bech32')
         hrp_expanded = [ord(x) >> 5 for x in hrp] + [0] + [ord(x) & 31 for x in hrp]
         self.assertEqual(_bech32_polymod(hrp_expanded + data), 1, msg="Invalid checksum for address %s" % test)
         test = test[:pos+1] + chr(ord(test[pos + 1]) ^ 1) + test[pos+2:]
         try:
             self.assertFalse(addr_bech32_to_pubkeyhash(test, hrp))
         except EncodingError:
             continue
Esempio n. 5
0
def bech32_to_hash160(address):
    return change_base(addr_bech32_to_pubkeyhash(address), 256, 16)
Esempio n. 6
0
 def _get_tx_hash(self, address, n):
     try:
         pkh = str(n).encode() + addr_to_pubkeyhash(address)[1:]
     except Exception:
         pkh = str(n).encode() + addr_bech32_to_pubkeyhash(address)[1:]
     return hashlib.sha256(pkh).hexdigest()
Esempio n. 7
0
                '22ce2db091d6fd47294c9e2144fa0291949402e3003ce'
print("\n=== Convert DER encoded signature ===")
print(convert_der_sig(to_bytes(der_signature)))

print("\n=== Varbyte Int conversions ===")
print("Number 1000 as Varbyte Integer (hexstring): %s" % to_hexstring(int_to_varbyteint(1000)))
print("Converted back (3 is size in bytes: 1 size byte + integer in bytes): ", varbyteint_to_int(to_bytes('fde803')))

# Normalize data
print("\n=== Normalizations ===")
data = [
    u"guion cruz envío papel otoño percha hazaña salir joya gorra íntimo actriz",
    u'\u2167',
    u'\uFDFA',
    "あじわう ちしき たわむれる おくさま しゃそう うんこう ひてい みほん たいほ てのひら りこう わかれる かいすいよく こもん ねもと",
    '12345',
]

for dt in data:
    print("\nInput data", dt)
    print("Normalized unicode string (normalize_string): ", normalize_string(dt))
    print("Normalized variable (normalize_var): ", normalize_var(dt))


# Convert Bech32 address to Public key hash
address = "BC1QW508D6QEJXTDG4Y5R3ZARVARY0C5XW7KV8F3T4"
pkh = "0014751e76e8199196d454941c45d1b3a323f1433bd6"
pkh_converted = addr_bech32_to_pubkeyhash(address, prefix='bc', include_witver=True, as_hex=True)
print(pkh, " == ", pkh_converted)
addr = pubkeyhash_to_addr_bech32(pkh_converted, address[:2].lower())