npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@nimiq/libswap

v1.5.1

Published

Typed Javascript library to handle atomic swaps.

Downloads

101

Readme

libswap - Fastspot Atomic Swap Library

A Typescript handler class to handle and process atomic swaps with Fastspot.

The library does not work only with Fastspot, but it uses the Fastspot API types.

Package

This package can be installed from NPM as

@nimiq/libswap

https://www.npmjs.com/package/@nimiq/libswap

API

import { SwapHandler } from '@nimiq/libswap'

// Setup for swaps from NIM to BTC

// This fulfills the `Swap` type from @nimiq/fastspot-api
const swap = {
    from: {
        asset: 'NIM',
        amount: 1000e5, // 1000 NIM
    },
    to: {
        asset: 'BTC',
        amount: 0.001e8, // 1 mBTC
    },
    hash: '<swap hash (hashRoot) as HEX>',
    contracts: {
        NIM: {
            htlc: {
                address: '<HTLC address>',
                data: '<HTLC creation data as HEX>',
            }
        },
        BTC: {
            htlc: {
                address: '<HTLC address>',
                script: '<HTLC script as HEX>',
            }
        },
        /* For EUR: */
        // EUR: {
        //     htlc: {
        //         address: '<HTLC ID>',
        //     }
        // },
    }
}

/**
 * Initialize a SwapHandler with the swap object, the client for
 * the FROM asset and the client for the TO asset:
 */
const swapHandler = new SwapHandler(swap, nimiqClient, electrumClient)

/****************************************
 ** General process for any swap pair: **
 ****************************************/

/**
 * 1. Wait for swap partner to create their HTLC
 *
 * This method validates the HTLC data against the swap object
 * and resolves when the HTLC is valid.
 *
 * The optional `onUpdate` callback receives the transaction/HTLC object:
 * @type {(tx: Transaction<ToAsset>) => any} onUpdate
 */
await swapHandler.awaitIncoming(onUpdate)

/**
 * 2.A Create our HTLC
 *
 * The optional `onPending` callback receives the transaction object:
 * @type {(tx: Transaction<FromAsset>) => any} onPending
 */
await swapHandler.createOutgoing(serializedFundingTx, onPending)

/**
 * 2.B Alternatively, await HTLC funding from external wallet
 *
 * The optional `onUpdate` callback receives the transaction object:
 * @type {(tx: Transaction<FromAsset>) => any} onUpdate
 */
await swapHandler.awaitOutgoing(onUpdate)

/**
 * 3. Wait for the swap secret to be published on-chain
 */
const secret = await swapHandler.awaitSecret()

/**
 * 4. Settle the incoming HTLC with the swap secret
 *
 * The `serializedSettlementTx` (HEX string) must have a string of 0s (zeros) in place of
 * the swap secret, which will be replaced with the secret automatically.
 */
await swapHandler.settleIncoming(serializedSettlementTx, secret)

/**
 * 5. Await confirmation of settled HTLC
 *
 * This is especially relevant for EUR contracts, as they may take a
 * few minutes to confirm after they were settled.
 *
 * The optional `onUpdate` callback receives the transaction object:
 * @type {(tx: Transaction<ToAsset>) => any} onUpdate
 */
await swapHandler.awaitIncomingConfirmation(onUpdate)

// Done!