Skip to content

Commit

Permalink
Add WalletConnect confguration for FCL (#813)
Browse files Browse the repository at this point in the history
  • Loading branch information
jribbink authored Jun 17, 2024
1 parent 37adde5 commit 7e4bef8
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 167 deletions.
10 changes: 8 additions & 2 deletions docs/tools/clients/fcl-js/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,17 @@ addStuff().then((d) => console.log(d)); // 13 (5 + 7 + 1)
| Name | Example | Description |
| --------------------------------- | ------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `accessNode.api` **(required)** | `https://rest-testnet.onflow.org` | API URL for the Flow Blockchain Access Node you want to be communicating with. See all available access node endpoints [here](https://developers.onflow.org/http-api/). |
| `app.detail.title` | `Cryptokitties` | Your applications title, can be requested by wallets and other services. |
| `app.detail.icon` | `https://fcl-discovery.onflow.org/images/blocto.png` | Url for your applications icon, can be requested by wallets and other services. |
| `app.detail.title` | `Cryptokitties` | Your applications title, can be requested by wallets and other services. Used by WalletConnect plugin & Wallet Discovery service. |
| `app.detail.icon` | `https://fcl-discovery.onflow.org/images/blocto.png` | Url for your applications icon, can be requested by wallets and other services. Used by WalletConnect plugin & Wallet Discovery service. |
| `app.detail.description` | `Cryptokitties is a blockchain game` | Your applications description, can be requested by wallets and other services. Used by WalletConnect plugin & Wallet Discovery service. |
| `app.detail.url` | `https://cryptokitties.co` | Your applications url, can be requested by wallets and other services. Used by WalletConnect plugin & Wallet Discovery service. |
| `challenge.handshake` | **DEPRECATED** | Use `discovery.wallet` instead. |
| `discovery.authn.endpoint` | `https://fcl-discovery.onflow.org/api/testnet/authn` | Endpoint for alternative configurable Wallet Discovery mechanism. Read more on [discovery](#discovery) |
| `discovery.wallet` **(required)** | `https://fcl-discovery.onflow.org/testnet/authn` | Points FCL at the Wallet or Wallet Discovery mechanism. |
| `discovery.wallet.method` | `IFRAME/RPC`, `POP/RPC`, `TAB/RPC`, `HTTP/POST`, or `EXT/RPC` | Describes which service strategy a wallet should use. |
| `fcl.limit` | `100` | Specifies fallback compute limit if not provided in transaction. Provided as integer. |
| `flow.network` **(recommended)** | `testnet` | Used in conjunction with stored interactions and provides FCLCryptoContract address for `testnet` and `mainnet`. Possible values: `local`, `testnet`, `mainnet`. |
| `walletconnect.projectId` | `YOUR_PROJECT_ID` | Your app's WalletConnect project ID. See [WalletConnect Cloud](https://cloud.walletconnect.com/sign-in) to obtain a project ID for your application. |

## Using Contracts in Scripts and Transactions

Expand Down Expand Up @@ -115,10 +118,13 @@ import * as fcl from '@onflow/fcl';
fcl
.config()
.put('flow.network', 'testnet')
.put('walletconnect.projectId', 'YOUR_PROJECT_ID')
.put('accessNode.api', 'https://rest-testnet.onflow.org')
.put('discovery.wallet', 'https://fcl-discovery.onflow.org/testnet/authn')
.put('app.detail.title', 'Test Harness')
.put('app.detail.icon', 'https://i.imgur.com/r23Zhvu.png')
.put('app.detail.description', 'A test harness for FCL')
.put('app.detail.url', 'https://myapp.com')
.put('service.OpenID.scopes', 'email email_verified name zoneinfo')
.put('0xFlowToken', '0x7e60df042a9c0868');
```
Expand Down
98 changes: 53 additions & 45 deletions docs/tools/clients/fcl-js/configure-fcl.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,50 +13,51 @@ Values only need to be set once. We recomend doing this once and as early in the
To set a configuation value, the `put` method on the `config` instance needs to be called, the `put` method returns the `config` instance so they can be chained.

```javascript
import * as fcl from "@onflow/fcl"
import * as fcl from '@onflow/fcl';

fcl.config() // returns the config instance
.put("foo", "bar") // configures "foo" to be "bar"
.put("baz", "buz") // configures "baz" to be "buz"
fcl
.config() // returns the config instance
.put('foo', 'bar') // configures "foo" to be "bar"
.put('baz', 'buz'); // configures "baz" to be "buz"
```

## Getting Configuration Values

The `config` instance has an asynchronous `get` method. You can also pass it a fallback value incase the configuration state does not include what you are wanting.

```javascript
import * as fcl from "@onflow/fcl"
import * as fcl from '@onflow/fcl';

fcl.config()
.put("foo", "bar")
.put("woot", 5)
.put("rawr", 7)
fcl.config().put('foo', 'bar').put('woot', 5).put('rawr', 7);

const FALLBACK = 1
const FALLBACK = 1;

async function addStuff () {
var woot = await fcl.config().get("woot", FALLBACK) // will be 5 -- set in the config before
var rawr = await fcl.config().get("rawr", FALLBACK) // will be 7 -- set in the config before
var hmmm = await fcl.config().get("hmmm", FALLBACK) // will be 1 -- uses fallback because this isnt in the config
async function addStuff() {
var woot = await fcl.config().get('woot', FALLBACK); // will be 5 -- set in the config before
var rawr = await fcl.config().get('rawr', FALLBACK); // will be 7 -- set in the config before
var hmmm = await fcl.config().get('hmmm', FALLBACK); // will be 1 -- uses fallback because this isnt in the config

return woot + rawr + hmmm
return woot + rawr + hmmm;
}

addStuff().then(d => console.log(d)) // 13 (5 + 7 + 1)
addStuff().then((d) => console.log(d)); // 13 (5 + 7 + 1)
```

## Common Configuration Keys

- `accessNode.api` -- Api URL for the Flow Blockchain Access Node you want to be communicating with.
- `app.detail.title` - **(INTRODUCED `@onflow/[email protected]`)** Your applications title, can be requested by wallets and other services.
- `app.detail.icon` - **(INTRODUCED `@onflow/[email protected]`)** Url for your applications icon, can be requested by wallets and other services.
- `app.detail.title` - **(INTRODUCED `@onflow/[email protected]`)** Your applications title, can be requested by wallets and other services. Used by WalletConnect plugin & Wallet Discovery service.
- `app.detail.icon` - **(INTRODUCED `@onflow/[email protected]`)** Url for your applications icon, can be requested by wallets and other services. Used by WalletConnect plugin & Wallet Discovery service.
- `app.detail.description` - **(INTRODUCED `@onflow/[email protected]`)** Your applications description, can be requested by wallets and other services. Used by WalletConnect plugin & Wallet Discovery service.
- `app.detail.url` - **(INTRODUCED `@onflow/[email protected]`)** Your applications url, can be requested by wallets and other services. Used by WalletConnect plugin & Wallet Discovery service.
- `challenge.handshake` -- **(DEPRECATED `@onflow/[email protected]`)** Points FCL at the Wallet or Wallet Discovery mechanism.
- `discovery.wallet` -- **(INTRODUCED `@onflow/[email protected]`)** Points FCL at the Wallet or Wallet Discovery mechanism.
- `discovery.wallet.method` -- Describes which service strategy a wallet should use: `IFRAME/RPC`, `POP/RPC`, `TAB/RPC`, `HTTP/POST`, `EXT/RPC`
- `env` -- **(DEPRECATED `@onflow/[email protected]`)** Used in conjunction with stored interactions. Possible values: `local`, `testnet`, `mainnet`
- `fcl.limit` -- Specifies fallback compute limit if not provided in transaction. Provided as integer.
- `fcl.limit` -- Specifies fallback compute limit if not provided in transaction. Provided as integer.
- `flow.network` (recommended) -- **(INTRODUCED `@onflow/[email protected]`)** Used in conjunction with stored interactions and provides FCLCryptoContract address for `testnet` and `mainnet`. Possible values: `local`, `testnet`, `mainnet`.
- `service.OpenID.scopes` - **(INTRODUCED `@onflow/[email protected]`)** Open ID Connect claims for Wallets and OpenID services.
- `walletconnect.projectId` -- **(INTRODUCED `@onflow/[email protected]`)** Your app's WalletConnect project ID. See [WalletConnect Cloud](https://cloud.walletconnect.com/sign-in) to obtain a project ID for your application.

## Using Contracts in Scripts and Transactions

Expand All @@ -65,59 +66,66 @@ addStuff().then(d => console.log(d)) // 13 (5 + 7 + 1)
Configuration keys that start with `0x` will be replaced in FCL scripts and transactions, this allows you to write your script or transaction Cadence code once and not have to change it when you point your application at a difference instance of the Flow Blockchain.

```javascript
import * as fcl from "@onflow/fcl"
import * as fcl from '@onflow/fcl';

fcl.config()
.put("0xFungibleToken", "0xf233dcee88fe0abe")
fcl.config().put('0xFungibleToken', '0xf233dcee88fe0abe');

async function myScript () {
return fcl.send([
fcl.script`
async function myScript() {
return fcl
.send([
fcl.script`
import FungibleToken from 0xFungibleToken // will be replaced with 0xf233dcee88fe0abe because of the configuration
access(all) fun main() { /* Rest of the script goes here */ }
`
]).then(fcl.decode)
`,
])
.then(fcl.decode);
}

async function myTransaction () {
return fcl.send([
fcl.transaction`
async function myTransaction() {
return fcl
.send([
fcl.transaction`
import FungibleToken from 0xFungibleToken // will be replaced with 0xf233dcee88fe0abe because of the configuration
transaction { /* Rest of the transaction goes here */ }
`
]).then(fcl.decode)
`,
])
.then(fcl.decode);
}
```

#### Example

```javascript
import * as fcl from "@onflow/fcl"

fcl.config()
.put("flow.network", "testnet")
.put("accessNode.api", "https://rest-testnet.onflow.org")
.put("discovery.wallet", "https://fcl-discovery.onflow.org/testnet/authn")
.put("app.detail.title", "Test Harness")
.put("app.detail.icon", "https://i.imgur.com/r23Zhvu.png")
.put("0xFlowToken", "0x7e60df042a9c0868")
import * as fcl from '@onflow/fcl';

fcl
.config()
.put('flow.network', 'testnet')
.put('accessNode.api', 'https://rest-testnet.onflow.org')
.put('discovery.wallet', 'https://fcl-discovery.onflow.org/testnet/authn')
.put('walletconnect.projectId', 'YOUR_PROJECT_ID')
.put('app.detail.title', 'Test Harness')
.put('app.detail.icon', 'https://i.imgur.com/r23Zhvu.png')
.put('app.detail.description', 'A test harness for FCL')
.put('app.detail.url', 'https://myapp.com')
.put('0xFlowToken', '0x7e60df042a9c0868');
```

### Using Flow.json

A simpler way to import contracts in scripts and transactions is to use the `config.load` method to ingest your contracts from your `flow.json` file. This keeps the import syntax unified across tools and lets FCL figure out which address to use for what network based on the network provided in config. To use `config.load` you must first import your `flow.json` file and then pass it to `config.load` as a parameter.

```javascript
import { config } from '@onflow/fcl'
import flowJSON from '../flow.json'
import { config } from '@onflow/fcl';
import flowJSON from '../flow.json';

config({
'flow.network': 'testnet',
'accessNode.api': 'https://rest-testnet.onflow.org',
'discovery.wallet': `https://fcl-discovery.onflow.org/testnet/authn`,
}).load({ flowJSON })
}).load({ flowJSON });
```

Let's say your `flow.json` file looks like this:
Expand All @@ -138,4 +146,4 @@ import "HelloWorld"

FCL will automatically replace the contract name with the address for the network you are using.

> Note: never put private keys in your `flow.json`. You should use the [key/location syntax](../../flow-cli/flow.json/security.md) to separate your keys into a separate git ignored file.
> Note: never put private keys in your `flow.json`. You should use the [key/location syntax](../../flow-cli/flow.json/security.md) to separate your keys into a separate git ignored file.
72 changes: 43 additions & 29 deletions docs/tools/clients/fcl-js/discovery.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ title: Wallet Discovery

Knowing all the wallets available to users on a blockchain can be challenging. FCL's Discovery mechanism relieves much of the burden of integrating with Flow compatible wallets and let's developers focus on building their dapp and providing as many options as possible to their users.

There are two ways an app can use Discovery:
1. The **UI version** which can be configured for display via iFrame, Popup, or Tab.
2. The **API version** which allows you to access authentication services directly in your code via `fcl.discovery.authn` method which we'll describe below.
There are two ways an app can use Discovery:

1. The **UI version** which can be configured for display via iFrame, Popup, or Tab.
2. The **API version** which allows you to access authentication services directly in your code via `fcl.discovery.authn` method which we'll describe below.

## UI Version

Expand All @@ -18,17 +19,17 @@ When authenticating via FCL using Discovery UI, a user is shown a list of servic

This method is the simplest way to integrate Discovery and its wallets and services into your app. All you have to do is configure `discovery.wallet` with the host endpoint for testnet or mainnet.

> **Note**: Opt-in wallets, like Ledger and Dapper Wallet, require you to explicitly state you'd like to use them. For more information on including opt-in wallets, [see these docs](./api.md#more-configuration).
>
> **Note**: Opt-in wallets, like Ledger and Dapper Wallet, require you to explicitly state you'd like to use them. For more information on including opt-in wallets, [see these docs](./api.md#more-configuration).
>
> A [Dapper Wallet](https://meetdapper.com/developers) developer account is required. To enable Dapper Wallet inside FCL, you need to [follow this guide](https://docs.meetdapper.com/quickstart).
```javascript
import { config } from "@onflow/fcl";
import { config } from '@onflow/fcl';

config({
"accessNode.api": "https://rest-testnet.onflow.org",
"discovery.wallet": "https://fcl-discovery.onflow.org/testnet/authn"
})
'accessNode.api': 'https://rest-testnet.onflow.org',
'discovery.wallet': 'https://fcl-discovery.onflow.org/testnet/authn',
});
```

Any time you call `fcl.authenticate` the user will be presented with that screen.
Expand All @@ -42,12 +43,12 @@ Starting in version 0.0.79-alpha.4, dapps now have the ability to display app a
All you have to do is set `app.detail.icon` and `app.detail.title` like this:

```javascript
import { config } from "@onflow/fcl";
import { config } from '@onflow/fcl';

