@otim/sdk-server 0.0.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.
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.mjs","names":["Environment","exhaustiveCheck: never","config: ServerAccountConfig","AuthClient","ConfigClient","DelegationClient","OrchestrationClient"],"sources":["../src/client/api-client.ts","../src/client/utils/asserts.ts","../src/client/server-account.ts","../src/client/server-client.ts","../src/client/create-server-client.ts"],"sourcesContent":["import type { CreateInstanceParameters } from \"@otim/utils/api\";\nimport type { Optional } from \"@otim/utils/helpers\";\n\nimport { Environment, getApiUrl } from \"@otim/sdk-core/config\";\nimport { APIClient, createInstance } from \"@otim/utils/api\";\n\nexport interface CreateServerAPIClientOptions\n extends Omit<CreateInstanceParameters, \"baseURL\"> {\n sessionToken?: Optional<string>;\n environment?: Environment;\n}\n\n/**\n * Creates an API client for server-side requests.\n *\n * Configures the base URL and authentication headers for API communication.\n *\n * @param config - Optional configuration for the API client\n * @returns Configured API client instance\n *\n * @internal\n */\nexport const createServerAPIClient = (\n config?: CreateServerAPIClientOptions,\n) => {\n const {\n sessionToken,\n environment = Environment.Sandbox,\n ...restConfig\n } = config ?? {};\n\n const instance = createInstance({\n baseURL: getApiUrl(environment),\n ...restConfig,\n });\n\n instance.interceptors.request.use(async (requestConfig) => {\n if (sessionToken) {\n requestConfig.headers = requestConfig.headers ?? {};\n\n // If the session token does not start with \"Bearer \", add it.\n const authorizationToken = sessionToken.startsWith(\"Bearer \")\n ? sessionToken\n : `Bearer ${sessionToken}`;\n\n requestConfig.headers.Authorization = authorizationToken;\n }\n\n return requestConfig;\n });\n\n return new APIClient({ instance });\n};\n","export function assertDefined<T>(\n value: T,\n errorMessage: string,\n): asserts value is NonNullable<T> {\n if (value === null || value === undefined) {\n throw new Error(errorMessage);\n }\n}\n","import type {\n ApiAccountConfig,\n OtimAccountSignMessageArgs,\n OtimAccount as OtimAccountType,\n PrivateKeyAccountConfig,\n ServerAccountConfig,\n} from \"@otim/sdk-core/account\";\nimport type { Nullable } from \"@otim/utils/helpers\";\nimport type { Hex } from \"viem\";\n\nimport { ServerAccountType } from \"@otim/sdk-core/account\";\nimport { env, getTurnkeyApiUrl } from \"@otim/sdk-core/config\";\nimport { ApiKeyStamper } from \"@turnkey/api-key-stamper\";\nimport { TurnkeyClient } from \"@turnkey/http\";\nimport { createAccount } from \"@turnkey/viem\";\nimport { privateKeyToAccount } from \"viem/accounts\";\n\nimport { assertDefined } from \"./utils/asserts\";\n\ntype PrimitiveAccount = {\n signMessage: (args: {\n message: string | { raw: Hex | Uint8Array };\n }) => Promise<Hex>;\n};\n\nconst createApiAccount = async (\n config: ApiAccountConfig,\n): Promise<PrimitiveAccount> => {\n const stamper = new ApiKeyStamper({\n apiPublicKey: config.publicKey,\n apiPrivateKey: config.privateKey,\n });\n\n const client = new TurnkeyClient(\n {\n baseUrl: getTurnkeyApiUrl(env.ENVIRONMENT),\n },\n stamper,\n );\n\n const account = await createAccount({\n organizationId: config.appId,\n signWith: \"0xB54c8E6f303627f392884412ED2C84fFaF4779CA\",\n client,\n });\n\n return {\n signMessage: (args) => account.signMessage(args),\n };\n};\n\nconst createPrivateKeyAccount = (\n config: PrivateKeyAccountConfig,\n): PrimitiveAccount => {\n const account = privateKeyToAccount(config.privateKey);\n\n return {\n signMessage: (args) => account.signMessage(args),\n };\n};\n\nconst initializeAccount = async (\n config: ServerAccountConfig,\n): Promise<PrimitiveAccount> => {\n switch (config.type) {\n case ServerAccountType.Api:\n return createApiAccount(config);\n case ServerAccountType.PrivateKey:\n return createPrivateKeyAccount(config);\n default: {\n const exhaustiveCheck: never = config;\n throw new Error(\n `Unsupported account type: \"${(exhaustiveCheck as ServerAccountConfig).type}\". Supported types are: ${Object.values(ServerAccountType).join(\", \")}`,\n );\n }\n }\n};\n\n/**\n * Server-side account implementation for signing operations.\n *\n * Supports both private key and API authentication methods.\n *\n * @internal\n */\nexport class OtimAccount implements OtimAccountType {\n private account: Nullable<PrimitiveAccount> = null;\n private initialized = false;\n\n constructor(private readonly config: ServerAccountConfig) {}\n\n async initialize(): Promise<void> {\n if (this.initialized) {\n throw new Error(\n \"Account already initialized. The initialize() method should only be called once.\",\n );\n }\n\n this.account = await initializeAccount(this.config);\n this.initialized = true;\n }\n\n async signMessage({ message }: OtimAccountSignMessageArgs) {\n assertDefined(\n this.account,\n \"Account not initialized. Call initialize() before using signMessage().\",\n );\n return this.account.signMessage({ message });\n }\n}\n","import type { ServerAccountConfig } from \"@otim/sdk-core/account\";\nimport type { OtimServerClientContext } from \"@otim/sdk-core/context\";\nimport type { APIClient } from \"@otim/utils/api\";\n\nimport {\n createClientContext,\n isApiAccountConfig,\n} from \"@otim/sdk-core/account\";\nimport {\n AuthClient,\n ConfigClient,\n DelegationClient,\n OrchestrationClient,\n} from \"@otim/sdk-core/clients\";\n\nimport { createServerAPIClient } from \"./api-client\";\nimport { OtimAccount } from \"./server-account\";\n\n/**\n * Otim Client for server-side blockchain operations.\n *\n * Provides access to authentication, configuration, delegation, and\n * orchestration services.\n * The client must be initialized before use by calling the init() method.\n\n */\nexport class OtimServerClient {\n private readonly apiClient: APIClient;\n private readonly account: OtimAccount;\n private readonly context: OtimServerClientContext;\n\n readonly auth: AuthClient;\n readonly config: ConfigClient;\n readonly delegation: DelegationClient;\n readonly orchestration: OrchestrationClient;\n\n constructor(config: ServerAccountConfig) {\n this.context = createClientContext(config);\n this.apiClient = createServerAPIClient({\n environment: config.environment,\n ...(isApiAccountConfig(config) ? { sessionToken: config.apiKey } : {}),\n });\n\n this.account = new OtimAccount(config);\n\n this.auth = new AuthClient(this.apiClient, this.account, this.context);\n this.config = new ConfigClient(this.apiClient);\n this.delegation = new DelegationClient(this.apiClient);\n this.orchestration = new OrchestrationClient(\n this.apiClient,\n this.account,\n this.context,\n );\n }\n\n /**\n * Initializes the Otim Client.\n *\n * This method must be called before using any other client methods.\n * It sets up the authentication account and prepares the client for use.\n *\n * @throws {Error} If the account is already initialized\n */\n async init(): Promise<void> {\n await this.account.initialize();\n }\n}\n","import type { Environment } from \"@otim/sdk-core/config\";\n\nimport { ServerAccountType } from \"@otim/sdk-core/account\";\n\nimport { OtimServerClient } from \"./server-client\";\n\n/**\n * Creates an Otim Client for server-side blockchain operations.\n *\n * @param config - Configuration with private key only\n * @returns Configured Otim Client instance\n *\n * @example\n * ```typescript\n * import { Environment } from '@otim/sdk';\n *\n * const client = createOtimServerClient({\n * privateKey: '0x...',\n * environment: Environment.Production,\n * });\n * await client.init();\n * ```\n */\nexport function createOtimServerClient(config: {\n privateKey: `0x${string}`;\n environment?: Environment;\n}): OtimServerClient;\n\n/**\n * Creates an Otim Client for server-side operations with API authentication.\n *\n * @param config - Configuration with API credentials\n * @returns Configured Otim Client instance\n *\n * @example\n * ```typescript\n * import { Environment } from '@otim/sdk';\n *\n * const client = createOtimServerClient({\n * appId: 'your-app-id',\n * privateKey: '0x...',\n * publicKey: 'your-public-key',\n * apiKey: 'your-api-key',\n * environment: Environment.Production,\n * });\n * await client.init();\n * ```\n */\nexport function createOtimServerClient(config: {\n appId: string;\n privateKey: `0x${string}`;\n publicKey: string;\n apiKey?: string;\n environment?: Environment;\n}): OtimServerClient;\n\nexport function createOtimServerClient(config: {\n privateKey?: `0x${string}`;\n publicKey?: string;\n apiKey?: string;\n appId?: string;\n environment?: Environment;\n}): OtimServerClient {\n if (config.appId && config.privateKey && config.publicKey && config.apiKey) {\n return new OtimServerClient({\n type: ServerAccountType.Api,\n appId: config.appId,\n privateKey: config.privateKey,\n publicKey: config.publicKey,\n apiKey: config.apiKey,\n environment: config.environment,\n });\n }\n\n if (config.privateKey) {\n return new OtimServerClient({\n type: ServerAccountType.PrivateKey,\n privateKey: config.privateKey,\n environment: config.environment,\n });\n }\n\n throw new Error(\"Invalid Otim Server Client configuration\");\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAsBA,MAAa,yBACX,WACG;CACH,MAAM,EACJ,cACA,cAAcA,cAAY,SAC1B,GAAG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AAkB;EACxB,YAAY,OAAO;EACnB,aAAa,OAAO;EACrB,CAAC;AAGJ,OAAM,IAAI,MAAM,2CAA2C"}
package/package.json ADDED
@@ -0,0 +1,117 @@
1
+ {
2
+ "name": "@otim/sdk-server",
3
+ "version": "0.0.1",
4
+ "description": "Otim's TypeScript SDK for blockchain automation and smart contract interactions",
5
+ "type": "module",
6
+ "main": "./dist/index.cjs",
7
+ "module": "./dist/index.mjs",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "import": {
12
+ "types": "./dist/index.d.mts",
13
+ "default": "./dist/index.mjs"
14
+ },
15
+ "require": {
16
+ "types": "./dist/index.d.cts",
17
+ "default": "./dist/index.cjs"
18
+ }
19
+ },
20
+ "./package.json": "./package.json"
21
+ },
22
+ "files": [
23
+ "dist",
24
+ "README.md",
25
+ "LICENSE"
26
+ ],
27
+ "sideEffects": false,
28
+ "keywords": [
29
+ "otim",
30
+ "sdk",
31
+ "ethereum",
32
+ "blockchain",
33
+ "defi",
34
+ "smart-contracts",
35
+ "typescript"
36
+ ],
37
+ "license": "Apache-2.0",
38
+ "repository": {
39
+ "type": "git",
40
+ "url": "https://github.com/otimlabs/otim-ts-sdk.git",
41
+ "directory": "packages/server"
42
+ },
43
+ "bugs": {
44
+ "url": "https://github.com/otimlabs/otim-ts-sdk/issues"
45
+ },
46
+ "homepage": "https://github.com/otimlabs/otim-ts-sdk#readme",
47
+ "devDependencies": {
48
+ "@arethetypeswrong/cli": "^0.18.2",
49
+ "@playwright/test": "^1.56.1",
50
+ "@types/adm-zip": "^0.5.7",
51
+ "@types/node": "^24.10.1",
52
+ "@types/node-fetch": "^2.6.13",
53
+ "adm-zip": "^0.5.16",
54
+ "dotenv": "^17.2.3",
55
+ "eslint": "^9.39.1",
56
+ "msw": "^2.12.2",
57
+ "node-fetch": "^3.3.2",
58
+ "tsdown": "^0.16.5",
59
+ "typescript-eslint": "^8.47.0",
60
+ "vitest": "^4.0.10",
61
+ "@otim/eslint-config": "0.0.1",
62
+ "@otim/typescript-config": "0.0.0"
63
+ },
64
+ "dependencies": {
65
+ "@otim/turnkey": "0.0.2-development.0",
66
+ "@otim/utils": "0.0.2-development.0",
67
+ "@t3-oss/env-core": "^0.13.8",
68
+ "@testing-library/user-event": "^14.6.1",
69
+ "@turnkey/api-key-stamper": "^0.5.0",
70
+ "@turnkey/core": "^1.7.0",
71
+ "@turnkey/http": "^3.15.0",
72
+ "@turnkey/sdk-server": "^4.12.0",
73
+ "@turnkey/viem": "^0.14.13",
74
+ "@wagmi/connectors": "^7.0.0",
75
+ "@wagmi/core": "^3.0.0",
76
+ "abitype": "^1.1.2",
77
+ "axios": "^1.13.2",
78
+ "viem": "^2.39.3",
79
+ "ws": "^8.18.3",
80
+ "zod": "^4.1.12",
81
+ "@otim/sdk-core": "0.0.1"
82
+ },
83
+ "peerDependencies": {
84
+ "@wagmi/core": "2.x",
85
+ "typescript": ">=5.0.4",
86
+ "viem": "2.x"
87
+ },
88
+ "peerDependenciesMeta": {
89
+ "@wagmi/core": {
90
+ "optional": false
91
+ },
92
+ "viem": {
93
+ "optional": false
94
+ }
95
+ },
96
+ "engines": {
97
+ "node": ">=18.0.0"
98
+ },
99
+ "scripts": {
100
+ "build": "tsdown",
101
+ "dev": "tsdown --watch",
102
+ "dev:clear": "tsc --watch --pretty --preserveWatchOutput",
103
+ "lint": "eslint .",
104
+ "lint:check": "eslint .",
105
+ "lint:fix": "eslint . --fix",
106
+ "check-types": "tsc --noEmit",
107
+ "check-exports": "attw --pack . --format=table --ignore-rules=no-resolution untyped-resolution fallback-condition || true",
108
+ "clean": "rm -rf dist",
109
+ "ci": "pnpm run lint:check && pnpm run check-types && pnpm run build && pnpm run check-exports",
110
+ "test": "vitest run",
111
+ "test:watch": "vitest --watch",
112
+ "test:coverage": "vitest run --coverage",
113
+ "test:e2e": "playwright test",
114
+ "e2e": "playwright test",
115
+ "e2e:ui": "playwright test --ui"
116
+ }
117
+ }