Skip to content

Commit

Permalink
Unlock Service settings, migrate to Noble
Browse files Browse the repository at this point in the history
  • Loading branch information
martindale committed Sep 12, 2023
1 parent 9c67c3b commit 49217c7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
16 changes: 16 additions & 0 deletions tests/fabric.hash256.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ const Hash256 = require('../types/hash256');
const sample = 'Hello, world!';
const fixture = '315f5bdb76d078c43b8ac0064e4a0164612b1fce77c869345bfc94c75894edd3';

const NIST_TEST = 'abc';
const NIST_CHECK = 'ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad';

describe('@fabric/core/types/hash256', function () {
describe('Hash256', function () {
it('is available from @fabric/core', function () {
Expand Down Expand Up @@ -41,10 +44,23 @@ describe('@fabric/core/types/hash256', function () {
assert.strictEqual(digest, fixture);
});

it('provides a hash property', function () {
const sha256 = new Hash256(sample);
const digest = sha256.hash;
assert.ok(digest);
assert.strictEqual(digest.length, 64);
assert.strictEqual(digest, fixture);
});

it('throws an error when static digest() is called on a non-string', function () {
assert.throws(() => Hash256.digest({ sample }));
});

it('correctly provides the NIST hash', function () {
const hash = new Hash256(NIST_TEST);
assert.strictEqual(hash.value, NIST_CHECK);
});

it('can reverse a known hash', function () {
assert.throws(() => Hash256.digest({ sample }));

Expand Down
15 changes: 12 additions & 3 deletions types/hash256.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const { crypto } = require('bitcoinjs-lib');
const { sha256 } = require('@noble/hashes/sha256');

/**
* Simple interaction with 256-bit spaces.
Expand All @@ -24,16 +24,21 @@ class Hash256 {
}
}

// Ensure the input can be cast to a buffer
const buffer = Buffer.from(settings.input, 'utf8');

// Settings
this.settings = Object.assign({
hash: Hash256.digest(settings.input)
hash: Hash256.digest(buffer)
}, settings);

return this;
}

static compute (input) {
if (typeof input === 'string') input = Buffer.from(input, 'utf8');
return crypto.hash256(input).toString('hex');
const buffer = sha256(input);
return Buffer.from(buffer).toString('hex');
}

/**
Expand All @@ -49,6 +54,10 @@ class Hash256 {
return Hash256.compute(input);
}

get hash () {
return this.value;
}

// TODO: document `hash256.value`
get value () {
return Hash256.digest(this.settings.input);
Expand Down
2 changes: 1 addition & 1 deletion types/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class Service extends Actor {
messages: {},
members: {}
} */
}, this.settings, settings);
}, settings);

// Reserve a place for ourselves
this.agent = null;
Expand Down

0 comments on commit 49217c7

Please sign in to comment.