# Getting Started

{% embed url="<https://github.com/pintswap/pintswap-sdk>" %}

## Installation

To add the SDK to your project, use the command:

```bash
yarn add @pintswap/sdk
```

## Initialization

The minimal requirement for a Pintswap instance to be instantiated is a signer object which conforms to the `ethers.Signer` interface. If the `Pintswap.initialize` method is used to construct a Pintswap instance, a PeerId will be generated randomly.

```typescript
import { Pintswap } from "@pintswap/sdk";
import { ethers } from "ethers";
(async () => {
  const provider = new ethers.InfuraProvider('mainnet') // PintSwap currently only supports mainnet
  const signer = ethers.Wallet.createRandom().connect(provider);
  const pintswap = await Pintswap.initialize({ signer });
  pintswap.on('peer:discovery', (peer) => {
    pintswap.logger.info('discovered peer:');
    pintswap.logger.info(peer);
  });
  await pintswap.startNode(); 
})().catch((err) => console.error(err));
```

It is also possible to instantiate a Pintswap instance with a deterministically generated PeerId, using the provided signer object and a salt phrase. The signer provided via a call to `Pintswap.fromPassword({ signer, password })` will be used to sign a message of the following structure:

```typescript
Welcome to PintSwap!
PintP2P v1.0.0
0x3890267d5092ba03d86870b24061b034d41617b0f6e9f024bce2680884a959e9
```

The third line of this message is computed as:

```typescript
keccak(/pintp2p/1.0.0/your-password/)
```

Where `your-password` is the password supplied to the function.

```typescript
import { Pintswap } from "@pintswap/sdk";
import { ethers } from "ethers";
(async () => {
  const provider = new ethers.InfuraProvider('mainnet') // PintSwap currently only supports mainnet
  const signer = ethers.Wallet.createRandom().connect(provider);
  const pintswap = await Pintswap.fromPassword({ signer, password: await signer.getAddress() }); // the PeerId will be the same every time the Pintswap instance is created this way
  pintswap.on('peer:discovery', (peer) => {
    pintswap.logger.info('discovered peer:');
    pintswap.logger.info(peer);
  });
  await pintswap.startNode();
})().catch((err) => console.error(err));
```

Note that a PeerId cannot be shared between two actively running peers on the network. The PeerId is integral to the way libp2p routes webRTC traffic, but it is also used as your identity on PintSwap.

## Initiate Connection

It is required to explicitly start and stop the PintSwap connection to the p2p network using the following:

### Start

```typescript
await pintswap.startNode();
```

Starts P2P network connection.

### Stop

```typescript
await pintswap.stopNode();
```

Stops P2P connectivity associated with the PintSwap instance.
