1
1
use askar_crypto:: alg:: {
2
2
ed25519:: Ed25519KeyPair , k256:: K256KeyPair , p256:: P256KeyPair , x25519:: X25519KeyPair ,
3
3
} ;
4
- use serde_json:: json;
4
+ use askar_crypto:: repr:: { KeyPublicBytes , KeySecretBytes } ;
5
+ use serde_json:: { json, Value } ;
5
6
use std:: io:: Cursor ;
6
7
use varint:: { VarintRead , VarintWrite } ;
7
8
@@ -298,16 +299,22 @@ impl AsKnownKeyPair for Secret {
298
299
. into_vec ( )
299
300
. to_didcomm ( "Wrong base58 value in secret material" ) ?;
300
301
301
- let curve25519_point_size = 32 ;
302
- let ( d_value, x_value) = decoded_value. split_at ( curve25519_point_size) ;
303
- let base64_url_d_value = base64:: encode_config ( & d_value, base64:: URL_SAFE_NO_PAD ) ;
304
- let base64_url_x_value = base64:: encode_config ( & x_value, base64:: URL_SAFE_NO_PAD ) ;
302
+ let key_pair = X25519KeyPair :: from_secret_bytes ( & decoded_value)
303
+ . kind ( ErrorKind :: Malformed , "Unable parse x25519 secret material" ) ?;
305
304
306
- let jwk = json ! ( {
305
+ let mut jwk = json ! ( {
307
306
"kty" : "OKP" ,
308
307
"crv" : "X25519" ,
309
- "x" : base64_url_x_value,
310
- "d" : base64_url_d_value
308
+ } ) ;
309
+
310
+ key_pair. with_public_bytes ( |buf| {
311
+ jwk[ "x" ] = Value :: String ( base64:: encode_config ( buf, base64:: URL_SAFE_NO_PAD ) )
312
+ } ) ;
313
+
314
+ key_pair. with_secret_bytes ( |buf| {
315
+ if let Some ( sk) = buf {
316
+ jwk[ "d" ] = Value :: String ( base64:: encode_config ( sk, base64:: URL_SAFE_NO_PAD ) )
317
+ }
311
318
} ) ;
312
319
313
320
X25519KeyPair :: from_jwk_value ( & jwk)
@@ -355,16 +362,22 @@ impl AsKnownKeyPair for Secret {
355
362
) ) ?
356
363
}
357
364
358
- let curve25519_point_size = 32 ;
359
- let ( d_value, x_value) = decoded_value. split_at ( curve25519_point_size) ;
360
- let base64_url_d_value = base64:: encode_config ( & d_value, base64:: URL_SAFE_NO_PAD ) ;
361
- let base64_url_x_value = base64:: encode_config ( & x_value, base64:: URL_SAFE_NO_PAD ) ;
365
+ let key_pair = X25519KeyPair :: from_secret_bytes ( & decoded_value)
366
+ . kind ( ErrorKind :: Malformed , "Unable parse x25519 secret material" ) ?;
362
367
363
- let jwk = json ! ( {
368
+ let mut jwk = json ! ( {
364
369
"kty" : "OKP" ,
365
370
"crv" : "X25519" ,
366
- "x" : base64_url_x_value,
367
- "d" : base64_url_d_value
371
+ } ) ;
372
+
373
+ key_pair. with_public_bytes ( |buf| {
374
+ jwk[ "x" ] = Value :: String ( base64:: encode_config ( buf, base64:: URL_SAFE_NO_PAD ) )
375
+ } ) ;
376
+
377
+ key_pair. with_secret_bytes ( |buf| {
378
+ if let Some ( sk) = buf {
379
+ jwk[ "d" ] = Value :: String ( base64:: encode_config ( sk, base64:: URL_SAFE_NO_PAD ) )
380
+ }
368
381
} ) ;
369
382
370
383
X25519KeyPair :: from_jwk_value ( & jwk)
@@ -473,16 +486,18 @@ mod tests {
473
486
let actual_key = Secret {
474
487
id : "did:example:eve#key-x25519-1" . to_string ( ) ,
475
488
type_ : SecretType :: X25519KeyAgreementKey2019 ,
476
- secret_material : ( SecretMaterial :: Base58 {
477
- value : "2b5J8uecvwAo9HUGge5NKQ7HoRNKUKCjZ7Fr4mDgWkwqFyjLPWt7rv5kL3UPeG3e4B9Sy4H2Q2zAuWcP2RNtgJ4t ". to_string ( )
489
+ secret_material : ( SecretMaterial :: Base58 {
490
+ value : "CMhHdN419VCSfbGxt6PEAEy72tJxYHTGHuCSH6BW9oi3 ". to_string ( ) ,
478
491
} ) ,
479
- } . as_key_pair ( ) . unwrap ( ) ;
492
+ }
493
+ . as_key_pair ( )
494
+ . unwrap ( ) ;
480
495
481
496
let expected_key = X25519KeyPair :: from_jwk_value ( & json ! ( {
482
497
"kty" : "OKP" ,
483
498
"crv" : "X25519" ,
484
- "x" : "piw5XSMkceDeklaHQZXPBLQySyAwF8eZ-vddihdURS0 " ,
485
- "d" : "T2azVap7CYD_kB8ilbnFYqwwYb5N-GcD6yjGEvquZXg "
499
+ "x" : "tGskN_ae61DP4DLY31_fjkbvnKqf-ze7kA6Cj2vyQxU " ,
500
+ "d" : "qL25gw-HkNJC9m4EsRzCoUx1KntjwHPzxo6a2xUcyFQ "
486
501
} ) )
487
502
. map ( KnownKeyPair :: X25519 )
488
503
. unwrap ( ) ;
@@ -494,10 +509,12 @@ mod tests {
494
509
let actual_key = Secret {
495
510
id : "did:example:eve#key-ed25519-1" . to_string ( ) ,
496
511
type_ : SecretType :: Ed25519VerificationKey2018 ,
497
- secret_material : ( SecretMaterial :: Base58 {
512
+ secret_material : ( SecretMaterial :: Base58 {
498
513
value : "2b5J8uecvwAo9HUGge5NKQ7HoRNKUKCjZ7Fr4mDgWkwqATnLmZDx7Seu6NqTuFKkxuHNT27GcoxVZQCkWJhNvaUQ" . to_string ( )
499
514
} ) ,
500
- } . as_key_pair ( ) . unwrap ( ) ;
515
+ }
516
+ . as_key_pair ( )
517
+ . unwrap ( ) ;
501
518
502
519
let expected_key = Ed25519KeyPair :: from_jwk_value ( & json ! ( {
503
520
"kty" : "OKP" ,
@@ -515,16 +532,18 @@ mod tests {
515
532
let actual_key = Secret {
516
533
id : "did:example:eve#key-x25519-1" . to_string ( ) ,
517
534
type_ : SecretType :: X25519KeyAgreementKey2020 ,
518
- secret_material : ( SecretMaterial :: Multibase {
519
- value : "zshCmpUZKtFrAfudMf7NzD3oR6yhWe6i2434FDktk9CYZfkndn7suDrqnRWvrVDHk95Z7vBRJChFxTgBF9qzq7D3xPe " . to_string ( )
535
+ secret_material : ( SecretMaterial :: Multibase {
536
+ value : "z3wei8fqKMwJsyTpqDQDWZ9ytPC716YyNDZL6kewQ9qtKrTD " . to_string ( ) ,
520
537
} ) ,
521
- } . as_key_pair ( ) . unwrap ( ) ;
538
+ }
539
+ . as_key_pair ( )
540
+ . unwrap ( ) ;
522
541
523
542
let expected_key = X25519KeyPair :: from_jwk_value ( & json ! ( {
524
543
"kty" : "OKP" ,
525
544
"crv" : "X25519" ,
526
- "x" : "piw5XSMkceDeklaHQZXPBLQySyAwF8eZ-vddihdURS0 " ,
527
- "d" : "T2azVap7CYD_kB8ilbnFYqwwYb5N-GcD6yjGEvquZXg "
545
+ "x" : "tGskN_ae61DP4DLY31_fjkbvnKqf-ze7kA6Cj2vyQxU " ,
546
+ "d" : "qL25gw-HkNJC9m4EsRzCoUx1KntjwHPzxo6a2xUcyFQ "
528
547
} ) )
529
548
. map ( KnownKeyPair :: X25519 )
530
549
. unwrap ( ) ;
@@ -536,10 +555,12 @@ mod tests {
536
555
let actual_key = Secret {
537
556
id : "did:example:eve#key-ed25519-1" . to_string ( ) ,
538
557
type_ : SecretType :: Ed25519VerificationKey2020 ,
539
- secret_material : ( SecretMaterial :: Multibase {
558
+ secret_material : ( SecretMaterial :: Multibase {
540
559
value : "zrv2DyJwnoQWzS74nPkHHdM7NYH27BRNFBG9To7Fca9YzWhfBVa9Mek52H9bJexjdNqxML1F3TGCpjLNkCwwgQDvd5J" . to_string ( )
541
560
} ) ,
542
- } . as_key_pair ( ) . unwrap ( ) ;
561
+ }
562
+ . as_key_pair ( )
563
+ . unwrap ( ) ;
543
564
544
565
let expected_key = Ed25519KeyPair :: from_jwk_value ( & json ! ( {
545
566
"kty" : "OKP" ,
@@ -667,4 +688,27 @@ mod tests {
667
688
assert_eq ! ( is_did( "example:example:alice" ) , false ) ;
668
689
assert_eq ! ( is_did( "example:alice" ) , false ) ;
669
690
}
691
+
692
+ #[ test]
693
+ fn deserialization_for_base58_key_representation_works ( ) {
694
+ let expected_serialzied = r#"{"id":"did:example:eve#key-x25519-1","secret_material":{"format":"Base58","value":"2b5J8uecvwAo9HUGge5NKQ7HoRNKUKCjZ7Fr4mDgWkwqFyjLPWt7rv5kL3UPeG3e4B9Sy4H2Q2zAuWcP2RNtgJ4t"},"type":"X25519KeyAgreementKey2019"}"# ;
695
+ let base58key = "2b5J8uecvwAo9HUGge5NKQ7HoRNKUKCjZ7Fr4mDgWkwqFyjLPWt7rv5kL3UPeG3e4B9Sy4H2Q2zAuWcP2RNtgJ4t" ;
696
+ let actual_key = Secret {
697
+ id : "did:example:eve#key-x25519-1" . to_string ( ) ,
698
+ type_ : SecretType :: X25519KeyAgreementKey2019 ,
699
+ secret_material : SecretMaterial :: Base58 {
700
+ value : base58key. to_string ( ) ,
701
+ } ,
702
+ } ;
703
+
704
+ let serialized = json ! ( actual_key) . to_string ( ) ;
705
+ assert_eq ! ( expected_serialzied, serialized) ;
706
+
707
+ let deserialized: Secret = serde_json:: from_str ( & serialized) . unwrap ( ) ;
708
+ assert_eq ! ( format!( "{:?}" , deserialized) , format!( "{:?}" , actual_key) ) ;
709
+ match deserialized. secret_material {
710
+ SecretMaterial :: Base58 { value } => assert_eq ! ( value, base58key) ,
711
+ _ => assert ! ( false ) ,
712
+ }
713
+ }
670
714
}
0 commit comments