aboutsummaryrefslogtreecommitdiffstats
path: root/packages/testnet-faucets/src/ts/id_management.ts
blob: 7c598f91c44faacb44b9cdc6ba5612d6237a919a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import EthereumTx = require('ethereumjs-tx');
import * as ethUtil from 'ethereumjs-util';
import * as _ from 'lodash';

import { configs } from './configs';

type Callback = (err: Error | null, result: any) => void;

export const idManagement = {
    getAccounts(callback: Callback) {
        callback(null, [configs.DISPENSER_ADDRESS]);
    },
    approveTransaction(txData: object, callback: Callback) {
        callback(null, true);
    },
    signTransaction(txData: object, callback: Callback) {
        const tx = new EthereumTx(txData);
        const privateKeyBuffer = new Buffer(configs.DISPENSER_PRIVATE_KEY as string, 'hex');
        tx.sign(privateKeyBuffer);
        const rawTx = `0x${tx.serialize().toString('hex')}`;
        callback(null, rawTx);
    },
    signMessage(message: object, callback: Callback) {
        const dataIfExists = _.get(message, 'data');
        if (_.isUndefined(dataIfExists)) {
            callback(new Error('NO_DATA_TO_SIGN'), null);
        }
        const privateKeyBuffer = new Buffer(configs.DISPENSER_PRIVATE_KEY as string, 'hex');
        const dataBuff = ethUtil.toBuffer(dataIfExists);
        const msgHashBuff = ethUtil.hashPersonalMessage(dataBuff);
        const sig = ethUtil.ecsign(msgHashBuff, privateKeyBuffer);
        const rpcSig = ethUtil.toRpcSig(sig.v, sig.r, sig.s);
        callback(null, rpcSig);
    },
};