Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More granular control over address formats #9

Merged
merged 1 commit into from
Sep 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pip install ethereum-keys
'0x1b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f70beaf8f588b541507fed6a642c5ab42dfdf8120a7f639de5122d47a69a8e8d1'
>>> signature
'0xccda990dba7864b79dc49158fea269338a1cf5747bc4c4bf1b96823e31a0997e7d1e65c06c5bf128b7109e1b4b9ba8d1305dc33f32f624695b2fa8e02c12c1e000'
>>> pk.public_key.to_address()
>>> pk.public_key.to_checksum_address()
'0x1a642f0E3c3aF545E7AcBD38b07251B3990914F1'
>>> signature.verify_msg(b'a message', pk.public_key)
True
Expand Down Expand Up @@ -142,9 +142,19 @@ hash of the `message`.

#### `PublicKey.to_address() -> text`

Returns the hex encoded ethereum address for this public key.


#### `PublicKey.to_checksum_address() -> text`

Returns the ERC55 checksum formatted ethereum address for this public key.


#### `PublicKey.to_canonical_address() -> bytes`

Returns the 20-byte representation of the ethereum address for this public key.


### `KeyAPI.PrivateKey(private_key_bytes)`

The `PrivateKey` class takes a single argument which must be a bytes string with length 32.
Expand Down
12 changes: 11 additions & 1 deletion eth_keys/datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
int_to_big_endian,
keccak,
to_checksum_address,
to_normalized_address,
)

from eth_keys.utils.address import (
Expand Down Expand Up @@ -129,9 +130,18 @@ def verify_msg(self, message, signature):
def verify_msg_hash(self, message_hash, signature):
return self.backend.ecdsa_verify(message_hash, signature, self)

def to_address(self):
#
# Ethereum address conversions
#
def to_checksum_address(self):
return to_checksum_address(public_key_bytes_to_address(bytes(self)))

def to_address(self):
return to_normalized_address(public_key_bytes_to_address(bytes(self)))

def to_canonical_address(self):
return public_key_bytes_to_address(bytes(self))


class PrivateKey(BaseKey, BackendProxied):
public_key = None
Expand Down
16 changes: 16 additions & 0 deletions tests/core/test_key_and_signature_datastructures.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
decode_hex,
keccak,
is_same_address,
is_normalized_address,
is_checksum_address,
is_canonical_address,
)

from eth_keys.backends import NativeECCBackend
Expand Down Expand Up @@ -88,4 +91,17 @@ def test_recover_from_signature_obj(ecc_backend, private_key):

def test_to_address_from_public_key(private_key):
address = private_key.public_key.to_address()
assert is_normalized_address(address)
assert is_same_address(address, ADDRESS)


def test_to_checksum_address_from_public_key(private_key):
address = private_key.public_key.to_checksum_address()
assert is_checksum_address(address)
assert is_same_address(address, ADDRESS)


def test_to_canonical_address_from_public_key(private_key):
address = private_key.public_key.to_canonical_address()
assert is_canonical_address(address)
assert is_same_address(address, ADDRESS)