Skip to content

Commit

Permalink
refactor: Use keypair internally within warp-ipfs (#522)
Browse files Browse the repository at this point in the history
  • Loading branch information
dariusc93 authored May 23, 2024
1 parent 32c64be commit 3c308f1
Show file tree
Hide file tree
Showing 13 changed files with 553 additions and 522 deletions.
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

0 comments on commit 3c308f1

Please sign in to comment.