Skip to content
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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
target/*
.vscode/*
*/build/*
Cargo.lock
Cargo.lock
/ckb-miscellaneous-scripts
.history
69 changes: 37 additions & 32 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,2 +1,6 @@
[workspace]
members = ["ckb-lib-rsa","ckb-lib-secp256k1","ckb-lib-smt"]
members = [
"ckb-lib-rsa",
"ckb-lib-secp256k1",
"ckb-lib-smt",
]
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,17 @@ git clone https://github.com/nervosnetwork/ckb-miscellaneous-scripts

cd ckb-miscellaneous-scripts

git checkout -b static-lib

git pull origin static-lib

make all-via-docker

mv ./c/build/ckb_smt ./ckb-lib-smt/lib/
make static-via-docker

cp ./ckb-miscellaneous-scripts/build/librsa_secp256k1.a ./ckb-lib-smt/lib/

mv ./ckb-miscellaneous-scripts/build/secp256k1_blake2b_sighash_all_dual ./ckb-lib-secp256k1/lib
cp ./ckb-miscellaneous-scripts/build/librsa_secp256k1.a ./ckb-lib-secp256k1/lib

mv ./ckb-miscellaneous-scripts/build/rsa_sighash_all ./ckb-lib-rsa/lib
cp ./ckb-miscellaneous-scripts/build/librsa_secp256k1.a ./ckb-lib-rsa/lib
```
15 changes: 8 additions & 7 deletions ckb-lib-rsa/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@
authors = ["johnz <johnz@lay2.dev>"]
edition = "2018"
name = "ckb-lib-rsa"
version = "0.1.0"
version = "0.3.0"

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

[dependencies]
ckb-std = {version = "0.7",optional = true}
email-rs = {git = "https://github.com/sking789/email-rs.git"}
cfg-if = "1.0"
ckb-std = {version = "0.9",optional = true}
email-rs = {git = "https://github.com/sking789/email-rs.git", branch = "multi-dkim-header"}
sha2 = {version = "0.9.2", default-features = false}

[build-dependencies]
blake2b-rs = "0.1.5"
cfg-if = "1.0"
blake2b-rs = "0.2"

[features]
no_std = ["ckb-std"]
c_file = []
default = ["no_std"]
static_lib = ["ckb-std"]
dynamic_lib = ["ckb-std"]
111 changes: 65 additions & 46 deletions ckb-lib-rsa/build.rs
Original file line number Diff line number Diff line change
@@ -1,57 +1,76 @@
pub use blake2b_rs::{Blake2b, Blake2bBuilder};
use std::path::Path;

use std::{
fs::File,
io::{BufWriter, Read, Write},
path::Path,
};
cfg_if::cfg_if! {
if #[cfg(feature="static_lib")] {
fn build_fn() {
let dir = std::env::var("CARGO_MANIFEST_DIR").unwrap();
println!(
"cargo:rustc-link-search=native={}",
Path::new(&dir).join("lib").display()
);
println!("cargo:rustc-link-lib=static=rsa_secp256k1");
}
} else {
use std::{
fs::File,
io::{BufWriter, Read, Write},
};

const BUF_SIZE: usize = 8 * 1024;
const CKB_HASH_PERSONALIZATION: &[u8] = b"ckb-default-hash";
const BUF_SIZE: usize = 8 * 1024;

fn main() {
let paths = [(
"RSA",
// Path::new("../ckb-production-scripts/build/validate_signature_rsa"),
Path::new("./lib/rsa_sighash_all"),
)];

if paths.iter().find(|(_, path)| !path.exists()).is_some() {
return;
}
fn build_fn() {
let paths = [(
"RSA",
// Path::new("../ckb-production-scripts/build/validate_signature_rsa"),
Path::new("./lib/rsa_sighash_all"),
)];

let out_path = Path::new("src").join("code_hashes.rs");
let mut out_file = BufWriter::new(File::create(&out_path).expect("create code_hashes.rs"));

paths.iter().for_each(|(name, path)| {
let mut buf = [0u8; BUF_SIZE];

// build hash
let mut blake2b = new_blake2b();
let mut fd = File::open(path).expect("open file");
loop {
let read_bytes = fd.read(&mut buf).expect("read file");
if read_bytes > 0 {
blake2b.update(&buf[..read_bytes]);
} else {
break;
if paths.iter().find(|(_, path)| !path.exists()).is_some() {
return;
}

let out_path = Path::new("src").join("code_hashes.rs");
let mut out_file = BufWriter::new(File::create(&out_path).expect("create code_hashes.rs"));

paths.iter().for_each(|(name, path)| {
let mut buf = [0u8; BUF_SIZE];

// build hash
let mut blake2b = new_blake2b();
let mut fd = File::open(path).expect("open file");
loop {
let read_bytes = fd.read(&mut buf).expect("read file");
if read_bytes > 0 {
blake2b.update(&buf[..read_bytes]);
} else {
break;
}
}

let mut hash = [0u8; 32];
blake2b.finalize(&mut hash);

write!(
&mut out_file,
"pub const CODE_HASH_{:}: [u8; 32] = {:?};\n",
name, hash
)
.expect("write to code_hashes.rs");
});
}

let mut hash = [0u8; 32];
blake2b.finalize(&mut hash);
pub use blake2b_rs::{Blake2b, Blake2bBuilder};

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

write!(
&mut out_file,
"pub const CODE_HASH_{:}: [u8; 32] = {:?};\n",
name, hash
)
.expect("write to code_hashes.rs");
});
pub fn new_blake2b() -> Blake2b {
Blake2bBuilder::new(32)
.personal(CKB_HASH_PERSONALIZATION)
.build()
}
}
}

pub fn new_blake2b() -> Blake2b {
Blake2bBuilder::new(32)
.personal(CKB_HASH_PERSONALIZATION)
.build()
fn main() {
build_fn()
}
Binary file added ckb-lib-rsa/lib/librsa_secp256k1.a
Binary file not shown.
37 changes: 19 additions & 18 deletions ckb-lib-rsa/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,26 @@
* @Description: In User Settings Edit
* @FilePath: /ckb-lib-c-script/ckb-lib-rsa/src/lib.rs
*/
#![no_std]
#[cfg(all(feature = "c_file", feature = "no_std"))]
compile_error!("feature \"no_std\" and feature \"c_file\" cannot be enabled at the same time");

#[cfg(feature = "no_std")]
extern crate alloc;

mod code_hashes;
#[cfg(feature = "no_std")]
mod librsa;

pub use code_hashes::CODE_HASH_RSA;
#[cfg(feature = "no_std")]
pub use librsa::*;
pub mod email_rs{
pub mod email_rs {
pub use email_rs::*;
}

#[cfg(feature = "c_file")]
pub fn get_librsa_bin() -> &'static [u8] {
include_bytes!("../lib/rsa_sighash_all")
cfg_if::cfg_if! {
if #[cfg(feature = "static_lib" )] {
extern crate alloc;

mod librsa_static;
pub use librsa_static::*;
} else if #[cfg(feature = "dynamic_lib")] {
extern crate alloc;

mod code_hashes;
pub use code_hashes::CODE_HASH_RSA;
mod librsa_dynamic;
pub use librsa_dynamic::*;
} else {
pub fn get_librsa_bin() -> &'static [u8] {
include_bytes!("../lib/rsa_sighash_all")
}
}
}
Loading