Skip to content

Commit

Permalink
enabled hash check with difficulty
Browse files Browse the repository at this point in the history
  • Loading branch information
calidion committed Oct 31, 2019
1 parent 60fe092 commit 808712b
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 2 deletions.
4 changes: 4 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ Hash.slow = (buffer, variant) => {
return addon.getSlowHash(buffer, variant);
}

Hash.check = (buffer, difficulty) => {
return addon.checkHash(buffer, difficulty);
}

module.exports.Scalar = Scalar;
module.exports.Crypto = Crypto;
module.exports.Key = Key;
Expand Down
7 changes: 7 additions & 0 deletions native/src/hash.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use neon::prelude::*;
use cryptonote_raw_crypto::{hash::Hash};
use std::fmt::Write;
use util::*;

pub fn get_fast_hash(mut cx: FunctionContext) -> JsResult<JsString> {
let mut b: Handle<JsBuffer> = cx.argument(0)?;
Expand Down Expand Up @@ -30,3 +31,9 @@ pub fn get_slow_hash(mut cx: FunctionContext) -> JsResult<JsString> {
}
Ok(cx.string(s))
}

pub fn check_with_difficulty(mut cx: FunctionContext) -> JsResult<JsBoolean> {
let hash = get_hash(&mut cx, 0);
let difficulty = cx.argument::<JsNumber>(1)?.value() as u64;
Ok(cx.boolean(Hash::check_with_difficulty(&hash, difficulty)))
}
1 change: 1 addition & 0 deletions native/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ register_module!(mut cx, {
cx.export_class::<JsDifficulty>("Difficulty")?;
cx.export_function("getFastHash", get_fast_hash)?;
cx.export_function("getSlowHash", get_slow_hash)?;
cx.export_function("checkHash", check_with_difficulty)?;
cx.export_function("isScalar", is_scalar)?;
cx.export_function("setupRandom", init_random)?;
cx.export_function("randomScalar", random_scalar)?;
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@vigcoin/neon",
"version": "0.2.2",
"version": "0.2.3",
"description": "",
"main": "lib/index.js",
"author": "calidion <[email protected]>",
Expand Down
7 changes: 7 additions & 0 deletions test/hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,11 @@ describe("Test Hash", () => {
assert(expected.equals(Buffer.from(Hash.slow(plain, 0), 'hex')));
}
});

it("should test check hash", function () {
assert(Hash.check(Buffer.from([
0, 223, 74, 253, 65, 221, 188, 172, 253, 50, 122, 246, 173, 212, 162, 103, 13, 174, 254,
199, 175, 49, 254, 177, 181, 91, 56, 9, 98, 1, 0, 0
]), 10343869));
});
});

0 comments on commit 808712b

Please sign in to comment.