@atomicfinance/bitcoin-ddk-provider 4.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export { default } from './BitcoinDdkProvider';
2
+ export * from './BitcoinDdkProvider';
@@ -0,0 +1,118 @@
1
+ import { Messages, PayoutRequest } from '@atomicfinance/types';
2
+ import {
3
+ DlcAccept,
4
+ DlcClose,
5
+ DlcOffer,
6
+ DlcSign,
7
+ DlcTransactions,
8
+ MessageType,
9
+ } from '@node-dlc/messaging';
10
+ import randomBytes from 'randombytes';
11
+
12
+ export const asyncForEach = async (
13
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
14
+ array: any,
15
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
16
+ callback: any,
17
+ ): Promise<void> => {
18
+ for (let index = 0; index < array.length; index++) {
19
+ await callback(array[index], index, array);
20
+ }
21
+ };
22
+
23
+ export function generateSerialId(): bigint {
24
+ return randomBytes(4).reduce((acc, num, i) => acc + num ** i, 0);
25
+ }
26
+
27
+ export function generateSerialIds(count: number): bigint[] {
28
+ return Array.from({ length: count }, () => generateSerialId());
29
+ }
30
+
31
+ export function checkTypes(types: ICheckTypesRequest): ICheckTypesResponse {
32
+ const { _dlcOffer, _dlcAccept, _dlcSign, _dlcClose, _dlcTxs } = types;
33
+ if (_dlcOffer && _dlcOffer.type !== MessageType.DlcOffer)
34
+ throw Error('DlcOffer must be V0');
35
+ if (_dlcAccept && _dlcAccept.type !== MessageType.DlcAccept)
36
+ throw Error('DlcAccept must be V0');
37
+ if (_dlcSign && _dlcSign.type !== MessageType.DlcSign)
38
+ throw Error('DlcSign must be V0');
39
+ if (_dlcClose && _dlcClose.type !== MessageType.DlcClose)
40
+ throw Error('DlcClose must be V0');
41
+ if (_dlcTxs && _dlcTxs.type !== MessageType.DlcTransactionsV0)
42
+ throw Error('DlcTransactions must be V0');
43
+
44
+ let dlcOffer: DlcOffer;
45
+ let dlcAccept: DlcAccept;
46
+ let dlcSign: DlcSign;
47
+ let dlcClose: DlcClose;
48
+ let dlcTxs: DlcTransactions;
49
+
50
+ if (_dlcOffer) dlcOffer = _dlcOffer as DlcOffer;
51
+ if (_dlcAccept) dlcAccept = _dlcAccept as DlcAccept;
52
+ if (_dlcSign) dlcSign = _dlcSign as DlcSign;
53
+ if (_dlcClose) dlcClose = _dlcClose as DlcClose;
54
+ if (_dlcTxs) dlcTxs = _dlcTxs as DlcTransactions;
55
+
56
+ return { dlcOffer, dlcAccept, dlcSign, dlcClose, dlcTxs };
57
+ }
58
+
59
+ export function outputsToPayouts(
60
+ outputs: PayoutGroup[],
61
+ rValuesMessagesList: Messages[],
62
+ localCollateral: bigint,
63
+ remoteCollateral: bigint,
64
+ payoutLocal: boolean,
65
+ ): OutputsToPayoutsResponse {
66
+ const payouts: PayoutRequest[] = [];
67
+ const messagesList: Messages[] = [];
68
+
69
+ outputs.forEach((output: PayoutGroup) => {
70
+ const { payout, groups } = output;
71
+ const payoutAmount: bigint = payout;
72
+
73
+ groups.forEach((group: number[]) => {
74
+ const messages = [];
75
+ for (let i = 0; i < group.length; i++) {
76
+ const digit: number = group[i];
77
+ messages.push(rValuesMessagesList[i].messages[digit]);
78
+ }
79
+
80
+ const local = payoutLocal
81
+ ? payoutAmount
82
+ : localCollateral + remoteCollateral - payoutAmount;
83
+ const remote = payoutLocal
84
+ ? localCollateral + remoteCollateral - payoutAmount
85
+ : payoutAmount;
86
+ payouts.push({ local, remote });
87
+ messagesList.push({ messages });
88
+ });
89
+ });
90
+
91
+ return { payouts, messagesList };
92
+ }
93
+
94
+ export interface ICheckTypesRequest {
95
+ _dlcOffer?: DlcOffer;
96
+ _dlcAccept?: DlcAccept;
97
+ _dlcSign?: DlcSign;
98
+ _dlcClose?: DlcClose;
99
+ _dlcTxs?: DlcTransactions;
100
+ }
101
+
102
+ export interface ICheckTypesResponse {
103
+ dlcOffer?: DlcOffer;
104
+ dlcAccept?: DlcAccept;
105
+ dlcSign?: DlcSign;
106
+ dlcClose?: DlcClose;
107
+ dlcTxs?: DlcTransactions;
108
+ }
109
+
110
+ interface PayoutGroup {
111
+ payout: bigint;
112
+ groups: number[][];
113
+ }
114
+
115
+ interface OutputsToPayoutsResponse {
116
+ payouts: PayoutRequest[];
117
+ messagesList: Messages[];
118
+ }
package/package.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "name": "@atomicfinance/bitcoin-ddk-provider",
3
+ "umdName": "BitcoinDdkProvider",
4
+ "version": "4.1.0",
5
+ "description": "Bitcoin Abstraction Layer Ddk Provider",
6
+ "author": "Atomic Finance <info@atomic.finance>",
7
+ "homepage": "",
8
+ "license": "ISC",
9
+ "main": "dist/index.js",
10
+ "dependencies": {
11
+ "@atomicfinance/bitcoin-utils": "4.1.0",
12
+ "@atomicfinance/provider": "^4.1.0",
13
+ "@atomicfinance/types": "^4.1.0",
14
+ "@atomicfinance/utils": "^4.1.0",
15
+ "bignumber.js": "^9.0.1",
16
+ "bip66": "^1.1.5",
17
+ "bitcoin-networks": "^1.0.0",
18
+ "bitcoinjs-lib": "6.1.7",
19
+ "ecpair": "^2.1.0",
20
+ "tiny-secp256k1": "^2.2.0"
21
+ },
22
+ "devDependencies": {
23
+ "@types/lodash": "^4.14.160",
24
+ "@types/node": "20.19.10"
25
+ },
26
+ "publishConfig": {
27
+ "access": "public"
28
+ },
29
+ "scripts": {
30
+ "build": "../../node_modules/.bin/tsc --project tsconfig.json",
31
+ "test": "pnpm run build",
32
+ "lint": "../../node_modules/.bin/eslint .",
33
+ "lint:fix": "../../node_modules/.bin/eslint --fix ."
34
+ }
35
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,7 @@
1
+ {
2
+ "extends": "../../tsconfig.json",
3
+ "compilerOptions": {
4
+ "outDir": "./dist",
5
+ },
6
+ "include": ["./lib"],
7
+ }