Esempio n. 1
0
def MakeSqueak(
    private_key: SqueakPrivateKey,
    content: bytes,
    block_height: int,
    block_hash: bytes,
    timestamp: int,
    reply_to: Optional[bytes] = None,
    recipient: Optional[SqueakPublicKey] = None,
):
    """Create a new squeak.

    Returns a tuple of (squeak, secret_key)

    private_key (SqueakPrivatekey)
    content (bytes)
    block_height (int)
    block_hash (bytes)
    timestamp (int)
    reply_to (Optional[bytes])
    recipient (Optional[SqueakPublickey])
    """
    secret_key = generate_secret_key()
    data_key = sha256(secret_key)
    if recipient:
        shared_key = private_key.get_shared_key(recipient)
        data_key = xor_bytes(data_key, shared_key)
    initialization_vector = generate_initialization_vector()
    enc_content = EncryptContent(data_key, initialization_vector, content)
    hash_enc_content = HashEncryptedContent(enc_content)
    payment_point_encoded = payment_point_bytes_from_scalar_bytes(secret_key)
    nonce = generate_nonce()
    author_public_key = private_key.get_public_key()
    squeak = CSqueak(
        hashEncContent=hash_enc_content,
        hashReplySqk=reply_to or b'\x00' * HASH_LENGTH,
        hashBlock=block_hash,
        nBlockHeight=block_height,
        pubKey=author_public_key.to_bytes(),
        recipientPubKey=recipient.to_bytes() if recipient else b'\x00' *
        PUB_KEY_LENGTH,
        paymentPoint=payment_point_encoded,
        iv=initialization_vector,
        nTime=timestamp,
        nNonce=nonce,
        encContent=enc_content,
    )
    sig = SignSqueak(private_key, squeak)
    squeak.SetSignature(sig)
    return squeak, secret_key
Esempio n. 2
0
def SignSqueak(private_key: SqueakPrivateKey, squeak_header: CSqueakHeader):
    """Generate a signature for the given squeak header.

    private_key (SqueakPrivateKey)
    squeak_header (CSqueakHeader)
    """
    squeak_hash = squeak_header.GetHash()
    return private_key.sign(squeak_hash)
Esempio n. 3
0
    def test_serialize_deserialize_private_key(self, priv_key, pub_key, data):
        serialized = priv_key.to_bytes()
        deserialized_priv_key = SqueakPrivateKey.from_bytes(serialized)

        assert deserialized_priv_key == priv_key

        signature = deserialized_priv_key.sign(data)

        assert len(serialized) == PRIV_KEY_LENGTH
        assert pub_key.verify(data, signature)
Esempio n. 4
0
def generate_private_key():
    return SqueakPrivateKey.generate()
Esempio n. 5
0
    def test_deserialize_invalid_private_key(self):
        invalid_private_key_bytes = b""

        with pytest.raises(InvalidPrivateKeyError):
            SqueakPrivateKey.from_bytes(invalid_private_key_bytes)
Esempio n. 6
0
def other_priv_key():
    yield SqueakPrivateKey.generate()
Esempio n. 7
0
def recipient_private_key():
    yield SqueakPrivateKey.generate()
Esempio n. 8
0
def private_key():
    yield SqueakPrivateKey.generate()