mm-btc 0.0.5__py3-none-any.whl → 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
mm_btc/blockstream.py CHANGED
@@ -1,7 +1,7 @@
1
1
  from collections.abc import Sequence
2
2
  from typing import TypeAlias
3
3
 
4
- from mm_std import Err, Ok, Result, hr
4
+ from mm_std import Err, HResponse, Ok, Result, hr
5
5
  from mm_std.random_ import random_str_choice
6
6
  from pydantic import BaseModel
7
7
 
@@ -14,6 +14,13 @@ ERROR_INVALID_NETWORK = "INVALID_NETWORK"
14
14
  Proxy: TypeAlias = str | Sequence[str] | None
15
15
 
16
16
 
17
+ class Mempool(BaseModel):
18
+ count: int
19
+ vsize: int
20
+ total_fee: int
21
+ fee_histogram: list[tuple[float, int]]
22
+
23
+
17
24
  class Address(BaseModel):
18
25
  class ChainStats(BaseModel):
19
26
  funded_txo_count: int
@@ -33,31 +40,71 @@ class Address(BaseModel):
33
40
  mempool_stats: MempoolStats
34
41
 
35
42
 
36
- def get_address(
37
- address: str, testnet: bool = False, timeout: int = 10, proxies: Proxy = None, attempts: int = 1
38
- ) -> Result[Address]:
39
- result: Result[Address] = Err("not started yet")
40
- data = None
41
- base_url = TESTNET_BASE_URL if testnet else MAINNET_BASE_URL
42
- for _ in range(attempts):
43
- try:
44
- res = hr(f"{base_url}/address/{address}", timeout=timeout, proxy=random_str_choice(proxies))
45
- data = res.to_dict()
46
- if res.code == 400 and (
47
- "invalid bitcoin address" in res.body.lower() or "bech32 segwit decoding error" in res.body.lower()
48
- ):
49
- return Err(ERROR_INVALID_ADDRESS, data=data)
50
- elif res.code == 400 and "invalid network" in res.body.lower():
51
- return Err(ERROR_INVALID_NETWORK, data=data)
52
- return Ok(Address(**res.json))
53
- except Exception as err:
54
- result = Err(err, data=data)
55
- return result
56
-
57
-
58
- def get_confirmed_balance(
59
- address: str, testnet: bool = False, timeout: int = 10, proxies: Proxy = None, attempts: int = 1
60
- ) -> Result[int]:
61
- return get_address(address, testnet=testnet, timeout=timeout, proxies=proxies, attempts=attempts).and_then(
62
- lambda a: Ok(a.chain_stats.funded_txo_sum - a.chain_stats.spent_txo_sum),
63
- )
43
+ class Utxo(BaseModel):
44
+ class Status(BaseModel):
45
+ confirmed: bool
46
+ block_height: int
47
+ block_hash: str
48
+ block_time: int
49
+
50
+ txid: str
51
+ vout: int
52
+ status: Status
53
+ value: int
54
+
55
+
56
+ class BlockstreamClient:
57
+ def __init__(self, testnet: bool = False, timeout: int = 10, proxies: Proxy = None, attempts: int = 1):
58
+ self.testnet = testnet
59
+ self.timeout = timeout
60
+ self.proxies = proxies
61
+ self.attempts = attempts
62
+ self.base_url = TESTNET_BASE_URL if testnet else MAINNET_BASE_URL
63
+
64
+ def get_address(self, address: str) -> Result[Address]:
65
+ result: Result[Address] = Err("not started yet")
66
+ data = None
67
+ for _ in range(self.attempts):
68
+ try:
69
+ res = self._request(f"/address/{address}")
70
+ data = res.to_dict()
71
+ if res.code == 400 and (
72
+ "invalid bitcoin address" in res.body.lower() or "bech32 segwit decoding error" in res.body.lower()
73
+ ):
74
+ return Err(ERROR_INVALID_ADDRESS, data=data)
75
+ elif res.code == 400 and "invalid network" in res.body.lower():
76
+ return Err(ERROR_INVALID_NETWORK, data=data)
77
+ return Ok(Address(**res.json), data=data)
78
+ except Exception as err:
79
+ result = Err(err, data=data)
80
+ return result
81
+
82
+ def get_confirmed_balance(self, address: str) -> Result[int]:
83
+ return self.get_address(address).and_then(lambda a: Ok(a.chain_stats.funded_txo_sum - a.chain_stats.spent_txo_sum))
84
+
85
+ def get_utxo(self, address: str) -> Result[list[Utxo]]:
86
+ result: Result[list[Utxo]] = Err("not started yet")
87
+ data = None
88
+ for _ in range(self.attempts):
89
+ try:
90
+ res = self._request(f"/address/{address}/utxo")
91
+ data = res.to_dict()
92
+ return Ok([Utxo(**out) for out in res.json], data=data)
93
+ except Exception as err:
94
+ result = Err(err, data=data)
95
+ return result
96
+
97
+ def get_mempool(self) -> Result[Mempool]:
98
+ result: Result[Mempool] = Err("not started yet")
99
+ data = None
100
+ for _ in range(self.attempts):
101
+ try:
102
+ res = self._request("/mempool")
103
+ data = res.to_dict()
104
+ return Ok(Mempool(**res.json), data=data)
105
+ except Exception as err:
106
+ result = Err(err, data=data)
107
+ return result
108
+
109
+ def _request(self, url: str) -> HResponse:
110
+ return hr(f"{self.base_url}{url}", timeout=self.timeout, proxy=random_str_choice(self.proxies))
mm_btc/cli/cli.py CHANGED
@@ -1,3 +1,4 @@
1
+ from pathlib import Path
1
2
  from typing import Annotated
