Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
129 changes: 121 additions & 8 deletions key-wallet/src/bip32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2448,13 +2448,71 @@ mod tests {
[0x01; 32],
[0x02; 32],
ApplicationKeyPurpose::Encryption,
0,
)
.unwrap();
assert_eq!(
path.to_string(),
format!("m/9'/5'/5'/7'/0'/0x{}'/0x{}'/1'/0'", "01".repeat(32), "02".repeat(32))
);

let path = DerivationPath::application_encryption_path(
Network::Testnet,
[0x01; 32],
[0x02; 32],
ApplicationKeyPurpose::Decryption,
7,
)
.unwrap();
assert_eq!(
path.to_string(),
format!("m/9'/5'/5'/7'/0'/0x{}'/0x{}'/1'", "01".repeat(32), "02".repeat(32))
format!("m/9'/1'/5'/7'/0'/0x{}'/0x{}'/2'/7'", "01".repeat(32), "02".repeat(32))
);

// The largest rotation fits in 31 bits; anything with the hardened bit set is rejected.
let max = (1u32 << 31) - 1;
let path = DerivationPath::application_encryption_path(
Network::Mainnet,
[0x01; 32],
[0x02; 32],
ApplicationKeyPurpose::Encryption,
max,
)
.unwrap();
assert_eq!(
path[path.len() - 1],
ChildNumber::Hardened {
index: max
}
);
for rotation in [1u32 << 31, u32::MAX] {
assert_eq!(
DerivationPath::application_encryption_path(
Network::Mainnet,
[0x01; 32],
[0x02; 32],
ApplicationKeyPurpose::Encryption,
rotation,
),
Err(Error::InvalidChildNumber(rotation))
);
}
}

