@lombard.finance/sdk-common 3.1.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 +1 -1
- package/dist/index.js +25 -13
- package/package.json +16 -8
- package/src/env.ts +1 -0
- package/src/utils/bitcoin.ts +21 -0
- package/src/utils/btc-address-type.ts +22 -0
- package/src/utils/err.ts +79 -0
- package/src/utils/get-output-script.ts +3 -6
- package/src/utils/numbers.ts +17 -0
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty
|
|
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,20 +1,32 @@
|
|
|
1
|
-
|
|
2
|
-
import { initEccLib as i, address as u, networks as r } from "bitcoinjs-lib";
|
|
3
|
-
const t = {
|
|
1
|
+
const r = {
|
|
4
2
|
prod: "prod",
|
|
5
3
|
testnet: "testnet",
|
|
6
4
|
stage: "stage",
|
|
7
|
-
dev: "dev"
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
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
|
|
14
26
|
).toString("hex")}`;
|
|
15
27
|
}
|
|
16
28
|
export {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
29
|
+
p as DEFAULT_ENV,
|
|
30
|
+
r as Env,
|
|
31
|
+
s as getOutputScript
|
|
20
32
|
};
|
package/package.json
CHANGED
|
@@ -1,35 +1,43 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lombard.finance/sdk-common",
|
|
3
|
-
"version": "3.1
|
|
3
|
+
"version": "3.3.1",
|
|
4
4
|
"exports": {
|
|
5
5
|
".": {
|
|
6
6
|
"types": "./src/index.ts",
|
|
7
7
|
"import": "./dist/index.js",
|
|
8
8
|
"require": "./dist/index.cjs"
|
|
9
|
-
}
|
|
9
|
+
},
|
|
10
|
+
"./utils/*": "./src/utils/*.ts"
|
|
10
11
|
},
|
|
11
12
|
"license": "MIT",
|
|
12
13
|
"type": "module",
|
|
13
14
|
"types": "./src/index.ts",
|
|
14
|
-
"files": [
|
|
15
|
+
"files": [
|
|
16
|
+
"dist",
|
|
17
|
+
"src"
|
|
18
|
+
],
|
|
15
19
|
"engines": {
|
|
16
20
|
"node": ">= 22.14.0"
|
|
17
21
|
},
|
|
18
22
|
"scripts": {
|
|
19
23
|
"build-docs": "rimraf ./sdk-docs && typedoc --out sdk-docs",
|
|
20
24
|
"build": "yarn types && vite build",
|
|
21
|
-
"lint": "biome lint",
|
|
25
|
+
"lint": "biome lint --diagnostic-level=error",
|
|
22
26
|
"types": "tsc --noEmit"
|
|
23
27
|
},
|
|
24
28
|
"peerDependencies": {
|
|
25
|
-
"@
|
|
29
|
+
"@bitcoinerlab/secp256k1": "1.2.0",
|
|
26
30
|
"bitcoinjs-lib": "6.1.5"
|
|
27
31
|
},
|
|
28
32
|
"devDependencies": {
|
|
29
|
-
"@
|
|
33
|
+
"@biomejs/biome": "^1.9.4",
|
|
34
|
+
"@bitcoinerlab/secp256k1": "1.2.0",
|
|
35
|
+
"bitcoinjs-lib": "6.1.5",
|
|
30
36
|
"rimraf": "^5",
|
|
31
37
|
"typedoc": "^0.26.6",
|
|
32
|
-
"typescript": "^5.4.5"
|
|
33
|
-
|
|
38
|
+
"typescript": "^5.4.5"
|
|
39
|
+
},
|
|
40
|
+
"dependencies": {
|
|
41
|
+
"vite": "^6.3.5"
|
|
34
42
|
}
|
|
35
43
|
}
|
package/src/env.ts
CHANGED
|
@@ -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
|
+
}
|
package/src/utils/err.ts
ADDED
|
@@ -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,9 +1,6 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { address as addressUtils, initEccLib, networks } from 'bitcoinjs-lib';
|
|
1
|
+
import { bitcoin } from './bitcoin';
|
|
3
2
|
import { Env } from '../env';
|
|
4
3
|
|
|
5
|
-
initEccLib(ecc);
|
|
6
|
-
|
|
7
4
|
/**
|
|
8
5
|
* Get output script from address.
|
|
9
6
|
*
|
|
@@ -18,9 +15,9 @@ export function getOutputScript(
|
|
|
18
15
|
): `0x${string}` {
|
|
19
16
|
console.info(`Getting output script for ${address} (${env})`);
|
|
20
17
|
|
|
21
|
-
const outputScriptBuf =
|
|
18
|
+
const outputScriptBuf = bitcoin.address.toOutputScript(
|
|
22
19
|
address,
|
|
23
|
-
env === Env.prod ? networks.bitcoin : networks.testnet,
|
|
20
|
+
env === Env.prod ? bitcoin.networks.bitcoin : bitcoin.networks.testnet,
|
|
24
21
|
);
|
|
25
22
|
const outputScript = outputScriptBuf.toString('hex');
|
|
26
23
|
return `0x${outputScript}`;
|
|
@@ -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
|
+
}
|