2
3
 
3
4
  import typer
@@ -5,7 +6,7 @@ import typer.core
5
6
  from mm_std import print_plain
6
7
 
7
8
  from mm_btc.cli import cli_utils
8
- from mm_btc.cli.cmd import address_cmd, mnemonic_cmd
9
+ from mm_btc.cli.cmd import address_cmd, create_tx_cmd, decode_tx_cmd, mnemonic_cmd, utxo_cmd
9
10
 
10
11
  app = typer.Typer(no_args_is_help=True, pretty_exceptions_enable=False, add_completion=False)
11
12
 
@@ -42,6 +43,24 @@ def address_command(address: str) -> None:
42
43
  address_cmd.run(address)
43
44
 
44
45
 
46
+ @app.command("create-tx")
47
+ def create_tx_command(config_path: Annotated[Path, typer.Argument(exists=True)]) -> None:
48
+ """Create a transaction"""
49
+ create_tx_cmd.run(config_path)
50
+
51
+
52
+ @app.command("decode-tx")
53
+ def decode_tx_command(tx_hex: str, testnet: Annotated[bool, typer.Option("--testnet", "-t")] = False) -> None:
54
+ """Decode a transaction"""
55
+ decode_tx_cmd.run(tx_hex, testnet)
56
+
57
+
58
+ @app.command("utxo")
59
+ def utxo_command(address: str) -> None:
60
+ """Get UTXOs from Blockstream API"""
61
+ utxo_cmd.run(address)
62
+
63
+
45
64
  def version_callback(value: bool) -> None:
46
65
  if value:
47
66
  print_plain(f"mm-btc: v{cli_utils.get_version()}")
@@ -1,10 +1,10 @@
1
1
  from mm_std import print_json
2
2
 
3
- from mm_btc import blockstream
3
+ from mm_btc.blockstream import BlockstreamClient
4
4
  from mm_btc.wallet import is_testnet_address
5
5
 
6
6
 
7
7
  def run(address: str) -> None:
8
- testnet = is_testnet_address(address)
9
- res = blockstream.get_address(address, testnet=testnet)
8
+ client = BlockstreamClient(testnet=is_testnet_address(address))
9
+ res = client.get_address(address)
10
10
  print_json(res)
