x-signature
and x-timestamp
are required to be included in the headers of each request to verify the identify of request sender.
Generating x-signature
const qs = require('querystring')
const ethers = require('ethers')
function ksort(obj) {
let sortObj = {}
keys = Object.keys(obj)
keys.sort()
keys.forEach((key) => {
sortObj[key] = obj[key]
})
return sortObj
}
// generate the signature for request parameters
function getSignStr(data, timestamp) {
// 1.parameters are sorted in ascending order by key dictionary
const params = ksort({ ...data, "timestamp": timestamp })
// 2.generate string in query format, like 'key1=value1&key2=value2'
const signStr = qs.stringify(params)
return signStr
}
const data = {
userAddress:'' // address of the user wallet
}
const timestamp = Date.now()
const signStr = getSignStr(data,timestamp)
// sign with private key or ask user to sign the signStr
const wallet = new ethers.Wallet(privateKey)
const x_signature = await wallet.signMessage(signStr)
Generating x-timestamp
const timestamp = Date.now() // Timestamp in milliseconds