Skip to content

Commit

Permalink
chore: change sha256 to sha160
Browse files Browse the repository at this point in the history
  • Loading branch information
Nesopie committed Mar 28, 2024
1 parent b1eb1a3 commit 2a0c941
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 18 deletions.
21 changes: 14 additions & 7 deletions src/features/validator/hash.ts
Original file line number Diff line number Diff line change
@@ -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 = "";
Expand All @@ -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}`;
Expand All @@ -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
)}`;
Expand Down
4 changes: 2 additions & 2 deletions src/features/validator/signature.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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;
Expand Down
15 changes: 6 additions & 9 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -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"))
Expand Down Expand Up @@ -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"
)
);

0 comments on commit 2a0c941

Please sign in to comment.