Skip to content

Commit

Permalink
Added unit tests for sha256
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaszslabon committed Sep 29, 2023
1 parent d868a2f commit 04e8d75
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
2 changes: 0 additions & 2 deletions typescript/src/bitcoin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,6 @@ export function createKeyRing(
* @param text - Text the HASH160 is computed for.
* @returns Hash as a 20-byte un-prefixed hex string.
*/
// TODO: Make it use Hex for input and return values.
export function computeHash160(text: string): string {
const sha256Hash = utils.sha256(
Hex.from(Buffer.from(text, "hex")).toPrefixedString()
Expand All @@ -542,7 +541,6 @@ export function computeHash160(text: string): string {
* @param text - Text the single SHA256 is computed for.
* @returns Hash as a 32-byte un-prefixed hex string.
*/
// TODO: Consider adding unit tests.
export function computeSha256(text: Hex): Hex {
const hash = utils.sha256(text.toPrefixedString())
return Hex.from(hash)
Expand Down
16 changes: 16 additions & 0 deletions typescript/test/bitcoin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
createAddressFromPublicKey,
readCompactSizeUint,
computeHash160,
computeSha256,
computeHash256,
isP2PKHScript,
isP2WPKHScript,
Expand Down Expand Up @@ -98,6 +99,21 @@ describe("Bitcoin", () => {
})
})

describe("computeSha256", () => {
it("should compute hash256 correctly", () => {
const hexValue = Hex.from(
"03474444cca71c678f5019d16782b6522735717a94602085b4adf707b465c36ca8"
)
const expectedSha256 = Hex.from(
"c62e5cb26c97cb52fea7f9965e9ea1f8d41c97773688aa88674e64629fc02901"
)

expect(computeSha256(hexValue).toString()).to.be.equal(
expectedSha256.toString()
)
})
})

describe("P2PKH <-> public key hash conversion", () => {
const publicKeyHash = "3a38d44d6a0c8d0bb84e0232cc632b7e48c72e0e"
const P2WPKHAddress = "bc1q8gudgnt2pjxshwzwqgevccet0eyvwtswt03nuy"
Expand Down

0 comments on commit 04e8d75

Please sign in to comment.