Skip to content

Commit

Permalink
move wasm impl to signer crate
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinheavey committed Oct 22, 2024
1 parent 513b8ac commit 1f460af
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 34 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions programs/sbf/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions sdk/signer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
28 changes: 28 additions & 0 deletions sdk/signer/src/keypair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, JsValue> {
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<ed25519_dalek::Keypair> for Keypair {
fn from(value: ed25519_dalek::Keypair) -> Self {
Self(value)
Expand Down
37 changes: 3 additions & 34 deletions sdk/src/wasm/keypair.rs
Original file line number Diff line number Diff line change
@@ -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, JsValue> {
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.

0 comments on commit 1f460af

Please sign in to comment.