This repository has been archived by the owner on Jul 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8211ace
commit e57b1e6
Showing
3 changed files
with
53 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { describe, it, expect } from "vitest"; | ||
import { buildTxnHash } from "./transaction"; | ||
|
||
describe("buildTxnHash() function", () => { | ||
|
||
it("doesn't begin with 0x", () => { | ||
const hash = "1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef"; | ||
|
||
expect(() => buildTxnHash(hash)).toThrow(); | ||
}); | ||
|
||
it("too few digits", () => { | ||
const hash = "0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcde"; | ||
|
||
expect(() => buildTxnHash(hash)).toThrow(); | ||
}); | ||
|
||
it("too many digits", () => { | ||
const hash = "0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef1"; | ||
|
||
expect(() => buildTxnHash(hash)).toThrow(); | ||
}); | ||
|
||
it("valid hash", () => { | ||
const hash = "0x1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF"; | ||
|
||
const result = buildTxnHash(hash); | ||
expect(result.hash).toStrictEqual(hash.toLowerCase()); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
export interface TxnHash { | ||
hash: `0x${string}`; | ||
} | ||
|
||
const regex = /^0x[0-9a-fA-F]{64}$/; | ||
|
||
const isTxnHash = (maybeTxnHash: string): boolean => { | ||
return regex.test(maybeTxnHash); | ||
} | ||
|
||
export const buildTxnHash = (maybeTxnHash: string): TxnHash => { | ||
|
||
if (!isTxnHash(maybeTxnHash)) | ||
throw new Error(`Invalid transaction hash: ${maybeTxnHash}`); | ||
|
||
const normalizedHash = maybeTxnHash.toLowerCase() as `0x${string}`; | ||
|
||
return { | ||
hash: normalizedHash | ||
}; | ||
} |