const crypto = require('crypto');
class HDWaaSClient {
constructor(apiKey, secretKey, baseUrl = 'https://api.2settle.io') {
this.apiKey = apiKey;
this.secretKey = secretKey;
this.baseUrl = baseUrl;
}
sign(method, path, body = {}) {
const timestamp = Date.now().toString();
const bodyStr = JSON.stringify(body);
const bodyHash = crypto.createHash('sha256').update(bodyStr).digest('hex');
const payload = `${timestamp}|${method}|${path}|${bodyHash}`;
const hmacKey = crypto.createHash('sha256').update(this.secretKey).digest('hex');
const signature = crypto.createHmac('sha256', hmacKey).update(payload).digest('hex');
return { timestamp, signature, bodyStr };
}
async request(method, path, body) {
const { timestamp, signature, bodyStr } = this.sign(method, path, body);
const response = await fetch(`${this.baseUrl}${path}`, {
method,
headers: {
'Content-Type': 'application/json',
'X-API-Key': this.apiKey,
'X-Timestamp': timestamp,
'X-Signature': signature,
},
body: method !== 'GET' ? bodyStr : undefined,
});
return response.json();
}
async createWallet(options) {
return this.request('POST', '/v1/wallets', {
network: options.network,
crypto: options.crypto,
metadata: options.metadata,
expiresInMinutes: options.expiresInMinutes,
});
}
async getWallet(walletId) {
return this.request('GET', `/v1/wallets/${walletId}`, {});
}
async listWallets(options = {}) {
const params = new URLSearchParams();
if (options.status) params.append('status', options.status);
if (options.limit) params.append('limit', options.limit.toString());
if (options.offset) params.append('offset', options.offset.toString());
const query = params.toString();
const path = `/v1/wallets${query ? '?' + query : ''}`;
return this.request('GET', path, {});
}
}
const client = new HDWaaSClient('pk_your_api_key', 'sk_your_secret_key');
const wallet = await client.createWallet({
network: 'trc20',
crypto: 'USDT',
metadata: { orderId: 'ORD-12345' },
expiresInMinutes: 60,
});
console.log('Deposit address:', wallet.wallet.address);
const status = await client.getWallet(wallet.wallet.id);
console.log('Status:', status.wallet.status);