-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added serde feature to enable serde
Serialize
and Deserialize
for…
… execution-core
- Loading branch information
Showing
4 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
// | ||
// Copyright (c) DUSK NETWORK. All rights reserved. | ||
|
||
extern crate alloc; | ||
|
||
use crate::keys::public::PublicKey; | ||
use alloc::format; | ||
use alloc::string::String; | ||
use bs58; | ||
use dusk_bytes::Serializable; | ||
use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; | ||
|
||
impl Serialize for PublicKey { | ||
fn serialize<S: Serializer>( | ||
&self, | ||
serializer: S, | ||
) -> Result<S::Ok, S::Error> { | ||
let s = bs58::encode(self.to_bytes()).into_string(); | ||
serializer.serialize_str(&s) | ||
} | ||
} | ||
|
||
impl<'de> Deserialize<'de> for PublicKey { | ||
fn deserialize<D: Deserializer<'de>>( | ||
deserializer: D, | ||
) -> Result<Self, D::Error> { | ||
let s = String::deserialize(deserializer)?; | ||
let mut bytes: [u8; Self::SIZE] = [0; Self::SIZE]; | ||
match bs58::decode(&s).into(&mut bytes) { | ||
Ok(n) => { | ||
if n != Self::SIZE { | ||
return Err(de::Error::custom( | ||
"failed to deserialize AccountPublicKey", | ||
)); | ||
} | ||
} | ||
Err(err) => return Err(de::Error::custom(format!("{err:?}"))), | ||
} | ||
let pubk = PublicKey::from_bytes(&bytes) | ||
.map_err(|err| de::Error::custom(format!("{err:?}")))?; | ||
Ok(pubk) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters