From fe06f17e931b588d182e994411fe1c3f65f043bf Mon Sep 17 00:00:00 2001 From: kevinheavey Date: Sun, 6 Oct 2024 19:08:21 +0400 Subject: [PATCH] move wasm impl to signer crate --- Cargo.lock | 1 + programs/sbf/Cargo.lock | 1 + sdk/signer/Cargo.toml | 3 +++ sdk/signer/src/keypair.rs | 28 ++++++++++++++++++++++++++++ sdk/src/wasm/keypair.rs | 37 +++---------------------------------- 5 files changed, 36 insertions(+), 34 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 20ece08d3d4501..3beba19c40dad5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8208,6 +8208,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 10683e64b785aa..3bb740372f5f37 100644 --- a/programs/sbf/Cargo.lock +++ b/programs/sbf/Cargo.lock @@ -6933,6 +6933,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.