From b5190e10129859f2264c91e3cddebd21f9946431 Mon Sep 17 00:00:00 2001 From: Rafal Czajkowski Date: Wed, 5 Jul 2023 15:50:08 +0200 Subject: [PATCH] Add unit tests for `createOutputScriptFromAddress` Test this fn with mainnet addressess: - P2PKH (`1*`) - P2WPKH (`bc1q*`) - P2SH (`3*`) - P2WSH (`bc1q*`) --- typescript/test/bitcoin.test.ts | 161 ++++++++++++++++++++++---------- 1 file changed, 114 insertions(+), 47 deletions(-) diff --git a/typescript/test/bitcoin.test.ts b/typescript/test/bitcoin.test.ts index 93b849ae8..a1bb5da82 100644 --- a/typescript/test/bitcoin.test.ts +++ b/typescript/test/bitcoin.test.ts @@ -467,53 +467,120 @@ describe("Bitcoin", () => { }) describe("createOutputScriptFromAddress", () => { - const btcAddresses = { - P2PKH: { - address: "mjc2zGWypwpNyDi4ZxGbBNnUA84bfgiwYc", - redeemerOutputScript: - "0x1976a9142cd680318747b720d67bf4246eb7403b476adb3488ac", - }, - P2WPKH: { - address: "tb1qumuaw3exkxdhtut0u85latkqfz4ylgwstkdzsx", - redeemerOutputScript: - "0x160014e6f9d74726b19b75f16fe1e9feaec048aa4fa1d0", - }, - P2SH: { - address: "2MsM67NLa71fHvTUBqNENW15P68nHB2vVXb", - redeemerOutputScript: - "0x17a914011beb6fb8499e075a57027fb0a58384f2d3f78487", - }, - P2WSH: { - address: - "tb1qau95mxzh2249aa3y8exx76ltc2sq0e7kw8hj04936rdcmnynhswqqz02vv", - redeemerOutputScript: - "0x220020ef0b4d985752aa5ef6243e4c6f6bebc2a007e7d671ef27d4b1d0db8dcc93bc1c", - }, - } - - Object.entries(btcAddresses).forEach( - ([ - addressType, - { address, redeemerOutputScript: expectedRedeemerOutputScript }, - ]) => { - it(`should create correct output script for ${addressType} address type`, () => { - const result = createOutputScriptFromAddress(address) - - // Check if we can build the prefixed raw redeemer output script based - // on the result. - // Convert the output script to raw bytes buffer. - const rawRedeemerOutputScript = Buffer.from(result.toString(), "hex") - // Prefix the output script bytes buffer with 0x and its own length. - const prefixedRawRedeemerOutputScript = `0x${Buffer.concat([ - Buffer.from([rawRedeemerOutputScript.length]), - rawRedeemerOutputScript, - ]).toString("hex")}` - - expect(prefixedRawRedeemerOutputScript).to.eq( - expectedRedeemerOutputScript - ) - }) + context("with testnet addresses", () => { + const btcAddresses = { + P2PKH: { + address: "mjc2zGWypwpNyDi4ZxGbBNnUA84bfgiwYc", + redeemerOutputScript: + "0x1976a9142cd680318747b720d67bf4246eb7403b476adb3488ac", + outputScript: "76a9142cd680318747b720d67bf4246eb7403b476adb3488ac", + }, + P2WPKH: { + address: "tb1qumuaw3exkxdhtut0u85latkqfz4ylgwstkdzsx", + redeemerOutputScript: + "0x160014e6f9d74726b19b75f16fe1e9feaec048aa4fa1d0", + outputScript: "0014e6f9d74726b19b75f16fe1e9feaec048aa4fa1d0", + }, + P2SH: { + address: "2MsM67NLa71fHvTUBqNENW15P68nHB2vVXb", + redeemerOutputScript: + "0x17a914011beb6fb8499e075a57027fb0a58384f2d3f78487", + outputScript: "a914011beb6fb8499e075a57027fb0a58384f2d3f78487", + }, + P2WSH: { + address: + "tb1qau95mxzh2249aa3y8exx76ltc2sq0e7kw8hj04936rdcmnynhswqqz02vv", + redeemerOutputScript: + "0x220020ef0b4d985752aa5ef6243e4c6f6bebc2a007e7d671ef27d4b1d0db8dcc93bc1c", + outputScript: + "0020ef0b4d985752aa5ef6243e4c6f6bebc2a007e7d671ef27d4b1d0db8dcc93bc1c", + }, } - ) + + Object.entries(btcAddresses).forEach( + ([ + addressType, + { + address, + redeemerOutputScript: expectedRedeemerOutputScript, + outputScript: expectedOutputScript, + }, + ]) => { + it(`should create correct output script for ${addressType} address type`, () => { + const result = createOutputScriptFromAddress(address) + + expect(result.toString()).to.eq(expectedOutputScript) + // Check if we can build the prefixed raw redeemer output script based + // on the result. + expect(buildRawPrefixedOutputScript(result.toString())).to.eq( + expectedRedeemerOutputScript + ) + }) + } + ) + }) + + context("with mainnet addresses", () => { + const btcAddresses = { + P2PKH: { + address: "12higDjoCCNXSA95xZMWUdPvXNmkAduhWv", + redeemerOutputScript: + "0x1976a91412ab8dc588ca9d5787dde7eb29569da63c3a238c88ac", + outputScript: "76a91412ab8dc588ca9d5787dde7eb29569da63c3a238c88ac", + }, + P2WPKH: { + address: "bc1q34aq5drpuwy3wgl9lhup9892qp6svr8ldzyy7c", + redeemerOutputScript: + "0x1600148d7a0a3461e3891723e5fdf8129caa0075060cff", + outputScript: "00148d7a0a3461e3891723e5fdf8129caa0075060cff", + }, + P2SH: { + address: "342ftSRCvFHfCeFFBuz4xwbeqnDw6BGUey", + redeemerOutputScript: + "0x17a91419a7d869032368fd1f1e26e5e73a4ad0e474960e87", + outputScript: "a91419a7d869032368fd1f1e26e5e73a4ad0e474960e87", + }, + P2WSH: { + address: + "bc1qeklep85ntjz4605drds6aww9u0qr46qzrv5xswd35uhjuj8ahfcqgf6hak", + redeemerOutputScript: + "0x220020cdbf909e935c855d3e8d1b61aeb9c5e3c03ae8021b286839b1a72f2e48fdba70", + outputScript: + "0020cdbf909e935c855d3e8d1b61aeb9c5e3c03ae8021b286839b1a72f2e48fdba70", + }, + } + + Object.entries(btcAddresses).forEach( + ([ + addressType, + { + address, + redeemerOutputScript: expectedRedeemerOutputScript, + outputScript: expectedOutputScript, + }, + ]) => { + it(`should create correct output script for ${addressType} address type`, () => { + const result = createOutputScriptFromAddress(address) + + expect(result.toString()).to.eq(expectedOutputScript) + // Check if we can build the prefixed raw redeemer output script based + // on the result. + expect(buildRawPrefixedOutputScript(result.toString())).to.eq( + expectedRedeemerOutputScript + ) + }) + } + ) + }) }) }) + +const buildRawPrefixedOutputScript = (outputScript: string) => { + // Convert the output script to raw bytes buffer. + const rawRedeemerOutputScript = Buffer.from(outputScript.toString(), "hex") + // Prefix the output script bytes buffer with 0x and its own length. + return `0x${Buffer.concat([ + Buffer.from([rawRedeemerOutputScript.length]), + rawRedeemerOutputScript, + ]).toString("hex")}` +}