@lombard.finance/sdk-common 3.0.0 → 3.3.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/index.cjs CHANGED
@@ -1,2 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const u=require("@bitcoin-js/tiny-secp256k1-asmjs"),r=require("bitcoinjs-lib");function p(t){const e=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(t){for(const n in t)if(n!=="default"){const o=Object.getOwnPropertyDescriptor(t,n);Object.defineProperty(e,n,o.get?o:{enumerable:!0,get:()=>t[n]})}}return e.default=t,Object.freeze(e)}const a=p(u),s={prod:"prod",testnet:"testnet",stage:"stage"},f=s.prod;r.initEccLib(a);function d(t,e=s.prod){var c;const n=g(t),i=(c=r.payments[n]({address:t,network:e===s.prod?r.networks.bitcoin:r.networks.testnet}).output)==null?void 0:c.toString("hex");if(!i)throw new Error("Output script is not found.");return`0x${i}`}function g(t){const e=r.address.fromBech32(t);if(e.version===1&&e.data.length===32)return"p2tr";if(e.version===0&&e.data.length===20)return"p2wpkh";if(l(t))return"p2wsh";throw new Error("Address type is not supported.")}function l(t){return(t.startsWith("bc1")||t.startsWith("tb1"))&&t.length===62}exports.DEFAULT_ENV=f;exports.Env=s;exports.getOutputScript=d;
2
- //# sourceMappingURL=index.cjs.map
1
+ "use strict";var s=Object.create;var p=Object.defineProperty;var a=Object.getOwnPropertyDescriptor;var d=Object.getOwnPropertyNames;var l=Object.getPrototypeOf,S=Object.prototype.hasOwnProperty;var b=(t,i,o,n)=>{if(i&&typeof i=="object"||typeof i=="function")for(let r of d(i))!S.call(t,r)&&r!==o&&p(t,r,{get:()=>i[r],enumerable:!(n=a(i,r))||n.enumerable});return t};var u=(t,i,o)=>(o=t!=null?s(l(t)):{},b(i||!t||!t.__esModule?p(o,"default",{value:t,enumerable:!0}):o,t));Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e={prod:"prod",testnet:"testnet",stage:"stage",dev:"dev",ibc:"ibc"},g=e.prod;let c;const E=async()=>{const t=await import("bitcoinjs-lib"),i=await import("@bitcoinerlab/secp256k1");try{t.initEccLib(i)}catch(o){if(!/already initialized/i.test(String(o)))throw o}c=t};E().catch(t=>{console.error("Failed to initialize bitcoinjs-lib ECC library",t)});function f(t,i=e.prod){return console.info(`Getting output script for ${t} (${i})`),`0x${c.address.toOutputScript(t,i===e.prod?c.networks.bitcoin:c.networks.testnet).toString("hex")}`}exports.DEFAULT_ENV=g;exports.Env=e;exports.getOutputScript=f;
package/dist/index.js CHANGED
@@ -1,37 +1,32 @@
1
- import * as c from "@bitcoin-js/tiny-secp256k1-asmjs";
2
- import { initEccLib as u, payments as a, networks as i, address as d } from "bitcoinjs-lib";
3
- const n = {
1
+ const r = {
4
2
  prod: "prod",
5
3
  testnet: "testnet",
6
- stage: "stage"
7
- }, g = n.prod;
8
- u(c);
9
- function w(t, r = n.prod) {
10
- var s;
11
- const e = f(t), o = (s = a[e]({
12
- address: t,
13
- network: r === n.prod ? i.bitcoin : i.testnet
14
- }).output) == null ? void 0 : s.toString("hex");
15
- if (!o)
16
- throw new Error("Output script is not found.");
17
- return `0x${o}`;
18
- }
19
- function f(t) {
20
- const r = d.fromBech32(t);
21
- if (r.version === 1 && r.data.length === 32)
22
- return "p2tr";
23
- if (r.version === 0 && r.data.length === 20)
24
- return "p2wpkh";
25
- if (h(t))
26
- return "p2wsh";
27
- throw new Error("Address type is not supported.");
28
- }
29
- function h(t) {
30
- return (t.startsWith("bc1") || t.startsWith("tb1")) && t.length === 62;
4
+ stage: "stage",
5
+ dev: "dev",
6
+ ibc: "ibc"
7
+ }, p = r.prod;
8
+ let o;
9
+ const n = async () => {
10
+ const t = await import("bitcoinjs-lib"), i = await import("@bitcoinerlab/secp256k1");
11
+ try {
12
+ t.initEccLib(i);
13
+ } catch (c) {
14
+ if (!/already initialized/i.test(String(c)))
15
+ throw c;
16
+ }
17
+ o = t;
18
+ };
19
+ n().catch((t) => {
20
+ console.error("Failed to initialize bitcoinjs-lib ECC library", t);
21
+ });
22
+ function s(t, i = r.prod) {
23
+ return console.info(`Getting output script for ${t} (${i})`), `0x${o.address.toOutputScript(
24
+ t,
25
+ i === r.prod ? o.networks.bitcoin : o.networks.testnet
26
+ ).toString("hex")}`;
31
27
  }
32
28
  export {
33
- g as DEFAULT_ENV,
34
- n as Env,
35
- w as getOutputScript
29
+ p as DEFAULT_ENV,
30
+ r as Env,
31
+ s as getOutputScript
36
32
  };
37
- //# sourceMappingURL=index.js.map
package/package.json CHANGED
@@ -1,12 +1,13 @@
1
1
  {
2
2
  "name": "@lombard.finance/sdk-common",
3
- "version": "3.0.0",
3
+ "version": "3.3.1",
4
4
  "exports": {
5
5
  ".": {
6
+ "types": "./src/index.ts",
6
7
  "import": "./dist/index.js",
7
- "require": "./dist/index.cjs",
8
- "types": "./src/index.ts"
9
- }
8
+ "require": "./dist/index.cjs"
9
+ },
10
+ "./utils/*": "./src/utils/*.ts"
10
11
  },
11
12
  "license": "MIT",
12
13
  "type": "module",
@@ -21,18 +22,22 @@
21
22
  "scripts": {
22
23
  "build-docs": "rimraf ./sdk-docs && typedoc --out sdk-docs",
23
24
  "build": "yarn types && vite build",
24
- "lint": "biome lint",
25
+ "lint": "biome lint --diagnostic-level=error",
25
26
  "types": "tsc --noEmit"
26
27
  },
27
28
  "peerDependencies": {
28
- "@bitcoin-js/tiny-secp256k1-asmjs": "2.2.3",
29
+ "@bitcoinerlab/secp256k1": "1.2.0",
29
30
  "bitcoinjs-lib": "6.1.5"
30
31
  },
31
32
  "devDependencies": {
32
- "@bitcoin-js/tiny-secp256k1-asmjs": "2.2.3",
33
+ "@biomejs/biome": "^1.9.4",
34
+ "@bitcoinerlab/secp256k1": "1.2.0",
35
+ "bitcoinjs-lib": "6.1.5",
33
36
  "rimraf": "^5",
34
37
  "typedoc": "^0.26.6",
35
- "typescript": "^5.4.5",
36
- "bitcoinjs-lib": "6.1.5"
38
+ "typescript": "^5.4.5"
39
+ },
40
+ "dependencies": {
41
+ "vite": "^6.3.5"
37
42
  }
38
43
  }
