@chainflip/bitcoin 1.2.7 → 2.1.0-beta.1

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/dist/rpc.d.mts ADDED
@@ -0,0 +1,167 @@
1
+ import { z } from "zod";
2
+
3
+ //#region src/rpc.d.ts
4
+ type RpcRequest = {
5
+ getrawtransaction: [txId: string, enhanced?: boolean];
6
+ getblock: [blockHash: string, enhanced?: boolean];
7
+ };
8
+ declare const responseSchemas: {
9
+ readonly getrawtransaction: z.ZodObject<{
10
+ vout: z.ZodTuple<[z.ZodIntersection<z.ZodObject<{
11
+ scriptPubKey: z.ZodObject<{
12
+ type: z.ZodEnum<["witness_v1_taproot", "witness_v0_scripthash", "witness_v0_keyhash", "pubkeyhash", "scripthash"]>;
13
+ address: z.ZodString;
14
+ }, "strip", z.ZodTypeAny, {
15
+ type: "witness_v1_taproot" | "witness_v0_scripthash" | "witness_v0_keyhash" | "pubkeyhash" | "scripthash";
16
+ address: string;
17
+ }, {
18
+ type: "witness_v1_taproot" | "witness_v0_scripthash" | "witness_v0_keyhash" | "pubkeyhash" | "scripthash";
19
+ address: string;
20
+ }>;
21
+ }, "strip", z.ZodTypeAny, {
22
+ scriptPubKey: {
23
+ type: "witness_v1_taproot" | "witness_v0_scripthash" | "witness_v0_keyhash" | "pubkeyhash" | "scripthash";
24
+ address: string;
25
+ };
26
+ }, {
27
+ scriptPubKey: {
28
+ type: "witness_v1_taproot" | "witness_v0_scripthash" | "witness_v0_keyhash" | "pubkeyhash" | "scripthash";
29
+ address: string;
30
+ };
31
+ }>, z.ZodObject<{
32
+ value: z.ZodEffects<z.ZodNumber, bigint, number>;
33
+ n: z.ZodNumber;
34
+ }, "strip", z.ZodTypeAny, {
35
+ value: bigint;
36
+ n: number;
37
+ }, {
38
+ value: number;
39
+ n: number;
40
+ }>>, z.ZodIntersection<z.ZodObject<{
41
+ scriptPubKey: z.ZodObject<{
42
+ type: z.ZodLiteral<"nulldata">;
43
+ hex: z.ZodEffects<z.ZodString, Uint8Array<ArrayBufferLike>, string>;
44
+ }, "strip", z.ZodTypeAny, {
45
+ type: "nulldata";
46
+ hex: Uint8Array<ArrayBufferLike>;
47
+ }, {
48
+ type: "nulldata";
49
+ hex: string;
50
+ }>;
51
+ }, "strip", z.ZodTypeAny, {
52
+ scriptPubKey: {
53
+ type: "nulldata";
54
+ hex: Uint8Array<ArrayBufferLike>;
55
+ };
56
+ }, {
57
+ scriptPubKey: {
58
+ type: "nulldata";
59
+ hex: string;
60
+ };
61
+ }>, z.ZodObject<{
62
+ value: z.ZodEffects<z.ZodNumber, bigint, number>;
63
+ n: z.ZodNumber;
64
+ }, "strip", z.ZodTypeAny, {
65
+ value: bigint;
66
+ n: number;
67
+ }, {
68
+ value: number;
69
+ n: number;
70
+ }>>, z.ZodIntersection<z.ZodObject<{
71
+ scriptPubKey: z.ZodObject<{
72
+ type: z.ZodEnum<["witness_v1_taproot", "witness_v0_scripthash", "witness_v0_keyhash", "pubkeyhash", "scripthash"]>;
73
+ address: z.ZodString;
74
+ }, "strip", z.ZodTypeAny, {
75
+ type: "witness_v1_taproot" | "witness_v0_scripthash" | "witness_v0_keyhash" | "pubkeyhash" | "scripthash";
76
+ address: string;
77
+ }, {
78
+ type: "witness_v1_taproot" | "witness_v0_scripthash" | "witness_v0_keyhash" | "pubkeyhash" | "scripthash";
79
+ address: string;
80
+ }>;
81
+ }, "strip", z.ZodTypeAny, {
82
+ scriptPubKey: {
83
+ type: "witness_v1_taproot" | "witness_v0_scripthash" | "witness_v0_keyhash" | "pubkeyhash" | "scripthash";
84
+ address: string;
85
+ };
86
+ }, {
87
+ scriptPubKey: {
88
+ type: "witness_v1_taproot" | "witness_v0_scripthash" | "witness_v0_keyhash" | "pubkeyhash" | "scripthash";
89
+ address: string;
90
+ };
91
+ }>, z.ZodObject<{
92
+ value: z.ZodEffects<z.ZodNumber, bigint, number>;
93
+ n: z.ZodNumber;
94
+ }, "strip", z.ZodTypeAny, {
95
+ value: bigint;
96
+ n: number;
97
+ }, {
98
+ value: number;
99
+ n: number;
100
+ }>>], null>;
101
+ blockhash: z.ZodOptional<z.ZodNullable<z.ZodString>>;
102
+ }, "strip", z.ZodTypeAny, {
103
+ vout: [{
104
+ scriptPubKey: {
105
+ type: "witness_v1_taproot" | "witness_v0_scripthash" | "witness_v0_keyhash" | "pubkeyhash" | "scripthash";
106
+ address: string;
107
+ };
108
+ } & {
109
+ value: bigint;
110
+ n: number;
111
+ }, {
112
+ scriptPubKey: {
113
+ type: "nulldata";
114
+ hex: Uint8Array<ArrayBufferLike>;
115
+ };
116
+ } & {
117
+ value: bigint;
118
+ n: number;
119
+ }, {
120
+ scriptPubKey: {
121
+ type: "witness_v1_taproot" | "witness_v0_scripthash" | "witness_v0_keyhash" | "pubkeyhash" | "scripthash";
122
+ address: string;
123
+ };
124
+ } & {
125
+ value: bigint;
126
+ n: number;
127
+ }];
128
+ blockhash?: string | null | undefined;
129
+ }, {
130
+ vout: [{
131
+ scriptPubKey: {
132
+ type: "witness_v1_taproot" | "witness_v0_scripthash" | "witness_v0_keyhash" | "pubkeyhash" | "scripthash";
133
+ address: string;
134
+ };
135
+ } & {
136
+ value: number;
137
+ n: number;
138
+ }, {
139
+ scriptPubKey: {
140
+ type: "nulldata";
141
+ hex: string;
142
+ };
143
+ } & {
144
+ value: number;
145
+ n: number;
146
+ }, {
147
+ scriptPubKey: {
148
+ type: "witness_v1_taproot" | "witness_v0_scripthash" | "witness_v0_keyhash" | "pubkeyhash" | "scripthash";
149
+ address: string;
150
+ };
151
+ } & {
152
+ value: number;
153
+ n: number;
154
+ }];
155
+ blockhash?: string | null | undefined;
156
+ }>;
157
+ readonly getblock: z.ZodObject<{
158
+ height: z.ZodNumber;
159
+ }, "strip", z.ZodTypeAny, {
160
+ height: number;
161
+ }, {
162
+ height: number;
163
+ }>;
164
+ };
165
+ declare const makeRequest: <T extends keyof RpcRequest & keyof typeof responseSchemas>(rpcUrl: string, method: T, params: RpcRequest[T]) => Promise<z.output<(typeof responseSchemas)[T]> | null>;
166
+ //#endregion
167
+ export { makeRequest };
package/dist/rpc.mjs ADDED
@@ -0,0 +1,84 @@
1
+ import { hexToBytes } from "@chainflip/utils/bytes";
2
+ import BigNumber from "bignumber.js";
3
+ import { parseUrlWithBasicAuth } from "@chainflip/utils/url";
4
+ import { z } from "zod";
5
+
6
+ //#region src/rpc.ts
7
+ const hexString = z.string().regex(/^([0-9a-f]{2})+$/, { message: "expected hex string" });
8
+ const vout = z.object({
9
+ value: z.number().transform((n) => BigInt(new BigNumber(n).shiftedBy(8).toFixed(0))),
10
+ n: z.number()
11
+ });
12
+ const nulldataVout = z.object({ scriptPubKey: z.object({
13
+ type: z.literal("nulldata"),
14
+ hex: hexString.transform((x) => hexToBytes(`0x${x.slice(4)}`))
15
+ }) }).and(vout);
16
+ const addressVout = z.object({ scriptPubKey: z.object({
17
+ type: z.enum([
18
+ "witness_v1_taproot",
19
+ "witness_v0_scripthash",
20
+ "witness_v0_keyhash",
21
+ "pubkeyhash",
22
+ "scripthash"
23
+ ]),
24
+ address: z.string()
25
+ }) }).and(vout);
26
+ const responseSchemas = {
27
+ getrawtransaction: z.object({
28
+ vout: z.tuple([
29
+ addressVout,
30
+ nulldataVout,
31
+ addressVout
32
+ ]),
33
+ blockhash: hexString.nullish()
34
+ }),
35
+ getblock: z.object({ height: z.number() })
36
+ };
37
+ const rpcResponse = z.union([z.object({
38
+ result: z.null(),
39
+ error: z.object({
40
+ code: z.number(),
41
+ message: z.string()
42
+ })
43
+ }), z.object({
44
+ result: z.unknown(),
45
+ error: z.null()
46
+ })]);
47
+ const makeRequest = async (rpcUrl, method, params) => {
48
+ const { url, headers } = parseUrlWithBasicAuth(rpcUrl);
49
+ const res = await fetch(url, {
50
+ method: "POST",
51
+ headers: {
52
+ ...headers,
53
+ "Content-Type": "application/json"
54
+ },
55
+ body: JSON.stringify({
56
+ jsonrpc: "2.0",
57
+ id: 1,
58
+ method,
59
+ params
60
+ })
61
+ });
62
+ const text = await res.text();
63
+ let json;
64
+ try {
65
+ json = JSON.parse(text);
66
+ } catch {
67
+ if (res.status !== 200) throw new Error(`HTTP error [${res.status}]: ${text || res.statusText}`);
68
+ throw new Error(`Invalid JSON response: ${text}`);
69
+ }
70
+ const result = rpcResponse.parse(json);
71
+ if (result.error) {
72
+ if (result.error.code === -5) return null;
73
+ throw new Error(`RPC error [${result.error.code}]: ${result.error.message}`);
74
+ }
75
+ const parseResult = responseSchemas[method].safeParse(result.result);
76
+ if (!parseResult.success) {
77
+ if (method === "getrawtransaction") return null;
78
+ throw parseResult.error;
79
+ }
80
+ return parseResult.data;
81
+ };
82
+
83
+ //#endregion
84
+ export { makeRequest };
package/dist/scale.cjs ADDED
@@ -0,0 +1,52 @@
1
+ const require_rolldown_runtime = require('./_virtual/rolldown_runtime.cjs');
2
+ let _chainflip_utils_chainflip = require("@chainflip/utils/chainflip");
3
+ let scale_ts = require("scale-ts");
4
+
5
+ //#region src/scale.ts
6
+ const addressByteLengths = {
7
+ Bitcoin: void 0,
8
+ Arbitrum: 20,
9
+ Ethereum: 20,
10
+ Solana: 32,
11
+ Polkadot: 32,
12
+ Assethub: 32
13
+ };
14
+ const createSwapDataCodecV0 = (asset) => (0, scale_ts.Struct)({
15
+ version: scale_ts.u8,
16
+ destinationAsset: scale_ts.u8,
17
+ destinationAddress: (0, scale_ts.Bytes)(addressByteLengths[_chainflip_utils_chainflip.assetConstants[asset].chain]),
18
+ parameters: (0, scale_ts.Struct)({
19
+ retryDuration: scale_ts.u16,
20
+ minOutputAmount: scale_ts.u128,
21
+ numberOfChunks: scale_ts.u16,
22
+ chunkInterval: scale_ts.u16,
23
+ boostFee: scale_ts.u8,
24
+ brokerFee: scale_ts.u8,
25
+ affiliates: (0, scale_ts.Vector)((0, scale_ts.Struct)({
26
+ accountIndex: scale_ts.u8,
27
+ commissionBps: scale_ts.u8
28
+ }))
29
+ })
30
+ });
31
+ const createSwapDataCodecV1 = (asset) => (0, scale_ts.Struct)({
32
+ version: scale_ts.u8,
33
+ destinationAsset: scale_ts.u8,
34
+ destinationAddress: (0, scale_ts.Bytes)(addressByteLengths[_chainflip_utils_chainflip.assetConstants[asset].chain]),
35
+ parameters: (0, scale_ts.Struct)({
36
+ retryDuration: scale_ts.u16,
37
+ minOutputAmount: scale_ts.u128,
38
+ maxOraclePriceSlippage: scale_ts.u8,
39
+ numberOfChunks: scale_ts.u16,
40
+ chunkInterval: scale_ts.u16,
41
+ boostFee: scale_ts.u8,
42
+ brokerFee: scale_ts.u8,
43
+ affiliates: (0, scale_ts.Vector)((0, scale_ts.Struct)({
44
+ accountIndex: scale_ts.u8,
45
+ commissionBps: scale_ts.u8
46
+ }))
47
+ })
48
+ });
49
+
50
+ //#endregion
51
+ exports.createSwapDataCodecV0 = createSwapDataCodecV0;
52
+ exports.createSwapDataCodecV1 = createSwapDataCodecV1;
@@ -0,0 +1,44 @@
1
+ import { ChainflipAsset } from "@chainflip/utils/chainflip";
2
+ import * as scale_ts0 from "scale-ts";
3
+ import { CodecType } from "scale-ts";
4
+
5
+ //#region src/scale.d.ts
6
+ declare const createSwapDataCodecV0: (asset: ChainflipAsset) => scale_ts0.Codec<{
7
+ version: number;
8
+ destinationAsset: number;
9
+ destinationAddress: Uint8Array<ArrayBufferLike>;
10
+ parameters: {
11
+ retryDuration: number;
12
+ minOutputAmount: bigint;
13
+ numberOfChunks: number;
14
+ chunkInterval: number;
15
+ boostFee: number;
16
+ brokerFee: number;
17
+ affiliates: {
18
+ accountIndex: number;
19
+ commissionBps: number;
20
+ }[];
21
+ };
22
+ }>;
23
+ declare const createSwapDataCodecV1: (asset: ChainflipAsset) => scale_ts0.Codec<{
24
+ version: number;
25
+ destinationAsset: number;
26
+ destinationAddress: Uint8Array<ArrayBufferLike>;
27
+ parameters: {
28
+ retryDuration: number;
29
+ minOutputAmount: bigint;
30
+ maxOraclePriceSlippage: number;
31
+ numberOfChunks: number;
32
+ chunkInterval: number;
33
+ boostFee: number;
34
+ brokerFee: number;
35
+ affiliates: {
36
+ accountIndex: number;
37
+ commissionBps: number;
38
+ }[];
39
+ };
40
+ }>;
41
+ type UtxoDataV0 = CodecType<ReturnType<typeof createSwapDataCodecV0>>['parameters'];
42
+ type UtxoDataV1 = CodecType<ReturnType<typeof createSwapDataCodecV1>>['parameters'];
43
+ //#endregion
44
+ export { UtxoDataV0, UtxoDataV1, createSwapDataCodecV0, createSwapDataCodecV1 };
@@ -0,0 +1,44 @@
1
+ import { ChainflipAsset } from "@chainflip/utils/chainflip";
2
+ import * as scale_ts0 from "scale-ts";
3
+ import { CodecType } from "scale-ts";
4
+
5
+ //#region src/scale.d.ts
6
+ declare const createSwapDataCodecV0: (asset: ChainflipAsset) => scale_ts0.Codec<{
7
+ version: number;
8
+ destinationAsset: number;
9
+ destinationAddress: Uint8Array<ArrayBufferLike>;
10
+ parameters: {
11
+ retryDuration: number;
12
+ minOutputAmount: bigint;
13
+ numberOfChunks: number;
14
+ chunkInterval: number;
15
+ boostFee: number;
16
+ brokerFee: number;
17
+ affiliates: {
18
+ accountIndex: number;
19
+ commissionBps: number;
20
+ }[];
21
+ };
22
+ }>;
23
+ declare const createSwapDataCodecV1: (asset: ChainflipAsset) => scale_ts0.Codec<{
24
+ version: number;
25
+ destinationAsset: number;
26
+ destinationAddress: Uint8Array<ArrayBufferLike>;
27
+ parameters: {
28
+ retryDuration: number;
29
+ minOutputAmount: bigint;
30
+ maxOraclePriceSlippage: number;
31
+ numberOfChunks: number;
32
+ chunkInterval: number;
33
+ boostFee: number;
34
+ brokerFee: number;
35
+ affiliates: {
36
+ accountIndex: number;
37
+ commissionBps: number;
38
+ }[];
39
+ };
40
+ }>;
41
+ type UtxoDataV0 = CodecType<ReturnType<typeof createSwapDataCodecV0>>['parameters'];
42
+ type UtxoDataV1 = CodecType<ReturnType<typeof createSwapDataCodecV1>>['parameters'];
43
+ //#endregion
44
+ export { UtxoDataV0, UtxoDataV1, createSwapDataCodecV0, createSwapDataCodecV1 };
package/dist/scale.mjs ADDED
@@ -0,0 +1,50 @@
1
+ import { assetConstants } from "@chainflip/utils/chainflip";
2
+ import { Bytes, Struct, Vector, u128, u16, u8 } from "scale-ts";
3
+
4
+ //#region src/scale.ts
5
+ const addressByteLengths = {
6
+ Bitcoin: void 0,
7
+ Arbitrum: 20,
8
+ Ethereum: 20,
9
+ Solana: 32,
10
+ Polkadot: 32,
11
+ Assethub: 32
12
+ };
13
+ const createSwapDataCodecV0 = (asset) => Struct({
14
+ version: u8,
15
+ destinationAsset: u8,
16
+ destinationAddress: Bytes(addressByteLengths[assetConstants[asset].chain]),
17
+ parameters: Struct({
18
+ retryDuration: u16,
19
+ minOutputAmount: u128,
20
+ numberOfChunks: u16,
21
+ chunkInterval: u16,
22
+ boostFee: u8,
23
+ brokerFee: u8,
24
+ affiliates: Vector(Struct({
25
+ accountIndex: u8,
26
+ commissionBps: u8
27
+ }))
28
+ })
29
+ });
30
+ const createSwapDataCodecV1 = (asset) => Struct({
31
+ version: u8,
32
+ destinationAsset: u8,
33
+ destinationAddress: Bytes(addressByteLengths[assetConstants[asset].chain]),
34
+ parameters: Struct({
35
+ retryDuration: u16,
36
+ minOutputAmount: u128,
37
+ maxOraclePriceSlippage: u8,
38
+ numberOfChunks: u16,
39
+ chunkInterval: u16,
40
+ boostFee: u8,
41
+ brokerFee: u8,
42
+ affiliates: Vector(Struct({
43
+ accountIndex: u8,
44
+ commissionBps: u8
45
+ }))
46
+ })
47
+ });
48
+
49
+ //#endregion
50
+ export { createSwapDataCodecV0, createSwapDataCodecV1 };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chainflip/bitcoin",
3
- "version": "1.2.7",
3
+ "version": "2.1.0-beta.1",
4
4
  "type": "module",
