Skip to content

Commit

Permalink
Init commit (#2)
Browse files Browse the repository at this point in the history
* Initial commit
  • Loading branch information
XuJiandong authored Nov 22, 2023
1 parent cfd398d commit 4da9459
Show file tree
Hide file tree
Showing 34 changed files with 6,721 additions and 12 deletions.
16 changes: 16 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: Rust

on: [push, pull_request]

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v1
- name: Run installation
run: make install
- name: Run CI
run: make ci

21 changes: 9 additions & 12 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
# Generated by Cargo
# will have compiled files and executables
debug/
target/
# Rust
target
.cache/.cargo/*

# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock
# C
contracts/c/build/*

# These are backup files generated by rustfmt
**/*.rs.bk

# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb
# others
build/*
!.gitkeep
.tmp/
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"rust-analyzer.cargo.target": "riscv64imac-unknown-none-elf"
}

109 changes: 109 additions & 0 deletions Cargo.lock

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

18 changes: 18 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[workspace]
members = ["ckb-typed-message", "contracts/typed-message-lock-demo"]
exclude = ["tests"]

[profile.release]
overflow-checks = true
opt-level = 3
panic = 'abort'
strip = true
lto = true
debug-assertions = true

[profile.dev]
strip = true
opt-level = 1
debug = false
panic = 'abort'
debug-assertions = true
5 changes: 5 additions & 0 deletions Cross.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[build]
default-target = "riscv64imac-unknown-none-elf"

[target.riscv64imac-unknown-none-elf]
image = "nervos/ckb-riscv-gnu-toolchain:focal-20230214"
21 changes: 21 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@


all:
capsule build --release

mol:
moleculec --language rust --schema-file schemas/basic.mol > ckb-typed-message/src/schemas/basic.rs
moleculec --language rust --schema-file schemas/top_level.mol > ckb-typed-message/src/schemas/top_level.rs
cargo fmt

install:
rustup target add riscv64imac-unknown-none-elf
cargo install cross --git https://github.com/cross-rs/cross
wget 'https://github.com/nervosnetwork/capsule/releases/download/v0.10.2/capsule_v0.10.2_x86_64-linux.tar.gz'
tar zxvf capsule_v0.10.2_x86_64-linux.tar.gz
mv capsule_v0.10.2_x86_64-linux/capsule ~/.cargo/bin
cargo install [email protected] --locked

ci:
capsule build --release
cd tests && cargo test && cd ..
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# ckb-typed-message(PoC)
This project is a proof of concept that aims to demonstrate how to adopt typed
messages (similar to EIP-712) in scripts. It also includes extended witnesses to
simplify signing and DApp interoperability.


## Build
Build contracts:

``` sh
capsule build
```

Run tests:

``` sh
capsule test
```
Empty file added build/.gitkeep
Empty file.
Binary file added build/auth
Binary file not shown.
13 changes: 13 additions & 0 deletions capsule.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# [rust]
# # path of rust contracts workspace directory,
# # a `Cargo.toml` file is expected under the directory.
# workspace_dir = "."

# capsule version
version = "0.10.2 5e49142"
# path of deployment config file
deployment = "deployment.toml"

[[contracts]]
name = "typed-message-lock-demo"
template_type = "Rust"
11 changes: 11 additions & 0 deletions ckb-typed-message/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "ckb-typed-message"
version = "0.1.0"
edition = "2021"

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

[dependencies]
blake2b-ref = "0.3.1"
ckb-std = { version = "0.14.3", default-features = false, features = ["ckb-types", "calc-hash"] }
molecule = { version = "0.7.5", default-features = false }
5 changes: 5 additions & 0 deletions ckb-typed-message/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

This is a library used for writing scripts that are intended to support typed
messages on CKB.


18 changes: 18 additions & 0 deletions ckb-typed-message/src/blake2b.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
pub use blake2b_ref::{Blake2b, Blake2bBuilder};

pub const CKB_PERSONALIZATION: &[u8] = b"ckb-default-hash";

pub fn new_blake2b() -> Blake2b {
Blake2bBuilder::new(32)
.personal(CKB_PERSONALIZATION)
.build()
}

pub fn hash(bytes: &[u8]) -> [u8; 32] {
let mut hasher = new_blake2b();
hasher.update(bytes);

let mut hash = [0u8; 32];
hasher.finalize(&mut hash);
hash
}
Loading

0 comments on commit 4da9459

Please sign in to comment.