-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathutils.js
More file actions
110 lines (91 loc) · 3.49 KB
/
utils.js
File metadata and controls
110 lines (91 loc) · 3.49 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import Promise from 'bluebird';
const Wallet = require('ethereumjs-wallet');
const Web3Utils = require('web3-utils');
const util = require("ethereumjs-util");
import { NFT_ABI, ERC20_ABI } from './metadata';
const SIGNATURE_PREFIX = "\x19Ethereum Signed Message:\n32";
/**
* @desc Get token object from the contract address.
* @param {String} [tokenAddress] - token contract address
* @param {Object} [web3] - web3 object (from web3.js lib)
* @return {Object}
*/
export const getToken = (tokenAddress, web3) => {
console.log("getting token ", tokenAddress)
const instance = web3.eth.contract(ERC20_ABI).at(tokenAddress);
console.log("got token")
Promise.promisifyAll(instance, { suffix: 'Promise' });
// fix for DAI token and web3js bug
if (tokenAddress === '0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359') {
instance.decimalsPromise = () => Promise.resolve(web3.toBigNumber(18));
instance.symbolPromise = () => Promise.resolve("DAI");
}
return instance;
};
/**
* @desc Get token object from the contract address.
* @param {String} [tokenAddress] - token contract address
* @param {Object} [web3] - web3 object (from web3.js lib)
* @return {Object}
*/
export const getTokenNFT = (tokenAddress, web3) => {
const instance = web3.eth.contract(NFT_ABI).at(tokenAddress);
Promise.promisifyAll(instance, { suffix: 'Promise' });
return instance;
};
/**
* @desc Generate Ethereum Account
* @return {'address': String, 'privateKey': String}
*/
export const generateAccount = () => {
const wallet = Wallet.generate();
const address = wallet.getChecksumAddressString();
const privateKey = wallet.getPrivateKey();
return { address, privateKey };
}
/**
* @desc Get Ethereum address from private key.
* @param {String} [privateKey]
* @return {String}
*/
export const getAddressFromPrivateKey = (privateKey) => {
return '0x' + Wallet.fromPrivateKey(
new Buffer(privateKey, 'hex')).getAddress().toString('hex');
}
/**
* @desc Sign message hash with private key.
* @param {String} [privateKey]
* @param {String} [msg] - message hash
* @return {'v': Number, 'r': String, 's': String}
*/
const _signWithPK = (privateKey, msg) => {
return util.ecsign(new Buffer(util.stripHexPrefix(msg), 'hex'), new Buffer(privateKey, 'hex'));
}
/**
* @desc Sign Ethereum address with private key.
* @param {String} [privateKey]
* @param {String} [address] - Ethereum address
* @return {'v': Number, 'r': String, 's': String}
*/
export const signAddress = ({address, privateKey}) => {
const verificationHash = Web3Utils.soliditySha3(SIGNATURE_PREFIX, { type: 'address', value: address });
const signature = _signWithPK(privateKey, verificationHash);
const v = signature.v;
const r = '0x' + signature.r.toString("hex");
const s = '0x' + signature.s.toString("hex");
return { v, r, s };
}
/**
* @desc Sign Ethereum address with private key.
* @param {String} [privateKey]
* @param {String} [address] - Ethereum address
* @return {'v': Number, 'r': String, 's': String}
*/
export const sign2Addresses = ({address, referralAddress, privateKey}) => {
const verificationHash = Web3Utils.soliditySha3(SIGNATURE_PREFIX, { type: 'address', value: address }, { type: 'address', value: referralAddress });
const signature = _signWithPK(privateKey, verificationHash);
const v = signature.v;
const r = '0x' + signature.r.toString("hex");
const s = '0x' + signature.s.toString("hex");
return { v, r, s };
}