Skip to content

Commit e3daf74

Browse files
author
Harry Kim
committed
Add API: DID-to-public-key-byte
1 parent 30e12fe commit e3daf74

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

src/did/mod.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ pub fn did_to_hex_public_key(did: String, address_type: AddressType) -> Result<S
132132
todo!()
133133
};
134134

135-
let decoded_address = bs58::decode(address).into_vec()?;
135+
let decoded_address: Vec<u8> = bs58::decode(address).into_vec()?;
136136

137137
let public_key_bytes: [u8; 32] = match address_type {
138138
AddressType::Ed25519 => {
@@ -150,6 +150,22 @@ pub fn did_to_hex_public_key(did: String, address_type: AddressType) -> Result<S
150150
Ok(hex::encode(public_key_bytes))
151151
}
152152

153+
pub fn did_to_public_key_bytes(did: String) -> Result<Vec<u8>, Error> {
154+
let splited_did: Vec<&str> = did.split(":").collect();
155+
let method_name = splited_did[1]; // DID method name. See DID spec document
156+
let address = if method_name == "infra" {
157+
if splited_did.len() < 4 {
158+
return Err(Error::InvalidDID)
159+
}
160+
splited_did[3]
161+
} else {
162+
todo!()
163+
};
164+
let decoded_address = bs58::decode(address).into_vec().expect("Can not decode address");
165+
166+
Ok(decoded_address)
167+
}
168+
153169
pub fn ss58_address_to_did(address: String, network_id: String) -> Result<String, Error> {
154170
let did = format!("did:infra:{}:{}", network_id, address);
155171
Ok(did)
@@ -242,6 +258,17 @@ mod tests {
242258
)
243259
}
244260

261+
#[test]
262+
fn test_did_to_bytesvec_public_key() {
263+
264+
let did = did_to_public_key_bytes("did:infra:01:5GM7RtekqU8cGiS4MKQ7tufoH4Q1itzmoFpVcvcPfjksyPrw".to_string()).unwrap();
265+
266+
assert_eq!(
267+
hex::encode(did),
268+
"bd7436a22571207d018ffe83f5dc77d0750b7777f1eb169053d40201d6c68d53".to_string()
269+
)
270+
}
271+
245272
#[test]
246273
fn test_ss58_address_to_did() {
247274
assert_eq!(

0 commit comments

Comments
 (0)