-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnftcdn_hmac.js
executable file
·33 lines (24 loc) · 1.08 KB
/
nftcdn_hmac.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/usr/bin/env node
const crypto = require('crypto');
const { URLSearchParams } = require('url');
function nftcdnUrl(domain, key, token, uri, params = {}) {
params.tk = "";
let url = buildUrl(domain, token, uri, params);
// base64url codec requires Node.js >= 16, else 3rd party libraries can be used
params.tk = crypto.createHmac("sha256", key).update(url).digest("base64url");
return buildUrl(domain, token, uri, params);
}
function buildUrl(domain, token, uri, params) {
const searchParams = new URLSearchParams(params);
return `https://${token}.${domain}.nftcdn.io${uri}?${searchParams.toString()}`;
}
// EXAMPLES
// Your nftcdn.io subdomain and secret key
let [domain, key] = ["preprod", Buffer.from("7FoxfBgV2k+RSz6UUts3/fG1edG7oIGXxdtIVCdalaI=", "base64")];
let token = "asset1cpfcfxay6s73xez8srvhf0pydtd9yqs8hyfawv"
// Original image
console.log(nftcdnUrl(domain, key, token, "/image"));
// Resized 256x256 WebP image
console.log(nftcdnUrl(domain, key, token, "/image", { size: 256 }));
// Metadata
console.log(nftcdnUrl(domain, key, token, "/metadata"));