Skip to content
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

Caravan Health Package #112

Merged
merged 93 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
93 commits
Select commit Hold shift + click to select a range
3ac2703
Initializing Caravan Health and adding privacy score
Harshil-Jani Jul 3, 2024
822cf54
Utilize client methods from @caravan/client
Harshil-Jani Jul 8, 2024
e3989b9
Added fees score method to @caravan/health
Harshil-Jani Jul 8, 2024
7737e3e
Export all privacy methods
Harshil-Jani Jul 8, 2024
7421376
Add some context on the fee score functions
Harshil-Jani Jul 8, 2024
c3f167e
Rename to camelCase and fix wordings
Harshil-Jani Jul 14, 2024
27cacf1
Setup prettier for @caravan/health and format
Harshil-Jani Jul 14, 2024
29c2207
Refactor privacyScoreByTxTopology for improved readability and mainta…
Harshil-Jani Jul 15, 2024
26dfd30
Add types for Transaction and UTXO for privacy.ts
Harshil-Jani Jul 17, 2024
f1e6a38
Changing UTXO type to Wallet UTXO which brings addresses from memory
Harshil-Jani Jul 17, 2024
7f7ed1d
Fix UTXO set length comming wrong
Harshil-Jani Jul 17, 2024
1f70cab
Adding Test Cases for Privacy
Harshil-Jani Jul 18, 2024
afe92ad
Type Improvements and code improvement
Harshil-Jani Jul 21, 2024
0011b21
Better handling of exports and address functionality
Harshil-Jani Jul 21, 2024
270a035
Rewrite test cases for privacy
Harshil-Jani Jul 22, 2024
fc589e2
Fix in privacy and it's test case
Harshil-Jani Jul 22, 2024
afb69a4
Completed with feescore test cases
Harshil-Jani Jul 22, 2024
a4c6ed7
Improving Documentation
Harshil-Jani Jul 22, 2024
f4959d7
Type Improvements
Harshil-Jani Jul 22, 2024
dd77fa3
Added Transaction[] as return type in clients fxn
Harshil-Jani Jul 25, 2024
ffd741c
Add dependency on @caravan-bitcoin
Harshil-Jani Jul 28, 2024
49b6886
Update logic for address reuse
Harshil-Jani Jul 28, 2024
c39eb85
Moving Transaction type into clients
Harshil-Jani Jul 28, 2024
f5e6567
Performance improvement for feeRate percentile history
Harshil-Jani Jul 28, 2024
f71c17a
Update fees test cases
Harshil-Jani Jul 29, 2024
cdb7bfd
Decouple feeScore from the client
Harshil-Jani Jul 29, 2024
e647deb
Move tx topology as a separate function
Harshil-Jani Jul 29, 2024
87c7425
Prettier Formatting
Harshil-Jani Jul 29, 2024
a493057
Added the changeset
Harshil-Jani Jul 29, 2024
d33a99d
Adding README
Harshil-Jani Jul 29, 2024
68e072e
Add isSend logic for mempool and blockstream using prevouts address c…
Harshil-Jani Jul 29, 2024
bc977e3
Add waste metric to the feescore file
Harshil-Jani Jul 29, 2024
2f2b58f
Add test case for waste metric
Harshil-Jani Jul 29, 2024
64d8a18
remove @caravan/health from @caravan/clients
Harshil-Jani Jul 29, 2024
6d7f289
Move interface to types.ts
Harshil-Jani Jul 30, 2024
76509c9
rolling release version of @caravan-health as 1.0.0-beta
Harshil-Jani Jul 30, 2024
69eb402
improving grammar and variable naming
Harshil-Jani Aug 6, 2024
b72beea
More descriptive changeset
Harshil-Jani Aug 7, 2024
021f461
Adding call to bitcoind in bitcoind.js file
Harshil-Jani Aug 7, 2024
b1a2828
Descriptive Test cases added
Harshil-Jani Aug 7, 2024
12b78bd
Replacing Weightage with UTXO Mass Factor
Harshil-Jani Aug 8, 2024
99ca020
L to estimateLongTermFeeRate
Harshil-Jani Aug 8, 2024
0a2f9ff
Improve docs
Harshil-Jani Aug 9, 2024
626714b
remove precision from tests
Harshil-Jani Aug 9, 2024
e3a94bb
syntax improvement
Harshil-Jani Aug 9, 2024
2b185fd
Introducing new class WasteMetric
Harshil-Jani Aug 11, 2024
8892097
Remove deps from network client
Harshil-Jani Aug 11, 2024
ccad6e7
Adding PrivacyMetric class
Harshil-Jani Aug 12, 2024
41bcab3
Descriptive Test cases and Object Oriented Restructure
Harshil-Jani Aug 12, 2024
22b863b
Introduce Address Usage Map
Harshil-Jani Aug 12, 2024
a4eafb7
solve blocktime and block_time naming inconsistency
Harshil-Jani Aug 12, 2024
97a8093
Cache addressUsageMap in the class
Harshil-Jani Aug 13, 2024
ac28f30
raise exceptions for other clients if not mempool for feerate history
Harshil-Jani Aug 13, 2024
1785c8f
Adding test cases for clients methods
Harshil-Jani Aug 14, 2024
e66b321
Clean test cases
Harshil-Jani Aug 15, 2024
7329b13
AddressUsageMap test case
Harshil-Jani Aug 15, 2024
e35af1d
Improved Documentation
Harshil-Jani Aug 15, 2024
bd7bf1e
Fixing package-lock.json
Harshil-Jani Aug 15, 2024
cf835e6
Storing transactions, utxos in a class
Harshil-Jani Aug 15, 2024
5a5ab61
Separate out utility functions
Harshil-Jani Aug 15, 2024
2ee031d
Updating documentation
Harshil-Jani Aug 15, 2024
9781cc2
Adding the new UTXO dust method and updating address lookup Map
Harshil-Jani Aug 18, 2024
74d656d
Make transactions and UTXO options for ad-hoc cases
Harshil-Jani Aug 18, 2024
0d0ec9f
Consider Script Type for dust limits
Harshil-Jani Aug 23, 2024
a223d80
Fix failing tests
Harshil-Jani Aug 23, 2024
1e4acb1
Update packages/caravan-health/src/waste.ts
Harshil-Jani Aug 23, 2024
af16010
Update packages/caravan-health/src/waste.ts
Harshil-Jani Aug 23, 2024
303da1f
Fix CI errors
Harshil-Jani Aug 23, 2024
4e60eb9
Update packages/caravan-health/src/waste.ts
Harshil-Jani Aug 26, 2024
15f8475
Update packages/caravan-health/README.md
Harshil-Jani Aug 26, 2024
1235246
Update packages/caravan-health/README.md
Harshil-Jani Aug 26, 2024
6d7093b
Update packages/caravan-health/src/privacy.ts
Harshil-Jani Aug 26, 2024
bba82f7
Adding script type into calculateDustMetric
Harshil-Jani Aug 27, 2024
b4bf711
Updating test cases
Harshil-Jani Aug 27, 2024
5f98fef
Code cleaning
Harshil-Jani Aug 27, 2024
9b68338
Update packages/caravan-health/src/privacy.test.ts
Harshil-Jani Aug 28, 2024
a3032b1
Update packages/caravan-health/src/privacy.test.ts
Harshil-Jani Aug 28, 2024
17de9ee
Update packages/caravan-health/src/privacy.test.ts
Harshil-Jani Aug 28, 2024
8f8eff0
Update packages/caravan-health/src/privacy.test.ts
Harshil-Jani Aug 28, 2024
f327999
Update packages/caravan-health/src/privacy.test.ts
Harshil-Jani Aug 28, 2024
ac249a9
Update packages/caravan-health/src/waste.ts
Harshil-Jani Aug 28, 2024
10d68d7
SpendTypeScore just takes in Input and Output
Harshil-Jani Aug 28, 2024
bb06e48
Adding nit for length of UTXO
Harshil-Jani Aug 28, 2024
d47823b
Remove unused map
Harshil-Jani Aug 28, 2024
564a8ed
Dust calculations update
Harshil-Jani Aug 28, 2024
2e22bf3
Update config variable names
Harshil-Jani Aug 28, 2024
3a2dee1
Removed unncessary comments
Harshil-Jani Aug 28, 2024
afa3386
Update logic for spend type determination
Harshil-Jani Aug 29, 2024
7fc3f81
Update packages/caravan-health/src/waste.ts
Harshil-Jani Aug 29, 2024
5b7989d
Update packages/caravan-health/src/waste.ts
Harshil-Jani Aug 29, 2024
5eec580
Update packages/caravan-health/src/privacy.ts
Harshil-Jani Aug 29, 2024
eb43fa4
utility.js -> spendType.js
Harshil-Jani Aug 29, 2024
b07b5a4
Merge branch 'main' into caravan/health
bucko13 Aug 30, 2024
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
14 changes: 14 additions & 0 deletions .changeset/eighty-planets-help.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
"@caravan/bitcoin": minor
"@caravan/clients": minor
---

