Skip to content

Commit

Permalink
Adding docs
Browse files Browse the repository at this point in the history
  • Loading branch information
y3fers0n committed Jun 6, 2024
1 parent 40f363e commit 3a9f923
Show file tree
Hide file tree
Showing 11 changed files with 240 additions and 21 deletions.
23 changes: 13 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,8 @@
```

Welcome to the Trust Wallet Web3 Provider repository. A set of providers and
javascript wrappers designed to facilitate web3 interactions between dApps and
Wallets.

This library allows you to handle dApps requests on your wallet and send the
response back by implementing providers for you, including EIP-1193.
A modular TypeScript library designed to offer Web3 interfaces, enabling your
wallet to connect with decentralized applications.

```
+----------------+ +------------------+ +---------------+
Expand All @@ -33,7 +29,14 @@ response back by implementing providers for you, including EIP-1193.

## Supported chains

- Cosmos
- Solana
- Binance Chain
- Ethereum
- Cosmos [Docs](/packages/cosmos/README.md)
- Solana - _Wallet Standard fully compatible_ [Docs](/packages/solana/README.md)
- Ethereum _EIP-1193_ [Docs](/packages/ethereum/README.md)

# Useful links

[Using the library](/docs/USAGE.md)

[Contributing](/docs/BUILD.md)

[Adding a new chain](/docs/NEW.md)
56 changes: 56 additions & 0 deletions docs/BUILD.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
## Contributing

### Fork and set up your local env

Install bun

```bash
curl https://bun.sh/install | bash
```

Install deps

```bash
bun install
```

### Useful scripts

Build packages

```bash
bun run build:packages
```

Run tests

```bash
bun run test
```

Run tests watch mode

```bash
bun run test:watch
```

### Test locally

If you wish to test a package inside the extension, let's say solana:

```bash
cd packages/solana
npm link
```

Then you can run in the extension workspace:

```bash
npm link @trustwallet/web3-provider-solana
```

---

## Adding a new chain

[Adding a new chain](/docs/NEW.md)
25 changes: 25 additions & 0 deletions docs/NEW.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
## Adding a new chain

Run the generate command

```bash
bun run generate awesomeNewChain
```

This will generate all the boilerplate structure for your new chain:

```
📦 newChain
├─ tests
├─ exceptions
├─ types
├─ index.ts
├─ NewChainProvider.ts
└─ package.json
```

Then you can build it using the command

```bash
bun run build:packages
```
56 changes: 56 additions & 0 deletions docs/USAGE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
## Using the library

# 1. Run

```bash
npm i @trustwallet/web3-provider-core
```

# 2. Install your desired chain

Let's use for example, ethereum

```bash
npm i @trustwallet/web3-provider-ethereum
```

# 3. Example usage

```typescript
import { Web3Provider } from '@trustwallet/web3-provider-core';
import { EthereumProvider } from '@trustwallet/web3-provider-ethereum';

const ethereum = new EthereumProvider();

new Web3Provider({
strategy: AdapterStrategy.PROMISES,
handler: (params: IHandlerParams) => {},
}).registerProvider(ethereum);

// Register the ethereum provider
window.ethereum = ethereum;
```

dApps that use EIP-1193 will be able to connect to your wallet now.

---

## Find more about the chains

- Cosmos [Docs](/packages/cosmos/README.md)
- Solana - _Wallet Standard fully compatible_ [Docs](/packages/solana/README.md)
- Ethereum _EIP-1193_ [Docs](/packages/ethereum/README.md)

### Using the callback Adapter

```typescript
const provider = new Web3Provider({
strategy: AdapterStrategy.CALLBACK,
}).registerProvider(ethereum);

const handler = (params: IHandlerParams) => {
provider.sendResponse(params.id, ['0x0....']);
};

provider.setHandler(handler);
```
4 changes: 2 additions & 2 deletions packages/core/adapter/Adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export interface IAdapterRequestParams {
method: string;
}

export type IHandler = (params: IHandlerParams) => Promise<any>;
export type IHandler = (params: IHandlerParams) => Promise<any> | void;

export type AdapterStrategyType = keyof typeof AdapterStrategy;

Expand Down Expand Up @@ -51,7 +51,7 @@ export abstract class Adapter {
request(
params: IAdapterRequestParams | ICallbackAdapterRequestParams,
network: string,
): Promise<any> {
): Promise<any> | void {
if (!this.#handler) {
throw new Error('No handler defined for Adapter');
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/adapter/PromiseAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export class PromiseAdapter extends Adapter {
super(AdapterStrategy.PROMISES);
}

request(params: IRequestArguments, network: string): Promise<any> {
request(params: IRequestArguments, network: string): Promise<any> | void {
return super.request(params as IAdapterRequestParams, network);
}
}
23 changes: 21 additions & 2 deletions packages/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,23 @@ import { PromiseAdapter } from './adapter/PromiseAdapter';
export class Web3Provider {
#adapter!: Adapter;

constructor(params: { strategy: AdapterStrategyType; handler: IHandler }) {
constructor(params: { strategy: AdapterStrategyType; handler?: IHandler }) {
const adapter =
params.strategy === AdapterStrategy.CALLBACK
? new CallbackAdapter()
: new PromiseAdapter();

adapter.setHandler(params.handler);
if (params.handler) {
adapter.setHandler(params.handler);
}

this.setAdapter(adapter);
}

setHandler(handler: IHandler) {
this.#adapter.setHandler(handler);
}

private setAdapter(adapter: Adapter) {
this.#adapter = adapter;
return this;
Expand All @@ -40,6 +47,18 @@ export class Web3Provider {
providers.forEach((provider) => this.registerProvider(provider));
return this;
}

sendResponse(requestId: string, response: any) {
if (this.#adapter.getStrategy() === 'CALLBACK') {
(this.#adapter as CallbackAdapter).sendResponse(requestId, response);
}
}

sendError(requestId: string, error: any) {
if (this.#adapter.getStrategy() === 'CALLBACK') {
(this.#adapter as CallbackAdapter).sendError(requestId, error);
}
}
}

export * from './Provider';
18 changes: 16 additions & 2 deletions packages/cosmos/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@
```

### Cosmos Package Description
### Cosmos JavaScript Provider Implementation

TBD
### Config Object

```typescript
const config: {
disableMobileAdapter?: boolean;
isKeplr?: boolean;
isTrust?: boolean;
} = {};
```

### Usage

```typescript
const cosmos = new CosmosProvider(config);
```
22 changes: 20 additions & 2 deletions packages/ethereum/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,24 @@
//
```

### Ethereum Package Description
### Ethereum JavaScript Provider Implementation

TBD
### Config Object

```typescript
const config: {
rpc?: string;
chainId?: string;
overwriteMetamask?: boolean;
supportedMethods?: string[];
unsupportedMethods?: string[];
disableMobileAdapter?: boolean;
isTrust?: boolean;
} = {};
```

### Usage

```typescript
const ethereum = new EthereumProvider(config);
```
13 changes: 13 additions & 0 deletions packages/ethereum/tests/EthereumProvider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@ test('Ethereum Provider → eth_requestAccounts', async () => {
expect(accounts).toEqual([account]);
});

test('Ethereum Provider → eth_requestAccounts callback', async () => {
const provider = new Web3Provider({
strategy: AdapterStrategy.CALLBACK,
}).registerProvider(ethereum);

provider.setHandler((params: IHandlerParams) => {
provider.sendResponse(params.id!, [account]);
});

const accounts = await ethereum.request({ method: 'eth_requestAccounts' });
expect(accounts).toEqual([account]);
});

test('Ethereum Provider → unsupported method returns error', async () => {
const method = 'eth_newFilter';
const ethereum = new EthereumProvider({ unsupportedMethods: [method] });
Expand Down
19 changes: 17 additions & 2 deletions packages/solana/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,21 @@
```

### Solana Package Description
### Solana JavaScript Provider Implementation that uses Wallet Standard

TBD
### Config Object

```typescript
const config: {
isTrust?: boolean;
enableAdapter?: boolean;
cluster?: string;
disableMobileAdapter?: boolean;
} = {};
```

### Usage

```typescript
const solana = new SolanaProvider(config);
```

0 comments on commit 3a9f923

Please sign in to comment.