forked from cryptape/ckb-transaction-cobuild-poc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Big refactor according to updated spec
Reference C implementation: cryptape/omnilock#3 Changes: - add lazy reader support - API changed - add testing - add documents. make it ready for publishing - fix according to updated spec - add logs
- Loading branch information
1 parent
e29514f
commit d904f9f
Showing
34 changed files
with
5,086 additions
and
389 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,68 @@ | ||
pub use blake2b_ref::{Blake2b, Blake2bBuilder}; | ||
pub use molecule::lazy_reader::Cursor; | ||
|
||
pub const PERSONALIZATION_SIGHASH_ALL: &[u8] = b"ckb-tcob-sighash"; | ||
pub const PERSONALIZATION_SIGHASH_ALL_ONLY: &[u8] = b"ckb-tcob-sgohash"; | ||
pub const PERSONALIZATION_OTX: &[u8] = b"ckb-tcob-otxhash"; | ||
|
||
const BATCH_SIZE: usize = 2048; | ||
|
||
/// return a blake2b instance with personalization for SighashAll | ||
pub fn new_sighash_all_blake2b() -> Blake2b { | ||
Blake2bBuilder::new(32) | ||
.personal(PERSONALIZATION_SIGHASH_ALL) | ||
.build() | ||
pub fn new_sighash_all_blake2b() -> Blake2bStatistics { | ||
Blake2bStatistics::new( | ||
Blake2bBuilder::new(32) | ||
.personal(PERSONALIZATION_SIGHASH_ALL) | ||
.build(), | ||
) | ||
} | ||
|
||
/// return a blake2b instance with personalization for SighashAllOnly | ||
pub fn new_sighash_all_only_blake2b() -> Blake2b { | ||
Blake2bBuilder::new(32) | ||
.personal(PERSONALIZATION_SIGHASH_ALL_ONLY) | ||
.build() | ||
pub fn new_sighash_all_only_blake2b() -> Blake2bStatistics { | ||
Blake2bStatistics::new( | ||
Blake2bBuilder::new(32) | ||
.personal(PERSONALIZATION_SIGHASH_ALL_ONLY) | ||
.build(), | ||
) | ||
} | ||
|
||
/// return a blake2b instance with personalization for OTX | ||
pub fn new_otx_blake2b() -> Blake2b { | ||
Blake2bBuilder::new(32) | ||
.personal(PERSONALIZATION_OTX) | ||
.build() | ||
pub fn new_otx_blake2b() -> Blake2bStatistics { | ||
Blake2bStatistics::new( | ||
Blake2bBuilder::new(32) | ||
.personal(PERSONALIZATION_OTX) | ||
.build(), | ||
) | ||
} | ||
|
||
pub struct Blake2bStatistics { | ||
count: usize, | ||
blake2b: Blake2b, | ||
} | ||
|
||
impl Blake2bStatistics { | ||
pub fn new(blake2b: Blake2b) -> Self { | ||
Self { count: 0, blake2b } | ||
} | ||
|
||
pub fn update(&mut self, data: &[u8]) { | ||
self.blake2b.update(data); | ||
self.count += data.len(); | ||
} | ||
pub fn update_cursor(&mut self, mut cursor: Cursor) { | ||
let mut buf = [0u8; BATCH_SIZE]; | ||
while cursor.size > 0 { | ||
let read_len = cursor.read_at(&mut buf).unwrap(); | ||
if read_len > 0 { | ||
self.update(&buf[0..read_len]); | ||
cursor = cursor.slice_by_start(read_len).unwrap(); | ||
} | ||
} | ||
} | ||
|
||
pub fn finalize(self, dst: &mut [u8]) { | ||
self.blake2b.finalize(dst) | ||
} | ||
pub fn count(&self) -> usize { | ||
self.count | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
use ckb_std::error::SysError; | ||
use molecule::error::VerificationError; | ||
pub use molecule::lazy_reader::Error as LazyReaderError; | ||
|
||
#[derive(Debug)] | ||
pub enum Error { | ||
Sys(SysError), | ||
LazyReader(LazyReaderError), | ||
MoleculeEncoding, | ||
WrongSighashAll, | ||
WrongWitnessLayout, | ||
WrongOtxStart, | ||
WrongOtx, | ||
NoSealFound, | ||
AuthError, | ||
ScriptHashAbsent, | ||
WrongCount, | ||
} | ||
|
||
impl From<SysError> for Error { | ||
fn from(e: SysError) -> Self { | ||
Error::Sys(e) | ||
} | ||
} | ||
|
||
impl From<VerificationError> for Error { | ||
fn from(_: VerificationError) -> Self { | ||
Error::MoleculeEncoding | ||
} | ||
} | ||
|
||
impl From<LazyReaderError> for Error { | ||
fn from(e: LazyReaderError) -> Self { | ||
Error::LazyReader(e) | ||
} | ||
} |
Oops, something went wrong.