## @catalogfi/wallets

The package `@catalogfi/wallets` provides Bitcoin, EVM, and HTLC wallets.

### Installation

```
npm install @catalogfi/wallets
```

For more information regarding using the library in different environments see [setup](https://github.com/catalogfi/catalog.js-v2/tree/main/packages/wallets#setup).

### Usage

1. Sending bitcoin to an address
   1. Using OTA wallet: ([should send funds and send the change back to itself](https://github.com/catalogfi/catalog.js-v2/blob/3019559e8857bf0b54b0c3affcdcad580b868947/packages/wallets/src/lib/bitcoin/ota.spec.ts#L83))
   2. Using BitcoinWallet: ([should send funds and send the change back to itself](https://github.com/catalogfi/catalog.js-v2/blob/3019559e8857bf0b54b0c3affcdcad580b868947/packages/wallets/src/lib/bitcoin/wallet.spec.ts#L88))
2. Should spend from a bitcoin script
   1. Using OTA wallet: ([should be able to spend from a script](https://github.com/catalogfi/catalog.js-v2/blob/3019559e8857bf0b54b0c3affcdcad580b868947/packages/wallets/src/lib/bitcoin/ota.spec.ts#L1148))
   2. Using BitcoinWallet: ([should be able to spend from a script](https://github.com/catalogfi/catalog.js-v2/blob/3019559e8857bf0b54b0c3affcdcad580b868947/packages/wallets/src/lib/bitcoin/wallet.spec.ts#L120))
3. Creating an atomic swap from a bitcoin wallet: ([inits should happen](https://github.com/catalogfi/catalog.js-v2/blob/3019559e8857bf0b54b0c3affcdcad580b868947/packages/wallets/src/lib/bitcoin/htlc.spec.ts#L29))
4. Redeeming atomic swaps using a bitcoin wallet: ([redeems should happen with the right secret](https://github.com/catalogfi/catalog.js-v2/blob/3019559e8857bf0b54b0c3affcdcad580b868947/packages/wallets/src/lib/bitcoin/htlc.spec.ts#L108))
5. Refunding atomic swaps using a bitcoin wallet: ([refunds should happen after expiry](https://github.com/catalogfi/catalog.js-v2/blob/3019559e8857bf0b54b0c3affcdcad580b868947/packages/wallets/src/lib/bitcoin/htlc.spec.ts#L157))
6. [Interacting with the bitcoin blockchain using the bitcoin provider](https://github.com/catalogfi/catalog.js-v2/blob/main/packages/wallets/src/lib/bitcoin/provider.spec.ts)
7. Creating an atomic swap using an EVM wallet: ([inits should happen with the correct params](https://github.com/catalogfi/catalog.js-v2/blob/3019559e8857bf0b54b0c3affcdcad580b868947/packages/wallets/src/lib/evm/htlc.spec.ts#L129))
8. Redeeming atomic swaps using an EVM wallet: ([redeem should happen with a valid secret and sufficient funds](https://github.com/catalogfi/catalog.js-v2/blob/3019559e8857bf0b54b0c3affcdcad580b868947/packages/wallets/src/lib/evm/htlc.spec.ts#L208))
9. Refunding atomic swaps using an EVM wallet: ([refunds should happen after expiry and with sufficient funds](https://github.com/catalogfi/catalog.js-v2/blob/3019559e8857bf0b54b0c3affcdcad580b868947/packages/wallets/src/lib/evm/htlc.spec.ts#L281))
10. Creating a bitcoin-EVM atomic swap: ([should be able to perform a swap between bitcoin and EVM wallets](https://github.com/catalogfi/catalog.js-v2/blob/main/packages/wallets/src/lib/htlc.spec.ts))

## Setup

### Node

No extra setup is required and both `cjs` and `esm` modules are supported.

### Vite

Install the vite plugins:

```
npm install vite-plugin-wasm vite-plugin-node-polyfills --save-dev
```

and update your `vite.config.ts` as follows:

```
import { defineConfig } from "vite";
import wasm from "vite-plugin-wasm";
import { nodePolyfills } from "vite-plugin-node-polyfills";

export default defineConfig({
	plugins: [
		nodePolyfills(),
		wasm(),
		//other plugins
	],
	//other settings
});
```

### Webpack

If you're using webpack with a framework like next, then in your webpack config, (if you're using NextJS, this can be found in `next.config.js` ) add support for wasm as shown below:

```
/** @type {import('next').NextConfig} */
const nextConfig = {
	//other nextConfig options
	webpack: function (config, options) {
		//other webpack config options
		config.experiments = {
			...config.experiments,
			asyncWebAssembly: true,
		};
	return config;
	},
};
module.exports = nextConfig;
```