package/src/env.ts CHANGED
@@ -2,6 +2,8 @@ export const Env = {
2
2
  prod: 'prod',
3
3
  testnet: 'testnet',
4
4
  stage: 'stage',
5
+ dev: 'dev',
6
+ ibc: 'ibc',
5
7
  } as const;
6
8
 
7
9
  export type Env = (typeof Env)[keyof typeof Env];
@@ -0,0 +1,21 @@
1
+ let bitcoin: typeof import('bitcoinjs-lib');
2
+
3
+ const initBitcoin = async () => {
4
+ const module = await import('bitcoinjs-lib');
5
+ const ecc = await import('@bitcoinerlab/secp256k1');
6
+ try {
7
+ module.initEccLib(ecc);
8
+ } catch (err) {
9
+ if (!/already initialized/i.test(String(err))) {
10
+ throw err;
11
+ }
12
+ }
13
+ bitcoin = module;
14
+ };
15
+
16
+ void initBitcoin().catch(err => {
17
+ // Surface initialization failures during development but swallow to avoid unhandled rejections.
18
+ console.error('Failed to initialize bitcoinjs-lib ECC library', err);
19
+ });
20
+
21
+ export { bitcoin };
@@ -0,0 +1,22 @@
1
+ import { address } from 'bitcoinjs-lib';
2
+
3
+ export enum BtcAddressType {
4
+ p2tr = 'p2tr',
5
+ p2wpkh = 'p2wpkh',
6
+ p2wsh = 'p2wsh',
7
+ }
8
+
9
+ export function getBtcAddressType(btcAddress: string): BtcAddressType {
10
+ const { data, version } = address.fromBech32(btcAddress);
11
+
12
+ if (version === 0) {
13
+ if (data.length === 20) return BtcAddressType.p2wpkh;
14
+ if (data.length === 32) return BtcAddressType.p2wsh;
15
+ }
16
+
17
+ if (version === 1) {
18
+ if (data.length === 32) return BtcAddressType.p2tr;
19
+ }
20
+
21
+ throw new Error('Invalid BTC address');
22
+ }
@@ -0,0 +1,79 @@
1
+ import { AxiosError } from 'axios';
2
+
3
+ /**
4
+ * Extract a readable error message from any error object
5
+ * @param error Error object to extract message from
6
+ * @returns A readable error message string
7
+ */
8
+ export function extractErrorMessage(error: unknown): string {
9
+ // If error is already a string, return it
10
+ if (typeof error === 'string') {
11
+ return error;
12
+ }
13
+
14
+ // Check for data.message pattern
15
+ // biome-ignore lint/suspicious/noExplicitAny: unknown err
16
+ const hasDataMessage = (err: any): err is { data: { message: string } } =>
17
+ err?.data?.message && typeof err.data.message === 'string';
18
+
19
+ if (hasDataMessage(error)) {
20
+ return error.data.message;
21
+ }
22
+
23
+ // Handle standard Error objects and Axios errors
24
+ if (error instanceof Error) {
25
+ if ('isAxiosError' in error && error.isAxiosError) {
26
+ return extractAxiosErrorMessage(error as unknown as AxiosError);
27
+ }
28
+ return error.message;
29
+ }
30
+
31
+ // Handle generic objects with a message property
32
+ if (error !== null && typeof error === 'object') {
33
+ if ('message' in error && typeof error.message === 'string') {
34
+ return error.message;
35
+ }
36
+
37
+ // Try to convert object to a readable string
38
+ try {
39
+ return JSON.stringify(error);
40
+ } catch {
41
+ // If JSON.stringify fails, return a generic message
42
+ return 'Unknown error object';
43
+ }
44
+ }
45
+
46
+ return 'Unknown error';
47
+ }
48
+
49
+ /**
50
+ * Extract error message from an Axios error
51
+ * @param error Axios error object
52
+ * @returns Error message from the Axios error
53
+ */
54
+ function extractAxiosErrorMessage(error: AxiosError): string {
55
+ if (error.response) {
56
+ const responseData = error.response.data;
57
+ if (responseData && typeof responseData === 'object') {
58
+ if (
59
+ 'message' in responseData &&
60
+ responseData.message &&
61
+ typeof responseData.message === 'string'
62
+ ) {
63
+ return responseData.message;
64
+ }
65
+ try {
66
+ return JSON.stringify(responseData);
67
+ } catch {
68
+ return 'Error in API response';
69
+ }
70
+ }
71
+ return `HTTP error ${error.response.status}: ${error.response.statusText}`;
72
+ }
73
+
74
+ if (error.message) {
75
+ return error.message;
76
+ }
77
+
78
+ return 'Network error';
79
+ }
@@ -1,16 +1,6 @@
1
- import * as ecc from '@bitcoin-js/tiny-secp256k1-asmjs';
2
- import {
3
- address as addressUtils,
4
- initEccLib,
5
- networks,
6
- payments,
7
- } from 'bitcoinjs-lib';
1
+ import { bitcoin } from './bitcoin';
8
2
  import { Env } from '../env';
9
3
 
10
- initEccLib(ecc);
11
-
12
- type AddressType = 'p2tr' | 'p2wpkh' | 'p2wsh';
13
-
14
4
  /**
15
5
  * Get output script from address.
16
6
  *
@@ -19,46 +9,16 @@ type AddressType = 'p2tr' | 'p2wpkh' | 'p2wsh';
19
9
  *
20
10
  * @returns The output script.
21
11
  */
22
- export function getOutputScript(address: string, env: Env = Env.prod): string {
23
- const addressType = getAddressType(address);
12
+ export function getOutputScript(
13
+ address: string,
14
+ env: Env = Env.prod,
15
+ ): `0x${string}` {
16
+ console.info(`Getting output script for ${address} (${env})`);
24
17
 
25
- const payment = payments[addressType]({
18
+ const outputScriptBuf = bitcoin.address.toOutputScript(
26
19
  address,
27
- network: env === Env.prod ? networks.bitcoin : networks.testnet,
28
- });
29
-
30
- const paymentOutputScript = payment.output?.toString('hex');
31
-
32
- if (!paymentOutputScript) {
33
- throw new Error('Output script is not found.');
34
- }
35
-
36
- return `0x${paymentOutputScript}`;
37
- }
38
-
39
- function getAddressType(address: string): AddressType {
40
- const result = addressUtils.fromBech32(address);
41
-
42
- const isP2TR = result.version === 1 && result.data.length === 32;
43
- if (isP2TR) {
44
- return 'p2tr';
45
- }
46
-
47
- const isP2WPKH = result.version === 0 && result.data.length === 20;
48
- if (isP2WPKH) {
49
- return 'p2wpkh';
50
- }
51
-
52
- if (isP2WSHAddressType(address)) {
53
- return 'p2wsh';
54
- }
55
-
56
- throw new Error('Address type is not supported.');
57
- }
58
-
59
- function isP2WSHAddressType(address: string): boolean {
60
- return (
61
- (address.startsWith('bc1') || address.startsWith('tb1')) &&
62
- address.length === 62
20
+ env === Env.prod ? bitcoin.networks.bitcoin : bitcoin.networks.testnet,
63
21
  );
22
+ const outputScript = outputScriptBuf.toString('hex');
23
+ return `0x${outputScript}`;
64
24
  }
@@ -0,0 +1,17 @@
1
+ import BigNumber from 'bignumber.js';
2
+
3
+ export function toBaseDenomination(
4
+ input: BigNumber.Value,
5
+ decimalPlaces: number,
6
+ ) {
7
+ return BigNumber(input)
8
+ .multipliedBy(BigNumber(10).pow(decimalPlaces))
9
+ .decimalPlaces(0, BigNumber.ROUND_HALF_UP);
10
+ }
11
+
12
+ export function fromBaseDenomination(
13
+ input: BigNumber.Value,
14
+ decimalPlaces: number,
15
+ ) {
16
+ return BigNumber(input).dividedBy(BigNumber(10).pow(decimalPlaces));
17
+ }
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.cjs","sources":["../src/env.ts","../src/utils/get-output-script.ts"],"sourcesContent":["export const Env = {\n prod: 'prod',\n testnet: 'testnet',\n stage: 'stage',\n} as const;\n\nexport type Env = (typeof Env)[keyof typeof Env];\n\nexport const DEFAULT_ENV: Env = Env.prod;\n","import * as ecc from '@bitcoin-js/tiny-secp256k1-asmjs';\nimport {\n address as addressUtils,\n initEccLib,\n networks,\n payments,\n} from 'bitcoinjs-lib';\nimport { Env } from '../env';\n\ninitEccLib(ecc);\n\ntype AddressType = 'p2tr' | 'p2wpkh' | 'p2wsh';\n\n/**\n * Get output script from address.\n *\n * @param address - The address.\n * @param env\n *\n * @returns The output script.\n */\nexport function getOutputScript(address: string, env: Env = Env.prod): string {\n const addressType = getAddressType(address);\n\n const payment = payments[addressType]({\n address,\n network: env === Env.prod ? networks.bitcoin : networks.testnet,\n });\n\n const paymentOutputScript = payment.output?.toString('hex');\n\n if (!paymentOutputScript) {\n throw new Error('Output script is not found.');\n }\n\n return `0x${paymentOutputScript}`;\n}\n\nfunction getAddressType(address: string): AddressType {\n const result = addressUtils.fromBech32(address);\n\n const isP2TR = result.version === 1 && result.data.length === 32;\n if (isP2TR) {\n return 'p2tr';\n }\n\n const isP2WPKH = result.version === 0 && result.data.length === 20;\n if (isP2WPKH) {\n return 'p2wpkh';\n }\n\n if (isP2WSHAddressType(address)) {\n return 'p2wsh';\n }\n\n throw new Error('Address type is not supported.');\n}\n\nfunction isP2WSHAddressType(address: string): boolean {\n return (\n (address.startsWith('bc1') || address.startsWith('tb1')) &&\n address.length === 62\n );\n}\n"],"names":["Env","DEFAULT_ENV","initEccLib","ecc","getOutputScript","address","env","addressType","getAddressType","paymentOutputScript","_a","payments","networks","result","addressUtils","isP2WSHAddressType"],"mappings":"ybAAaA,EAAM,CACjB,KAAM,OACN,QAAS,UACT,MAAO,OACT,EAIaC,EAAmBD,EAAI,KCCpCE,EAAA,WAAWC,CAAG,EAYP,SAASC,EAAgBC,EAAiBC,EAAWN,EAAI,KAAc,OACtE,MAAAO,EAAcC,EAAeH,CAAO,EAOpCI,GAAsBC,EALZC,WAASJ,CAAW,EAAE,CACpC,QAAAF,EACA,QAASC,IAAQN,EAAI,KAAOY,EAAA,SAAS,QAAUA,WAAS,OAAA,CACzD,EAEmC,SAAR,YAAAF,EAAgB,SAAS,OAErD,GAAI,CAACD,EACG,MAAA,IAAI,MAAM,6BAA6B,EAG/C,MAAO,KAAKA,CAAmB,EACjC,CAEA,SAASD,EAAeH,EAA8B,CAC9C,MAAAQ,EAASC,EAAAA,QAAa,WAAWT,CAAO,EAG9C,GADeQ,EAAO,UAAY,GAAKA,EAAO,KAAK,SAAW,GAErD,MAAA,OAIT,GADiBA,EAAO,UAAY,GAAKA,EAAO,KAAK,SAAW,GAEvD,MAAA,SAGL,GAAAE,EAAmBV,CAAO,EACrB,MAAA,QAGH,MAAA,IAAI,MAAM,gCAAgC,CAClD,CAEA,SAASU,EAAmBV,EAA0B,CAEjD,OAAAA,EAAQ,WAAW,KAAK,GAAKA,EAAQ,WAAW,KAAK,IACtDA,EAAQ,SAAW,EAEvB"}
package/dist/index.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sources":["../src/env.ts","../src/utils/get-output-script.ts"],"sourcesContent":["export const Env = {\n prod: 'prod',\n testnet: 'testnet',\n stage: 'stage',\n} as const;\n\nexport type Env = (typeof Env)[keyof typeof Env];\n\nexport const DEFAULT_ENV: Env = Env.prod;\n","import * as ecc from '@bitcoin-js/tiny-secp256k1-asmjs';\nimport {\n address as addressUtils,\n initEccLib,\n networks,\n payments,\n} from 'bitcoinjs-lib';\nimport { Env } from '../env';\n\ninitEccLib(ecc);\n\ntype AddressType = 'p2tr' | 'p2wpkh' | 'p2wsh';\n\n/**\n * Get output script from address.\n *\n * @param address - The address.\n * @param env\n *\n * @returns The output script.\n */\nexport function getOutputScript(address: string, env: Env = Env.prod): string {\n const addressType = getAddressType(address);\n\n const payment = payments[addressType]({\n address,\n network: env === Env.prod ? networks.bitcoin : networks.testnet,\n });\n\n const paymentOutputScript = payment.output?.toString('hex');\n\n if (!paymentOutputScript) {\n throw new Error('Output script is not found.');\n }\n\n return `0x${paymentOutputScript}`;\n}\n\nfunction getAddressType(address: string): AddressType {\n const result = addressUtils.fromBech32(address);\n\n const isP2TR = result.version === 1 && result.data.length === 32;\n if (isP2TR) {\n return 'p2tr';\n }\n\n const isP2WPKH = result.version === 0 && result.data.length === 20;\n if (isP2WPKH) {\n return 'p2wpkh';\n }\n\n if (isP2WSHAddressType(address)) {\n return 'p2wsh';\n }\n\n throw new Error('Address type is not supported.');\n}\n\nfunction isP2WSHAddressType(address: string): boolean {\n return (\n (address.startsWith('bc1') || address.startsWith('tb1')) &&\n address.length === 62\n );\n}\n"],"names":["Env","DEFAULT_ENV","initEccLib","ecc","getOutputScript","address","env","addressType","getAddressType","paymentOutputScript","_a","payments","networks","result","addressUtils","isP2WSHAddressType"],"mappings":";;AAAO,MAAMA,IAAM;AAAA,EACjB,MAAM;AAAA,EACN,SAAS;AAAA,EACT,OAAO;AACT,GAIaC,IAAmBD,EAAI;ACCpCE,EAAWC,CAAG;AAYP,SAASC,EAAgBC,GAAiBC,IAAWN,EAAI,MAAc;;AACtE,QAAAO,IAAcC,EAAeH,CAAO,GAOpCI,KAAsBC,IALZC,EAASJ,CAAW,EAAE;AAAA,IACpC,SAAAF;AAAA,IACA,SAASC,MAAQN,EAAI,OAAOY,EAAS,UAAUA,EAAS;AAAA,EAAA,CACzD,EAEmC,WAAR,gBAAAF,EAAgB,SAAS;AAErD,MAAI,CAACD;AACG,UAAA,IAAI,MAAM,6BAA6B;AAG/C,SAAO,KAAKA,CAAmB;AACjC;AAEA,SAASD,EAAeH,GAA8B;AAC9C,QAAAQ,IAASC,EAAa,WAAWT,CAAO;AAG9C,MADeQ,EAAO,YAAY,KAAKA,EAAO,KAAK,WAAW;AAErD,WAAA;AAIT,MADiBA,EAAO,YAAY,KAAKA,EAAO,KAAK,WAAW;AAEvD,WAAA;AAGL,MAAAE,EAAmBV,CAAO;AACrB,WAAA;AAGH,QAAA,IAAI,MAAM,gCAAgC;AAClD;AAEA,SAASU,EAAmBV,GAA0B;AAEjD,UAAAA,EAAQ,WAAW,KAAK,KAAKA,EAAQ,WAAW,KAAK,MACtDA,EAAQ,WAAW;AAEvB;"}