Skip to content

Commit 9766010

Browse files
committed
Merge remote-tracking branch 'origin/main' into whankinsiv/accounts-endpoints-using-address-state
2 parents f7c3d34 + c8b9cd9 commit 9766010

File tree

1 file changed

+79
-22
lines changed

1 file changed

+79
-22
lines changed

common/src/address.rs

Lines changed: 79 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -308,8 +308,10 @@ impl ShelleyAddress {
308308

309309
let mut data = Vec::new();
310310

311-
let build_header =
312-
|variant: u8| -> u8 { network_bits | (payment_bits << 4) | (variant << 5) };
311+
let build_header = |delegation_bits: u8| -> u8 {
312+
let addr_type = ((delegation_bits & 0x03) << 1) | (payment_bits & 0x01);
313+
(addr_type << 4) | (network_bits & 0x0F)
314+
};
313315

314316
match &self.delegation {
315317
ShelleyAddressDelegationPart::None => {
@@ -351,14 +353,21 @@ impl ShelleyAddress {
351353
}
352354

353355
let header = data[0];
356+
357+
if header & 0x80 != 0 {
358+
return Err(anyhow!("invalid header: high bit set"));
359+
}
360+
354361
let network = match header & 0x0F {
355362
0 => NetworkId::Testnet,
356363
1 => NetworkId::Mainnet,
357364
_ => return Err(anyhow!("invalid network bits in header")),
358365
};
359366

360-
let payment_bits = (header >> 4) & 0x01;
361-
let delegation_bits = (header >> 5) & 0x03;
367+
let addr_type = (header >> 4) & 0x0F;
368+
369+
let payment_bits = addr_type & 0x01;
370+
let delegation_bits = (addr_type >> 1) & 0x03;
362371

363372
let payment_hash = {
364373
let mut arr = [0u8; 28];
@@ -1117,49 +1126,97 @@ mod tests {
11171126
let stake_hash = Hash::new([0x22; 28]);
11181127
let script_hash = Hash::new([0x33; 28]);
11191128

1120-
// Normal address
1121-
let addr_base = ShelleyAddress {
1129+
// (KeyHash, StakeKeyHash)
1130+
let type_1 = ShelleyAddress {
11221131
network: NetworkId::Mainnet,
11231132
payment: ShelleyAddressPaymentPart::PaymentKeyHash(payment_hash),
11241133
delegation: ShelleyAddressDelegationPart::StakeKeyHash(stake_hash),
11251134
};
1126-
let bytes = addr_base.to_bytes_key();
1135+
let bytes = type_1.to_bytes_key();
1136+
assert_eq!(bytes[0], 0x01);
11271137
let decoded = ShelleyAddress::from_bytes_key(&bytes).expect("decode base");
1128-
assert_eq!(addr_base, decoded);
1138+
assert_eq!(type_1, decoded);
11291139

1130-
// Script address
1131-
let addr_script = ShelleyAddress {
1132-
network: NetworkId::Testnet,
1140+
// (ScriptKeyHash, StakeKeyHash)
1141+
let type_2 = ShelleyAddress {
1142+
network: NetworkId::Mainnet,
1143+
payment: ShelleyAddressPaymentPart::ScriptHash(payment_hash),
1144+
delegation: ShelleyAddressDelegationPart::StakeKeyHash(stake_hash),
1145+
};
1146+
let bytes = type_2.to_bytes_key();
1147+
assert_eq!(bytes[0], 0x11);
1148+
let decoded = ShelleyAddress::from_bytes_key(&bytes).expect("decode base");
1149+
assert_eq!(type_2, decoded);
1150+
1151+
// (KeyHash, ScriptHash)
1152+
let type_3 = ShelleyAddress {
1153+
network: NetworkId::Mainnet,
1154+
payment: ShelleyAddressPaymentPart::PaymentKeyHash(payment_hash),
1155+
delegation: ShelleyAddressDelegationPart::ScriptHash(stake_hash),
1156+
};
1157+
let bytes = type_3.to_bytes_key();
1158+
assert_eq!(bytes[0], 0x21);
1159+
let decoded = ShelleyAddress::from_bytes_key(&bytes).expect("decode base");
1160+
assert_eq!(type_3, decoded);
1161+
1162+
// (ScriptHash, ScriptHash)
1163+
let type_4 = ShelleyAddress {
1164+
network: NetworkId::Mainnet,
11331165
payment: ShelleyAddressPaymentPart::ScriptHash(payment_hash),
11341166
delegation: ShelleyAddressDelegationPart::ScriptHash(script_hash),
11351167
};
1136-
let bytes = addr_script.to_bytes_key();
1168+
let bytes = type_4.to_bytes_key();
1169+
assert_eq!(bytes[0], 0x31);
11371170
let decoded = ShelleyAddress::from_bytes_key(&bytes).expect("decode script");
1138-
assert_eq!(addr_script, decoded);
1171+
assert_eq!(type_4, decoded);
11391172

1140-
// Pointer address
1173+
// (KeyHash, Pointer)
11411174
let pointer = ShelleyAddressPointer {
11421175
slot: 1234,
11431176
tx_index: 56,
11441177
cert_index: 2,
11451178
};
1146-
let addr_pointer = ShelleyAddress {
1179+
let type_5 = ShelleyAddress {
11471180
network: NetworkId::Mainnet,
11481181
payment: ShelleyAddressPaymentPart::PaymentKeyHash(payment_hash),
1182+
delegation: ShelleyAddressDelegationPart::Pointer(pointer.clone()),
1183+
};
1184+
let bytes = type_5.to_bytes_key();
1185+
assert_eq!(bytes[0], 0x41);
1186+
let decoded = ShelleyAddress::from_bytes_key(&bytes).expect("decode pointer");
1187+
assert_eq!(type_5, decoded);
1188+
1189+
// (ScriptHash, Pointer)
1190+
let type_6 = ShelleyAddress {
1191+
network: NetworkId::Mainnet,
1192+
payment: ShelleyAddressPaymentPart::ScriptHash(payment_hash),
11491193
delegation: ShelleyAddressDelegationPart::Pointer(pointer),
11501194
};
1151-
let bytes = addr_pointer.to_bytes_key();
1195+
let bytes = type_6.to_bytes_key();
1196+
assert_eq!(bytes[0], 0x51);
11521197
let decoded = ShelleyAddress::from_bytes_key(&bytes).expect("decode pointer");
1153-
assert_eq!(addr_pointer, decoded);
1198+
assert_eq!(type_6, decoded);
11541199

1155-
// Enterprise address
1156-
let addr_none = ShelleyAddress {
1157-
network: NetworkId::Testnet,
1200+
// (KeyHash, None)
1201+
let type_7 = ShelleyAddress {
1202+
network: NetworkId::Mainnet,
11581203
payment: ShelleyAddressPaymentPart::PaymentKeyHash(payment_hash),
11591204
delegation: ShelleyAddressDelegationPart::None,
11601205
};
1161-
let bytes = addr_none.to_bytes_key();
1206+
let bytes = type_7.to_bytes_key();
1207+
assert_eq!(bytes[0], 0x61);
1208+
let decoded = ShelleyAddress::from_bytes_key(&bytes).expect("decode none");
1209+
assert_eq!(type_7, decoded);
1210+
1211+
// (ScriptHash, None)
1212+
let type_8 = ShelleyAddress {
1213+
network: NetworkId::Mainnet,
1214+
payment: ShelleyAddressPaymentPart::ScriptHash(payment_hash),
1215+
delegation: ShelleyAddressDelegationPart::None,
1216+
};
1217+
let bytes = type_8.to_bytes_key();
1218+
assert_eq!(bytes[0], 0x71);
11621219
let decoded = ShelleyAddress::from_bytes_key(&bytes).expect("decode none");
1163-
assert_eq!(addr_none, decoded);
1220+
assert_eq!(type_8, decoded);
11641221
}
11651222
}

0 commit comments

Comments
 (0)