From 2a0c9416c0f7977fc5aa7f06c1fcb9d1c6cc9c2b Mon Sep 17 00:00:00 2001 From: Nesopie Date: Thu, 28 Mar 2024 08:18:41 +0530 Subject: [PATCH] chore: change sha256 to sha160 --- src/features/validator/hash.ts | 21 ++++++++++++++------- src/features/validator/signature.ts | 4 ++-- src/utils.ts | 15 ++++++--------- 3 files changed, 22 insertions(+), 18 deletions(-) diff --git a/src/features/validator/hash.ts b/src/features/validator/hash.ts index d709b2a..925b09d 100644 --- a/src/features/validator/hash.ts +++ b/src/features/validator/hash.ts @@ -1,8 +1,14 @@ -import { Transaction, TransactionType } from "../../types"; +import { TransactionType } from "../../types"; import * as crypto from "crypto"; -import { asmToHex, hash256, sha256 } from "../../utils"; +import { asmToHex, hash160, sha256 } from "../../utils"; import { OP_CODES } from "../script/op_codes"; import { collapseTextChangeRangesAcrossMultipleVersions } from "typescript"; +import { Transaction } from "../transaction"; + +//in p2sh p2wsh +//sha256(inner witness script) = last part of inneer redeem script +//hash160 of the inner redeem script should be equal to the hash in the script pubkey +//eg 0d9ef76964c23e940ebcddde868c1089dfdb52147364da01ee92438dfb7c9375 export const HashValidator = (tx: Transaction) => { let lockingScript = ""; @@ -11,12 +17,13 @@ export const HashValidator = (tx: Transaction) => { let publicKey = ""; for (const input of tx.vin) { + if (!input.prevout) continue; switch (input.prevout.scriptpubkey_type) { case TransactionType.P2PKH: const scriptAsmTokens = input.scriptsig_asm.split(" "); publicKey = scriptAsmTokens[scriptAsmTokens.length - 1]; - hash = hash256(publicKey); + hash = hash160(publicKey); lockingScript = input.prevout.scriptpubkey; script = `${OP_CODES.OP_DUP}${OP_CODES.OP_HASH160}${OP_CODES.OP_PUSHBYTES_20}${hash}${OP_CODES.OP_EQUALVERIFY}${OP_CODES.OP_CHECKSIG}`; @@ -28,27 +35,27 @@ export const HashValidator = (tx: Transaction) => { const hex = asmToHex(inputScript[inputScript.length - 1]); lockingScript = input.prevout.scriptpubkey; - const scriptHash = hash256(hex); + const scriptHash = hash160(hex); script = `${OP_CODES.OP_HASH160}${OP_CODES.OP_PUSHBYTES_20}${scriptHash}${OP_CODES.OP_EQUAL}`; if (script !== lockingScript) return false; break; case TransactionType.P2WPKH: + if (!input.witness || !input.witness[1]) return false; publicKey = input.witness[1]; - if (!publicKey) return false; - hash = hash256(publicKey); + hash = hash160(publicKey); lockingScript = input.prevout.scriptpubkey; script = `${OP_CODES.OP_0}${OP_CODES.OP_PUSHBYTES_20}${hash}`; if (script !== lockingScript) return false; break; case TransactionType.P2WSH: + if (!input.witness) return false; const witnessScript = input.witness[input.witness.length - 1]; if (!witnessScript) return false; lockingScript = input.prevout.scriptpubkey; - //OP_0 OP_PUSHBYTES_32 0b685cc06add0b2e23bcd67f0bef8d364cdc1abcf6fb126958826a7cfe351bf3 script = `${OP_CODES.OP_0}${OP_CODES.OP_PUSHBYTES_32}${sha256( witnessScript )}`; diff --git a/src/features/validator/signature.ts b/src/features/validator/signature.ts index 1987e9f..e1ce455 100644 --- a/src/features/validator/signature.ts +++ b/src/features/validator/signature.ts @@ -1,6 +1,6 @@ import { SigHash, TransactionType } from "../../types"; import { Transaction } from "../transaction"; -import { hash256, sha256 } from "../../utils"; +import { hash160, sha256 } from "../../utils"; import * as asn1js from "asn1js"; import { ECPairFactory } from "ecpair"; @@ -66,7 +66,7 @@ export const signatureValidator = (tx: Transaction): boolean => { if (!input.witness) return false; derEncodedSignature = input.witness[0]; pubkey = input.witness[1]; - const pubkeyHash = hash256(pubkey); + const pubkeyHash = hash160(pubkey); const pubkeyInScript = input.prevout.scriptpubkey.slice(4); if (pubkeyHash !== pubkeyInScript) return false; diff --git a/src/utils.ts b/src/utils.ts index f8693b1..14daafb 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,7 +1,7 @@ import * as crypto from "crypto"; import { OP_CODES } from "./features/script/op_codes"; -export const hash256 = (str: string) => { +export const hash160 = (str: string) => { return crypto .createHash("ripemd160") .update(Buffer.from(sha256(str), "hex")) @@ -31,11 +31,8 @@ export const reversify = (str: string) => { .join(""); }; -// const asm = "OP_0 OP_PUSHBYTES_20 15ff0337937ecadd10ce56ffdfd4674817613223"; -// const hex = asmToHex(asm); -// console.log(hex); -// console.log( -// sha256( -// "201dda24da0b91e0eed9770878c504aeb07628ca4ccae9a7bd5347b96ee85dac52ac0063036f726401010a746578742f706c61696e00357b2270223a226272632d3230222c226f70223a226d696e74222c227469636b223a22646f6765222c22616d74223a2234323030227d68" -// ) -// ); +console.log( + hash160( + "00202791caef68f38a0fa3f14d5f4169894ebc318355d2c33bfc1a9d606403b1dbea" + ) +);