Getting Started
TypeScript library encapsulating the PintSwap p2p stack, protocol definitions, and associated cryptography routines. Embedded within the pintswap-daemon processes as well as the PintSwap web frontend.
Installation
To add the SDK to your project, use the command:
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.
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:
Welcome to PintSwap!
PintP2P v1.0.0
0x3890267d5092ba03d86870b24061b034d41617b0f6e9f024bce2680884a959e9
The third line of this message is computed as:
keccak(/pintp2p/1.0.0/your-password/)
Where your-password
is the password supplied to the function.
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
await pintswap.startNode();
Starts P2P network connection.
Stop
await pintswap.stopNode();
Stops P2P connectivity associated with the PintSwap instance.
Last updated