import { createCipheriv, createHmac, randomBytes } from 'node:crypto'
const BASE_URL = 'https://api.pontisglobe.com'
const API_KEY = 'YOUR_API_KEY'
const ENC_SECRET = 'YOUR_ENCRYPTION_SECRET' // base64url
const HMAC_SECRET = 'YOUR_HMAC_SECRET' // base64url
function encrypt(plain) {
const key = Buffer.from(ENC_SECRET, 'base64url')
const iv = randomBytes(12)
const c = createCipheriv('aes-256-gcm', key, iv)
const ct = Buffer.concat([c.update(JSON.stringify(plain), 'utf8'), c.final()])
return [iv, c.getAuthTag(), ct].map((b) => b.toString('base64url')).join(':')
}
export async function call(path, body, jwt = null) {
const ts = String(Math.floor(Date.now() / 1000))
const enc = encrypt(body)
const sig = createHmac('sha256', Buffer.from(HMAC_SECRET, 'base64url'))
.update(`${ts}.${enc}`)
.digest('hex')
const headers = {
'content-type': 'application/json',
'x-api-key': API_KEY,
'x-timestamp': ts,
'x-signature': sig,
}
if (jwt) headers['authorization'] = `Bearer ${jwt}`
const res = await fetch(`${BASE_URL}${path}`, {
method: 'POST',
headers,
body: JSON.stringify({ data: enc }),
})
return { status: res.status, body: await res.json() }
}