mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-29 21:57:55 +00:00
Remove Parent Hash to Session mapping (#928)
* Adds a SigningContext type * Bump spec versions * Fixes requested changes * Bump ParachainHost api_version and guard signing_context call * Improve error message * If there is no signing_context api use default value Co-authored-by: Robert Habermeier <rphmeier@gmail.com>
This commit is contained in:
@@ -34,12 +34,11 @@ use std::{
|
||||
sync::Arc,
|
||||
};
|
||||
use codec::Encode;
|
||||
use polkadot_primitives::Hash;
|
||||
use polkadot_primitives::parachain::{
|
||||
Id as ParaId, Chain, DutyRoster, AbridgedCandidateReceipt,
|
||||
Statement as PrimitiveStatement,
|
||||
PoVBlock, ErasureChunk, ValidatorSignature, ValidatorIndex,
|
||||
ValidatorPair, ValidatorId,
|
||||
ValidatorPair, ValidatorId, SigningContext,
|
||||
};
|
||||
use primitives::Pair;
|
||||
|
||||
@@ -135,9 +134,13 @@ pub struct GroupInfo {
|
||||
/// Sign a table statement against a parent hash.
|
||||
/// The actual message signed is the encoded statement concatenated with the
|
||||
/// parent hash.
|
||||
pub fn sign_table_statement(statement: &Statement, key: &ValidatorPair, parent_hash: &Hash) -> ValidatorSignature {
|
||||
pub fn sign_table_statement(
|
||||
statement: &Statement,
|
||||
key: &ValidatorPair,
|
||||
signing_context: &SigningContext,
|
||||
) -> ValidatorSignature {
|
||||
let mut encoded = PrimitiveStatement::from(statement).encode();
|
||||
encoded.extend(parent_hash.as_ref());
|
||||
encoded.extend(signing_context.encode());
|
||||
|
||||
key.sign(&encoded)
|
||||
}
|
||||
@@ -147,12 +150,12 @@ pub fn check_statement(
|
||||
statement: &Statement,
|
||||
signature: &ValidatorSignature,
|
||||
signer: ValidatorId,
|
||||
parent_hash: &Hash,
|
||||
signing_context: &SigningContext,
|
||||
) -> bool {
|
||||
use runtime_primitives::traits::AppVerify;
|
||||
|
||||
let mut encoded = PrimitiveStatement::from(statement).encode();
|
||||
encoded.extend(parent_hash.as_ref());
|
||||
encoded.extend(signing_context.encode());
|
||||
|
||||
signature.verify(&encoded[..], &signer)
|
||||
}
|
||||
@@ -214,11 +217,19 @@ mod tests {
|
||||
fn sign_and_check_statement() {
|
||||
let statement: Statement = GenericStatement::Valid([1; 32].into());
|
||||
let parent_hash = [2; 32].into();
|
||||
let signing_context = SigningContext {
|
||||
session_index: Default::default(),
|
||||
parent_hash,
|
||||
};
|
||||
|
||||
let sig = sign_table_statement(&statement, &Sr25519Keyring::Alice.pair().into(), &parent_hash);
|
||||
let sig = sign_table_statement(&statement, &Sr25519Keyring::Alice.pair().into(), &signing_context);
|
||||
|
||||
assert!(check_statement(&statement, &sig, Sr25519Keyring::Alice.public().into(), &parent_hash));
|
||||
assert!(!check_statement(&statement, &sig, Sr25519Keyring::Alice.public().into(), &[0xff; 32].into()));
|
||||
assert!(!check_statement(&statement, &sig, Sr25519Keyring::Bob.public().into(), &parent_hash));
|
||||
let wrong_signing_context = SigningContext {
|
||||
session_index: Default::default(),
|
||||
parent_hash: [0xff; 32].into(),
|
||||
};
|
||||
assert!(check_statement(&statement, &sig, Sr25519Keyring::Alice.public().into(), &signing_context));
|
||||
assert!(!check_statement(&statement, &sig, Sr25519Keyring::Alice.public().into(), &wrong_signing_context));
|
||||
assert!(!check_statement(&statement, &sig, Sr25519Keyring::Bob.public().into(), &signing_context));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user