Skip to content

Commit 8e63eac

Browse files
authored
change: Simplify cross chain public key usage (#1057)
1 parent ead5329 commit 8e63eac

File tree

8 files changed

+61
-65
lines changed

8 files changed

+61
-65
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

changelog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ session in order to make `pallet_session` use authorities with less delay.
2626
## Added
2727

2828
* Added `partner-chains-node smart-contracts upsert-script` command for inserting and updating versioned scripts.
29+
* `cross_chain_app` module in `sidechain_domain`, implementing the cross-chain key
30+
types using Substrate's `app_crypto` macro. This should be a drop-in replacement
31+
for the analogous cross-chain key types that previously had to be defined by each
32+
Partner Chain separately for use in `pallet_session_validator_management` and
33+
other pallets that use cross-chain keys.
2934

3035
# v1.8.0
3136

demo/runtime/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ sp-sidechain = { workspace = true }
7474
pallet-sidechain = { workspace = true }
7575
pallet-session-validator-management = { workspace = true }
7676
sp-session-validator-management = { workspace = true, features = ["serde"] }
77-
sidechain-domain = { workspace = true, features = ["serde"] }
77+
sidechain-domain = { workspace = true, features = ["serde", "app-crypto"] }
7878
sidechain-slots = { workspace = true }
7979
pallet-address-associations = { workspace = true }
8080
pallet-block-producer-metadata = { workspace = true }

demo/runtime/src/lib.rs

Lines changed: 2 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -111,54 +111,6 @@ pub mod opaque {
111111
/// Opaque block identifier type.
112112
pub type BlockId = generic::BlockId<Block>;
113113

114-
pub const CROSS_CHAIN: KeyTypeId = KeyTypeId(*b"crch");
115-
pub struct CrossChainRuntimeAppPublic;
116-
117-
pub mod cross_chain_app {
118-
use super::CROSS_CHAIN;
119-
use parity_scale_codec::MaxEncodedLen;
120-
use sidechain_domain::SidechainPublicKey;
121-
use sp_core::crypto::AccountId32;
122-
use sp_runtime::MultiSigner;
123-
use sp_runtime::app_crypto::{app_crypto, ecdsa};
124-
use sp_runtime::traits::IdentifyAccount;
125-
use sp_std::vec::Vec;
126-
127-
app_crypto!(ecdsa, CROSS_CHAIN);
128-
impl MaxEncodedLen for Signature {
129-
fn max_encoded_len() -> usize {
130-
ecdsa::Signature::max_encoded_len()
131-
}
132-
}
133-
134-
impl From<Signature> for Vec<u8> {
135-
fn from(value: Signature) -> Self {
136-
value.into_inner().0.to_vec()
137-
}
138-
}
139-
140-
impl From<Public> for AccountId32 {
141-
fn from(value: Public) -> Self {
142-
MultiSigner::from(ecdsa::Public::from(value)).into_account()
143-
}
144-
}
145-
146-
impl From<Public> for Vec<u8> {
147-
fn from(value: Public) -> Self {
148-
value.into_inner().0.to_vec()
149-
}
150-
}
151-
152-
impl TryFrom<SidechainPublicKey> for Public {
153-
type Error = SidechainPublicKey;
154-
fn try_from(pubkey: SidechainPublicKey) -> Result<Self, Self::Error> {
155-
let cross_chain_public_key =
156-
Public::try_from(pubkey.0.as_slice()).map_err(|_| pubkey)?;
157-
Ok(cross_chain_public_key)
158-
}
159-
}
160-
}
161-
162114
impl_opaque_keys! {
163115
#[derive(MaxEncodedLen, PartialOrd, Ord)]
164116
pub struct SessionKeys {
@@ -181,7 +133,7 @@ pub mod opaque {
181133
}
182134
}
183135

184-
pub type CrossChainPublic = opaque::cross_chain_app::Public;
136+
pub type CrossChainPublic = sidechain_domain::cross_chain_app::Public;
185137

186138
// To learn more about runtime versioning, see:
187139
// https://docs.substrate.io/main-docs/build/upgrade#runtime-versioning
@@ -400,7 +352,7 @@ impl pallet_session_validator_management::Config for Runtime {
400352
input: AuthoritySelectionInputs,
401353
sidechain_epoch: ScEpochNumber,
402354
) -> Option<BoundedVec<Self::CommitteeMember, Self::MaxValidators>> {
403-
select_authorities::<opaque::cross_chain_app::Public, SessionKeys, MaxValidators>(
355+
select_authorities::<sidechain_domain::cross_chain_app::Public, SessionKeys, MaxValidators>(
404356
Sidechain::genesis_utxo(),
405357
input,
406358
sidechain_epoch,

toolkit/committee-selection/pallet/src/lib.rs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -93,20 +93,6 @@
9393
//! }
9494
//! ```
9595
//!
96-
//! In addition to the session keys, the runtime needs to define an ECDSA key type to represent
97-
//! the `cross-chain key`:
98-
//! ```rust
99-
//! pub mod cross_chain_app {
100-
//! use sp_runtime::KeyTypeId;
101-
//! use sp_runtime::app_crypto::{ app_crypto, ecdsa };
102-
//! pub const CROSS_CHAIN: KeyTypeId = KeyTypeId(*b"crch");
103-
//! app_crypto!(ecdsa, CROSS_CHAIN);
104-
//! }
105-
//! pub type CrossChainPublic = cross_chain_app::Public;
106-
//! ```
107-
//!
108-
//! This key serves as the identity of a Partner Chain user across all chains in the ecosystem.
109-
//!
11096
//! ### Adding the pallet
11197
//!
11298
//! The pallet should be added to the runtime _before_ `pallet_session`, but after the consensus
@@ -140,6 +126,8 @@
140126
//! defined by other crates and in previous steps:
141127
//!
142128
//! ```rust, ignore
129+
//! use sidechain_domain::cross_chain_app::Public as CrossChainPublic;
130+
//!
143131
//! impl pallet_session_validator_management::Config for Runtime {
144132
//! type MaxValidators = MaxValidators;
145133
//! type AuthorityId = CrossChainPublic;

toolkit/sidechain/domain/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ secp256k1 = { workspace = true, features = ["hashes", "alloc"] }
3434
ed25519-zebra = { workspace = true, optional = true }
3535
k256 = { workspace = true }
3636
derive-where = { workspace = true }
37+
sp-runtime = { workspace = true, optional = true }
3738

3839
[dev-dependencies]
3940
serde_json = { workspace = true }
@@ -55,5 +56,7 @@ std = [
5556
"secp256k1/global-context",
5657
"ed25519-zebra",
5758
"ed25519-zebra/std",
59+
"sp-runtime?/std",
5860
]
5961
serde = ["dep:serde", "scale-info/serde", "sp-core/serde"]
62+
app-crypto = ["dep:sp-runtime"]
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//! Substrate key pair types for Partner Chain cross-chain keys
2+
3+
use crate::SidechainPublicKey;
4+
use parity_scale_codec::MaxEncodedLen;
5+
use sp_core::crypto::AccountId32;
6+
use sp_runtime::app_crypto::{app_crypto, ecdsa};
7+
use sp_runtime::traits::IdentifyAccount;
8+
use sp_runtime::{KeyTypeId, MultiSigner};
9+
use sp_std::vec::Vec;
10+
11+
/// Key type for the cross-chain keys
12+
pub const KEY_TYPE_ID: KeyTypeId = KeyTypeId(*b"crch");
13+
14+
app_crypto!(ecdsa, KEY_TYPE_ID);
15+
16+
impl MaxEncodedLen for Signature {
17+
fn max_encoded_len() -> usize {
18+
ecdsa::Signature::max_encoded_len()
19+
}
20+
}
21+
22+
impl From<Signature> for Vec<u8> {
23+
fn from(value: Signature) -> Self {
24+
value.into_inner().0.to_vec()
25+
}
26+
}
27+
28+
impl From<Public> for AccountId32 {
29+
fn from(value: Public) -> Self {
30+
MultiSigner::from(ecdsa::Public::from(value)).into_account()
31+
}
32+
}
33+
34+
impl From<Public> for Vec<u8> {
35+
fn from(value: Public) -> Self {
36+
value.into_inner().0.to_vec()
37+
}
38+
}
39+
40+
impl TryFrom<SidechainPublicKey> for Public {
41+
type Error = SidechainPublicKey;
42+
fn try_from(pubkey: SidechainPublicKey) -> Result<Self, Self::Error> {
43+
Public::try_from(pubkey.0.as_slice()).map_err(|_| pubkey)
44+
}
45+
}

toolkit/sidechain/domain/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#![deny(missing_docs)]
77

88
pub mod byte_string;
9+
#[cfg(feature = "app-crypto")]
10+
pub mod cross_chain_app;
911
pub mod crypto;
1012
pub mod mainchain_epoch;
1113

0 commit comments

Comments
 (0)