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

@tbdex/protocol

v2.2.1

Published

Library that includes type definitions for tbdex messages

Downloads

3,635

Readme

tbDEX Protocol

Library that can be used to create, parse, verify, and validate the tbDEX Messages and Resources defined in the protocol specification

Table of Contents

Installation

npm install @tbdex/protocol

Usage

Message Creation

There's a concrete class for each Message Kind. These classes can be used to create messages. e.g.

import { DevTools, Rfq } from '@tbdex/protocol'

const rfq = Rfq.create({
  metadata : { from: alice.did, to: 'did:ex:pfi' },
  data     : {
    offeringId  : 'abcd123',
    payinMethod : {
      kind           : 'DEBIT_CARD',
      paymentDetails : {
        'cardNumber'     : '1234567890123456',
        'expiryDate'     : '12/22',
        'cardHolderName' : 'Ephraim Bartholomew Winthrop',
        'cvv'            : '123'
      }
    },
    payoutMethod: {
      kind           : 'BTC_ADDRESS',
      paymentDetails : {
        btcAddress: '1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa'
      }
    },
    payinAmount : '200.00',
    claims      : ['']
  }
})

await rfq.sign(alice)

console.log(JSON.stringify({ message: rfq }, null, 2))

Message Parsing

All messages can be parsed from json into an instance of the Message Kind's class using the parseMessage method. e.g.

import { parseMessage } from '@tbdex/protocol'

const jsonMessage = "<SERIALIZED_MESSAGE>"
const message = await parseMessage(jsonMessage)

if (message.isRfq()) {
  // rfq specific logic
}

If you know what kind of message you are expecting, you can use a message kind specific .parse() method. e.g.

import { Rfq, Quote } from '@tbdex/protocol'

const jsonRfq = "<SERIALIZED_RFQ>"
const rfq = Rfq.parse(jsonRfq)

const jsonQuote = "<SERIALIZED_QUOTE>"
const quote = Quote.parse(jsonQuote)

Parsing a message includes format validation, signature integrity checking, and other validations specific to each message kind.

Message Validation and Verification

Given an instance of a Message or Resource, you can perform validations with different levels of granularity.

  • Message#verify() performs format validation, a signature integrity check, and validations specific to a each message kind. However, it does NOT perform validations that require knowledge of other messages in an exchange.
  • Message#verifySignature() performs a signature integrity check.
  • Message#validate() performs a format validation against the official TBDex JSON Schemas. It will fail if the message has not yet been signed.

Rfq Validation

An Rfq must also be validated against its corresponding Offering.

import { Offering, Rfq } from '@tbdex/protocol'

const offering = Offering.parse("<SERIALIZED_OFFERING>")
const rfq = Rfq.parse("<SERIALIZED_RFQ>")
await rfq.verifyOfferingRequirements(offering)