Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
fametrano committed Jan 2, 2023
1 parent e4678a4 commit bff9191
Show file tree
Hide file tree
Showing 22 changed files with 152 additions and 46 deletions.
12 changes: 6 additions & 6 deletions btclib/alias.py
Expand Up @@ -35,7 +35,7 @@
# dsa.Sig (DER serialization of ECDSA signature),
# ssa.Sig (BIP340 serialization of Schnorr signature)
# etc.
Octets = Union[bytes, str]
Octets: TypeAlias = Union[bytes, str]

# bytes or text string (not hex-string)
#
Expand All @@ -61,18 +61,18 @@
#
# In those cases often there is no need to encode() to bytes
# as b58decode/b32decode/etc. will take care of that
String = Union[bytes, str]
String: TypeAlias = Union[bytes, str]

# binary data, usually to be cosumed as byte stream,
# but possibily provided as Octets too
BinaryData = Union[BytesIO, Octets]
BinaryData: TypeAlias = Union[BytesIO, Octets]

# hex-string or bytes representation of an int
# Integer = Union[Octets, int]
Integer = Union[bytes, str, int]
Integer: TypeAlias = Union[bytes, str, int]

# Hash digest constructor: it may be any name suitable to hashlib.new()
HashF = Callable[[], Any]
HashF: TypeAlias = Callable[[], Any]
# HashF = Callable[[Any], Any]

# Elliptic curve point in affine coordinates.
Expand All @@ -88,7 +88,7 @@
INF = 5, 0

# Elliptic curve point in Jacobian coordinates.
JacPoint = Tuple[int, int, int]
JacPoint: TypeAlias = Tuple[int, int, int]

# Infinity point in Jacobian coordinates is INF = (int, int, 0).
# It can be checked with 'INF[2] == 0'
Expand Down
23 changes: 22 additions & 1 deletion btclib/bip32/__init__.py
Expand Up @@ -8,7 +8,7 @@
# No part of btclib including this file, may be copied, modified, propagated,
# or distributed except according to the terms contained in the LICENSE file.

"""btclib.bip32 submodule."""
"""btclib.bip32 module."""

from btclib.bip32.bip32 import (
BIP32Key,
Expand All @@ -34,3 +34,24 @@
decode_hd_key_paths,
encode_to_bip32_derivs,
)

__all__ = [
"BIP32Key",
"BIP32KeyOrigin",
"BIP32KeyData",
"HdKeyPaths",
"crack_prv_key",
"derive",
"derive_from_account",
"rootxprv_from_seed",
"xpub_from_xprv",
"bytes_from_bip32_path",
"indexes_from_bip32_path",
"int_from_index_str",
"str_from_bip32_path",
"str_from_index_int",
"assert_valid_hd_key_paths",
"decode_from_bip32_derivs",
"decode_hd_key_paths",
"encode_to_bip32_derivs",
]
9 changes: 2 additions & 7 deletions btclib/bip32/slip132.py
Expand Up @@ -17,13 +17,8 @@
from typing import Any, Callable, List, Tuple

from btclib import b32, b58
from btclib.bip32.bip32 import (
BIP32DerPath,
BIP32Key,
BIP32KeyData,
derive,
xpub_from_xprv,
)
from btclib.bip32.bip32 import BIP32Key, BIP32KeyData, derive, xpub_from_xprv
from btclib.bip32.der_path import BIP32DerPath
from btclib.exceptions import BTClibValueError
from btclib.network import (
NETWORKS,
Expand Down
4 changes: 3 additions & 1 deletion btclib/block/__init__.py
Expand Up @@ -8,7 +8,9 @@
# No part of btclib including this file, may be copied, modified, propagated,
# or distributed except according to the terms contained in the LICENSE file.

"""btclib.ecc submodule."""
"""btclib.ecc module."""

from btclib.block.block import Block
from btclib.block.block_header import BlockHeader

__all__ = ["Block", "BlockHeader"]
12 changes: 11 additions & 1 deletion btclib/ec/__init__.py
Expand Up @@ -8,7 +8,17 @@
# No part of btclib including this file, may be copied, modified, propagated,
# or distributed except according to the terms contained in the LICENSE file.

"""btclib.ec submodule."""
"""btclib.ec module."""

from btclib.ec.curve import Curve, double_mult, mult, multi_mult, secp256k1
from btclib.ec.sec_point import bytes_from_point, point_from_octets

__all__ = [
"Curve",
"double_mult",
"mult",
"multi_mult",
"secp256k1",
"bytes_from_point",
"point_from_octets",
]
4 changes: 2 additions & 2 deletions btclib/ec/curve.py
Expand Up @@ -207,10 +207,10 @@ def __repr__(self) -> str:
secp256k1 = CURVES["secp256k1"]


def mult(m: Integer, Q: Optional[Point] = None, ec: Curve = secp256k1) -> Point:
def mult(m_int: Integer, Q: Optional[Point] = None, ec: Curve = secp256k1) -> Point:
"Elliptic curve scalar multiplication."

m = int_from_integer(m) % ec.n
m: int = int_from_integer(m_int) % ec.n

if (Q == ec.G or Q is None) and ec == secp256k1 and libsecp256k1.is_enabled():
return libsecp256k1.mult.mult(m)
Expand Down
2 changes: 1 addition & 1 deletion btclib/ec/curve_group_2.py
Expand Up @@ -52,7 +52,7 @@
def mods(m: int, w: int) -> int:
"Signed modulo function."

w2 = pow(2, w)
w2: int = pow(2, w)
M = m % w2
return M - w2 if M >= (w2 / 2) else M

Expand Down
2 changes: 1 addition & 1 deletion btclib/ec/curve_group_f.py
Expand Up @@ -17,7 +17,7 @@
from typing import List

from btclib.alias import INF, Point
from btclib.ec.curve import CurveGroup
from btclib.ec.curve_group import CurveGroup
from btclib.exceptions import BTClibValueError


Expand Down
4 changes: 3 additions & 1 deletion btclib/ecc/__init__.py
Expand Up @@ -8,6 +8,8 @@
# No part of btclib including this file, may be copied, modified, propagated,
# or distributed except according to the terms contained in the LICENSE file.

"""btclib.ecc submodule."""
"""btclib.ecc module."""

from btclib.ecc.pedersen import second_generator

__all__ = ["second_generator"]
4 changes: 2 additions & 2 deletions btclib/ecc/ssa.py
Expand Up @@ -266,7 +266,7 @@ def challenge_(msg_hash: Octets, x_Q: int, x_K: int, ec: Curve, hf: HashF) -> in
)
t = tagged_hash("BIP0340/challenge".encode(), t, hf)

