Skip to content

Commit

Permalink
Implement create_with_seed and relevant test (#69)
Browse files Browse the repository at this point in the history
Co-authored-by: Lucas Bruder <lbruder@me.com>
  • Loading branch information
LucasBruder and Lucas-Bruder authored May 31, 2021
1 parent f468483 commit 964aa25
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
3 changes: 2 additions & 1 deletion solana/publickey.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ def to_base58(self) -> bytes:
@staticmethod
def create_with_seed(from_public_key: PublicKey, seed: str, program_id: PublicKey) -> PublicKey:
"""Derive a public key from another key, a seed, and a program ID."""
raise NotImplementedError("create_with_seed not implemented")
buf = bytes(from_public_key) + seed.encode("utf-8") + bytes(program_id)
return PublicKey(sha256(buf).digest())

@staticmethod
def create_program_address(seeds: List[bytes], program_id: PublicKey) -> PublicKey:
Expand Down
7 changes: 7 additions & 0 deletions tests/unit/test_publickey.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,3 +252,10 @@ def test_is_on_curve():

off_curve = PublicKey("12rqwuEgBYiGhBrDJStCiqEtzQpTTiZbh7teNVLuYcFA")
assert not PublicKey._is_on_curve(pubkey_bytes=bytes(off_curve)) # pylint: disable=protected-access


def test_create_with_seed():
"""Test create with seed"""
default_public_key = PublicKey("11111111111111111111111111111111")
derived_key = PublicKey.create_with_seed(default_public_key, "limber chicken: 4/45", default_public_key)
assert derived_key == PublicKey("9h1HyLCW5dZnBVap8C5egQ9Z6pHyjsh5MNy83iPqqRuq")

0 comments on commit 964aa25

Please sign in to comment.