The x-signature
and x-timestamp
are required in headers of several APIs. Below is a tutorial on how to generate them.
1. Generate x-timestamp
x-timestamp
x-timestamp
is the current Unix timestamp in millisecond.
const x_timestamp = Date.now()
2. Create string for signature
Arrange the request parameters together with timestamp with key in ascending order .
function ksort(obj) {
let sortObj = {}
keys = Object.keys(obj)
keys.sort()
keys.forEach((key) => {
sortObj[key] = obj[key]
})
return sortObj
}
const params = ksort({ ...request_parameters, "timestamp": x_timestamp })
Generate a string in the query format from sorted parameters, e.g. key1=value1&key2=value2
const qs = require('querystring')
const signString = qs.stringify(params)
3. Sign the signString with wallet
Use wallet's private key to sign the signString
. Alternatively, you can use web3Provider
to sign the message when deployed on a website.
const wallet = new ethers.Wallet(privateKey)
const x_signature = await wallet.signMessage(signString)