Skip to content

Commit

Permalink
add ESM + CJS builds (#133)
Browse files Browse the repository at this point in the history
  • Loading branch information
LeTamanoir authored Oct 16, 2024
1 parent 55b0b38 commit 0736979
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 53 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.DS_Store
node_modules/
lib/
build/
.idea/
17 changes: 12 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
{
"name": "@kilnfi/sdk",
"version": "3.1.0",
"version": "3.1.1",
"autor": "Kiln <[email protected]> (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",
Expand Down
54 changes: 36 additions & 18 deletions src/fireblocks.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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');
}
Expand Down Expand Up @@ -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');
}
Expand Down Expand Up @@ -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');
}
Expand Down Expand Up @@ -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');
}
Expand Down Expand Up @@ -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');
}
Expand Down Expand Up @@ -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');
}
Expand Down Expand Up @@ -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');
}
Expand Down Expand Up @@ -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');
}
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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,
},
});

Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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');
Expand Down
8 changes: 4 additions & 4 deletions src/fireblocks_signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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<TransactionResponse> {
public async sign(payloadToSign: object, assetId?: AssetId, note = ''): Promise<TransactionResponse> {
try {
const assetArgs = assetId
? {
Expand Down Expand Up @@ -116,7 +116,7 @@ export class FireblocksSigner {
public async signTypedMessage(
eip712message: object,
assetId: 'ETH' | 'ETH_TEST5' | 'ETH_TEST6',
note?: string,
note = '',
): Promise<TransactionResponse> {
try {
const tx: TransactionArguments = {
Expand Down Expand Up @@ -160,7 +160,7 @@ export class FireblocksSigner {
tx: components['schemas']['ETHUnsignedTx'] | components['schemas']['POLUnsignedTx'],
destinationId: string,
sendAmount = true,
note?: string,
note = '',
): Promise<TransactionResponse> {
try {
const txArgs: TransactionArguments = {
Expand Down
10 changes: 5 additions & 5 deletions src/kiln.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
17 changes: 13 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -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));
};
Expand All @@ -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);
};
Expand All @@ -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);
};
Expand All @@ -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);
};
Expand Down
35 changes: 35 additions & 0 deletions tsconfig.base.json
Original file line number Diff line number Diff line change
@@ -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
}
}
10 changes: 10 additions & 0 deletions tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
Loading

0 comments on commit 0736979

Please sign in to comment.