Created
August 3, 2018 06:59
-
-
Save shingonu/40f65ee06098b6dbf48b83237ab183c1 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import { Router } from 'express'; | |
| import keythereum from 'keythereum'; | |
| import ethUtil from 'ethereumjs-util'; | |
| export default () => { | |
| const api = Router(); | |
| api.post('/pk/to', async (req, res, next) => { | |
| const datadir = process.env.DATADIR_LOCAL; | |
| const address = req.body.address; | |
| const password = req.body.password; | |
| if (web3.utils.isAddress(address)) { | |
| const keyObject = keythereum.importFromFile(address, datadir); | |
| const privateKey = keythereum.recover(password, keyObject); | |
| res.status(200).json({ | |
| code: 0, | |
| message: 'success', | |
| response: privateKey.toString('hex'), | |
| }); | |
| } else { | |
| res.status(400).json({ | |
| code: 1, | |
| message: 'invalid address format', | |
| }); | |
| } | |
| }); | |
| api.post('/pk/from', async (req, res, next) => { | |
| const pk = req.body.pk; | |
| const address = ethUtil.privateToAddress('0x' + pk); | |
| return res.status(200).json({ | |
| code: 0, | |
| message: 'success', | |
| response: '0x' + address.toString('hex'), | |
| }); | |
| }); | |
| return api; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment