diff --git a/wasm/src/jsval.rs b/wasm/src/jsval.rs index 170199836..1bb6b5cfc 100644 --- a/wasm/src/jsval.rs +++ b/wasm/src/jsval.rs @@ -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; @@ -168,7 +168,11 @@ pub fn sexp_from_js_object(sstart: Srcloc, v: &JsValue) -> Option> { } 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) { diff --git a/wasm/tests/clvm-tools-interface/src/lib/tests/index.test.ts b/wasm/tests/clvm-tools-interface/src/lib/tests/index.test.ts index 96863fd66..d56d29e68 100644 --- a/wasm/tests/clvm-tools-interface/src/lib/tests/index.test.ts +++ b/wasm/tests/clvm-tools-interface/src/lib/tests/index.test.ts @@ -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); + }); +})