Skip to content
This repository was archived by the owner on Jan 21, 2023. It is now read-only.

Commit e69d268

Browse files
committed
WIP hash160
1 parent e90699a commit e69d268

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@ authors = [ "onokatio <onokatio@gmail.com>"]
66

77
[dependencies]
88
bimap = "0.3"
9+
sha2 = "0.8"
10+
ripemd160 = "0.8"
11+
digest = "0.8"

src/main.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
extern crate bimap;
2+
extern crate sha2;
3+
extern crate ripemd160;
4+
extern crate digest;
25

36
//use std::io;
7+
use std::mem;
48
use bimap::BiMap;
9+
use digest::Digest;
10+
use sha2::Sha256;
11+
use ripemd160::Ripemd160;
512

613
struct Compiler {
714
opcode_list: BiMap<&'static str, i32>,
@@ -35,6 +42,7 @@ impl Compiler {
3542
opcode_list.insert("OP_NOTIF", 0x64);
3643
opcode_list.insert("OP_ELSE", 0x67);
3744
opcode_list.insert("OP_ENDIF", 0x68);
45+
opcode_list.insert("OP_HASH160", 0xa9);
3846

3947
let mut opcode_alias_list: BiMap<&'static str, &'static str> = BiMap::new();
4048
opcode_alias_list.insert("OP_FALSE", "OP_0");
@@ -148,6 +156,7 @@ impl<'borrow_code_lifetime> VM<'borrow_code_lifetime> {
148156
else if self.codes[self.pc] == compiler.compile_single("OP_IF") { self.op_if(); }
149157
else if self.codes[self.pc] == compiler.compile_single("OP_ENDIF") { return 1; }
150158
else if self.codes[self.pc] == compiler.compile_single("OP_ELSE") { return 1; }
159+
else if self.codes[self.pc] == compiler.compile_single("OP_HASH160") { self.op_hash160(); }
151160
else { panic!("[VM] The opcode {:#x} is not implemented yet,", self.codes[self.pc]); }
152161

153162
return 0;
@@ -211,6 +220,17 @@ impl<'borrow_code_lifetime> VM<'borrow_code_lifetime> {
211220

212221
//panic!("debug");
213222
}
223+
fn op_hash160(&mut self){
224+
225+
let value = self.stack.pop().unwrap();
226+
227+
let sha256hash = Sha256::digest(&value.to_be_bytes());
228+
let ripemd160hash = Ripemd160::digest(sha256hash.as_slice()).as_ref();
229+
230+
let value = mem::transmute::<[u8; 12], i32>(ripemd160hash);
231+
232+
self.stack.push(ripemd160hash.as_ref());
233+
}
214234
}
215235

216236
fn main() {

0 commit comments

Comments
 (0)