Skip to content

Commit

Permalink
Merge pull request #27 from boschresearch/fix/expand-verkey
Browse files Browse the repository at this point in the history
Fix implementation for the indy verkey expansion
  • Loading branch information
c2bo authored Oct 25, 2022
2 parents b26f765 + 4da5ffc commit 277329f
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 6 deletions.
3 changes: 2 additions & 1 deletion about.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ accepted = [
"MIT",
"CC0-1.0",
"BSD-3-Clause",
"Zlib"
"Zlib",
"Unicode-DFS-2016"
]
2 changes: 1 addition & 1 deletion docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ARG RUST_VER=1.58
ARG RUST_VER=1.63

FROM rust:${RUST_VER}-buster as builder
USER root
Expand Down
1 change: 1 addition & 0 deletions indy-didresolver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ thiserror = "1.0"
url = "2.2.2"
urlencoding = "2.1.0"
log = "0.4.0"
bs58 = "0.4.0"

[dev-dependencies]
rstest = "0.12"
94 changes: 90 additions & 4 deletions indy-didresolver/src/did_document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,52 @@ pub struct DidDocument {
diddoc_content: Option<Value>,
}

pub fn expand_verkey(id: &str, verkey: &str) -> String {
if verkey.starts_with('~') {
format!("{}{}", id, &verkey[1..])
// Returns raw verkey in case of errors, otherwise 'default' indy handling
pub fn expand_verkey(id :&str, verkey: &str) -> String {
let expanded_verkey = expand_verkey_internal(id, verkey);
if expanded_verkey.is_err(){
return verkey.to_string();
}
return expanded_verkey.unwrap();
}

pub fn expand_verkey_internal(id: &str, verkey: &str) -> Result<String, DidIndyError> {
// separate optional crypto_type from verkey part
let (key, key_type) = if verkey.contains(":") {
let vec: Vec<&str> = verkey.split(":").collect();
match vec.len() {
0 | 1 => {
return Err(DidIndyError::UnexpectedKeyFormat);
},
_ => {
let key = vec[0];
let key_type = vec[vec.len() - 1];
if key.len() == 0 || key_type.len() == 0 {
return Err(DidIndyError::UnexpectedKeyFormat);
}
(key, Some(key_type))
},
}
} else {
(verkey, None)
};
// Decode parts from base58 and re-encode
let mut result: String;
if key.starts_with("~") && key.len() >= 2 {
let mut decoded = bs58::decode(id).into_vec()?;
let mut keys = key.chars();
keys.next();
let mut key_decoded = bs58::decode( keys.as_str()).into_vec()?;
decoded.append(&mut key_decoded);
result = bs58::encode(decoded).into_string();
} else {
verkey.to_string()
result = verkey.to_string();
};
// Add key type if it was used
if key_type.is_some() && key_type.unwrap() != "" {
result = format!("{}:{}", result, key_type.unwrap());
}
Ok(result)
}

impl DidDocument {
Expand Down Expand Up @@ -193,6 +233,52 @@ mod tests {

use super::*;

#[test]
fn expand_verkey_no_type() {
let id = "V4SGRU86Z58d6TV7PBUe6f";
let verkey = "~CoRER63DVYnWZtK8uAzNbx";
let expected_verkey = "GJ1SzoWzavQYfNL9XkaJdrQejfztN4XqdsiV4ct3LXKL";

let expanded_verkey = expand_verkey(id, verkey);
assert_eq!(expanded_verkey, expected_verkey)
}

#[test]
fn expand_verkey_key_type() {
let id = "V4SGRU86Z58d6TV7PBUe6f";
let verkey = "~CoRER63DVYnWZtK8uAzNbx:ed25519";
let expected_verkey = "GJ1SzoWzavQYfNL9XkaJdrQejfztN4XqdsiV4ct3LXKL:ed25519";

let expanded_verkey = expand_verkey(id, verkey);
assert_eq!(expanded_verkey, expected_verkey)
}

#[test]
fn expand_verkey_key_type_edgecase_empty() {
let id = "V4SGRU86Z58d6TV7PBUe6f";
let verkey = ":ed25519";

let expanded_verkey = expand_verkey(id, verkey);
assert_eq!(expanded_verkey, verkey)
}

#[test]
fn expand_verkey_key_type_edgecase_nothing() {
let id = "V4SGRU86Z58d6TV7PBUe6f";
let verkey = ":";

let expanded_verkey = expand_verkey(id, verkey);
assert_eq!(expanded_verkey, verkey)
}

#[test]
fn expand_verkey_invalid() {
let id = "V4SGRU86Z58d6TV7PBUe6f";
let verkey = "~CoRER63DVYnWZtK8uAzNb0";
let expanded_verkey = expand_verkey(id, verkey);
assert_eq!(expanded_verkey, verkey)
}

#[test]
fn serialze_diddoc_without_diddoc_content() {
let doc = DidDocument::new(
Expand Down
4 changes: 4 additions & 0 deletions indy-didresolver/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ pub enum DidIndyError {
NotImplemented,
#[error("VDR error")]
VdrError(#[from] VdrError),
#[error("Base58 Parsing error")]
FromBase58Error(#[from] bs58::decode::Error),
#[error("Unexpected Key Format")]
UnexpectedKeyFormat
}

// impl fmt::Display for DidIndyError {
Expand Down

0 comments on commit 277329f

Please sign in to comment.