Make keystore return None when a key doesn't exist (#8163)

* Make keystore return `None` when a key doesn't exist

* Fixes

* More fixes

* Update comment

* Update primitives/keystore/src/lib.rs

Co-authored-by: André Silva <123550+andresilva@users.noreply.github.com>

* Update client/keystore/src/local.rs

Co-authored-by: André Silva <123550+andresilva@users.noreply.github.com>

* Address comments

* Update client/keystore/src/local.rs

Co-authored-by: André Silva <123550+andresilva@users.noreply.github.com>

Co-authored-by: André Silva <123550+andresilva@users.noreply.github.com>
This commit is contained in:
Bastian Köcher
2021-02-22 15:24:09 +01:00
committed by GitHub
parent ecf4404903
commit 4f4a0c5b38
12 changed files with 174 additions and 118 deletions
+26 -22
View File
@@ -132,7 +132,7 @@ impl CryptoStore for KeyStore {
id: KeyTypeId,
key: &CryptoTypePublicPair,
msg: &[u8],
) -> Result<Vec<u8>, Error> {
) -> Result<Option<Vec<u8>>, Error> {
SyncCryptoStore::sign_with(self, id, key, msg)
}
@@ -141,7 +141,7 @@ impl CryptoStore for KeyStore {
key_type: KeyTypeId,
public: &sr25519::Public,
transcript_data: VRFTranscriptData,
) -> Result<VRFSignature, Error> {
) -> Result<Option<VRFSignature>, Error> {
SyncCryptoStore::sr25519_vrf_sign(self, key_type, public, transcript_data)
}
}
@@ -280,27 +280,27 @@ impl SyncCryptoStore for KeyStore {
id: KeyTypeId,
key: &CryptoTypePublicPair,
msg: &[u8],
) -> Result<Vec<u8>, Error> {
) -> Result<Option<Vec<u8>>, Error> {
use codec::Encode;
match key.0 {
ed25519::CRYPTO_ID => {
let key_pair: ed25519::Pair = self
.ed25519_key_pair(id, &ed25519::Public::from_slice(key.1.as_slice()))
.ok_or_else(|| Error::PairNotFound("ed25519".to_owned()))?;
return Ok(key_pair.sign(msg).encode());
let key_pair = self
.ed25519_key_pair(id, &ed25519::Public::from_slice(key.1.as_slice()));
key_pair.map(|k| k.sign(msg).encode()).map(Ok).transpose()
}
sr25519::CRYPTO_ID => {
let key_pair: sr25519::Pair = self
.sr25519_key_pair(id, &sr25519::Public::from_slice(key.1.as_slice()))
.ok_or_else(|| Error::PairNotFound("sr25519".to_owned()))?;
return Ok(key_pair.sign(msg).encode());
let key_pair = self
.sr25519_key_pair(id, &sr25519::Public::from_slice(key.1.as_slice()));
key_pair.map(|k| k.sign(msg).encode()).map(Ok).transpose()
}
ecdsa::CRYPTO_ID => {
let key_pair: ecdsa::Pair = self
.ecdsa_key_pair(id, &ecdsa::Public::from_slice(key.1.as_slice()))
.ok_or_else(|| Error::PairNotFound("ecdsa".to_owned()))?;
return Ok(key_pair.sign(msg).encode());
let key_pair = self
.ecdsa_key_pair(id, &ecdsa::Public::from_slice(key.1.as_slice()));
key_pair.map(|k| k.sign(msg).encode()).map(Ok).transpose()
}
_ => Err(Error::KeyNotSupported(id))
}
@@ -311,15 +311,19 @@ impl SyncCryptoStore for KeyStore {
key_type: KeyTypeId,
public: &sr25519::Public,
transcript_data: VRFTranscriptData,
) -> Result<VRFSignature, Error> {
) -> Result<Option<VRFSignature>, Error> {
let transcript = make_transcript(transcript_data);
let pair = self.sr25519_key_pair(key_type, public)
.ok_or_else(|| Error::PairNotFound("Not found".to_owned()))?;
let pair = if let Some(k) = self.sr25519_key_pair(key_type, public) {
k
} else {
return Ok(None)
};
let (inout, proof, _) = pair.as_ref().vrf_sign(transcript);
Ok(VRFSignature {
Ok(Some(VRFSignature {
output: inout.to_output(),
proof,
})
}))
}
}
@@ -394,7 +398,7 @@ mod tests {
&key_pair.public(),
transcript_data.clone(),
);
assert!(result.is_err());
assert!(result.unwrap().is_none());
SyncCryptoStore::insert_unknown(
&store,
@@ -410,6 +414,6 @@ mod tests {
transcript_data,
);
assert!(result.is_ok());
assert!(result.unwrap().is_some());
}
}