From 3bf89f1e9ea9276c3d1df6612d9ee2dc94797764 Mon Sep 17 00:00:00 2001 From: nazeh Date: Sun, 4 Aug 2024 14:19:20 +0300 Subject: [PATCH] feat(js): add createRecoveryFile and decryptRecoveryFile --- pubky/pkg/test/recovery.js | 19 ++++++++++++++++ pubky/src/native.rs | 4 ++++ pubky/src/wasm.rs | 44 +++++++++++++++++++++++++++----------- pubky/src/wasm/keys.rs | 6 ++++++ 4 files changed, 60 insertions(+), 13 deletions(-) create mode 100644 pubky/pkg/test/recovery.js diff --git a/pubky/pkg/test/recovery.js b/pubky/pkg/test/recovery.js new file mode 100644 index 0000000..cf05160 --- /dev/null +++ b/pubky/pkg/test/recovery.js @@ -0,0 +1,19 @@ +import test from 'tape' + +import { PubkyClient, Keypair } from '../index.cjs' + +test('recovery', async (t) => { + const keypair = Keypair.random(); + + const recoveryFile = PubkyClient.createRecoveryFile(keypair, 'very secure password'); + + t.is(recoveryFile.length, 91) + t.deepEqual( + Array.from(recoveryFile.slice(0, 19)), + [112, 117, 98, 107, 121, 46, 111, 114, 103, 47, 114, 101, 99, 111, 118, 101, 114, 121, 10] + ) + + const recovered = PubkyClient.decryptRecoveryFile(recoveryFile, 'very secure password') + + t.is(recovered.publicKey().z32(), keypair.publicKey().z32()) +}) diff --git a/pubky/src/native.rs b/pubky/src/native.rs index 620c94f..6fc3e72 100644 --- a/pubky/src/native.rs +++ b/pubky/src/native.rs @@ -106,9 +106,13 @@ impl PubkyClient { // === Helpers === + /// Create a recovery file of the `keypair`, containing the secret key encrypted + /// using the `passphrase`. pub fn create_recovery_file(keypair: &Keypair, passphrase: &str) -> Result> { create_recovery_file(keypair, passphrase) } + + /// Recover a keypair from a recovery file by decrypting the secret key using `passphrase`. pub fn decrypt_recovery_file(recovery_file: &[u8], passphrase: &str) -> Result { decrypt_recovery_file(recovery_file, passphrase) } diff --git a/pubky/src/wasm.rs b/pubky/src/wasm.rs index 43a5080..7f44e44 100644 --- a/pubky/src/wasm.rs +++ b/pubky/src/wasm.rs @@ -8,7 +8,10 @@ use wasm_bindgen::prelude::*; use reqwest::{IntoUrl, Method, RequestBuilder, Response}; use url::Url; -use crate::PubkyClient; +use crate::{ + shared::recovery_file::{create_recovery_file, decrypt_recovery_file}, + PubkyClient, +}; mod http; mod keys; @@ -49,28 +52,43 @@ impl PubkyClient { } } + /// Create a recovery file of the `keypair`, containing the secret key encrypted + /// using the `passphrase`. + #[wasm_bindgen(js_name = "createRecoveryFile")] + pub fn create_recovery_file( + keypair: &Keypair, + passphrase: &str, + ) -> Result { + create_recovery_file(keypair.as_inner(), passphrase) + .map(|b| b.as_slice().into()) + .map_err(|e| e.into()) + } + + /// Create a recovery file of the `keypair`, containing the secret key encrypted + /// using the `passphrase`. + #[wasm_bindgen(js_name = "decryptRecoveryFile")] + pub fn decrypt_recovery_file( + recovery_file: &[u8], + passphrase: &str, + ) -> Result { + decrypt_recovery_file(recovery_file, passphrase) + .map(Keypair::from) + .map_err(|e| e.into()) + } + /// Set Pkarr relays used for publishing and resolving Pkarr packets. /// /// By default, [PubkyClient] will use `["https://relay.pkarr.org"]` #[wasm_bindgen(js_name = "setPkarrRelays")] - pub fn set_pkarr_relays(mut self, relays: Vec) -> Self { - let relays: Vec = relays - .into_iter() - .filter_map(|name| name.as_string()) - .collect(); - + pub fn set_pkarr_relays(mut self, relays: Vec) -> Self { self.pkarr_relays = relays; self } // Read the set of pkarr relays used by this client. #[wasm_bindgen(js_name = "getPkarrRelays")] - pub fn get_pkarr_relays(&self) -> Vec { - self.pkarr_relays - .clone() - .into_iter() - .map(JsValue::from) - .collect() + pub fn get_pkarr_relays(&self) -> Vec { + self.pkarr_relays.clone() } /// Signup to a homeserver and update Pkarr accordingly. diff --git a/pubky/src/wasm/keys.rs b/pubky/src/wasm/keys.rs index fd82c4c..345e721 100644 --- a/pubky/src/wasm/keys.rs +++ b/pubky/src/wasm/keys.rs @@ -41,6 +41,12 @@ impl Keypair { } } +impl From for Keypair { + fn from(keypair: pkarr::Keypair) -> Self { + Self(keypair) + } +} + #[wasm_bindgen] pub struct PublicKey(pkarr::PublicKey);