diff --git a/Cargo.lock b/Cargo.lock index 5ae0261d9..ec635a6ea 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3522,8 +3522,6 @@ dependencies = [ "borsh 0.10.4", "borsh 1.5.5", "bytemuck", - "console_error_panic_hook", - "console_log", "getrandom 0.2.15", "lazy_static", "log", @@ -3575,7 +3573,6 @@ dependencies = [ "solana-sysvar", "solana-sysvar-id", "thiserror 2.0.12", - "wasm-bindgen", ] [[package]] @@ -3726,7 +3723,6 @@ dependencies = [ "bincode", "bs58", "getrandom 0.1.16", - "js-sys", "serde", "solana-account", "solana-derivation-path", @@ -3760,7 +3756,6 @@ dependencies = [ "solana-transaction", "solana-transaction-error", "thiserror 2.0.12", - "wasm-bindgen", ] [[package]] @@ -3780,6 +3775,20 @@ dependencies = [ "syn 2.0.98", ] +[[package]] +name = "solana-sdk-wasm-js" +version = "1.0.0" +dependencies = [ + "console_error_panic_hook", + "console_log", + "js-sys", + "log", + "solana-instruction", + "solana-program", + "solana-sdk", + "wasm-bindgen", +] + [[package]] name = "solana-secp256k1-program" version = "2.2.3" @@ -4771,7 +4780,7 @@ version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb" dependencies = [ - "windows-sys 0.48.0", + "windows-sys 0.59.0", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 4dcf9b4ca..3c8476212 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -71,6 +71,7 @@ members = [ "sdk", "sdk-ids", "sdk-macro", + "sdk-wasm-js", "secp256k1-program", "secp256k1-recover", "secp256r1-program", diff --git a/program/Cargo.toml b/program/Cargo.toml index c99dc1bbc..e6fe6f240 100644 --- a/program/Cargo.toml +++ b/program/Cargo.toml @@ -18,7 +18,7 @@ all-features = true rustdoc-args = ["--cfg=docsrs"] [lib] -crate-type = ["cdylib", "rlib"] +crate-type = ["rlib"] [features] default = ["borsh"] @@ -113,12 +113,6 @@ thiserror = { workspace = true } num-bigint = { workspace = true } solana-example-mocks = { workspace = true } -[target.'cfg(target_arch = "wasm32")'.dependencies] -console_error_panic_hook = { workspace = true } -console_log = { workspace = true } -getrandom = { workspace = true, features = ["js", "wasm-bindgen"] } -wasm-bindgen = { workspace = true } - # This is currently needed to build on-chain programs reliably. # Borsh 0.10 may pull in hashbrown 0.13, which uses ahash 0.8, which uses # getrandom 0.2 underneath. This explicit dependency allows for no-std if cargo diff --git a/program/src/lib.rs b/program/src/lib.rs index c148b4daf..36c35c3b5 100644 --- a/program/src/lib.rs +++ b/program/src/lib.rs @@ -94,16 +94,22 @@ //! //! ```toml //! [lib] -//! crate-type = ["cdylib", "rlib"] +//! crate-type = ["cdylib"] //! //! [features] //! no-entrypoint = [] //! ``` //! -//! Note that a Solana program must specify its crate-type as "cdylib", and -//! "cdylib" crates will automatically be discovered and built by the `cargo -//! build-bpf` command. Solana programs also often have crate-type "rlib" so -//! they can be linked to other Rust crates. +//! Note that a Solana program must specify its crate-type as "cdylib", to +//! be discovered and built by the `cargo-build-sbf` command as a deployable program. +//! Solana programs also often have crate-type "rlib" so they can be linked to other Rust crates. +//! Avoid using "rlib" and "cdylib" crates together, since their combined usage precludes +//! compiler optimizations that may decrease program size and CU usage. +//! +//! Prefer writing a separate package if it is supposed to be used as a library for other Solana +//! programs (i.e. a "rlib" only crate). This would be normally the case for defining account +//! types and helpers that are used by both clients and program. When creating a Rust project +//! intended to be a program ready for deployment, use only the "cdylib" crate type. //! //! # On-chain vs. off-chain compilation targets //! @@ -489,7 +495,6 @@ pub mod slot_hashes; pub mod slot_history; pub mod syscalls; pub mod sysvar; -pub mod wasm; #[deprecated(since = "2.2.0", note = "Use `solana-big-mod-exp` crate instead")] pub use solana_big_mod_exp as big_mod_exp; @@ -528,8 +533,6 @@ pub use solana_short_vec as short_vec; pub use solana_stable_layout as stable_layout; #[cfg(not(target_os = "solana"))] pub use solana_sysvar::program_stubs; -#[cfg(target_arch = "wasm32")] -pub use wasm_bindgen::prelude::wasm_bindgen; pub use { solana_account_info::{self as account_info, debug_account_data}, solana_clock as clock, diff --git a/scripts/build-sbf.sh b/scripts/build-sbf.sh index d73b55f83..d39654f80 100755 --- a/scripts/build-sbf.sh +++ b/scripts/build-sbf.sh @@ -19,6 +19,7 @@ exclude_list=( "presigner" "quic-definitions" "rent-collector" + "sdk-wasm-js" "secp256k1-program" "secp256r1-program" "system-transaction" diff --git a/scripts/test-wasm.sh b/scripts/test-wasm.sh index 12b224f86..3f8fee394 100755 --- a/scripts/test-wasm.sh +++ b/scripts/test-wasm.sh @@ -5,10 +5,6 @@ here="$(dirname "$0")" src_root="$(readlink -f "${here}/..")" cd "${src_root}" -for dir in program sdk ; do - ( - cd "$dir" - npm install - npm test - ) -done +cd sdk-wasm-js +npm install +npm test diff --git a/program/.gitignore b/sdk-wasm-js/.gitignore similarity index 100% rename from program/.gitignore rename to sdk-wasm-js/.gitignore diff --git a/sdk-wasm-js/Cargo.toml b/sdk-wasm-js/Cargo.toml new file mode 100644 index 000000000..ed838d355 --- /dev/null +++ b/sdk-wasm-js/Cargo.toml @@ -0,0 +1,33 @@ +[package] +name = "solana-sdk-wasm-js" +description = "Solana SDK Wasm JS" +documentation = "https://docs.rs/solana-sdk-wasm-js" +version = "1.0.0" +authors = { workspace = true } +repository = { workspace = true } +homepage = { workspace = true } +license = { workspace = true } +edition = { workspace = true } + +[package.metadata.docs.rs] +targets = ["wasm32-unknown-unknown"] +all-features = true +rustdoc-args = ["--cfg=docsrs"] + +[lib] +crate-type = ["cdylib"] + +[dependencies] +solana-instruction = { workspace = true, features = ["std"] } +solana-program = { workspace = true } +solana-sdk = { workspace = true } + +[target.'cfg(target_arch = "wasm32")'.dependencies] +console_error_panic_hook = { workspace = true } +console_log = { workspace = true } +js-sys = { workspace = true } +log = { workspace = true } +wasm-bindgen = { workspace = true } + +[lints] +workspace = true diff --git a/sdk-wasm-js/README.md b/sdk-wasm-js/README.md new file mode 100644 index 000000000..7d6f12cc1 --- /dev/null +++ b/sdk-wasm-js/README.md @@ -0,0 +1,15 @@ +

+ + Solana + +

+ +# Solana SDK WASM JS + +Use the Solana SDK WASM JS crate to build a JS package with `wasm-pack` usable in Node or browser environments. +See the [Solana Program Crate](https://crates.io/crates/solana-program) instead for on-chain programs, and, the [Solana SDK Crate](https://crates.io/crates/solana-sdk) for client-side +applications. + +More information about Solana is available in the [Solana documentation](https://solana.com/docs). + +Still have questions? Ask us on [Stack Exchange](https://sola.na/sse) diff --git a/program/package.json b/sdk-wasm-js/package.json similarity index 100% rename from program/package.json rename to sdk-wasm-js/package.json diff --git a/program/src/wasm/mod.rs b/sdk-wasm-js/src/lib.rs similarity index 59% rename from program/src/wasm/mod.rs rename to sdk-wasm-js/src/lib.rs index 337dfb1db..313f60573 100644 --- a/program/src/wasm/mod.rs +++ b/sdk-wasm-js/src/lib.rs @@ -2,7 +2,24 @@ #![cfg(target_arch = "wasm32")] #[deprecated(since = "2.2.0", note = "Use solana_instruction::wasm instead.")] pub use solana_instruction::wasm as instructions; -use wasm_bindgen::prelude::*; +use {::log::Level, wasm_bindgen::prelude::*}; +pub use { + solana_program::*, + solana_sdk::{ + // These imports exist in both solana_sdk and solana_program, so we use + // direct imports to suppress ambiguous re-export warnings. + declare_deprecated_id, + declare_id, + entrypoint, + entrypoint_deprecated, + example_mocks, + hash, + program_stubs, + pubkey, + *, + }, +}; + // This module is intentionally left empty. The wasm system instruction impl can be // found in the `solana-system-interface` crate. pub mod system_instruction {} @@ -15,7 +32,7 @@ pub fn solana_program_init() { INIT.call_once(|| { std::panic::set_hook(Box::new(console_error_panic_hook::hook)); - console_log::init_with_level(log::Level::Info).unwrap(); + console_log::init_with_level(Level::Info).unwrap(); }); } diff --git a/program/tests/hash.mjs b/sdk-wasm-js/tests/hash.mjs similarity index 100% rename from program/tests/hash.mjs rename to sdk-wasm-js/tests/hash.mjs diff --git a/sdk/tests/keypair.mjs b/sdk-wasm-js/tests/keypair.mjs similarity index 100% rename from sdk/tests/keypair.mjs rename to sdk-wasm-js/tests/keypair.mjs diff --git a/program/tests/pubkey.mjs b/sdk-wasm-js/tests/pubkey.mjs similarity index 100% rename from program/tests/pubkey.mjs rename to sdk-wasm-js/tests/pubkey.mjs diff --git a/sdk/tests/transaction.mjs b/sdk-wasm-js/tests/transaction.mjs similarity index 100% rename from sdk/tests/transaction.mjs rename to sdk-wasm-js/tests/transaction.mjs diff --git a/sdk/.gitignore b/sdk/.gitignore index 14bd5d170..7809eb37b 100644 --- a/sdk/.gitignore +++ b/sdk/.gitignore @@ -1,4 +1,2 @@ /farf/ -/node_modules/ -/package-lock.json /target/ diff --git a/sdk/Cargo.toml b/sdk/Cargo.toml index 21be8b508..e58c640b2 100644 --- a/sdk/Cargo.toml +++ b/sdk/Cargo.toml @@ -17,7 +17,7 @@ all-features = true rustdoc-args = ["--cfg=docsrs"] [lib] -crate-type = ["cdylib", "rlib"] +crate-type = ["rlib"] [features] # "program" feature is a legacy feature retained to support v1.3 and older @@ -110,8 +110,6 @@ thiserror = { workspace = true } [target.'cfg(target_arch = "wasm32")'.dependencies] getrandom = { version = "0.1.1", features = ["wasm-bindgen"] } -js-sys = { workspace = true } -wasm-bindgen = { workspace = true } [dev-dependencies] solana-instructions-sysvar = { workspace = true, features = ["dev-context-only-utils"] } diff --git a/sdk/package.json b/sdk/package.json deleted file mode 120000 index 763b62f88..000000000 --- a/sdk/package.json +++ /dev/null @@ -1 +0,0 @@ -../program/package.json \ No newline at end of file diff --git a/sdk/src/lib.rs b/sdk/src/lib.rs index a4c77b479..afb5cfaa3 100644 --- a/sdk/src/lib.rs +++ b/sdk/src/lib.rs @@ -38,8 +38,6 @@ extern crate self as solana_sdk; pub use solana_message as message; #[cfg(not(target_os = "solana"))] pub use solana_program::program_stubs; -#[cfg(target_arch = "wasm32")] -pub use solana_program::wasm_bindgen; pub use solana_program::{ account_info, big_mod_exp, blake3, bpf_loader, bpf_loader_deprecated, clock, config, custom_heap_default, custom_panic_default, debug_account_data, declare_deprecated_sysvar_id, @@ -67,7 +65,6 @@ pub mod signature; pub mod signer; pub mod transaction; pub mod transport; -pub mod wasm; #[deprecated(since = "2.1.0", note = "Use `solana-account` crate instead")] pub use solana_account as account; diff --git a/sdk/src/wasm/keypair.rs b/sdk/src/wasm/keypair.rs deleted file mode 100644 index 5c5da471a..000000000 --- a/sdk/src/wasm/keypair.rs +++ /dev/null @@ -1,3 +0,0 @@ -//! This module is empty but has not yet been removed because that would -//! technically be a breaking change. There was never anything to import -//! from here. diff --git a/sdk/src/wasm/mod.rs b/sdk/src/wasm/mod.rs deleted file mode 100644 index 6946e730f..000000000 --- a/sdk/src/wasm/mod.rs +++ /dev/null @@ -1,5 +0,0 @@ -//! solana-sdk Javascript interface -#![cfg(target_arch = "wasm32")] - -pub mod keypair; -pub mod transaction; diff --git a/sdk/src/wasm/transaction.rs b/sdk/src/wasm/transaction.rs deleted file mode 100644 index 5c5da471a..000000000 --- a/sdk/src/wasm/transaction.rs +++ /dev/null @@ -1,3 +0,0 @@ -//! This module is empty but has not yet been removed because that would -//! technically be a breaking change. There was never anything to import -//! from here.