Skip to content

Commit

Permalink
Merge pull request #496 from coasys/fix-bootstrap-publishing
Browse files Browse the repository at this point in the history
Fix bootstrap publishing
  • Loading branch information
lucksus authored Jul 2, 2024
2 parents a61ddae + 7867b15 commit a4f02e1
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
19 changes: 17 additions & 2 deletions rust-executor/src/agent/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ pub fn did() -> String {
did_document().id.clone()
}

pub fn check_keys_and_create(did: String) -> did_key::Document {
let wallet_instance = Wallet::instance();
let mut wallet = wallet_instance.lock().expect("wallet lock");
let mut wallet_ref = wallet.as_mut().expect("wallet instance");
let name = "main".to_string();
if wallet_ref.get_did_document(&name).is_none() {
wallet_ref.initialize_keys(name, did).unwrap()
} else {
did_document()
}
}

pub fn create_signed_expression<T: Serialize>(data: T) -> Result<Expression<T>, AnyError> {
let timestamp = chrono::Utc::now();
let signature = hex::encode(sign(&signatures::hash_data_and_timestamp(
Expand Down Expand Up @@ -293,7 +305,7 @@ impl AgentService {
let file = std::fs::read_to_string(self.file.as_str()).expect("Failed to read agent file");
let dump: AgentStore = serde_json::from_str(&file).unwrap();

self.did = Some(dump.did);
self.did = Some(dump.did.clone());
self.did_document = Some(dump.did_document);
self.signing_key_id = Some(dump.signing_key_id);

Expand All @@ -313,8 +325,11 @@ impl AgentService {
self.agent =
Some(serde_json::from_str(&file_profile).expect("Failed to parse agent profile"));
} else {
let did_clone = dump.did.clone();
let did = check_keys_and_create(did_clone).id.clone();

self.agent = Some(Agent {
did: did(),
did,
perspective: Some(Perspective { links: vec![] }),
direct_message_language: None,
});
Expand Down
17 changes: 17 additions & 0 deletions rust-executor/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,23 @@ impl Wallet {
.insert(name, Key::from(key));
}

pub fn initialize_keys(&mut self, name: String, did: String) -> Option<did_key::Document>{
if self.keys.is_none() {
self.keys = Some(Keys::new());
let key = did_key::resolve(did.as_str()).expect("Failed to get key pair");
self.keys
.as_mut()
.unwrap()
.by_name
.insert(name.clone(), Key::from(key));
let key = did_key::resolve(did.as_str()).expect("Failed to get key pair");
let did_document = key.get_did_document(did_key::Config::default());
Some(did_document)
} else {
None
}
}

pub fn get_public_key(&self, name: &String) -> Option<Vec<u8>> {
self.keys
.as_ref()?
Expand Down

0 comments on commit a4f02e1

Please sign in to comment.