diff --git a/.gitignore b/.gitignore index 7870e9df3..b2081cbf9 100644 --- a/.gitignore +++ b/.gitignore @@ -19,6 +19,7 @@ node_modules # lockfiles package-lock.json yarn.lock +examples/*/pnpm-lock.yaml # debug .pnpm-debug.log* diff --git a/biome.json b/biome.json index abeea31b8..08afed512 100644 --- a/biome.json +++ b/biome.json @@ -67,6 +67,16 @@ "organizeImports": { "enabled": false } + }, + { + "include": ["./examples/**/*.ts"], + "linter": { + "rules": { + "style": { + "noNonNullAssertion": "off" + } + } + } } ] } diff --git a/examples/viem-onboarding/README.md b/examples/viem-onboarding/README.md new file mode 100644 index 000000000..210c2794c --- /dev/null +++ b/examples/viem-onboarding/README.md @@ -0,0 +1,3 @@ +# Viem Onboarding Example + +[![Open in StackBlitz](https://developer.stackblitz.com/img/open_in_stackblitz.svg)](https://stackblitz.com/github/lens-protocol/lens-sdk/tree/next/examples/viem_eip-1193) diff --git a/examples/viem-onboarding/index.html b/examples/viem-onboarding/index.html new file mode 100644 index 000000000..172ada1f2 --- /dev/null +++ b/examples/viem-onboarding/index.html @@ -0,0 +1,17 @@ + + + + + + + +

Viem EIP-1193 Example

+
Loading...
+ + + diff --git a/examples/viem-onboarding/index.ts b/examples/viem-onboarding/index.ts new file mode 100644 index 000000000..64ab6e6c8 --- /dev/null +++ b/examples/viem-onboarding/index.ts @@ -0,0 +1,70 @@ +import 'viem/window'; + +import { chains } from '@lens-network/sdk/viem'; +import { PublicClient, testnet as protocolTestnet } from '@lens-protocol/client'; +import { createAccountWithUsername, fetchAccount } from '@lens-protocol/client/actions'; +import { handleWith } from '@lens-protocol/client/viem'; +import { account } from '@lens-protocol/metadata'; +import { StorageClient, testnet as storageTestnet } from '@lens-protocol/storage-node-client'; +import { type Address, createWalletClient, custom } from 'viem'; + +const chain = chains.testnet; + +// hoist account +const [address] = (await window.ethereum!.request({ method: 'eth_requestAccounts' })) as [Address]; + +const walletClient = createWalletClient({ + account: address, + chain, + transport: custom(window.ethereum!), +}); + +const client = PublicClient.create({ + environment: protocolTestnet, +}); + +const sessionClient = await client + .login({ + onboardingUser: { + wallet: walletClient.account.address, + app: '0xe5439696f4057aF073c0FB2dc6e5e755392922e1', + }, + signMessage: async (message) => walletClient.signMessage({ message }), + }) + .match( + (result) => result, + (error) => { + throw error; + }, + ); + +const storageClient = StorageClient.create(storageTestnet); + +const metadata = account({ + name: 'John Doe', +}); + +const { uri } = await storageClient.uploadFile( + new File([JSON.stringify(metadata)], 'metadata.json', { type: 'application/json' }), +); + +const created = await createAccountWithUsername(sessionClient, { + metadataUri: uri, + username: { + localName: `john.doe.${Date.now()}`, + }, +}) + .andThen(handleWith(walletClient)) + .andThen(sessionClient.waitForTransaction) + .andThen((txHash) => fetchAccount(sessionClient, { txHash })) + .match( + (result) => result, + (error) => { + throw error; + }, + ); + +export default [ + `

${created?.username?.value}

`, + `

Address: ${await created?.address}

