Skip to content

Conversation

@rooooooooob
Copy link
Contributor

@rooooooooob rooooooooob commented Jan 26, 2022

This also auto-generates .ts (and thus flow) definitions for all types and inserts them into the to_js_value(): JsValue .ts/flow defs in place of any.

This is a multi-phase process:

  1. Generate (using schemars traits) .json schemas for all types. This
    is done by running the program in rust/json-gen which will export these
    to rust/json-gen/schemas
  2. Running scripts/run-json2ts.js to convert these to .ts files. This also does a lot of fixing up like appending JSON, and removing weird warts generated by the 2 tools (schemars/json-schemas->ts)
  3. Running scripts/json-ts-types.js to replace any any return types
    with the correct *JSON type generated above and merging these into the main .ts def file

so that we can get better type information than simply `any` in the
.ts/flow type annotations.

This is a multi-phase process:
1) Generate (using `schemars` traits) .json schemas for all types. This
is done by running the program in rust/json-gen which will export these
to rust/json-gen/schemas
2) Running scripts/run-json2ts.js to convert these to .ts files
3) We will need to convert the names to add in the JSON suffix to these
(not implemented yet)
4) Running scripts/json-ts-types.js to replace any `any` return types
with the correct *JSON type generated above

some files are hardcoded so they don't line up right now and some steps
aren't working as the schemas generated in 1) inline ALL referenced
schemas within a single schema so in step 2) we end up with a lot of
duplicate type definitions. We might be able to just have a stage to
remove duplicates from the schemas, or maybe there's a way to change the
json to ts tool to not do that.

Some types also throw runtime errors when serializing e.g. non-string
keys despite compiling fine since serde != serde_json
@rooooooooob rooooooooob marked this pull request as draft January 26, 2022 06:02
@rooooooooob
Copy link
Contributor Author

More byte types should probably be manually declared as hex strings too since schemars/serde default to a number array.

rand_os = "0.1"
rand_chacha = "0.1"

[build-dependencies]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can probably get rid of these. I was experimenting with rust build scripts but decided to just have a subcrate to run the script instead since I couldn't access the main crate from it (but I've never worked with rust build scripts so maybe it's possible). It makes more sense to only run these when we do the wasm builds/flowgen I think though anyway.

rooooooooob and others added 4 commits February 9, 2022 14:28
-get rid of [k: string]: unknown in structs
-add js:ts-json-gen command that runs all stages of rust->schemas->ts
gen
-fixes for some runtime errors (maps)
-adding some missing json impls
All definitions are wiped from schemas now and all references to those
local definitions are transformed into external definitions which we
pass in a param to the schema compiler to not generate definitions for.
Fixes for:
-Strings/empty MapOf_ defs
-to_js_value() to actually be a JsValue instead of to_json()
-swap out type defs in .ts with the JSON ones
-get rid of infinite recursion in certain types (no longer using
external $refs)
-finalize npm build commands
-probably some other fixes
@rooooooooob rooooooooob marked this pull request as ready for review February 17, 2022 23:08
@rooooooooob rooooooooob changed the title [WIP] initial work on generating JSON (de)serialization + .ts types from it JSON support Feb 17, 2022
# wasm
[target.'cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))'.dependencies]
wasm-bindgen = "=0.2.78"
wasm-bindgen = { version = "=0.2.78", features = ["serde-serialize"] }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SebastienGllmt I had to use this to construct JsValue from a serde-serialization value (I didn't see a way from JSON strings either without making the object a string itself using JsValue::from_str()). I had to do this as otherwise you just end up with a string return type. I'm still confused why we were manually specifying types like ErgoBoxJSON in our flow types when we used sigma-rust from Yoroi since those serde_json calls are the same as I used here in to_json() which definitely return a string (tested from wasm). That's why I put in both to_json() and to_js_value().

Did we have some magic conversion while ignoring the true type going on for sigma-rust that ran JSON.parse()?

Copy link
Contributor

@SebastienGllmt SebastienGllmt Feb 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To answer your question, no we didn't do anything special.


// just for now we'll do json-in-json until I can figure this out better
// TODO: maybe not generate this? or how do we do this?
impl JsonSchema for TransactionMetadatum {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not the best solution but the options are either:

  1. Just make our own (incompatible with cardano-node) JSON rep using schemars/serde
  2. Manually write .ts defs for the cardano-node JSON schema we follow (I picked DetailedSchema since it supports everything (besides edge-cases with i64 in serde-json) and merge those in as a final step. We'd want to do the same with plutus datums I guess if we go this route.

I'm leaning towards 2 but wanted to get opinions on it. We might have to write them as JSON schemas and generate from there and hope we can get it to match the .ts we want as otherwise other schemas referring to metadata (e.g. tx) might break.


#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
pub fn to_js_value(&self) -> Result<JsValue, JsError> {
JsValue::from_serde(&self)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment above in the cargo.toml

}
}

impl serde::Serialize for BigNum {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to_json()/to_js_value()/from_json() here (and BigInt, etc) for consistency even though to_str()/from_str() do the same thing as this would do?

// TODO: we might want to make sure we don't have other cases where this would replace
// things it shouldn't. We'd have to do some go-back-a-few-lines replace to only do this
// for to_json() comments.
inputFile[i] = line.replace(/(\s?\*\s?\@returns\s\{)(any)(\})/, `$1${currentClass}JSON$3`);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

None of the types return any in our library for now but this could be a problem in the future.

@vsubhuman vsubhuman added this to the 10.1.0 milestone Feb 18, 2022
# wasm
[target.'cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))'.dependencies]
wasm-bindgen = "=0.2.78"
wasm-bindgen = { version = "=0.2.78", features = ["serde-serialize"] }
Copy link
Contributor

@SebastienGllmt SebastienGllmt Feb 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To answer your question, no we didn't do anything special.

@lisicky
Copy link
Contributor

lisicky commented May 17, 2022

@rooooooooob could you update cardano_serialization_lib.js.flow ? I'm afraid I do smth wrong because I got different result then in the branch.

@SebastienGllmt
Copy link
Contributor

@lisicky dcSpark will no longer be contributing to this repo because we are instead managing our fork (cardano-multiplatform-lib) and it would be too much work to back-port all our changes back to here.

@rooooooooob
Copy link
Contributor Author

@lisicky it's a generated file you shouldn't need to manually edit it

@vsubhuman vsubhuman modified the milestones: 10.1.0, 10.2.0 May 19, 2022
# Conflicts:
#	rust/pkg/cardano_serialization_lib.js.flow
#	rust/src/utils.rs
@lisicky
Copy link
Contributor

lisicky commented May 20, 2022

/check

Copy link
Contributor

@lisicky lisicky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/check

Copy link
Contributor

@lisicky lisicky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/check

lisicky added 3 commits June 20, 2022 18:04
# Conflicts:
#	rust/pkg/cardano_serialization_lib.js.flow
#	rust/src/crypto.rs
#	rust/src/lib.rs
#	rust/src/plutus.rs
#	rust/src/tx_builder.rs
#	rust/src/utils.rs
Copy link
Contributor

@lisicky lisicky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/check

@lisicky lisicky changed the base branch from master to babbage June 21, 2022 15:19
lisicky and others added 4 commits June 23, 2022 13:34
# Conflicts:
#	rust/pkg/cardano_serialization_lib.js.flow
#	rust/src/lib.rs
#	rust/src/metadata.rs
Copy link
Contributor

@lisicky lisicky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/check

Base automatically changed from babbage to master July 27, 2022 13:15
# Conflicts:
#	rust/src/plutus.rs
Copy link
Contributor

@lisicky lisicky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/check

Copy link
Contributor

@lisicky lisicky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/check

# Conflicts:
#	rust/pkg/cardano_serialization_lib.js.flow
@vsubhuman vsubhuman merged commit ef546b4 into master Jul 29, 2022
@vsubhuman vsubhuman deleted the auto-json-to-ts branch July 29, 2022 18:55
@vsubhuman vsubhuman mentioned this pull request Sep 29, 2022
14 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants