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: add ctrl wallet adapter #24

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ This repository contains wallet adapters and components for Tron DApps. With out
| |Browser Extension | >= 3.16.3 |
| [Ledger](https://www.ledger.com/) | - | All versions |
| [WalletConnect](https://walletconnect.org) | - | >= v2.0 |
| [CtrlWallet](https://ctrl.xyz//) | - | >= 1.0.0 |

> **Note**: In case wallet developers intend to release breaking changes, you can [open an issue here](https://github.com/web3-geek/tronwallet-adapter/issues/new) to inform us, thus enabling us to update the new protocols accordingly.

Expand Down Expand Up @@ -225,6 +226,7 @@ You can use the `@tronweb3/tronwallet-adapters` package, or add the individual w
| [gatewallet](https://www.gate.io/web3) | Adapter for gate.io Wallet App(IOS and Android) | [`@tronweb3/tronwallet-adapter-gatewallet`](https://www.npmjs.com/package/@tronweb3/tronwallet-adapter-gatewallet) |
| [foxwallet](https://foxwallet.com/) | Adapter for Fox Wallet App(IOS and Android) | [`@tronweb3/tronwallet-adapter-foxwallet`](https://www.npmjs.com/package/@tronweb3/tronwallet-adapter-foxwallet) |
| [bybit](https://www.bybit.com/en/web3/home) | Adapter for Bybit Wallet App(IOS and Android) and Extension | [`@tronweb3/tronwallet-adapter-bybit`](https://www.npmjs.com/package/@tronweb3/tronwallet-adapter-bybit) |
| [ctrlwallet](https://ctrl.xyz/) | Adapter for Ctrl Wallet | [`@tronweb3/tronwallet-adapter-ctrlwallet`](https://www.npmjs.com/package/@tronweb3/tronwallet-adapter-ctrlwallet) |

### React Components

Expand Down
20 changes: 20 additions & 0 deletions packages/adapters/ctrlwallet/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The MIT License (MIT)
Copyright (c) 2022-Present, web3-geek

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.
91 changes: 91 additions & 0 deletions packages/adapters/ctrlwallet/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# `@tronweb3/tronwallet-adapter-ctrlwallet`

This package provides an adapter to enable TRON DApps to connect to the [Ctrl Wallet extension](https://chromewebstore.google.com/detail/ctrl-wallet/hmeobnfnfcmdkdcmlblgagmfpfboieaf).

## Demo

```typescript
import { CtrlWalletAdapter } from '@tronweb3/tronwallet-adapter-ctrlwallet';
import TronWeb from 'tronweb';

const tronWeb = new TronWeb({
fullHost: 'https://api.trongrid.io',
headers: { 'TRON-PRO-API-KEY': 'your api key' },
});

const adapter = new CtrlWalletAdapter();
// connect
await adapter.connect();

// then you can get address
console.log(adapter.address);

// create a send TRX transaction
const unSignedTransaction = await tronWeb.transactionBuilder.sendTrx(targetAddress, 100, adapter.address);
// using adapter to sign the transaction
const signedTransaction = await adapter.signTransaction(unSignedTransaction);
// broadcast the transaction
await tronWeb.trx.sendRawTransaction(signedTransaction);
```

## Documentation

### API

- `Constructor(config: CtrlWalletAdapterConfig)`
```typescript
interface CtrlWalletAdapterConfig {
/**
* Set if open Wallet's website url when wallet is not installed.
* Default is true.
*/
openUrlWhenWalletNotFound?: boolean;
/**
* Timeout in millisecond for checking if TronLink wallet exists.
* Default is 30 * 1000ms
*/
checkTimeout?: number;
/**
* Set if open TronLink app using DeepLink on mobile device.
* Default is true.
*/
openTronLinkAppOnMobile?: boolean;
/**
* The icon of your dapp. Used when open TronLink app in mobile device browsers.
* Default is current website icon.
*/
dappIcon?: string;
/**
* The name of your dapp. Used when open TronLink app in mobile device browsers.
* Default is `document.title`.
*/
dappName?: string;
}
```
- `network()` method is supported to get current network information. The type of returned value is `Network` as follows:

```typescript
export enum NetworkType {
Mainnet = 'Mainnet',
Shasta = 'Shasta',
Nile = 'Nile',
/**
* When use custom node
*/
Unknown = 'Unknown',
}

export type Network = {
networkType: NetworkType;
chainId: string;
fullNode: string;
solidityNode: string;
eventServer: string;
};
```

### Caveats

- **Ctrl Wallet doesn't support `disconnect` by DApp**. As CtrlWalletAdapter doesn't support disconnect by DApp website, call `adapter.disconnect()` won't disconnect from Ctrl Wallet extension really.

For more information about tronwallet adapters, please refer to [`@tronweb3/tronwallet-adapters`](https://github.com/web3-geek/tronwallet-adapter/tree/main/packages/adapters/adapters)
18 changes: 18 additions & 0 deletions packages/adapters/ctrlwallet/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
export default {
preset: 'ts-jest',
testEnvironment: 'jsdom',
transform: {
'\\.tsx?$': [
'ts-jest',
{
useESM: true,
},
],
},
moduleNameMapper: {
'bignumber\\.js': '$0',
'(.+)\\.js': '$1',
},
extensionsToTreatAsEsm: ['.ts'],
};
54 changes: 54 additions & 0 deletions packages/adapters/ctrlwallet/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"name": "@tronweb3/tronwallet-adapter-ctrlwallet",
"version": "1.0.0",
"description": "Wallet adapter for Ctrl Wallet extension and app.",
"keywords": [
"TRON",
"TronWeb",
"Ctrl Wallet"
],
"author": "web3-geek",
"repository": {
"type": "git",
"url": "https://github.com/web3-geek/tronwallet-adapter"
},
"license": "MIT",
"type": "module",
"sideEffects": false,
"engines": {
"node": ">=16",
"pnpm": ">=7"
},
"main": "./lib/cjs/index.js",
"module": "./lib/esm/index.js",
"types": "./lib/types/index.d.ts",
"exports": {
"require": "./lib/cjs/index.js",
"import": "./lib/esm/index.js",
"types": "./lib/types/index.d.ts"
},
"files": [
"lib",
"src",
"LICENSE"
],
"publishConfig": {
"access": "public"
},
"scripts": {
"clean": "shx mkdir -p lib && shx rm -rf lib",
"package": "shx echo '{ \"type\": \"commonjs\" }' > lib/cjs/package.json",
"test": "jest",
"test:coverage": "jest --coverage",
"build:umd": "node ../../../scripts/build-umd.js"
},
"dependencies": {
"@tronweb3/tronwallet-abstract-adapter": "workspace:^"
},
"devDependencies": {
"@testing-library/dom": "^8.20.0",
"jest": "29.6.4",
"jest-environment-jsdom": "^29.6.4",
"shx": "^0.3.4"
}
}
Loading