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

Assign some methods as properties #19

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
110 changes: 110 additions & 0 deletions sdk/aleo.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
class Account:
@staticmethod
def from_private_key(private_key: PrivateKey) -> Account: ...
@property
def private_key(self) -> PrivateKey: ...
@property
def view_key(self) -> ViewKey: ...
@property
def address(self) -> Address: ...
def sign(self, message: bytes) -> Signature: ...
def verify(self, signature: Signature, message: bytes) -> bool: ...
def decrypt(self, record_ciphertext: RecordCiphertext) -> RecordPlaintext: ...
def is_owner(self, record_ciphertext: RecordCiphertext) -> bool: ...

class Address:
@staticmethod
def from_string(s: str) -> Address: ...
def to_string(self) -> str: ...

class CoinbasePuzzle:
@staticmethod
def load() -> CoinbasePuzzle: ...
@property
def verifying_key(self) -> CoinbaseVerifyingKey: ...

class CoinbaseVerifyingKey:
pass

class ComputeKey:
@property
def address(self) -> Address: ...
@property
def pk_sig(self) -> str: ...
@property
def pr_sig(self) -> str: ...
@property
def sk_prf(self) -> str: ...

class EpochChallenge:
@staticmethod
def from_json(json: str) -> EpochChallenge: ...
def to_json(self) -> str: ...

class PrivateKey:
@property
def address(self) -> Address: ...
@property
def compute_key(self) -> ComputeKey: ...
@staticmethod
def from_string(private_key: str) -> PrivateKey: ...
@property
def seed(self) -> str: ...
def sign(self, message: bytes) -> Signature: ...
@property
def sk_sig(self) -> str: ...
@property
def r_sig(self) -> str: ...
def to_string(self) -> str: ...
@property
def view_key(self) -> ViewKey: ...

class ProverSolution:
@staticmethod
def from_json(json: str) -> ProverSolution: ...
def to_json(self) -> str: ...
@property
def address(self) -> Address: ...
def verify(self, verifying_key: CoinbaseVerifyingKey,
epoch_challenge: EpochChallenge, proof_target: int) -> bool: ...

class RecordCiphertext:
@staticmethod
def from_string(s: str) -> RecordCiphertext: ...
def to_string(self) -> str: ...
def decrypt(self, view_key: ViewKey) -> RecordPlaintext: ...
def is_owner(self, view_key: ViewKey) -> bool: ...

class RecordPlaintext:
@staticmethod
def from_string(s: str) -> RecordPlaintext: ...
@property
def owner(self) -> str: ...
@property
def nonce(self) -> str: ...
def serial_number_string(self, private_key: PrivateKey, program_id: str,
record_name: str) -> str: ...
def to_string(self) -> str: ...

class Signature:
@property
def challenge(self) -> str: ...
@property
def compute_key(self) -> ComputeKey: ...
@staticmethod
def from_string(s: str) -> Signature: ...
@property
def response(self) -> str: ...
@staticmethod
def sign(private_key: PrivateKey, message: bytes) -> Signature: ...
def to_string(self) -> str: ...
def verify(self, address: Address, message: bytes) -> bool: ...

