From 0736979c49b6ec162b561443ee81b3a433980b62 Mon Sep 17 00:00:00 2001 From: Martin Saldinger <51637671+LeTamanoir@users.noreply.github.com> Date: Wed, 16 Oct 2024 17:09:06 +0200 Subject: [PATCH] add ESM + CJS builds (#133) --- .gitignore | 2 +- package.json | 17 +++++++++---- src/fireblocks.ts | 54 ++++++++++++++++++++++++++-------------- src/fireblocks_signer.ts | 8 +++--- src/kiln.ts | 10 ++++---- src/utils.ts | 17 ++++++++++--- tsconfig.base.json | 35 ++++++++++++++++++++++++++ tsconfig.build.json | 10 ++++++++ tsconfig.json | 22 +++++----------- 9 files changed, 122 insertions(+), 53 deletions(-) create mode 100644 tsconfig.base.json create mode 100644 tsconfig.build.json diff --git a/.gitignore b/.gitignore index 7151acf..ee386fb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ .DS_Store node_modules/ -lib/ +build/ .idea/ \ No newline at end of file diff --git a/package.json b/package.json index 0b5be21..0fb3e25 100644 --- a/package.json +++ b/package.json @@ -1,21 +1,28 @@ { "name": "@kilnfi/sdk", - "version": "3.1.0", + "version": "3.1.1", "autor": "Kiln (https://kiln.fi)", "license": "BUSL-1.1", "description": "JavaScript sdk for Kiln API", "type": "module", + "main": "./build/cjs/index.js", + "module": "./build/esm/index.js", + "types": "./build/types/index.d.ts", + "typings": "./build/types/index.d.ts", "exports": { ".": { - "default": "./lib/kiln.js", - "types": "./lib/kiln.d.ts", - "import": "./lib/kiln.js" + "default": "./build/cjs/kiln.js", + "types": "./build/types/kiln.d.ts", + "import": "./build/esm/kiln.js" } }, "scripts": { "lint": "biome check ./src", "format": "biome check ./src --write --unsafe", - "build": "tsc" + "build": "bun run build:esm && bun run build:cjs && bun run build:types", + "build:esm": "tsc --project ./tsconfig.build.json --module es2020 --outDir ./build/esm", + "build:cjs": "tsc --project ./tsconfig.build.json --module commonjs --outDir ./build/cjs --removeComments --verbatimModuleSyntax false", + "build:types": "tsc --project ./tsconfig.build.json --module esnext --declarationDir ./build/types --emitDeclarationOnly --declaration --declarationMap" }, "keywords": [ "sdk", diff --git a/src/fireblocks.ts b/src/fireblocks.ts index 38e930f..81c66eb 100644 --- a/src/fireblocks.ts +++ b/src/fireblocks.ts @@ -1,7 +1,7 @@ import { type AssetTypeResponse, FireblocksSDK, type PublicKeyResponse, SigningAlgorithm } from 'fireblocks-sdk'; import type { Client } from 'openapi-fetch'; -import { FireblocksSigner } from './fireblocks_signer'; -import type { components, paths } from './openapi/schema'; +import { FireblocksSigner } from './fireblocks_signer.js'; +import type { components, paths } from './openapi/schema.js'; export type FireblocksIntegration = { provider: 'fireblocks'; @@ -189,7 +189,8 @@ export class FireblocksService { const fbSigner = this.getSigner(integration); const fbNote = note ? note : 'ATOM tx from @kilnfi/sdk'; const fbTx = await fbSigner.sign(payload, assetId, fbNote); - const signature = fbTx.signedMessages?.[0].signature.fullSig; + const signature = fbTx.signedMessages?.[0]?.signature.fullSig; + if (!signature) { throw new Error('Fireblocks signature is missing'); } @@ -237,7 +238,8 @@ export class FireblocksService { const fbSigner = this.getSigner(integration); const fbNote = note ? note : 'DYDX tx from @kilnfi/sdk'; const fbTx = await fbSigner.sign(payload, 'DYDX_DYDX', fbNote); - const signature = fbTx.signedMessages?.[0].signature.fullSig; + const signature = fbTx.signedMessages?.[0]?.signature.fullSig; + if (!signature) { throw new Error('Fireblocks signature is missing'); } @@ -287,7 +289,8 @@ export class FireblocksService { const fbSigner = this.getSigner(integration); const fbNote = note ? note : 'FET tx from @kilnfi/sdk'; const fbTx = await fbSigner.sign(payload, undefined, fbNote); - const signature = fbTx.signedMessages?.[0].signature.fullSig; + const signature = fbTx.signedMessages?.[0]?.signature.fullSig; + if (!signature) { throw new Error('Fireblocks signature is missing'); } @@ -335,7 +338,8 @@ export class FireblocksService { const fbSigner = this.getSigner(integration); const fbNote = note ? note : 'INJ tx from @kilnfi/sdk'; const fbTx = await fbSigner.sign(payload, 'INJ_INJ', fbNote); - const signature = fbTx.signedMessages?.[0].signature.fullSig; + const signature = fbTx.signedMessages?.[0]?.signature.fullSig; + if (!signature) { throw new Error('Fireblocks signature is missing'); } @@ -383,7 +387,8 @@ export class FireblocksService { const fbSigner = this.getSigner(integration); const fbNote = note ? note : 'KAVA tx from @kilnfi/sdk'; const fbTx = await fbSigner.sign(payload, 'KAVA_KAVA', fbNote); - const signature = fbTx.signedMessages?.[0].signature.fullSig; + const signature = fbTx.signedMessages?.[0]?.signature.fullSig; + if (!signature) { throw new Error('Fireblocks signature is missing'); } @@ -431,7 +436,8 @@ export class FireblocksService { const fbSigner = this.getSigner(integration); const fbNote = note ? note : 'OSMO tx from @kilnfi/sdk'; const fbTx = await fbSigner.sign(payload, 'OSMO', fbNote); - const signature = fbTx.signedMessages?.[0].signature.fullSig; + const signature = fbTx.signedMessages?.[0]?.signature.fullSig; + if (!signature) { throw new Error('Fireblocks signature is missing'); } @@ -479,7 +485,8 @@ export class FireblocksService { const fbSigner = this.getSigner(integration); const fbNote = note ? note : 'TIA tx from @kilnfi/sdk'; const fbTx = await fbSigner.sign(payload, 'CELESTIA', fbNote); - const signature = fbTx.signedMessages?.[0].signature.fullSig; + const signature = fbTx.signedMessages?.[0]?.signature.fullSig; + if (!signature) { throw new Error('Fireblocks signature is missing'); } @@ -529,7 +536,8 @@ export class FireblocksService { const fbSigner = this.getSigner(integration); const fbNote = note ? note : 'ZETA tx from @kilnfi/sdk'; const fbTx = await fbSigner.sign(payload, undefined, fbNote); - const signature = fbTx.signedMessages?.[0].signature.fullSig; + const signature = fbTx.signedMessages?.[0]?.signature.fullSig; + if (!signature) { throw new Error('Fireblocks signature is missing'); } @@ -569,7 +577,7 @@ export class FireblocksService { const fbSigner = this.getSigner(integration); const fbNote = note ? note : 'DOT tx from @kilnfi/sdk'; const fbTx = await fbSigner.sign(payload, 'DOT', fbNote); - const signature = `0x00${fbTx.signedMessages?.[0].signature.fullSig}`; + const signature = `0x00${fbTx.signedMessages?.[0]?.signature.fullSig}`; const preparedTx = await this.client.POST('/v1/dot/transaction/prepare', { body: { @@ -604,7 +612,7 @@ export class FireblocksService { const fbSigner = this.getSigner(integration); const fbNote = note ? note : 'KSM tx from @kilnfi/sdk'; const fbTx = await fbSigner.sign(payload, 'KSM', fbNote); - const signature = `0x00${fbTx.signedMessages?.[0].signature.fullSig}`; + const signature = `0x00${fbTx.signedMessages?.[0]?.signature.fullSig}`; const preparedTx = await this.client.POST('/v1/ksm/transaction/prepare', { body: { @@ -650,12 +658,18 @@ export class FireblocksService { const fbNote = note ? note : 'ETH tx from @kilnfi/sdk'; const fbTx = await fbSigner.sign(payload, assetId, fbNote); + const signature = fbTx?.signedMessages?.[0]?.signature; + + if (!signature) { + throw new Error('Fireblocks signature is missing'); + } + const preparedTx = await this.client.POST('/v1/eth/transaction/prepare', { body: { unsigned_tx_serialized: tx.unsigned_tx_serialized, - r: `0x${fbTx?.signedMessages?.[0].signature.r}`, - s: `0x${fbTx?.signedMessages?.[0].signature.s}`, - v: fbTx?.signedMessages?.[0].signature.v ?? 0, + r: `0x${signature.r}`, + s: `0x${signature.s}`, + v: signature.v ?? 0, }, }); @@ -741,7 +755,11 @@ export class FireblocksService { const fbSigner = this.getSigner(integration); const fbNote = note ? note : 'TON tx from @kilnfi/sdk'; const fbTx = await fbSigner.sign(payload, assetId, fbNote); - const signature = fbTx.signedMessages?.[0].signature.fullSig; + const signature = fbTx.signedMessages?.[0]?.signature.fullSig; + + if (!signature) { + throw new Error('Fireblocks signature is missing'); + } const preparedTx = await this.client.POST('/v1/ton/transaction/prepare', { body: { @@ -783,7 +801,7 @@ export class FireblocksService { const fbSigner = this.getSigner(integration); const fbNote = note ? note : 'XTZ tx from @kilnfi/sdk'; const fbTx = await fbSigner.sign(payload, assetId, fbNote); - const signature = fbTx.signedMessages?.[0].signature.fullSig; + const signature = fbTx.signedMessages?.[0]?.signature.fullSig; if (!signature) { throw new Error('Fireblocks signature is missing'); @@ -828,7 +846,7 @@ export class FireblocksService { const fbSigner = this.getSigner(integration); const fbNote = note ? note : 'NEAR tx from @kilnfi/sdk'; const fbTx = await fbSigner.sign(payload, assetId, fbNote); - const signature = fbTx.signedMessages?.[0].signature.fullSig; + const signature = fbTx.signedMessages?.[0]?.signature.fullSig; if (!signature) { throw new Error('Fireblocks signature is missing'); diff --git a/src/fireblocks_signer.ts b/src/fireblocks_signer.ts index c6f8ed1..4b79c6f 100644 --- a/src/fireblocks_signer.ts +++ b/src/fireblocks_signer.ts @@ -9,7 +9,7 @@ import { } from 'fireblocks-sdk'; import { formatEther, formatUnits } from 'viem'; -import type { components } from './openapi/schema'; +import type { components } from './openapi/schema.js'; export type AssetId = | 'SOL_TEST' @@ -82,7 +82,7 @@ export class FireblocksSigner { * @param assetId fireblocks asset id * @param note optional fireblocks custom note */ - public async sign(payloadToSign: object, assetId?: AssetId, note?: string): Promise { + public async sign(payloadToSign: object, assetId?: AssetId, note = ''): Promise { try { const assetArgs = assetId ? { @@ -116,7 +116,7 @@ export class FireblocksSigner { public async signTypedMessage( eip712message: object, assetId: 'ETH' | 'ETH_TEST5' | 'ETH_TEST6', - note?: string, + note = '', ): Promise { try { const tx: TransactionArguments = { @@ -160,7 +160,7 @@ export class FireblocksSigner { tx: components['schemas']['ETHUnsignedTx'] | components['schemas']['POLUnsignedTx'], destinationId: string, sendAmount = true, - note?: string, + note = '', ): Promise { try { const txArgs: TransactionArguments = { diff --git a/src/kiln.ts b/src/kiln.ts index 4c0b4d7..3978192 100644 --- a/src/kiln.ts +++ b/src/kiln.ts @@ -1,9 +1,9 @@ -import type { paths } from './openapi/schema'; -export * from './validators'; -export * from './openapi/schema'; -export * from './utils'; +import type { paths } from './openapi/schema.js'; +export * from './validators.js'; +export * from './openapi/schema.js'; +export * from './utils.js'; import createClient, { type Client } from 'openapi-fetch'; -import { FireblocksService } from './fireblocks'; +import { FireblocksService } from './fireblocks.js'; type Config = { baseUrl: string; diff --git a/src/utils.ts b/src/utils.ts index b7f9454..811f31b 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,6 +1,6 @@ import { formatEther, formatUnits, parseUnits } from 'viem'; -// Convert Ethereum wei to ETH +// Convert wei to ETH export const weiToEth = (wei: bigint): string => { return formatEther(BigInt(wei)); }; @@ -10,7 +10,7 @@ export const weiToPol = (wei: bigint): string => { return formatUnits(wei, 18); }; -// Convert Solana lamports to SOL +// Convert lamports to SOL export const lamportsToSol = (lamports: bigint): string => { return formatUnits(lamports, 9); }; @@ -30,44 +30,52 @@ export const lovelaceToAda = (lovelace: bigint): string => { return formatUnits(lovelace, 6); }; -// Converts Near yocto to NEAR +// Converts yocto to NEAR export const yoctoToNear = (yocto: bigint): string => { return formatUnits(yocto, 24); }; +// Convert nanoTON to TON export const nanotonToTon = (nanoton: bigint): string => { return formatUnits(nanoton, 9); }; +// Convert uZETA to ZETA export const uzetaToZeta = (uzeta: bigint): string => { return formatUnits(uzeta, 6); }; +// Convert uINJ to INJ export const uinjToInj = (uinj: bigint): string => { return formatUnits(uinj, 6); }; +// Convert aFET to FET export const afetToFet = (afet: bigint): string => { return formatUnits(afet, 18); }; +// Convert uFET to FET export const ufetToFet = (ufet: bigint): string => { return formatUnits(ufet, 6); }; -// Convert Tezos micro tez (mutez) to XTZ +// Convert mutez to XTZ export const mutezToXtz = (mutez: bigint): string => { return formatUnits(mutez, 6); }; +// Convert uDYDX to DYDX export const udydxToDydx = (udydx: bigint): string => { return formatUnits(udydx, 6); }; +// Convert planck to DOT export const planckToDot = (planck: bigint): string => { return formatUnits(planck, 10); }; +// Convert planck to KSM export const planckToKsm = (planck: bigint): string => { return formatUnits(planck, 12); }; @@ -77,6 +85,7 @@ export const uatomToAtom = (uatom: bigint): string => { return formatUnits(uatom, 6); }; +// Convert u{Cosmos chain token: ATOM, OSMO, etc...} to {Cosmos chain token: ATOM, OSMO, etc...} export const uunitToUnit = (uunit: bigint): string => { return formatUnits(uunit, 6); }; diff --git a/tsconfig.base.json b/tsconfig.base.json new file mode 100644 index 0000000..d8f7605 --- /dev/null +++ b/tsconfig.base.json @@ -0,0 +1,35 @@ +{ + // This tsconfig file contains the shared config for the build (tsconfig.build.json) and type checking (tsconfig.json) config. + "include": [], + "compilerOptions": { + // Type checking + "strict": true, + "useDefineForClassFields": true, // Not enabled by default in `strict` mode unless we bump `target` to ES2022. + "noFallthroughCasesInSwitch": true, // Not enabled by default in `strict` mode. + "noImplicitReturns": true, // Not enabled by default in `strict` mode. + "noImplicitOverride": true, // Not enabled by default in `strict` mode. + "noUnusedLocals": true, // Not enabled by default in `strict` mode. + "noUnusedParameters": true, // Not enabled by default in `strict` mode. + "exactOptionalPropertyTypes": true, + "noUncheckedIndexedAccess": true, + + // JavaScript support + "allowJs": false, + "checkJs": false, + + // Interop constraints + "esModuleInterop": false, + "allowSyntheticDefaultImports": false, + "forceConsistentCasingInFileNames": true, + "verbatimModuleSyntax": true, + + // Language and environment + "moduleResolution": "NodeNext", + "module": "NodeNext", + "target": "ES2021", + "lib": ["ES2022", "DOM"], + + // Skip type checking for node modules + "skipLibCheck": true + } +} diff --git a/tsconfig.build.json b/tsconfig.build.json new file mode 100644 index 0000000..51a5ea3 --- /dev/null +++ b/tsconfig.build.json @@ -0,0 +1,10 @@ +{ + // This file is used to compile the for cjs and esm (see package.json build scripts). It should exclude all test files. + "extends": "./tsconfig.base.json", + "include": ["src"], + "compilerOptions": { + "moduleResolution": "node", + "sourceMap": true, + "rootDir": "./src" + } +} diff --git a/tsconfig.json b/tsconfig.json index 52e2785..831e92f 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,19 +1,9 @@ { - "compilerOptions": { - "module": "ES2020", - "target": "ES2021", - "declaration": true, - "outDir": "./lib", - "strict": true, - "esModuleInterop": true, - "allowSyntheticDefaultImports": true, - "forceConsistentCasingInFileNames": true, - "verbatimModuleSyntax": false, - "moduleResolution": "node", - "skipLibCheck": true, - "sourceMap": true, - "rootDir": "src" - }, + // This configuration is used for local development and type checking. + "extends": "./tsconfig.base.json", "include": ["src"], - "exclude": ["node_modules", "**/__tests__/*", "examples"] + "exclude": [], + "compilerOptions": { + "baseUrl": "." + } }