Skip to content

Commit

Permalink
Fix js interface error identified by fcoleman
Browse files Browse the repository at this point in the history
  • Loading branch information
prozacchiwawa committed May 17, 2024
1 parent 6471d34 commit f3d11b2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
8 changes: 6 additions & 2 deletions wasm/src/jsval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::convert::TryFrom;
use std::rc::Rc;
use std::str::FromStr;

use clvm_tools_rs::classic::clvm::__type_compatibility__::{Bytes, BytesFromType};
use clvm_tools_rs::classic::clvm::__type_compatibility__::{bi_zero, Bytes, BytesFromType};
use clvm_tools_rs::compiler::sexp::SExp;
use clvm_tools_rs::compiler::srcloc::{Srcloc, Until};
use clvm_tools_rs::util::Number;
Expand Down Expand Up @@ -168,7 +168,11 @@ pub fn sexp_from_js_object(sstart: Srcloc, v: &JsValue) -> Option<Rc<SExp>> {
} else if let Some(fval) = v.as_f64() {
(fval as i64)
.to_bigint()
.map(|x| Rc::new(SExp::Integer(sstart.clone(), x)))
.map(|x| if x == bi_zero() {
Rc::new(SExp::Nil(sstart.clone()))
} else {
Rc::new(SExp::Integer(sstart.clone(), x))
})
} else if let Some(g1_bytes) = detect_serializable(&sstart, v) {
Some(g1_bytes)
} else if let Ok(converted) = detect_convertible(v) {
Expand Down
19 changes: 19 additions & 0 deletions wasm/tests/clvm-tools-interface/src/lib/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,3 +228,22 @@ it('can uncurry an example program', async () => {
assert.equal(uncurried[1][1].toString(), 'a06d95dae356e32a71db5ddcb42224754a02524c615c5fc35f568c2af04774e589');
assert.equal(uncurried[1][2].toString(), cat2_curried_program);
});

// Thanks: fcoleman
describe('Program', () => {
it('preserves the representation across js and hex representations', () => {
const puzzle1 = Program.to([0]);
const puzzleHash1 = Buffer.from(puzzle1.sha256tree()).toString('hex');

const puzzle2 = Program.from_hex(puzzle1.toString());
const puzzleHash2 = Buffer.from(puzzle2.sha256tree()).toString('hex');

const puzzle3 = Program.from_hex('ff8080');
const puzzleHash3 = Buffer.from(puzzle3.sha256tree()).toString('hex');

expect(puzzle1.toString()).toEqual(puzzle2.toString());
expect(puzzle1.toString()).toEqual(puzzle3.toString());
expect(puzzleHash1).toEqual(puzzleHash2);
expect(puzzleHash1).toEqual(puzzleHash3);
});
})

0 comments on commit f3d11b2

Please sign in to comment.