@caravan/client
We are exposing a new method `getAddressTransactions` which will fetch all the transaction for a given address and format it as per needs. To facilitate the change, we had moved the interfaces in the new file `types.ts`.

Another change was about getting the block fee-rate percentile history from mempool as a client.

@caravan/bitcoin
The new function that has the capability to detect the address type (i.e P2SH, P2PKH, P2WSH or P2TR) was added.

Overall, The changes were to support the new library within caravan called @caravan/health.
20 changes: 20 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 52 additions & 1 deletion packages/caravan-bitcoin/src/addresses.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { validateAddress } from "./addresses";
import { validateAddress, getAddressType } from "./addresses";
import * as multisig from "./multisig";
import { Network } from "./networks";

const P2PKH = "P2PKH";
const P2TR = "P2TR";
const P2WSH = "P2WSH";

const ADDRESSES = {};
ADDRESSES[Network.MAINNET] = {};
ADDRESSES[Network.MAINNET][P2PKH] = ["1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH"];
ADDRESSES[Network.MAINNET][(multisig as any).P2SH] = [

Check warning on line 12 in packages/caravan-bitcoin/src/addresses.test.ts

View workflow job for this annotation

GitHub Actions / ci (20.x)

Unexpected any. Specify a different type
"3LRW7jeCvQCRdPF8S3yUCfRAx4eqXFmdcr",
];
ADDRESSES[Network.MAINNET][(multisig as any).P2WSH] = [

Check warning on line 15 in packages/caravan-bitcoin/src/addresses.test.ts

View workflow job for this annotation

GitHub Actions / ci (20.x)

Unexpected any. Specify a different type
"bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4",
"bc1qng72v5ceptk07htel0wcv6k27fkg6tmmd8887jr2l2yz5a5lnawqqeceya",
];
Expand All @@ -21,10 +22,10 @@

