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

Make BLS types support bytes as input in from_json_dict() #280

Merged
merged 1 commit into from
Oct 16, 2023
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
57 changes: 37 additions & 20 deletions chia-bls/src/public_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,28 +305,45 @@ impl ToJsonDict for PublicKey {

#[cfg(feature = "py-bindings")]
pub fn parse_hex_string(o: &PyAny, len: usize, name: &str) -> PyResult<Vec<u8>> {
use pyo3::exceptions::PyValueError;
let s: String = o.extract()?;
let s = if let Some(st) = s.strip_prefix("0x") {
st
} else {
&s[..]
};
let buf = match hex::decode(s) {
Err(_) => {
return Err(PyValueError::new_err("invalid hex"));
use pyo3::exceptions::{PyTypeError, PyValueError};
if let Ok(s) = o.extract::<String>() {
let s = if let Some(st) = s.strip_prefix("0x") {
st
} else {
&s[..]
};
let buf = match hex::decode(s) {
Err(_) => {
return Err(PyValueError::new_err("invalid hex"));
}
Ok(v) => v,
};
if buf.len() != len {
Err(PyValueError::new_err(format!(
"{}, invalid length {} expected {}",
name,
buf.len(),
len
)))
} else {
Ok(buf)
}
} else if let Ok(buf) = o.extract::<Vec<u8>>() {
if buf.len() != len {
Err(PyValueError::new_err(format!(
"{}, invalid length {} expected {}",
name,
buf.len(),
len
)))
} else {
Ok(buf)
}
Ok(v) => v,
};
if buf.len() != len {
Err(PyValueError::new_err(format!(
"{}, invalid length {} expected {}",
name,
buf.len(),
len
)))
} else {
Ok(buf)
Err(PyTypeError::new_err(format!(
"invalid input type for {}",
name
)))
}
}

Expand Down
6 changes: 6 additions & 0 deletions tests/test_streamable.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,9 @@ def test_g1_element() -> None:
d = G1Element.from_json_dict("0xa24d88ce995cea579675377728938eeb3956d5da608414efc9064774dc9653764edeb4823fc8da22c810917bf389c127")
assert d == a

d = G1Element.from_json_dict(bytes.fromhex("a24d88ce995cea579675377728938eeb3956d5da608414efc9064774dc9653764edeb4823fc8da22c810917bf389c127"))
assert d == a

def test_g2_element() -> None:

a = G2Element.from_bytes(bytes.fromhex("a566b4d972db20765c668ce7fdcd76a4a5a8201dc2d5b1e747e2993fcdd99c8c96c1ca0503ade72809ae6d19c5e8400e10900a24ae56b7c9c84231ed5b7dd4c0790dd1aef56e0820e86994aa02c33bd409d3f17ace74c7fa40b00fe5022cc6d6"))
Expand All @@ -380,6 +383,9 @@ def test_g2_element() -> None:
d = G2Element.from_json_dict("0xa566b4d972db20765c668ce7fdcd76a4a5a8201dc2d5b1e747e2993fcdd99c8c96c1ca0503ade72809ae6d19c5e8400e10900a24ae56b7c9c84231ed5b7dd4c0790dd1aef56e0820e86994aa02c33bd409d3f17ace74c7fa40b00fe5022cc6d6")
assert a == d

d = G2Element.from_json_dict(bytes.fromhex("a566b4d972db20765c668ce7fdcd76a4a5a8201dc2d5b1e747e2993fcdd99c8c96c1ca0503ade72809ae6d19c5e8400e10900a24ae56b7c9c84231ed5b7dd4c0790dd1aef56e0820e86994aa02c33bd409d3f17ace74c7fa40b00fe5022cc6d6"))
assert a == d

def test_program() -> None:
p = Program.from_json_dict("0xff8080")
assert str(p) == "Program(ff8080)"
Expand Down
2 changes: 1 addition & 1 deletion wheel/chia_rs.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class AugSchemeMPL:
@staticmethod
def derive_child_sk_unhardened(pk: PrivateKey, index: int) -> PrivateKey: ...
@staticmethod
def derive_child_pk_unhardened(pk: PublicKey, index: int) -> PublicKey: ...
arvidn marked this conversation as resolved.
Show resolved Hide resolved
def derive_child_pk_unhardened(pk: G1Element, index: int) -> G1Element: ...

class G1Element:
SIZE: ClassVar[int] = ...
Expand Down
2 changes: 1 addition & 1 deletion wheel/generate_type_stubs.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ def derive_child_sk(pk: PrivateKey, index: int) -> PrivateKey: ...
@staticmethod
def derive_child_sk_unhardened(pk: PrivateKey, index: int) -> PrivateKey: ...
@staticmethod
def derive_child_pk_unhardened(pk: PublicKey, index: int) -> PublicKey: ...
def derive_child_pk_unhardened(pk: G1Element, index: int) -> G1Element: ...
"""
)

Expand Down
Loading