Skip to content

Commit 3e3be01

Browse files
authored
Merge pull request #1487 from opentensor/associate-evm-key
Create extrinsic to allow EVM key association
2 parents 433be65 + 77cae16 commit 3e3be01

File tree

12 files changed

+401
-6
lines changed

12 files changed

+401
-6
lines changed

Diff for: Cargo.lock

+5-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ futures = "0.3.30"
6767
hex = { version = "0.4", default-features = false }
6868
hex-literal = "0.4.1"
6969
jsonrpsee = { version = "0.24.4", default-features = false }
70+
libsecp256k1 = { version = "0.7.2", default-features = false }
7071
log = { version = "0.4.21", default-features = false }
7172
memmap2 = "0.9.4"
7273
ndarray = { version = "0.15.6", default-features = false }

Diff for: pallets/subtensor/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ serde_bytes = { workspace = true, features = ["alloc"] }
3434
serde_with = { workspace = true, features = ["macros"] }
3535
sp-runtime = { workspace = true }
3636
sp-std = { workspace = true }
37+
libsecp256k1 = { workspace = true }
3738
log = { workspace = true }
3839
substrate-fixed = { workspace = true }
3940
pallet-transaction-payment = { workspace = true }
@@ -91,6 +92,7 @@ std = [
9192
"sp-tracing/std",
9293
"sp-version/std",
9394
"hex/std",
95+
"libsecp256k1/std",
9496
"log/std",
9597
"ndarray/std",
9698
"serde/std",

Diff for: pallets/subtensor/src/lib.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ pub mod pallet {
7777
};
7878
use frame_system::pallet_prelude::*;
7979
use pallet_drand::types::RoundNumber;
80-
use sp_core::{ConstU32, H256};
80+
use sp_core::{ConstU32, H160, H256};
8181
use sp_runtime::traits::{Dispatchable, TrailingZeroInput};
8282
use sp_std::collections::vec_deque::VecDeque;
8383
use sp_std::vec;
@@ -1561,6 +1561,14 @@ pub mod pallet {
15611561
OptionQuery,
15621562
>;
15631563

1564+
/// =============================
1565+
/// ==== EVM related storage ====
1566+
/// =============================
1567+
#[pallet::storage]
1568+
/// --- DMAP (netuid, uid) --> (H160, last_block_where_ownership_was_proven)
1569+
pub type AssociatedEvmAddress<T: Config> =
1570+
StorageDoubleMap<_, Twox64Concat, u16, Twox64Concat, u16, (H160, u64), OptionQuery>;
1571+
15641572
/// ==================
15651573
/// ==== Genesis =====
15661574
/// ==================

Diff for: pallets/subtensor/src/macros/dispatches.rs

+45
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ mod dispatches {
99
use frame_support::traits::schedule::DispatchTime;
1010
use frame_support::traits::schedule::v3::Anon as ScheduleAnon;
1111
use frame_system::pallet_prelude::BlockNumberFor;
12+
use sp_core::ecdsa::Signature;
1213
use sp_runtime::traits::Saturating;
1314

1415
use crate::MAX_CRV3_COMMIT_SIZE_BYTES;
@@ -1929,6 +1930,50 @@ mod dispatches {
19291930
Ok(())
19301931
}
19311932

1933+
/// Attempts to associate a hotkey with an EVM key.
1934+
///
1935+
/// The signature will be checked to see if the recovered public key matches the `evm_key` provided.
1936+
///
1937+
/// The EVM key is expected to sign the message according to this formula to produce the signature:
1938+
/// ```text
1939+
/// keccak_256(hotkey ++ keccak_256(block_number))
1940+
/// ```
1941+
///
1942+
/// # Arguments
1943+
/// * `origin` - The origin of the transaction, which must be signed by the coldkey that owns the `hotkey`.
1944+
/// * `netuid` - The netuid that the `hotkey` belongs to.
1945+
/// * `hotkey` - The hotkey associated with the `origin`.
1946+
/// * `evm_key` - The EVM key to associate with the `hotkey`.
1947+
/// * `block_number` - The block number used in the `signature`.
1948+
/// * `signature` - A signed message by the `evm_key` containing the `hotkey` and the hashed `block_number`.
1949+
///
1950+
/// # Errors
1951+
/// Returns an error if:
1952+
/// * The transaction is not signed.
1953+
/// * The hotkey is not owned by the origin coldkey.
1954+
/// * The hotkey does not belong to the subnet identified by the netuid.
1955+
/// * The EVM key cannot be recovered from the signature.
1956+
/// * The EVM key recovered from the signature does not match the given EVM key.
1957+
///
1958+
/// # Events
1959+
/// May emit a `EvmKeyAssociated` event on success
1960+
#[pallet::call_index(93)]
1961+
#[pallet::weight((
1962+
Weight::from_parts(3_000_000, 0).saturating_add(T::DbWeight::get().reads_writes(2, 1)),
1963+
DispatchClass::Operational,
1964+
Pays::Yes
1965+
))]
1966+
pub fn associate_evm_key(
1967+
origin: T::RuntimeOrigin,
1968+
netuid: u16,
1969+
hotkey: T::AccountId,
1970+
evm_key: H160,
1971+
block_number: u64,
1972+
signature: Signature,
1973+
) -> DispatchResult {
1974+
Self::do_associate_evm_key(origin, netuid, hotkey, evm_key, block_number, signature)
1975+
}
1976+
19321977
/// Recycles alpha from a cold/hot key pair, reducing AlphaOut on a subnet
19331978
///
19341979
/// # Arguments

Diff for: pallets/subtensor/src/macros/errors.rs

+4
Original file line numberDiff line numberDiff line change
@@ -203,5 +203,9 @@ mod errors {
203203
NotEnoughAlphaOutToRecycle,
204204
/// Cannot burn or recycle TAO from root subnet
205205
CannotBurnOrRecycleOnRootSubnet,
206+
/// Public key cannot be recovered.
207+
UnableToRecoverPublicKey,
208+
/// Recovered public key is invalid.
209+
InvalidRecoveredPublicKey,
206210
}
207211
}

Diff for: pallets/subtensor/src/macros/events.rs

+12
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,18 @@ mod events {
294294
/// (coldkey, hotkey, amount, subnet_id)
295295
AlphaBurned(T::AccountId, T::AccountId, u64, u16),
296296

297+
/// An EVM key has been associated with a hotkey.
298+
EvmKeyAssociated {
299+
/// The subnet that the hotkey belongs to.
300+
netuid: u16,
301+
/// The hotkey associated with the EVM key.
302+
hotkey: T::AccountId,
303+
/// The EVM key being associated with the hotkey.
304+
evm_key: H160,
305+
/// The block where the association happened.
306+
block_associated: u64,
307+
},
308+
297309
/// CRV3 Weights have been successfully revealed.
298310
///
299311
/// - **netuid**: The network identifier.

0 commit comments

Comments
 (0)