config({
"app.detail.icon": "https://placekitten.com/g/200/200",
"app.detail.title": "Kitten Dapp"
})
'app.detail.icon': 'https://placekitten.com/g/200/200',
'app.detail.title': 'Kitten Dapp',
});
```

**Note:** If these configuration options aren't set, Dapps using the Discovery API will still display a default icon and "Unknown App" as the title when attempting to authorize a user who is not logged in. It is highly recommended to set these values accurately before going live.
Expand All @@ -59,47 +60,59 @@ If you want more control over your authentication UI, the Discovery API is also
Setup still requires configuration of the Discovery endpoint, but when using the API it is set via `discovery.authn.endpoint` as shown below.

```javascript
import { config } from "@onflow/fcl"
import { config } from '@onflow/fcl';

config({
"accessNode.api": "https://rest-testnet.onflow.org",
"discovery.authn.endpoint": "https://fcl-discovery.onflow.org/api/testnet/authn"
})
'accessNode.api': 'https://rest-testnet.onflow.org',
'discovery.authn.endpoint':
'https://fcl-discovery.onflow.org/api/testnet/authn',
});
```

You can access services in your Dapp from `fcl.discovery`:

```javascript
import * as fcl from "@onflow/fcl"
import * as fcl from '@onflow/fcl';

