# @zerohash-sdk/crypto-withdrawals-js

A JavaScript SDK that enables frontend applications to seamlessly integrate with the Connect Crypto Withdrawals product.

Connect Crypto Withdrawals provides a secure, customizable flow for withdrawing crypto assets directly within your application.

## Installation

### Via NPM (recommended)

```bash
npm install @zerohash-sdk/crypto-withdrawals-js
```

```javascript
import { CryptoWithdrawals } from '@zerohash-sdk/crypto-withdrawals-js';
```

### Via CDN

```html
<script
  type="module"
  src="https://sdk-cdn.zerohash.com/crypto-withdrawals-web/index.js"
></script>
```

Or import directly in your JavaScript code:

```javascript
import { CryptoWithdrawals } from 'https://sdk-cdn.zerohash.com/crypto-withdrawals-web/index.js';
```

## Getting Started

### 1. Import the CryptoWithdrawals module

```javascript
import { CryptoWithdrawals } from '@zerohash-sdk/crypto-withdrawals-js';
```

### 2. Initialize and render the widget

```javascript
const cryptoWithdrawals = new CryptoWithdrawals({
  jwt: 'your-jwt-token',
  env: 'prod',
  theme: 'auto',
  onCompleted: ({ withdrawalRequestId }) => {
    console.log('Withdrawal completed:', withdrawalRequestId);
  },
  onFailed: ({ withdrawalRequestId, statusDetails }) => {
    // statusDetails is the only explanation of the failure available.
    console.log('Withdrawal failed:', withdrawalRequestId, statusDetails);
  },
  onError: ({ errorCode, reason }) => {
    console.error('Error:', errorCode, 'Reason:', reason);
  },
  onClose: () => console.log('Widget closed'),
  onEvent: ({ type, data }) => console.log('Event:', type, data),
  onLoaded: () => console.log('Widget loaded and ready'),
});

await cryptoWithdrawals.render(document.getElementById('crypto-withdrawals-container'));

cryptoWithdrawals.destroy();
```

### 2.1 Using TypeScript (optional)

```typescript
import { CryptoWithdrawals, CryptoWithdrawalsConfig } from '@zerohash-sdk/crypto-withdrawals-js';

const config: CryptoWithdrawalsConfig = {
  jwt: 'your-jwt-token',
  env: 'cert',
  onCompleted: ({ withdrawalRequestId }) => {
    console.log(withdrawalRequestId);
  },
};

const cryptoWithdrawals = new CryptoWithdrawals(config);
await cryptoWithdrawals.render(document.getElementById('container')!);
```

## API Reference

### Configuration

| Prop          | Type                                                                    | Required | Default  | Description                                              |
| ------------- | ----------------------------------------------------------------------- | -------- | -------- | -------------------------------------------------------- |
| `jwt`         | `string`                                                                | Yes      | -        | JWT token for authentication with Connect                |
| `env`         | `"prod" \| "cert" \| "dev" \| "local"`                                  | No       | `"prod"` | Target environment                                       |
| `theme`       | `"auto" \| "light" \| "dark"`                                           | No       | `"auto"` | Theme mode for the interface                             |
| `onCompleted` | `({ withdrawalRequestId, status, assetId, networkId, amount }) => void` | No       | -        | Callback when the withdrawal flow completes successfully |
| `onFailed`    | `({ withdrawalRequestId }) => void`                                     | No       | -        | Callback when a withdrawal ends in a failed state        |
| `onError`     | `({ errorCode, reason }) => void`                                       | No       | -        | Callback for error events                                |
| `onClose`     | `() => void`                                                            | No       | -        | Callback when the widget is closed                       |
| `onEvent`     | `({ type, data }) => void`                                              | No       | -        | Callback for general events                              |
| `onLoaded`    | `() => void`                                                            | No       | -        | Callback when the widget is loaded and ready             |

### Methods

#### `render(container: HTMLElement): Promise<void>`

#### `updateConfig(config: Partial<CryptoWithdrawalsConfig>): void`

#### `destroy(): void`

#### `isRendered(): boolean`

#### `getConfig(): CryptoWithdrawalsConfig`

## Browser Support

- Chrome/Edge 90+
- Firefox 88+
- Safari 14+
- All modern browsers with Web Components support

### `onCompleted` payload

`onCompleted` receives `{ withdrawalRequestId, status, assetId, networkId, amount }`.
What `status` and `amount` mean depends on the flow that completed:

| Flow             | `status`    | `amount`                                |
| ---------------- | ----------- | --------------------------------------- |
| Classic (manual) | `SUBMITTED` | The requested amount, before fees       |
| Auth             | `CONFIRMED` | The net amount the destination received |

`SUBMITTED` means the withdrawal was submitted, not confirmed on-chain.
`assetId` is the asset ticker (e.g. `BTC`, `USDC`) and `networkId` the network
chain code (e.g. `bitcoin`, `ethereum`).

### `onFailed` and `onError` on a failed withdrawal

A failed withdrawal fires **both** `onFailed` and `onError`, so existing
integrations that only implemented `onError` keep working.

`onFailed` is the correct callback: it carries the withdrawal's details,
including `statusDetails`, the only explanation of the failure available. Build
against `onFailed`, and if you handle both, guard against counting one failure
twice. Firing `onError` for a failed withdrawal is deprecated and will be
removed in a future major version.

## More Information & Support

For comprehensive documentation or support about Connect, visit our [Documentation Page](https://docs.zerohash.com/).
