Skip to content
This repository has been archived by the owner on Apr 23, 2024. It is now read-only.

Commit

Permalink
Merge pull request #93 from libotony/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
libotony authored Sep 12, 2019
2 parents 1eca663 + 5e48d9d commit 38853bc
Show file tree
Hide file tree
Showing 10 changed files with 598 additions and 2,128 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ language: node_js
node_js:
- "8"
- "10"
- "12"
env:
- CXX=g++-4.8
addons:
Expand Down
2,641 changes: 579 additions & 2,062 deletions package-lock.json

Large diffs are not rendered by default.

18 changes: 8 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "thorify",
"version": "1.3.0",
"version": "1.4.0",
"description": "A web3 adaptor for VeChain Thor RESTful HTTP API.",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"scripts": {
"build": "tsc -p .",
"lint": "tslint --fix -p .",
"prepare": "rm -rf dist/; npm run build",
"test": "NODE_ENV=test mocha --require ts-node/register --timeout 20000 --recursive --exclude './test/browser/*.ts' './**/*.test.ts'",
"test": "NODE_ENV=test mocha --require ts-node/register/transpile-only --timeout 20000 --recursive --exclude './test/browser/*.ts' './**/*.test.ts'",
"cover": "NODE_ENV=test nyc npm t",
"coverall": "NODE_ENV=test nyc npm t && nyc report --reporter=text-lcov | coveralls",
"test:browser": "TS_NODE_PROJECT=\"test/browser/tsconfig.webpack.json\" webpack --config test/browser/webpack.test.config.ts && open test/browser/index.html"
Expand All @@ -26,7 +26,7 @@
".ts"
],
"require": [
"ts-node/register"
"ts-node/register/transpile-only"
],
"reporter": [
"json",
Expand All @@ -40,7 +40,7 @@
"devDependencies": {
"@types/chai": "^4.1.3",
"@types/mocha": "^5.2.0",
"@types/node": "^10.0.3",
"@types/node": "^12.7.5",
"@types/webpack": "^4.4.11",
"@types/ws": "^6.0.0",
"chai": "^4.1.2",
Expand All @@ -50,21 +50,19 @@
"repl-x": "^0.1.4",
"rewiremock": "^3.7.2",
"ts-loader": "^5.1.1",
"ts-node": "^5.0.1",
"tslint": "^5.9.1",
"typescript": "^2.8.1",
"ts-node": "^8.3.0",
"tslint": "^5.20.0",
"typescript": "^3.6.3",
"web3": "^1.2.1",
"webpack": "^4.19.1",
"webpack-cli": "^3.1.0"
},
"dependencies": {
"debug": "^3.1.0",
"eth-lib": "^0.2.8",
"eventemitter3": "^3.1.0",
"isomorphic-ws": "^4.0.1",
"thor-devkit": "^0.2.3",
"thor-devkit": "^1.1.0",
"web3-core-subscriptions": "^1.2.1",
"web3-utils": "^1.2.1",
"ws": "^6.0.0",
"xhr2": "0.1.4"
},
Expand Down
21 changes: 6 additions & 15 deletions src/extend/accounts.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
'use strict'

const web3Utils = require('web3-utils')
const debug = require('debug')('thor:injector')
const EthLib = require('eth-lib/lib')
import { cry, Transaction } from 'thor-devkit'
import { Callback, EthTransaction } from '../types'
import * as utils from '../utils'

const extendAccounts = function(web3: any): any {

const web3Utils = web3.utils
// signTransaction supports both callback and promise style
web3.eth.accounts.signTransaction = function signTransaction(ethTx: EthTransaction, privateKey: string, callback: Callback) {
debug('tx to sign: %O', ethTx)
Expand Down Expand Up @@ -38,8 +37,10 @@ const extendAccounts = function(web3: any): any {
tx.data = '0x'
}
if (!tx.gas) {
const pubKey = cry.secp256k1.derivePublicKey(Buffer.from(utils.sanitizeHex(privateKey), 'hex'))
const from = '0x' + cry.publicKeyToAddress(pubKey).toString('hex')
const gas = await web3.eth.estimateGas({
from: EthLib.account.fromPrivate(utils.toPrefixedHex(privateKey)).address,
from,
to: tx.to ? tx.to : '',
value: tx.value ? tx.value : 0,
data: tx.data,
Expand Down Expand Up @@ -95,17 +96,8 @@ const extendAccounts = function(web3: any): any {
}

web3.eth.accounts.recoverTransaction = function recoverTransaction(encodedRawTx: string) {
const values = EthLib.RLP.decode(encodedRawTx)

const signingDataHex = EthLib.RLP.encode(values.slice(0, 9))
const singingHashBuffer = cry.blake2b256(Buffer.from(utils.sanitizeHex(signingDataHex), 'hex'))
const signature = values[9]

const signatureBuffer = Buffer.from(utils.sanitizeHex(signature), 'hex')
const pubKey = cry.secp256k1.recover(singingHashBuffer, signatureBuffer)
const address = cry.publicKeyToAddress(pubKey)

return utils.toPrefixedHex(address.toString('hex'))
const decoded = Transaction.decode(Buffer.from(utils.sanitizeHex(encodedRawTx), 'hex'))
return decoded.origin
}

web3.eth.accounts.hashMessage = function hashMessage(data: string | Buffer) {
Expand All @@ -132,7 +124,6 @@ const extendAccounts = function(web3: any): any {
}

web3.eth.accounts.recover = function recover(message: any, signature: string, preFixed: boolean) {
const args = [].slice.apply(arguments)

if (utils.isObject(message)) {
return this.recover(message.messageHash, message.signature, true)
Expand Down
2 changes: 1 addition & 1 deletion src/extend/formatters.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
'use strict'
const debug = require('debug')('thor:injector')
const web3Utils = require('web3-utils')
import { LogFilterOptions, TransferFilterOptions } from '../types'
import * as utils from '../utils'

const extendFormatters = function(web3: any) {

const web3Utils = web3.utils
const outputTransactionFormatter = web3.extend.formatters.outputTransactionFormatter
web3.extend.formatters.outputTransactionFormatter = function(tx: any) {
if (tx && tx.isThorified) {
Expand Down
1 change: 0 additions & 1 deletion src/extend/methods.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict'
const Subscriptions = require('web3-core-subscriptions').subscriptions
import * as utils from '../utils'
import { inputBlockFilterFormatter, inputLogFilterFormatter, inputTransferFilterFormatter } from './formatters'

const extendMethods = function(web3: any) {
Expand Down
10 changes: 0 additions & 10 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
'use strict'
const web3Utils = require('web3-utils')
import { EthTransaction } from '../types'
import { params } from './params'

Expand Down Expand Up @@ -87,15 +86,6 @@ export const isFunction = function(o: any): boolean {
return typeof o === 'function'
}

export const mustToBN = function(value: any) {
if (isNull(value) || isUndefined(value)) {
throw new Error('input can\'t be null or undefined')
}

const num = web3Utils.toBN(value)
return num.abs()
}

export const validNumberOrDefault = function(value: any, defaultValue: number) {
if (typeof value === 'number' && Number.isInteger(value)) {
return Math.abs(value)
Expand Down
2 changes: 1 addition & 1 deletion test/test-utils/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const Web3 = require('web3')

// Test utilities
const xhrUtility = new FakeXHR2()
const wsUtility = new FakeWebSocket(null)
const wsUtility = new FakeWebSocket('')
wsUtility.close()

rewiremock('xhr2').with(require('./fake-xhr2'))
Expand Down
27 changes: 0 additions & 27 deletions test/utils/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,6 @@
import { expect } from 'chai'
import * as utils from '../../src/utils'

describe('utils:mustToBN', () => {

it('input null', () => {
expect(() => { utils.mustToBN(null) }).to.throw('input can\'t be null or undefined')
})

it('input undefined', () => {
expect(() => { utils.mustToBN(undefined) }).to.throw('input can\'t be null or undefined')
})

it('input number', () => {
const ret = utils.mustToBN(100)
expect(ret.toString()).to.be.equal('100')
})

it('input string', () => {
const ret = utils.mustToBN('100')
expect(ret.toString()).to.be.equal('100')
})

it('input hex string', () => {
const ret = utils.mustToBN('0x64')
expect(ret.toString()).to.be.equal('100')
})

})

describe('utils:validNumberOrDefault', () => {

it('input hex string', () => {
Expand Down
3 changes: 2 additions & 1 deletion tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"only-arrow-functions": false,
"interface-name": false,
"max-line-length":false,
"no-namespace":false
"no-namespace":false,
"radix": false
},
"rulesDirectory": []
}

0 comments on commit 38853bc

Please sign in to comment.