Skip to content

Commit 1b40b6f

Browse files
feat: Tezos Provider
1 parent f205f2a commit 1b40b6f

16 files changed

+8261
-2
lines changed

.gitignore

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
.DS_Store
4+
5+
# dependencies
6+
/node_modules
7+
/.pnp
8+
.pnp.js
9+
10+
# testing
11+
/coverage
12+
13+
# next.js
14+
/.next/
15+
/out/
16+
17+
# production
18+
/build
19+
/dist
20+
21+
# misc
22+
.DS_Store
23+
*.pem
24+
25+
# debug
26+
npm-debug.log*
27+
yarn-debug.log*
28+
yarn-error.log*
29+
30+
# local env files
31+
.env
32+
.env.local
33+
.env.development.local
34+
.env.test.local
35+
.env.production.local
36+
37+
# vercel
38+
.vercel
39+
40+
# webstorm ide
41+
.idea
42+
43+
# vscode
44+
.vscode/settings.json

.prettierrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"arrowParens": "avoid",
3+
"trailingComma": "es5"
4+
}

.swcrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"sourceMaps": false
3+
}

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 Trilitech
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 155 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,155 @@
1-
# tezos-provider
2-
Tezos Provider for dApps using WalletConnect SDK
1+
# TezosProvider
2+
3+
The `TezosProvider` is a class that allows you to interact with the Tezos blockchain via the WalletConnect protocol.
4+
This provider manages the connection to the Tezos network, facilitates transactions, and handles account management.
5+
6+
## Installation
7+
8+
```
9+
npm i @walletconnect/tezos-provider @walletconnect/modal
10+
```
11+
12+
## Initialization
13+
14+
To use `TezosProvider`, you first need to initialize it with the necessary options:
15+
16+
```typescript
17+
import TezosProvider from 'path-to-tezos-provider';
18+
19+
const provider = await TezosProvider.init({
20+
projectId: 'your-project-id', // REQUIRED WalletConnect project ID
21+
metadata: {
22+
name: 'Your DApp Name',
23+
description: 'Your DApp Description',
24+
url: 'https://your-dapp-url.com',
25+
icons: ['https://your-dapp-url.com/icon.png'],
26+
},
27+
relayUrl: 'wss://relay.walletconnect.com', // OPTIONAL WalletConnect relay URL
28+
storageOptions: {}, // OPTIONAL key-value storage settings
29+
disableProviderPing: false, // OPTIONAL set to true to disable provider ping
30+
logger: 'info', // OPTIONAL log level, default is 'info'
31+
});
32+
```
33+
34+
Default relay URL is defined in `RelayUrl`.
35+
36+
### Options (TezosProviderOpts)
37+
38+
- `projectId`: Your WalletConnect project ID.
39+
- `metadata`: Metadata for your DApp, including name, description, url, and icons.
40+
- `relayUrl`: URL of the WalletConnect relay server.
41+
- `storageOptions`: Optional settings for key-value storage.
42+
- `disableProviderPing`: If set to true, disables provider ping.
43+
- `logger`: Sets the log level, default is 'info'.
44+
45+
## Display WalletConnectModal with QR code / Connecting to the Tezos Network
46+
47+
After initializing the provider, you can connect it to the Tezos network:
48+
49+
```typescript
50+
await provider.connect({
51+
chains: [
52+
{
53+
id: 'tezos:mainnet',
54+
rpc: ['https://mainnet-tezos.giganode.io'],
55+
},
56+
],
57+
methods: ['tezos_getAccounts', 'tezos_send', 'tezos_sign'],
58+
events: [], // OPTIONAL Tezos events
59+
});
60+
```
61+
62+
Connection Options (TezosConnectOpts):
63+
64+
- `chains`: An array of chain data, each with an id and rpc endpoint(s). Default chain data is defined in `TezosChainMap`.
65+
- `methods`: An array of methods that the provider should support. Default methods are defined in `DefaultTezosMethods`.
66+
- `events`: An array of event names that the provider should listen for.
67+
68+
If you are not using a modal for QR code display, you can subscribe to the `display_uri` event to handle the connection URI yourself:
69+
70+
```typescript
71+
provider.on("display_uri", (uri: string) => {
72+
// Handle the connection URI
73+
console.log('Connection URI:', uri);
74+
});
75+
76+
await provider.connect();
77+
```
78+
79+
## Sending requests
80+
81+
### Get Accounts
82+
83+
To send a request to the Tezos network:
84+
85+
```typescript
86+
const accounts = await provider.request({ method: "tezos_getAccounts" });
87+
88+
// OR
89+
90+
provider.sendAsync({ method: "tezos_getAccounts" }, callbackFunction);
91+
```
92+
93+
### Send Transactions
94+
95+
To send a transaction:
96+
97+
```typescript
98+
const transactionResponse = await provider.tezosSendTransaction({
99+
kind: 'transaction',
100+
destination: 'tz1...',
101+
amount: '1000000', // Amount in mutez
102+
});
103+
104+
console.log('Transaction hash:', transactionResponse.hash);
105+
```
106+
107+
### Sign Messages
108+
109+
To sign a message, encode it to hex first:
110+
111+
```typescript
112+
const textEncoder = new TextEncoder();
113+
const bytes = textEncoder.encode('Your string here');
114+
const hexBytes = Buffer.from(bytes).toString('hex');
115+
116+
const signResponse = await provider.tezosSign({
117+
payload: hexBytes,
118+
});
119+
120+
console.log('Signature:', signResponse.signature);
121+
```
122+
123+
## Events
124+
125+
Listen to various events from the TezosProvider:
126+
127+
```typescript
128+
// chain changed
129+
provider.on("chainChanged", handler);
130+
// accounts changed
131+
provider.on("accountsChanged", handler);
132+
// session established
133+
provider.on("connect", handler);
134+
// session event - chainChanged/accountsChanged/custom events
135+
provider.on("session_event", handler);
136+
// connection uri
137+
provider.on("display_uri", handler);
138+
// session disconnect
139+
provider.on("disconnect", handler);
140+
```
141+
142+
## Error Handling
143+
The provider will throw errors if:
144+
145+
- `TezosInitializationError`: If the provider is not initialized correctly.
146+
- `TezosProviderError`: If there are issues with the connection or account retrieval.
147+
148+
## Supported WalletConnectModal options (qrModalOptions)
149+
150+
Please reference the [up-to-date WalletConnect documentation](https://docs.walletconnect.com) for any additional `qrModalOptions`.
151+
152+
## References
153+
154+
- [Tezos documentation for WalletConnect](https://docs.walletconnect.com/advanced/multichain/rpc-reference/tezos-rpc)
155+
- [dApp examples](https://github.com/WalletConnect/web-examples/tree/main/dapps)

eslint.config.mjs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
2+
import tsParser from "@typescript-eslint/parser"; // Import the TypeScript parser
3+
import tsPlugin from "@typescript-eslint/eslint-plugin"; // Import the TypeScript plugin
4+
import prettierPlugin from "eslint-plugin-prettier"; // Import the Prettier plugin
5+
import importPlugin from 'eslint-plugin-import';
6+
7+
export default {
8+
languageOptions: {
9+
globals: {
10+
NodeJS: true,
11+
},
12+
parser: tsParser,
13+
parserOptions: {
14+
ecmaVersion: 2020,
15+
sourceType: "module",
16+
},
17+
},
18+
files: ["**/*.ts"],
19+
rules: {
20+
"comma-dangle": ["error", "always-multiline"],
21+
"require-await": "error",
22+
"no-undef": ["error"],
23+
"no-var": ["error"],
24+
"object-curly-spacing": ["error", "always"],
25+
"quotes": ["error", "double", { "allowTemplateLiterals": true }],
26+
"semi": ["error", "always"],
27+
"no-console": ["error", { allow: ["warn"] }],
28+
"import/no-extraneous-dependencies": ["error"],
29+
"import/order": [
30+
"warn",
31+
{
32+
groups: [
33+
"builtin", // Built-in imports (come from NodeJS native) go first
34+
"external", // <- External imports
35+
"internal", // <- Absolute imports
36+
["sibling", "parent"], // <- Relative imports, the sibling and parent types they can be mingled together
37+
"index", // <- index imports
38+
"unknown", // <- unknown
39+
],
40+
"newlines-between": "always",
41+
alphabetize: {
42+
order: "asc",
43+
caseInsensitive: true,
44+
},
45+
},
46+
],
47+
},
48+
ignores: ["dist", "node_modules/*"],
49+
plugins: {
50+
"@typescript-eslint": tsPlugin,
51+
prettier: prettierPlugin,
52+
import: importPlugin,
53+
},
54+
};

jest.config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export default {
2+
preset: 'ts-jest',
3+
testEnvironment: 'node',
4+
};

package.json

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
{
2+
"name": "@trili/tezos-provider",
3+
"description": "Tezos Provider for WalletConnect Protocol",
4+
"version": "1.0.3",
5+
"author": "Trilitech TriliTech <contact@trili.tech> (https://trili.tech)",
6+
"homepage": "https://trili.tech",
7+
"repository": {
8+
"type": "git",
9+
"url": "https://github.com/trilitech/tezos-connect",
10+
"directory": "."
11+
},
12+
"license": "MIT",
13+
"type": "module",
14+
"main": "dist/index.cjs.js",
15+
"module": "dist/index.es.js",
16+
"unpkg": "dist/index.umd.js",
17+
"types": "dist/types/index.d.ts",
18+
"sideEffects": false,
19+
"exports": {
20+
".": {
21+
"types": "./dist/index.d.ts",
22+
"import": "./dist/index.js",
23+
"require": "./dist/index.cjs"
24+
}
25+
},
26+
"files": [
27+
"dist"
28+
],
29+
"keywords": [
30+
"wallet",
31+
"walletconnect"
32+
],
33+
"scripts": {
34+
"clean": "rm -rf dist",
35+
"build": "tsup-node",
36+
"test:pre": "rm -rf ./test/test.db",
37+
"test:run": "jest",
38+
"test": "yarn test:pre; yarn test:run",
39+
"prettier": "prettier --check '{src,test}/**/*.{js,ts,jsx,tsx}' --write",
40+
"lint": "eslint -c 'eslint.config.mjs' --fix './src/**/*.ts'",
41+
"lint:ci": "eslint 'src/**/*.ts' -f json -o lintReport.json || true"
42+
},
43+
"tsup": {
44+
"dts": true,
45+
"entry": [
46+
"src/index.ts"
47+
],
48+
"clean": true,
49+
"format": [
50+
"cjs",
51+
"esm"
52+
]
53+
},
54+
"dependencies": {
55+
"@airgap/beacon-types": "^4.2.2",
56+
"@taquito/rpc": "~20.0.1",
57+
"@taquito/taquito": "~20.0.1",
58+
"@trili/tezos-provider": "^1.0.2",
59+
"@walletconnect/keyvaluestorage": "^1.1.1",
60+
"@walletconnect/logger": "^2.1.2",
61+
"@walletconnect/types": "2.13.3",
62+
"@walletconnect/universal-provider": "^2.17.1",
63+
"axios": "^1.7.7",
64+
"micromatch": "^4.0.8",
65+
"package.json": "^2.0.1",
66+
"rollup": "^4.24.0",
67+
"typescript": "^5.6.3",
68+
"yarn.lock": "^0.0.1-security"
69+
},
70+
"devDependencies": {
71+
"@jest/globals": "^29.7.0",
72+
"@types/jest": "^29.5.13",
73+
"@types/micromatch": "^4",
74+
"@typescript-eslint/eslint-plugin": "^8.8.1",
75+
"@typescript-eslint/parser": "^8.8.1",
76+
"depcheck": "^1.4.7",
77+
"eslint": "^8.57.0",
78+
"eslint-config-prettier": "^9.1.0",
79+
"eslint-plugin-import": "^2.31.0",
80+
"eslint-plugin-prettier": "^5.2.1",
81+
"jest": "^29.7.0",
82+
"prettier": "^3.3.3",
83+
"ts-jest": "^29.2.5",
84+
"tsup": "^8.2.4"
85+
},
86+
"engines": {
87+
"node": ">=18"
88+
}
89+
}

0 commit comments

Comments
 (0)