import BitcoinSignatureHelper from "../signature" import testData from "./data" describe("BitcoinSignatureHelper", () => { describe("normalizeV", () => { const range = [0, 1, 2, 3] const types = { "compressed p2pkh": 27, "uncompressed p2pkh": 31, "p2sh-p2wpkh": 35, p2wpkh: 39, } describe.each(range)( "should correctly normalize offset %i", (baseOffset) => { it.each(Object.entries(types))( "to %s offsets", (_, normalizedOffset) => { expect( BitcoinSignatureHelper.normalizeV(baseOffset, normalizedOffset), ).toEqual(baseOffset + normalizedOffset) }, ) }, ) describe.each(range)( "should correctly set the requested high bit and normalize offset %i", (baseOffset) => { it.each(Object.entries(types))( "to %s offsets", (_, normalizedOffset) => { const normalizedV = BitcoinSignatureHelper.normalizeV( baseOffset, normalizedOffset, true, ) // eslint-disable-next-line no-bitwise expect(normalizedV & 0b0111_1111).toEqual( baseOffset + normalizedOffset, ) // eslint-disable-next-line no-bitwise expect(normalizedV & 0b1000_0000).toEqual(0b1000_0000) }, ) }, ) }) describe("normalizeSignature", () => { describe("supported addresses", () => { describe.each([ { addressType: "P2SH-P2WPKH", address: "2N3dhPvUMaGaqrXGrHa8VnKVhfxy38A4YVW", signature: Buffer.from( "1f2d48374c17b5af8da605fbeb1540540f0e4e0a5108c26c13ac4ec3bd9eafa8011951b2160995b9a9da85502a6c3c689a556f542a0e7a57f7cd187670e7d9c4f9", "hex", ), expectedSignature: "0x232d48374c17b5af8da605fbeb1540540f0e4e0a5108c26c13ac4ec3bd9eafa8011951b2160995b9a9da85502a6c3c689a556f542a0e7a57f7cd187670e7d9c4f9", publicKey: "033b37d8b5dda991cdeb628c28c7958cf9d7bc61dfde29357b8a7190b9b3295423", }, { addressType: "P2PKH", address: "mrXtbjt4XXcX2aruY1Lx58YF2F8LXYzi1U", signature: Buffer.from( "1fdea55216183919f35950e0bfe5b4889c6103c708d50fd2e0fe7eddc826b295a94a504475fb5f8896a87ac156590575a33390d8bde9ddb637aa096ae61546201f", "hex", ), expectedSignature: "0x1fdea55216183919f35950e0bfe5b4889c6103c708d50fd2e0fe7eddc826b295a94a504475fb5f8896a87ac156590575a33390d8bde9ddb637aa096ae61546201f", // Public key is not required for this type of address. publicKey: "", }, { addressType: "P2WPKH", address: "tb1q3x5lk9l83aek9c0vu38tx69u2lyxsdy6tww2rp", signature: Buffer.from( "20ab1f9cc6955f17a18240014fae5303576ea9a7caacabebf26c2b296e7b7b75857b432a1e0b6499b8f4a82f34f02e293b884de1792c5d6cd761eefe4e9f1130c0", "hex", ), // Public key is not required for this type of address. publicKey: "", expectedSignature: "0x28ab1f9cc6955f17a18240014fae5303576ea9a7caacabebf26c2b296e7b7b75857b432a1e0b6499b8f4a82f34f02e293b884de1792c5d6cd761eefe4e9f1130c0", }, ])( "$addressType", ({ signature, expectedSignature, address, publicKey }) => { it("should return correct signature", () => { expect( BitcoinSignatureHelper.normalizeSignature( signature, address, publicKey, ), ).toBe(expectedSignature) }) }, ) }) describe("unsupported addresses", () => { const unsupportedAddresses = [ ...testData.unsupportedP2TRAddresses, ...testData.unsupportedP2SHAddresses, ] const signature = Buffer.from( "20ab1f9cc6955f17a18240014fae5303576ea9a7caacabebf26c2b296e7b7b75857b432a1e0b6499b8f4a82f34f02e293b884de1792c5d6cd761eefe4e9f1130c0", "hex", ) describe.each(unsupportedAddresses)("$type", ({ address }) => { it("should throw an error", () => { expect(() => BitcoinSignatureHelper.normalizeSignature(signature, address, ""), ).toThrow("Unsupported Bitcoin address type") }) }) }) }) })