fcl.discovery.authn.subscribe(callback)
fcl.discovery.authn.subscribe(callback);

// OR
// OR

fcl.discovery.authn.snapshot()
fcl.discovery.authn.snapshot();
```

In order to authenticate with a service (for example, when a user click's "login"), pass the selected service to the `fcl.authenticate` method described here [in the API reference](./api.md#authenticate):

```jsx
fcl.authenticate({ service })
fcl.authenticate({ service });
```

A simple React component may end up looking like this:

```jsx
import "./config"
import { useState, useEffect } from "react"
import * as fcl from "@onflow/fcl"
import './config';
import { useState, useEffect } from 'react';
import * as fcl from '@onflow/fcl';

function Component() {
const [services, setServices] = useState([])
useEffect(() => fcl.discovery.authn.subscribe(res => setServices(res.results)), [])
const [services, setServices] = useState([]);
useEffect(
() => fcl.discovery.authn.subscribe((res) => setServices(res.results)),
[],
);

return (
<div>
{services.map(service => <button key={service.provider.address} onClick={() => fcl.authenticate({ service })}>Login with {service.provider.name}</button>)}
{services.map((service) => (
<button
key={service.provider.address}
onClick={() => fcl.authenticate({ service })}
>
Login with {service.provider.name}
</button>
))}
</div>
)
);
}
```

Expand Down Expand Up @@ -171,5 +184,6 @@ fcl.config({
| `Ledger` | 0x9d2e44203cb13051 | 0xe5cd26afebe62781 |

To learn more about other possible configurations, check out the following links:

- [Discovery API Docs](./api.md#discovery-1)
- [Discovery Github Repo](https://github.com/onflow/fcl-discovery)
Loading

0 comments on commit 7e4bef8

Please sign in to comment.