Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support JWK public key VM processing into Key #1297

Merged
merged 7 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,047 changes: 540 additions & 507 deletions Cargo.lock

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,13 @@ unused_import_braces = "warn"
unused_lifetimes = "warn"
unused_qualifications = "warn"
let_underscore_drop = "allow"

[workspace.dependencies]
indy-vdr = { git = "https://github.com/hyperledger/indy-vdr.git", tag = "v0.4.3", default-features = false, features = [
"log",
] }
indy-vdr-proxy-client = { git = "https://github.com/hyperledger/indy-vdr.git", tag = "v0.4.3" }
indy-credx = { git = "https://github.com/hyperledger/indy-shared-rs", tag = "v1.1.0" }
anoncreds = { git = "https://github.com/hyperledger/anoncreds-rs.git", tag = "v0.2.0" }
aries-askar = { version = "0.3.1" }
askar-crypto = { version = "0.3.1", default-features = false }
4 changes: 2 additions & 2 deletions aries/aries_vcx_anoncreds/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ anoncreds = ["dep:anoncreds"]
legacy_proof = []

[dependencies]
indy-credx = { git = "https://github.com/hyperledger/indy-shared-rs", tag = "v1.1.0", optional = true }
anoncreds = { git = "https://github.com/hyperledger/anoncreds-rs.git", tag = "v0.2.0", optional = true }
indy-credx = { workspace = true, optional = true }
anoncreds = { workspace = true, optional = true }
aries_vcx_wallet = { path = "../aries_vcx_wallet" }
anoncreds_types = { path = "../misc/anoncreds_types" }
did_parser_nom = { path = "../../did_core/did_parser_nom" }
Expand Down
4 changes: 2 additions & 2 deletions aries/aries_vcx_ledger/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ aries_vcx_wallet = { path = "../aries_vcx_wallet" }
anoncreds_types = { path = "../misc/anoncreds_types" }
did_parser_nom = { path = "../../did_core/did_parser_nom" }
thiserror = "1.0.40"
indy-vdr = { git = "https://github.com/hyperledger/indy-vdr.git", rev = "c143268", default-features = false, features = ["log"] }
indy-vdr-proxy-client = { git = "https://github.com/hyperledger/indy-vdr.git", rev = "c143268", optional = true }
indy-vdr.workspace = true
indy-vdr-proxy-client = { workspace = true, optional = true }
serde_json = "1.0.95"
public_key = { path = "../../did_core/public_key"}
async-trait = "0.1.68"
Expand Down
2 changes: 1 addition & 1 deletion aries/aries_vcx_ledger/src/errors/mapping_indyvdr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ impl From<VdrError> for VcxLedgerError {
match err.kind() {
VdrErrorKind::Config => Self::InvalidConfiguration(err),
VdrErrorKind::Connection => Self::PoolLedgerConnect(err),
VdrErrorKind::FileSystem(_) => Self::IOError(err),
VdrErrorKind::FileSystem => Self::IOError(err),
VdrErrorKind::Input => Self::InvalidInput(err.to_string()),
VdrErrorKind::Resource
| VdrErrorKind::Unavailable
Expand Down
25 changes: 9 additions & 16 deletions aries/aries_vcx_ledger/src/ledger/request_submitter/vdr_ledger.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::{
collections::{hash_map::RandomState, HashMap},
collections::HashMap,
fmt::{Debug, Formatter},
sync::Arc,
};
Expand All @@ -8,7 +8,10 @@ use async_trait::async_trait;
use indy_vdr::{
common::error::VdrError,
config::PoolConfig,
pool::{PoolBuilder, PoolRunner, PoolTransactions, PreparedRequest, RequestResult},
pool::{
PoolBuilder, PoolRunner, PoolTransactions, PreparedRequest, RequestResult,
RequestResultMeta,
},
};
use log::info;
use tokio::sync::oneshot;
Expand Down Expand Up @@ -45,13 +48,9 @@ impl IndyVdrLedgerPool {
{indy_vdr_config:?}"
);
let txns = PoolTransactions::from_json_file(genesis_file_path)?;
let runner = PoolBuilder::new(
indy_vdr_config,
None,
Some(Self::generate_exclusion_weights(exclude_nodes)),
)
.transactions(txns)?
.into_runner()?;
let runner = PoolBuilder::new(indy_vdr_config, txns)
.node_weights(Some(Self::generate_exclusion_weights(exclude_nodes)))
.into_runner(None)?;

Ok(IndyVdrLedgerPool {
runner: Arc::new(runner),
Expand Down Expand Up @@ -83,13 +82,7 @@ impl RequestSubmitter for IndyVdrSubmitter {
async fn submit(&self, request: PreparedRequest) -> VcxLedgerResult<String> {
// indyvdr send_request is Async via a callback.
// Use oneshot channel to send result from callback, converting the fn to future.
type VdrSendRequestResult = Result<
(
RequestResult<String>,
Option<HashMap<String, f32, RandomState>>,
),
VdrError,
>;
type VdrSendRequestResult = Result<(RequestResult<String>, RequestResultMeta), VdrError>;
let (sender, recv) = oneshot::channel::<VdrSendRequestResult>();
self.pool.runner.send_request(
request,
Expand Down
6 changes: 3 additions & 3 deletions aries/aries_vcx_wallet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ edition.workspace = true

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[features]
vdrtools_wallet = [ "dep:libvdrtools", "dep:indy-api-types"]
vdrtools_wallet = ["dep:libvdrtools", "dep:indy-api-types"]
askar_wallet = ["dep:aries-askar"]

[dependencies]
anyhow = "1.0"
aries-askar = { version = "0.3.1", optional = true }
aries-askar = { workspace = true, optional = true }
async-trait = "0.1.68"
bs58 = { version = "0.5" }
base64 = "0.22.1"
Expand All @@ -22,7 +22,7 @@ log = "0.4.17"
indy-api-types = { path = "../misc/legacy/libvdrtools/indy-api-types", optional = true }
serde = { version = "1.0.159", features = ["derive"] }
serde_json = "1.0.95"
public_key = { path = "../../did_core/public_key"}
public_key = { path = "../../did_core/public_key" }
rand = "0.8.5"
thiserror = "1.0.40"
tokio = { version = "1.38" }
Expand Down
2 changes: 1 addition & 1 deletion aries/misc/indy_ledger_response_parser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ edition = "2021"
serde = { version = "1.0.163", features = ["derive"] }
serde_json = "1.0.96"
time = "0.3.20"
indy-vdr = { git = "https://github.com/hyperledger/indy-vdr.git", rev = "c143268", default-features = false, features = ["log"] }
indy-vdr.workspace = true
thiserror = "1.0.44"
anoncreds-clsignatures = "0.3.2"
9 changes: 7 additions & 2 deletions aries/misc/test_utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ edition.workspace = true

[features]
vdrtools_wallet = ["aries_vcx_wallet/vdrtools_wallet"]
vdr_proxy_ledger = ["aries_vcx_ledger/vdr_proxy_ledger", "credx", "dep:indy-ledger-response-parser", "dep:indy-vdr-proxy-client"]
vdr_proxy_ledger = [
"aries_vcx_ledger/vdr_proxy_ledger",
"credx",
"dep:indy-ledger-response-parser",
"dep:indy-vdr-proxy-client",
]
credx = ["aries_vcx_anoncreds/credx"]
anoncreds = ["aries_vcx_anoncreds/anoncreds"]

Expand All @@ -22,7 +27,7 @@ aries_vcx_wallet = { path = "../../aries_vcx_wallet" }
aries_vcx_ledger = { path = "../../aries_vcx_ledger" }
public_key = { path = "../../../did_core/public_key" }
indy-ledger-response-parser = { path = "../indy_ledger_response_parser", optional = true }
indy-vdr-proxy-client = { git = "https://github.com/hyperledger/indy-vdr.git", rev = "c143268", optional = true }
indy-vdr-proxy-client = { workspace = true, optional = true }
lazy_static = "1"
serde_json = "1"
rand = "0.8"
Expand Down
2 changes: 1 addition & 1 deletion aries/misc/wallet_migrator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ askar_wallet = ["aries_vcx_wallet/askar_wallet"]

[dependencies]
aries_vcx_wallet = { path = "../../aries_vcx_wallet" }
credx = { package = "indy-credx", git = "https://github.com/hyperledger/indy-shared-rs", tag = "v1.1.0" }
indy-credx.workspace = true
vdrtools = { package = "libvdrtools", path = "../legacy/libvdrtools" }
serde = { version = "1.0.159", features = ["derive"] }
serde_json = "1.0.96"
Expand Down
2 changes: 1 addition & 1 deletion aries/wrappers/uniffi-aries-vcx/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ aries_vcx = { path = "../../../aries_vcx", features = [
] }
aries_vcx_ledger = { path = "../../../aries_vcx_ledger" }
aries_vcx_anoncreds = { path = "../../../aries_vcx_anoncreds" }
indy-vdr = { git = "https://github.com/hyperledger/indy-vdr.git", rev = "c143268", default-features = false, features = ["log"] }
indy-vdr.workspace = true
tokio = { version = "1.38.0", features = ["rt-multi-thread"] }
once_cell = "1.17.0"
thiserror = "1.0.38"
Expand Down
3 changes: 3 additions & 0 deletions did_core/did_doc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ name = "did_doc"
version = "0.1.0"
edition = "2021"

[features]
jwk = ["public_key/jwk"]

[dependencies]
base64 = "0.22.1"
bs58 = "0.5.0"
Expand Down
52 changes: 52 additions & 0 deletions did_core/did_doc/src/schema/verification_method/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ impl VerificationMethod {
PublicKeyField::Multibase {
public_key_multibase,
} => Key::from_fingerprint(public_key_multibase)?,
#[cfg(feature = "jwk")]
PublicKeyField::Jwk { public_key_jwk } => Key::from_jwk(&public_key_jwk.to_string())?,
// TODO - FUTURE - other key types could do with some special handling, i.e.
// those where the key_type is encoded within the key field (multibase, jwk, etc)
_ => Key::new(
Expand Down Expand Up @@ -152,3 +154,53 @@ mod tests {
assert!(vm.is_err());
}
}

#[cfg(feature = "jwk")]
#[cfg(test)]
mod jwk_tests {
use ::public_key::KeyType;
use serde_json::json;

use super::*;

#[test]
fn test_public_key_from_ed25519_jwk_vm() {
let vm: VerificationMethod = serde_json::from_value(json!({
"id": "did:key:z6MkpTHR8VNsBxYAAWHut2Geadd9jSwuBV8xRoAnwWsdvktH#z6MkpTHR8VNsBxYAAWHut2Geadd9jSwuBV8xRoAnwWsdvktH",
"type": "Ed25519VerificationKey2018",
"controller": "did:key:z6MkpTHR8VNsBxYAAWHut2Geadd9jSwuBV8xRoAnwWsdvktH",
"publicKeyJwk": {
"kty": "OKP",
"crv": "Ed25519",
"x": "lJZrfAjkBXdfjebMHEUI9usidAPhAlssitLXR3OYxbI"
}
})).unwrap();
let pk = vm.public_key().unwrap();
assert!(matches!(pk.key_type(), KeyType::Ed25519));
assert_eq!(
pk.fingerprint(),
"z6MkpTHR8VNsBxYAAWHut2Geadd9jSwuBV8xRoAnwWsdvktH"
)
}

#[test]
fn test_public_key_from_p256_jwk_vm() {
let vm: VerificationMethod = serde_json::from_value(json!({
"id": "did:key:zDnaerDaTF5BXEavCrfRZEk316dpbLsfPDZ3WJ5hRTPFU2169#zDnaerDaTF5BXEavCrfRZEk316dpbLsfPDZ3WJ5hRTPFU2169",
"type": "JsonWebKey2020",
"controller": "did:key:zDnaerDaTF5BXEavCrfRZEk316dpbLsfPDZ3WJ5hRTPFU2169",
"publicKeyJwk": {
"kty": "EC",
"crv": "P-256",
"x": "fyNYMN0976ci7xqiSdag3buk-ZCwgXU4kz9XNkBlNUI",
"y": "hW2ojTNfH7Jbi8--CJUo3OCbH3y5n91g-IMA9MLMbTU"
}
})).unwrap();
let pk = vm.public_key().unwrap();
assert!(matches!(pk.key_type(), KeyType::P256));
assert_eq!(
pk.fingerprint(),
"zDnaerDaTF5BXEavCrfRZEk316dpbLsfPDZ3WJ5hRTPFU2169"
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ impl TryFrom<VerificationMethodType> for KeyType {
VerificationMethodType::Bls12381G2Key2020 => Ok(KeyType::Bls12381g2),
VerificationMethodType::X25519KeyAgreementKey2019
| VerificationMethodType::X25519KeyAgreementKey2020 => Ok(KeyType::X25519),
// The verification method type does not map directly to a key type.
// This may occur when the VM type is a multikey (JsonWebKey, Multikey, etc)
_ => Err(DidDocumentBuilderError::UnsupportedVerificationMethodType(
value,
)),
Expand Down
10 changes: 10 additions & 0 deletions did_core/public_key/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ name = "public_key"
version = "0.1.0"
edition = "2021"

[features]
jwk = ["dep:askar-crypto"]

[dependencies]
thiserror = "1.0.40"
serde = { version = "1.0.164", features = ["derive"] }
Expand All @@ -11,3 +14,10 @@ base64 = "0.22.1"
bs58 = "0.5.0"
multibase = "0.9.1"
unsigned-varint = "0.8.0"
# askar-crypto used for jwk conversion. maintain minimal feature set
askar-crypto = { workspace = true, features = [
"std",
"any_key",
"ec_curves",
"ed25519",
], optional = true }
6 changes: 6 additions & 0 deletions did_core/public_key/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::error::Error;

use thiserror::Error;

#[derive(Debug, Error)]
Expand All @@ -10,8 +12,12 @@ pub enum PublicKeyError {
MultibaseDecodingError(#[from] multibase::Error),
#[error("Varint decoding error")]
VarintDecodingError(#[from] VarintDecodingError),
#[error("JWK decoding error")]
JwkDecodingError(#[from] Box<dyn Error + Send + Sync>),
#[error("Unsupported multicodec descriptor: {0}")]
UnsupportedMulticodecDescriptor(u64),
#[error("Unsupported multicodec descriptor: {0}")]
UnsupportedKeyType(String),
}

#[derive(Debug, Error)]
Expand Down
100 changes: 100 additions & 0 deletions did_core/public_key/src/jwk.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
use askar_crypto::{
alg::{AnyKey, BlsCurves, EcCurves},
jwk::FromJwk,
repr::ToPublicBytes,
};

use crate::{Key, KeyType, PublicKeyError};

impl Key {
pub fn from_jwk(jwk: &str) -> Result<Key, PublicKeyError> {
let askar_key: Box<AnyKey> =
FromJwk::from_jwk(jwk).map_err(|e| PublicKeyError::JwkDecodingError(Box::new(e)))?;

let askar_alg = askar_key.algorithm();
let pub_key_bytes = askar_key
.to_public_bytes()
.map_err(|e| PublicKeyError::JwkDecodingError(Box::new(e)))?
.to_vec();

let key_type = match askar_alg {
askar_crypto::alg::KeyAlg::Ed25519 => KeyType::Ed25519,
askar_crypto::alg::KeyAlg::Bls12_381(BlsCurves::G1G2) => KeyType::Bls12381g1g2,
askar_crypto::alg::KeyAlg::Bls12_381(BlsCurves::G1) => KeyType::Bls12381g1,
askar_crypto::alg::KeyAlg::Bls12_381(BlsCurves::G2) => KeyType::Bls12381g2,
askar_crypto::alg::KeyAlg::X25519 => KeyType::X25519,
askar_crypto::alg::KeyAlg::EcCurve(EcCurves::Secp256r1) => KeyType::P256,
askar_crypto::alg::KeyAlg::EcCurve(EcCurves::Secp384r1) => KeyType::P384,
_ => return Err(PublicKeyError::UnsupportedKeyType(askar_alg.to_string())),
};

Key::new(pub_key_bytes, key_type)
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_from_ed25519_jwk() {
// vector from https://w3c-ccg.github.io/did-method-key/#ed25519-x25519
let jwk = r#"{
"kty": "OKP",
"crv": "Ed25519",
"x": "O2onvM62pC1io6jQKm8Nc2UyFXcd4kOmOsBIoYtZ2ik"
}"#;
let key = Key::from_jwk(jwk).unwrap();
assert_eq!(
key.fingerprint(),
"z6MkiTBz1ymuepAQ4HEHYSF1H8quG5GLVVQR3djdX3mDooWp"
);
}

#[test]
fn test_from_x25519_jwk() {
// vector from https://w3c-ccg.github.io/did-method-key/#ed25519-x25519
let jwk = r#"{
"kty": "OKP",
"crv": "X25519",
"x": "W_Vcc7guviK-gPNDBmevVw-uJVamQV5rMNQGUwCqlH0"
}"#;
let key = Key::from_jwk(jwk).unwrap();
assert_eq!(
key.fingerprint(),
"z6LShs9GGnqk85isEBzzshkuVWrVKsRp24GnDuHk8QWkARMW"
);
}

#[test]
fn test_from_p256_jwk() {
// vector from https://dev.uniresolver.io/
let jwk = r#"{
"kty": "EC",
"crv": "P-256",
"x": "fyNYMN0976ci7xqiSdag3buk-ZCwgXU4kz9XNkBlNUI",
"y": "hW2ojTNfH7Jbi8--CJUo3OCbH3y5n91g-IMA9MLMbTU"
}"#;
let key = Key::from_jwk(jwk).unwrap();
assert_eq!(
key.fingerprint(),
"zDnaerDaTF5BXEavCrfRZEk316dpbLsfPDZ3WJ5hRTPFU2169"
);
}

#[test]
fn test_from_p384_jwk() {
// vector from https://dev.uniresolver.io/
let jwk = r#"{
"kty": "EC",
"crv": "P-384",
"x": "bKq-gg3sJmfkJGrLl93bsumOTX1NubBySttAV19y5ClWK3DxEmqPy0at5lLqBiiv",
"y": "PJQtdHnInU9SY3e8Nn9aOPoP51OFbs-FWJUsU0TGjRtZ4bnhoZXtS92wdzuAotL9"
}"#;
let key = Key::from_jwk(jwk).unwrap();
assert_eq!(
key.fingerprint(),
"z82Lkytz3HqpWiBmt2853ZgNgNG8qVoUJnyoMvGw6ZEBktGcwUVdKpUNJHct1wvp9pXjr7Y"
);
}
}
2 changes: 2 additions & 0 deletions did_core/public_key/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
mod error;
#[cfg(feature = "jwk")]
mod jwk;
mod key;
mod key_type;

Expand Down
Loading
Loading