@@ -0,0 +1,27 @@
1
+ from pathlib import Path
2
+
3
+ from bit import PrivateKey, PrivateKeyTestnet
4
+ from mm_std import BaseConfig, print_console
5
+
6
+ from mm_btc.wallet import is_testnet_address
7
+
8
+
9
+ class Config(BaseConfig):
10
+ class Output(BaseConfig):
11
+ address: str
12
+ amount: int
13
+
14
+ from_address: str
15
+ private: str
16
+ outputs: list[Output]
17
+
18
+
19
+ def run(config_path: Path) -> None:
20
+ config = Config.read_config(config_path)
21
+ testnet = is_testnet_address(config.from_address)
22
+ key = PrivateKeyTestnet(config.private) if testnet else PrivateKey(config.private)
23
+
24
+ outputs = [(o.address, o.amount, "satoshi") for o in config.outputs]
25
+
26
+ tx = key.create_transaction(outputs)
27
+ print_console(tx)
@@ -0,0 +1,8 @@
1
+ from mm_std import print_json
2
+
3
+ from mm_btc.tx import decode_tx
4
+
5
+
6
+ def run(tx_hex: str, testnet: bool = False) -> None:
7
+ res = decode_tx(tx_hex, testnet)
8
+ print_json(res)
@@ -0,0 +1,11 @@
1
+ from mm_std import print_json
2
+
3
+ from mm_btc.blockstream import BlockstreamClient
4
+ from mm_btc.wallet import is_testnet_address
5
+
6
+
7
+ def run(address: str) -> None:
8
+ client = BlockstreamClient(testnet=is_testnet_address(address))
9
+ res = client.get_utxo(address)
10
+
11
+ print_json(res.ok_or_err())
mm_btc/tx.py ADDED
@@ -0,0 +1,5 @@
1
+ from bitcoinlib.transactions import Transaction
2
+
3
+
4
+ def decode_tx(tx_hex: str, testnet: bool = False) -> dict[str, object]:
5
+ return Transaction.parse(tx_hex, network="testnet" if testnet else "mainnet").as_dict() # type: ignore[no-any-return]
@@ -0,0 +1,9 @@
1
+ Metadata-Version: 2.3
2
+ Name: mm-btc
3
+ Version: 0.1.0
4
+ Requires-Python: >=3.12
5
+ Requires-Dist: bitcoinlib~=0.6.15
6
+ Requires-Dist: bit~=0.8.0
7
+ Requires-Dist: hdwallet~=2.2.1
8
+ Requires-Dist: mm-std~=0.1.2
9
+ Requires-Dist: typer~=0.12.3
@@ -0,0 +1,18 @@
1
+ mm_btc/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
+ mm_btc/blockstream.py,sha256=U2_pc6RYAkFcCvH_WrxjIm13VNavyY3WgK8ePowV-Ic,3620
3
+ mm_btc/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
+ mm_btc/tx.py,sha256=7KTMNuL1n8rRj_245BV1bH9M2_PctNvlvPsLEsRTYx4,245
5
+ mm_btc/wallet.py,sha256=0ejOIl5gbm1oMO4WY3AsQRiF3PCrB4WC2LknaYPmMfY,1838
6
+ mm_btc/cli/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
7
+ mm_btc/cli/cli.py,sha256=D_yQ_L7oBY9aJ3_TKeJOQmXFsx8yK3lmARTAsmMXjO8,2413
8
+ mm_btc/cli/cli_utils.py,sha256=g2o5ySLu-Tw8dZm84ZAsx8862yHij4sDfz4z6uLS2hw,102
9
+ mm_btc/cli/cmd/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
10
+ mm_btc/cli/cmd/address_cmd.py,sha256=fipX9FiQ2EgCEq8nVN6ELnDMoR5KTIX7bpAaRK3t_yg,284
11
+ mm_btc/cli/cmd/create_tx_cmd.py,sha256=dIuQBTOqTSefC6aMbrBN6Pq0EwV2G2ESM1tLU4WyFoU,690
12
+ mm_btc/cli/cmd/decode_tx_cmd.py,sha256=0jGlUjnA1X2Q2aYwSBeAjqVNZIBsW5X2lEwIpSfWJsU,175
13
+ mm_btc/cli/cmd/mnemonic_cmd.py,sha256=zAAHr9j0SLJX5BXoiMTZj8pIzTPX26aJEOUi7IN40Ug,1192
14
+ mm_btc/cli/cmd/utxo_cmd.py,sha256=mp-lVLURpXFqO3IeBIEwnHusEvT5tFjUKxnRYC-rFqQ,294
15
+ mm_btc-0.1.0.dist-info/METADATA,sha256=iCt1hMCcgWPzk7ush5pSB3QxfSfrhlind16xbDUSC1M,223
16
+ mm_btc-0.1.0.dist-info/WHEEL,sha256=1yFddiXMmvYK7QYTqtRNtX66WJ0Mz8PYEiEUoOUUxRY,87
17
+ mm_btc-0.1.0.dist-info/entry_points.txt,sha256=KphzLNE9eb9H1DO-L0gvBQaQT5ImedyVCN4a6svfiRg,46
18
+ mm_btc-0.1.0.dist-info/RECORD,,
@@ -1,5 +1,4 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (72.1.0)
2
+ Generator: hatchling 1.25.0
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
-
@@ -1,22 +0,0 @@
1
- Metadata-Version: 2.1
2
- Name: mm-btc
3
- Version: 0.0.5
4
- Requires-Python: >=3.12
5
- Requires-Dist: mm-std ~=0.1.0
6
- Requires-Dist: hdwallet ~=2.2.1
7
- Requires-Dist: typer ~=0.12.3
8
- Provides-Extra: dev
9
- Requires-Dist: build ~=1.2.1 ; extra == 'dev'
10
- Requires-Dist: twine ~=5.1.0 ; extra == 'dev'
11
- Requires-Dist: pytest ~=8.3.2 ; extra == 'dev'
12
- Requires-Dist: pytest-xdist ~=3.6.1 ; extra == 'dev'
13
- Requires-Dist: pytest-httpserver ~=1.0.8 ; extra == 'dev'
14
- Requires-Dist: coverage ~=7.6.0 ; extra == 'dev'
15
- Requires-Dist: ruff ~=0.5.2 ; extra == 'dev'
16
- Requires-Dist: pip-audit ~=2.7.0 ; extra == 'dev'
17
- Requires-Dist: bandit ~=1.7.7 ; extra == 'dev'
18
- Requires-Dist: mypy ~=1.11.0 ; extra == 'dev'
19
- Requires-Dist: types-python-dateutil ~=2.9.0 ; extra == 'dev'
20
- Requires-Dist: types-requests ~=2.32.0.20240523 ; extra == 'dev'
21
- Requires-Dist: types-PyYAML ~=6.0.12.12 ; extra == 'dev'
22
-
@@ -1,15 +0,0 @@
1
- mm_btc/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
- mm_btc/blockstream.py,sha256=HllRQBO1Ezl0ArVH8b-CPRi0f5zdRNXJT_K3oCpAtgg,2159
3
- mm_btc/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
- mm_btc/wallet.py,sha256=0ejOIl5gbm1oMO4WY3AsQRiF3PCrB4WC2LknaYPmMfY,1838
5
- mm_btc/cli/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
6
- mm_btc/cli/cli.py,sha256=5UWUzlJvaV8qq2hZ_XptkaLPf81EbwlYFNcT-_xmQ_U,1825
7
- mm_btc/cli/cli_utils.py,sha256=g2o5ySLu-Tw8dZm84ZAsx8862yHij4sDfz4z6uLS2hw,102
8
- mm_btc/cli/cmd/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
9
- mm_btc/cli/cmd/address_cmd.py,sha256=vOjPgK9D91w2YWyCvbRO1_dWfuRDxpjNzOUpJYvYEVE,262
10
- mm_btc/cli/cmd/mnemonic_cmd.py,sha256=zAAHr9j0SLJX5BXoiMTZj8pIzTPX26aJEOUi7IN40Ug,1192
11
- mm_btc-0.0.5.dist-info/METADATA,sha256=28ask6JTpLCaDXE_gNWlatFVY88mgZbovq3CvdMvg84,858
12
- mm_btc-0.0.5.dist-info/WHEEL,sha256=R0nc6qTxuoLk7ShA2_Y-UWkN8ZdfDBG2B6Eqpz2WXbs,91
13
- mm_btc-0.0.5.dist-info/entry_points.txt,sha256=KphzLNE9eb9H1DO-L0gvBQaQT5ImedyVCN4a6svfiRg,46
14
- mm_btc-0.0.5.dist-info/top_level.txt,sha256=0X0Ppv_QY0ulzgCB5HNezpaHfQ4wvih_B72hn5hHTUE,7
15
- mm_btc-0.0.5.dist-info/RECORD,,
@@ -1 +0,0 @@
1
- mm_btc