import crypto from 'crypto';
const API_KEY = process.env.PAYWARD_API_KEY;
const API_SECRET = process.env.PAYWARD_API_SECRET;
const BASE_URL = 'https://api.services.payward.com';
function getPaywardSignature(urlpath, body, secret, nonce) {
const noncePayload = body === null ? String(nonce) : String(nonce) + body;
const sha256Hash = crypto.createHash('sha256').update(noncePayload).digest();
const message = Buffer.concat([Buffer.from(urlpath), sha256Hash]);
const secretBuffer = Buffer.from(secret, 'base64');
const hmac = crypto.createHmac('sha512', secretBuffer);
hmac.update(message);
return hmac.digest('base64');
}
async function listAssets() {
const endpoint = '/v1/assets';
const nonce = process.hrtime.bigint().toString();
const signature = getPaywardSignature(endpoint, null, API_SECRET, nonce);
const response = await fetch(BASE_URL + endpoint, {
method: 'GET',
headers: {
'API-Key': API_KEY,
'API-Nonce': nonce,
'API-Sign': signature,
},
});
return response.json();
}
const assets = await listAssets();
console.log(assets);