Skip to content

Commit fbd3b7b

Browse files
committed
Enforce correct commitment prefix on confidential values
1 parent 84c7ee0 commit fbd3b7b

File tree

3 files changed

+14
-7
lines changed

3 files changed

+14
-7
lines changed

src/confidential.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,11 @@ macro_rules! impl_confidential_commitment {
8282
let explicit = $explicit_fn(Decodable::consensus_decode(&mut d)?);
8383
Ok($name::Explicit(explicit))
8484
}
85-
x => {
85+
p if p == $prefixA || p == $prefixB => {
8686
let commitment = <[u8; 32]>::consensus_decode(&mut d)?;
87-
Ok($name::Confidential(x, commitment))
87+
Ok($name::Confidential(p, commitment))
8888
}
89+
p => return Err(encode::Error::InvalidConfidentialPrefix(p)),
8990
}
9091
}
9192
}
@@ -143,12 +144,15 @@ macro_rules! impl_confidential_commitment {
143144
None => Err(A::Error::custom("missing commitment")),
144145
}
145146
}
146-
x => {
147+
p if p == $prefixA || p == $prefixB => {
147148
match access.next_element()? {
148-
Some(y) => Ok($name::Confidential(x, y)),
149+
Some(y) => Ok($name::Confidential(p, y)),
149150
None => Err(A::Error::custom("missing commitment")),
150151
}
151152
}
153+
p => return Err(A::Error::custom(format!(
154+
"invalid commitment, invalid prefix: 0x{:02x}", p
155+
))),
152156
}
153157
}
154158
}

src/encode.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ pub enum Error {
3939
},
4040
/// Parsing error
4141
ParseFailed(&'static str),
42+
/// Invalid prefix for the confidential type.
43+
InvalidConfidentialPrefix(u8),
4244
}
4345

4446
impl fmt::Display for Error {
@@ -50,6 +52,7 @@ impl fmt::Display for Error {
5052
max: ref m,
5153
} => write!(f, "oversized vector allocation: requested {}, maximum {}", r, m),
5254
Error::ParseFailed(ref e) => write!(f, "parse failed: {}", e),
55+
Error::InvalidConfidentialPrefix(p) => write!(f, "invalid confidential prefix: 0x{:02x}", p),
5356
}
5457
}
5558
}

src/transaction.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1562,7 +1562,7 @@ mod tests {
15621562
fn txout_null_data() {
15631563
// Output with high opcodes should not be considered nulldata
15641564
let output: TxOut = hex_deserialize!("\
1565-
c3319c0000000000d3d3d3d3d3d3d3d3d3d3d3d3fdfdfd0101010101010101010\
1565+
0a319c0000000000d3d3d3d3d3d3d3d3d3d3d3d3fdfdfd0101010101010101010\
15661566
101010101010101010101010101012e010101010101010101fdfdfdfdfdfdfdfd\
15671567
fdfdfdfdfdfdfdfdfdfdfdfd006a209f6a6a6a6a6a6a806a6afdfdfdfd17fdfdf\
15681568
dfdfdfdfdfdfdfdddfdfdfdfdfdfdfdfdfddedededededededededededededede\
@@ -1580,7 +1580,7 @@ mod tests {
15801580

15811581
// Output with pushes that are e.g. OP_1 are nulldata but not pegouts
15821582
let output: TxOut = hex_deserialize!("\
1583-
c32d3634393536d9a2d0aaba3823f442fb24363831fdfd0101010101010101010\
1583+
0a2d3634393536d9a2d0aaba3823f442fb24363831fdfd0101010101010101010\
15841584
1010101010101010101010101010101010101016a01010101fdfdfdfdfdfdfdfd\
15851585
fdfdfdfdfd3ca059fdfdfb6a2000002323232323232323232323232323232\
15861586
3232323232323232321232323010151232323232323232323232323232323\
@@ -1598,7 +1598,7 @@ mod tests {
15981598

15991599
// Output with just one push and nothing else should be nulldata but not pegout
16001600
let output: TxOut = hex_deserialize!("\
1601-
c32d3634393536d9a2d0aaba3823f442fb24363831fdfd0101010101010101010\
1601+
0a2d3634393536d9a2d0aaba3823f442fb24363831fdfd0101010101010101010\
16021602
1010101010101010101010101010101010101016a01010101fdfdfdfdfdfdfdfd\
16031603
fdfdfdfdfd3ca059fdf2226a20000000000000000000000000000000000000000\
16041604
0000000000000000000000000\

0 commit comments

Comments
 (0)