-
Notifications
You must be signed in to change notification settings - Fork 104
shannon entropy calculation and script type matching #335
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import { Network } from "@caravan/bitcoin"; | ||
| import { amountFingerprints, scriptTypeFingerprints } from "./fingerprint"; | ||
|
|
||
| describe("Fingerprint", () => { | ||
| it("can match by hrp", () => { | ||
| const result = scriptTypeFingerprints( | ||
| "P2WSH", | ||
| [ | ||
| "bc1qzcwk6mnfmdt6rx0qp0dwfll4gj8540yamdka7r", | ||
| "3EutoNctCnVBpVz8xcAvHBSNxSbmgWxZmV", | ||
| ], | ||
| Network.MAINNET, | ||
| ); | ||
| expect(result).toEqual([true, false]); | ||
| }); | ||
|
|
||
| it("can measure amount entropy", () => { | ||
| const result = amountFingerprints(["40.8199902", "1.18000000"]); | ||
| expect(result).toEqual([true, false]); | ||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import { getAddressType, Network } from "@caravan/bitcoin"; | ||
| import { ScriptType } from "@caravan/fees"; | ||
|
|
||
| export function scriptTypeFingerprints( | ||
| inputScriptType: ScriptType, | ||
| outputAddresses: string[], | ||
| network: Network, | ||
| ): boolean[] { | ||
| return outputAddresses.map((address) => { | ||
| const addressType = getAddressType(address, network); | ||
| return addressType === inputScriptType; | ||
| }); | ||
| } | ||
|
|
||
| function log2(x: number): number { | ||
| return Math.log(x) / Math.LN2; | ||
| } | ||
|
|
||
| function shannonEntropy(str: string): number { | ||
| const counts: Record<string, number> = {}; | ||
| for (const char of str) { | ||
| counts[char] = (counts[char] || 0) + 1; | ||
| } | ||
| const total = str.length; | ||
| return Object.values(counts).reduce((entropy, count) => { | ||
| const probability = count / total; | ||
| return entropy - probability * log2(probability); | ||
| }, 0); | ||
| } | ||
|
|
||
| export function amountFingerprints(amounts: string[]): boolean[] { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. would you mind adding docstrings to this to explain what's being solved here and in particular to describe how to interpret the output. |
||
| if (amounts.length === 0) return []; | ||
|
|
||
| // Right pad to 8 decimals | ||
| const paddedRight = amounts.map((a) => { | ||
| const [whole, frac = ""] = a.split("."); | ||
| return whole + "." + frac.padEnd(8, "0"); | ||
| }); | ||
|
|
||
| // Remove dot for uniformity, then left pad to max length | ||
| const noDot = paddedRight.map((a) => a.replace(".", "")); | ||
| const maxLen = Math.max(...noDot.map((a) => a.length)); | ||
| const paddedAmounts = noDot.map((a) => a.padStart(maxLen, "0")); | ||
|
|
||
| // Compute entropy for each padded amount | ||
| const entropies = paddedAmounts.map((a) => shannonEntropy(a)); | ||
| const maxEntropy = Math.max(...entropies); | ||
|
|
||
| // Mark true if this output has the maximum entropy | ||
| return entropies.map((e) => e === maxEntropy); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you have a "perfect spend" transaction that has 1 output, then this will always return |
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is the idea that there is always one (and only one) value that will be true?