Skip to content

Commit

Permalink
finish gpg webkey parse impl
Browse files Browse the repository at this point in the history
output still needs to be checked; testing
  • Loading branch information
fairingrey committed Jan 11, 2022
1 parent 4970859 commit 76c8592
Showing 1 changed file with 33 additions and 3 deletions.
36 changes: 33 additions & 3 deletions did-webkey/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use core::str::FromStr;

use async_trait::async_trait;
use openpgp::packet::key::KeyRole;
use serde::{Deserialize, Serialize};

use openpgp::parse::{PacketParser, PacketParserResult, Parse};
Expand Down Expand Up @@ -46,7 +47,6 @@ fn parse_pubkeys_gpg(
did: &str,
bytes: Vec<u8>,
) -> Result<(Vec<VerificationMethodMap>, Vec<DIDURL>), String> {
// TODO
let mut ppr = PacketParser::from_bytes(&bytes)
.map_err(|e| format!("Unable to parse GPG keyring: {}", e))?;
let did_urls = Vec::new();
Expand All @@ -60,14 +60,44 @@ fn parse_pubkeys_gpg(

// packet is expected to be a public key
if let Packet::PublicKey(pk) = packet {
dbg!(pubkey);
// TODO: map did_url and vm here
let (vm_map, did_url) = gpg_pk_to_vm(did, pk).map_err(|e| {
format!(
"Unable to convert GPG public key to verification method: {}",
e
)
})?;
vm_maps.push(vm_map);
did_urls.push(did_url);
}
}

Ok((vm_maps, did_urls))
}

fn gpg_pk_to_vm<P: KeyParts, R: KeyRole>(
did: &str,
pk: Key<P, R>,
) -> Result<(Vec<VerificationMethodMap>, Vec<DIDURL>), String> {
let jwk =
gpg_pkk_to_jwk(&pk).map_err(|e| format!("Unable to convert GPG key to JWK: {}", e))?;
let thumbprint = jwk
.thumbprint()
.map_err(|e| format!("Unable to calculate JWK thumbprint: {}", e))?;
let vm_url = DIDURL {
did: did.to_string(),
fragment: Some(thumbprint),
..Default::default()
};
let vm_map = VerificationMethodMap {
id: vm_url.to_string(),
type_: "PgpVerificationKey2021".to_string(),
public_key_jwk: Some(jwk),
controller: did.to_string(),
..Default::default()
};
Ok((vm_map, vm_url))
}

fn pk_to_vm_ed25519(
did: &str,
pk: sshkeys::Ed25519PublicKey,
Expand Down

0 comments on commit 76c8592

Please sign in to comment.