-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
89 changed files
with
2,822 additions
and
2,289 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# Database crate | ||
|
||
A lightweight library for managing MongoDB operations. This library provides an interface, the `Repository` trait with default implementations for interacting with MongoDB collections. It is used by the plugins in the workspace that require database access. | ||
This crate is part of the [DIDComm mediator](https://github.com/adorsys/didcomm-mediator-rs) project. | ||
|
||
## Usage | ||
|
||
### Requirements | ||
|
||
* [MongoDB](https://www.mongodb.com) server instance | ||
* Environment variables: | ||
* `MONGO_URI`: MongoDB connection string | ||
* `MONGO_DBN`: Database name | ||
|
||
### Example | ||
|
||
* Define an entity | ||
|
||
```rust | ||
use database::Repository; | ||
|
||
#[derive(Debug, Clone, Serialize, Deserialize)] | ||
struct MyEntity { | ||
#[serde(rename = "_id", skip_serializing_if = "Option::is_none")] | ||
id: Option<ObjectId>, | ||
name: String, | ||
} | ||
|
||
impl Identifiable for MyEntity { | ||
fn id(&self) -> Option<ObjectId> { | ||
self.id.clone() | ||
} | ||
|
||
fn set_id(&mut self, id: ObjectId) { | ||
self.id = Some(id); | ||
} | ||
} | ||
``` | ||
|
||
* Implement the `Repository` trait(the only required method is `get_collection`) | ||
|
||
```rust | ||
struct MyEntityRepository { | ||
collection: Arc<RwLock<Collection<MyEntity>>>, | ||
} | ||
|
||
#[async_trait] | ||
impl Repository<MyEntity> for MyEntityRepository { | ||
fn get_collection(&self) -> Arc<RwLock<Collection<MyEntity>>> { | ||
self.collection.clone() | ||
} | ||
} | ||
``` | ||
|
||
* Use the repository | ||
|
||
```rust | ||
let db = get_or_init_database(); | ||
let repo = MyEntityRepository { | ||
collection: Arc::new(RwLock::new(db.read().await.collection("my_entities"))), | ||
}; | ||
let entity = MyEntity { id: None, name: "example".to_string() }; | ||
repo.store(entity).await?; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Keystore Crate | ||
|
||
The `keystore` crate is a utility library for managing cryptographic secrets. It is used in the [Didcomm Mediator](https://github.com/adorsys/didcomm-mediator-rs/) to store and retrieve cryptographic keys for DIDcomm interactions. | ||
|
||
## Usage | ||
|
||
This crate is internal to the [Didcomm Mediator](https://github.com/adorsys/didcomm-mediator-rs/). Below is an example of interacting with the keystore: | ||
|
||
```rust | ||
use keystore::{KeyStore, Secrets}; | ||
use mongodb::bson::{doc, Bson, Document}; | ||
use did_utils::jwk::Jwk; | ||
|
||
// Initialize the keystore | ||
let keystore = KeyStore::get(); | ||
|
||
let jwk: Jwk = serde_json::from_str( | ||
r#"{ | ||
"kty": "OKP", | ||
"crv": "X25519", | ||
"x": "SHSUZ6V3x355FqCzIUfgoPzrZB0BQs0JKyag4UfMqHQ", | ||
"d": "0A8SSFkGHg3N9gmVDRnl63ih5fcwtEvnQu9912SVplY" | ||
}"#, | ||
) | ||
.unwrap(); | ||
|
||
// Store a secret | ||
let secret = Secrets { | ||
id: Some(ObjectId::new()), | ||
kid: "key-1".to_string(), | ||
secret_material: jwk, | ||
}; | ||
keystore.store(secret).await?; | ||
|
||
// Retrieve a secret by ID | ||
let secret = keystore.find_one(doc! {"kid": "key-1"}).await?; | ||
|
||
// Delete a secret by ID | ||
keystore.delete_one(secret.id.unwrap()).await?; | ||
``` |
Oops, something went wrong.