[netConfig.js] node.server port changed from 8888 to 60000.

[eth.js] lint it to remove semicolons at line endings.
This commit is contained in:
陆柯 2019-04-18 18:04:25 +08:00
parent 1e0567927e
commit c3dc3fbc45
2 changed files with 129 additions and 129 deletions

256
eth.js
View File

@ -1,22 +1,22 @@
'use strict' 'use strict'
const eth = require('etherscan-api').init('E3ZFFAEMNN33KX4HHVUZ4KF8XY1FXMR4BI'); const eth = require('etherscan-api').init('E3ZFFAEMNN33KX4HHVUZ4KF8XY1FXMR4BI')
const secretStorage = require('./utils/secret-storage'); const secretStorage = require('./utils/secret-storage')
const SigningKey = require('./utils/signing-key.js'); const SigningKey = require('./utils/signing-key.js')
const Ticrypto = require('tic.crypto'); const Ticrypto = require('tic.crypto')
const HDNode = require('./utils/hdnode'); const HDNode = require('./utils/hdnode')
const utils = require('./util.js'); const utils = require('./util.js')
const axios = require('axios'); const axios = require('axios')
require('setimmediate'); require('setimmediate')
const GAS_UNIT_WEI = 1e18; //1wei const GAS_UNIT_WEI = 1e18 //1wei
const GAS_UNIT_GWEI = 1e9; //1gwei = 1e9 wei const GAS_UNIT_GWEI = 1e9 //1gwei = 1e9 wei
const GAS_Fee = 0.000021; const GAS_Fee = 0.000021
const GAS_Fee_ERC20 = 0.000060; const GAS_Fee_ERC20 = 0.000060
const GAS_LIMIT = 21000; const GAS_LIMIT = 21000
const GAS_LIMIT_ERC20 = 60000; const GAS_LIMIT_ERC20 = 6e4
const defaultPath = "m/44'/60'/0'/0/0"; const defaultPath = "m/44'/60'/0'/0/0"
const ETH_NODE = require('./netConfig').ETH_NODE; const ETH_NODE = require('./netConfig').ETH_NODE
const transactionFields = [ const transactionFields = [
{name: 'nonce', maxLength: 32, }, {name: 'nonce', maxLength: 32, },
@ -25,15 +25,15 @@ const transactionFields = [
{name: 'to', length: 20, }, {name: 'to', length: 20, },
{name: 'value', maxLength: 32, }, {name: 'value', maxLength: 32, },
{name: 'data'}, {name: 'data'},
]; ]
class ETH { class ETH {
constructor(privateKey){ constructor(privateKey){
if(privateKey.length == 64 && !(privateKey.split('x')[1] && privateKey.split('x')[0] === '0')) if(privateKey.length == 64 && !(privateKey.split('x')[1] && privateKey.split('x')[0] === '0'))
privateKey = '0x'+privateKey; privateKey = '0x'+privateKey
var signingKey = privateKey; var signingKey = privateKey
if (!(privateKey instanceof SigningKey)) { if (!(privateKey instanceof SigningKey)) {
signingKey = new SigningKey(privateKey); signingKey = new SigningKey(privateKey)
} }
Object.defineProperties(this, { Object.defineProperties(this, {
'privateKey' : { 'privateKey' : {
@ -48,27 +48,27 @@ class ETH {
}, },
'url' : { 'url' : {
enumerable: true, enumerable: true,
get: function() { return this._url; }, get: function() { return this._url },
set: function(url) { set: function(url) {
if (typeof(url) !== 'string') { throw new Error('invalid url'); } if (typeof(url) !== 'string') { throw new Error('invalid url') }
this._url = url; this._url = url
}, },
}, },
'defaultGasFee' : { 'defaultGasFee' : {
enumerable: true, enumerable: true,
get: function() { return this._defaultGasFee; }, get: function() { return this._defaultGasFee },
set: function(value) { set: function(value) {
if (typeof(value) !== 'number') { throw new Error('invalid defaultGasFee'); } if (typeof(value) !== 'number') { throw new Error('invalid defaultGasFee') }
this._defaultGasFee = value; this._defaultGasFee = value
} }
} }
}) })
this._defaultGasFee = GAS_Fee; this._defaultGasFee = GAS_Fee
this._url = ETH_NODE; this._url = ETH_NODE
} }
static generateNewAccount(option = {path:defaultPath}){ static generateNewAccount(option = {path:defaultPath}){
//major path as default path >/0'/0/0 //major path as default path >/0'/0/0
var mnemonic = Ticrypto.randomSecword(); var mnemonic = Ticrypto.randomSecword()
return Object.assign(ETH.fromMnemonic(mnemonic, option),{mnemonic,mnemonic}) return Object.assign(ETH.fromMnemonic(mnemonic, option),{mnemonic,mnemonic})
} }
static fromMnemonic(mnemonic, option = {path:defaultPath}){ static fromMnemonic(mnemonic, option = {path:defaultPath}){
@ -77,7 +77,7 @@ class ETH {
return new ETH(HDNode.fromSeed(seed).derivePath(option.path).privateKey) return new ETH(HDNode.fromSeed(seed).derivePath(option.path).privateKey)
} }
static async getBalance(address){ static async getBalance(address){
if(!address){ throw new Error('Address is required'); } if(!address){ throw new Error('Address is required') }
let res = (await axios.post(ETH_NODE,{ let res = (await axios.post(ETH_NODE,{
"jsonrpc":"2.0","method":"eth_getBalance","params":[address, "latest"],"id":1 "jsonrpc":"2.0","method":"eth_getBalance","params":[address, "latest"],"id":1
})).data })).data
@ -87,114 +87,114 @@ class ETH {
} }
static async getActions(address){ static async getActions(address){
let tx = await eth.account.txlist(address, 0 ,'latast') let tx = await eth.account.txlist(address, 0 ,'latast')
if(tx && tx.message === "OK") if(tx && tx.message === 'OK')
return tx.result return tx.result
else return [] else return []
} }
static fromEncryptedWallet(json, password, progressCallback) { static fromEncryptedWallet(json, password, progressCallback) {
if (progressCallback && typeof(progressCallback) !== 'function') { if (progressCallback && typeof(progressCallback) !== 'function') {
throw new Error('invalid callback'); throw new Error('invalid callback')
} }
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
if (secretStorage.isCrowdsaleWallet(json)) { if (secretStorage.isCrowdsaleWallet(json)) {
try { try {
var privateKey = secretStorage.decryptCrowdsale(json, password); var privateKey = secretStorage.decryptCrowdsale(json, password)
resolve(new ETH(privateKey)); resolve(new ETH(privateKey))
} catch (error) { } catch (error) {
reject(error); reject(error)
} }
} else if (secretStorage.isValidWallet(json)) { } else if (secretStorage.isValidWallet(json)) {
secretStorage.decrypt(json, password, progressCallback).then(function(signingKey) { secretStorage.decrypt(json, password, progressCallback).then(function(signingKey) {
var wallet = new ETH(signingKey); var wallet = new ETH(signingKey)
if (signingKey.mnemonic && signingKey.path) { if (signingKey.mnemonic && signingKey.path) {
utils.defineProperty(wallet, 'mnemonic', signingKey.mnemonic); utils.defineProperty(wallet, 'mnemonic', signingKey.mnemonic)
utils.defineProperty(wallet, 'path', signingKey.path); utils.defineProperty(wallet, 'path', signingKey.path)
} }
resolve(wallet); resolve(wallet)
return null; return null
}, function(error) { }, function(error) {
reject(error); reject(error)
}).catch(function(error) { reject(error); }); }).catch(function(error) { reject(error) })
} else { } else {
reject('invalid wallet JSON'); reject('invalid wallet JSON')
} }
}); })
} }
static parseTransaction(rawTransaction){ static parseTransaction(rawTransaction){
rawTransaction = utils.hexlify(rawTransaction, 'rawTransaction'); rawTransaction = utils.hexlify(rawTransaction, 'rawTransaction')
var signedTransaction = utils.RLP.decode(rawTransaction); var signedTransaction = utils.RLP.decode(rawTransaction)
if (signedTransaction.length !== 9) { throw new Error('invalid transaction'); } if (signedTransaction.length !== 9) { throw new Error('invalid transaction') }
var raw = []; var raw = []
var transaction = {}; var transaction = {}
transactionFields.forEach(function(fieldInfo, index) { transactionFields.forEach(function(fieldInfo, index) {
transaction[fieldInfo.name] = signedTransaction[index]; transaction[fieldInfo.name] = signedTransaction[index]
raw.push(signedTransaction[index]); raw.push(signedTransaction[index])
}); })
if (transaction.to) { if (transaction.to) {
if (transaction.to == '0x') { if (transaction.to == '0x') {
delete transaction.to; delete transaction.to
} else { } else {
transaction.to = utils.getAddress(transaction.to); transaction.to = utils.getAddress(transaction.to)
} }
} }
['gasPrice', 'gasLimit', 'nonce', 'value'].forEach(function(name) { ['gasPrice', 'gasLimit', 'nonce', 'value'].forEach(function(name) {
if (!transaction[name]) { return; } if (!transaction[name]) { return }
if (transaction[name].length === 0) { if (transaction[name].length === 0) {
transaction[name] = utils.bigNumberify(0); transaction[name] = utils.bigNumberify(0)
} else { } else {
transaction[name] = utils.bigNumberify(transaction[name]); transaction[name] = utils.bigNumberify(transaction[name])
} }
}); })
if (transaction.nonce) { if (transaction.nonce) {
transaction.nonce = transaction.nonce.toNumber(); transaction.nonce = transaction.nonce.toNumber()
} else { } else {
transaction.nonce = 0; transaction.nonce = 0
} }
var v = utils.arrayify(signedTransaction[6]); var v = utils.arrayify(signedTransaction[6])
var r = utils.arrayify(signedTransaction[7]); var r = utils.arrayify(signedTransaction[7])
var s = utils.arrayify(signedTransaction[8]); var s = utils.arrayify(signedTransaction[8])
if (v.length >= 1 && r.length >= 1 && r.length <= 32 && s.length >= 1 && s.length <= 32) { if (v.length >= 1 && r.length >= 1 && r.length <= 32 && s.length >= 1 && s.length <= 32) {
transaction.v = utils.bigNumberify(v).toNumber(); transaction.v = utils.bigNumberify(v).toNumber()
transaction.r = signedTransaction[7]; transaction.r = signedTransaction[7]
transaction.s = signedTransaction[8]; transaction.s = signedTransaction[8]
var chainId = (transaction.v - 35) / 2; var chainId = (transaction.v - 35) / 2
if (chainId < 0) { chainId = 0; } if (chainId < 0) { chainId = 0 }
chainId = parseInt(chainId); chainId = parseInt(chainId)
transaction.chainId = chainId; transaction.chainId = chainId
var recoveryParam = transaction.v - 27; var recoveryParam = transaction.v - 27
if (chainId) { if (chainId) {
raw.push(utils.hexlify(chainId)); raw.push(utils.hexlify(chainId))
raw.push('0x'); raw.push('0x')
raw.push('0x'); raw.push('0x')
recoveryParam -= chainId * 2 + 8; recoveryParam -= chainId * 2 + 8
} }
var digest = utils.keccak256(utils.RLP.encode(raw)); var digest = utils.keccak256(utils.RLP.encode(raw))
try { try {
transaction.from = SigningKey.recover(digest, r, s, recoveryParam); transaction.from = SigningKey.recover(digest, r, s, recoveryParam)
} catch (error) { } catch (error) {
console.log(error); console.log(error)
} }
} }
return transaction; return transaction
} }
static encrypt(data, key){ static encrypt(data, key){
if(!data || !key) throw new Error('Required Params Missing') if(!data || !key) throw new Error('Required Params Missing')
@ -228,67 +228,67 @@ class ETH {
return ETH.getActions(this.address) return ETH.getActions(this.address)
} }
async getTransactionCount(){ async getTransactionCount(){
if(!this._url){ throw new Error('Base url required'); } if(!this._url){ throw new Error('Base url required') }
var self = this; var self = this
return (await axios.post(this._url,{ return (await axios.post(this._url,{
"jsonrpc":"2.0","method":"eth_getTransactionCount","params":[self.address, "latest"],"id":1 "jsonrpc":"2.0","method":"eth_getTransactionCount","params":[self.address, "latest"],"id":1
})).data.result||null })).data.result||null
} }
signTransaction(transaction){ signTransaction(transaction){
var chainId = transaction.chainId; var chainId = transaction.chainId
if (chainId == null && this.provider) { chainId = this.provider.chainId; } if (chainId == null && this.provider) { chainId = this.provider.chainId }
if (!chainId) { chainId = 0; } if (!chainId) { chainId = 0 }
var raw = []; var raw = []
transactionFields.forEach(function(fieldInfo) { transactionFields.forEach(function(fieldInfo) {
var value = transaction[fieldInfo.name] || ([]); var value = transaction[fieldInfo.name] || ([])
value = utils.arrayify(utils.hexlify(value), fieldInfo.name); value = utils.arrayify(utils.hexlify(value), fieldInfo.name)
// Fixed-width field // Fixed-width field
if (fieldInfo.length && value.length !== fieldInfo.length && value.length > 0) { if (fieldInfo.length && value.length !== fieldInfo.length && value.length > 0) {
var error = new Error('invalid ' + fieldInfo.name); var error = new Error('invalid ' + fieldInfo.name)
error.reason = 'wrong length'; error.reason = 'wrong length'
error.value = value; error.value = value
throw error; throw error
} }
// Variable-width (with a maximum) // Variable-width (with a maximum)
if (fieldInfo.maxLength) { if (fieldInfo.maxLength) {
value = utils.stripZeros(value); value = utils.stripZeros(value)
if (value.length > fieldInfo.maxLength) { if (value.length > fieldInfo.maxLength) {
var error = new Error('invalid ' + fieldInfo.name); var error = new Error('invalid ' + fieldInfo.name)
error.reason = 'too long'; error.reason = 'too long'
error.value = value; error.value = value
throw error; throw error
} }
} }
raw.push(utils.hexlify(value)); raw.push(utils.hexlify(value))
}); })
if (chainId) { if (chainId) {
raw.push(utils.hexlify(chainId)); raw.push(utils.hexlify(chainId))
raw.push('0x'); raw.push('0x')
raw.push('0x'); raw.push('0x')
} }
var digest = utils.keccak256(utils.RLP.encode(raw)); var digest = utils.keccak256(utils.RLP.encode(raw))
var signingKey = new SigningKey(this.privateKey); var signingKey = new SigningKey(this.privateKey)
var signature = signingKey.signDigest(digest); var signature = signingKey.signDigest(digest)
var v = 27 + signature.recoveryParam var v = 27 + signature.recoveryParam
if (chainId) { if (chainId) {
raw.pop(); raw.pop()
raw.pop(); raw.pop()
raw.pop(); raw.pop()
v += chainId * 2 + 8; v += chainId * 2 + 8
} }
raw.push(utils.hexlify(v)); raw.push(utils.hexlify(v))
raw.push(utils.stripZeros(utils.arrayify(signature.r))); raw.push(utils.stripZeros(utils.arrayify(signature.r)))
raw.push(utils.stripZeros(utils.arrayify(signature.s))); raw.push(utils.stripZeros(utils.arrayify(signature.s)))
return utils.RLP.encode(raw); return utils.RLP.encode(raw)
} }
async sendTransaction(toAddress, amount, option = {gasFee : GAS_Fee}){ async sendTransaction(toAddress, amount, option = {gasFee : GAS_Fee}){
/**************************************************************** /****************************************************************
@ -297,17 +297,17 @@ class ETH {
*GWei as the unit of gasPrice, minimum gasPrice is 1Gwei *GWei as the unit of gasPrice, minimum gasPrice is 1Gwei
*unit of amount is ethershould be translate to wei *unit of amount is ethershould be translate to wei
****************************************************************/ ****************************************************************/
let nonce = await this.getTransactionCount(); let nonce = await this.getTransactionCount()
if(!nonce) nonce = '0x0' if(!nonce) nonce = '0x0'
var gasPrice, gasLimit; var gasPrice, gasLimit
if(!option.gasPrice || !option.gasLimit){ if(!option.gasPrice || !option.gasLimit){
//Normal Mode:use customized gasFee( ether ) to caculate gasPrice( wei ), gasLimit use default value //Normal Mode:use customized gasFee( ether ) to caculate gasPrice( wei ), gasLimit use default value
gasLimit = GAS_LIMIT; gasLimit = GAS_LIMIT
gasPrice = String(option.gasFee * GAS_UNIT_WEI / gasLimit); gasPrice = String(option.gasFee * GAS_UNIT_WEI / gasLimit)
} }
else{ else{
//Advance Mode:specified the gasLimit and gasPrice( gwei ) //Advance Mode:specified the gasLimit and gasPrice( gwei )
gasLimit = option.gasLimit; gasLimit = option.gasLimit
gasPrice = String(GAS_UNIT_GWEI * option.gasPrice) gasPrice = String(GAS_UNIT_GWEI * option.gasPrice)
} }
let transaction = { let transaction = {
@ -317,9 +317,9 @@ class ETH {
to: toAddress, to: toAddress,
value: utils.parseEther(String(amount)), value: utils.parseEther(String(amount)),
}; }
try{ try{
let signedTransaction = this.signTransaction(transaction); let signedTransaction = this.signTransaction(transaction)
let ethTxRes = (await axios.post(ETH_NODE,{ let ethTxRes = (await axios.post(ETH_NODE,{
"jsonrpc":"2.0", "jsonrpc":"2.0",
"method":"eth_sendRawTransaction", "method":"eth_sendRawTransaction",
@ -342,7 +342,7 @@ class ETH {
class ERC20 extends ETH{ class ERC20 extends ETH{
constructor(privateKey, contractAddress){ constructor(privateKey, contractAddress){
if(!contractAddress) throw new Error('Missing contractAddress') if(!contractAddress) throw new Error('Missing contractAddress')
super(privateKey); super(privateKey)
Object.defineProperty(this, 'contractAddress',{ Object.defineProperty(this, 'contractAddress',{
enumerable:true, enumerable:true,
writable:false, writable:false,
@ -410,19 +410,19 @@ class ERC20 extends ETH{
1 Ether = 1e18 wei 1 Ether = 1e18 wei
1 Gwei = 1e9 wei 1 Gwei = 1e9 wei
*GWei as the unit of gasPrice, minimum gasPrice is 1Gwei *GWei as the unit of gasPrice, minimum gasPrice is 1Gwei
minimum gaslimit for erc20transaction is 60000 minimum gaslimit for erc20transaction is 6e4
****************************************************************/ ****************************************************************/
var nonce = await this.getTransactionCount(); var nonce = await this.getTransactionCount()
var gasPrice, gasLimit, decimals, contractAddress = this.contractAddress; var gasPrice, gasLimit, decimals, contractAddress = this.contractAddress
if(!nonce) nonce = '0x0' if(!nonce) nonce = '0x0'
if(!option.gasPrice || !option.gasLimit){ if(!option.gasPrice || !option.gasLimit){
//Normal Mode:use customized gasFee( ether ) to caculate gasPrice( wei ), gasLimit use default value //Normal Mode:use customized gasFee( ether ) to caculate gasPrice( wei ), gasLimit use default value
gasLimit = GAS_LIMIT_ERC20; gasLimit = GAS_LIMIT_ERC20
gasPrice = String(option.gasFee * GAS_UNIT_WEI / gasLimit); gasPrice = String(option.gasFee * GAS_UNIT_WEI / gasLimit)
} }
else{ else{
//Advance Mode:specified the gasLimit and gasPrice( gwei ) //Advance Mode:specified the gasLimit and gasPrice( gwei )
gasLimit = option.gasLimit; gasLimit = option.gasLimit
gasPrice = String(GAS_UNIT_GWEI * option.gasPrice) gasPrice = String(GAS_UNIT_GWEI * option.gasPrice)
} }
if(!option.decimals) decimals = await ERC20.getDecimals(contractAddress) if(!option.decimals) decimals = await ERC20.getDecimals(contractAddress)
@ -434,8 +434,8 @@ class ERC20 extends ETH{
to: contractAddress, to: contractAddress,
value : 0, value : 0,
data : txBody data : txBody
}; }
let signedTransaction = this.signTransaction(transaction); let signedTransaction = this.signTransaction(transaction)
try{ try{
let erc20TxRes = (await axios.post(ETH_NODE, { let erc20TxRes = (await axios.post(ETH_NODE, {
"jsonrpc":"2.0", "jsonrpc":"2.0",

View File

@ -1,5 +1,5 @@
const TIC_NODE = 'https://bank.bittic.net:8888/api'; const TIC_NODE = 'https://bank.bittic.net:60000/api';
const BTC_NODE = 'https://api.blockcypher.com/v1/btc/main'; const BTC_NODE = 'https://api.blockcypher.com/v1/btc/main';
const BTC_NODE2 = 'https://blockchain.info'//https://blockchain.info/unspent?active=12HnmPpLomtPL53Q4s6xEqRB4wkMHi5GEZ const BTC_NODE2 = 'https://blockchain.info'//https://blockchain.info/unspent?active=12HnmPpLomtPL53Q4s6xEqRB4wkMHi5GEZ
const ETH_NODE = 'https://mainnet.infura.io/8284219b092f4cc69f3de29e532b1eb2'; const ETH_NODE = 'https://mainnet.infura.io/8284219b092f4cc69f3de29e532b1eb2';