Skip to main content

pop_fork/
dev.rs

1// SPDX-License-Identifier: GPL-3.0
2
3//! Dev account utilities for forked chains.
4//!
5//! Provides well-known dev account public keys and helpers for building storage
6//! entries to fund them and set sudo.
7//!
8//! Two account sets are supported:
9//! - **Substrate** (32-byte sr25519): Alice, Bob, Charlie, Dave, Eve, Ferdie
10//! - **Ethereum** (20-byte H160): Alith, Baltathar, Charleth, Dorothy, Ethan, Faith
11
12use crate::strings::rpc_server::storage;
13
14/// Default balance for dev accounts: half of u128::MAX.
15pub const DEV_BALANCE: u128 = u128::MAX / 2;
16
17// ---------------------------------------------------------------------------
18// Substrate dev accounts (32-byte sr25519 public keys)
19// ---------------------------------------------------------------------------
20
21/// Well-known dev account: Alice.
22pub const ALICE: [u8; 32] = [
23	0xd4, 0x35, 0x93, 0xc7, 0x15, 0xfd, 0xd3, 0x1c, 0x61, 0x14, 0x1a, 0xbd, 0x04, 0xa9, 0x9f, 0xd6,
24	0x82, 0x2c, 0x85, 0x58, 0x85, 0x4c, 0xcd, 0xe3, 0x9a, 0x56, 0x84, 0xe7, 0xa5, 0x6d, 0xa2, 0x7d,
25];
26
27/// Well-known dev account: Bob.
28pub const BOB: [u8; 32] = [
29	0x8e, 0xaf, 0x04, 0x15, 0x16, 0x87, 0x73, 0x63, 0x26, 0xc9, 0xfe, 0xa1, 0x7e, 0x25, 0xfc, 0x52,
30	0x87, 0x61, 0x36, 0x93, 0xc9, 0x12, 0x90, 0x9c, 0xb2, 0x26, 0xaa, 0x47, 0x94, 0xf2, 0x6a, 0x48,
31];
32
33/// Well-known dev account: Charlie.
34pub const CHARLIE: [u8; 32] = [
35	0x90, 0xb5, 0xab, 0x20, 0x5c, 0x69, 0x74, 0xc9, 0xea, 0x84, 0x1b, 0xe6, 0x88, 0x86, 0x46, 0x33,
36	0xdc, 0x9c, 0xa8, 0xa3, 0x57, 0x84, 0x3e, 0xea, 0xcf, 0x23, 0x14, 0x64, 0x99, 0x65, 0xfe, 0x22,
37];
38
39/// Well-known dev account: Dave.
40pub const DAVE: [u8; 32] = [
41	0x30, 0x67, 0x21, 0x21, 0x1d, 0x54, 0x04, 0xbd, 0x9d, 0xa8, 0x8e, 0x02, 0x04, 0x36, 0x0a, 0x1a,
42	0x9a, 0xb8, 0xb8, 0x7c, 0x66, 0xc1, 0xbc, 0x2f, 0xcd, 0xd3, 0x7f, 0x3c, 0x22, 0x22, 0xcc, 0x20,
43];
44
45/// Well-known dev account: Eve.
46pub const EVE: [u8; 32] = [
47	0xe6, 0x59, 0xa7, 0xa1, 0x62, 0x8c, 0xdd, 0x93, 0xfe, 0xbc, 0x04, 0xa4, 0xe0, 0x64, 0x6e, 0xa2,
48	0x0e, 0x9f, 0x5f, 0x0c, 0xe0, 0x97, 0xd9, 0xa0, 0x52, 0x90, 0xd4, 0xa9, 0xe0, 0x54, 0xdf, 0x4e,
49];
50
51/// Well-known dev account: Ferdie.
52pub const FERDIE: [u8; 32] = [
53	0x1c, 0xbd, 0x2d, 0x43, 0x53, 0x0a, 0x44, 0x70, 0x5a, 0xd0, 0x88, 0xaf, 0x31, 0x3e, 0x18, 0xf8,
54	0x0b, 0x53, 0xef, 0x16, 0xb3, 0x61, 0x77, 0xcd, 0x4b, 0x77, 0xb8, 0x46, 0xf2, 0xa5, 0xf0, 0x7c,
55];
56
57/// All Substrate dev accounts (name, 32-byte public key).
58pub const SUBSTRATE_DEV_ACCOUNTS: [(&str, [u8; 32]); 6] = [
59	("Alice", ALICE),
60	("Bob", BOB),
61	("Charlie", CHARLIE),
62	("Dave", DAVE),
63	("Eve", EVE),
64	("Ferdie", FERDIE),
65];
66
67// ---------------------------------------------------------------------------
68// Ethereum dev accounts (20-byte H160 addresses, from Frontier/Moonbeam)
69// ---------------------------------------------------------------------------
70
71/// Well-known Ethereum dev account: Alith.
72pub const ALITH: [u8; 20] = [
73	0xf2, 0x4f, 0xf3, 0xa9, 0xcf, 0x04, 0xc7, 0x1d, 0xbc, 0x94, 0xd0, 0xb5, 0x66, 0xf7, 0xa2, 0x7b,
74	0x94, 0x56, 0x6c, 0xac,
75];
76
77/// Well-known Ethereum dev account: Baltathar.
78pub const BALTATHAR: [u8; 20] = [
79	0x3c, 0xd0, 0xa7, 0x05, 0xa2, 0xdc, 0x65, 0xe5, 0xb1, 0xe1, 0x20, 0x58, 0x96, 0xba, 0xa2, 0xbe,
80	0x8a, 0x07, 0xc6, 0xe0,
81];
82
83/// Well-known Ethereum dev account: Charleth.
84pub const CHARLETH: [u8; 20] = [
85	0x79, 0x8d, 0x4b, 0xa9, 0xba, 0xf0, 0x06, 0x4e, 0xc1, 0x9e, 0xb4, 0xf0, 0xa1, 0xa4, 0x57, 0x85,
86	0xae, 0x9d, 0x6d, 0xfc,
87];
88
89/// Well-known Ethereum dev account: Dorothy.
90pub const DOROTHY: [u8; 20] = [
91	0x77, 0x35, 0x39, 0xd4, 0xac, 0x0e, 0x78, 0x62, 0x33, 0xd9, 0x0a, 0x23, 0x36, 0x54, 0xcc, 0xee,
92	0x26, 0xa6, 0x13, 0xd9,
93];
94
95/// Well-known Ethereum dev account: Ethan.
96pub const ETHAN: [u8; 20] = [
97	0xff, 0x64, 0xd3, 0xf6, 0xef, 0xe2, 0x31, 0x7e, 0xe2, 0x80, 0x7d, 0x22, 0x3a, 0x0b, 0xdc, 0x4c,
98	0x0c, 0x49, 0xdf, 0xdb,
99];
100
101/// Well-known Ethereum dev account: Faith.
102pub const FAITH: [u8; 20] = [
103	0xc0, 0xf0, 0xf4, 0xab, 0x32, 0x4c, 0x46, 0xe5, 0x5d, 0x02, 0xd0, 0x03, 0x33, 0x43, 0xb4, 0xbe,
104	0x8a, 0x55, 0x53, 0x2d,
105];
106
107/// All Ethereum dev accounts (name, 20-byte H160 address).
108pub const ETHEREUM_DEV_ACCOUNTS: [(&str, [u8; 20]); 6] = [
109	("Alith", ALITH),
110	("Baltathar", BALTATHAR),
111	("Charleth", CHARLETH),
112	("Dorothy", DOROTHY),
113	("Ethan", ETHAN),
114	("Faith", FAITH),
115];
116
117// ---------------------------------------------------------------------------
118// Helpers
119// ---------------------------------------------------------------------------
120
121/// Compute the `System::Account` storage key for an account (Blake2_128Concat).
122///
123/// Works with both 32-byte (Substrate) and 20-byte (Ethereum) account IDs.
124pub fn account_storage_key(account: &[u8]) -> Vec<u8> {
125	let mut key = Vec::new();
126	key.extend(sp_core::twox_128(storage::SYSTEM_PALLET));
127	key.extend(sp_core::twox_128(storage::ACCOUNT_STORAGE));
128	key.extend(sp_core::blake2_128(account));
129	key.extend(account);
130	key
131}
132
133/// Compute the `Sudo::Key` storage key.
134pub fn sudo_key_storage_key() -> Vec<u8> {
135	let mut key = Vec::new();
136	key.extend(sp_core::twox_128(storage::SUDO_PALLET));
137	key.extend(sp_core::twox_128(storage::SUDO_KEY_STORAGE));
138	key
139}
140
141/// Build a fresh `AccountInfo` with the given free balance.
142///
143/// Layout (80 bytes):
144/// - nonce: u32 (4 bytes) = 0
145/// - consumers: u32 (4 bytes) = 0
146/// - providers: u32 (4 bytes) = 1
147/// - sufficients: u32 (4 bytes) = 0
148/// - data.free: u128 (16 bytes)
149/// - data.reserved: u128 (16 bytes) = 0
150/// - data.frozen: u128 (16 bytes) = 0
151/// - data.flags: u128 (16 bytes) = 0
152pub fn build_account_info(free_balance: u128) -> Vec<u8> {
153	let mut data = vec![0u8; 80];
154	// providers = 1 (offset 8..12)
155	data[8..12].copy_from_slice(&1u32.to_le_bytes());
156	// data.free (offset 16..32)
157	data[16..32].copy_from_slice(&free_balance.to_le_bytes());
158	data
159}
160
161/// Patch the free balance in an existing `AccountInfo` blob.
162///
163/// Overwrites bytes 16..32 (the `data.free` field) with the new balance.
164pub fn patch_free_balance(existing: &[u8], new_balance: u128) -> Vec<u8> {
165	let mut patched = existing.to_vec();
166	patched[16..32].copy_from_slice(&new_balance.to_le_bytes());
167	patched
168}
169
170#[cfg(test)]
171mod tests {
172	use super::*;
173
174	#[test]
175	fn substrate_account_storage_key_has_correct_length() {
176		let key = account_storage_key(&ALICE);
177		// twox128("System") + twox128("Account") + blake2_128(account) + account
178		// = 16 + 16 + 16 + 32 = 80
179		assert_eq!(key.len(), 80);
180	}
181
182	#[test]
183	fn ethereum_account_storage_key_has_correct_length() {
184		let key = account_storage_key(&ALITH);
185		// twox128("System") + twox128("Account") + blake2_128(account) + account
186		// = 16 + 16 + 16 + 20 = 68
187		assert_eq!(key.len(), 68);
188	}
189
190	#[test]
191	fn sudo_key_storage_key_has_correct_length() {
192		let key = sudo_key_storage_key();
193		// twox128("Sudo") + twox128("Key") = 16 + 16 = 32
194		assert_eq!(key.len(), 32);
195	}
196
197	#[test]
198	fn build_account_info_sets_providers_and_balance() {
199		let balance: u128 = 1_000_000_000_000;
200		let info = build_account_info(balance);
201		assert_eq!(info.len(), 80);
202		// providers = 1
203		assert_eq!(u32::from_le_bytes(info[8..12].try_into().unwrap()), 1);
204		// free balance
205		assert_eq!(u128::from_le_bytes(info[16..32].try_into().unwrap()), balance);
206	}
207
208	#[test]
209	fn patch_free_balance_preserves_other_fields() {
210		let original = build_account_info(100);
211		let patched = patch_free_balance(&original, 999);
212		// Balance changed
213		assert_eq!(u128::from_le_bytes(patched[16..32].try_into().unwrap()), 999);
214		// Providers preserved
215		assert_eq!(u32::from_le_bytes(patched[8..12].try_into().unwrap()), 1);
216		// Length preserved
217		assert_eq!(patched.len(), 80);
218	}
219}