class ViewKey:
def decrypt(self, record_ciphertext: RecordCiphertext) -> RecordPlaintext: ...
@staticmethod
def from_string(s: str) -> ViewKey: ...
def is_owner(self, record_ciphertext: RecordCiphertext) -> bool: ...
@property
def address(self) -> Address: ...
def to_string(self) -> str: ...
4 changes: 4 additions & 0 deletions sdk/src/account/compute_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,25 @@ pub struct ComputeKey(ComputeKeyNative);
#[pymethods]
impl ComputeKey {
/// Returns the address from the compute key.
#[getter]
fn address(&self) -> Address {
Address::from(self.0.to_address())
}

/// Returns the signature public key.
#[getter]
fn pk_sig(&self) -> String {
self.0.pk_sig().to_string()
}

/// Returns the signature public randomizer.
#[getter]
fn pr_sig(&self) -> String {
self.0.pr_sig().to_string()
}

/// Returns a reference to the PRF secret key.
#[getter]
fn sk_prf(&self) -> String {
self.0.sk_prf().to_string()
}
Expand Down
3 changes: 3 additions & 0 deletions sdk/src/account/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,19 @@ impl Account {
}

/// Returns an account private key.
#[getter]
fn private_key(&self) -> PrivateKey {
self.private_key
}

/// Returns an account view key.
#[getter]
fn view_key(&self) -> ViewKey {
self.view_key.clone()
}

/// Returns an account address.
#[getter]
fn address(&self) -> Address {
self.address.clone()
}
Expand Down
6 changes: 6 additions & 0 deletions sdk/src/account/private_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,13 @@ impl PrivateKey {
}

/// Derives the account address from an account private key.
#[getter]
pub fn address(&self) -> anyhow::Result<Address> {
Ok(Address::from(AddressNative::try_from(&self.0)?))
}

/// Derives the account compute key from an account private key.
#[getter]
fn compute_key(&self) -> ComputeKey {
let compute_key = ComputeKeyNative::try_from(&self.0).unwrap();
ComputeKey::from(compute_key)
Expand All @@ -60,6 +62,7 @@ impl PrivateKey {
}

/// Returns the account seed.
#[getter]
fn seed(&self) -> String {
self.0.seed().to_string()
}
Expand All @@ -70,11 +73,13 @@ impl PrivateKey {
}

/// Returns the signature secret key.
#[getter]
fn sk_sig(&self) -> String {
self.0.sk_sig().to_string()
}

/// Returns the signature randomizer.
#[getter]
fn r_sig(&self) -> String {
self.0.r_sig().to_string()
}
Expand All @@ -87,6 +92,7 @@ impl PrivateKey {
}

/// Initializes a new account view key from an account private key.
#[getter]
pub fn view_key(&self) -> ViewKey {
let view_key = ViewKeyNative::try_from(&self.0).unwrap();
ViewKey::from(view_key)
Expand Down
2 changes: 2 additions & 0 deletions sdk/src/account/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,13 @@ impl RecordPlaintext {
}

/// Returns the owner of the record as a string
#[getter]
fn owner(&self) -> String {
self.0.owner().to_string()
}

/// Returns the nonce of the record as a string
#[getter]
fn nonce(&self) -> String {
self.0.nonce().to_string()
}
Expand Down
3 changes: 3 additions & 0 deletions sdk/src/account/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@ pub struct Signature(SignatureNative);
#[pymethods]
impl Signature {
/// Returns the verifier challenge.
#[getter]
fn challenge(&self) -> String {
self.0.challenge().to_string()
}

/// Returns the signer compute key.
#[getter]
fn compute_key(&self) -> ComputeKey {
ComputeKey::from(self.0.compute_key())
}
Expand All @@ -52,6 +54,7 @@ impl Signature {
}

/// Returns the prover response.
#[getter]
fn response(&self) -> String {
self.0.response().to_string()
}
Expand Down
3 changes: 2 additions & 1 deletion sdk/src/account/view_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ impl ViewKey {
}

/// Returns the address corresponding to the view key.
fn to_address(&self) -> Address {
#[getter]
fn address(&self) -> Address {
let address = self.0.to_address();
Address::from(address)
}
Expand Down
1 change: 1 addition & 0 deletions sdk/src/coinbase/prover_solution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ impl ProverSolution {
}

/// Returns the address of the prover.
#[getter]
fn address(&self) -> Address {
Address::from(self.0.address())
}
Expand Down
1 change: 1 addition & 0 deletions sdk/src/coinbase/puzzle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ impl CoinbasePuzzle {
}

/// Returns the coinbase verifying key.
#[getter]
fn verifying_key(&self) -> anyhow::Result<CoinbaseVerifyingKey> {
let verifying_key = self.0.coinbase_verifying_key().clone();
Ok(CoinbaseVerifyingKey::from(verifying_key))
Expand Down
14 changes: 7 additions & 7 deletions sdk/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ def test_sanity():
private_key = aleo.PrivateKey.from_string(c_private_key)

assert str(private_key) == c_private_key
assert str(private_key.view_key()) == c_view_key
assert str(private_key.address()) == c_address
assert str(private_key.view_key) == c_view_key
assert str(private_key.address) == c_address

view_key = aleo.ViewKey.from_string(c_view_key)
assert str(view_key) == c_view_key
assert view_key == private_key.view_key()
assert view_key == private_key.view_key

address = aleo.Address.from_string(c_address)
assert str(address) == c_address
assert address == private_key.address()
assert address == private_key.address

def test_decrypt_success():
c_plaintext = """{
Expand Down Expand Up @@ -47,14 +47,14 @@ def test_signature_verify():
def test_account_sanity():
private_key = aleo.PrivateKey.from_string("APrivateKey1zkp3dQx4WASWYQVWKkq14v3RoQDfY2kbLssUj7iifi1VUQ6")
account = aleo.Account.from_private_key(private_key)
assert account.private_key() == private_key
assert account.private_key == private_key
assert account == aleo.Account.from_private_key(private_key)
message = bytes("asd", "utf-8")
bad_message = bytes("qwe", "utf-8")
signature = account.sign(message)
assert account.verify(signature, message)
assert not account.verify(signature, bad_message)
assert signature.verify(account.address(), message)
assert signature.verify(account.address, message)

def test_coinbase():
address = aleo.Address.from_string("aleo16xwtrvntrfnan84sy3qg2gdkkp5u5p7sjc882lx8n06fjx2k0yqsklw8sv")
Expand All @@ -63,7 +63,7 @@ def test_coinbase():
challenge = aleo.EpochChallenge.from_json(challenge_json)
solution = aleo.ProverSolution.from_json(solution_json)

assert solution.address() == address
assert solution.address == address
assert str(challenge) == challenge_json
assert str(solution) == solution_json

Expand Down