Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapt to associate and replay attack prevention sig #1085

Merged
merged 6 commits into from
Dec 15, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions precompiles/crowdloan-rewards/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ parameter_types! {
pub const TestInitialized: bool = false;
pub const TestInitializationPayment: Perbill = Perbill::from_percent(20);
pub const RelaySignaturesThreshold: Perbill = Perbill::from_percent(100);
pub const TestSigantureNetworkIdentifier: &'static [u8] = b"test-";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub const TestSigantureNetworkIdentifier: &'static [u8] = b"test-";
pub const TestSignatureNetworkIdentifier: &'static [u8] = b"test-";

}

impl pallet_crowdloan_rewards::Config for Runtime {
Expand All @@ -249,8 +250,11 @@ impl pallet_crowdloan_rewards::Config for Runtime {
type MinimumReward = TestMinimumReward;
type RewardCurrency = Balances;
type RelayChainAccountId = [u8; 32];
type RewardAddressAssociateOrigin = EnsureSigned<Self::AccountId>;
type RewardAddressRelayVoteThreshold = RelaySignaturesThreshold;
type RewardAddressChangeOrigin = EnsureSigned<Self::AccountId>;
type SignatureNetworkIdentifier = TestSigantureNetworkIdentifier;

type VestingBlockNumber = cumulus_primitives_core::relay_chain::BlockNumber;
type VestingBlockProvider =
cumulus_pallet_parachain_system::RelaychainBlockNumberProvider<Self>;
Expand Down
4 changes: 4 additions & 0 deletions runtime/moonbase/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,8 @@ parameter_types! {
pub const InitializationPayment: Perbill = Perbill::from_percent(30);
pub const MaxInitContributorsBatchSizes: u32 = 500;
pub const RelaySignaturesThreshold: Perbill = Perbill::from_percent(100);
pub const SignatureNetworkIdentifier: &'static [u8] = b"moonbase-";

}

impl pallet_crowdloan_rewards::Config for Runtime {
Expand All @@ -792,8 +794,10 @@ impl pallet_crowdloan_rewards::Config for Runtime {
type MinimumReward = MinimumReward;
type RewardCurrency = Balances;
type RelayChainAccountId = [u8; 32];
type RewardAddressAssociateOrigin = EnsureSigned<Self::AccountId>;
type RewardAddressChangeOrigin = EnsureSigned<Self::AccountId>;
type RewardAddressRelayVoteThreshold = RelaySignaturesThreshold;
type SignatureNetworkIdentifier = SignatureNetworkIdentifier;
type VestingBlockNumber = cumulus_primitives_core::relay_chain::BlockNumber;
type VestingBlockProvider =
cumulus_pallet_parachain_system::RelaychainBlockNumberProvider<Self>;
Expand Down
4 changes: 3 additions & 1 deletion runtime/moonbase/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -634,8 +634,10 @@ fn initialize_crowdloan_address_and_change_with_relay_key_sig() {
let public1 = pair1.public();
let public2 = pair2.public();

// signature is new_account || previous_account
// signature:
// WRAP_BYTES|| NetworkIdentifier|| new_account || previous_account || WRAP_BYTES
let mut message = pallet_crowdloan_rewards::WRAPPED_BYTES_PREFIX.to_vec();
message.append(&mut b"moonbase-".to_vec());
message.append(&mut AccountId::from(DAVE).encode());
message.append(&mut AccountId::from(CHARLIE).encode());
message.append(&mut pallet_crowdloan_rewards::WRAPPED_BYTES_POSTFIX.to_vec());
Expand Down
8 changes: 5 additions & 3 deletions runtime/moonbeam/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ use frame_support::{
},
PalletId,
};
use frame_system::{EnsureOneOf, EnsureRoot};
use frame_system::{EnsureOneOf, EnsureRoot, EnsureSigned};
pub use moonbeam_core_primitives::{
AccountId, AccountIndex, Address, Balance, BlockNumber, DigestItem, Hash, Header, Index,
Signature,
Expand Down Expand Up @@ -740,6 +740,7 @@ parameter_types! {
pub const InitializationPayment: Perbill = Perbill::from_percent(30);
pub const MaxInitContributorsBatchSizes: u32 = 500;
pub const RelaySignaturesThreshold: Perbill = Perbill::from_percent(100);
pub const SignatureNetworkIdentifier: &'static [u8] = b"moonbeam-";
}

impl pallet_crowdloan_rewards::Config for Runtime {
Expand All @@ -750,9 +751,10 @@ impl pallet_crowdloan_rewards::Config for Runtime {
type MinimumReward = MinimumReward;
type RewardCurrency = Balances;
type RelayChainAccountId = [u8; 32];
// This will get accessible to users in future phases.
type RewardAddressChangeOrigin = EnsureRoot<Self::AccountId>;
type RewardAddressAssociateOrigin = EnsureSigned<Self::AccountId>;
type RewardAddressChangeOrigin = EnsureSigned<Self::AccountId>;
type RewardAddressRelayVoteThreshold = RelaySignaturesThreshold;
type SignatureNetworkIdentifier = SignatureNetworkIdentifier;
type VestingBlockNumber = cumulus_primitives_core::relay_chain::BlockNumber;
type VestingBlockProvider =
cumulus_pallet_parachain_system::RelaychainBlockNumberProvider<Self>;
Expand Down
1 change: 1 addition & 0 deletions runtime/moonbeam/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,7 @@ fn initialize_crowdloan_address_and_change_with_relay_key_sig() {

// signature is new_account || previous_account
let mut message = pallet_crowdloan_rewards::WRAPPED_BYTES_PREFIX.to_vec();
message.append(&mut b"moonbeam-".to_vec());
message.append(&mut AccountId::from(DAVE).encode());
message.append(&mut AccountId::from(CHARLIE).encode());
message.append(&mut pallet_crowdloan_rewards::WRAPPED_BYTES_POSTFIX.to_vec());
Expand Down
4 changes: 4 additions & 0 deletions runtime/moonriver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,8 @@ parameter_types! {
pub const InitializationPayment: Perbill = Perbill::from_percent(30);
pub const MaxInitContributorsBatchSizes: u32 = 500;
pub const RelaySignaturesThreshold: Perbill = Perbill::from_percent(100);
pub const SignatureNetworkIdentifier: &'static [u8] = b"moonriver-";

}

impl pallet_crowdloan_rewards::Config for Runtime {
Expand All @@ -731,8 +733,10 @@ impl pallet_crowdloan_rewards::Config for Runtime {
type MinimumReward = MinimumReward;
type RewardCurrency = Balances;
type RelayChainAccountId = [u8; 32];
type RewardAddressAssociateOrigin = EnsureSigned<Self::AccountId>;
type RewardAddressChangeOrigin = EnsureSigned<Self::AccountId>;
type RewardAddressRelayVoteThreshold = RelaySignaturesThreshold;
type SignatureNetworkIdentifier = SignatureNetworkIdentifier;
type VestingBlockNumber = cumulus_primitives_core::relay_chain::BlockNumber;
type VestingBlockProvider =
cumulus_pallet_parachain_system::RelaychainBlockNumberProvider<Self>;
Expand Down
4 changes: 3 additions & 1 deletion runtime/moonriver/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -622,8 +622,10 @@ fn initialize_crowdloan_address_and_change_with_relay_key_sig() {
let public1 = pair1.public();
let public2 = pair2.public();

// signature is new_account || previous_account
// signature:
// WRAP_BYTES|| NetworkIdentifier|| new_account || previous_account || WRAP_BYTES
let mut message = pallet_crowdloan_rewards::WRAPPED_BYTES_PREFIX.to_vec();
message.append(&mut b"moonriver-".to_vec());
message.append(&mut AccountId::from(DAVE).encode());
message.append(&mut AccountId::from(CHARLIE).encode());
message.append(&mut pallet_crowdloan_rewards::WRAPPED_BYTES_POSTFIX.to_vec());
Expand Down
9 changes: 8 additions & 1 deletion tests/tests/test-crowdloan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -693,9 +693,15 @@ describeDevMoonbeam("Crowdloan", (context) => {
// toAssociateAccount should not be in accounts payable
expect(await getAccountPayable(context, toAssociateAccount.address)).to.be.null;

let message = new Uint8Array([
...stringToU8a("<Bytes>"),
...stringToU8a("moonbase-"),
...toAssociateAccount.addressRaw,
...stringToU8a("</Bytes>"),
]);
// Construct the signature
let signature = {};
signature["Ed25519"] = relayAccount.sign(toAssociateAccount.address);
signature["Ed25519"] = relayAccount.sign(message);

// Associate the identity
await context.polkadotApi.tx.crowdloanRewards
Expand Down Expand Up @@ -794,6 +800,7 @@ describeDevMoonbeam("Crowdloan", (context) => {

let message = new Uint8Array([
...stringToU8a("<Bytes>"),
...stringToU8a("moonbase-"),
...toAssociateAccount.addressRaw,
...firstAccount.addressRaw,
...stringToU8a("</Bytes>"),
Expand Down