def decode_transaction_raw(transaction_raw): """ Decode Bitcoin transaction raw. :param transaction_raw: Bitcoin transaction raw. :type transaction_raw: str :returns: dict -- decoded Bitcoin transaction. >>> from swap.providers.bitcoin.utils import decode_transaction_raw >>> transaction_raw = "eyJmZWUiOiA2NzgsICJyYXciOiAiMDIwMDAwMDAwMTg4OGJlN2VjMDY1MDk3ZDk1NjY0NzYzZjI3NmQ0MjU1NTJkNzM1ZmIxZDk3NGFlNzhiZjcyMTA2ZGNhMGYzOTEwMTAwMDAwMDAwZmZmZmZmZmYwMjEwMjcwMDAwMDAwMDAwMDAxN2E5MTQyYmIwMTNjM2U0YmViMDg0MjFkZWRjZjgxNWNiNjVhNWMzODgxNzhiODdiY2RkMGUwMDAwMDAwMDAwMTk3NmE5MTQ2NGE4MzkwYjBiMTY4NWZjYmYyZDRiNDU3MTE4ZGM4ZGE5MmQ1NTM0ODhhYzAwMDAwMDAwIiwgIm91dHB1dHMiOiBbeyJhbW91bnQiOiA5ODQ5NDYsICJuIjogMSwgInNjcmlwdCI6ICI3NmE5MTQ2NGE4MzkwYjBiMTY4NWZjYmYyZDRiNDU3MTE4ZGM4ZGE5MmQ1NTM0ODhhYyJ9XSwgIm5ldHdvcmsiOiAidGVzdG5ldCIsICJ0eXBlIjogImJpdGNvaW5fZnVuZF91bnNpZ25lZCJ9" >>> decode_transaction_raw(transaction_raw) {'fee': 678, 'type': 'bitcoin_fund_unsigned', 'tx': {'hex': '0200000001888be7ec065097d95664763f276d425552d735fb1d974ae78bf72106dca0f3910100000000ffffffff02102700000000000017a9142bb013c3e4beb08421dedcf815cb65a5c388178b87bcdd0e00000000001976a91464a8390b0b1685fcbf2d4b457118dc8da92d553488ac00000000', 'txid': 'abc70fd3466aec9478ea3115200a84f993204ad1f614fe08e92ecc5997a0d3ba', 'hash': 'abc70fd3466aec9478ea3115200a84f993204ad1f614fe08e92ecc5997a0d3ba', 'size': 117, 'vsize': 117, 'version': 2, 'locktime': 0, 'vin': [{'txid': '91f3a0dc0621f78be74a971dfb35d75255426d273f766456d9975006ece78b88', 'vout': 1, 'scriptSig': {'asm': '', 'hex': ''}, 'sequence': '4294967295'}], 'vout': [{'value': '0.00010000', 'n': 0, 'scriptPubKey': {'asm': 'OP_HASH160 2bb013c3e4beb08421dedcf815cb65a5c388178b OP_EQUAL', 'hex': 'a9142bb013c3e4beb08421dedcf815cb65a5c388178b87', 'type': 'p2sh', 'address': '2MwEDybGC34949zgzWX4M9FHmE3crDSUydP'}}, {'value': '0.00974268', 'n': 1, 'scriptPubKey': {'asm': 'OP_DUP OP_HASH160 64a8390b0b1685fcbf2d4b457118dc8da92d5534 OP_EQUALVERIFY OP_CHECKSIG', 'hex': '76a91464a8390b0b1685fcbf2d4b457118dc8da92d553488ac', 'type': 'p2pkh', 'address': 'mphBPZf15cRFcL5tUq6mCbE84XobZ1vg7Q'}}]}, 'network': 'testnet'} """ transaction_raw = str(transaction_raw + "=" * (-len(transaction_raw) % 4)) try: decoded_transaction_raw = json.loads( b64decode(str(transaction_raw).encode()).decode()) except (binascii.Error, json.decoder.JSONDecodeError) as _error: raise ValueError("invalid Bitcoin transaction raw") if "type" not in decoded_transaction_raw or not str( decoded_transaction_raw["type"]).startswith("bitcoin"): raise ValueError("invalid Bitcoin transaction raw") # Setting testnet stp(decoded_transaction_raw["network"], strict=True) tx = MutableTransaction.unhexlify(decoded_transaction_raw["raw"]) return dict(fee=decoded_transaction_raw["fee"], type=decoded_transaction_raw["type"], tx=tx.to_json(), network=decoded_transaction_raw["network"])
def decode_transaction_raw(transaction_raw): """ Decode Bitcoin transaction raw. :param transaction_raw: Bitcoin transaction raw. :type transaction_raw: str :returns: dict -- decoded Bitcoin transaction. >>> from shuttle.providers.bitcoin.utils import decode_transaction_raw >>> decode_transaction_raw(transaction_raw) {...} """ transaction_raw = str(transaction_raw + "=" * (-len(transaction_raw) % 4)) try: decoded_transaction_raw = json.loads(b64decode(str(transaction_raw).encode()).decode()) except (binascii.Error, json.decoder.JSONDecodeError) as _error: raise ValueError("invalid Bitcoin transaction raw") if "type" not in decoded_transaction_raw or not str(decoded_transaction_raw["type"]).startswith("bitcoin"): raise ValueError("invalid Bitcoin transaction raw") # Setting testnet stp(decoded_transaction_raw["network"], strict=True) tx = MutableTransaction.unhexlify(decoded_transaction_raw["raw"]) return dict( fee=decoded_transaction_raw["fee"], type=decoded_transaction_raw["type"], tx=tx.to_json(), network=decoded_transaction_raw["network"] )
def decode_transaction_raw(transaction_raw: str, offline: bool = True, headers: dict = config["headers"], timeout: int = config["timeout"]) -> dict: """ Decode Bitcoin transaction raw. :param transaction_raw: Bitcoin transaction raw. :type transaction_raw: str :param offline: Offline decode, defaults to True. :type offline: bool :param headers: Request headers, default to common headers. :type headers: dict :param timeout: Request timeout, default to 60. :type timeout: int :returns: dict -- Decoded Bitcoin transaction raw. >>> from swap.providers.bitcoin.utils import decode_transaction_raw >>> transaction_raw = "eyJmZWUiOiA2NzgsICJyYXciOiAiMDIwMDAwMDAwMTg4OGJlN2VjMDY1MDk3ZDk1NjY0NzYzZjI3NmQ0MjU1NTJkNzM1ZmIxZDk3NGFlNzhiZjcyMTA2ZGNhMGYzOTEwMTAwMDAwMDAwZmZmZmZmZmYwMjEwMjcwMDAwMDAwMDAwMDAxN2E5MTQyYmIwMTNjM2U0YmViMDg0MjFkZWRjZjgxNWNiNjVhNWMzODgxNzhiODdiY2RkMGUwMDAwMDAwMDAwMTk3NmE5MTQ2NGE4MzkwYjBiMTY4NWZjYmYyZDRiNDU3MTE4ZGM4ZGE5MmQ1NTM0ODhhYzAwMDAwMDAwIiwgIm91dHB1dHMiOiBbeyJhbW91bnQiOiA5ODQ5NDYsICJuIjogMSwgInNjcmlwdCI6ICI3NmE5MTQ2NGE4MzkwYjBiMTY4NWZjYmYyZDRiNDU3MTE4ZGM4ZGE5MmQ1NTM0ODhhYyJ9XSwgIm5ldHdvcmsiOiAidGVzdG5ldCIsICJ0eXBlIjogImJpdGNvaW5fZnVuZF91bnNpZ25lZCJ9" >>> decode_transaction_raw(transaction_raw=transaction_raw) {'fee': 678, 'type': 'bitcoin_fund_unsigned', 'tx': {'hex': '0200000001888be7ec065097d95664763f276d425552d735fb1d974ae78bf72106dca0f3910100000000ffffffff02102700000000000017a9142bb013c3e4beb08421dedcf815cb65a5c388178b87bcdd0e00000000001976a91464a8390b0b1685fcbf2d4b457118dc8da92d553488ac00000000', 'txid': 'abc70fd3466aec9478ea3115200a84f993204ad1f614fe08e92ecc5997a0d3ba', 'hash': 'abc70fd3466aec9478ea3115200a84f993204ad1f614fe08e92ecc5997a0d3ba', 'size': 117, 'vsize': 117, 'version': 2, 'locktime': 0, 'vin': [{'txid': '91f3a0dc0621f78be74a971dfb35d75255426d273f766456d9975006ece78b88', 'vout': 1, 'scriptSig': {'asm': '', 'hex': ''}, 'sequence': '4294967295'}], 'vout': [{'value': '0.00010000', 'n': 0, 'scriptPubKey': {'asm': 'OP_HASH160 2bb013c3e4beb08421dedcf815cb65a5c388178b OP_EQUAL', 'hex': 'a9142bb013c3e4beb08421dedcf815cb65a5c388178b87', 'type': 'p2sh', 'address': '2MwEDybGC34949zgzWX4M9FHmE3crDSUydP'}}, {'value': '0.00974268', 'n': 1, 'scriptPubKey': {'asm': 'OP_DUP OP_HASH160 64a8390b0b1685fcbf2d4b457118dc8da92d5534 OP_EQUALVERIFY OP_CHECKSIG', 'hex': '76a91464a8390b0b1685fcbf2d4b457118dc8da92d553488ac', 'type': 'p2pkh', 'address': 'mphBPZf15cRFcL5tUq6mCbE84XobZ1vg7Q'}}]}, 'network': 'testnet'} """ if not is_transaction_raw(transaction_raw=transaction_raw): raise TransactionRawError("Invalid Bitcoin transaction raw.") transaction_raw = clean_transaction_raw(transaction_raw) decoded_transaction_raw = b64decode(transaction_raw.encode()) loaded_transaction_raw = json.loads(decoded_transaction_raw.decode()) decoded_transaction: Optional[dict] = None if offline: stp(loaded_transaction_raw["network"], strict=True, force=True) tx = MutableTransaction.unhexlify(loaded_transaction_raw["raw"]) decoded_transaction = tx.to_json() else: url = f"{config[loaded_transaction_raw['network']]['blockcypher']['url']}/txs/decode" parameter = dict(token=config[loaded_transaction_raw["network"]] ["blockcypher"]["token"]) data = dict(tx=loaded_transaction_raw["raw"]) response = requests.post(url=url, data=json.dumps(data), params=parameter, headers=headers, timeout=timeout) decoded_transaction = response.json() return dict(fee=loaded_transaction_raw["fee"], type=loaded_transaction_raw["type"], tx=decoded_transaction, network=loaded_transaction_raw["network"])
def decode_raw(raw: str, network: str = config["network"], offline: bool = True, headers: dict = config["headers"], timeout: int = config["timeout"]) -> dict: """ Decode original Bitcoin raw. :param raw: Bitcoin transaction raw. :type raw: str :param network: Bitcoin network, defaults to mainnet. :type network: str :param offline: Offline decode, defaults to True. :type offline: bool :param headers: Request headers, default to common headers. :type headers: dict :param timeout: Request timeout, default to 60. :type timeout: int :returns: dict -- Bitcoin decoded transaction raw. >>> from swap.providers.bitcoin.rpc import decode_raw >>> decode_raw(raw="02000000011823f39a8c5f6f27845dd13a65e03fe2ef5108d235e7a36edb6eb267b0459c5a010000006a47304402207018b7fd1ba6624fe9bb0f16cd65fa243d202e32fdff452699f56465b61ab648022009f0dc1a0a63109246c45e120fc0d34b40e789dfc4d05e64f269602c7d67d9210121027f0dc0894bd690635412af782d05e4f79d3d40bf568978c650f3f1ca1a96cf36ffffffff02102700000000000017a9149418feed4647e156d6663db3e0cef7c050d038678734330100000000001976a91433ecab3d67f0e2bde43e52f41ec1ecbdc73f11f888ac00000000", network="testnet") {'hex': '02000000011823f39a8c5f6f27845dd13a65e03fe2ef5108d235e7a36edb6eb267b0459c5a010000006a47304402207018b7fd1ba6624fe9bb0f16cd65fa243d202e32fdff452699f56465b61ab648022009f0dc1a0a63109246c45e120fc0d34b40e789dfc4d05e64f269602c7d67d9210121027f0dc0894bd690635412af782d05e4f79d3d40bf568978c650f3f1ca1a96cf36ffffffff02102700000000000017a9149418feed4647e156d6663db3e0cef7c050d038678734330100000000001976a91433ecab3d67f0e2bde43e52f41ec1ecbdc73f11f888ac00000000', 'txid': '6e5c80f600f45acda3c3101128bb3075bf2cf7af4bab0d99c9d856ebfb4b0953', 'hash': '6e5c80f600f45acda3c3101128bb3075bf2cf7af4bab0d99c9d856ebfb4b0953', 'size': 223, 'vsize': 223, 'version': 2, 'locktime': 0, 'vin': [{'txid': '5a9c45b067b26edb6ea3e735d20851efe23fe0653ad15d84276f5f8c9af32318', 'vout': 1, 'scriptSig': {'asm': '304402207018b7fd1ba6624fe9bb0f16cd65fa243d202e32fdff452699f56465b61ab648022009f0dc1a0a63109246c45e120fc0d34b40e789dfc4d05e64f269602c7d67d92101 027f0dc0894bd690635412af782d05e4f79d3d40bf568978c650f3f1ca1a96cf36', 'hex': '47304402207018b7fd1ba6624fe9bb0f16cd65fa243d202e32fdff452699f56465b61ab648022009f0dc1a0a63109246c45e120fc0d34b40e789dfc4d05e64f269602c7d67d9210121027f0dc0894bd690635412af782d05e4f79d3d40bf568978c650f3f1ca1a96cf36'}, 'sequence': '4294967295'}], 'vout': [{'value': '0.00010000', 'n': 0, 'scriptPubKey': {'asm': 'OP_HASH160 9418feed4647e156d6663db3e0cef7c050d03867 OP_EQUAL', 'hex': 'a9149418feed4647e156d6663db3e0cef7c050d0386787', 'type': 'p2sh', 'address': '2N6kHwQy6Ph5EdKNgzGrcW2WhGHKGfmP5ae'}}, {'value': '0.00078644', 'n': 1, 'scriptPubKey': {'asm': 'OP_DUP OP_HASH160 33ecab3d67f0e2bde43e52f41ec1ecbdc73f11f8 OP_EQUALVERIFY OP_CHECKSIG', 'hex': '76a91433ecab3d67f0e2bde43e52f41ec1ecbdc73f11f888ac', 'type': 'p2pkh', 'address': 'mkFWGt4hT11XS8dJKzzRFsTrqjjAwZfQAC'}}]} """ if not is_network(network=network): raise NetworkError(f"Invalid Bitcoin '{network}' network", "choose only 'mainnet' or 'testnet' networks.") if offline: stp(network, strict=True, force=True) tx = MutableTransaction.unhexlify(raw) return tx.to_json() url = f"{config[network]['blockcypher']['url']}/txs/decode" parameter = dict(token=config[network]["blockcypher"]["token"]) data = dict(tx=raw) response = requests.post( url=url, data=json.dumps(data), params=parameter, headers=headers, timeout=timeout ) response_json = response.json() return response_json