5
5
  "repository": "https://github.com/chainflip-io/chainflip-product-toolkit.git",
6
6
  "publishConfig": {
@@ -18,15 +18,15 @@
18
18
  "default": "./dist/index.cjs"
19
19
  },
20
20
  "import": {
21
- "types": "./dist/index.d.ts",
22
- "default": "./dist/index.js"
21
+ "types": "./dist/index.d.mts",
22
+ "default": "./dist/index.mjs"
23
23
  }
24
24
  }
25
25
  },
26
26
  "dependencies": {
27
27
  "@chainflip/utils": "0.8.18",
28
28
  "bignumber.js": "^9.3.1",
29
- "bitcoinjs-lib": "7.0.0-rc.0",
29
+ "bitcoinjs-lib": "7.0.1",
30
30
  "scale-ts": "^1.6.1",
31
31
  "zod": "^3.25.75"
32
32
  },
@@ -37,8 +37,7 @@
37
37
  "prettier:write": "pnpm prettier:base --write",
38
38
  "prepublish": "pnpm build && pnpm test run",
39
39
  "clean": "rm -rf dist",
40
- "tsup:build": "tsup ./src/index.ts --format esm,cjs --dts --treeshake",
41
- "build": "pnpm clean && pnpm tsup:build",
40
+ "build": "pnpm clean && pnpm tsdown",
42
41
  "test:ci": "CI=1 pnpm t run",
