# @dcspark/adalib

> Cardano development utilities and wrappers

Latest version **1.3.1** (published 2023-05-01) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install @dcspark/adalib
pnpm add @dcspark/adalib
yarn add @dcspark/adalib
bun add @dcspark/adalib
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.3.1 |
| Published | 2023-05-01 |
| First published | 2023-01-07 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 6 |
| Unpacked size | 52.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 13 |
| Author | dcSpark |
| Maintainers | ggaabe, nicolasdp, ycl1, robkorn, sebastiengllmt |
| Keywords | wallet, cardano, web3, crypto, walletconnect |

## Links

- npm: https://www.npmjs.com/package/@dcspark/adalib
- Repository: https://github.com/dcspark/adalib
- Homepage: https://github.com/dcspark/adalib/
- Issues: https://github.com/dcspark/adalib/issues
- npm.io page: https://npm.io/package/@dcspark/adalib

## Dependencies (6)

- [bs58](https://npm.io/package/bs58.md) 5.0.0
- [buffer](https://npm.io/package/buffer.md) ^6.0.3
- [valtio](https://npm.io/package/valtio.md) 1.7.0
- [@web3modal/standalone](https://npm.io/package/@web3modal/standalone.md) 2.3.7
- [@walletconnect/universal-provider](https://npm.io/package/@walletconnect/universal-provider.md) ^2.7.0
- [@cardano-foundation/cardano-connect-with-wallet](https://npm.io/package/@cardano-foundation/cardano-connect-with-wallet.md) 0.1.45

## Alternatives

- [@gemini-wallet/core](https://npm.io/package/@gemini-wallet/core.md) — 515.6K weekly downloads
- [utility](https://npm.io/package/utility.md) — 416.6K weekly downloads
- [@primno/dpapi](https://npm.io/package/@primno/dpapi.md) — 7.2K weekly downloads
- [pi-readseek](https://npm.io/package/pi-readseek.md) — 3.7K weekly downloads
- [@emilia-protocol/verify](https://npm.io/package/@emilia-protocol/verify.md) — 1.1K weekly downloads

## Recent versions

- 1.3.1 (latest) — 2023-05-01
- 1.3.0 — 2023-04-30
- 1.2.1 — 2023-04-26
- 1.2.0 — 2023-04-26
- 1.1.0 — 2023-04-25
- 1.0.4 — 2023-04-10
- 1.0.1 — 2023-04-10
- 1.0.0 — 2023-03-06
- 0.1.19 — 2023-02-22
- 0.1.18 — 2023-02-03
- 0.1.16 — 2023-02-02
- 0.1.15 — 2023-02-02
- 0.1.14 — 2023-02-02
- 0.1.13 — 2023-02-02
- 0.1.12 — 2023-02-02
- … 18 more at https://npm.io/package/@dcspark/adalib/versions

## README

# Adalib

**Cardano** friendly API

Adalib implements a `Connector` interface that complies with WalletConnect's standards.

It attempts to closely emulate the CIP-30 standard within the connectors. A dapp developer can use these connectors to retrieve the enabled CIP-30 API, and benefit from the included typings this library provides.

You will need a Walletconnect Project ID to use this library. You can get one by signing up and registering a dapp at https://walletconnect.com/.

For an examples, see `App.tsx` and `Home.tsx` in [example project](adalib-example/) in this repo.

For further docs, see [docs](docs/docs).
### API

- Connect Wallet:
  - Flint
  - WalletConnect
  - Injected Connector

### Init

The init function needs to be called to prepare `adalib` to be able to call all
the functions in its API.

```ts
import { 
  init, 
  cardanoMainnetWalletConnect,
  FlintConnector, 
  WalletConnectConnector 
} from 'adalib'

init(
  {
    // The different connector methodologies that will be used.
    // FlintConnector will interact with injected Flint Wallet using browser
    // extension, while WalletConnectConnector can be used to interact with all
    // wallets that support the WalletConnect protocol.
    connectors: [
      new FlintConnector(),
      new WalletConnectConnector({
        relayerRegion: 'wss://relay.walletconnect.com',
        metadata: {
          description: 'Test app for adalib',
          name: 'Test Adalib dApp',
          icons: ['https://avatars.githubusercontent.com/u/37784886'],
          url: 'http://localhost:3000'
        },
        autoconnect: true,
        qrcode: true
      })
    ],
    // Name of the connector to be used.
    // The connector needs to be registered in the connectors field above.
    // This can be switched later using `switchConnector` function.
    connectorName: WalletConnectConnector.connectorName(),
    // The name of the chain and network to use.
    // Here, `mainnet` refers to the cardano mainnet network.
    chosenChain: cardanoMainnetWalletConnect()
  },
  WALLETCONNECT_PROJECT_ID
)
```

### Connect Wallet

The connect function can be used to connect a wallet to a dApp. The wallet
chosen needs to be configured in the `init` function above.

With the WalletConnect connector, if the user closes the QR modal without
scanning the QR code, the `connect` function will throw an error. It is important
to catch this error and handle it appropriately to ensure your application does not hang.

```ts
import { connect, getActiveConnector } from 'adalib'

const address = await connect()

// OR

getActiveConnector()
      .enable()
      .then(api => {
        console.log('CIP-30 API Created', { api });
        // Store the enabled CIP-30 api in state and make subsequent calls to it
        setEnabledAPI(api);
      });
```

### Watch Address

Instead of retrieving the address once on the connect function, one can globally
watch address changes using the `watchAddress` API.

```ts
import { watchAddress, connect } from 'adalib'

watchAddress(address => {
  console.log({ address })
})

// calls `enable` on the active connector
connect()
```


### Switch Connector

```ts
import { switchConnector, FlintConnector, connect } from 'adalib'

switchConnector(FlintConnector.connectorName)

const flintWalletAPI = await connect()
```

Note: Sometimes the connection will die and you will need to reconnect.
The connectors have an isConnected(timeout) function that can be used to check
if the connection is still alive. If it is not, you can call the `connect` function
again. The timeout is in milliseconds. The default is 10,000ms.

The walletconnect connector will ping the connected wallet. If there is no response
before the timeout, it will assume the connection is dead and will return false.

The injected connector will check the network ID. If there is no response before
the timeout, it will assume the connection is dead and will return false.

```ts
import { getActiveConnector } from 'adalib'

const connector = getActiveConnector()
const isStillConnected = await connector.isConnected(1000)
if (!isStillConnected) {
  await connector.enable()
}
```

<!-- # Folders
 -->
## Example

Example app written in react, for testing in the adalib-example folder.

---
_Source: https://npm.io/package/@dcspark/adalib · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
