@@ -132,7 +132,7 @@ pub fn did_to_hex_public_key(did: String, address_type: AddressType) -> Result<S
132
132
todo ! ( )
133
133
} ;
134
134
135
- let decoded_address = bs58:: decode ( address) . into_vec ( ) ?;
135
+ let decoded_address: Vec < u8 > = bs58:: decode ( address) . into_vec ( ) ?;
136
136
137
137
let public_key_bytes: [ u8 ; 32 ] = match address_type {
138
138
AddressType :: Ed25519 => {
@@ -150,6 +150,22 @@ pub fn did_to_hex_public_key(did: String, address_type: AddressType) -> Result<S
150
150
Ok ( hex:: encode ( public_key_bytes) )
151
151
}
152
152
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
+
153
169
pub fn ss58_address_to_did ( address : String , network_id : String ) -> Result < String , Error > {
154
170
let did = format ! ( "did:infra:{}:{}" , network_id, address) ;
155
171
Ok ( did)
@@ -242,6 +258,17 @@ mod tests {
242
258
)
243
259
}
244
260
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
+
245
272
#[ test]
246
273
fn test_ss58_address_to_did ( ) {
247
274
assert_eq ! (
0 commit comments