Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions packages/caravan-health/src/fingerprint.test.ts
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]);

Copy link
Copy Markdown
Contributor

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?

});
});
51 changes: 51 additions & 0 deletions packages/caravan-health/src/fingerprint.ts
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[] {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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 [ true ]. Is this desirable? Maybe yes, maybe no. I can't decide 😅. Maybe if it's length 1, it should be [ false ]?

}
Loading