forked from nervosnetwork/ckb-system-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.rs
94 lines (79 loc) · 2.59 KB
/
build.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
pub use blake2b_rs::{Blake2b, Blake2bBuilder};
use includedir_codegen::Compression;
use std::{
env,
fs::File,
io::{BufWriter, Read, Write},
path::Path,
};
const PATH_PREFIX: &str = "specs/cells/";
const BUF_SIZE: usize = 8 * 1024;
const CKB_HASH_PERSONALIZATION: &[u8] = b"ckb-default-hash";
const BINARIES: &[(&str, &str)] = &[
(
"secp256k1_blake160_sighash_all",
"bcacdaca1952748eac376c2c236783a960d09227d767b0224274d2000f976cd1",
),
(
"secp256k1_data",
"9799bee251b975b82c45a02154ce28cec89c5853ecc14d12b7b8cccfc19e0af4",
),
(
"dao",
"32064a14ce10d95d4b7343054cc19d73b25b16ae61a6c681011ca781a60c7923",
),
(
"secp256k1_blake160_multisig_all",
"43400de165f0821abf63dcac299bbdf7fd73898675ee4ddb099b0a0d8db63bfb",
),
];
fn main() {
let mut bundled = includedir_codegen::start("BUNDLED_CELL");
let out_path = Path::new(&env::var("OUT_DIR").unwrap()).join("code_hashes.rs");
let mut out_file = BufWriter::new(File::create(&out_path).expect("create code_hashes.rs"));
let mut errors = Vec::new();
for (name, expected_hash) in BINARIES {
let path = format!("{}{}", PATH_PREFIX, name);
let mut buf = [0u8; BUF_SIZE];
bundled
.add_file(&path, Compression::Gzip)
.expect("add files to resource bundle");
// 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);
let actual_hash = faster_hex::hex_string(&hash);
if expected_hash != &actual_hash {
errors.push((name, expected_hash, actual_hash));
continue;
}
writeln!(
&mut out_file,
"pub const {}: [u8; 32] = {:?};",
format!("CODE_HASH_{}", name.to_uppercase()),
hash
)
.expect("write to code_hashes.rs");
}
if !errors.is_empty() {
for (name, expected, actual) in errors.into_iter() {
eprintln!("{}: expect {}, actual {}", name, expected, actual);
}
panic!("not all hashes are right");
}
bundled.build("bundled.rs").expect("build resource bundle");
}
pub fn new_blake2b() -> Blake2b {
Blake2bBuilder::new(32)
.personal(CKB_HASH_PERSONALIZATION)
.build()
}