Exemple #1
0
 def export_wif(self):
     data = b'\x80'
     data = data + self.serialize_private_key()
     data += b'\01'
     checksum = Digest.hash256(data[0:34])
     data += checksum[0:4]
     return base58.b58encode(data)
 def hash256_bytes(self) -> bytes:
     tx_serial = self.serialize_unsigned()
     tx_serial = a2b_hex(tx_serial)
     r = Digest.hash256(tx_serial, False)
     if isinstance(r, bytes):
         return r
     else:
         raise RuntimeError
Exemple #3
0
 def hash256(self, is_hex: bool = False) -> bytes or str:
     tx_serial = self.serialize_unsigned()
     tx_serial = a2b_hex(tx_serial)
     r = Digest.hash256(tx_serial, is_hex)
     if isinstance(r, bytes) or isinstance(r, str):
         return r
     else:
         raise RuntimeError
 def hash256_explorer(self) -> str:
     tx_serial = self.serialize_unsigned()
     tx_serial = a2b_hex(tx_serial)
     digest = Digest.hash256(tx_serial)
     if isinstance(digest, bytes):
         return b2a_hex(digest[::-1]).decode('ascii')
     else:
         return ''
 def hash256_hex(self) -> str:
     tx_serial = self.serialize_unsigned()
     tx_serial = a2b_hex(tx_serial)
     r = Digest.hash256(tx_serial, True)
     if isinstance(r, str):
         return r
     else:
         raise RuntimeError
 def hash(bs: bytearray, engine):
     if engine.op_code == ScriptOp.SHA1:
         return None
     elif engine.op_code == ScriptOp.SHA256:
         return Digest.sha256(bs)
     elif engine.op_code == ScriptOp.HASH160:
         return Digest.hash160(bs)
     elif engine.op_code == ScriptOp.HASH256:
         return Digest.hash256(bs)
     return None
 def b58decode(address: str):
     data = base58.b58decode(address)
     if len(data) != 25:
         raise SDKException(ErrorCode.param_error)
     if data[0] != int.from_bytes(Address.__COIN_VERSION, "little"):
         raise SDKException(ErrorCode.param_error)
     checksum = Digest.hash256(data[0:21])
     if data[21:25] != checksum[0:4]:
         raise SDKException(ErrorCode.param_error)
     return Address(data[1:21])
Exemple #8
0
 def decodeBase58(addr):
     data = base58.b58decode(addr)
     if len(data) != 25:
         raise TypeError
     if data[0] != int.from_bytes(Address.__COIN_VERSION, "little"):
         raise TypeError
     checksum = Digest.hash256(data[0:21])
     if data[21:25] != checksum[0:4]:
         raise TypeError
     return Address(data[1:21])
Exemple #9
0
 def get_privatekey_from_wif(self, wif: str):
     if wif is None or wif is "":
         raise Exception("none wif")
     data = base58.b58decode(wif)
     if len(data) != 38 or data[0] != 0x80 or data[33] != 0x01:
         raise Exception("wif wrong")
     checksum = Digest.hash256(data[0:34])
     for i in range(4):
         if data[len(data) - 4 + i] != checksum[i]:
             raise Exception("wif wrong")
     return data[1:33]
    def export_wif(self) -> str:
        """
        This interface is used to get export ECDSA private key in the form of WIF which
        is a way to encoding an ECDSA private key and make it easier to copy.

        :return: a WIF encode private key.
        """
        data = b''.join([b'\x80', self.__private_key, b'\01'])
        checksum = Digest.hash256(data[0:34])
        wif = base58.b58encode(b''.join([data, checksum[0:4]]))
        return wif.decode('ascii')
    def export_wif(self) -> str:
        """
        This interface is used to get export ECDSA private key in the form of WIF which
        is a way to encoding an ECDSA private key and make it easier to copy.

        :return: a WIF encode private key.
        """
        data = b'\x80'
        data = data + self.serialize_private_key()
        data += b'\01'
        checksum = Digest.hash256(data[0:34])
        data += checksum[0:4]
        wif = base58.b58encode(data)
        return wif.decode('ascii')
    def get_private_key_from_wif(wif: str) -> bytes:
        """
        This interface is used to decode a WIF encode ECDSA private key.

        :param wif: a WIF encode private key.
        :return: a ECDSA private key in the form of bytes.
        """
        if wif is None or wif is "":
            raise Exception("none wif")
        data = base58.b58decode(wif)
        if len(data) != 38 or data[0] != 0x80 or data[33] != 0x01:
            raise Exception("wif wrong")
        checksum = Digest.hash256(data[0:34])
        for i in range(4):
            if data[len(data) - 4 + i] != checksum[i]:
                raise Exception("wif wrong")
        return data[1:33]
 def test_hash256(self):
     msg = b'Nobody inspects the spammish repetition'
     hash256_digest = b"\x9f>\x9eV\xb0\xe6K\x8a\xd0\xd8\xfd\xb4\x87\xfb\x0b\xfc\xfc\xd2\xb2x\xa3\xf2\x04\xdb\xc6t\x10\xdbL@\x90'"
     hex_hash256_digest = '9f3e9e56b0e64b8ad0d8fdb487fb0bfcfcd2b278a3f204dbc67410db4c409027'
     self.assertEqual(hash256_digest, Digest.hash256(msg))
     self.assertEqual(hex_hash256_digest, Digest.hash256(msg, is_hex=True))
Exemple #14
0
 def b58encode(self):
     data = Address.__COIN_VERSION + self.ZERO
     checksum = Digest.hash256(data)[0:4]
     return base58.b58encode(data + checksum).decode('utf-8')
 def b58encode(self):
     script_builder = Address.__COIN_VERSION + self.ZERO
     c256 = Digest.hash256(script_builder)[0:4]
     out_byte_array = script_builder + bytearray(c256)
     return base58.b58encode(bytes(out_byte_array)).decode('utf-8')
 def hash256(self) -> bytes:
     tx_serial = self.serialize_unsigned()
     tx_serial = a2b_hex(tx_serial)
     r = Digest.hash256(tx_serial)
     return a2b_hex(b2a_hex(r))
Exemple #17
0
 def hash256_explorer(self) -> str:
     tx_serial = self.serialize_unsigned()
     digest = Digest.hash256(tx_serial)
     if not isinstance(digest, bytes):
         raise SDKException(ErrorCode.require_bytes_params)
     return bytes.hex(digest[::-1])
 def b58encode(self):
     script_builder = Address.__COIN_VERSION + self.ZERO
     c256 = Digest.hash256(script_builder)[0:4]
     out_bytes = script_builder + c256
     return base58.b58encode(out_bytes).decode('utf-8')
Exemple #19
0
 def b58encode(self):
     sb = Address.__COIN_VERSION + self.ZERO
     c256 = Digest.hash256(sb)[0:4]
     outb = sb + bytearray(c256)
     return base58.b58encode(bytes(outb)).decode('utf-8')
Exemple #20
0
 def hash256(self, is_hex: bool = False) -> bytes or str:
     tx_serial = self.serialize_unsigned()
     digest = Digest.hash256(tx_serial, is_hex)
     return digest