Skip to content

Commit

Permalink
even though the type stub for get_hash() says bytes32, we currently r…
Browse files Browse the repository at this point in the history
…eturn a plain bytes object. This fixes it by actually constructing a bytes32 object to be returned. Also extend the built-in conversion to also support bytes48
  • Loading branch information
arvidn committed Aug 14, 2024
1 parent d409073 commit 42e45db
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
4 changes: 4 additions & 0 deletions crates/chia-protocol/src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,10 @@ impl<const N: usize> ChiaToPython for BytesImpl<N> {
let bytes_module = PyModule::import_bound(py, "chia_rs.sized_bytes")?;
let ty = bytes_module.getattr("bytes32")?;
ty.call1((self.0.into_py(py),))
} else if N == 48 {
let bytes_module = PyModule::import_bound(py, "chia_rs.sized_bytes")?;
let ty = bytes_module.getattr("bytes48")?;
ty.call1((self.0.into_py(py),))
} else {
Ok(PyBytes::new_bound(py, &self.0).into_any())
}
Expand Down
9 changes: 7 additions & 2 deletions crates/chia_py_streamable_macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,15 @@ pub fn py_streamable_macro(input: proc_macro::TokenStream) -> proc_macro::TokenS
}
}

pub fn get_hash<'p>(&self, py: pyo3::Python<'p>) -> pyo3::PyResult<pyo3::Bound<'p, pyo3::types::PyBytes>> {
pub fn get_hash<'p>(&self, py: pyo3::Python<'p>) -> pyo3::PyResult<pyo3::Bound<'p, pyo3::types::PyAny>> {
use pyo3::IntoPy;
use pyo3::types::PyModule;
use pyo3::prelude::PyAnyMethods;
let mut ctx = clvmr::sha2::Sha256::new();
#crate_name::Streamable::update_digest(self, &mut ctx);
Ok(pyo3::types::PyBytes::new_bound(py, &ctx.finalize()))
let bytes_module = PyModule::import_bound(py, "chia_rs.sized_bytes")?;
let ty = bytes_module.getattr("bytes32")?;
ty.call1((&ctx.finalize().into_py(py),))
}
#[pyo3(name = "to_bytes")]
pub fn py_to_bytes<'p>(&self, py: pyo3::Python<'p>) -> pyo3::PyResult<pyo3::Bound<'p, pyo3::types::PyBytes>> {
Expand Down
7 changes: 6 additions & 1 deletion tests/test_streamable.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
from chia_rs.sized_bytes import bytes32
import pytest
import copy
import random

sk = AugSchemeMPL.key_gen(bytes32.random())
rng = random.Random(1337)
sk = AugSchemeMPL.key_gen(bytes32.random(rng))
pk = sk.get_g1()

coin = b"bcbcbcbcbcbcbcbcbcbcbcbcbcbcbcbc"
Expand Down Expand Up @@ -71,6 +73,9 @@ def test_hash_spend() -> None:
assert type(c) is int
assert b != c

assert a1.get_hash() == bytes32.fromhex("2b72a6614da0368147fa6cb785445d6569603e38f2de230e5f30692bf6410245")
assert str(a1.get_hash()) == "2b72a6614da0368147fa6cb785445d6569603e38f2de230e5f30692bf6410245"


def test_hash_spend_bundle_conditions() -> None:

Expand Down

0 comments on commit 42e45db

Please sign in to comment.