Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Use keypair internally within warp-ipfs #522

Merged
merged 10 commits into from
May 23, 2024
39 changes: 18 additions & 21 deletions extensions/warp-ipfs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1166,15 +1166,18 @@ impl MultiPassImportExport for WarpIpfs {
} => {
let keypair = warp::crypto::keypair::did_from_mnemonic(&passphrase, None)?;

let bytes = Zeroizing::new(keypair.private_key_bytes());
let internal_keypair = rust_ipfs::Keypair::ed25519_from_bytes(bytes)
.map_err(|_| Error::PrivateKeyInvalid)?;

let bytes = fs::read(path).await?;
let decrypted_bundle = ecdh_decrypt(&keypair, None, bytes)?;

let decrypted_bundle = ecdh_decrypt(&internal_keypair, None, bytes)?;
let exported_document =
serde_json::from_slice::<ResolvedRootDocument>(&decrypted_bundle)?;

exported_document.verify()?;

let bytes = Zeroizing::new(keypair.private_key_bytes());

warp::crypto::keypair::mnemonic_into_tesseract(
&mut self.tesseract,
&passphrase,
Expand All @@ -1183,11 +1186,7 @@ impl MultiPassImportExport for WarpIpfs {
false,
)?;

self.init_ipfs(
rust_ipfs::Keypair::ed25519_from_bytes(bytes)
.map_err(|_| Error::PrivateKeyInvalid)?,
)
.await?;
self.init_ipfs(internal_keypair).await?;

let mut store = self.identity_store(false).await?;

Expand All @@ -1199,16 +1198,19 @@ impl MultiPassImportExport for WarpIpfs {
} => {
let keypair = warp::crypto::keypair::did_from_mnemonic(&passphrase, None)?;

let bytes = Zeroizing::new(keypair.private_key_bytes());
let internal_keypair = rust_ipfs::Keypair::ed25519_from_bytes(bytes)
.map_err(|_| Error::PrivateKeyInvalid)?;

let bytes = std::mem::take(buffer);

let decrypted_bundle = ecdh_decrypt(&keypair, None, bytes)?;
let decrypted_bundle = ecdh_decrypt(&internal_keypair, None, bytes)?;

let exported_document =
serde_json::from_slice::<ResolvedRootDocument>(&decrypted_bundle)?;

exported_document.verify()?;

let bytes = Zeroizing::new(keypair.private_key_bytes());

warp::crypto::keypair::mnemonic_into_tesseract(
&mut self.tesseract,
&passphrase,
Expand All @@ -1217,11 +1219,7 @@ impl MultiPassImportExport for WarpIpfs {
false,
)?;

self.init_ipfs(
rust_ipfs::Keypair::ed25519_from_bytes(bytes)
.map_err(|_| Error::PrivateKeyInvalid)?,
)
.await?;
self.init_ipfs(internal_keypair).await?;

let mut store = self.identity_store(false).await?;

Expand All @@ -1233,6 +1231,9 @@ impl MultiPassImportExport for WarpIpfs {
} => {
let keypair = warp::crypto::keypair::did_from_mnemonic(&passphrase, None)?;
let bytes = Zeroizing::new(keypair.private_key_bytes());
let internal_keypair = rust_ipfs::Keypair::ed25519_from_bytes(bytes)
.map_err(|_| Error::PrivateKeyInvalid)?;

warp::crypto::keypair::mnemonic_into_tesseract(
&mut self.tesseract,
&passphrase,
Expand All @@ -1241,11 +1242,7 @@ impl MultiPassImportExport for WarpIpfs {
false,
)?;

self.init_ipfs(
rust_ipfs::Keypair::ed25519_from_bytes(bytes)
.map_err(|_| Error::PrivateKeyInvalid)?,
)
.await?;
self.init_ipfs(internal_keypair).await?;

let mut store = self.identity_store(false).await?;

Expand Down
Loading