Skip to content

Commit

Permalink
feat: add createTaggedHash util
Browse files Browse the repository at this point in the history
Signed-off-by: Anmol Sharma <anmolsharma0234@gmail.com>
  • Loading branch information
theanmolsharma committed Mar 2, 2024
1 parent 2d88828 commit b194d40
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
9 changes: 9 additions & 0 deletions packages/core/src/utility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ export const hashOutpoints = (outpoints: Outpoint[]): Buffer => {
return createHash('sha256').update(outpointBuffer).digest();
};

export const createTaggedHash = (tag: string, buffer: Buffer): Buffer => {
const tagHash = createHash('sha256').update(tag, 'utf8').digest();
return createHash('sha256')
.update(tagHash)
.update(tagHash)
.update(buffer)
.digest();
};

export const calculateSumOfPrivateKeys = (keys: PrivateKey[]): Buffer => {
const negatedKeys = keys.map((key) => {
const privateKey = Buffer.from(key.key, 'hex');
Expand Down
21 changes: 21 additions & 0 deletions packages/core/test/fixtures/utility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,24 @@ export const inputPrivateKeys = [
'ee55616ce5a93e508f03f21949ecbe70a2a0b107b6e1df5d98b4e4da4adaca1b',
},
];

export const createTaggedHashData = [
{
tag: 'tag',
hex: '0000000000000000000000000000000000000000000000000000000000000000',
expected:
'25a36caa510aee2994cd72a09782258d8621d99a9df9c5d7428ef6a7b026f7bb',
},
{
tag: 'tag',
hex: 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff',
expected:
'f5f36a052776e5652c4250f4c49a78415da4c366531839c5d27ba6aafefe3859',
},
{
tag: 'tag',
hex: '25a36caa510aee2994cd72a09782258d8621d99a9df9c5d7428ef6a7b026f7bbf5f36a052776e5652c4250f4c49a78415da4c366531839c5d27ba6aafefe3859',
expected:
'de9cd3236391a236466fbc0d3c79e62503cde0270c6df0c4a7d3d10ca94404e4',
},
];
15 changes: 14 additions & 1 deletion packages/core/test/utility.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import {
calculateSumOfPrivateKeys,
createTaggedHash,
hashOutpoints,
Outpoint,
PrivateKey,
} from '../src';
import { inputPrivateKeys, outpoints } from './fixtures/utility';
import {
createTaggedHashData,
inputPrivateKeys,
outpoints,
} from './fixtures/utility';

describe('Utility', () => {
it.each(outpoints)(
Expand All @@ -24,4 +29,12 @@ describe('Utility', () => {
expect(sum.toString('hex')).toBe(expected);
},
);

it.each(createTaggedHashData)(
'should calculate tagged hash',
({ tag, hex, expected }) => {
const taggedHash = createTaggedHash(tag, Buffer.from(hex, 'hex'));
expect(taggedHash.toString('hex')).toBe(expected);
},
);
});

0 comments on commit b194d40

Please sign in to comment.