diff --git a/.gitignore b/.gitignore index 89c5cbdd..99338e82 100644 --- a/.gitignore +++ b/.gitignore @@ -29,5 +29,8 @@ subgraph.yaml .latest.json .docker +# SDK +lib + # Misc .DS_Store diff --git a/sdk/.npmignore b/sdk/.npmignore new file mode 100644 index 00000000..579fc838 --- /dev/null +++ b/sdk/.npmignore @@ -0,0 +1,14 @@ +.graphclient +!dist/.graphclient +examples +node_modules +src +!dist/src +test +.env +.env.example +.graphclientrc.yml +.npmignore +babel-jest.config.cjs +jest.config.js +tsconfig.json diff --git a/sdk/LICENSE b/sdk/LICENSE new file mode 100644 index 00000000..3f498aa4 --- /dev/null +++ b/sdk/LICENSE @@ -0,0 +1,7 @@ +Copyright 2023 ConsenSys Software, Inc + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/sdk/package.json b/sdk/package.json index 83b98646..59130503 100644 --- a/sdk/package.json +++ b/sdk/package.json @@ -1,6 +1,6 @@ { - "name": "linea-attestation-registry-sdk", - "version": "0.0.1", + "name": "@verax-attestation-registry/verax-sdk", + "version": "0.0.6", "description": "Verax Attestation Registry SDK to interact with the subgraph and the contracts", "keywords": [ "linea-attestation-registry", @@ -15,8 +15,31 @@ "license": "MIT", "author": "Consensys", "type": "module", + "files": [ + "lib/**/*" + ], + "types": "./lib/cjs/types/index.d.ts", + "main": "./lib/cjs/index.js", + "exports": { + ".": { + "import": { + "types": "./lib/esm/types/src/VeraxSdk.d.ts", + "default": "./lib/esm/src/VeraxSdk.js" + }, + "require": { + "types": "./lib/cjs/types/src/VeraxSdk.d.ts", + "default": "./lib/cjs/src/VeraxSdk.js" + } + } + }, "scripts": { "attestation": "ts-node examples/attestation/index.ts", + "clean": "rm -rf ./lib", + "build": "pnpm run clean && pnpm run build:esm && pnpm run build:cjs", + "build:esm": "tsc -p ./tsconfig.esm.json", + "build:cjs": "tsc -p ./tsconfig.cjs.json", + "prepack": "npm run build", + "publish:public": "pnpm publish --access public --no-git-checks", "module": "ts-node examples/module/index.ts", "portal": "ts-node examples/portal/index.ts", "schema": "ts-node examples/schema/index.ts", diff --git a/sdk/src/VeraxSdk.ts b/sdk/src/VeraxSdk.ts index 6c77f2dd..42ce5ca9 100644 --- a/sdk/src/VeraxSdk.ts +++ b/sdk/src/VeraxSdk.ts @@ -3,14 +3,24 @@ import AttestationDataMapper from "./dataMapper/AttestationDataMapper"; import SchemaDataMapper from "./dataMapper/SchemaDataMapper"; import ModuleDataMapper from "./dataMapper/ModuleDataMapper"; import PortalDataMapper from "./dataMapper/PortalDataMapper"; -import { createPublicClient, createWalletClient, custom, Hex, http, PublicClient, WalletClient } from "viem"; +import { Address, createPublicClient, createWalletClient, custom, Hex, http, PublicClient, WalletClient } from "viem"; import UtilsDataMapper from "./dataMapper/UtilsDataMapper"; -import { privateKeyToAccount } from "viem/accounts"; -import dotenv from "dotenv"; +import { PrivateKeyAccount, privateKeyToAccount } from "viem/accounts"; import { Conf } from "./types"; import { SDKMode } from "./utils/constants"; -dotenv.config({ path: "./.env" }); +let account: PrivateKeyAccount | Address; + +if (typeof window === "undefined") { + import("dotenv").then((dotenv) => { + dotenv.config({ path: "./.env" }); + account = privateKeyToAccount(process.env.PRIVATE_KEY as Hex); + }); +} else { + window.ethereum.request({ method: "eth_requestAccounts" }).then((result: Address[]) => { + account = result[0]; + }); +} export default class VeraxSdk { static DEFAULT_LINEA_MAINNET: Conf = { @@ -62,11 +72,12 @@ export default class VeraxSdk { conf.mode === SDKMode.BACKEND ? createWalletClient({ chain: conf.chain, - account: privateKeyToAccount(process.env.PRIVATE_KEY as Hex), + account, transport: http(), }) : createWalletClient({ chain: conf.chain, + account, transport: custom(window.ethereum), }); diff --git a/sdk/tsconfig.cjs.json b/sdk/tsconfig.cjs.json new file mode 100644 index 00000000..aa783b68 --- /dev/null +++ b/sdk/tsconfig.cjs.json @@ -0,0 +1,12 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "lib": ["ES6", "DOM"], + "target": "ES6", + "module": "CommonJS", + "moduleResolution": "Node", + "outDir": "./lib/cjs", + "declarationDir": "./lib/cjs/types" + }, + "include": ["src/**/*.ts", ".graphclient/**/*.ts"] +} diff --git a/sdk/tsconfig.esm.json b/sdk/tsconfig.esm.json new file mode 100644 index 00000000..b4974dda --- /dev/null +++ b/sdk/tsconfig.esm.json @@ -0,0 +1,11 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "lib": ["ESNext", "DOM"], + "module": "ESNext", + "moduleResolution": "Bundler", + "outDir": "./lib/esm", + "declarationDir": "./lib/esm/types" + }, + "include": ["src/**/*.ts", ".graphclient/**/*.ts"] +} diff --git a/sdk/tsconfig.json b/sdk/tsconfig.json index c27acc60..cf943c9d 100644 --- a/sdk/tsconfig.json +++ b/sdk/tsconfig.json @@ -1,8 +1,17 @@ { - "extends": "../tsconfig.json", "compilerOptions": { - "module": "ESNext", - "target": "ESNext" + "strict": true, + "esModuleInterop": true, + "forceConsistentCasingInFileNames": true, + "skipLibCheck": true, + "checkJs": true, + "allowJs": true, + "declaration": true, + "declarationMap": true, + "allowSyntheticDefaultImports": true, + "outDir": "./lib", + "sourceMap": true, + "noEmit": false }, "ts-node": { "esm": true,