def test_anybody_can_encrypt(): """ Similar to anybody_can_verify() above; we show that anybody can encrypt. """ someone = Character(start_learning_now=False, federated_only=True, crypto_power_ups=[SigningPower]) bob = Bob(is_me=False, federated_only=True,) cleartext = b"This is Officer Rod Farva. Come in, Ursula! Come in Ursula!" ciphertext = someone.encrypt_for(bob, cleartext) assert ciphertext is not None
def test_anybody_can_encrypt(): """ Similar to anybody_can_verify() above; we show that anybody can encrypt. """ someone = Character(start_learning_now=False, federated_only=True) bob = Bob(is_me=False, federated_only=True) cleartext = b"This is Officer Rod Farva. Come in, Ursula! Come in Ursula!" ciphertext, signature = someone.encrypt_for(bob, cleartext, sign=False) assert signature == constants.NOT_SIGNED assert ciphertext is not None
def test_character_blockchain_power(testerchain, three_agents): # TODO: Handle multiple providers eth_address = testerchain.interface.w3.eth.accounts[0] sig_privkey = testerchain.interface.provider.ethereum_tester.backend._key_lookup[ eth_utils.to_canonical_address(eth_address)] sig_pubkey = sig_privkey.public_key signer = Character(is_me=True, checksum_address=eth_address) signer._crypto_power.consume_power_up(BlockchainPower(testerchain, eth_address)) # Due to testing backend, the account is already unlocked. power = signer._crypto_power.power_ups(BlockchainPower) power.is_unlocked = True # power.unlock_account('this-is-not-a-secure-password') data_to_sign = b'What does Ursula look like?!?' sig = power.sign_message(data_to_sign) is_verified = verify_eip_191(address=eth_address, message=data_to_sign, signature=sig) assert is_verified is True # Test a bad address/pubkey pair is_verified = verify_eip_191(address=testerchain.interface.w3.eth.accounts[1], message=data_to_sign, signature=sig) assert is_verified is False # Test a signature without unlocking the account power.is_unlocked = False with pytest.raises(PowerUpError): power.sign_message(b'test') # Test lockAccount call del power
def test_actor_without_signing_power_cannot_sign(): """ We can create a Character with no real CryptoPower to speak of. This Character can't even sign a message. """ cannot_sign = CryptoPower(power_ups=[]) non_signer = Character(crypto_power=cannot_sign, start_learning_now=False, federated_only=True) # The non-signer's stamp doesn't work for signing... with pytest.raises(NoSigningPower): non_signer.stamp("something") # ...or as a way to cast the (non-existent) public key to bytes. with pytest.raises(NoSigningPower): bytes(non_signer.stamp)
def test_anybody_can_verify(): """ In the last example, we used the lower-level Crypto API to verify the signature. Here, we show that anybody can do it without needing to directly access Crypto. """ # Alice can sign by default, by dint of her _default_crypto_powerups. alice = Alice(federated_only=True, start_learning_now=False) # So, our story is fairly simple: an everyman meets Alice. somebody = Character(start_learning_now=False, federated_only=True) # Alice signs a message. message = b"A message for all my friends who can only verify and not sign." signature = alice.stamp(message) # Our everyman can verify it. cleartext = somebody.verify_from(alice, message, signature, decrypt=False) assert cleartext is constants.NO_DECRYPTION_PERFORMED
def test_character_transacting_power_signing(testerchain, agency, test_registry): # Pretend to be a character. eth_address = testerchain.etherbase_account signer = Character(is_me=True, registry=test_registry, checksum_address=eth_address) # Manually consume the power up transacting_power = TransactingPower( password=INSECURE_DEVELOPMENT_PASSWORD, signer=Web3Signer(testerchain.client), account=eth_address) signer._crypto_power.consume_power_up(transacting_power) # Retrieve the power up power = signer._crypto_power.power_ups(TransactingPower) assert power == transacting_power assert testerchain.transacting_power == power assert power.is_active is True assert power.is_unlocked is True assert testerchain.transacting_power.is_unlocked is True # Sign Message data_to_sign = b'Premium Select Luxury Pencil Holder' signature = power.sign_message(message=data_to_sign) is_verified = verify_eip_191(address=eth_address, message=data_to_sign, signature=signature) assert is_verified is True # Sign Transaction transaction_dict = { 'nonce': testerchain.client.w3.eth.getTransactionCount(eth_address), 'gasPrice': testerchain.client.w3.eth.gasPrice, 'gas': 100000, 'from': eth_address, 'to': testerchain.unassigned_accounts[1], 'value': 1, 'data': b'' } signed_transaction = power.sign_transaction( transaction_dict=transaction_dict) # Demonstrate that the transaction is valid RLP encoded. restored_transaction = Transaction.from_bytes( serialized_bytes=signed_transaction) restored_dict = restored_transaction.as_dict() assert to_checksum_address(restored_dict['to']) == transaction_dict['to']
def test_anybody_can_verify(): """ In the last example, we used the lower-level Crypto API to verify the signature. Here, we show that anybody can do it without needing to directly access Crypto. """ # Alice can sign by default, by dint of her _default_crypto_powerups. alice = Alice(federated_only=True, start_learning_now=False) # So, our story is fairly simple: an everyman meets Alice. somebody = Character(start_learning_now=False, federated_only=True) # Alice signs a message. message = b"A message for all my friends who can only verify and not sign." signature = alice.stamp(message) # Our everyman can verify it. cleartext = somebody.verify_from(alice, message, signature, decrypt=False) assert cleartext is constants.NO_DECRYPTION_PERFORMED # Of course, verification fails with any fake message with pytest.raises(InvalidSignature): fake = b"McLovin 892 Momona St. Honolulu, HI 96820" _ = somebody.verify_from(alice, fake, signature, decrypt=False) # Signature verification also works when Alice is not living with our # everyman in the same process, and he only knows her by her public key alice_pubkey_bytes = bytes(alice.stamp) hearsay_alice = Character.from_public_keys( {SigningPower: alice_pubkey_bytes}) cleartext = somebody.verify_from(hearsay_alice, message, signature, decrypt=False) assert cleartext is constants.NO_DECRYPTION_PERFORMED hearsay_alice = Character.from_public_keys( verifying_key=alice_pubkey_bytes) cleartext = somebody.verify_from(hearsay_alice, message, signature, decrypt=False) assert cleartext is constants.NO_DECRYPTION_PERFORMED alice.disenchant()
def test_actor_with_signing_power_can_sign(): """ However, simply giving that character a PowerUp bestows the power to sign. Instead of having a Character verify the signature, we'll use the lower level API. """ message = b"Llamas." signer = Character(crypto_power_ups=[SigningPower], is_me=True, start_learning_now=False, federated_only=True) stamp_of_the_signer = signer.stamp # We can use the signer's stamp to sign a message (since the signer is_me)... signature = stamp_of_the_signer(message) # ...or to get the signer's public key for verification purposes. # (note: we verify directly using Umbral API, skipping Character) verification = signature.verify(stamp_of_the_signer.as_umbral_pubkey(), message) assert verification is True