Example #1
0
    def _decode_signature(self, signature):
        """
            Decode the internal fields of the base64-encoded signature.
        """

        sig = a2b_base64(signature)
        if len(sig) != 65:
            raise EncodingError("Wrong length, expected 65")

        # split into the parts.
        first = byte2int(sig)
        r = from_bytes_32(sig[1:33])
        s = from_bytes_32(sig[33:33+32])

        # first byte encodes a bits we need to know about the point used in signature
        if not (27 <= first < 35):
            raise EncodingError("First byte out of range")

        # NOTE: The first byte encodes the "recovery id", or "recid" which is a 3-bit values
        # which selects compressed/not-compressed and one of 4 possible public pairs.
        #
        first -= 27
        is_compressed = bool(first & 0x4)

        return is_compressed, (first & 0x3), r, s
Example #2
0
    def _decode_signature(self, signature):
        """
            Decode the internal fields of the base64-encoded signature.
        """

        sig = a2b_base64(signature)
        if len(sig) != 65:
            raise EncodingError("Wrong length, expected 65")

        # split into the parts.
        first = byte2int(sig)
        r = from_bytes_32(sig[1:33])
        s = from_bytes_32(sig[33:33+32])

        # first byte encodes a bits we need to know about the point used in signature
        if not (27 <= first < 35):
            raise EncodingError("First byte out of range")

        # NOTE: The first byte encodes the "recovery id", or "recid" which is a 3-bit values
        # which selects compressed/not-compressed and one of 4 possible public pairs.
        #
        first -= 27
        is_compressed = bool(first & 0x4)

        return is_compressed, (first & 0x3), r, s
Example #3
0
    def test_sign_verify_mutual_compatability(self):
        if libsecp256k1 is None:
            raise unittest.SkipTest("no libsecp256k1")
        ctx = libsecp256k1.ctx
        signature = create_string_buffer(64)
        sighash = to_bytes_32(1000)
        secret_key = to_bytes_32(100)

        public_key = create_string_buffer(64)
        r = libsecp256k1.secp256k1_ec_pubkey_create(ctx, public_key,
                                                    secret_key)
        self.assertEqual(r, 1)
        self.assertEqual(
            b2h(public_key),
            '880f50f7ceb4210289266a40b306e33ef52bb75f834c172e65175e3ce2ac3bed'
            '6e2835e3d57ae1fcd0954808be17bd97bf871f7a8a5edadcffcc8812576f7ae5')

        r = libsecp256k1.secp256k1_ecdsa_sign(ctx, signature, sighash,
                                              secret_key, None, None)
        self.assertEqual(r, 1)

        r = libsecp256k1.secp256k1_ecdsa_verify(ctx, signature, sighash,
                                                public_key)
        self.assertEqual(r, 1)

        signature1 = signature[:-1] + int2byte(byte2int(signature[-1]) ^ 1)
        r = libsecp256k1.secp256k1_ecdsa_verify(ctx, signature1, sighash,
                                                public_key)
        self.assertEqual(r, 0)
Example #4
0
    def test_sign_verify_mutual_compatability(self):
        if libsecp256k1 is None:
            raise unittest.SkipTest("no libsecp256k1")
        ctx = libsecp256k1.ctx
        signature = create_string_buffer(64)
        sighash = to_bytes_32(1000)
        secret_key = to_bytes_32(100)

        public_key = create_string_buffer(64)
        r = libsecp256k1.secp256k1_ec_pubkey_create(ctx, public_key, secret_key)
        self.assertEqual(r, 1)
        self.assertEqual(
            b2h(public_key),
            '880f50f7ceb4210289266a40b306e33ef52bb75f834c172e65175e3ce2ac3bed'
            '6e2835e3d57ae1fcd0954808be17bd97bf871f7a8a5edadcffcc8812576f7ae5'
        )

        r = libsecp256k1.secp256k1_ecdsa_sign(ctx, signature, sighash, secret_key, None, None)
        self.assertEqual(r, 1)

        r = libsecp256k1.secp256k1_ecdsa_verify(ctx, signature, sighash, public_key)
        self.assertEqual(r, 1)

        signature1 = signature[:-1] + int2byte(byte2int(signature[-1]) ^ 1)
        r = libsecp256k1.secp256k1_ecdsa_verify(ctx, signature1, sighash, public_key)
        self.assertEqual(r, 0)
Example #5
0
 def from_bytes(bytes, byteorder="big", signed=False):
     if byteorder != "big":
         bytes = reversed(bytes)
     v = 0
     for c in iterbytes(bytes):
         v <<= 8
         v += c
     if signed and byte2int(bytes) & 0x80:
         v = v - (1 << (8 * len(bytes)))
     return v
Example #6
0
 def from_bytes(bytes, byteorder="big", signed=False):
     """This is the same functionality as ``int.from_bytes`` in python 3"""
     if byteorder != "big":
         bytes = reversed(bytes)
     v = 0
     for c in iterbytes(bytes):
         v <<= 8
         v += c
     if signed and byte2int(bytes) & 0x80:
         v = v - (1 << (8*len(bytes)))
     return v
Example #7
0
 def from_bytes(bytes, byteorder="big", signed=False):
     "See int.from_bytes in python 3"
     if byteorder != "big":
         bytes = reversed(bytes)
     v = 0
     for c in iterbytes(bytes):
         v <<= 8
         v += c
     if signed and byte2int(bytes) & 0x80:
         v = v - (1 << (8*len(bytes)))
     return v
Example #8
0
 def __init__(self, version, hash256):
     assert len(version) == 1
     assert isinstance(version, bytes)
     assert len(hash256) == 32
     assert isinstance(hash256, bytes)
     version_int = byte2int(version)
     assert 0 <= version_int <= 16
     self.version = version_int
     self.hash256 = hash256
     self._address = None
     self._script = None
Example #9
0
 def __init__(self, script_tools, address_api):
     self._script_tools = script_tools
     self._address = address_api
     for _ in "EQUAL HASH160 CHECKSIG CHECKSIGVERIFY CHECKMULTISIG CHECKMULTISIGVERIFY".split():
         setattr(self, "OP_%s" % _, byte2int(self._script_tools.compile('OP_%s' % _)))