Ejemplo n.º 1
0
 def _create_script_signature(self, secret_exponent,
                              signature_for_hash_type_f, signature_type,
                              script):
     sign_value = signature_for_hash_type_f(signature_type, script)
     order = secp256k1_generator.order()
     r, s = secp256k1_generator.sign(secret_exponent, sign_value)
     if s + s > order:
         s = order - s
     return der.sigencode_der(r, s) + int2byte(signature_type)
Ejemplo n.º 2
0
 def test_inverse_mod(self):
     prime = secp256k1_generator.curve().p()
     order = secp256k1_generator.order()
     for v in range(70):
         n = int(float("1e%d" % v))
         i = secp256k1_generator.inverse_mod(n, prime)
         assert n * i % prime == 1
         i = secp256k1_generator.inverse_mod(n, order)
         assert n * i % order == 1
Ejemplo n.º 3
0
    def __init__(self, secret_exponent=None, public_pair=None, hash160=None,
                 prefer_uncompressed=None, is_compressed=None, is_pay_to_script=False, netcode=None):
        """
        secret_exponent:
            a long representing the secret exponent
        public_pair:
            a tuple of long integers on the ecdsa curve
        hash160:
            a hash160 value corresponding to a bitcoin address

        Include at most one of secret_exponent, public_pair or hash160.

        prefer_uncompressed:
            whether or not to produce text outputs as compressed or uncompressed.

        is_pay_to_script:
            whether or not this key is for a pay-to-script style transaction

        netcode:
            the code for the network (as defined in pycoin.networks)

        Include at most one of secret_exponent, public_pair or hash160.
        prefer_uncompressed, is_compressed (booleans) are optional.
        """

        if is_compressed is None:
            is_compressed = False if hash160 else True
        if netcode is None:
            netcode = get_current_netcode()
        if [secret_exponent, public_pair, hash160].count(None) != 2:
            raise ValueError("exactly one of secret_exponent, public_pair, hash160 must be passed.")
        if prefer_uncompressed is None:
            prefer_uncompressed = not is_compressed
        self._prefer_uncompressed = prefer_uncompressed
        self._secret_exponent = secret_exponent
        self._public_pair = public_pair
        self._hash160_uncompressed = None
        self._hash160_compressed = None
        if hash160:
            if is_compressed:
                self._hash160_compressed = hash160
            else:
                self._hash160_uncompressed = hash160
        self._netcode = netcode

        if self._public_pair is None and self._secret_exponent is not None:
            if self._secret_exponent < 1 \
                    or self._secret_exponent >= secp256k1_generator.order():
                raise InvalidSecretExponentError()
            public_pair = self._secret_exponent * secp256k1_generator
            self._public_pair = public_pair

        if self._public_pair is not None \
                and (None in self._public_pair or
                     not secp256k1_generator.contains_point(*self._public_pair)):
            raise InvalidPublicPairError()
Ejemplo n.º 4
0
def sigmake(a_key, a_hash_for_sig, a_sig_type=SIGHASH_ALL):
    """
    Signs a_hash_for_sig with a_key and returns a DER-encoded signature
    with a_sig_type appended.
    """
    order = secp256k1_generator.order()
    r, s = secp256k1_generator.sign(a_key.secret_exponent(), a_hash_for_sig)

    if s + s > order:
        s = order - s

    return sigencode_der(r, s) + int2byte(a_sig_type)
Ejemplo n.º 5
0
 def do_test(secret_exponent, val_list):
     public_point = secret_exponent * secp256k1_generator
     for v in val_list:
         signature = secp256k1_generator.sign(secret_exponent, v)
         r = secp256k1_generator.verify(public_point, v, signature)
         assert r is True
         r = secp256k1_generator.verify(
             public_point, v,
             (signature[0], secp256k1_generator.order() - signature[1]))
         assert r is True
         signature = signature[0], signature[1] + 1
         r = secp256k1_generator.verify(public_point, v, signature)
         assert r is False
Ejemplo n.º 6
0
 def test_multiply(self):
     g2 = secp256k1_generator * 2
     g2p = g2 * 1
     self.assertEqual(g2p, g2)
     g4 = g2 * 2
     self.assertEqual(g4, secp256k1_generator * 4)
     g8 = g2 * 4
     self.assertEqual(g8, secp256k1_generator * 8)
     g24 = g8 * 3
     self.assertEqual(g24, secp256k1_generator * 24)
     g_big = g2 * (71**41)
     self.assertEqual(
         g_big,
         secp256k1_generator * ((2 * 71**41) % secp256k1_generator.order()))
Ejemplo n.º 7
0
    def test_key_limits(self):
        nc = 'BTC'
        cc = b'000102030405060708090a0b0c0d0e0f'
        order = secp256k1_generator.order()

        for k in -1, 0, order, order + 1:
            self.assertRaises(InvalidSecretExponentError,
                              Key,
                              secret_exponent=k)
            self.assertRaises(InvalidSecretExponentError,
                              BIP32Node,
                              nc,
                              cc,
                              secret_exponent=k)

        for i in range(1, 512):
            Key(secret_exponent=i)
            BIP32Node(nc, cc, secret_exponent=i)
Ejemplo n.º 8
0
def parse_as_secret_exponent(s):
    v = parse_as_number(s)
    if v and 0 < v < secp256k1_generator.order():
        return v
Ejemplo n.º 9
0
def generate_default_placeholder_signature():
    order = secp256k1_generator.order()
    r, s = order - 1, order // 2
    return der.sigencode_der(r, s) + int2byte(1)
Ejemplo n.º 10
0
import hashlib
import itertools

from pycoinzpub.ecdsa.secp256k1 import secp256k1_generator
from pycoinzpub.encoding import double_sha256, from_bytes_32, to_bytes_32
from pycoinzpub.key import Key
from pycoinzpub.serialize import b2h

ORDER = secp256k1_generator.order()


def initial_key_to_master_key(initial_key):
    """
    initial_key:
        a hex string of length 32
    """
    b = initial_key.encode("utf8")
    orig_input = b
    for i in range(100000):
        b = hashlib.sha256(b + orig_input).digest()
    return from_bytes_32(b)


class ElectrumWallet(Key):
    def __init__(self,
                 initial_key=None,
                 master_private_key=None,
                 master_public_key=None,
                 netcode='BTC'):
        if [initial_key, master_private_key, master_public_key
            ].count(None) != 2: