-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove config dep in updateContentBlacklist by extracting api signing (…
…#903) * remove config dep in updateContentBlacklist by extracting api signing logic * update imports to use apiSigning * update imports to use apiSigning again * remove blacklist env vars * add WAIT_HOSTS back
- Loading branch information
Showing
13 changed files
with
56 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
const Web3 = require('web3') | ||
const web3 = new Web3() | ||
|
||
/** | ||
* Generate the timestamp and signature for api signing | ||
* @param {object} data | ||
* @param {string} privateKey | ||
*/ | ||
const generateTimestampAndSignature = (data, privateKey) => { | ||
const timestamp = new Date().toISOString() | ||
const toSignObj = { ...data, timestamp } | ||
// JSON stringify automatically removes white space given 1 param | ||
const toSignStr = JSON.stringify(sortKeys(toSignObj)) | ||
const toSignHash = web3.utils.keccak256(toSignStr) | ||
const signedResponse = web3.eth.accounts.sign(toSignHash, privateKey) | ||
|
||
return { timestamp, signature: signedResponse.signature } | ||
} | ||
|
||
/** | ||
* Recover the public wallet address | ||
* @param {*} data obj with structure {...data, timestamp} | ||
* @param {*} signature signature generated with signed data | ||
*/ | ||
// eslint-disable-next-line no-unused-vars | ||
const recoverWallet = (data, signature) => { | ||
let structuredData = JSON.stringify(sortKeys(data)) | ||
const hashedData = web3.utils.keccak256(structuredData) | ||
const recoveredWallet = web3.eth.accounts.recover(hashedData, signature) | ||
|
||
return recoveredWallet | ||
} | ||
|
||
const sortKeys = x => { | ||
if (typeof x !== 'object' || !x) { return x } | ||
if (Array.isArray(x)) { return x.map(sortKeys) } | ||
return Object.keys(x).sort().reduce((o, k) => ({ ...o, [k]: sortKeys(x[k]) }), {}) | ||
} | ||
|
||
module.exports = { | ||
generateTimestampAndSignature, | ||
recoverWallet, | ||
sortKeys | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters