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

Commit 38853bc

Browse files
authored
Merge pull request #93 from libotony/dev
Dev
2 parents 1eca663 + 5e48d9d commit 38853bc

File tree

10 files changed

+598
-2128
lines changed

10 files changed

+598
-2128
lines changed

.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ language: node_js
22
node_js:
33
- "8"
44
- "10"
5+
- "12"
56
env:
67
- CXX=g++-4.8
78
addons:

package-lock.json

+579-2,062
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+8-10
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
{
22
"name": "thorify",
3-
"version": "1.3.0",
3+
"version": "1.4.0",
44
"description": "A web3 adaptor for VeChain Thor RESTful HTTP API.",
55
"main": "dist/index.js",
66
"typings": "dist/index.d.ts",
77
"scripts": {
88
"build": "tsc -p .",
99
"lint": "tslint --fix -p .",
1010
"prepare": "rm -rf dist/; npm run build",
11-
"test": "NODE_ENV=test mocha --require ts-node/register --timeout 20000 --recursive --exclude './test/browser/*.ts' './**/*.test.ts'",
11+
"test": "NODE_ENV=test mocha --require ts-node/register/transpile-only --timeout 20000 --recursive --exclude './test/browser/*.ts' './**/*.test.ts'",
1212
"cover": "NODE_ENV=test nyc npm t",
1313
"coverall": "NODE_ENV=test nyc npm t && nyc report --reporter=text-lcov | coveralls",
1414
"test:browser": "TS_NODE_PROJECT=\"test/browser/tsconfig.webpack.json\" webpack --config test/browser/webpack.test.config.ts && open test/browser/index.html"
@@ -26,7 +26,7 @@
2626
".ts"
2727
],
2828
"require": [
29-
"ts-node/register"
29+
"ts-node/register/transpile-only"
3030
],
3131
"reporter": [
3232
"json",
@@ -40,7 +40,7 @@
4040
"devDependencies": {
4141
"@types/chai": "^4.1.3",
4242
"@types/mocha": "^5.2.0",
43-
"@types/node": "^10.0.3",
43+
"@types/node": "^12.7.5",
4444
"@types/webpack": "^4.4.11",
4545
"@types/ws": "^6.0.0",
4646
"chai": "^4.1.2",
@@ -50,21 +50,19 @@
5050
"repl-x": "^0.1.4",
5151
"rewiremock": "^3.7.2",
5252
"ts-loader": "^5.1.1",
53-
"ts-node": "^5.0.1",
54-
"tslint": "^5.9.1",
55-
"typescript": "^2.8.1",
53+
"ts-node": "^8.3.0",
54+
"tslint": "^5.20.0",
55+
"typescript": "^3.6.3",
5656
"web3": "^1.2.1",
5757
"webpack": "^4.19.1",
5858
"webpack-cli": "^3.1.0"
5959
},
6060
"dependencies": {
6161
"debug": "^3.1.0",
62-
"eth-lib": "^0.2.8",
6362
"eventemitter3": "^3.1.0",
6463
"isomorphic-ws": "^4.0.1",
65-
"thor-devkit": "^0.2.3",
64+
"thor-devkit": "^1.1.0",
6665
"web3-core-subscriptions": "^1.2.1",
67-
"web3-utils": "^1.2.1",
6866
"ws": "^6.0.0",
6967
"xhr2": "0.1.4"
7068
},

src/extend/accounts.ts

+6-15
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
'use strict'
22

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

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

10+
const web3Utils = web3.utils
1211
// signTransaction supports both callback and promise style
1312
web3.eth.accounts.signTransaction = function signTransaction(ethTx: EthTransaction, privateKey: string, callback: Callback) {
1413
debug('tx to sign: %O', ethTx)
@@ -38,8 +37,10 @@ const extendAccounts = function(web3: any): any {
3837
tx.data = '0x'
3938
}
4039
if (!tx.gas) {
40+
const pubKey = cry.secp256k1.derivePublicKey(Buffer.from(utils.sanitizeHex(privateKey), 'hex'))
41+
const from = '0x' + cry.publicKeyToAddress(pubKey).toString('hex')
4142
const gas = await web3.eth.estimateGas({
42-
from: EthLib.account.fromPrivate(utils.toPrefixedHex(privateKey)).address,
43+
from,
4344
to: tx.to ? tx.to : '',
4445
value: tx.value ? tx.value : 0,
4546
data: tx.data,
@@ -95,17 +96,8 @@ const extendAccounts = function(web3: any): any {
9596
}
9697

9798
web3.eth.accounts.recoverTransaction = function recoverTransaction(encodedRawTx: string) {
98-
const values = EthLib.RLP.decode(encodedRawTx)
99-
100-
const signingDataHex = EthLib.RLP.encode(values.slice(0, 9))
101-
const singingHashBuffer = cry.blake2b256(Buffer.from(utils.sanitizeHex(signingDataHex), 'hex'))
102-
const signature = values[9]
103-
104-
const signatureBuffer = Buffer.from(utils.sanitizeHex(signature), 'hex')
105-
const pubKey = cry.secp256k1.recover(singingHashBuffer, signatureBuffer)
106-
const address = cry.publicKeyToAddress(pubKey)
107-
108-
return utils.toPrefixedHex(address.toString('hex'))
99+
const decoded = Transaction.decode(Buffer.from(utils.sanitizeHex(encodedRawTx), 'hex'))
100+
return decoded.origin
109101
}
110102

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

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

137128
if (utils.isObject(message)) {
138129
return this.recover(message.messageHash, message.signature, true)

src/extend/formatters.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
'use strict'
22
const debug = require('debug')('thor:injector')
3-
const web3Utils = require('web3-utils')
43
import { LogFilterOptions, TransferFilterOptions } from '../types'
54
import * as utils from '../utils'
65

76
const extendFormatters = function(web3: any) {
87

8+
const web3Utils = web3.utils
99
const outputTransactionFormatter = web3.extend.formatters.outputTransactionFormatter
1010
web3.extend.formatters.outputTransactionFormatter = function(tx: any) {
1111
if (tx && tx.isThorified) {

src/extend/methods.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use strict'
22
const Subscriptions = require('web3-core-subscriptions').subscriptions
3-
import * as utils from '../utils'
43
import { inputBlockFilterFormatter, inputLogFilterFormatter, inputTransferFilterFormatter } from './formatters'
54

65
const extendMethods = function(web3: any) {

src/utils/index.ts

-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
'use strict'
2-
const web3Utils = require('web3-utils')
32
import { EthTransaction } from '../types'
43
import { params } from './params'
54

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

90-
export const mustToBN = function(value: any) {
91-
if (isNull(value) || isUndefined(value)) {
92-
throw new Error('input can\'t be null or undefined')
93-
}
94-
95-
const num = web3Utils.toBN(value)
96-
return num.abs()
97-
}
98-
9989
export const validNumberOrDefault = function(value: any, defaultValue: number) {
10090
if (typeof value === 'number' && Number.isInteger(value)) {
10191
return Math.abs(value)

test/test-utils/init.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const Web3 = require('web3')
77

88
// Test utilities
99
const xhrUtility = new FakeXHR2()
10-
const wsUtility = new FakeWebSocket(null)
10+
const wsUtility = new FakeWebSocket('')
1111
wsUtility.close()
1212

1313
rewiremock('xhr2').with(require('./fake-xhr2'))

test/utils/utils.test.ts

-27
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,6 @@
33
import { expect } from 'chai'
44
import * as utils from '../../src/utils'
55

6-
describe('utils:mustToBN', () => {
7-
8-
it('input null', () => {
9-
expect(() => { utils.mustToBN(null) }).to.throw('input can\'t be null or undefined')
10-
})
11-
12-
it('input undefined', () => {
13-
expect(() => { utils.mustToBN(undefined) }).to.throw('input can\'t be null or undefined')
14-
})
15-
16-
it('input number', () => {
17-
const ret = utils.mustToBN(100)
18-
expect(ret.toString()).to.be.equal('100')
19-
})
20-
21-
it('input string', () => {
22-
const ret = utils.mustToBN('100')
23-
expect(ret.toString()).to.be.equal('100')
24-
})
25-
26-
it('input hex string', () => {
27-
const ret = utils.mustToBN('0x64')
28-
expect(ret.toString()).to.be.equal('100')
29-
})
30-
31-
})
32-
336
describe('utils:validNumberOrDefault', () => {
347

358
it('input hex string', () => {

tslint.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"only-arrow-functions": false,
1313
"interface-name": false,
1414
"max-line-length":false,
15-
"no-namespace":false
15+
"no-namespace":false,
16+
"radix": false
1617
},
1718
"rulesDirectory": []
1819
}

0 commit comments

Comments
 (0)