Add wasm bindings for Pubkey and Keypair

This commit is contained in:
Michael Vines
2021-10-13 16:52:52 -07:00
parent 6919c4863b
commit 488dc37fec
23 changed files with 537 additions and 15 deletions

View File

@ -47,6 +47,7 @@ pub mod system_transaction;
pub mod timing;
pub mod transaction;
pub mod transport;
pub mod wasm;
/// Same as `declare_id` except report that this id has been deprecated
pub use solana_sdk_macro::declare_deprecated_id;

View File

@ -17,9 +17,11 @@ use {
io::{Read, Write},
path::Path,
},
wasm_bindgen::prelude::*,
};
/// A vanilla Ed25519 key pair
#[wasm_bindgen]
#[derive(Debug)]
pub struct Keypair(ed25519_dalek::Keypair);

34
sdk/src/wasm/keypair.rs Normal file
View File

@ -0,0 +1,34 @@
//! `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()
}
}

4
sdk/src/wasm/mod.rs Normal file
View File

@ -0,0 +1,4 @@
//! solana-sdk Javascript interface
#![cfg(target_arch = "wasm32")]
pub mod keypair;