@chainflip/bitcoin 1.1.0 → 1.2.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/dist/index.cjs CHANGED
@@ -1,97 +1,6 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } }// src/index.ts
2
- var _assertion = require('@chainflip/utils/assertion');
3
- var _bytes = require('@chainflip/utils/bytes');
4
- var _bitcoinjslib = require('bitcoinjs-lib'); var bitcoin = _interopRequireWildcard(_bitcoinjslib);
5
- var p2pkhAddressVersion = {
6
- mainnet: 0,
7
- testnet: 111,
8
- regtest: 111
9
- };
10
- var p2shAddressVersion = {
11
- mainnet: 5,
12
- testnet: 196,
13
- regtest: 196
14
- };
15
- var networkHrp = {
16
- mainnet: "bc",
17
- testnet: "tb",
18
- regtest: "bcrt"
19
- };
20
- var segwitVersions = {
21
- P2WPKH: 0,
22
- P2WSH: 0,
23
- Taproot: 1
24
- };
25
- var networkMap = {
26
- mainnet: "mainnet",
27
- perseverance: "testnet",
28
- sisyphos: "testnet",
29
- testnet: "testnet",
30
- backspin: "regtest",
31
- regtest: "regtest"
32
- };
33
- var byteLikeToUint8Array = (data) => typeof data === "string" ? _bytes.hexToBytes.call(void 0, data) : new Uint8Array(data);
34
- var encodeAddress = (data, kind, cfOrBtcnetwork) => {
35
- const btcNetwork = networkMap[cfOrBtcnetwork];
36
- _assertion.assert.call(void 0, btcNetwork, `Invalid network: ${cfOrBtcnetwork}`);
37
- _assertion.assert.call(void 0, data.length % 2 === 0, "bytes must have an even number of characters");
38
- _assertion.assert.call(void 0,
39
- typeof data !== "string" || /^(0x)?[0-9a-f]*$/.test(data),
40
- "bytes are not a valid hex string"
41
- );
42
- const bytes = byteLikeToUint8Array(data);
43
- switch (kind) {
44
- case "P2PKH":
45
- case "P2SH": {
46
- const version = (kind === "P2SH" ? p2shAddressVersion : p2pkhAddressVersion)[btcNetwork];
47
- return bitcoin.address.toBase58Check(bytes, version);
48
- }
49
- case "P2WPKH":
50
- case "P2WSH":
51
- case "Taproot":
52
- return bitcoin.address.toBech32(bytes, segwitVersions[kind], networkHrp[btcNetwork]);
53
- default:
54
- throw new Error(`Invalid address type: ${kind}`);
55
- }
56
- };
57
- var decodeAddress = (address2, cfOrBtcNetwork) => {
58
- if (/^[13mn2]/.test(address2)) {
59
- const network = networkMap[cfOrBtcNetwork];
60
- const { hash, version } = bitcoin.address.fromBase58Check(address2);
61
- if (version === p2pkhAddressVersion[network]) {
62
- return { type: "P2PKH", data: hash, version };
63
- }
64
- if (version === p2shAddressVersion[network]) {
65
- return { type: "P2SH", data: hash, version };
66
- }
67
- throw new TypeError(`Invalid version: ${version}`);
68
- }
69
- if (/^(bc|tb|bcrt)1/.test(address2)) {
70
- const { data, prefix, version } = bitcoin.address.fromBech32(address2);
71
- let type;
72
- if (version === 0 && data.length === 20) {
73
- type = "P2WPKH";
74
- } else if (version === 0) {
75
- type = "P2WSH";
76
- } else if (version === 1) {
77
- type = "Taproot";
78
- } else {
79
- throw new TypeError(`Invalid version: ${version}`);
80
- }
81
- return { hrp: prefix, data, type, version };
82
- }
83
- throw new TypeError(`Invalid address: ${address2}`);
84
- };
85
- var isValidAddressForNetwork = (address2, cfOrBtcNetwork) => {
86
- try {
87
- decodeAddress(address2, cfOrBtcNetwork);
88
- return true;
89
- } catch (e) {
90
- return false;
91
- }
92
- };
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _createStarExport(obj) { Object.keys(obj) .filter((key) => key !== "default" && key !== "__esModule") .forEach((key) => { if (exports.hasOwnProperty(key)) { return; } Object.defineProperty(exports, key, {enumerable: true, configurable: true, get: () => obj[key]}); }); }// src/index.ts
2
+ var _addresscjs = require('./address.cjs'); _createStarExport(_addresscjs);
3
+ var _depositcjs = require('./deposit.cjs');
93
4
 
