diff --git a/Cargo.lock b/Cargo.lock index d36699dcb829b3..d029e776eebb87 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8130,6 +8130,7 @@ dependencies = [ "solana-signer", "solana-transaction-error", "static_assertions", + "wasm-bindgen", ] [[package]] diff --git a/programs/sbf/Cargo.lock b/programs/sbf/Cargo.lock index 782bae2aa4c89c..4e6152a887a691 100644 --- a/programs/sbf/Cargo.lock +++ b/programs/sbf/Cargo.lock @@ -6854,6 +6854,7 @@ dependencies = [ "solana-pubkey", "solana-signature", "solana-transaction-error", + "wasm-bindgen", ] [[package]] diff --git a/sdk/signer/Cargo.toml b/sdk/signer/Cargo.toml index 516d951130db03..37df95d72d77b7 100644 --- a/sdk/signer/Cargo.toml +++ b/sdk/signer/Cargo.toml @@ -17,6 +17,9 @@ solana-pubkey = { workspace = true } solana-signature = { workspace = true } solana-transaction-error = { workspace = true } +[target.'cfg(target_arch = "wasm32")'.dependencies] +wasm-bindgen = { workspace = true } + [dev-dependencies] serde_json = { workspace = true } solana-signer = { path = ".", features = ["dev-context-only-utils"] } diff --git a/sdk/signer/src/keypair.rs b/sdk/signer/src/keypair.rs index f4012563c3ed76..f0062a9f2c135b 100644 --- a/sdk/signer/src/keypair.rs +++ b/sdk/signer/src/keypair.rs @@ -93,6 +93,34 @@ impl Keypair { } } +#[cfg(target_arch = "wasm32")] +#[allow(non_snake_case)] +#[wasm_bindgen] +impl Keypair { + /// Create a new `Keypair ` + #[wasm_bindgen(constructor)] + pub fn constructor() -> Keypair { + Keypair::new() + } + + /// Convert a `Keypair` to a `Uint8Array` + pub fn toBytes(&self) -> Box<[u8]> { + self.to_bytes().into() + } + + /// Recover a `Keypair` from a `Uint8Array` + pub fn fromBytes(bytes: &[u8]) -> Result { + Keypair::from_bytes(bytes).map_err(|e| e.to_string().into()) + } + + /// Return the `Pubkey` for this `Keypair` + #[wasm_bindgen(js_name = pubkey)] + pub fn js_pubkey(&self) -> Pubkey { + // `wasm_bindgen` does not support traits (`Signer) yet + self.pubkey() + } +} + impl From for Keypair { fn from(value: ed25519_dalek::Keypair) -> Self { Self(value) diff --git a/sdk/src/wasm/keypair.rs b/sdk/src/wasm/keypair.rs index 6f2ffebbb7ccf5..5c5da471a7878a 100644 --- a/sdk/src/wasm/keypair.rs +++ b/sdk/src/wasm/keypair.rs @@ -1,34 +1,3 @@ -//! `Keypair` Javascript interface -#![cfg(target_arch = "wasm32")] -#![allow(non_snake_case)] -use { - crate::signer::{keypair::Keypair, Signer}, - solana_program::{pubkey::Pubkey, wasm::display_to_jsvalue}, - wasm_bindgen::prelude::*, -}; - -#[wasm_bindgen] -impl Keypair { - /// Create a new `Keypair ` - #[wasm_bindgen(constructor)] - pub fn constructor() -> Keypair { - Keypair::new() - } - - /// Convert a `Keypair` to a `Uint8Array` - pub fn toBytes(&self) -> Box<[u8]> { - self.to_bytes().into() - } - - /// Recover a `Keypair` from a `Uint8Array` - pub fn fromBytes(bytes: &[u8]) -> Result { - Keypair::from_bytes(bytes).map_err(display_to_jsvalue) - } - - /// Return the `Pubkey` for this `Keypair` - #[wasm_bindgen(js_name = pubkey)] - pub fn js_pubkey(&self) -> Pubkey { - // `wasm_bindgen` does not support traits (`Signer) yet - self.pubkey() - } -} +//! 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.