ADDRESSES[Network.TESTNET] = {};
ADDRESSES[Network.TESTNET][P2PKH] = ["mrCDrCybB6J1vRfbwM5hemdJz73FwDBC8r"];
ADDRESSES[Network.TESTNET][(multisig as any).P2SH] = [

Check warning on line 25 in packages/caravan-bitcoin/src/addresses.test.ts

View workflow job for this annotation

GitHub Actions / ci (20.x)

Unexpected any. Specify a different type
"2NByiBUaEXrhmqAsg7BbLpcQSAQs1EDwt5w",
];
ADDRESSES[Network.TESTNET][(multisig as any).P2WSH] = [

Check warning on line 28 in packages/caravan-bitcoin/src/addresses.test.ts

View workflow job for this annotation

GitHub Actions / ci (20.x)

Unexpected any. Specify a different type
"tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3q0sl5k7",
];
ADDRESSES[Network.TESTNET][P2TR] = [
Expand All @@ -32,10 +33,10 @@
];
ADDRESSES[Network.REGTEST] = {};
ADDRESSES[Network.REGTEST][P2PKH] = ["msX7BWX7xnyqGMxHRCqWoogcvQ93PQtWWR"];
ADDRESSES[Network.REGTEST][(multisig as any).P2SH] = [

Check warning on line 36 in packages/caravan-bitcoin/src/addresses.test.ts

View workflow job for this annotation

GitHub Actions / ci (20.x)

Unexpected any. Specify a different type
"2NByiBUaEXrhmqAsg7BbLpcQSAQs1EDwt5w",
];
ADDRESSES[Network.REGTEST][(multisig as any).P2WSH] = [

Check warning on line 39 in packages/caravan-bitcoin/src/addresses.test.ts

View workflow job for this annotation

GitHub Actions / ci (20.x)

Unexpected any. Specify a different type
"bcrt1q5qlrrl9lq2mktevfdlxl6upxqluadr74x3znhc",
];
ADDRESSES[Network.REGTEST][P2TR] = [
Expand All @@ -44,8 +45,8 @@

const ADDRESS_TYPES = [
P2PKH,
(multisig as any).P2SH,

Check warning on line 48 in packages/caravan-bitcoin/src/addresses.test.ts

View workflow job for this annotation

GitHub Actions / ci (20.x)

Unexpected any. Specify a different type
(multisig as any).P2WSH,

Check warning on line 49 in packages/caravan-bitcoin/src/addresses.test.ts

View workflow job for this annotation

GitHub Actions / ci (20.x)

Unexpected any. Specify a different type
P2TR,
];

Expand Down Expand Up @@ -104,4 +105,54 @@
});
});
});

