feat: Rebrand Polkadot/Substrate references to PezkuwiChain
This commit systematically rebrands various references from Parity Technologies' Polkadot/Substrate ecosystem to PezkuwiChain within the kurdistan-sdk. Key changes include: - Updated external repository URLs (zombienet-sdk, parity-db, parity-scale-codec, wasm-instrument) to point to pezkuwichain forks. - Modified internal documentation and code comments to reflect PezkuwiChain naming and structure. - Replaced direct references to with or specific paths within the for XCM, Pezkuwi, and other modules. - Cleaned up deprecated issue and PR references in various and files, particularly in and modules. - Adjusted image and logo URLs in documentation to point to PezkuwiChain assets. - Removed or rephrased comments related to external Polkadot/Substrate PRs and issues. This is a significant step towards fully customizing the SDK for the PezkuwiChain ecosystem.
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
[package]
|
||||
name = "pezsp-keystore"
|
||||
version = "0.34.0"
|
||||
authors.workspace = true
|
||||
edition.workspace = true
|
||||
license = "Apache-2.0"
|
||||
homepage.workspace = true
|
||||
repository.workspace = true
|
||||
description = "Keystore primitives."
|
||||
documentation = "https://docs.rs/pezsp-core"
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
||||
[package.metadata.docs.rs]
|
||||
targets = ["x86_64-unknown-linux-gnu"]
|
||||
|
||||
[dependencies]
|
||||
codec = { features = ["derive"], workspace = true }
|
||||
parking_lot = { optional = true, workspace = true }
|
||||
pezsp-core = { workspace = true }
|
||||
pezsp-externalities = { workspace = true }
|
||||
|
||||
[features]
|
||||
default = ["std"]
|
||||
std = ["codec/std", "dep:parking_lot", "pezsp-core/std", "pezsp-externalities/std"]
|
||||
|
||||
# This feature adds BLS crypto primitives.
|
||||
# It should not be used in production since the implementation and interface may still
|
||||
# be subject to significant changes.
|
||||
bls-experimental = ["pezsp-core/bls-experimental"]
|
||||
|
||||
# This feature adds Bandersnatch crypto primitives.
|
||||
# It should not be used in production since the implementation and interface may still
|
||||
# be subject to significant changes.
|
||||
bandersnatch-experimental = ["pezsp-core/bandersnatch-experimental"]
|
||||
@@ -0,0 +1,714 @@
|
||||
// This file is part of Bizinikiwi.
|
||||
|
||||
// Copyright (C) Parity Technologies (UK) Ltd.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//! Keystore traits
|
||||
|
||||
#![cfg_attr(not(feature = "std"), no_std)]
|
||||
|
||||
extern crate alloc;
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
pub mod testing;
|
||||
|
||||
#[cfg(feature = "bandersnatch-experimental")]
|
||||
use pezsp_core::bandersnatch;
|
||||
#[cfg(feature = "bls-experimental")]
|
||||
use pezsp_core::{bls381, ecdsa_bls381};
|
||||
use pezsp_core::{
|
||||
crypto::{ByteArray, CryptoTypeId, KeyTypeId},
|
||||
ecdsa, ed25519, sr25519,
|
||||
};
|
||||
|
||||
use alloc::{string::String, sync::Arc, vec::Vec};
|
||||
|
||||
/// Keystore error
|
||||
#[derive(Debug)]
|
||||
pub enum Error {
|
||||
/// Public key type is not supported
|
||||
KeyNotSupported(KeyTypeId),
|
||||
/// Validation error
|
||||
ValidationError(String),
|
||||
/// Keystore unavailable
|
||||
Unavailable,
|
||||
/// Programming errors
|
||||
Other(String),
|
||||
}
|
||||
|
||||
impl core::fmt::Display for Error {
|
||||
fn fmt(&self, fmt: &mut core::fmt::Formatter) -> core::fmt::Result {
|
||||
match self {
|
||||
Error::KeyNotSupported(key_type) => write!(fmt, "Key not supported: {key_type:?}"),
|
||||
Error::ValidationError(error) => write!(fmt, "Validation error: {error}"),
|
||||
Error::Unavailable => fmt.write_str("Keystore unavailable"),
|
||||
Error::Other(error) => write!(fmt, "An unknown keystore error occurred: {error}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl std::error::Error for Error {}
|
||||
|
||||
/// Something that generates, stores and provides access to secret keys.
|
||||
pub trait Keystore: Send + Sync {
|
||||
/// Returns all the sr25519 public keys for the given key type.
|
||||
fn sr25519_public_keys(&self, key_type: KeyTypeId) -> Vec<sr25519::Public>;
|
||||
|
||||
/// Generate a new sr25519 key pair for the given key type and an optional seed.
|
||||
///
|
||||
/// Returns an `sr25519::Public` key of the generated key pair or an `Err` if
|
||||
/// something failed during key generation.
|
||||
fn sr25519_generate_new(
|
||||
&self,
|
||||
key_type: KeyTypeId,
|
||||
seed: Option<&str>,
|
||||
) -> Result<sr25519::Public, Error>;
|
||||
|
||||
/// Generate an sr25519 signature for a given message.
|
||||
///
|
||||
/// Receives [`KeyTypeId`] and an [`sr25519::Public`] key to be able to map
|
||||
/// them to a private key that exists in the keystore.
|
||||
///
|
||||
/// Returns an [`sr25519::Signature`] or `None` in case the given `key_type`
|
||||
/// and `public` combination doesn't exist in the keystore.
|
||||
/// An `Err` will be returned if generating the signature itself failed.
|
||||
fn sr25519_sign(
|
||||
&self,
|
||||
key_type: KeyTypeId,
|
||||
public: &sr25519::Public,
|
||||
msg: &[u8],
|
||||
) -> Result<Option<sr25519::Signature>, Error>;
|
||||
|
||||
/// Generate an sr25519 VRF signature for the given data.
|
||||
///
|
||||
/// Receives [`KeyTypeId`] and an [`sr25519::Public`] key to be able to map
|
||||
/// them to a private key that exists in the keystore.
|
||||
///
|
||||
/// Returns `None` if the given `key_type` and `public` combination doesn't
|
||||
/// exist in the keystore or an `Err` when something failed.
|
||||
fn sr25519_vrf_sign(
|
||||
&self,
|
||||
key_type: KeyTypeId,
|
||||
public: &sr25519::Public,
|
||||
data: &sr25519::vrf::VrfSignData,
|
||||
) -> Result<Option<sr25519::vrf::VrfSignature>, Error>;
|
||||
|
||||
/// Generate an sr25519 VRF pre-output for a given input data.
|
||||
///
|
||||
/// Receives [`KeyTypeId`] and an [`sr25519::Public`] key to be able to map
|
||||
/// them to a private key that exists in the keystore.
|
||||
///
|
||||
/// Returns `None` if the given `key_type` and `public` combination doesn't
|
||||
/// exist in the keystore or an `Err` when something failed.
|
||||
fn sr25519_vrf_pre_output(
|
||||
&self,
|
||||
key_type: KeyTypeId,
|
||||
public: &sr25519::Public,
|
||||
input: &sr25519::vrf::VrfInput,
|
||||
) -> Result<Option<sr25519::vrf::VrfPreOutput>, Error>;
|
||||
|
||||
/// Returns all ed25519 public keys for the given key type.
|
||||
fn ed25519_public_keys(&self, key_type: KeyTypeId) -> Vec<ed25519::Public>;
|
||||
|
||||
/// Generate a new ed25519 key pair for the given key type and an optional seed.
|
||||
///
|
||||
/// Returns an `ed25519::Public` key of the generated key pair or an `Err` if
|
||||
/// something failed during key generation.
|
||||
fn ed25519_generate_new(
|
||||
&self,
|
||||
key_type: KeyTypeId,
|
||||
seed: Option<&str>,
|
||||
) -> Result<ed25519::Public, Error>;
|
||||
|
||||
/// Generate an ed25519 signature for a given message.
|
||||
///
|
||||
/// Receives [`KeyTypeId`] and an [`ed25519::Public`] key to be able to map
|
||||
/// them to a private key that exists in the keystore.
|
||||
///
|
||||
/// Returns an [`ed25519::Signature`] or `None` in case the given `key_type`
|
||||
/// and `public` combination doesn't exist in the keystore.
|
||||
/// An `Err` will be returned if generating the signature itself failed.
|
||||
fn ed25519_sign(
|
||||
&self,
|
||||
key_type: KeyTypeId,
|
||||
public: &ed25519::Public,
|
||||
msg: &[u8],
|
||||
) -> Result<Option<ed25519::Signature>, Error>;
|
||||
|
||||
/// Returns all ecdsa public keys for the given key type.
|
||||
fn ecdsa_public_keys(&self, key_type: KeyTypeId) -> Vec<ecdsa::Public>;
|
||||
|
||||
/// Generate a new ecdsa key pair for the given key type and an optional seed.
|
||||
///
|
||||
/// Returns an `ecdsa::Public` key of the generated key pair or an `Err` if
|
||||
/// something failed during key generation.
|
||||
fn ecdsa_generate_new(
|
||||
&self,
|
||||
key_type: KeyTypeId,
|
||||
seed: Option<&str>,
|
||||
) -> Result<ecdsa::Public, Error>;
|
||||
|
||||
/// Generate an ecdsa signature for a given message.
|
||||
///
|
||||
/// Receives [`KeyTypeId`] and an [`ecdsa::Public`] key to be able to map
|
||||
/// them to a private key that exists in the keystore.
|
||||
///
|
||||
/// Returns an [`ecdsa::Signature`] or `None` in case the given `key_type`
|
||||
/// and `public` combination doesn't exist in the keystore.
|
||||
/// An `Err` will be returned if generating the signature itself failed.
|
||||
fn ecdsa_sign(
|
||||
&self,
|
||||
key_type: KeyTypeId,
|
||||
public: &ecdsa::Public,
|
||||
msg: &[u8],
|
||||
) -> Result<Option<ecdsa::Signature>, Error>;
|
||||
|
||||
/// Generate an ecdsa signature for a given pre-hashed message.
|
||||
///
|
||||
/// Receives [`KeyTypeId`] and an [`ecdsa::Public`] key to be able to map
|
||||
/// them to a private key that exists in the keystore.
|
||||
///
|
||||
/// Returns an [`ecdsa::Signature`] or `None` in case the given `key_type`
|
||||
/// and `public` combination doesn't exist in the keystore.
|
||||
/// An `Err` will be returned if generating the signature itself failed.
|
||||
fn ecdsa_sign_prehashed(
|
||||
&self,
|
||||
key_type: KeyTypeId,
|
||||
public: &ecdsa::Public,
|
||||
msg: &[u8; 32],
|
||||
) -> Result<Option<ecdsa::Signature>, Error>;
|
||||
|
||||
/// Returns all the bandersnatch public keys for the given key type.
|
||||
#[cfg(feature = "bandersnatch-experimental")]
|
||||
fn bandersnatch_public_keys(&self, key_type: KeyTypeId) -> Vec<bandersnatch::Public>;
|
||||
|
||||
/// Generate a new bandersnatch key pair for the given key type and an optional seed.
|
||||
///
|
||||
/// Returns an `bandersnatch::Public` key of the generated key pair or an `Err` if
|
||||
/// something failed during key generation.
|
||||
#[cfg(feature = "bandersnatch-experimental")]
|
||||
fn bandersnatch_generate_new(
|
||||
&self,
|
||||
key_type: KeyTypeId,
|
||||
seed: Option<&str>,
|
||||
) -> Result<bandersnatch::Public, Error>;
|
||||
|
||||
/// Generate an bandersnatch signature for a given message.
|
||||
///
|
||||
/// Receives [`KeyTypeId`] and an [`bandersnatch::Public`] key to be able to map
|
||||
/// them to a private key that exists in the keystore.
|
||||
///
|
||||
/// Returns an [`bandersnatch::Signature`] or `None` in case the given `key_type`
|
||||
/// and `public` combination doesn't exist in the keystore.
|
||||
/// An `Err` will be returned if generating the signature itself failed.
|
||||
#[cfg(feature = "bandersnatch-experimental")]
|
||||
fn bandersnatch_sign(
|
||||
&self,
|
||||
key_type: KeyTypeId,
|
||||
public: &bandersnatch::Public,
|
||||
msg: &[u8],
|
||||
) -> Result<Option<bandersnatch::Signature>, Error>;
|
||||
|
||||
/// Generate a bandersnatch VRF signature for the given data.
|
||||
///
|
||||
/// Receives [`KeyTypeId`] and an [`bandersnatch::Public`] key to be able to map
|
||||
/// them to a private key that exists in the keystore.
|
||||
///
|
||||
/// Returns `None` if the given `key_type` and `public` combination doesn't
|
||||
/// exist in the keystore or an `Err` when something failed.
|
||||
#[cfg(feature = "bandersnatch-experimental")]
|
||||
fn bandersnatch_vrf_sign(
|
||||
&self,
|
||||
key_type: KeyTypeId,
|
||||
public: &bandersnatch::Public,
|
||||
input: &bandersnatch::vrf::VrfSignData,
|
||||
) -> Result<Option<bandersnatch::vrf::VrfSignature>, Error>;
|
||||
|
||||
/// Generate a bandersnatch VRF pre-output for a given input data.
|
||||
///
|
||||
/// Receives [`KeyTypeId`] and an [`bandersnatch::Public`] key to be able to map
|
||||
/// them to a private key that exists in the keystore.
|
||||
///
|
||||
/// Returns `None` if the given `key_type` and `public` combination doesn't
|
||||
/// exist in the keystore or an `Err` when something failed.
|
||||
#[cfg(feature = "bandersnatch-experimental")]
|
||||
fn bandersnatch_vrf_pre_output(
|
||||
&self,
|
||||
key_type: KeyTypeId,
|
||||
public: &bandersnatch::Public,
|
||||
input: &bandersnatch::vrf::VrfInput,
|
||||
) -> Result<Option<bandersnatch::vrf::VrfPreOutput>, Error>;
|
||||
|
||||
/// Generate a bandersnatch ring-VRF signature for the given data.
|
||||
///
|
||||
/// Receives [`KeyTypeId`] and an [`bandersnatch::Public`] key to be able to map
|
||||
/// them to a private key that exists in the keystore.
|
||||
///
|
||||
/// Also takes a [`bandersnatch::ring_vrf::RingProver`] instance obtained from
|
||||
/// a valid [`bandersnatch::ring_vrf::RingContext`].
|
||||
///
|
||||
/// The ring signature is verifiable if the public key corresponding to the
|
||||
/// signing [`bandersnatch::Pair`] is part of the ring from which the
|
||||
/// [`bandersnatch::ring_vrf::RingProver`] has been constructed.
|
||||
/// If not, the produced signature is just useless.
|
||||
///
|
||||
/// Returns `None` if the given `key_type` and `public` combination doesn't
|
||||
/// exist in the keystore or an `Err` when something failed.
|
||||
#[cfg(feature = "bandersnatch-experimental")]
|
||||
fn bandersnatch_ring_vrf_sign(
|
||||
&self,
|
||||
key_type: KeyTypeId,
|
||||
public: &bandersnatch::Public,
|
||||
input: &bandersnatch::vrf::VrfSignData,
|
||||
prover: &bandersnatch::ring_vrf::RingProver,
|
||||
) -> Result<Option<bandersnatch::ring_vrf::RingVrfSignature>, Error>;
|
||||
|
||||
/// Returns all bls12-381 public keys for the given key type.
|
||||
#[cfg(feature = "bls-experimental")]
|
||||
fn bls381_public_keys(&self, id: KeyTypeId) -> Vec<bls381::Public>;
|
||||
|
||||
/// Returns all (ecdsa,bls12-381) paired public keys for the given key type.
|
||||
#[cfg(feature = "bls-experimental")]
|
||||
fn ecdsa_bls381_public_keys(&self, id: KeyTypeId) -> Vec<ecdsa_bls381::Public>;
|
||||
|
||||
/// Generate a new bls381 key pair for the given key type and an optional seed.
|
||||
///
|
||||
/// Returns an `bls381::Public` key of the generated key pair or an `Err` if
|
||||
/// something failed during key generation.
|
||||
#[cfg(feature = "bls-experimental")]
|
||||
fn bls381_generate_new(
|
||||
&self,
|
||||
key_type: KeyTypeId,
|
||||
seed: Option<&str>,
|
||||
) -> Result<bls381::Public, Error>;
|
||||
|
||||
/// Generate a new (ecdsa,bls381) key pair for the given key type and an optional seed.
|
||||
///
|
||||
/// Returns an `ecdsa_bls381::Public` key of the generated key pair or an `Err` if
|
||||
/// something failed during key generation.
|
||||
#[cfg(feature = "bls-experimental")]
|
||||
fn ecdsa_bls381_generate_new(
|
||||
&self,
|
||||
key_type: KeyTypeId,
|
||||
seed: Option<&str>,
|
||||
) -> Result<ecdsa_bls381::Public, Error>;
|
||||
|
||||
/// Generate a bls381 signature for a given message.
|
||||
///
|
||||
/// Receives [`KeyTypeId`] and a [`bls381::Public`] key to be able to map
|
||||
/// them to a private key that exists in the keystore.
|
||||
///
|
||||
/// Returns an [`bls381::Signature`] or `None` in case the given `key_type`
|
||||
/// and `public` combination doesn't exist in the keystore.
|
||||
/// An `Err` will be returned if generating the signature itself failed.
|
||||
#[cfg(feature = "bls-experimental")]
|
||||
fn bls381_sign(
|
||||
&self,
|
||||
key_type: KeyTypeId,
|
||||
public: &bls381::Public,
|
||||
msg: &[u8],
|
||||
) -> Result<Option<bls381::Signature>, Error>;
|
||||
|
||||
/// Generate a bls381 Proof of Possession for a given public key
|
||||
///
|
||||
/// Receives ['KeyTypeId'] and a ['bls381::Public'] key to be able to map
|
||||
/// them to a private key that exists in the keystore
|
||||
///
|
||||
/// Returns an ['bls381::Signature'] or 'None' in case the given 'key_type'
|
||||
/// and 'public' combination doesn't exist in the keystore.
|
||||
/// An 'Err' will be returned if generating the proof of possession itself failed.
|
||||
#[cfg(feature = "bls-experimental")]
|
||||
fn bls381_generate_proof_of_possession(
|
||||
&self,
|
||||
key_type: KeyTypeId,
|
||||
public: &bls381::Public,
|
||||
owner: &[u8],
|
||||
) -> Result<Option<bls381::ProofOfPossession>, Error>;
|
||||
|
||||
/// Generate a (ecdsa,bls381) signature pair for a given message.
|
||||
///
|
||||
/// Receives [`KeyTypeId`] and a [`ecdsa_bls381::Public`] key to be able to map
|
||||
/// them to a private key that exists in the keystore.
|
||||
///
|
||||
/// Returns an [`ecdsa_bls381::Signature`] or `None` in case the given `key_type`
|
||||
/// and `public` combination doesn't exist in the keystore.
|
||||
/// An `Err` will be returned if generating the signature itself failed.
|
||||
#[cfg(feature = "bls-experimental")]
|
||||
fn ecdsa_bls381_sign(
|
||||
&self,
|
||||
key_type: KeyTypeId,
|
||||
public: &ecdsa_bls381::Public,
|
||||
msg: &[u8],
|
||||
) -> Result<Option<ecdsa_bls381::Signature>, Error>;
|
||||
|
||||
/// Hashes the `message` using keccak256 and then signs it using ECDSA
|
||||
/// algorithm. It does not affect the behavior of BLS12-381 component. It generates
|
||||
/// BLS12-381 Signature according to IETF standard.
|
||||
///
|
||||
/// Receives [`KeyTypeId`] and a [`ecdsa_bls381::Public`] key to be able to map
|
||||
/// them to a private key that exists in the keystore.
|
||||
///
|
||||
/// Returns an [`ecdsa_bls381::Signature`] or `None` in case the given `key_type`
|
||||
/// and `public` combination doesn't exist in the keystore.
|
||||
/// An `Err` will be returned if generating the signature itself failed.
|
||||
#[cfg(feature = "bls-experimental")]
|
||||
fn ecdsa_bls381_sign_with_keccak256(
|
||||
&self,
|
||||
key_type: KeyTypeId,
|
||||
public: &ecdsa_bls381::Public,
|
||||
msg: &[u8],
|
||||
) -> Result<Option<ecdsa_bls381::Signature>, Error>;
|
||||
|
||||
/// Insert a new secret key.
|
||||
fn insert(&self, key_type: KeyTypeId, suri: &str, public: &[u8]) -> Result<(), ()>;
|
||||
|
||||
/// List all supported keys of a given type.
|
||||
///
|
||||
/// Returns a set of public keys the signer supports in raw format.
|
||||
fn keys(&self, key_type: KeyTypeId) -> Result<Vec<Vec<u8>>, Error>;
|
||||
|
||||
/// Checks if the private keys for the given public key and key type combinations exist.
|
||||
///
|
||||
/// Returns `true` iff all private keys could be found.
|
||||
fn has_keys(&self, public_keys: &[(Vec<u8>, KeyTypeId)]) -> bool;
|
||||
|
||||
/// Convenience method to sign a message using the given key type and a raw public key
|
||||
/// for secret lookup.
|
||||
///
|
||||
/// The message is signed using the cryptographic primitive specified by `crypto_id`.
|
||||
///
|
||||
/// Schemes supported by the default trait implementation:
|
||||
/// - sr25519
|
||||
/// - ed25519
|
||||
/// - ecdsa
|
||||
/// - bandersnatch
|
||||
/// - bls381
|
||||
/// - (ecdsa,bls381) paired keys
|
||||
///
|
||||
/// To support more schemes you can overwrite this method.
|
||||
///
|
||||
/// Returns the SCALE encoded signature if key is found and supported, `None` if the key doesn't
|
||||
/// exist or an error when something failed.
|
||||
fn sign_with(
|
||||
&self,
|
||||
id: KeyTypeId,
|
||||
crypto_id: CryptoTypeId,
|
||||
public: &[u8],
|
||||
msg: &[u8],
|
||||
) -> Result<Option<Vec<u8>>, Error> {
|
||||
use codec::Encode;
|
||||
|
||||
let signature = match crypto_id {
|
||||
sr25519::CRYPTO_ID => {
|
||||
let public = sr25519::Public::from_slice(public)
|
||||
.map_err(|_| Error::ValidationError("Invalid public key format".into()))?;
|
||||
self.sr25519_sign(id, &public, msg)?.map(|s| s.encode())
|
||||
},
|
||||
ed25519::CRYPTO_ID => {
|
||||
let public = ed25519::Public::from_slice(public)
|
||||
.map_err(|_| Error::ValidationError("Invalid public key format".into()))?;
|
||||
self.ed25519_sign(id, &public, msg)?.map(|s| s.encode())
|
||||
},
|
||||
ecdsa::CRYPTO_ID => {
|
||||
let public = ecdsa::Public::from_slice(public)
|
||||
.map_err(|_| Error::ValidationError("Invalid public key format".into()))?;
|
||||
|
||||
self.ecdsa_sign(id, &public, msg)?.map(|s| s.encode())
|
||||
},
|
||||
#[cfg(feature = "bandersnatch-experimental")]
|
||||
bandersnatch::CRYPTO_ID => {
|
||||
let public = bandersnatch::Public::from_slice(public)
|
||||
.map_err(|_| Error::ValidationError("Invalid public key format".into()))?;
|
||||
self.bandersnatch_sign(id, &public, msg)?.map(|s| s.encode())
|
||||
},
|
||||
#[cfg(feature = "bls-experimental")]
|
||||
bls381::CRYPTO_ID => {
|
||||
let public = bls381::Public::from_slice(public)
|
||||
.map_err(|_| Error::ValidationError("Invalid public key format".into()))?;
|
||||
self.bls381_sign(id, &public, msg)?.map(|s| s.encode())
|
||||
},
|
||||
#[cfg(feature = "bls-experimental")]
|
||||
ecdsa_bls381::CRYPTO_ID => {
|
||||
let public = ecdsa_bls381::Public::from_slice(public)
|
||||
.map_err(|_| Error::ValidationError("Invalid public key format".into()))?;
|
||||
self.ecdsa_bls381_sign(id, &public, msg)?.map(|s| s.encode())
|
||||
},
|
||||
_ => return Err(Error::KeyNotSupported(id)),
|
||||
};
|
||||
Ok(signature)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Keystore + ?Sized> Keystore for Arc<T> {
|
||||
fn sr25519_public_keys(&self, key_type: KeyTypeId) -> Vec<sr25519::Public> {
|
||||
(**self).sr25519_public_keys(key_type)
|
||||
}
|
||||
|
||||
fn sr25519_generate_new(
|
||||
&self,
|
||||
key_type: KeyTypeId,
|
||||
seed: Option<&str>,
|
||||
) -> Result<sr25519::Public, Error> {
|
||||
(**self).sr25519_generate_new(key_type, seed)
|
||||
}
|
||||
|
||||
fn sr25519_sign(
|
||||
&self,
|
||||
key_type: KeyTypeId,
|
||||
public: &sr25519::Public,
|
||||
msg: &[u8],
|
||||
) -> Result<Option<sr25519::Signature>, Error> {
|
||||
(**self).sr25519_sign(key_type, public, msg)
|
||||
}
|
||||
|
||||
fn sr25519_vrf_sign(
|
||||
&self,
|
||||
key_type: KeyTypeId,
|
||||
public: &sr25519::Public,
|
||||
data: &sr25519::vrf::VrfSignData,
|
||||
) -> Result<Option<sr25519::vrf::VrfSignature>, Error> {
|
||||
(**self).sr25519_vrf_sign(key_type, public, data)
|
||||
}
|
||||
|
||||
fn sr25519_vrf_pre_output(
|
||||
&self,
|
||||
key_type: KeyTypeId,
|
||||
public: &sr25519::Public,
|
||||
input: &sr25519::vrf::VrfInput,
|
||||
) -> Result<Option<sr25519::vrf::VrfPreOutput>, Error> {
|
||||
(**self).sr25519_vrf_pre_output(key_type, public, input)
|
||||
}
|
||||
|
||||
fn ed25519_public_keys(&self, key_type: KeyTypeId) -> Vec<ed25519::Public> {
|
||||
(**self).ed25519_public_keys(key_type)
|
||||
}
|
||||
|
||||
fn ed25519_generate_new(
|
||||
&self,
|
||||
key_type: KeyTypeId,
|
||||
seed: Option<&str>,
|
||||
) -> Result<ed25519::Public, Error> {
|
||||
(**self).ed25519_generate_new(key_type, seed)
|
||||
}
|
||||
|
||||
fn ed25519_sign(
|
||||
&self,
|
||||
key_type: KeyTypeId,
|
||||
public: &ed25519::Public,
|
||||
msg: &[u8],
|
||||
) -> Result<Option<ed25519::Signature>, Error> {
|
||||
(**self).ed25519_sign(key_type, public, msg)
|
||||
}
|
||||
|
||||
fn ecdsa_public_keys(&self, key_type: KeyTypeId) -> Vec<ecdsa::Public> {
|
||||
(**self).ecdsa_public_keys(key_type)
|
||||
}
|
||||
|
||||
fn ecdsa_generate_new(
|
||||
&self,
|
||||
key_type: KeyTypeId,
|
||||
seed: Option<&str>,
|
||||
) -> Result<ecdsa::Public, Error> {
|
||||
(**self).ecdsa_generate_new(key_type, seed)
|
||||
}
|
||||
|
||||
fn ecdsa_sign(
|
||||
&self,
|
||||
key_type: KeyTypeId,
|
||||
public: &ecdsa::Public,
|
||||
msg: &[u8],
|
||||
) -> Result<Option<ecdsa::Signature>, Error> {
|
||||
(**self).ecdsa_sign(key_type, public, msg)
|
||||
}
|
||||
|
||||
fn ecdsa_sign_prehashed(
|
||||
&self,
|
||||
key_type: KeyTypeId,
|
||||
public: &ecdsa::Public,
|
||||
msg: &[u8; 32],
|
||||
) -> Result<Option<ecdsa::Signature>, Error> {
|
||||
(**self).ecdsa_sign_prehashed(key_type, public, msg)
|
||||
}
|
||||
|
||||
#[cfg(feature = "bandersnatch-experimental")]
|
||||
fn bandersnatch_public_keys(&self, key_type: KeyTypeId) -> Vec<bandersnatch::Public> {
|
||||
(**self).bandersnatch_public_keys(key_type)
|
||||
}
|
||||
|
||||
#[cfg(feature = "bandersnatch-experimental")]
|
||||
fn bandersnatch_generate_new(
|
||||
&self,
|
||||
key_type: KeyTypeId,
|
||||
seed: Option<&str>,
|
||||
) -> Result<bandersnatch::Public, Error> {
|
||||
(**self).bandersnatch_generate_new(key_type, seed)
|
||||
}
|
||||
|
||||
#[cfg(feature = "bandersnatch-experimental")]
|
||||
fn bandersnatch_sign(
|
||||
&self,
|
||||
key_type: KeyTypeId,
|
||||
public: &bandersnatch::Public,
|
||||
msg: &[u8],
|
||||
) -> Result<Option<bandersnatch::Signature>, Error> {
|
||||
(**self).bandersnatch_sign(key_type, public, msg)
|
||||
}
|
||||
|
||||
#[cfg(feature = "bandersnatch-experimental")]
|
||||
fn bandersnatch_vrf_sign(
|
||||
&self,
|
||||
key_type: KeyTypeId,
|
||||
public: &bandersnatch::Public,
|
||||
input: &bandersnatch::vrf::VrfSignData,
|
||||
) -> Result<Option<bandersnatch::vrf::VrfSignature>, Error> {
|
||||
(**self).bandersnatch_vrf_sign(key_type, public, input)
|
||||
}
|
||||
|
||||
#[cfg(feature = "bandersnatch-experimental")]
|
||||
fn bandersnatch_vrf_pre_output(
|
||||
&self,
|
||||
key_type: KeyTypeId,
|
||||
public: &bandersnatch::Public,
|
||||
input: &bandersnatch::vrf::VrfInput,
|
||||
) -> Result<Option<bandersnatch::vrf::VrfPreOutput>, Error> {
|
||||
(**self).bandersnatch_vrf_pre_output(key_type, public, input)
|
||||
}
|
||||
|
||||
#[cfg(feature = "bandersnatch-experimental")]
|
||||
fn bandersnatch_ring_vrf_sign(
|
||||
&self,
|
||||
key_type: KeyTypeId,
|
||||
public: &bandersnatch::Public,
|
||||
input: &bandersnatch::vrf::VrfSignData,
|
||||
prover: &bandersnatch::ring_vrf::RingProver,
|
||||
) -> Result<Option<bandersnatch::ring_vrf::RingVrfSignature>, Error> {
|
||||
(**self).bandersnatch_ring_vrf_sign(key_type, public, input, prover)
|
||||
}
|
||||
|
||||
#[cfg(feature = "bls-experimental")]
|
||||
fn bls381_public_keys(&self, id: KeyTypeId) -> Vec<bls381::Public> {
|
||||
(**self).bls381_public_keys(id)
|
||||
}
|
||||
|
||||
#[cfg(feature = "bls-experimental")]
|
||||
fn ecdsa_bls381_public_keys(&self, id: KeyTypeId) -> Vec<ecdsa_bls381::Public> {
|
||||
(**self).ecdsa_bls381_public_keys(id)
|
||||
}
|
||||
|
||||
#[cfg(feature = "bls-experimental")]
|
||||
fn bls381_generate_new(
|
||||
&self,
|
||||
key_type: KeyTypeId,
|
||||
seed: Option<&str>,
|
||||
) -> Result<bls381::Public, Error> {
|
||||
(**self).bls381_generate_new(key_type, seed)
|
||||
}
|
||||
|
||||
#[cfg(feature = "bls-experimental")]
|
||||
fn ecdsa_bls381_generate_new(
|
||||
&self,
|
||||
key_type: KeyTypeId,
|
||||
seed: Option<&str>,
|
||||
) -> Result<ecdsa_bls381::Public, Error> {
|
||||
(**self).ecdsa_bls381_generate_new(key_type, seed)
|
||||
}
|
||||
|
||||
#[cfg(feature = "bls-experimental")]
|
||||
fn bls381_sign(
|
||||
&self,
|
||||
key_type: KeyTypeId,
|
||||
public: &bls381::Public,
|
||||
msg: &[u8],
|
||||
) -> Result<Option<bls381::Signature>, Error> {
|
||||
(**self).bls381_sign(key_type, public, msg)
|
||||
}
|
||||
|
||||
#[cfg(feature = "bls-experimental")]
|
||||
fn bls381_generate_proof_of_possession(
|
||||
&self,
|
||||
key_type: KeyTypeId,
|
||||
public: &bls381::Public,
|
||||
owner: &[u8],
|
||||
) -> Result<Option<bls381::ProofOfPossession>, Error> {
|
||||
(**self).bls381_generate_proof_of_possession(key_type, public, owner)
|
||||
}
|
||||
|
||||
#[cfg(feature = "bls-experimental")]
|
||||
fn ecdsa_bls381_sign(
|
||||
&self,
|
||||
key_type: KeyTypeId,
|
||||
public: &ecdsa_bls381::Public,
|
||||
msg: &[u8],
|
||||
) -> Result<Option<ecdsa_bls381::Signature>, Error> {
|
||||
(**self).ecdsa_bls381_sign(key_type, public, msg)
|
||||
}
|
||||
|
||||
#[cfg(feature = "bls-experimental")]
|
||||
fn ecdsa_bls381_sign_with_keccak256(
|
||||
&self,
|
||||
key_type: KeyTypeId,
|
||||
public: &ecdsa_bls381::Public,
|
||||
msg: &[u8],
|
||||
) -> Result<Option<ecdsa_bls381::Signature>, Error> {
|
||||
(**self).ecdsa_bls381_sign_with_keccak256(key_type, public, msg)
|
||||
}
|
||||
|
||||
fn insert(&self, key_type: KeyTypeId, suri: &str, public: &[u8]) -> Result<(), ()> {
|
||||
(**self).insert(key_type, suri, public)
|
||||
}
|
||||
|
||||
fn keys(&self, key_type: KeyTypeId) -> Result<Vec<Vec<u8>>, Error> {
|
||||
(**self).keys(key_type)
|
||||
}
|
||||
|
||||
fn has_keys(&self, public_keys: &[(Vec<u8>, KeyTypeId)]) -> bool {
|
||||
(**self).has_keys(public_keys)
|
||||
}
|
||||
}
|
||||
|
||||
/// A shared pointer to a keystore implementation.
|
||||
pub type KeystorePtr = Arc<dyn Keystore>;
|
||||
|
||||
pezsp_externalities::decl_extension! {
|
||||
/// The keystore extension to register/retrieve from the externalities.
|
||||
pub struct KeystoreExt(KeystorePtr);
|
||||
}
|
||||
|
||||
impl KeystoreExt {
|
||||
/// Create a new instance of `KeystoreExt`
|
||||
///
|
||||
/// This is more performant as we don't need to wrap keystore in another [`Arc`].
|
||||
pub fn from(keystore: KeystorePtr) -> Self {
|
||||
Self(keystore)
|
||||
}
|
||||
|
||||
/// Create a new instance of `KeystoreExt` using the given `keystore`.
|
||||
pub fn new<T: Keystore + 'static>(keystore: T) -> Self {
|
||||
Self(Arc::new(keystore))
|
||||
}
|
||||
}
|
||||
|
||||
pezsp_core::generate_feature_enabled_macro!(
|
||||
bandersnatch_experimental_enabled,
|
||||
feature = "bandersnatch-experimental",
|
||||
$
|
||||
);
|
||||
|
||||
pezsp_core::generate_feature_enabled_macro!(
|
||||
bls_experimental_enabled,
|
||||
feature = "bls-experimental",
|
||||
$
|
||||
);
|
||||
@@ -0,0 +1,678 @@
|
||||
// This file is part of Bizinikiwi.
|
||||
|
||||
// Copyright (C) Parity Technologies (UK) Ltd.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//! Types that should only be used for testing!
|
||||
|
||||
use crate::{Error, Keystore, KeystorePtr};
|
||||
|
||||
#[cfg(feature = "bandersnatch-experimental")]
|
||||
use pezsp_core::bandersnatch;
|
||||
#[cfg(feature = "bls-experimental")]
|
||||
use pezsp_core::{
|
||||
bls381, ecdsa_bls381, proof_of_possession::ProofOfPossessionGenerator, KeccakHasher,
|
||||
};
|
||||
use pezsp_core::{
|
||||
crypto::{ByteArray, KeyTypeId, Pair, VrfSecret},
|
||||
ecdsa, ed25519, sr25519,
|
||||
};
|
||||
|
||||
use parking_lot::RwLock;
|
||||
use std::{collections::HashMap, sync::Arc};
|
||||
|
||||
/// A keystore implementation usable in tests.
|
||||
#[derive(Default, Clone)]
|
||||
pub struct MemoryKeystore {
|
||||
/// `KeyTypeId` maps to public keys and public keys map to private keys.
|
||||
keys: Arc<RwLock<HashMap<KeyTypeId, HashMap<Vec<u8>, String>>>>,
|
||||
}
|
||||
|
||||
impl MemoryKeystore {
|
||||
/// Creates a new instance of `Self`.
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
|
||||
fn pair<T: Pair>(&self, key_type: KeyTypeId, public: &T::Public) -> Option<T> {
|
||||
self.keys.read().get(&key_type).and_then(|inner| {
|
||||
inner
|
||||
.get(public.as_slice())
|
||||
.map(|s| T::from_string(s, None).expect("seed slice is valid"))
|
||||
})
|
||||
}
|
||||
|
||||
fn public_keys<T: Pair>(&self, key_type: KeyTypeId) -> Vec<T::Public> {
|
||||
self.keys
|
||||
.read()
|
||||
.get(&key_type)
|
||||
.map(|keys| {
|
||||
keys.iter()
|
||||
.filter_map(|(raw_pubkey, s)| {
|
||||
let pair = T::from_string(s, None).expect("seed slice is valid");
|
||||
let pubkey = pair.public();
|
||||
(pubkey.as_slice() == raw_pubkey).then_some(pubkey)
|
||||
})
|
||||
.collect()
|
||||
})
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
fn generate_new<T: Pair>(
|
||||
&self,
|
||||
key_type: KeyTypeId,
|
||||
seed: Option<&str>,
|
||||
) -> Result<T::Public, Error> {
|
||||
match seed {
|
||||
Some(seed) => {
|
||||
let pair = T::from_string(seed, None)
|
||||
.map_err(|_| Error::ValidationError("Generates a pair.".to_owned()))?;
|
||||
self.keys
|
||||
.write()
|
||||
.entry(key_type)
|
||||
.or_default()
|
||||
.insert(pair.public().to_raw_vec(), seed.into());
|
||||
Ok(pair.public())
|
||||
},
|
||||
None => {
|
||||
let (pair, phrase, _) = T::generate_with_phrase(None);
|
||||
self.keys
|
||||
.write()
|
||||
.entry(key_type)
|
||||
.or_default()
|
||||
.insert(pair.public().to_raw_vec(), phrase);
|
||||
Ok(pair.public())
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn sign<T: Pair>(
|
||||
&self,
|
||||
key_type: KeyTypeId,
|
||||
public: &T::Public,
|
||||
msg: &[u8],
|
||||
) -> Result<Option<T::Signature>, Error> {
|
||||
let sig = self.pair::<T>(key_type, public).map(|pair| pair.sign(msg));
|
||||
Ok(sig)
|
||||
}
|
||||
|
||||
fn vrf_sign<T: Pair + VrfSecret>(
|
||||
&self,
|
||||
key_type: KeyTypeId,
|
||||
public: &T::Public,
|
||||
data: &T::VrfSignData,
|
||||
) -> Result<Option<T::VrfSignature>, Error> {
|
||||
let sig = self.pair::<T>(key_type, public).map(|pair| pair.vrf_sign(data));
|
||||
Ok(sig)
|
||||
}
|
||||
|
||||
fn vrf_pre_output<T: Pair + VrfSecret>(
|
||||
&self,
|
||||
key_type: KeyTypeId,
|
||||
public: &T::Public,
|
||||
input: &T::VrfInput,
|
||||
) -> Result<Option<T::VrfPreOutput>, Error> {
|
||||
let pre_output = self.pair::<T>(key_type, public).map(|pair| pair.vrf_pre_output(input));
|
||||
Ok(pre_output)
|
||||
}
|
||||
|
||||
#[cfg(feature = "bls-experimental")]
|
||||
fn generate_proof_of_possession<T: Pair + ProofOfPossessionGenerator>(
|
||||
&self,
|
||||
key_type: KeyTypeId,
|
||||
public: &T::Public,
|
||||
owner: &[u8],
|
||||
) -> Result<Option<T::ProofOfPossession>, Error> {
|
||||
let proof_of_possession = self
|
||||
.pair::<T>(key_type, public)
|
||||
.map(|mut pair| pair.generate_proof_of_possession(owner));
|
||||
Ok(proof_of_possession)
|
||||
}
|
||||
}
|
||||
|
||||
impl Keystore for MemoryKeystore {
|
||||
fn sr25519_public_keys(&self, key_type: KeyTypeId) -> Vec<sr25519::Public> {
|
||||
self.public_keys::<sr25519::Pair>(key_type)
|
||||
}
|
||||
|
||||
fn sr25519_generate_new(
|
||||
&self,
|
||||
key_type: KeyTypeId,
|
||||
seed: Option<&str>,
|
||||
) -> Result<sr25519::Public, Error> {
|
||||
self.generate_new::<sr25519::Pair>(key_type, seed)
|
||||
}
|
||||
|
||||
fn sr25519_sign(
|
||||
&self,
|
||||
key_type: KeyTypeId,
|
||||
public: &sr25519::Public,
|
||||
msg: &[u8],
|
||||
) -> Result<Option<sr25519::Signature>, Error> {
|
||||
self.sign::<sr25519::Pair>(key_type, public, msg)
|
||||
}
|
||||
|
||||
fn sr25519_vrf_sign(
|
||||
&self,
|
||||
key_type: KeyTypeId,
|
||||
public: &sr25519::Public,
|
||||
data: &sr25519::vrf::VrfSignData,
|
||||
) -> Result<Option<sr25519::vrf::VrfSignature>, Error> {
|
||||
self.vrf_sign::<sr25519::Pair>(key_type, public, data)
|
||||
}
|
||||
|
||||
fn sr25519_vrf_pre_output(
|
||||
&self,
|
||||
key_type: KeyTypeId,
|
||||
public: &sr25519::Public,
|
||||
input: &sr25519::vrf::VrfInput,
|
||||
) -> Result<Option<sr25519::vrf::VrfPreOutput>, Error> {
|
||||
self.vrf_pre_output::<sr25519::Pair>(key_type, public, input)
|
||||
}
|
||||
|
||||
fn ed25519_public_keys(&self, key_type: KeyTypeId) -> Vec<ed25519::Public> {
|
||||
self.public_keys::<ed25519::Pair>(key_type)
|
||||
}
|
||||
|
||||
fn ed25519_generate_new(
|
||||
&self,
|
||||
key_type: KeyTypeId,
|
||||
seed: Option<&str>,
|
||||
) -> Result<ed25519::Public, Error> {
|
||||
self.generate_new::<ed25519::Pair>(key_type, seed)
|
||||
}
|
||||
|
||||
fn ed25519_sign(
|
||||
&self,
|
||||
key_type: KeyTypeId,
|
||||
public: &ed25519::Public,
|
||||
msg: &[u8],
|
||||
) -> Result<Option<ed25519::Signature>, Error> {
|
||||
self.sign::<ed25519::Pair>(key_type, public, msg)
|
||||
}
|
||||
|
||||
fn ecdsa_public_keys(&self, key_type: KeyTypeId) -> Vec<ecdsa::Public> {
|
||||
self.public_keys::<ecdsa::Pair>(key_type)
|
||||
}
|
||||
|
||||
fn ecdsa_generate_new(
|
||||
&self,
|
||||
key_type: KeyTypeId,
|
||||
seed: Option<&str>,
|
||||
) -> Result<ecdsa::Public, Error> {
|
||||
self.generate_new::<ecdsa::Pair>(key_type, seed)
|
||||
}
|
||||
|
||||
fn ecdsa_sign(
|
||||
&self,
|
||||
key_type: KeyTypeId,
|
||||
public: &ecdsa::Public,
|
||||
msg: &[u8],
|
||||
) -> Result<Option<ecdsa::Signature>, Error> {
|
||||
self.sign::<ecdsa::Pair>(key_type, public, msg)
|
||||
}
|
||||
|
||||
fn ecdsa_sign_prehashed(
|
||||
&self,
|
||||
key_type: KeyTypeId,
|
||||
public: &ecdsa::Public,
|
||||
msg: &[u8; 32],
|
||||
) -> Result<Option<ecdsa::Signature>, Error> {
|
||||
let sig = self.pair::<ecdsa::Pair>(key_type, public).map(|pair| pair.sign_prehashed(msg));
|
||||
Ok(sig)
|
||||
}
|
||||
|
||||
#[cfg(feature = "bandersnatch-experimental")]
|
||||
fn bandersnatch_public_keys(&self, key_type: KeyTypeId) -> Vec<bandersnatch::Public> {
|
||||
self.public_keys::<bandersnatch::Pair>(key_type)
|
||||
}
|
||||
|
||||
#[cfg(feature = "bandersnatch-experimental")]
|
||||
fn bandersnatch_generate_new(
|
||||
&self,
|
||||
key_type: KeyTypeId,
|
||||
seed: Option<&str>,
|
||||
) -> Result<bandersnatch::Public, Error> {
|
||||
self.generate_new::<bandersnatch::Pair>(key_type, seed)
|
||||
}
|
||||
|
||||
#[cfg(feature = "bandersnatch-experimental")]
|
||||
fn bandersnatch_sign(
|
||||
&self,
|
||||
key_type: KeyTypeId,
|
||||
public: &bandersnatch::Public,
|
||||
msg: &[u8],
|
||||
) -> Result<Option<bandersnatch::Signature>, Error> {
|
||||
self.sign::<bandersnatch::Pair>(key_type, public, msg)
|
||||
}
|
||||
|
||||
#[cfg(feature = "bandersnatch-experimental")]
|
||||
fn bandersnatch_vrf_sign(
|
||||
&self,
|
||||
key_type: KeyTypeId,
|
||||
public: &bandersnatch::Public,
|
||||
data: &bandersnatch::vrf::VrfSignData,
|
||||
) -> Result<Option<bandersnatch::vrf::VrfSignature>, Error> {
|
||||
self.vrf_sign::<bandersnatch::Pair>(key_type, public, data)
|
||||
}
|
||||
|
||||
#[cfg(feature = "bandersnatch-experimental")]
|
||||
fn bandersnatch_ring_vrf_sign(
|
||||
&self,
|
||||
key_type: KeyTypeId,
|
||||
public: &bandersnatch::Public,
|
||||
data: &bandersnatch::vrf::VrfSignData,
|
||||
prover: &bandersnatch::ring_vrf::RingProver,
|
||||
) -> Result<Option<bandersnatch::ring_vrf::RingVrfSignature>, Error> {
|
||||
let sig = self
|
||||
.pair::<bandersnatch::Pair>(key_type, public)
|
||||
.map(|pair| pair.ring_vrf_sign(data, prover));
|
||||
Ok(sig)
|
||||
}
|
||||
|
||||
#[cfg(feature = "bandersnatch-experimental")]
|
||||
fn bandersnatch_vrf_pre_output(
|
||||
&self,
|
||||
key_type: KeyTypeId,
|
||||
public: &bandersnatch::Public,
|
||||
input: &bandersnatch::vrf::VrfInput,
|
||||
) -> Result<Option<bandersnatch::vrf::VrfPreOutput>, Error> {
|
||||
self.vrf_pre_output::<bandersnatch::Pair>(key_type, public, input)
|
||||
}
|
||||
|
||||
#[cfg(feature = "bls-experimental")]
|
||||
fn bls381_public_keys(&self, key_type: KeyTypeId) -> Vec<bls381::Public> {
|
||||
self.public_keys::<bls381::Pair>(key_type)
|
||||
}
|
||||
|
||||
#[cfg(feature = "bls-experimental")]
|
||||
fn bls381_generate_new(
|
||||
&self,
|
||||
key_type: KeyTypeId,
|
||||
seed: Option<&str>,
|
||||
) -> Result<bls381::Public, Error> {
|
||||
self.generate_new::<bls381::Pair>(key_type, seed)
|
||||
}
|
||||
|
||||
#[cfg(feature = "bls-experimental")]
|
||||
fn bls381_sign(
|
||||
&self,
|
||||
key_type: KeyTypeId,
|
||||
public: &bls381::Public,
|
||||
msg: &[u8],
|
||||
) -> Result<Option<bls381::Signature>, Error> {
|
||||
self.sign::<bls381::Pair>(key_type, public, msg)
|
||||
}
|
||||
|
||||
#[cfg(feature = "bls-experimental")]
|
||||
fn bls381_generate_proof_of_possession(
|
||||
&self,
|
||||
key_type: KeyTypeId,
|
||||
public: &bls381::Public,
|
||||
owner: &[u8],
|
||||
) -> Result<Option<bls381::ProofOfPossession>, Error> {
|
||||
self.generate_proof_of_possession::<bls381::Pair>(key_type, public, owner)
|
||||
}
|
||||
|
||||
#[cfg(feature = "bls-experimental")]
|
||||
fn ecdsa_bls381_public_keys(&self, key_type: KeyTypeId) -> Vec<ecdsa_bls381::Public> {
|
||||
self.public_keys::<ecdsa_bls381::Pair>(key_type)
|
||||
}
|
||||
|
||||
#[cfg(feature = "bls-experimental")]
|
||||
fn ecdsa_bls381_generate_new(
|
||||
&self,
|
||||
key_type: KeyTypeId,
|
||||
seed: Option<&str>,
|
||||
) -> Result<ecdsa_bls381::Public, Error> {
|
||||
let pubkey = self.generate_new::<ecdsa_bls381::Pair>(key_type, seed)?;
|
||||
|
||||
let s: String = self
|
||||
.keys
|
||||
.read()
|
||||
.get(&key_type)
|
||||
.and_then(|inner| inner.get(pubkey.as_slice()).map(|s| s.to_string()))
|
||||
.expect("Can Retrieve Seed");
|
||||
|
||||
// This is done to give the keystore access to individual keys, this is necessary to avoid
|
||||
// redundant host functions for paired keys and re-use host functions implemented for each
|
||||
// element of the pair.
|
||||
self.generate_new::<ecdsa::Pair>(key_type, Some(&s))
|
||||
.expect("seed slice is valid");
|
||||
self.generate_new::<bls381::Pair>(key_type, Some(&s))
|
||||
.expect("seed slice is valid");
|
||||
|
||||
Ok(pubkey)
|
||||
}
|
||||
|
||||
#[cfg(feature = "bls-experimental")]
|
||||
fn ecdsa_bls381_sign(
|
||||
&self,
|
||||
key_type: KeyTypeId,
|
||||
public: &ecdsa_bls381::Public,
|
||||
msg: &[u8],
|
||||
) -> Result<Option<ecdsa_bls381::Signature>, Error> {
|
||||
self.sign::<ecdsa_bls381::Pair>(key_type, public, msg)
|
||||
}
|
||||
|
||||
#[cfg(feature = "bls-experimental")]
|
||||
fn ecdsa_bls381_sign_with_keccak256(
|
||||
&self,
|
||||
key_type: KeyTypeId,
|
||||
public: &ecdsa_bls381::Public,
|
||||
msg: &[u8],
|
||||
) -> Result<Option<ecdsa_bls381::Signature>, Error> {
|
||||
let sig = self
|
||||
.pair::<ecdsa_bls381::Pair>(key_type, public)
|
||||
.map(|pair| pair.sign_with_hasher::<KeccakHasher>(msg));
|
||||
Ok(sig)
|
||||
}
|
||||
|
||||
fn insert(&self, key_type: KeyTypeId, suri: &str, public: &[u8]) -> Result<(), ()> {
|
||||
self.keys
|
||||
.write()
|
||||
.entry(key_type)
|
||||
.or_default()
|
||||
.insert(public.to_owned(), suri.to_string());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn keys(&self, key_type: KeyTypeId) -> Result<Vec<Vec<u8>>, Error> {
|
||||
let keys = self
|
||||
.keys
|
||||
.read()
|
||||
.get(&key_type)
|
||||
.map(|map| map.keys().cloned().collect())
|
||||
.unwrap_or_default();
|
||||
Ok(keys)
|
||||
}
|
||||
|
||||
fn has_keys(&self, public_keys: &[(Vec<u8>, KeyTypeId)]) -> bool {
|
||||
public_keys
|
||||
.iter()
|
||||
.all(|(k, t)| self.keys.read().get(t).and_then(|s| s.get(k)).is_some())
|
||||
}
|
||||
}
|
||||
|
||||
impl Into<KeystorePtr> for MemoryKeystore {
|
||||
fn into(self) -> KeystorePtr {
|
||||
Arc::new(self)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use pezsp_core::{
|
||||
sr25519,
|
||||
testing::{ECDSA, ED25519, SR25519},
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn store_key_and_extract() {
|
||||
let store = MemoryKeystore::new();
|
||||
|
||||
let public = store.ed25519_generate_new(ED25519, None).expect("Generates key");
|
||||
|
||||
let public_keys = store.ed25519_public_keys(ED25519);
|
||||
|
||||
assert!(public_keys.contains(&public.into()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn store_unknown_and_extract_it() {
|
||||
let store = MemoryKeystore::new();
|
||||
|
||||
let secret_uri = "//Alice";
|
||||
let key_pair = sr25519::Pair::from_string(secret_uri, None).expect("Generates key pair");
|
||||
|
||||
store
|
||||
.insert(SR25519, secret_uri, key_pair.public().as_ref())
|
||||
.expect("Inserts unknown key");
|
||||
|
||||
let public_keys = store.sr25519_public_keys(SR25519);
|
||||
|
||||
assert!(public_keys.contains(&key_pair.public().into()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sr25519_vrf_sign() {
|
||||
let store = MemoryKeystore::new();
|
||||
|
||||
let secret_uri = "//Alice";
|
||||
let key_pair = sr25519::Pair::from_string(secret_uri, None).expect("Generates key pair");
|
||||
|
||||
let data = sr25519::vrf::VrfInput::new(
|
||||
b"Test",
|
||||
&[
|
||||
(b"one", &1_u64.to_le_bytes()),
|
||||
(b"two", &2_u64.to_le_bytes()),
|
||||
(b"three", "test".as_bytes()),
|
||||
],
|
||||
)
|
||||
.into_sign_data();
|
||||
|
||||
let result = store.sr25519_vrf_sign(SR25519, &key_pair.public(), &data);
|
||||
assert!(result.unwrap().is_none());
|
||||
|
||||
store
|
||||
.insert(SR25519, secret_uri, key_pair.public().as_ref())
|
||||
.expect("Inserts unknown key");
|
||||
|
||||
let result = store.sr25519_vrf_sign(SR25519, &key_pair.public(), &data);
|
||||
|
||||
assert!(result.unwrap().is_some());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sr25519_vrf_pre_output() {
|
||||
let store = MemoryKeystore::new();
|
||||
|
||||
let secret_uri = "//Alice";
|
||||
let pair = sr25519::Pair::from_string(secret_uri, None).expect("Generates key pair");
|
||||
|
||||
let input = sr25519::vrf::VrfInput::new(
|
||||
b"Test",
|
||||
&[
|
||||
(b"one", &1_u64.to_le_bytes()),
|
||||
(b"two", &2_u64.to_le_bytes()),
|
||||
(b"three", "test".as_bytes()),
|
||||
],
|
||||
);
|
||||
|
||||
let result = store.sr25519_vrf_pre_output(SR25519, &pair.public(), &input);
|
||||
assert!(result.unwrap().is_none());
|
||||
|
||||
store
|
||||
.insert(SR25519, secret_uri, pair.public().as_ref())
|
||||
.expect("Inserts unknown key");
|
||||
|
||||
let pre_output =
|
||||
store.sr25519_vrf_pre_output(SR25519, &pair.public(), &input).unwrap().unwrap();
|
||||
|
||||
let result = pre_output.make_bytes::<32>(b"rand", &input, &pair.public());
|
||||
assert!(result.is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ecdsa_sign_prehashed_works() {
|
||||
let store = MemoryKeystore::new();
|
||||
|
||||
let suri = "//Alice";
|
||||
let pair = ecdsa::Pair::from_string(suri, None).unwrap();
|
||||
|
||||
// Let's pretend this to be the hash output as content doesn't really matter here.
|
||||
let hash = [0xff; 32];
|
||||
|
||||
// no key in key store
|
||||
let res = store.ecdsa_sign_prehashed(ECDSA, &pair.public(), &hash).unwrap();
|
||||
assert!(res.is_none());
|
||||
|
||||
// insert key, sign again
|
||||
store.insert(ECDSA, suri, pair.public().as_ref()).unwrap();
|
||||
|
||||
let res = store.ecdsa_sign_prehashed(ECDSA, &pair.public(), &hash).unwrap();
|
||||
assert!(res.is_some());
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "bls-experimental")]
|
||||
fn ecdsa_bls381_sign_with_keccak_works() {
|
||||
use pezsp_core::testing::ECDSA_BLS377;
|
||||
|
||||
let store = MemoryKeystore::new();
|
||||
|
||||
let suri = "//Alice";
|
||||
let pair = ecdsa_bls381::Pair::from_string(suri, None).unwrap();
|
||||
|
||||
let msg = b"this should be a normal unhashed message not a hash of a message because bls scheme comes with its own hashing";
|
||||
|
||||
// insert key, sign again
|
||||
store.insert(ECDSA_BLS377, suri, pair.public().as_ref()).unwrap();
|
||||
|
||||
let res = store
|
||||
.ecdsa_bls381_sign_with_keccak256(ECDSA_BLS377, &pair.public(), &msg[..])
|
||||
.unwrap();
|
||||
|
||||
assert!(res.is_some());
|
||||
|
||||
// does not verify with default out-of-the-box verification
|
||||
assert!(!ecdsa_bls381::Pair::verify(&res.unwrap(), &msg[..], &pair.public()));
|
||||
|
||||
// should verify using keccak256 as hasher
|
||||
assert!(ecdsa_bls381::Pair::verify_with_hasher::<KeccakHasher>(
|
||||
&res.unwrap(),
|
||||
msg,
|
||||
&pair.public()
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "bls-experimental")]
|
||||
fn ecdsa_bls381_generate_with_none_works() {
|
||||
use pezsp_core::testing::ECDSA_BLS381;
|
||||
|
||||
let store = MemoryKeystore::new();
|
||||
let ecdsa_bls381_key =
|
||||
store.ecdsa_bls381_generate_new(ECDSA_BLS381, None).expect("Can generate key..");
|
||||
|
||||
let ecdsa_keys = store.ecdsa_public_keys(ECDSA_BLS381);
|
||||
let bls381_keys = store.bls381_public_keys(ECDSA_BLS381);
|
||||
let ecdsa_bls381_keys = store.ecdsa_bls381_public_keys(ECDSA_BLS381);
|
||||
|
||||
assert_eq!(ecdsa_keys.len(), 1);
|
||||
assert_eq!(bls381_keys.len(), 1);
|
||||
assert_eq!(ecdsa_bls381_keys.len(), 1);
|
||||
|
||||
let ecdsa_key = ecdsa_keys[0];
|
||||
let bls381_key = bls381_keys[0];
|
||||
|
||||
let mut combined_key_raw = [0u8; ecdsa_bls381::PUBLIC_KEY_LEN];
|
||||
combined_key_raw[..ecdsa::PUBLIC_KEY_SERIALIZED_SIZE].copy_from_slice(ecdsa_key.as_ref());
|
||||
combined_key_raw[ecdsa::PUBLIC_KEY_SERIALIZED_SIZE..].copy_from_slice(bls381_key.as_ref());
|
||||
let combined_key = ecdsa_bls381::Public::from_raw(combined_key_raw);
|
||||
|
||||
assert_eq!(combined_key, ecdsa_bls381_key);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "bls-experimental")]
|
||||
fn ecdsa_bls381_generate_with_seed_works() {
|
||||
use pezsp_core::testing::ECDSA_BLS381;
|
||||
|
||||
let store = MemoryKeystore::new();
|
||||
let ecdsa_bls381_key = store
|
||||
.ecdsa_bls381_generate_new(ECDSA_BLS381, Some("//Alice"))
|
||||
.expect("Can generate key..");
|
||||
|
||||
let ecdsa_keys = store.ecdsa_public_keys(ECDSA_BLS381);
|
||||
let bls381_keys = store.bls381_public_keys(ECDSA_BLS381);
|
||||
let ecdsa_bls381_keys = store.ecdsa_bls381_public_keys(ECDSA_BLS381);
|
||||
|
||||
assert_eq!(ecdsa_keys.len(), 1);
|
||||
assert_eq!(bls381_keys.len(), 1);
|
||||
assert_eq!(ecdsa_bls381_keys.len(), 1);
|
||||
|
||||
let ecdsa_key = ecdsa_keys[0];
|
||||
let bls381_key = bls381_keys[0];
|
||||
|
||||
let mut combined_key_raw = [0u8; ecdsa_bls381::PUBLIC_KEY_LEN];
|
||||
combined_key_raw[..ecdsa::PUBLIC_KEY_SERIALIZED_SIZE].copy_from_slice(ecdsa_key.as_ref());
|
||||
combined_key_raw[ecdsa::PUBLIC_KEY_SERIALIZED_SIZE..].copy_from_slice(bls381_key.as_ref());
|
||||
let combined_key = ecdsa_bls381::Public::from_raw(combined_key_raw);
|
||||
|
||||
assert_eq!(combined_key, ecdsa_bls381_key);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "bandersnatch-experimental")]
|
||||
fn bandersnatch_vrf_sign() {
|
||||
use pezsp_core::testing::BANDERSNATCH;
|
||||
|
||||
let store = MemoryKeystore::new();
|
||||
|
||||
let secret_uri = "//Alice";
|
||||
let key_pair =
|
||||
bandersnatch::Pair::from_string(secret_uri, None).expect("Generates key pair");
|
||||
let sign_data = bandersnatch::vrf::VrfSignData::new(b"vrf_input", b"aux_data");
|
||||
|
||||
let result = store.bandersnatch_vrf_sign(BANDERSNATCH, &key_pair.public(), &sign_data);
|
||||
assert!(result.unwrap().is_none());
|
||||
|
||||
store
|
||||
.insert(BANDERSNATCH, secret_uri, key_pair.public().as_ref())
|
||||
.expect("Inserts unknown key");
|
||||
|
||||
let result = store.bandersnatch_vrf_sign(BANDERSNATCH, &key_pair.public(), &sign_data);
|
||||
|
||||
assert!(result.unwrap().is_some());
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "bandersnatch-experimental")]
|
||||
fn bandersnatch_ring_vrf_sign() {
|
||||
use pezsp_core::testing::BANDERSNATCH;
|
||||
|
||||
let store = MemoryKeystore::new();
|
||||
|
||||
let ring_ctx = bandersnatch::ring_vrf::RingContext::<1024>::new_testing();
|
||||
|
||||
let mut pks: Vec<_> = (0..16)
|
||||
.map(|i| bandersnatch::Pair::from_seed(&[i as u8; 32]).public())
|
||||
.collect();
|
||||
|
||||
let prover_idx = 3;
|
||||
let prover = ring_ctx.prover(&pks, prover_idx);
|
||||
|
||||
let secret_uri = "//Alice";
|
||||
let pair = bandersnatch::Pair::from_string(secret_uri, None).expect("Generates key pair");
|
||||
pks[prover_idx] = pair.public();
|
||||
|
||||
let sign_data = bandersnatch::vrf::VrfSignData::new(b"vrf_input", b"aux_data");
|
||||
|
||||
let result =
|
||||
store.bandersnatch_ring_vrf_sign(BANDERSNATCH, &pair.public(), &sign_data, &prover);
|
||||
assert!(result.unwrap().is_none());
|
||||
|
||||
store
|
||||
.insert(BANDERSNATCH, secret_uri, pair.public().as_ref())
|
||||
.expect("Inserts unknown key");
|
||||
|
||||
let result =
|
||||
store.bandersnatch_ring_vrf_sign(BANDERSNATCH, &pair.public(), &sign_data, &prover);
|
||||
|
||||
assert!(result.unwrap().is_some());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user