Skip to content

Commit c5fd6e6

Browse files
committed
transaction: use better error typing for pegin destructuring
We should redo the error types here as well at some point.
1 parent 76f4984 commit c5fd6e6

1 file changed

Lines changed: 10 additions & 7 deletions

File tree

src/transaction.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use std::collections::HashMap;
2020
use std::convert::TryFrom;
2121

2222
use bitcoin::{self, VarInt};
23+
use internals::slice::SliceExt;
2324
use crate::hashes::Hash;
2425

2526
use crate::{confidential, ContractHash};
@@ -432,12 +433,14 @@ impl<'tx> PeginData<'tx> {
432433
pegin_witness: &'tx [Vec<u8>],
433434
prevout: bitcoin::OutPoint,
434435
) -> Result<PeginData<'tx>, &'static str> {
435-
if pegin_witness.len() != 6 {
436-
return Err("size not 6");
437-
}
438-
if pegin_witness[5].len() < 80 {
439-
return Err("merkle proof too short");
440-
}
436+
let pegin_witness = match <&[Vec<u8>; 6]>::try_from(pegin_witness) {
437+
Ok(pw) => pw,
438+
Err(..) => return Err("size not 6"),
439+
};
440+
let block_header = match SliceExt::split_first_chunk::<80>(pegin_witness[5].as_slice()) {
441+
Some(bh) => bh.0,
442+
None => return Err("merkle proof too short"),
443+
};
441444

442445
Ok(PeginData {
443446
outpoint: prevout,
@@ -448,7 +451,7 @@ impl<'tx> PeginData<'tx> {
448451
claim_script: &pegin_witness[3],
449452
tx: &pegin_witness[4],
450453
merkle_proof: &pegin_witness[5],
451-
referenced_block: bitcoin::BlockHash::hash(&pegin_witness[5][0..80]),
454+
referenced_block: bitcoin::BlockHash::hash(block_header),
452455
})
453456
}
454457

0 commit comments

Comments
 (0)