Skip to content

Commit

Permalink
fix: serialized block header
Browse files Browse the repository at this point in the history
  • Loading branch information
Th0rgal committed Oct 23, 2024
1 parent 768ef11 commit 7c36fed
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/UtuProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,19 @@ export class UtuProvider {
.join("");
};

// New helper function to serialize hash
const serializedHash = (hash: string): string[] => {
return hash
.match(/.{8}/g)!
.map((chunk) => "0x" + chunk.match(/.{2}/g)!.reverse().join(""))
.reverse();
};

// Serialize each field
const serialized = [
"0x" + toLittleEndianHex(blockHeader.version),
"0x" + blockHeader.previousblockhash,
"0x" + blockHeader.merkleroot,
...serializedHash(blockHeader.previousblockhash as string),
...serializedHash(blockHeader.merkleroot),
"0x" + toLittleEndianHex(blockHeader.time),
"0x" + blockHeader.bits,
"0x" + toLittleEndianHex(blockHeader.nonce),
Expand Down
33 changes: 33 additions & 0 deletions src/tests/UtuProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,37 @@ describe("UtuProvider", () => {
"f6d90508da8aa581f7203f4899498c775ed4878544adcdef5e7b53a4ab691dd7",
]);
});

it("should get register blocks tx for a given block hash", async () => {
const blockHash =
"00000000d1145790a8694403d4063f323d499e655c83426834d4ce2f8dd4a2ee";
const registerBlocksTx = await utuProvider.getRegisterBlocksTx([blockHash]);

expect(registerBlocksTx).toBeDefined();
expect(registerBlocksTx.contractAddress).toBe("0x..."); // Replace with actual contract address
expect(registerBlocksTx.selector).toBe("0x..."); // Replace with actual selector
expect(registerBlocksTx.calldata).toEqual([
"0x1",
"0x01000000",
"0x55bd840a",
"0x78798ad0",
"0xda853f68",
"0x974f3d18",
"0x3e2bd1db",
"0x6a842c1f",
"0xeecf222a",
"0x00000000",
"0xff104ccb",
"0x05421ab9",
"0x3e63f8c3",
"0xce5c2c2e",
"0x9dbb37de",
"0x2764b3a3",
"0x175c8166",
"0x562cac7d",
"0x51b96a49",
"0x1d00ffff",
"0x283e9e70",
]);
});
});

0 comments on commit 7c36fed

Please sign in to comment.