Skip to content

Commit 30e12fe

Browse files
author
Harry Kim
committed
If DID method name is 'infra', fail if network id is not given
1 parent 31b944e commit 30e12fe

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

src/did/mod.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,15 @@ pub fn generate_ss58_did_from_phrase(
122122

123123
pub fn did_to_hex_public_key(did: String, address_type: AddressType) -> Result<String, Error> {
124124
let splited_did: Vec<&str> = did.split(":").collect();
125-
let address = splited_did[3];
125+
let method_name = splited_did[1]; // DID method name. See DID spec document
126+
let address = if method_name == "infra" {
127+
if splited_did.len() < 4 {
128+
return Err(Error::InvalidDID)
129+
}
130+
splited_did[3]
131+
} else {
132+
todo!()
133+
};
126134

127135
let decoded_address = bs58::decode(address).into_vec()?;
128136

@@ -222,6 +230,18 @@ mod tests {
222230
);
223231
}
224232

233+
#[test]
234+
fn test_wrong_did_format() {
235+
// DID method name "infra" requires format of {network_name}:{public_key}
236+
assert!(
237+
did_to_hex_public_key(
238+
"did:infra:5GM7RtekqU8cGiS4MKQ7tufoH4Q1itzmoFpVcvcPfjksyPrw".to_string(),
239+
AddressType::Ed25519
240+
)
241+
.is_err()
242+
)
243+
}
244+
225245
#[test]
226246
fn test_ss58_address_to_did() {
227247
assert_eq!(

0 commit comments

Comments
 (0)