Struct rings_core::ecc::PublicKey
source · Expand description
PublicKey for ECDSA and EdDSA.
Tuple Fields§
§0: [u8; 33]Implementations§
source§impl PublicKey
impl PublicKey
sourcepub fn try_from_b58t(value: &str) -> Result<PublicKey>
pub fn try_from_b58t(value: &str) -> Result<PublicKey>
trezor style b58
sourcepub fn try_from_b58m(value: &str) -> Result<PublicKey>
pub fn try_from_b58m(value: &str) -> Result<PublicKey>
monero and bitcoin style b58
sourcepub fn try_from_b58m_uncheck(value: &str) -> Result<PublicKey>
pub fn try_from_b58m_uncheck(value: &str) -> Result<PublicKey>
monero style uncheck base56
sourcepub fn from_u8(value: &[u8]) -> Result<PublicKey>
pub fn from_u8(value: &[u8]) -> Result<PublicKey>
from raw u8, the length can be 32, or 33
Examples found in repository?
src/ecc/types.rs (line 28)
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
pub fn try_from_b58t(value: &str) -> Result<PublicKey> {
let value: Vec<u8> =
base58::FromBase58::from_base58(value).map_err(|_| Error::PublicKeyBadFormat)?;
Self::from_u8(value.as_slice())
}
/// monero and bitcoin style b58
pub fn try_from_b58m(value: &str) -> Result<PublicKey> {
let value: &[u8] =
&base58_monero::decode_check(value).map_err(|_| Error::PublicKeyBadFormat)?;
Self::from_u8(value)
}
/// monero style uncheck base56
pub fn try_from_b58m_uncheck(value: &str) -> Result<PublicKey> {
let value: &[u8] = &base58_monero::decode(value).map_err(|_| Error::PublicKeyBadFormat)?;
Self::from_u8(value)
}sourcepub fn to_base58_string(&self) -> Result<String>
pub fn to_base58_string(&self) -> Result<String>
convert pubkey to base58_string
source§impl PublicKey
impl PublicKey
sourcepub fn address(&self) -> Address
pub fn address(&self) -> Address
Examples found in repository?
src/ecc/signers.rs (line 32)
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
pub fn verify(msg: &str, address: &Address, sig: impl AsRef<[u8]>) -> bool {
if let Ok(p) = recover(msg, sig) {
p.address() == *address
} else {
false
}
}
}
/// eip191.
/// ref <https://eips.ethereum.org/EIPS/eip-191>
pub mod eip191 {
use super::*;
/// sign function passing raw message parameter.
pub fn sign_raw(sec: SecretKey, msg: &str) -> [u8; 65] {
sign(sec, &hash(msg))
}
/// sign function with `hash` data.
pub fn sign(sec: SecretKey, hash: &[u8; 32]) -> [u8; 65] {
let mut sig = sec.sign_hash(hash);
sig[64] += 27;
sig
}
/// \x19Ethereum Signed Message\n use for PersionSign, which can encode by send `personalSign` rpc call.
pub fn hash(msg: &str) -> [u8; 32] {
let mut prefix_msg = format!("\x19Ethereum Signed Message:\n{}", msg.len()).into_bytes();
prefix_msg.extend_from_slice(msg.as_bytes());
keccak256(&prefix_msg)
}
/// recover pubkey according to signature.
pub fn recover(msg: &str, sig: impl AsRef<[u8]>) -> Result<PublicKey> {
let sig_byte: [u8; 65] = sig.as_ref().try_into()?;
let hash = hash(msg);
let mut sig712 = sig_byte;
sig712[64] -= 27;
crate::ecc::recover_hash(&hash, &sig712)
}
/// verify message signed by Ethereum address.
pub fn verify(msg: &str, address: &Address, sig: impl AsRef<[u8]>) -> bool {
if let Ok(p) = recover(msg, sig) {
p.address() == *address
} else {
false
}
}
}
/// ed25519 sign algorithm using ed25519_dalek
pub mod ed25519 {
use ed25519_dalek::Verifier;
use super::*;
/// ref <https://www.rfc-editor.org/rfc/rfc8709>
pub fn verify(msg: &str, address: &Address, sig: impl AsRef<[u8]>, pubkey: PublicKey) -> bool {
if pubkey.address() != *address {
return false;
}
if sig.as_ref().len() != 64 {
return false;
}
let sig_data: [u8; 64] = sig.as_ref().try_into().unwrap();
if let (Ok(p), Ok(s)) = (
TryInto::<ed25519_dalek::PublicKey>::try_into(pubkey),
ed25519_dalek::Signature::from_bytes(&sig_data),
) {
match p.verify(msg.as_bytes(), &s) {
Ok(()) => true,
Err(_) => false,
}
} else {
false
}
}More examples
src/session.rs (line 179)
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
pub fn gen_unsign_info_with_pubkey(
ttl: Option<Ttl>,
signer: Option<Signer>,
pubkey: PublicKey,
) -> Result<(AuthorizedInfo, SecretKey)> {
let key = SecretKey::random();
let signer = signer.unwrap_or(Signer::DEFAULT);
let authorizer = Authorizer {
did: pubkey.address().into(),
pubkey: Some(pubkey),
};
let info = AuthorizedInfo {
signer,
authorizer,
did: key.address().into(),
ttl_ms: ttl.unwrap_or(Ttl::Some(DEFAULT_SESSION_TTL_MS)),
ts_ms: utils::get_epoch_ms(),
};
Ok((info, key))
}src/transports/default/transport.rs (line 316)
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
async fn on_ice_connection_state_change(&self) -> Self::OnIceConnectionStateChangeHdlrFn {
let event_sender = self.event_sender.clone();
let public_key = Arc::clone(&self.public_key);
let id = self.id;
box move |cs: RTCIceConnectionState| {
let event_sender = event_sender.clone();
let public_key = Arc::clone(&public_key);
let id = id;
Box::pin(async move {
match cs {
RTCIceConnectionState::Connected => {
let local_did = public_key.read().await.unwrap().address().into();
if AcChannel::send(&event_sender, Event::RegisterTransport((local_did, id)))
.await
.is_err()
{
tracing::error!("Failed when send RegisterTransport");
}
}
RTCIceConnectionState::Failed
| RTCIceConnectionState::Disconnected
| RTCIceConnectionState::Closed => {
let local_did = public_key.read().await.unwrap().address().into();
if AcChannel::send(&event_sender, Event::ConnectClosed((local_did, id)))
.await
.is_err()
{
tracing::error!("Failed when send RegisterTransport");
}
}
_ => {
tracing::debug!("IceTransport state change {:?}", cs);
}
}
})
}
}Trait Implementations§
source§impl<'de> Deserialize<'de> for PublicKey
impl<'de> Deserialize<'de> for PublicKey
source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
source§impl PartialEq<PublicKey> for PublicKey
impl PartialEq<PublicKey> for PublicKey
impl Copy for PublicKey
impl Eq for PublicKey
impl StructuralEq for PublicKey
impl StructuralPartialEq for PublicKey
Auto Trait Implementations§
impl RefUnwindSafe for PublicKey
impl Send for PublicKey
impl Sync for PublicKey
impl Unpin for PublicKey
impl UnwindSafe for PublicKey
Blanket Implementations§
§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
§impl<'a, T> AsTaggedExplicit<'a> for Twhere
T: 'a,
impl<'a, T> AsTaggedExplicit<'a> for Twhere
T: 'a,
§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
§impl<'a, T> AsTaggedImplicit<'a> for Twhere
T: 'a,
impl<'a, T> AsTaggedImplicit<'a> for Twhere
T: 'a,
source§impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to
key and return true if they are equal.