43
42
  "test": "vitest"
44
43
  }
package/dist/index.d.ts DELETED
@@ -1,35 +0,0 @@
1
- import { ChainflipNetwork } from '@chainflip/utils/chainflip';
2
- import { VaultSwapData } from '@chainflip/utils/types';
3
-
4
- type BitcoinNetwork = 'mainnet' | 'testnet' | 'regtest';
5
-
6
- type Bytelike = Uint8Array | number[] | `0x${string}`;
7
- declare const networkHrp: {
8
- readonly mainnet: "bc";
9
- readonly testnet: "tb";
10
- readonly regtest: "bcrt";
11
- };
12
- type HRP = (typeof networkHrp)[keyof typeof networkHrp];
13
- type Base58AddressType = 'P2SH' | 'P2PKH';
14
- type DecodedBase58Address = {
15
- type: Base58AddressType;
16
- data: Uint8Array;
17
- version: number;
18
- };
19
- type DecodedSegwitAddress = {
20
- hrp: HRP;
21
- data: Uint8Array;
22
- type: SegwitAddressType;
23
- version: number;
24
- };
25
- type SegwitAddressType = 'P2WPKH' | 'P2WSH' | 'Taproot';
26
- declare const encodeAddress: (data: Bytelike, kind: Base58AddressType | SegwitAddressType, cfOrBtcnetwork: BitcoinNetwork | ChainflipNetwork) => string;
27
- declare const decodeAddress: (address: string, cfOrBtcNetwork: BitcoinNetwork | ChainflipNetwork) => DecodedBase58Address | DecodedSegwitAddress;
28
- declare const isValidAddressForNetwork: (address: string, cfOrBtcNetwork: BitcoinNetwork | ChainflipNetwork) => boolean;
29
-
30
- type BitcoinVaultSwapData = VaultSwapData<null> & {
31
- depositAddress: string;
32
- };
33
- declare const findVaultSwapData: (url: string, txId: string) => Promise<BitcoinVaultSwapData | null>;
34
-
35
- export { type Base58AddressType, type BitcoinNetwork, type BitcoinVaultSwapData, type Bytelike, type DecodedBase58Address, type DecodedSegwitAddress, type HRP, type SegwitAddressType, decodeAddress, encodeAddress, findVaultSwapData, isValidAddressForNetwork };