mm-btc 0.1.1__tar.gz → 0.1.3__tar.gz

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.
Files changed (29) hide show
  1. mm_btc-0.1.3/PKG-INFO +10 -0
  2. {mm_btc-0.1.1 → mm_btc-0.1.3}/justfile +2 -2
  3. {mm_btc-0.1.1 → mm_btc-0.1.3}/pyproject.toml +14 -18
  4. {mm_btc-0.1.1 → mm_btc-0.1.3}/src/mm_btc/cli/cli.py +3 -0
  5. {mm_btc-0.1.1 → mm_btc-0.1.3}/src/mm_btc/cli/cmd/mnemonic_cmd.py +3 -2
  6. mm_btc-0.1.3/src/mm_btc/wallet.py +85 -0
  7. {mm_btc-0.1.1 → mm_btc-0.1.3}/tests/test_wallet.py +5 -4
  8. mm_btc-0.1.3/uv.lock +1230 -0
  9. mm_btc-0.1.1/PKG-INFO +0 -10
  10. mm_btc-0.1.1/src/mm_btc/wallet.py +0 -66
  11. mm_btc-0.1.1/uv.lock +0 -1239
  12. {mm_btc-0.1.1 → mm_btc-0.1.3}/.env.example +0 -0
  13. {mm_btc-0.1.1 → mm_btc-0.1.3}/.gitignore +0 -0
  14. {mm_btc-0.1.1 → mm_btc-0.1.3}/README.txt +0 -0
  15. {mm_btc-0.1.1 → mm_btc-0.1.3}/src/mm_btc/__init__.py +0 -0
  16. {mm_btc-0.1.1 → mm_btc-0.1.3}/src/mm_btc/blockstream.py +0 -0
  17. {mm_btc-0.1.1 → mm_btc-0.1.3}/src/mm_btc/cli/__init__.py +0 -0
  18. {mm_btc-0.1.1 → mm_btc-0.1.3}/src/mm_btc/cli/cli_utils.py +0 -0
  19. {mm_btc-0.1.1 → mm_btc-0.1.3}/src/mm_btc/cli/cmd/__init__.py +0 -0
  20. {mm_btc-0.1.1 → mm_btc-0.1.3}/src/mm_btc/cli/cmd/address_cmd.py +0 -0
  21. {mm_btc-0.1.1 → mm_btc-0.1.3}/src/mm_btc/cli/cmd/create_tx_cmd.py +0 -0
  22. {mm_btc-0.1.1 → mm_btc-0.1.3}/src/mm_btc/cli/cmd/decode_tx_cmd.py +0 -0
  23. {mm_btc-0.1.1 → mm_btc-0.1.3}/src/mm_btc/cli/cmd/utxo_cmd.py +0 -0
  24. {mm_btc-0.1.1 → mm_btc-0.1.3}/src/mm_btc/py.typed +0 -0
  25. {mm_btc-0.1.1 → mm_btc-0.1.3}/src/mm_btc/tx.py +0 -0
  26. {mm_btc-0.1.1 → mm_btc-0.1.3}/tests/__init__.py +0 -0
  27. {mm_btc-0.1.1 → mm_btc-0.1.3}/tests/cli/cmd/test_mnemonic_cmd.py +0 -0
  28. {mm_btc-0.1.1 → mm_btc-0.1.3}/tests/conftest.py +0 -0
  29. {mm_btc-0.1.1 → mm_btc-0.1.3}/tests/test_blockstream.py +0 -0