#[test]
fn test_application_encryption_path_without_rotation() {
let path = DerivationPath::application_encryption_path_without_rotation(
Network::Mainnet,
[0x01; 32],
[0x02; 32],
ApplicationKeyPurpose::Encryption,
);
assert_eq!(
path.to_string(),
format!("m/9'/5'/5'/7'/0'/0x{}'/0x{}'/1'", "01".repeat(32), "02".repeat(32))
);

let path = DerivationPath::application_encryption_path_without_rotation(
Network::Testnet,
[0x01; 32],
[0x02; 32],
Expand Down Expand Up @@ -2496,13 +2554,26 @@ mod tests {
Network::Testnet,
)
};
let encryption = |identity_id, contract_id, key_purpose| {
let encryption = |identity_id, contract_id, key_purpose, rotation| {
public_key_hex(
DerivationPath::application_encryption_path(
Network::Mainnet,
identity_id,
contract_id,
key_purpose,
rotation,
)
.unwrap(),
Network::Mainnet,
)
};
let legacy_encryption = |identity_id, contract_id, key_purpose| {
public_key_hex(
DerivationPath::application_encryption_path_without_rotation(
Network::Mainnet,
identity_id,
contract_id,
key_purpose,
),
Network::Mainnet,
)
Expand All @@ -2512,20 +2583,44 @@ mod tests {
session([0x35; 32], [0x6B; 32]),
"022c8b2e806244482374b1caf8306146dc03aad3b99a5954efd5e70a0eddd37a5d"
);
assert_eq!(
encryption([0x35; 32], [0x6B; 32], ApplicationKeyPurpose::Encryption),
"03e989de1b62f137231cc06659c810c17100becd1f5e23d5710faa7602769fe434"
);
assert_eq!(
session(ascending, descending),
"0278ad78c1a220924bf0e06e1111c61422efeace068f30efa825604b436970b230"
);

// Keys under the DIP-13 layout, which ends with the hardened rotation level.
assert_eq!(
encryption([0x35; 32], [0x6B; 32], ApplicationKeyPurpose::Encryption, 0),
"031a882c119e2b1f8e0349b444d877afa2599efeef49a26930fd25518cb6e9d57a"
);
assert_eq!(
encryption([0x35; 32], [0x6B; 32], ApplicationKeyPurpose::Encryption, 1),
"03d0f6f0f48d968b9554d8e7fe467d7135bacf29d5c4994ca593bd10ebeed696f6"
);
assert_eq!(
encryption(ascending, descending, ApplicationKeyPurpose::Encryption, 0),
"02dfed57ddaaea8c0758e8da44513520038eb8d6b0b92109953c14d6ab1087259b"
);
assert_eq!(
encryption(ascending, descending, ApplicationKeyPurpose::Decryption, 0),
"020bcde800f41ad1f924477a9502f111445224cc9ab70114ae4e5099ba02bdd034"
);
assert_eq!(
encryption(ascending, descending, ApplicationKeyPurpose::Decryption, 1),
"021b97869c21933673076a938337c28848c8bf8094bbe6883e3a4acbf33c639072"
);

// Pre-DIP keys that wallets derive only to recognise earlier registrations.
assert_eq!(
legacy_encryption([0x35; 32], [0x6B; 32], ApplicationKeyPurpose::Encryption),
"03e989de1b62f137231cc06659c810c17100becd1f5e23d5710faa7602769fe434"
);
assert_eq!(
encryption(ascending, descending, ApplicationKeyPurpose::Encryption),
legacy_encryption(ascending, descending, ApplicationKeyPurpose::Encryption),
"03c4e462229f176595ec17e28e9096d3cfbc42496fb8ed4d48f8f58355d6105aa9"
);
assert_eq!(
encryption(ascending, descending, ApplicationKeyPurpose::Decryption),
legacy_encryption(ascending, descending, ApplicationKeyPurpose::Decryption),
"031fd0a3cfc01bebb89d43c2e12a6321c37ebdacb62c09495899e5b65a24fa5c99"
);

Expand All @@ -2545,6 +2640,24 @@ mod tests {
descending,
)
);
let parsed: DerivationPath = format!(
"m/9'/5'/5'/7'/0'/0x{}'/0x{}'/2'/1'",
ascending.to_lower_hex_string(),
descending.to_lower_hex_string()
)
.parse()
.unwrap();
assert_eq!(
parsed,
DerivationPath::application_encryption_path(
Network::Mainnet,
ascending,
descending,
ApplicationKeyPurpose::Decryption,
1,
)
.unwrap()
);
}

#[test]
Expand Down
34 changes: 31 additions & 3 deletions key-wallet/src/dip9.rs
Original file line number Diff line number Diff line change
Expand Up @@ -801,14 +801,42 @@ impl DerivationPath {
}

/// DIP-13 application encryption key path,
/// `m/9'/coin_type'/5'/7'/0'/identity_id'/contract_id'/key_purpose'`. The identity id and
/// contract id are DIP-14 256-bit hardened children, which only secp256k1 derivation defines,
/// so the key type level is always ECDSA (`0'`).
/// `m/9'/coin_type'/5'/7'/0'/identity_id'/contract_id'/key_purpose'/rotation'`. The identity
/// id and contract id are DIP-14 256-bit hardened children, which only secp256k1 derivation
/// defines, so the key type level is always ECDSA (`0'`). `rotation` is the hardened 31-bit
/// rotation level, starting at `0`, that lets an identity replace its encryption key for a
/// contract.
///
/// Returns [`Error::InvalidChildNumber`] if `rotation` is not below `2^31`.
pub fn application_encryption_path(
network: Network,
identity_id: [u8; 32],
contract_id: [u8; 32],
key_purpose: ApplicationKeyPurpose,
rotation: u32,
) -> Result<Self, Error> {
let rotation = ChildNumber::from_hardened_idx(rotation)?;
Ok(Self::application_encryption_path_without_rotation(
network,
identity_id,
contract_id,
key_purpose,
)
.extend([rotation]))
}

/// The pre-DIP application encryption key path without a rotation level,
/// `m/9'/coin_type'/5'/7'/0'/identity_id'/contract_id'/key_purpose'`.
///
/// This is the layout wallets used before DIP-13 added the rotation level. Wallets derive it
/// only to recognise encryption keys registered under it on testnet or devnet; new keys must
/// use [`Self::application_encryption_path`]. The rotation paths are hardened children of
/// this one, so their keys differ from the key at this path.
pub fn application_encryption_path_without_rotation(
network: Network,
identity_id: [u8; 32],
contract_id: [u8; 32],
key_purpose: ApplicationKeyPurpose,
) -> Self {
let root_derivation_path: DerivationPath = match network {
Network::Mainnet => APPLICATION_ENCRYPTION_PATH_MAINNET,
Expand Down
Loading