describe("getAddressType", () => {
it("correctly identifies P2SH addresses", () => {
ADDRESSES[Network.MAINNET][P2PKH].forEach((address) => {
expect(getAddressType(address, Network.MAINNET)).toBe(P2PKH);
});
ADDRESSES[Network.TESTNET][P2PKH].forEach((address) => {
expect(getAddressType(address, Network.TESTNET)).toBe("P2PKH");
});
ADDRESSES[Network.REGTEST][P2PKH].forEach((address) => {
expect(getAddressType(address, Network.REGTEST)).toBe(P2PKH);
});
});

it("correctly identifies P2WSH addresses", () => {
ADDRESSES[Network.MAINNET][(multisig as any).P2WSH].forEach((address) => {

Check warning on line 123 in packages/caravan-bitcoin/src/addresses.test.ts

View workflow job for this annotation

GitHub Actions / ci (20.x)

Unexpected any. Specify a different type
expect(getAddressType(address, Network.MAINNET)).toBe(P2WSH);
});
ADDRESSES[Network.TESTNET][(multisig as any).P2WSH].forEach((address) => {

Check warning on line 126 in packages/caravan-bitcoin/src/addresses.test.ts

View workflow job for this annotation

GitHub Actions / ci (20.x)

Unexpected any. Specify a different type
expect(getAddressType(address, Network.TESTNET)).toBe(P2WSH);
});
ADDRESSES[Network.REGTEST][(multisig as any).P2WSH].forEach((address) => {
expect(getAddressType(address, Network.REGTEST)).toBe(P2WSH);
});
});

it("correctly identifies P2TR addresses", () => {
ADDRESSES[Network.MAINNET][P2TR].forEach((address) => {
expect(getAddressType(address, Network.MAINNET)).toBe(P2TR);
});
ADDRESSES[Network.TESTNET][P2TR].forEach((address) => {
expect(getAddressType(address, Network.TESTNET)).toBe(P2TR);
});
ADDRESSES[Network.REGTEST][P2TR].forEach((address) => {
expect(getAddressType(address, Network.REGTEST)).toBe(P2TR);
});
});

it("returns UNKNOWN for unrecognized addresses", () => {
expect(getAddressType("unknownaddress1", Network.MAINNET)).toBe(
"UNKNOWN",
);
expect(getAddressType("unknownaddress2", Network.TESTNET)).toBe(
"UNKNOWN",
);
expect(getAddressType("unknownaddress3", Network.REGTEST)).toBe(
"UNKNOWN",
);
});
});
});
29 changes: 29 additions & 0 deletions packages/caravan-bitcoin/src/addresses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from "bitcoin-address-validation";

import { Network } from "./networks";
import { MultisigAddressType } from "./types";

const MAINNET_ADDRESS_MAGIC_BYTE_PATTERN = "^(bc1|[13])";
const TESTNET_ADDRESS_MAGIC_BYTE_PATTERN = "^(tb1|bcrt1|[mn2])";
Expand Down Expand Up @@ -60,3 +61,31 @@ export function validateAddress(address: string, network: Network) {

return valid ? "" : "Address is invalid.";
}

export function getAddressType(
address: string,
network: Network,
): MultisigAddressType {
if (validateAddress(address, network) !== "") {
return "UNKNOWN";
}
const bech32Regex = /^(bc1|tb1|bcrt1)/;
const p2pkhRegex = /^(1|m|n)/;
const p2shRegex = /^(3|2)/;

if (address.match(bech32Regex)) {
if (
address.startsWith("bc1p") ||
address.startsWith("tb1p") ||
address.startsWith("bcrt1p")
) {
return "P2TR";
}
return "P2WSH";
} else if (address.match(p2pkhRegex)) {
return "P2PKH";
} else if (address.match(p2shRegex)) {
return "P2SH";
}
return "UNKNOWN";
}
2 changes: 1 addition & 1 deletion packages/caravan-bitcoin/src/types/addresses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
// address type.
// We should be able to replace this with use of the MULTISIG_ADDRESS_TYPES
// enum when that file (./multisig.js) gets converted to typescript
export type MultisigAddressType = "P2SH" | "P2WSH" | "P2SH-P2WSH" | "P2TR";
export type MultisigAddressType = "P2SH" | "P2WSH" | "P2SH-P2WSH" | "P2TR" | "P2PKH" | "UNKNOWN";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why was "UNKNOWN" necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can avoid UNKNOWN and instead show the error as it is if it occurs. It was just to keep the code consistant by adding a new default type in case of address type matching failure.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think it should just fail if it doesn't match.

14 changes: 14 additions & 0 deletions packages/caravan-clients/src/bitcoind.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,17 @@ export function bitcoindImportMulti({ url, auth, addresses, label, rescan }) {
}
return callBitcoind(...params);
}

export async function bitcoindRawTxData(txid){
try{
return await callBitcoind(
this.bitcoindParams.url,
this.bitcoindParams.auth,
"decoderawtransaction",
[txid],
);
}
catch(e){
return e;
}
}
Loading
Loading