mm_btc-0.1.3/PKG-INFO ADDED
@@ -0,0 +1,10 @@
1
+ Metadata-Version: 2.4
2
+ Name: mm-btc
3
+ Version: 0.1.3
4
+ Requires-Python: >=3.12
5
+ Requires-Dist: bitcoinlib~=0.7.1
6
+ Requires-Dist: bit~=0.8.0
7
+ Requires-Dist: hdwallet~=3.2.0
8
+ Requires-Dist: mm-std~=0.1.9
9
+ Requires-Dist: mnemonic>=0.21
10
+ Requires-Dist: typer>=0.15.1
@@ -6,14 +6,14 @@ clean:
6
6
  rm -rf .pytest_cache .mypy_cache .ruff_cache .coverage dist build src/*.egg-info
7
7
 
8
8
  build: clean lint audit test
9
- uvx --from build pyproject-build --installer uv
9
+ uv build
10
10
 
11
11
  format:
12
12
  uv run ruff check --select I --fix src tests
13
13
  uv run ruff format src tests
14
14
 
15
15
  test:
16
- uv run coverage run -m pytest -n auto tests
16
+ uv run pytest -n auto tests
17
17
 
18
18
  lint: format
19
19
  uv run ruff check src tests
@@ -1,17 +1,16 @@
1
1
  [project]
2
2
  name = "mm-btc"
3
- version = "0.1.1"
3
+ version = "0.1.3"
4
4
  description = ""
5
5
  requires-python = ">=3.12"
6
6
  dependencies = [
7
- "mm-std~=0.1.2",
8
- "hdwallet~=2.2.1",
7
+ "mm-std~=0.1.9",
8
+ "hdwallet~=3.2.0",
9
9
  "bit~=0.8.0",
10
- "bitcoinlib~=0.6.15",
11
-
10
+ "bitcoinlib~=0.7.1",
11
+ "typer>=0.15.1",
12
+ "mnemonic>=0.21",
12
13
  ]
13
- [project.optional-dependencies]
14
- cli = ["typer~=0.12.5"]
15
14
  [project.scripts]
16
15
  mm-btc = "mm_btc.cli.cli:app"
17
16
 
@@ -21,20 +20,17 @@ build-backend = "hatchling.build"
21
20
 
22
21
  [tool.uv]
23
22
  dev-dependencies = [
24
- "pytest~=8.3.3",
23
+ "pytest~=8.3.4",
25
24
  "pytest-xdist~=3.6.1",
26
- "pytest-httpserver~=1.1.0",
27
- "coverage~=7.6.0",
28
- "ruff~=0.6.4",
29
- "pip-audit~=2.7.0",
30
- "bandit~=1.7.7",
31
- "mypy~=1.11.2",
32
- "types-python-dateutil~=2.9.0.20240906",
33
- "types-PyYAML~=6.0.12.12",
25
+ "ruff~=0.8.4",
26
+ "pip-audit~=2.7.3",
27
+ "bandit~=1.8.0",
28
+ "mypy~=1.14.0",
29
+ "types-PyYAML~=6.0.12.20241221",
34
30
  ]
35
31
 
36
32
  [tool.mypy]
37
- python_version = "3.12"
33
+ python_version = "3.13"
38
34
  warn_no_return = false
39
35
  strict = true
40
36
  exclude = ["^tests/", "^tmp/"]
@@ -44,7 +40,7 @@ ignore_missing_imports = true
44
40
 
45
41
  [tool.ruff]
46
42
  line-length = 130
47
- target-version = "py312"
43
+ target-version = "py313"
48
44
  lint.select = [
49
45
  "F", # Pyflakes
50
46
  "E", "W", # pycodestyle
@@ -7,6 +7,7 @@ from mm_std import print_plain
7
7
 
8
8
  from mm_btc.cli import cli_utils
9
9
  from mm_btc.cli.cmd import address_cmd, create_tx_cmd, decode_tx_cmd, mnemonic_cmd, utxo_cmd
10
+ from mm_btc.wallet import AddressType
10
11
 
11
12
  app = typer.Typer(no_args_is_help=True, pretty_exceptions_enable=False, add_completion=False)
12
13
 
@@ -17,6 +18,7 @@ def mnemonic_command( # nosec B107:hardcoded_password_default
17
18
  mnemonic: Annotated[str, typer.Option("--mnemonic", "-m", help="")] = "",
18
19
  passphrase: Annotated[str, typer.Option("--passphrase", "-p")] = "",
19
20
  path: Annotated[str, typer.Option("--path", help="Derivation path. Examples: bip44, bip88, m/44'/0'/0'/0")] = "bip44",
21
+ address_type: Annotated[AddressType, typer.Option("--address-type", "-a", help="Bitcoin address type")] = AddressType.P2WPKH,
20
22
  hex_: Annotated[bool, typer.Option("--hex", help="Print private key in hex format instead of WIF")] = False,
21
23
  words: int = typer.Option(12, "--words", "-w", help="Number of mnemonic words"),
22
24
  limit: int = typer.Option(10, "--limit", "-l"),
@@ -30,6 +32,7 @@ def mnemonic_command( # nosec B107:hardcoded_password_default
30
32
  words=words,
31
33
  limit=limit,
32
34
  path=path,
35
+ address_type=address_type,
33
36
  hex=hex_,
34
37
  testnet=testnet,
35
38
  )
@@ -3,7 +3,7 @@ from enum import Enum
3
3
 
4
4
  from mm_std import print_plain
5
5
 
6
- from mm_btc.wallet import derive_accounts, generate_mnemonic
6
+ from mm_btc.wallet import AddressType, derive_accounts, generate_mnemonic
7
7
 
8
8
 
9
9
  class PrivateType(str, Enum):
@@ -18,6 +18,7 @@ class Args:
18
18
  words: int
19
19
  limit: int
20
20
  hex: bool # Print private key in hex format instead of WIF
21
+ address_type: AddressType
21
22
  path: str
22
23
  testnet: bool
23
24
 
@@ -26,7 +27,7 @@ def run(args: Args) -> None:
26
27
  mnemonic = args.mnemonic or generate_mnemonic()
27
28
  passphrase = args.passphrase
28
29
  path = get_derivation_path_prefix(args.path, args.testnet)
29
- accounts = derive_accounts(mnemonic, passphrase, path, args.limit)
30
+ accounts = derive_accounts(mnemonic, passphrase, path, args.address_type, args.limit)
30
31
 
31
32
  print_plain(f"{mnemonic}")
32
33
  if passphrase:
@@ -0,0 +1,85 @@
1
+ from dataclasses import dataclass
2
+ from enum import Enum, unique
3
+
4
+ from hdwallet import HDWallet
5
+ from hdwallet.cryptocurrencies import Bitcoin
6
+ from hdwallet.derivations import CustomDerivation
7
+ from hdwallet.hds import BIP32HD
8
+ from hdwallet.mnemonics import BIP39Mnemonic
9
+ from mnemonic import Mnemonic
10
+
11
+ BIP44_MAINNET_PATH = "m/44'/0'/0'/0"
12
+ BIP44_TESTNET_PATH = "m/44'/1'/0'/0"
13
+ BIP84_MAINNET_PATH = "m/84'/0'/0'/0"
14
+ BIP84_TESTNET_PATH = "m/84'/1'/0'/0"
15
+
16
+
17
+ @dataclass
18
+ class Account:
19
+ address: str
20
+ private: str
21
+ wif: str
22
+ path: str
23
+
24
+
25
+ @unique
26
+ class AddressType(str, Enum):
27
+ P2PKH = "P2PKH" # Pay to Public Key Hash
28
+ P2SH = "P2SH" # Pay to Script Hash
29
+ P2TR = "P2TR" # Taproot
30
+ P2WPKH = "P2WPKH" # Native SegWit
31
+ P2WPKH_IN_P2SH = "P2WPKH-In-P2SH"
32
+ P2WSH = "P2WSH" # Native SegWit
33
+ P2WSH_IN_P2SH = "P2WSH-In-P2SH"
34
+
35
+
36
+ def generate_mnemonic(language: str = "english", words: int = 12) -> str:
37
+ return Mnemonic(language).generate(strength=mnemonic_words_to_strenght(words))
38
+
39
+
40
+ def derive_accounts(mnemonic: str, passphrase: str, path_prefix: str, address_type: AddressType, limit: int) -> list[Account]:
41
+ coin = Bitcoin
42
+ if path_prefix.startswith("m/84'/1'"):
43
+ network = coin.NETWORKS.TESTNET
44
+ elif path_prefix.startswith("m/44'/1'"):
45
+ network = coin.NETWORKS.TESTNET
46
+ elif path_prefix.startswith("m/84'/0'"):
47
+ network = coin.NETWORKS.MAINNET
48
+ elif path_prefix.startswith("m/44'/0'"):
49
+ network = coin.NETWORKS.MAINNET
50
+ else:
51
+ raise ValueError("Invalid path")
52
+
53
+ wallet = HDWallet(cryptocurrency=coin, network=network, hd=BIP32HD, passphrase=passphrase).from_mnemonic(
54
+ BIP39Mnemonic(mnemonic)
55
+ )
56
+ wallet.clean_derivation()
57
+
58
+ accounts = []
59
+ for index_path in range(limit):
60
+ wallet.clean_derivation()
61
+ path = f"{path_prefix}/{index_path}"
62
+ w = wallet.from_derivation(derivation=CustomDerivation(path=path))
63
+ accounts.append(Account(address=w.address(address_type), private=w.private_key(), wif=w.wif(), path=path))
64
+ w.clean_derivation()
65
+
66
+ return accounts
67
+
68
+
69
+ def mnemonic_words_to_strenght(words: int) -> int:
70
+ if words == 12:
71
+ return 128
72
+ if words == 15:
73
+ return 160
74
+ if words == 18:
75
+ return 192
76
+ if words == 21:
77
+ return 224
78
+ if words == 24:
79
+ return 256
80
+
81
+ raise ValueError("Invalid words")
82
+
83
+
84
+ def is_testnet_address(address: str) -> bool:
85
+ return address.startswith(("m", "n", "tb1"))
@@ -3,6 +3,7 @@ from mm_btc.wallet import (
3
3
  BIP44_TESTNET_PATH,
4
4
  BIP84_MAINNET_PATH,
5
5
  BIP84_TESTNET_PATH,
6
+ AddressType,
6
7
  derive_accounts,
7
8
  generate_mnemonic,
8
9
  )
@@ -17,28 +18,28 @@ def test_generate_mnemonic():
17
18
 
18
19
  def test_derive_accounts(mnemonic, passphrase):
19
20
  # mainnet bip44
20
- accs = derive_accounts(mnemonic, passphrase, BIP44_MAINNET_PATH, 2)
21
+ accs = derive_accounts(mnemonic, passphrase, BIP44_MAINNET_PATH, AddressType.P2PKH, 2)
21
22
  assert accs[1].path == "m/44'/0'/0'/0/1"
22
23
  assert accs[1].address == "13MJrGQq8RwLcGfdnY58666kCsZqevsadE"
23
24
  assert accs[1].private == "0d5feb50af849f2ebcc0c83a4446ddbceefd7b0ceba437a57161fa37702d096e"
24
25
  assert accs[1].wif == "Kwfi75WxsuaKnmxRUQDoEZDzKXkMC8E9Pzk3vVjEdPsJu1DXCFPv"
25
26
 
26
27
  # mainnet bip84
27
- accs = derive_accounts(mnemonic, passphrase, BIP84_MAINNET_PATH, 2)
28
+ accs = derive_accounts(mnemonic, passphrase, BIP84_MAINNET_PATH, AddressType.P2WPKH, 2)
28
29
  assert accs[1].path == "m/84'/0'/0'/0/1"
29
30
  assert accs[1].address == "bc1qszqeg6gfcmt8zurkeayes2895urnea2m54rt3w"
30
31
  assert accs[1].private == "1cc57553f9e3e1b863693be92ac753f682f61f1a2dede2ceff97262027635334"
31
32
  assert accs[1].wif == "KxBdzQTQduYKoec1FWgwMDF9R1a4UdQp3ezyhjniw4W2k54Cb6gE"
32
33
 
33
34
  # testnet bip44
34
- accs = derive_accounts(mnemonic, passphrase, BIP44_TESTNET_PATH, 2)
35
+ accs = derive_accounts(mnemonic, passphrase, BIP44_TESTNET_PATH, AddressType.P2PKH, 2)
35
36
  assert accs[1].path == "m/44'/1'/0'/0/1"
36
37
  assert accs[1].address == "miRfXh2aH6pjhdo5EoBt4RRdAipR88dcV3"
37
38
  assert accs[1].private == "3a2e8fc45c75ab8a7cdafacf7c77c941f8eebc813ee2f0e17ee86e7ac8584b3a"
38
39
  assert accs[1].wif == "cPXoLA4cRKxeht4iH6ZsCDQxPspFi8SEieZNTKDzGgog94Zg5Y64"
39
40
 
40
41
  # testnet bip84
41
- accs = derive_accounts(mnemonic, passphrase, BIP84_TESTNET_PATH, 2)
42
+ accs = derive_accounts(mnemonic, passphrase, BIP84_TESTNET_PATH, AddressType.P2WPKH, 2)
42
43
  assert accs[1].path == "m/84'/1'/0'/0/1"
43
44
  assert accs[1].address == "tb1qp36pk9w408pgfpwn5myxwurr3fhzryn0lpyah2"
44
45
  assert accs[1].private == "150fd20e4cb377ae57b43fed59ca2765a9f5d91a20640a4cfdf43ceb5403d48c"