94
5
 
95
-
96
-
97
- exports.decodeAddress = decodeAddress; exports.encodeAddress = encodeAddress; exports.isValidAddressForNetwork = isValidAddressForNetwork;
6
+ exports.findVaultSwapData = _depositcjs.findVaultSwapData;
package/dist/index.d.cts CHANGED
@@ -1,5 +1,9 @@
1
+ import { ChainflipNetwork as ChainflipNetwork$1 } from '@chainflip/utils/chainflip';
2
+ import { VaultSwapData } from '@chainflip/utils/types';
3
+
1
4
  type ChainflipNetwork = 'mainnet' | 'perseverance' | 'sisyphos' | 'backspin';
2
5
  type BitcoinNetwork = 'mainnet' | 'testnet' | 'regtest';
6
+
3
7
  type Bytelike = Uint8Array | number[] | `0x${string}`;
4
8
  declare const networkHrp: {
5
9
  readonly mainnet: "bc";
@@ -20,8 +24,13 @@ type DecodedSegwitAddress = {
20
24
  version: number;
21
25
  };
22
26
  type SegwitAddressType = 'P2WPKH' | 'P2WSH' | 'Taproot';
23
- declare const encodeAddress: (data: Bytelike, kind: Base58AddressType | SegwitAddressType, cfOrBtcnetwork: BitcoinNetwork | ChainflipNetwork) => string;
24
- declare const decodeAddress: (address: string, cfOrBtcNetwork: BitcoinNetwork | ChainflipNetwork) => DecodedBase58Address | DecodedSegwitAddress;
25
- declare const isValidAddressForNetwork: (address: string, cfOrBtcNetwork: BitcoinNetwork | ChainflipNetwork) => boolean;
27
+ declare const encodeAddress: (data: Bytelike, kind: Base58AddressType | SegwitAddressType, cfOrBtcnetwork: BitcoinNetwork | ChainflipNetwork$1) => string;
28
+ declare const decodeAddress: (address: string, cfOrBtcNetwork: BitcoinNetwork | ChainflipNetwork$1) => DecodedBase58Address | DecodedSegwitAddress;
29
+ declare const isValidAddressForNetwork: (address: string, cfOrBtcNetwork: BitcoinNetwork | ChainflipNetwork$1) => boolean;
30
+
31
+ type BitcoinVaultSwapData = VaultSwapData & {
32
+ depositAddress: string;
33
+ };
34
+ declare const findVaultSwapData: (url: string, txId: string) => Promise<BitcoinVaultSwapData | null>;
26
35
 
27
- export { type Base58AddressType, type BitcoinNetwork, type Bytelike, type ChainflipNetwork, type DecodedBase58Address, type DecodedSegwitAddress, type HRP, type SegwitAddressType, decodeAddress, encodeAddress, isValidAddressForNetwork };
36
+ export { type Base58AddressType, type BitcoinNetwork, type BitcoinVaultSwapData, type Bytelike, type ChainflipNetwork, type DecodedBase58Address, type DecodedSegwitAddress, type HRP, type SegwitAddressType, decodeAddress, encodeAddress, findVaultSwapData, isValidAddressForNetwork };
package/dist/index.d.ts CHANGED
@@ -1,5 +1,9 @@
1
+ import { ChainflipNetwork as ChainflipNetwork$1 } from '@chainflip/utils/chainflip';
2
+ import { VaultSwapData } from '@chainflip/utils/types';
3
+
1
4
  type ChainflipNetwork = 'mainnet' | 'perseverance' | 'sisyphos' | 'backspin';
2
5
  type BitcoinNetwork = 'mainnet' | 'testnet' | 'regtest';
6
+
3
7
  type Bytelike = Uint8Array | number[] | `0x${string}`;
4
8
  declare const networkHrp: {
5
9
  readonly mainnet: "bc";
@@ -20,8 +24,13 @@ type DecodedSegwitAddress = {
20
24
  version: number;
21
25
  };
22
26
  type SegwitAddressType = 'P2WPKH' | 'P2WSH' | 'Taproot';
23
- declare const encodeAddress: (data: Bytelike, kind: Base58AddressType | SegwitAddressType, cfOrBtcnetwork: BitcoinNetwork | ChainflipNetwork) => string;
24
- declare const decodeAddress: (address: string, cfOrBtcNetwork: BitcoinNetwork | ChainflipNetwork) => DecodedBase58Address | DecodedSegwitAddress;
25
- declare const isValidAddressForNetwork: (address: string, cfOrBtcNetwork: BitcoinNetwork | ChainflipNetwork) => boolean;
27
+ declare const encodeAddress: (data: Bytelike, kind: Base58AddressType | SegwitAddressType, cfOrBtcnetwork: BitcoinNetwork | ChainflipNetwork$1) => string;
28
+ declare const decodeAddress: (address: string, cfOrBtcNetwork: BitcoinNetwork | ChainflipNetwork$1) => DecodedBase58Address | DecodedSegwitAddress;
29
+ declare const isValidAddressForNetwork: (address: string, cfOrBtcNetwork: BitcoinNetwork | ChainflipNetwork$1) => boolean;
30
+
31
+ type BitcoinVaultSwapData = VaultSwapData & {
32
+ depositAddress: string;
33
+ };
34
+ declare const findVaultSwapData: (url: string, txId: string) => Promise<BitcoinVaultSwapData | null>;
26
35
 
27
- export { type Base58AddressType, type BitcoinNetwork, type Bytelike, type ChainflipNetwork, type DecodedBase58Address, type DecodedSegwitAddress, type HRP, type SegwitAddressType, decodeAddress, encodeAddress, isValidAddressForNetwork };
36
+ export { type Base58AddressType, type BitcoinNetwork, type BitcoinVaultSwapData, type Bytelike, type ChainflipNetwork, type DecodedBase58Address, type DecodedSegwitAddress, type HRP, type SegwitAddressType, decodeAddress, encodeAddress, findVaultSwapData, isValidAddressForNetwork };
package/dist/index.mjs CHANGED
@@ -1,97 +1,6 @@
1
1
  // src/index.ts
2
- import { assert } from "@chainflip/utils/assertion";
3
- import { hexToBytes } from "@chainflip/utils/bytes";
4
- import * as bitcoin from "bitcoinjs-lib";
5
- var p2pkhAddressVersion = {
6
- mainnet: 0,
7
- testnet: 111,
8
- regtest: 111
9
- };
10
- var p2shAddressVersion = {
11
- mainnet: 5,
12
- testnet: 196,
13
- regtest: 196
14
- };
15
- var networkHrp = {
16
- mainnet: "bc",
17
- testnet: "tb",
18
- regtest: "bcrt"
19
- };
20
- var segwitVersions = {
21
- P2WPKH: 0,
22
- P2WSH: 0,
23
- Taproot: 1
24
- };
25
- var networkMap = {
26
- mainnet: "mainnet",
27
- perseverance: "testnet",
28
- sisyphos: "testnet",
29
- testnet: "testnet",
30
- backspin: "regtest",
31
- regtest: "regtest"
32
- };
33
- var byteLikeToUint8Array = (data) => typeof data === "string" ? hexToBytes(data) : new Uint8Array(data);
34
- var encodeAddress = (data, kind, cfOrBtcnetwork) => {
35
- const btcNetwork = networkMap[cfOrBtcnetwork];
36
- assert(btcNetwork, `Invalid network: ${cfOrBtcnetwork}`);
37
- assert(data.length % 2 === 0, "bytes must have an even number of characters");
38
- assert(
39
- typeof data !== "string" || /^(0x)?[0-9a-f]*$/.test(data),
40
- "bytes are not a valid hex string"
41
- );
42
- const bytes = byteLikeToUint8Array(data);
43
- switch (kind) {
44
- case "P2PKH":
45
- case "P2SH": {
46
- const version = (kind === "P2SH" ? p2shAddressVersion : p2pkhAddressVersion)[btcNetwork];
47
- return bitcoin.address.toBase58Check(bytes, version);
48
- }
49
- case "P2WPKH":
50
- case "P2WSH":
51
- case "Taproot":
52
- return bitcoin.address.toBech32(bytes, segwitVersions[kind], networkHrp[btcNetwork]);
53
- default:
54
- throw new Error(`Invalid address type: ${kind}`);
55
- }
56
- };
57
- var decodeAddress = (address2, cfOrBtcNetwork) => {
58
- if (/^[13mn2]/.test(address2)) {
59
- const network = networkMap[cfOrBtcNetwork];
60
- const { hash, version } = bitcoin.address.fromBase58Check(address2);
61
- if (version === p2pkhAddressVersion[network]) {
62
- return { type: "P2PKH", data: hash, version };
63
- }
64
- if (version === p2shAddressVersion[network]) {
65
- return { type: "P2SH", data: hash, version };
66
- }
67
- throw new TypeError(`Invalid version: ${version}`);
68
- }
69
- if (/^(bc|tb|bcrt)1/.test(address2)) {
70
- const { data, prefix, version } = bitcoin.address.fromBech32(address2);
71
- let type;
72
- if (version === 0 && data.length === 20) {
73
- type = "P2WPKH";
74
- } else if (version === 0) {
75
- type = "P2WSH";
76
- } else if (version === 1) {
77
- type = "Taproot";
78
- } else {
79
- throw new TypeError(`Invalid version: ${version}`);
80
- }
81
- return { hrp: prefix, data, type, version };
82
- }
83
- throw new TypeError(`Invalid address: ${address2}`);
84
- };
85
- var isValidAddressForNetwork = (address2, cfOrBtcNetwork) => {
86
- try {
87
- decodeAddress(address2, cfOrBtcNetwork);
88
- return true;
89
- } catch {
90
- return false;
91
- }
92
- };
2
+ export * from "./address.mjs";
3
+ import { findVaultSwapData } from "./deposit.mjs";
93
4
  export {
94
- decodeAddress,
95
- encodeAddress,
96
- isValidAddressForNetwork
5
+ findVaultSwapData
97
6
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chainflip/bitcoin",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "type": "module",
5
5
  "repository": "https://github.com/chainflip-io/chainflip-product-toolkit.git",
6
6
  "publishConfig": {
@@ -14,14 +14,22 @@
14
14
  "main": "dist/index.cjs",
15
15
  "module": "dist/index.mjs",
16
16
  "dependencies": {
17
- "@chainflip/utils": "^0.4.0",
18
- "bitcoinjs-lib": "^7.0.0-rc.0"
17
+ "@chainflip/utils": "0.7.0",
18
+ "bignumber.js": "^9.1.2",
19
+ "bitcoinjs-lib": "^7.0.0-rc.0",
20
+ "scale-ts": "^1.6.1",
21
+ "zod": "^3.24.2"
19
22
  },
20
23
  "scripts": {
24
+ "eslint:check": "pnpm eslint --max-warnings 0 './**/*.ts'",
25
+ "prettier:base": "prettier ./** --ignore-path=../../.prettierignore",
26
+ "prettier:check": "pnpm prettier:base --check",
27
+ "prettier:write": "pnpm prettier:base --write",
21
28
  "prepublish": "pnpm build && pnpm test run",
22
29
  "clean": "rm -rf dist",
23
30
  "tsup:build": "tsup --config tsup.config.mts",
24
31
  "build": "pnpm clean && pnpm tsup:build",
32
+ "test:ci": "CI=1 pnpm t run",
25
33
  "test": "vitest"
26
34
  }
27
35
  }