@hashlock-tech/sdk 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +250 -0
- package/dist/index.cjs +539 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +446 -0
- package/dist/index.d.ts +446 -0
- package/dist/index.js +507 -0
- package/dist/index.js.map +1 -0
- package/package.json +56 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 HashLock Tech
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
# @hashlock/sdk
|
|
2
|
+
|
|
3
|
+
TypeScript SDK for [HashLock](https://hashlock.tech) — institutional OTC trading with HTLC atomic settlement on Ethereum and Bitcoin.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @hashlock/sdk
|
|
9
|
+
# or
|
|
10
|
+
pnpm add @hashlock/sdk
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Quick Start
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { HashLock } from '@hashlock/sdk';
|
|
17
|
+
|
|
18
|
+
const hl = new HashLock({
|
|
19
|
+
endpoint: 'http://142.93.106.129/graphql',
|
|
20
|
+
accessToken: 'your-jwt-token',
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
// Create an RFQ to sell 1 ETH for USDT
|
|
24
|
+
const rfq = await hl.createRFQ({
|
|
25
|
+
baseToken: 'ETH',
|
|
26
|
+
quoteToken: 'USDT',
|
|
27
|
+
side: 'SELL',
|
|
28
|
+
amount: '1.0',
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
console.log(`RFQ created: ${rfq.id}`);
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Authentication
|
|
35
|
+
|
|
36
|
+
Get a JWT token by logging into the HashLock platform, then pass it to the SDK:
|
|
37
|
+
|
|
38
|
+
```ts
|
|
39
|
+
const hl = new HashLock({
|
|
40
|
+
endpoint: 'http://142.93.106.129/graphql',
|
|
41
|
+
accessToken: 'eyJhbGciOiJIUzI1NiIs...',
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
// Or update the token later
|
|
45
|
+
hl.setAccessToken('new-token');
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## RFQ Trading
|
|
49
|
+
|
|
50
|
+
### Create an RFQ (Request for Quote)
|
|
51
|
+
|
|
52
|
+
```ts
|
|
53
|
+
const rfq = await hl.createRFQ({
|
|
54
|
+
baseToken: 'BTC',
|
|
55
|
+
quoteToken: 'USDT',
|
|
56
|
+
side: 'BUY',
|
|
57
|
+
amount: '0.5',
|
|
58
|
+
expiresIn: 300, // 5 minutes
|
|
59
|
+
});
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### Respond with a Quote
|
|
63
|
+
|
|
64
|
+
```ts
|
|
65
|
+
const quote = await hl.submitQuote({
|
|
66
|
+
rfqId: rfq.id,
|
|
67
|
+
price: '68500.00',
|
|
68
|
+
amount: '0.5',
|
|
69
|
+
});
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### Accept a Quote (creates a Trade)
|
|
73
|
+
|
|
74
|
+
```ts
|
|
75
|
+
const accepted = await hl.acceptQuote(quote.id);
|
|
76
|
+
// accepted.trade.id -> trade ready for settlement
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
### List & Query
|
|
80
|
+
|
|
81
|
+
```ts
|
|
82
|
+
const { rfqs, total } = await hl.listRFQs({ status: 'ACTIVE', page: 1 });
|
|
83
|
+
const rfq = await hl.getRFQ('rfq-uuid');
|
|
84
|
+
const quotes = await hl.getQuotes('rfq-uuid');
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
## HTLC Settlement — ETH / ERC-20
|
|
88
|
+
|
|
89
|
+
After a trade is accepted, both parties lock assets in HTLC contracts.
|
|
90
|
+
|
|
91
|
+
### Record an HTLC Lock (after on-chain tx)
|
|
92
|
+
|
|
93
|
+
```ts
|
|
94
|
+
// 1. Send ETH lock tx on-chain via ethers.js / viem
|
|
95
|
+
// 2. Record it in HashLock:
|
|
96
|
+
const result = await hl.fundHTLC({
|
|
97
|
+
tradeId: 'trade-uuid',
|
|
98
|
+
txHash: '0xabc123...',
|
|
99
|
+
role: 'INITIATOR',
|
|
100
|
+
timelock: Math.floor(Date.now() / 1000) + 3600,
|
|
101
|
+
hashlock: '0xdef456...',
|
|
102
|
+
chainType: 'evm',
|
|
103
|
+
});
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
### Claim an HTLC (reveal preimage)
|
|
107
|
+
|
|
108
|
+
```ts
|
|
109
|
+
const claimed = await hl.claimHTLC({
|
|
110
|
+
tradeId: 'trade-uuid',
|
|
111
|
+
txHash: '0xclaim...',
|
|
112
|
+
preimage: '0xsecret...',
|
|
113
|
+
chainType: 'evm',
|
|
114
|
+
});
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
### Refund (after timelock expiry)
|
|
118
|
+
|
|
119
|
+
```ts
|
|
120
|
+
const refunded = await hl.refundHTLC({
|
|
121
|
+
tradeId: 'trade-uuid',
|
|
122
|
+
txHash: '0xrefund...',
|
|
123
|
+
});
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
### Check HTLC Status
|
|
127
|
+
|
|
128
|
+
```ts
|
|
129
|
+
const status = await hl.getHTLCStatus('trade-uuid');
|
|
130
|
+
console.log(status?.initiatorHTLC?.status); // 'ACTIVE'
|
|
131
|
+
console.log(status?.counterpartyHTLC?.status); // 'PENDING'
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
## HTLC Settlement — Bitcoin
|
|
135
|
+
|
|
136
|
+
Bitcoin HTLCs use P2WSH scripts (no smart contract deployment needed).
|
|
137
|
+
|
|
138
|
+
### Prepare a Bitcoin HTLC
|
|
139
|
+
|
|
140
|
+
```ts
|
|
141
|
+
const btcHtlc = await hl.prepareBitcoinHTLC({
|
|
142
|
+
tradeId: 'trade-uuid',
|
|
143
|
+
role: 'INITIATOR',
|
|
144
|
+
senderPubKey: '02abc...', // 33-byte compressed pubkey
|
|
145
|
+
receiverPubKey: '03def...',
|
|
146
|
+
timelock: Math.floor(Date.now() / 1000) + 7200,
|
|
147
|
+
amountSats: '100000', // 0.001 BTC
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
console.log(`Send BTC to: ${btcHtlc.htlcAddress}`);
|
|
151
|
+
// Fund this P2WSH address with your Bitcoin wallet
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
### Claim a Bitcoin HTLC
|
|
155
|
+
|
|
156
|
+
```ts
|
|
157
|
+
// Build unsigned PSBT
|
|
158
|
+
const psbt = await hl.buildBitcoinClaimPSBT({
|
|
159
|
+
tradeId: 'trade-uuid',
|
|
160
|
+
htlcId: btcHtlc.htlcId,
|
|
161
|
+
preimage: '0xsecret...',
|
|
162
|
+
destinationPubKey: '02abc...',
|
|
163
|
+
feeRate: 10, // sat/vB
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
// Sign with wallet (Xverse, Leather, UniSat, etc.)
|
|
167
|
+
const signedTx = await wallet.signPsbt(psbt.psbtBase64);
|
|
168
|
+
|
|
169
|
+
// Broadcast
|
|
170
|
+
const broadcast = await hl.broadcastBitcoinTx({
|
|
171
|
+
tradeId: 'trade-uuid',
|
|
172
|
+
txHex: signedTx,
|
|
173
|
+
});
|
|
174
|
+
console.log(`BTC claimed: ${broadcast.txid}`);
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
## Cross-Chain Atomic Swap (ETH ↔ BTC)
|
|
178
|
+
|
|
179
|
+
```ts
|
|
180
|
+
// Alice (ETH side) locks USDT on Ethereum
|
|
181
|
+
await hl.fundHTLC({
|
|
182
|
+
tradeId, txHash: evmTxHash, role: 'INITIATOR',
|
|
183
|
+
hashlock, timelock: now + 7200, chainType: 'evm',
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
// Bob (BTC side) locks BTC on Bitcoin
|
|
187
|
+
const btc = await hl.prepareBitcoinHTLC({
|
|
188
|
+
tradeId, role: 'COUNTERPARTY',
|
|
189
|
+
senderPubKey: bobPub, receiverPubKey: alicePub,
|
|
190
|
+
timelock: now + 3600, amountSats: '100000',
|
|
191
|
+
});
|
|
192
|
+
// Bob funds the P2WSH address, then:
|
|
193
|
+
await hl.fundHTLC({
|
|
194
|
+
tradeId, txHash: btcFundingTxid, role: 'COUNTERPARTY',
|
|
195
|
+
chainType: 'bitcoin', redeemScript: btc.redeemScript,
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
// Alice claims BTC (reveals preimage)
|
|
199
|
+
// Bob sees preimage on-chain → claims USDT on Ethereum
|
|
200
|
+
// Trade complete!
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
## Error Handling
|
|
204
|
+
|
|
205
|
+
```ts
|
|
206
|
+
import { HashLockError, GraphQLError, AuthError, NetworkError } from '@hashlock/sdk';
|
|
207
|
+
|
|
208
|
+
try {
|
|
209
|
+
await hl.getTrade('bad-id');
|
|
210
|
+
} catch (err) {
|
|
211
|
+
if (err instanceof AuthError) {
|
|
212
|
+
// Token expired — refresh and retry
|
|
213
|
+
} else if (err instanceof GraphQLError) {
|
|
214
|
+
console.error('API error:', err.errors);
|
|
215
|
+
} else if (err instanceof NetworkError) {
|
|
216
|
+
console.error('Network issue:', err.message);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
## Configuration
|
|
222
|
+
|
|
223
|
+
```ts
|
|
224
|
+
const hl = new HashLock({
|
|
225
|
+
endpoint: 'http://142.93.106.129/graphql', // mainnet
|
|
226
|
+
accessToken: 'jwt-token',
|
|
227
|
+
timeout: 30000, // 30s (default)
|
|
228
|
+
retries: 3, // retry count (default)
|
|
229
|
+
});
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
| Option | Type | Default | Description |
|
|
233
|
+
|--------|------|---------|-------------|
|
|
234
|
+
| `endpoint` | `string` | — | GraphQL API URL (required) |
|
|
235
|
+
| `accessToken` | `string` | — | JWT bearer token |
|
|
236
|
+
| `timeout` | `number` | `30000` | Request timeout (ms) |
|
|
237
|
+
| `retries` | `number` | `3` | Retry attempts for transient failures |
|
|
238
|
+
| `fetch` | `typeof fetch` | `globalThis.fetch` | Custom fetch implementation |
|
|
239
|
+
|
|
240
|
+
## Mainnet Contracts (Ethereum)
|
|
241
|
+
|
|
242
|
+
| Contract | Address |
|
|
243
|
+
|----------|---------|
|
|
244
|
+
| HashedTimelockEther | [`0x0CEDC56b17d714dA044954EE26F38e90eC10434A`](https://etherscan.io/address/0x0cedc56b17d714da044954ee26f38e90ec10434a) |
|
|
245
|
+
| HashedTimelockEtherFee | [`0xfBAEA1423b5FBeCE89998da6820902fD8f159014`](https://etherscan.io/address/0xfbaea1423b5fbece89998da6820902fd8f159014) |
|
|
246
|
+
| HashedTimelockERC20Fee | [`0x4B65490D140Bab3DB828C2386e21646Ed8c4D072`](https://etherscan.io/address/0x4b65490d140bab3db828c2386e21646ed8c4d072) |
|
|
247
|
+
|
|
248
|
+
## License
|
|
249
|
+
|
|
250
|
+
MIT
|