Skip to content

Instantly share code, notes, and snippets.

@shingonu
Last active August 3, 2018 07:56
Show Gist options
  • Select an option

  • Save shingonu/a0c8e3ded12894994ba7b8339b314af0 to your computer and use it in GitHub Desktop.

Select an option

Save shingonu/a0c8e3ded12894994ba7b8339b314af0 to your computer and use it in GitHub Desktop.
api.post('/', async (req, res) => {
const serializedTx = req.body.serializedTx;
try {
web3.eth.sendSignedTransaction(serializedTx)
.on('receipt', receipt => {
return res.status(200).json({
code: 0,
message: 'success',
response: {
txhash: receipt.transactionHash,
}
});
})
.on('error', error => {
return res.status(400).json({
code: 1,
message: error.message,
});
});
} catch (err) {
return res.status(400).json({
code: 1,
message: err.message,
});
}
});
api.post('/make', async (req, res) => {
const privateKey = new Buffer(req.body.privateKey, 'hex');
if (!ethUtil.isValidPrivate(privateKey)) {
return res.status(400).json({
code: 1,
message: `private key length is invalid`,
});
}
const rawTx = req.body.rawTx;
const tx = new Tx(rawTx);
tx.sign(privateKey);
const serializedTx = tx.serialize();
return res.status(200).json({
code: 0,
message: 'success',
response: {
serializedTx: '0x' + serializedTx.toString('hex'),
}
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment