Skip to content

Commit 182d978

Browse files
regnarockgithub-actions[bot]h-adamik
authored
[ADV-152] BTC implementation (#39)
* feat: first iteration on data model & api * fix(chains.ts): added btc chain early informations * feat: implement single get address state * feat: add address validation & get balance * feat: add route /addresses/state * fix: try to fix build by specifying openapi * fix:try string instead of integer * fix:turn `pipe(coalesce)` into `transform` * Update OpenAPI JSON docs * fix: make multi address state endpoint available * Update OpenAPI JSON docs * fix: make address vs origin more explicit in doc * Update OpenAPI JSON docs * Update src/app/model/types_WIP.ts Co-authored-by: Hakim <162046728+hakim-adamik@users.noreply.github.com> * fix: transaction schema naming and missing fields * fix: chains * fix(model/service.ts): remove useless method * fix: replace state origin by account_id * fix: renamed account_id to accountId * Update OpenAPI JSON docs * fix: MultiRecipientTransaction model definition * fix:add support for single sender in prevalidation * feat: fill chain parameters * fix: non working transactionMethodAdaptator * Update OpenAPI JSON docs * fix: code style & error handling * Implementation of GET addresses and xpub states (#54) * fully typed xpub parsing supported * feat: Add unconfirmed balance * feat:Adds prevalidation and tx broadcast * fix: zod<->openapi generation issues * fix: remove useless type guard of multiRecipientTx * fix: Code style & error handling * fix: confirmation number for BTC * [BTC] Tx crafting (#65) * feat: Adds simple TX crafting * feat: adds support for useMaxAmount Additionally: - improved coinselect types and documentation - moved validation from zod to BTC service prevalidate as handling of simple transactions is widely different in BTC - fixed a potential race condition in transaction encoding * fix: patch package.lock version * [BTC] Merge TX_TO_MANY feature to normal TX_NATIVE (#66) * [adv-279] fix broadcast error on nano (#64) * fix: attempt signature for legacy * feat: merge TX_TO_MANY feature to normal TX_NATIVE - LOTS of code style cleans - remove all wrappers - use the new tx schema - replace usage of `plain` with `data` - moved transaction mod per chainId preValidation to zod - removed `PlainTransaction` usage in favor of `Transaction['data']` - introduce `CompleteTransaction` to differentiate before and after calling the method `completeTransaction` in the Family Service new signatures: - `completeTransaction(Transaction) => CompleteTransaction` - `validateTransaction(CompleteTransaction)` - `encodeTransactionToSign(completeTransaction)` - `encodeTransactionToBroadcast(completeTransaction)` - change on /addresses/state: - aggregated all balances together - use model `AccountState` instead of `AddressState` - use the fiel `accountId` instead of `address` or `origin` - removed MultiBalancesService --------- Co-authored-by: hakim-adamik <hakim@adamik.io> * revert: plans for pubkey to address * Update pnpm-lock.yaml * Minor review fixes * Last batch of review fixes * Update OpenAPI JSON docs --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: Hakim <162046728+hakim-adamik@users.noreply.github.com> Co-authored-by: hakim-adamik <hakim@adamik.io>
1 parent fbd5544 commit 182d978

67 files changed

Lines changed: 4090 additions & 1587 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

jest.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const createJestConfig = nextJest({
1010
const config: Config = {
1111
coverageProvider: "babel",
1212
testEnvironment: "node",
13+
workerThreads: true,
1314
};
1415

1516
// createJestConfig is exported this way to ensure that next/jest can load the Next.js config which is async

package.json

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,18 @@
2626
"algosdk": "^2.7.0",
2727
"bech32": "^2.0.0",
2828
"bignumber.js": "^9.1.2",
29+
"bitcoinjs-lib": "^6.1.6",
30+
"coinselect": "^3.1.13",
2931
"cosmjs-types": "^0.9.0",
3032
"hi-base32": "^0.5.1",
3133
"js-sha512": "^0.8.0",
3234
"next": "14.0.3",
3335
"react": "^18.2.0",
36+
"tiny-secp256k1": "^2.2.3",
3437
"tsx": "^4.16.5",
3538
"tweetnacl": "^1.0.3",
3639
"viem": "^2.14.0",
37-
"zod": "^3.22.4",
40+
"zod": "^3.23.8",
3841
"zod-openapi": "^2.14.0",
3942
"zod-validation-error": "^3.3.0"
4043
},
@@ -50,5 +53,10 @@
5053
"ts-node": "^10.4.0",
5154
"tsconfig-paths": "^4.2.0",
5255
"typescript": "^5"
56+
},
57+
"pnpm": {
58+
"patchedDependencies": {
59+
"coinselect@3.1.13": "patches/coinselect@3.1.13.patch"
60+
}
5361
}
54-
}
62+
}

patches/coinselect@3.1.13.patch

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
diff --git a/index.d.ts b/index.d.ts
2+
new file mode 100644
3+
index 0000000..20cc98a
4+
--- /dev/null
5+
+++ b/index.d.ts
6+
@@ -0,0 +1,48 @@
7+
+// Declare the main coinselect module
8+
+declare module "coinselect" {
9+
+ export interface UTXO {
10+
+ txid: string | Buffer;
11+
+ vout: number;
12+
+ value: number;
13+
+ nonWitnessUtxo?: Buffer;
14+
+ witnessUtxo?: {
15+
+ script: Buffer;
16+
+ value: number;
17+
+ };
18+
+ }
19+
+
20+
+ export type Target =
21+
+ | {
22+
+ address: string;
23+
+ value: number;
24+
+ }
25+
+ | {
26+
+ address: string;
27+
+ }
28+
+ | {
29+
+ value: number;
30+
+ };
31+
+
32+
+ export interface SelectedUTXO {
33+
+ inputs?: UTXO[];
34+
+ outputs?: Target[];
35+
+ fee: number;
36+
+ }
37+
+
38+
+ export default function coinSelect(
39+
+ utxos: UTXO[],
40+
+ outputs: Target[],
41+
+ feeRate: number
42+
+ ): SelectedUTXO;
43+
+}
44+
+
45+
+// Declare the coinselect/split module
46+
+declare module "coinselect/split.js" {
47+
+ import { UTXO, Target, SelectedUTXO } from "coinselect";
48+
+
49+
+ export default function split(
50+
+ utxos: UTXO[],
51+
+ outputs: Target[],
52+
+ feeRate: number
53+
+ ): SelectedUTXO;
54+
+}
55+
diff --git a/package.json b/package.json
56+
index 6cdef00..337bf4f 100644
57+
--- a/package.json
58+
+++ b/package.json
59+
@@ -31,6 +31,7 @@
60+
"utils.js"
61+
],
62+
"main": "index.js",
63+
+ "types": "index.d.ts",
64+
"repository": {
65+
"type": "git",
66+
"url": "https://github.com/bitcoinjs/coinselect.git"

patches/how_to.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Why this patch
2+
3+
The library `coinselect.js` is manipulating important and low level objects, it seems important to make sure we don't just send any information to it.
4+
Because the lib is only published in NPM without its types ([because the maintainer doesnt trust NPM](https://github.com/bitcoinjs/coinselect/pull/77#issuecomment-1676430774)), I've patched the lib using their own type definition [their own definitions](https://github.com/bitcoinjs/coinselect/blob/master/index.d.ts), modified to actually reflect the lib's bahaviour.
5+
6+
7+
# How to maintain the patch
8+
9+
Create a temporary copy of the library to patch
10+
```bash
11+
pnpm patch
12+
```
13+
14+
You now have access to a similar folder as: `/private/var/folders/yn/k6gkp3pd7fq4dn71mw1yb8qh0000gn/T/5191b8b733d6bbfd169476c53b6a123b` that I'll refer to as `temp_lib`
15+
16+
Open it on vscode
17+
```bash
18+
code "/temp_lib"
19+
```
20+
21+
Do all the changes that you need.
22+
And then run
23+
```
24+
diff -Naur node_modules/.pnpm/coinselect@3.1.13/node_modules/coinselect/ /temp_lib
25+
```
26+
27+
And copy the changes to `patches/coinselect@3.1.13.patch`, just taking the lines with `+/-` preceeded by the ones with `@@`.
28+
29+
You can now your patch with
30+
```bash
31+
pnpm install --force
32+
```
33+
34+
If all is good, `patches/coinselect@3.1.13.patch` can be commited as any other file.
35+
36+
# Why use `diff` manually and not use `pmpm patch-commit temp_lib` ?
37+
38+
For some reason, it seems pnpm 8 doesn't handle well newly created files (and not just modified ones). This should be better in [pnpm 9](https://github.com/pnpm/pnpm/issues/5686#issuecomment-2272406668)
39+
40+

0 commit comments

Comments
 (0)