Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Big Refactor According to Updated Spec #44

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 59 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[workspace]
members = ["ckb-transaction-cobuild", "contracts/transaction-cobuild-lock-demo", "contracts/transaction-cobuild-type-demo", "contracts/transaction-cobuild-otx-lock-demo"]
members = ["ckb-transaction-cobuild", "contracts/transaction-cobuild-lock-demo", "contracts/transaction-cobuild-type-demo", "contracts/transaction-cobuild-otx-lock-demo", "contracts/always-success"]
exclude = ["tests"]
resolver = "2"

[profile.release]
overflow-checks = true
Expand All @@ -16,3 +17,6 @@ opt-level = 1
debug = false
panic = 'abort'
debug-assertions = true

[patch.crates-io]
molecule = { git = "https://github.com/XuJiandong/molecule.git", rev = "785a309" }
11 changes: 11 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ mol:
moleculec --language rust --schema-file schemas/top_level.mol > ckb-transaction-cobuild/src/schemas/top_level.rs
cargo fmt

mol2:
moleculec --language rust-lazy-reader --schema-file schemas/basic.mol > ckb-transaction-cobuild/src/schemas2/basic.rs
moleculec --language rust-lazy-reader --schema-file schemas/top_level.mol > ckb-transaction-cobuild/src/schemas2/top_level.rs
moleculec --language rust-lazy-reader --schema-file schemas/blockchain.mol > ckb-transaction-cobuild/src/schemas2/blockchain.rs
cargo fmt


install:
rustup target add riscv64imac-unknown-none-elf
cargo install cross --git https://github.com/cross-rs/cross
Expand All @@ -19,3 +26,7 @@ install:
ci:
capsule build --release
cd tests && cargo test && cd ..

debug:
capsule build --release
cd tests && RUST_LOG=debug cargo test -- --nocapture && cd ..
20 changes: 4 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
# CKB Transaction Co-Build Protocol(TCoB)
This project is a proof of concept that aims to demonstrate how to adopt
transaction co-build protocol and message (similar to EIP-712) in CKB. It also
includes witnesses layout change to simplify signing and DApp interoperability.

Rust Implementation of [CKB Transaction Co-Build
Protocol(TCoB)](https://talk.nervos.org/t/ckb-transaction-cobuild-protocol-overview/7702)

## Build
Build contracts:
Build script:

```sh
capsule build --release
```

## Migration to TCoB
See [migration](./docs/migration.md)


## Integration with Dapp and Wallet
See [dapp](./dapp/README.md), using Lumos and Spore SDK.


## Project Structure
* ckb-transaction-cobuild
Expand All @@ -26,11 +18,7 @@ See [dapp](./dapp/README.md), using Lumos and Spore SDK.

* contracts/transaction-cobuild-lock-demo

A demo lock demonstrating how to write a lock script.

* dapp

DApp and wallet demo projects. With these projects, we can test/deploy on the testnet/devnet.
A demo lock demonstrating how to write a lock script and type script.

* schemas

Expand Down
4 changes: 4 additions & 0 deletions capsule.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@ template_type = "Rust"
[[contracts]]
name = "transaction-cobuild-otx-lock-demo"
template_type = "Rust"

[[contracts]]
name = "always-success"
template_type = "Rust"
6 changes: 5 additions & 1 deletion ckb-transaction-cobuild/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[features]
default = []
log = []

[dependencies]
blake2b-ref = "0.3.1"
ckb-std = { version = "0.14.3", default-features = false, features = ["ckb-types"] }
molecule = { version = "0.7.5", default-features = false }
molecule = { version = "0.7.5", default-features = false, features = ["bytes_vec"] }
ckb-gen-types = { version = "0.111.0", default-features = false }
66 changes: 54 additions & 12 deletions ckb-transaction-cobuild/src/blake2b.rs
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
}
}
36 changes: 36 additions & 0 deletions ckb-transaction-cobuild/src/error.rs
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)
}
}
Loading