Skip to content
This repository was archived by the owner on Dec 12, 2024. It is now read-only.

Commit 49ba2b6

Browse files
authored
add README (#146)
* add README * badge * update readme
1 parent 7f98548 commit 49ba2b6

File tree

1 file changed

+146
-0
lines changed

1 file changed

+146
-0
lines changed

bound/typescript/README.md

+146
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
# tbDEX SDK
2+
3+
[![npm version](https://badge.fury.io/js/%40tbdex%2Fsdk.svg)](https://badge.fury.io/js/%40tbdex%2Fsdk)
4+
5+
A TypeScript SDK for interacting with the tbDEX protocol. This SDK is built on a Rust core with WebAssembly bindings, offering high performance, type safety, and interoperability. This library that can be used to create, parse, verify, and validate the tbDEX Messages and Resources defined in the [protocol specification](https://github.com/TBD54566975/tbdex/blob/main/README.md). This library also includes an HTTP client that can be used to send tbdex messages to PFIs.
6+
7+
8+
## Features
9+
10+
- **Complete tbDEX Protocol Support**: Implements all tbDEX message types and resources
11+
- **Type-Safe**: Full TypeScript support with comprehensive type definitions
12+
- **Cross-Platform**: Works in Node.js and modern browsers
13+
14+
## Installation
15+
16+
```bash
17+
npm install @tbdex/sdk
18+
```
19+
20+
## Basic Usage
21+
22+
### Message Creation
23+
24+
```typescript
25+
import {
26+
Rfq,
27+
CreateRfqData,
28+
} from '@tbdex/sdk';
29+
30+
const createRfqData: CreateRfqData = {
31+
claims: [verifiableCredential],
32+
offeringId,
33+
payin: {
34+
amount: '101',
35+
kind: 'USD_LEDGER',
36+
paymentDetails: null,
37+
},
38+
payout: {
39+
kind: 'MOMO_MPESA',
40+
paymentDetails: {
41+
phoneNumber: '867-5309',
42+
reason: 'cause',
43+
},
44+
},
45+
};
46+
47+
const rfq = Rfq.create(pfiDidUri, bearerDid.did.uri, createRfqData);
48+
49+
await rfq.sign(bearerDid);
50+
await rfq.verify();
51+
```
52+
53+
### Message Parsing
54+
```typescript
55+
56+
import {
57+
Rfq,
58+
} from '@tbdex/sdk';
59+
60+
const jsonMessage = "<SERIALIZED_MESSAGE>"
61+
const rfq = Rfq.fromJSONString(jsonMessage);
62+
```
63+
64+
### Http Client Create Exchange Flow
65+
```typescript
66+
import {
67+
Rfq,
68+
CreateRfqData,
69+
BearerDid,
70+
getOfferings,
71+
createExchange,
72+
getExchange,
73+
Exchange,
74+
} from '@tbdex/sdk';
75+
76+
// Step 1: Fetch offerings
77+
console.log('1. Fetching offerings...');
78+
const offerings = await getOfferings(pfiDidUri);
79+
if (!offerings || offerings.length === 0) {
80+
throw new Error('No offerings available.');
81+
}
82+
83+
const offeringId = offerings[0].metadata.id;
84+
console.log(`Successfully fetched offering ID: ${offeringId}\n`);
85+
86+
// Step 2: Create exchange (RFQ)
87+
console.log('2. Creating exchange...');
88+
const createRfqData: CreateRfqData = {
89+
claims: [verifiableCredential],
90+
offeringId,
91+
payin: {
92+
amount: '101',
93+
kind: 'USD_LEDGER',
94+
paymentDetails: null,
95+
},
96+
payout: {
97+
kind: 'PAYOUT_KIND',
98+
paymentDetails: {
99+
phoneNumber: '867-5309',
100+
reason: 'cause',
101+
},
102+
},
103+
};
104+
105+
const rfq = Rfq.create(pfiDidUri, bearerDid.did.uri, createRfqData);
106+
await rfq.sign(bearerDid);
107+
await rfq.verify();
108+
await createExchange(rfq);
109+
110+
const exchangeId = rfq.metadata.exchangeId;
111+
console.log(`Created exchange with ID: ${exchangeId}\n`);
112+
```
113+
114+
115+
## Message Types
116+
117+
The SDK supports all tbDEX message types:
118+
119+
- **RFQ (Request for Quote)**: Initial request for exchange
120+
- **Quote**: Response with exchange terms
121+
- **Order**: Acceptance of quote terms
122+
- **OrderInstructions**: Payment process details
123+
- **OrderStatus**: Transaction status updates
124+
- **Close**: Exchange completion notification
125+
- **Cancel**: Exchange cancellation
126+
127+
## Resources
128+
129+
Supported tbDEX resources:
130+
131+
- **Offering**: Defines exchange terms and requirements
132+
- **Balance**: Shows available currency amounts
133+
134+
## Error Handling
135+
136+
The SDK provides detailed error information for common scenarios:
137+
138+
```typescript
139+
try {
140+
await rfq.verify();
141+
} catch (error) {
142+
if (error instanceof TbdexError) {
143+
console.error('Verification failed:', error.message);
144+
}
145+
}
146+
```

0 commit comments

Comments
 (0)