Skip to content

feat: Tezos Provider #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"tabWidth": 2,
"useTabs": false,
"trailingComma": "all",
"printWidth": 100
}
3 changes: 3 additions & 0 deletions .swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"sourceMaps": false
}
Binary file added .yarn/install-state.gz
Binary file not shown.
925 changes: 925 additions & 0 deletions .yarn/releases/yarn-4.4.0.cjs

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
nodeLinker: node-modules

yarnPath: .yarn/releases/yarn-4.4.0.cjs
188 changes: 133 additions & 55 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,77 +1,155 @@
# Tezos Connect

## Overview
Tezos Connect is a library designed to standardize the JSON RPC
interface between decentralized
applications (dApps) and wallets on the Tezos blockchain. This
library contains all necessary interface definitions and utility
functions to help developers build dApps and wallets with a well-defined,
standard interface. It assists in creating JSON RPC messages in the
correct format and validates received JSON RPC requests for syntactic
correctness.
This libabry should be used with WalletConnect, an open source protocol
that allows you to connect your crypto wallet to dapps on the web.

## Features
- Standardized JSON RPC interface definitions for Tezos dApps and wallets.
- Utility functions to create and validate JSON RPC messages.
# TezosProvider

The `TezosProvider` is a class that allows you to interact with the Tezos blockchain via the WalletConnect protocol.
This provider manages the connection to the Tezos network, facilitates transactions, and handles account management.

## Installation

### Using npm
```sh
yarn install tezos-connect
```

### Using yarn
```sh
yarn add tezos-connect
npm i @walletconnect/tezos-provider @walletconnect/modal
```

## Usage
### Importing the Library example
```ts
import { PartialTezosOperation } from '@trilitech/tezos-connect';
```
## Initialization

To use `TezosProvider`, you first need to initialize it with the necessary options:

## Development
### Cloning the Repository
```sh
git clone https://github.com/Trilitech/tezos-connect.git
cd tezos-connect
```typescript
import TezosProvider from 'path-to-tezos-provider';

const provider = await TezosProvider.init({
projectId: 'your-project-id', // REQUIRED WalletConnect project ID
metadata: {
name: 'Your DApp Name',
description: 'Your DApp Description',
url: 'https://your-dapp-url.com',
icons: ['https://your-dapp-url.com/icon.png'],
},
relayUrl: 'wss://relay.walletconnect.com', // OPTIONAL WalletConnect relay URL
storageOptions: {}, // OPTIONAL key-value storage settings
disableProviderPing: false, // OPTIONAL set to true to disable provider ping
logger: 'info', // OPTIONAL log level, default is 'info'
});
```

### Installing Dependencies
Default relay URL is defined in `RelayUrl`.

### Options (TezosProviderOpts)

- `projectId`: Your WalletConnect project ID.
- `metadata`: Metadata for your DApp, including name, description, url, and icons.
- `relayUrl`: URL of the WalletConnect relay server.
- `storageOptions`: Optional settings for key-value storage.
- `disableProviderPing`: If set to true, disables provider ping.
- `logger`: Sets the log level, default is 'info'.

## Display WalletConnectModal with QR code / Connecting to the Tezos Network

After initializing the provider, you can connect it to the Tezos network:

```typescript
await provider.connect({
chains: [
{
id: 'tezos:mainnet',
rpc: ['https://mainnet-tezos.giganode.io'],
},
],
methods: ['tezos_getAccounts', 'tezos_send', 'tezos_sign'],
events: [], // OPTIONAL Tezos events
});
```
yarn install

Connection Options (TezosConnectOpts):

- `chains`: An array of chain data, each with an id and rpc endpoint(s). Default chain data is defined in `TezosChainMap`.
- `methods`: An array of methods that the provider should support. Default methods are defined in `DefaultTezosMethods`.
- `events`: An array of event names that the provider should listen for.

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:

```typescript
provider.on("display_uri", (uri: string) => {
// Handle the connection URI
console.log('Connection URI:', uri);
});

await provider.connect();
```

### Building the Library
## Sending requests

### Get Accounts

To send a request to the Tezos network:

```typescript
const accounts = await provider.request({ method: "tezos_getAccounts" });

// OR

provider.sendAsync({ method: "tezos_getAccounts" }, callbackFunction);
```
yarn build

### Send Transactions

To send a transaction:

```typescript
const transactionResponse = await provider.tezosSendTransaction({
kind: 'transaction',
destination: 'tz1...',
amount: '1000000', // Amount in mutez
});