`, +]; diff --git a/examples/viem-onboarding/package.json b/examples/viem-onboarding/package.json new file mode 100644 index 000000000..ffb6367e0 --- /dev/null +++ b/examples/viem-onboarding/package.json @@ -0,0 +1,19 @@ +{ + "name": "example-viem-onboarding", + "private": true, + "type": "module", + "scripts": { + "dev": "vite" + }, + "dependencies": { + "@lens-network/sdk": "canary", + "@lens-protocol/client": "canary", + "@lens-protocol/metadata": "next", + "@lens-protocol/storage-node-client": "next", + "viem": "^2.21.55" + }, + "devDependencies": { + "typescript": "^5.6.3", + "vite": "^5.4.11" + } +} diff --git a/examples/viem-onboarding/tsconfig.json b/examples/viem-onboarding/tsconfig.json new file mode 100644 index 000000000..6da89b87d --- /dev/null +++ b/examples/viem-onboarding/tsconfig.json @@ -0,0 +1,19 @@ +{ + "compilerOptions": { + "target": "ESNext", + "useDefineForClassFields": true, + "lib": ["ESNext", "DOM"], + "module": "ESNext", + "moduleResolution": "Bundler", + "strict": true, + "resolveJsonModule": true, + "isolatedModules": true, + "esModuleInterop": true, + "noEmit": true, + "noUnusedLocals": true, + "noUnusedParameters": true, + "noImplicitReturns": true, + "skipLibCheck": true + }, + "include": ["./"] +} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 584f184a7..77f72d5af 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -841,10 +841,22 @@ packages: resolution: {integrity: sha512-TlaHRXDehJuRNR9TfZDNQ45mMEd5dwUwmicsafcIX4SsNiqnCHKjE/1alYPd/lDRVhxdhUAlv8uEhMCI5zjIJQ==} engines: {node: ^14.21.3 || >=16} + '@noble/curves@1.7.0': + resolution: {integrity: sha512-UTMhXK9SeDhFJVrHeUJ5uZlI6ajXg10O6Ddocf9S6GjbSBVZsJo88HzKwXznNfGpMTRDyJkqMjNDPYgf0qFWnw==} + engines: {node: ^14.21.3 || >=16} + '@noble/hashes@1.5.0': resolution: {integrity: sha512-1j6kQFb7QRru7eKN3ZDvRcP13rugwdxZqCjbiAVZfIJwgj2A65UmT4TgARXGlXgnRkORLTDTrO19ZErt7+QXgA==} engines: {node: ^14.21.3 || >=16} + '@noble/hashes@1.6.0': + resolution: {integrity: sha512-YUULf0Uk4/mAA89w+k3+yUYh6NrEvxZa5T6SY3wlMvE2chHkxFUUIDI8/XW1QSC357iA5pSnqt7XEhvFOqmDyQ==} + engines: {node: ^14.21.3 || >=16} + + '@noble/hashes@1.6.1': + resolution: {integrity: sha512-pq5D8h10hHBjyqX+cfBm0i8JUXJ0UhczFc4r74zbuT9XgewFo2E3J1cOaGtdZynILNmQ685YWGzGE1Zv6io50w==} + engines: {node: ^14.21.3 || >=16} + '@nodelib/fs.scandir@2.1.5': resolution: {integrity: sha512-vq24Bq3ym5HEQm2NKCr3yXDwjc7vTsEThRDnkp2DK9p1uqLR+DHurm/NOTo0KG7HYHU7eppKZj3MyqYuMBf62g==} engines: {node: '>= 8'} @@ -1044,12 +1056,21 @@ packages: '@scure/base@1.1.9': resolution: {integrity: sha512-8YKhl8GHiNI/pU2VMaofa2Tor7PJRAjwQLBBuilkJ9L5+13yVbC7JO/wS7piioAvPSwR3JKM1IJ/u4xQzbcXKg==} + '@scure/base@1.2.1': + resolution: {integrity: sha512-DGmGtC8Tt63J5GfHgfl5CuAXh96VF/LD8K9Hr/Gv0J2lAoRGlPOMpqMpMbCTOoOJMZCk2Xt+DskdDyn6dEFdzQ==} + '@scure/bip32@1.5.0': resolution: {integrity: sha512-8EnFYkqEQdnkuGBVpCzKxyIwDCBLDVj3oiX0EKUFre/tOjL/Hqba1D6n/8RcmaQy4f95qQFrO2A8Sr6ybh4NRw==} + '@scure/bip32@1.6.0': + resolution: {integrity: sha512-82q1QfklrUUdXJzjuRU7iG7D7XiFx5PHYVS0+oeNKhyDLT7WPqs6pBcM2W5ZdwOwKCwoE1Vy1se+DHjcXwCYnA==} + '@scure/bip39@1.4.0': resolution: {integrity: sha512-BEEm6p8IueV/ZTfQLp/0vhw4NPnT9oWf5+28nvmeUICjP99f4vr2d+qc7AVGDDtwRep6ifR43Yed9ERVmiITzw==} + '@scure/bip39@1.5.0': + resolution: {integrity: sha512-Dop+ASYhnrwm9+HA/HwXg7j2ZqM6yk2fyLWb5znexjctFY3+E+eU8cIWI0Pql0Qx4hPZCijlGq4OL71g+Uz30A==} + '@sinclair/typebox@0.27.8': resolution: {integrity: sha512-+Fj43pSMwJs4KRrH/938Uf+uAELIgVBmQzg/q1YG10djyfA3TnrU8N8XzqCh/okZdszqBQTZf96idMfE5lnwTA==} @@ -1150,6 +1171,17 @@ packages: zod: optional: true + abitype@1.0.7: + resolution: {integrity: sha512-ZfYYSktDQUwc2eduYu8C4wOs+RDPmnRYMh7zNfzeMtGGgb0U+6tLGjixUic6mXf5xKKCcgT5Qp6cv39tOARVFw==} + peerDependencies: + typescript: '>=5.0.4' + zod: ^3 >=3.22.0 + peerDependenciesMeta: + typescript: + optional: true + zod: + optional: true + aggregate-error@4.0.1: resolution: {integrity: sha512-0poP0T7el6Vq3rstR8Mn4V/IQrpBLO6POkUSrN7RhyY+GF/InCFShQzsQ39T25gkHhLgSLByyAz+Kjb+c2L98w==} engines: {node: '>=12'} @@ -3162,8 +3194,16 @@ snapshots: dependencies: '@noble/hashes': 1.5.0 + '@noble/curves@1.7.0': + dependencies: + '@noble/hashes': 1.6.0 + '@noble/hashes@1.5.0': {} + '@noble/hashes@1.6.0': {} + + '@noble/hashes@1.6.1': {} + '@nodelib/fs.scandir@2.1.5': dependencies: '@nodelib/fs.stat': 2.0.5 @@ -3289,17 +3329,30 @@ snapshots: '@scure/base@1.1.9': {} + '@scure/base@1.2.1': {} + '@scure/bip32@1.5.0': dependencies: '@noble/curves': 1.6.0 '@noble/hashes': 1.5.0 '@scure/base': 1.1.9 + '@scure/bip32@1.6.0': + dependencies: + '@noble/curves': 1.7.0 + '@noble/hashes': 1.6.1 + '@scure/base': 1.2.1 + '@scure/bip39@1.4.0': dependencies: '@noble/hashes': 1.5.0 '@scure/base': 1.1.9 + '@scure/bip39@1.5.0': + dependencies: + '@noble/hashes': 1.6.1 + '@scure/base': 1.2.1 + '@sinclair/typebox@0.27.8': {} '@testing-library/dom@10.4.0': @@ -3412,6 +3465,11 @@ snapshots: typescript: 5.7.2 zod: 3.23.8 + abitype@1.0.7(typescript@5.7.2)(zod@3.23.8): + optionalDependencies: + typescript: 5.7.2 + zod: 3.23.8 + aggregate-error@4.0.1: dependencies: clean-stack: 4.2.0 @@ -4296,11 +4354,11 @@ snapshots: ox@0.1.2(typescript@5.7.2)(zod@3.23.8): dependencies: '@adraffy/ens-normalize': 1.11.0 - '@noble/curves': 1.6.0 - '@noble/hashes': 1.5.0 - '@scure/bip32': 1.5.0 - '@scure/bip39': 1.4.0 - abitype: 1.0.6(typescript@5.7.2)(zod@3.23.8) + '@noble/curves': 1.7.0 + '@noble/hashes': 1.6.1 + '@scure/bip32': 1.6.0 + '@scure/bip39': 1.5.0 + abitype: 1.0.7(typescript@5.7.2)(zod@3.23.8) eventemitter3: 5.0.1 optionalDependencies: typescript: 5.7.2 @@ -4920,8 +4978,8 @@ snapshots: webauthn-p256@0.0.10: dependencies: - '@noble/curves': 1.6.0 - '@noble/hashes': 1.5.0 + '@noble/curves': 1.7.0 + '@noble/hashes': 1.6.1 webidl-conversions@4.0.2: {}