Skip to content

Commit 13f3c20

Browse files
committed
Reject non-canonical Bitcoin Cash CashAddr addresses
A CashAddr with non-zero padding bits or one extra 5-bit group and a valid checksum decoded to the same hash as the canonical address. The decoder now re-encodes the payload and rejects any input that differs, as the reference implementation does.
1 parent 9f610b2 commit 13f3c20

1 file changed

Lines changed: 6 additions & 0 deletions

File tree

‎core/crates/gem_bitcoin/src/signer/address/bitcoin_cash.rs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ fn cashaddr_script(address: &str) -> Option<AddressScript> {
4040
return None;
4141
}
4242
let bytes: Vec<u8> = values[..data_len].iter().copied().fes_to_bytes().collect();
43+
if !bytes.iter().copied().bytes_to_fes().eq(values[..data_len].iter().copied()) {
44+
return None;
45+
}
4346
let (&version, hash) = bytes.split_first()?;
4447
let hash = hash20(hash).ok()?;
4548
match version {
@@ -93,6 +96,9 @@ mod tests {
9396
for address in [
9497
"bitcoincash:qpm2qsznhks23z7629mms6s4cwef74vcwvy22gdx6b",
9598
"bitcoincash:qpm2qsznhks23z7629mms6s4cwef74vcwvY22GDX6A",
99+
"bitcoincash:qpm2qsznhks23z7629mms6s4cwef74vcw0c0g40rx7",
100+
"bitcoincash:qpm2qsznhks23z7629mms6s4cwef74vcwvq5xhgekz9",
101+
"bitcoincash:qpm2qsznhks23z7629mms6s4cwef74vcwvlqw6w57k6",
96102
"bchtest:qpm2qsznhks23z7629mms6s4cwef74vcwvy22gdx6a",
97103
"BitcoinCash:qpm2qsznhks23z7629mms6s4cwef74vcwvy22gdx6a",
98104
"BITCOINCASH:QPM2QSZNHKS23Z7629MMS6S4CWEF74VCWVY22GDX6A",

0 commit comments

Comments
 (0)