console.log('Transaction hash:', transactionResponse.hash);
```
### Running Tests

### Sign Messages

To sign a message, encode it to hex first:

```typescript
const textEncoder = new TextEncoder();
const bytes = textEncoder.encode('Your string here');
const hexBytes = Buffer.from(bytes).toString('hex');

const signResponse = await provider.tezosSign({
payload: hexBytes,
});

console.log('Signature:', signResponse.signature);
```
yarn test
yarn type-test

## Events

Listen to various events from the TezosProvider:

```typescript
// chain changed
provider.on("chainChanged", handler);
// accounts changed
provider.on("accountsChanged", handler);
// session established
provider.on("connect", handler);
// session event - chainChanged/accountsChanged/custom events
provider.on("session_event", handler);
// connection uri
provider.on("display_uri", handler);
// session disconnect
provider.on("disconnect", handler);
```

## Contributing
We welcome contributions to the Tezos Connect library! Please follow these steps to contribute:
## Error Handling
The provider will throw errors if:

1. Fork the repository.
2. Create a new branch (`git checkout -b feature/your-feature`).
3. Make your changes.
4. Commit your changes (`git commit -m 'Add some feature'`).
5. Push to the branch (`git push origin feature/your-feature`).
6. Create a new Pull Request.
- `TezosInitializationError`: If the provider is not initialized correctly.
- `TezosProviderError`: If there are issues with the connection or account retrieval.

## License
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
## Supported WalletConnectModal options (qrModalOptions)

## Contact
For any questions or suggestions, please open an issue or submit a pull request.
Please reference the [up-to-date WalletConnect documentation](https://docs.walletconnect.com) for any additional `qrModalOptions`.

---
## References

Happy coding with Tezos!
- [Tezos documentation for WalletConnect](https://docs.walletconnect.com/advanced/multichain/rpc-reference/tezos-rpc)
- [dApp examples](https://github.com/WalletConnect/web-examples/tree/main/dapps)
61 changes: 61 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@

import tsParser from "@typescript-eslint/parser"; // Import the TypeScript parser
import tsPlugin from "@typescript-eslint/eslint-plugin"; // Import the TypeScript plugin
import prettierPlugin from "eslint-plugin-prettier"; // Import the Prettier plugin
import importPlugin from 'eslint-plugin-import';

export default {
languageOptions: {
globals: {
NodeJS: true,
},
parser: tsParser,
parserOptions: {
ecmaVersion: 2020,
sourceType: "module",
},
},
files: ["**/*.ts"],
rules: {
"@typescript-eslint/ban-ts-ignore": ["off"],
"@typescript-eslint/camelcase": ["off"],
"@typescript-eslint/explicit-function-return-type": ["off"],
"@typescript-eslint/interface-name-prefix": ["off"],
"@typescript-eslint/no-explicit-any": ["off"],
"@typescript-eslint/no-unused-expressions": ["off"],
"@typescript-eslint/no-var-requires": ["off"],
"@typescript-eslint/no-use-before-define": ["off"],
"@typescript-eslint/no-unused-vars": ["off"],
"@typescript-eslint/no-namespace": ["off"],
"@typescript-eslint/ban-ts-comment": "off",
"camelcase": "off",
"no-unused-expressions": "off",
"comma-dangle": ["error", "always-multiline"],
"require-await": "warn",
"no-async-promise-executor": ["off"],
"no-empty-pattern": ["off"],
"no-undef": ["error"],
"no-var": ["error"],
"object-curly-spacing": ["error", "always"],
"quotes": ["error", "double", { "allowTemplateLiterals": true }],
"semi": ["error", "always"],
"spaced-comment": ["off"],
"no-prototype-builtins": ["off"],
"sort-keys": ["off"],
"space-before-function-paren": ["off"],
"indent": ["off"],
"promise/param-names": "off",
"no-console": ["error", { allow: ["warn"] }],
"no-useless-constructor": "off",
"no-use-before-define": "off",
"curly": "off",
"prefer-promise-reject-errors": "off",
"import/no-extraneous-dependencies": ["error"],
},
ignores: ["dist", "node_modules/*"],
plugins: {
"@typescript-eslint": tsPlugin,
prettier: prettierPlugin,
import: importPlugin,
},
};
3 changes: 1 addition & 2 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
module.exports = {
export default {
preset: 'ts-jest',
testEnvironment: 'node',
testMatch: ['**/?(*.)+(spec|test).[jt]s?(x)'],
};
Loading