c = int_from_bits(t, ec.nlen) % ec.n
c: int = int_from_bits(t, ec.nlen) % ec.n
if c == 0:
raise BTClibRuntimeError("invalid zero challenge") # pragma: no cover
return c
Expand Down Expand Up @@ -439,7 +439,7 @@ def _recover_pub_key_(c: int, r: int, s: int, ec: Curve) -> int:
if QJ[2] == 0:
err_msg = "invalid (INF) key" # pragma: no cover
raise BTClibRuntimeError(err_msg) # pragma: no cover
return ec.x_aff_from_jac(QJ)
return int(ec.x_aff_from_jac(QJ))


def crack_prv_key_(
Expand Down
4 changes: 2 additions & 2 deletions btclib/hashes.py
Expand Up @@ -65,7 +65,7 @@ def reduce_to_hlen(msg: Octets, hf: HashF = hashlib.sha256) -> bytes:
# Step 4 of SEC 1 v.2 section 4.1.3
h = hf()
h.update(msg)
return h.digest()
return bytes(h.digest())


def magic_message(msg: Octets) -> bytes:
Expand Down Expand Up @@ -126,4 +126,4 @@ def tagged_hash(tag: bytes, m: bytes, hf: HashF = hashlib.sha256) -> bytes:
# it could be sped up by storing the above midstate

h2.update(m)
return h2.digest()
return bytes(h2.digest())
20 changes: 19 additions & 1 deletion btclib/mnemonic/__init__.py
Expand Up @@ -8,7 +8,7 @@
# No part of btclib including this file, may be copied, modified, propagated,
# or distributed except according to the terms contained in the LICENSE file.

"""btclib.mnemonic submodule."""
"""btclib.mnemonic module."""

from btclib.mnemonic.entropy import (
BinStr,
Expand All @@ -29,3 +29,21 @@
indexes_from_mnemonic,
mnemonic_from_indexes,
)

__all__ = [
"BinStr",
"Entropy",
"bin_str_entropy_from_bytes",
"bin_str_entropy_from_entropy",
"bin_str_entropy_from_int",
"bin_str_entropy_from_random",
"bin_str_entropy_from_rolls",
"bin_str_entropy_from_str",
"bin_str_entropy_from_wordlist_indexes",
"bytes_entropy_from_str",
"collect_rolls",
"wordlist_indexes_from_bin_str_entropy",
"Mnemonic",
"indexes_from_mnemonic",
"mnemonic_from_indexes",
]
24 changes: 19 additions & 5 deletions btclib/psbt/__init__.py
Expand Up @@ -8,15 +8,29 @@
# No part of btclib including this file, may be copied, modified, propagated,
# or distributed except according to the terms contained in the LICENSE file.

"""btclib.psbt submodule."""
"""btclib.psbt module."""

from btclib.psbt.psbt import Psbt, combine_psbts, extract_tx, finalize_psbt
from btclib.psbt.psbt_in import HdKeyPaths, PsbtIn, Tx
from btclib.psbt.psbt_out import (
PsbtOut,
from btclib.psbt.psbt_in import PsbtIn
from btclib.psbt.psbt_out import PsbtOut
from btclib.psbt.psbt_utils import (
assert_valid_unknown,
decode_dict_bytes_bytes,
encode_dict_bytes_bytes,
serialize_dict_bytes_bytes,
serialize_hd_key_paths,
)
from btclib.psbt.psbt_utils import serialize_hd_key_paths

__all__ = [
"PsbtOut",
"PsbtIn",
"Psbt",
"combine_psbts",
"extract_tx",
"finalize_psbt",
"assert_valid_unknown",
"decode_dict_bytes_bytes",
"encode_dict_bytes_bytes",
"serialize_dict_bytes_bytes",
"serialize_hd_key_paths",
]
8 changes: 5 additions & 3 deletions btclib/psbt/psbt.py
Expand Up @@ -19,14 +19,16 @@

from btclib.alias import Octets, String
from btclib.bip32 import (
BIP32KeyOrigin,
HdKeyPaths,
assert_valid_hd_key_paths,
decode_from_bip32_derivs,
decode_hd_key_paths,
encode_to_bip32_derivs,
)
from btclib.exceptions import BTClibValueError
from btclib.hashes import hash160, sha256
from btclib.psbt.psbt_in import BIP32KeyOrigin, HdKeyPaths, PsbtIn, Tx, Witness
from btclib.psbt.psbt_in import PsbtIn
from btclib.psbt.psbt_out import PsbtOut
from btclib.psbt.psbt_utils import (
assert_valid_unknown,
Expand All @@ -39,8 +41,8 @@
serialize_dict_bytes_bytes,
serialize_hd_key_paths,
)
from btclib.script import serialize
from btclib.script.script_pub_key import type_and_payload
from btclib.script import Witness, serialize, type_and_payload
from btclib.tx import Tx
from btclib.utils import bytesio_from_binarydata

PSBT_MAGIC_BYTES = b"psbt"
Expand Down
3 changes: 2 additions & 1 deletion btclib/psbt/psbt_in.py
Expand Up @@ -18,6 +18,8 @@

from btclib.alias import Octets
from btclib.bip32 import (
BIP32KeyOrigin,
HdKeyPaths,
assert_valid_hd_key_paths,
decode_from_bip32_derivs,
decode_hd_key_paths,
Expand All @@ -27,7 +29,6 @@
from btclib.ecc import dsa
from btclib.exceptions import BTClibValueError
from btclib.hashes import hash160, hash256, ripemd160, sha256
from btclib.psbt.psbt_out import BIP32KeyOrigin, HdKeyPaths
from btclib.psbt.psbt_utils import (
assert_valid_redeem_script,
assert_valid_unknown,
Expand Down
40 changes: 39 additions & 1 deletion btclib/script/__init__.py
Expand Up @@ -8,12 +8,13 @@
# No part of btclib including this file, may be copied, modified, propagated,
# or distributed except according to the terms contained in the LICENSE file.

"""btclib.script submodule."""
"""btclib.script module."""

from btclib.script.script import Command, Script, op_int, parse, serialize
from btclib.script.script_pub_key import (
ScriptPubKey,
address,
assert_nulldata,
assert_p2ms,
assert_p2pk,
assert_p2pkh,
Expand All @@ -23,7 +24,11 @@
assert_p2wsh,
is_nulldata,
is_p2ms,
is_p2pk,
is_p2sh,
is_p2tr,
is_p2wpkh,
is_p2wsh,
type_and_payload,
)
from btclib.script.taproot import (
Expand All @@ -34,3 +39,36 @@
output_pubkey,
)
from btclib.script.witness import Witness

__all__ = [
"Command",
"Script",
"op_int",
"parse",
"serialize",
"ScriptPubKey",
"address",
"assert_p2ms",
"is_p2ms",
"assert_p2pk",
"is_p2pk",
"assert_p2pkh",
"is_p2pk",
"assert_p2sh",
"is_p2sh",
"assert_p2tr",
"is_p2tr",
"assert_p2wpkh",
"is_p2wpkh",
"assert_p2wsh",
"is_p2wsh",
"assert_nulldata",
"is_nulldata",
"type_and_payload",
"Witness",
"TaprootScriptTree",
"check_output_pubkey",
"input_script_sig",
"output_prvkey",
"output_pubkey",
]
8 changes: 5 additions & 3 deletions btclib/script/taproot.py
Expand Up @@ -78,19 +78,21 @@ def output_pubkey(


def output_prvkey(
internal_prvkey: PrvKey,
prvkey: PrvKey,
script_tree: Optional[TaprootScriptTree] = None,
ec: Curve = secp256k1,
) -> int:
internal_prvkey = int_from_prv_key(internal_prvkey)
internal_prvkey: int = int_from_prv_key(prvkey)
P = mult(internal_prvkey)
if script_tree:
_, h = tree_helper(script_tree)
else:
h = b""
has_even_y = ec.y_even(P[0]) == P[1]
internal_prvkey = internal_prvkey if has_even_y else ec.n - internal_prvkey
t = int.from_bytes(tagged_hash(b"TapTweak", P[0].to_bytes(32, "big") + h), "big")
t: int = int.from_bytes(
tagged_hash(b"TapTweak", P[0].to_bytes(32, "big") + h), "big"
)
# edge case that cannot be reproduced in the test suite
if t >= ec.n:
raise BTClibValueError("Invalid script tree hash") # pragma: no cover
Expand Down

0 comments on commit bff9191

Please sign in to comment.