@lombard.finance/sdk-common 1.0.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/README.md +1 -0
- package/dist/index.cjs +2 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.js +37 -0
- package/dist/index.js.map +1 -0
- package/package.json +37 -0
- package/src/btcSdk/utils/getOutputScript.ts +64 -0
- package/src/const/defaultEnv.ts +3 -0
- package/src/index.ts +3 -0
- package/src/types/env.ts +7 -0
- package/src/vite-env.d.ts +1 -0
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# @lombard.finance/sdk-common
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
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=l(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 l(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(g(t))return"p2wsh";throw new Error("Address type is not supported.")}function g(t){return(t.startsWith("bc1")||t.startsWith("tb1"))&&t.length===62}exports.Env=s;exports.defaultEnv=f;exports.getOutputScript=d;
|
|
2
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":["../src/types/env.ts","../src/const/defaultEnv.ts","../src/btcSdk/utils/getOutputScript.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","import { Env } from '../types/env';\n\nexport const defaultEnv: 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 '../../types/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","defaultEnv","initEccLib","ecc","getOutputScript","address","env","addressType","getAddressType","paymentOutputScript","_a","payments","networks","result","addressUtils","isP2WSHAddressType"],"mappings":"ybAAaA,EAAM,CACjB,KAAM,OACN,QAAS,UACT,MAAO,OACT,ECFaC,EAAkBD,EAAI,KCOnCE,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
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
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 r = {
|
|
4
|
+
prod: "prod",
|
|
5
|
+
testnet: "testnet",
|
|
6
|
+
stage: "stage"
|
|
7
|
+
}, g = r.prod;
|
|
8
|
+
u(c);
|
|
9
|
+
function w(t, n = r.prod) {
|
|
10
|
+
var s;
|
|
11
|
+
const e = f(t), o = (s = a[e]({
|
|
12
|
+
address: t,
|
|
13
|
+
network: n === r.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 n = d.fromBech32(t);
|
|
21
|
+
if (n.version === 1 && n.data.length === 32)
|
|
22
|
+
return "p2tr";
|
|
23
|
+
if (n.version === 0 && n.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;
|
|
31
|
+
}
|
|
32
|
+
export {
|
|
33
|
+
r as Env,
|
|
34
|
+
g as defaultEnv,
|
|
35
|
+
w as getOutputScript
|
|
36
|
+
};
|
|
37
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../src/types/env.ts","../src/const/defaultEnv.ts","../src/btcSdk/utils/getOutputScript.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","import { Env } from '../types/env';\n\nexport const defaultEnv: 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 '../../types/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","defaultEnv","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,GCFaC,IAAkBD,EAAI;ACOnCE,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;"}
|
package/package.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@lombard.finance/sdk-common",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"exports": {
|
|
5
|
+
".": {
|
|
6
|
+
"import": "./dist/index.js",
|
|
7
|
+
"require": "./dist/index.cjs",
|
|
8
|
+
"types": "./src/index.ts"
|
|
9
|
+
}
|
|
10
|
+
},
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"type": "module",
|
|
13
|
+
"types": "./src/index.ts",
|
|
14
|
+
"files": [
|
|
15
|
+
"dist",
|
|
16
|
+
"src"
|
|
17
|
+
],
|
|
18
|
+
"engines": {
|
|
19
|
+
"node": ">= 18"
|
|
20
|
+
},
|
|
21
|
+
"scripts": {
|
|
22
|
+
"types": "tsc --noEmit",
|
|
23
|
+
"build": "yarn types && vite build",
|
|
24
|
+
"build-docs": "rimraf ./sdk-docs && typedoc --out sdk-docs"
|
|
25
|
+
},
|
|
26
|
+
"peerDependencies": {
|
|
27
|
+
"@bitcoin-js/tiny-secp256k1-asmjs": "2.2.3",
|
|
28
|
+
"bitcoinjs-lib": "6.1.5"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"@bitcoin-js/tiny-secp256k1-asmjs": "2.2.3",
|
|
32
|
+
"rimraf": "^5",
|
|
33
|
+
"typedoc": "^0.26.6",
|
|
34
|
+
"typescript": "^5.4.5",
|
|
35
|
+
"bitcoinjs-lib": "6.1.5"
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
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';
|
|
8
|
+
import { Env } from '../../types/env';
|
|
9
|
+
|
|
10
|
+
initEccLib(ecc);
|
|
11
|
+
|
|
12
|
+
type AddressType = 'p2tr' | 'p2wpkh' | 'p2wsh';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Get output script from address.
|
|
16
|
+
*
|
|
17
|
+
* @param address - The address.
|
|
18
|
+
* @param env
|
|
19
|
+
*
|
|
20
|
+
* @returns The output script.
|
|
21
|
+
*/
|
|
22
|
+
export function getOutputScript(address: string, env: Env = Env.prod): string {
|
|
23
|
+
const addressType = getAddressType(address);
|
|
24
|
+
|
|
25
|
+
const payment = payments[addressType]({
|
|
26
|
+
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
|
|
63
|
+
);
|
|
64
|
+
}
|
package/src/index.ts
ADDED
package/src/types/env.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
/// <reference types="vite/client" />
|