@@ -81,10 +81,7 @@ peg::parser! {
81
81
= kt: ( secp256k1( ) / ed25519( ) / rsa( ) / x25519( ) ) { kt }
82
82
83
83
rule key_purpose( ) -> KeyPurpose
84
- = i( "veriKey" ) { KeyPurpose :: VerificationKey } / "sigAuth" { KeyPurpose :: SignatureAuthentication } / "enc" { KeyPurpose :: Encryption } / "xmtp" { KeyPurpose :: Xmtp }
85
-
86
- rule key_metadata( ) -> KeyMetadata
87
- = i( "inst" ) { KeyMetadata :: Installation }
84
+ = i( "veriKey" ) { KeyPurpose :: VerificationKey } / "sigAuth" { KeyPurpose :: SignatureAuthentication } / "enc" { KeyPurpose :: Encryption }
88
85
89
86
rule encoding( ) -> KeyEncoding
90
87
= i( "hex" ) { KeyEncoding :: Hex } / "base64" { KeyEncoding :: Base64 } / "base58" { KeyEncoding :: Base58 }
@@ -98,24 +95,30 @@ peg::parser! {
98
95
rule service( ) -> ServiceType
99
96
= padding( ) "did/svc/" svc: ( messaging_service( ) / other_service( ) ) padding( ) { svc }
100
97
101
- rule public_key( ) -> ( KeyType , KeyPurpose , Option < KeyMetadata > , KeyEncoding )
102
- = padding( ) "did/pub/" kt: key_type( ) "/" kp: key_purpose( ) "/" me : metadata_and_encoding ( ) padding( ) {
103
- ( kt, kp, me . 0 , me . 1 )
98
+ rule public_key( ) -> ( KeyType , KeyPurpose , KeyEncoding )
99
+ = padding( ) "did/pub/" kt: key_type( ) "/" kp: key_purpose( ) "/" enc : encoding ( ) padding( ) {
100
+ ( kt, kp, enc )
104
101
}
105
102
106
- rule metadata_and_encoding( ) -> ( Option <KeyMetadata >, KeyEncoding )
107
- = km: key_metadata( ) "/" enc: encoding( ) { ( Some ( km) , enc) } / enc: encoding( ) { ( None , enc) }
103
+ rule xmtp_purpose( ) -> XmtpKeyPurpose
104
+ = "installation" { XmtpKeyPurpose :: Installation }
105
+
106
+ rule xmtp( ) -> Attribute
107
+ = padding( ) "xmtp/" xmtp: xmtp_purpose( ) "/" enc: encoding( ) padding( ) { Attribute :: Xmtp ( XmtpAttribute { purpose: xmtp, encoding: enc } ) }
108
+
109
+ rule ethr( ) -> Attribute
110
+ = pk: public_key( ) {
111
+ let key = PublicKey { key_type: pk. 0 , purpose: pk. 1 , encoding: pk. 2 } ;
112
+ Attribute :: PublicKey ( key)
113
+ }
114
+ / svc: service( ) { Attribute :: Service ( svc) }
108
115
109
116
/// Parses the DID attribute name value
110
117
///
111
118
/// Parses the `did/pub/(Secp256k1|RSA|Ed25519|X25519)/(veriKey|sigAuth|enc|xmtp)/(hex|base64|base58)` part of a DID attribute name for adding a public key,
112
119
/// or the `did/svc/[ServiceName]` part for adding a service
113
120
pub rule attribute( ) -> Attribute
114
- = pk: public_key( ) {
115
- let key = PublicKey { key_type: pk. 0 , purpose: pk. 1 , metadata: pk. 2 , encoding: pk. 3 } ;
116
- Attribute :: PublicKey ( key)
117
- }
118
- / svc: service( ) { Attribute :: Service ( svc) }
121
+ = x: xmtp( ) { x } / e: ethr( ) { e }
119
122
}
120
123
}
121
124
@@ -186,12 +189,6 @@ mod tests {
186
189
"did/pub/X25519/enc/hex" ,
187
190
"did/pub/X25519/enc/base64" ,
188
191
"did/pub/X25519/enc/base58" ,
189
- "did/pub/ed25519/xmtp/inst/hex" ,
190
- "did/pub/x25519/xmtp/inst/hex" ,
191
- "did/pub/Secp256k1/xmtp/inst/hex" ,
192
- "did/pub/RSA/xmtp/inst/hex" ,
193
- "did/pub/ed25519/xmtp/inst/base64" ,
194
- "did/pub/ed25519/xmtp/inst/base58" ,
195
192
"did/svc/MessagingService" ,
196
193
] ;
197
194
@@ -201,6 +198,47 @@ mod tests {
201
198
}
202
199
}
203
200
201
+ #[ test]
202
+ fn test_did_xmtp_attribute_parser ( ) {
203
+ let keys = [
204
+ "xmtp/installation/hex" ,
205
+ "xmtp/installation/base58" ,
206
+ "xmtp/installation/base64" ,
207
+ ] ;
208
+
209
+ for key in keys {
210
+ let parsed = parse_attribute ( key) ;
211
+ assert ! ( parsed. is_ok( ) , "Failed to parse key: {}" , key) ;
212
+ }
213
+ }
214
+
215
+ #[ test]
216
+ fn test_xmtp_attribute_parses ( ) {
217
+ assert_eq ! (
218
+ parse_attribute( "xmtp/installation/hex" ) ,
219
+ Ok ( Attribute :: Xmtp ( XmtpAttribute {
220
+ purpose: XmtpKeyPurpose :: Installation ,
221
+ encoding: KeyEncoding :: Hex
222
+ } ) )
223
+ ) ;
224
+
225
+ assert_eq ! (
226
+ parse_attribute( "xmtp/installation/base64" ) ,
227
+ Ok ( Attribute :: Xmtp ( XmtpAttribute {
228
+ purpose: XmtpKeyPurpose :: Installation ,
229
+ encoding: KeyEncoding :: Base64 ,
230
+ } ) )
231
+ ) ;
232
+
233
+ assert_eq ! (
234
+ parse_attribute( "xmtp/installation/base58" ) ,
235
+ Ok ( Attribute :: Xmtp ( XmtpAttribute {
236
+ purpose: XmtpKeyPurpose :: Installation ,
237
+ encoding: KeyEncoding :: Base58
238
+ } ) )
239
+ ) ;
240
+ }
241
+
204
242
#[ test]
205
243
fn test_ethr_method_parser ( ) {
206
244
let parsed =
0 commit comments