Skip to content

Commit

Permalink
Merge pull request #280 from Chia-Network/json-binary
Browse files Browse the repository at this point in the history
Make BLS types support bytes as input in from_json_dict()
  • Loading branch information
arvidn authored Oct 16, 2023
2 parents 012fb16 + c4aa38d commit 063857a
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 22 deletions.
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: ...
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

0 comments on commit 063857a

Please sign in to comment.