Skip to content
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

feat: Tezos Provider #1

Merged
merged 6 commits into from
Dec 17, 2024
Merged
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
44 changes: 44 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

.DS_Store

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# next.js
/.next/
/out/

# production
/build
/dist

# misc
.DS_Store
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# local env files
.env
.env.local
.env.development.local
.env.test.local
.env.production.local

# vercel
.vercel

# webstorm ide
.idea

# vscode
.vscode/settings.json
4 changes: 4 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"arrowParens": "avoid",
"trailingComma": "es5"
}
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Trilitech

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.
172 changes: 170 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,170 @@
# tezos-provider
Tezos Provider for dApps using WalletConnect SDK
# 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

```
npm i @walletconnect/tezos-provider @walletconnect/modal
npx install-peerdeps @trili/tezos-provider
```

These libraries are external and have to be installed before using the Tezos Provider:

```
"external": [
"@taquito/taquito",
"@taquito/rpc",
"@airgap/beacon-types",
"@walletconnect/universal-provider",
"@walletconnect/types",
"@walletconnect/keyvaluestorage",
"@walletconnect/logger"
]
```

## Initialization

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

```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'
});
```

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
});
```

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();
```

## 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);
```

### 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);
```

### 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);
```

## 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);
```

## Error Handling

The provider will throw errors if:

- `TezosProviderError`: If there are issues with the connection or account retrieval.

## Supported WalletConnectModal options (qrModalOptions)

Please reference the [up-to-date WalletConnect documentation](https://docs.walletconnect.com) for any additional `qrModalOptions`.

## References

- [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)
Loading