diff --git a/packages/core/src/utility.ts b/packages/core/src/utility.ts index bdf8498..82364d5 100644 --- a/packages/core/src/utility.ts +++ b/packages/core/src/utility.ts @@ -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'); diff --git a/packages/core/test/fixtures/utility.ts b/packages/core/test/fixtures/utility.ts index 3e3ede4..40db486 100644 --- a/packages/core/test/fixtures/utility.ts +++ b/packages/core/test/fixtures/utility.ts @@ -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', + }, +]; diff --git a/packages/core/test/utility.spec.ts b/packages/core/test/utility.spec.ts index 4014d43..729411b 100644 --- a/packages/core/test/utility.spec.ts +++ b/packages/core/test/utility.spec.ts @@ -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)( @@ -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); + }, + ); });