Skip to content

Commit

Permalink
1.5.0 Initial commit changes for Wanchain
Browse files Browse the repository at this point in the history
  • Loading branch information
tyrion70 committed Sep 10, 2018
1 parent e3fc214 commit 71e9fa4
Show file tree
Hide file tree
Showing 13 changed files with 9,310 additions and 391 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,4 @@ jspm_packages
# Yarn Integrity file
.yarn-integrity

.idea
35 changes: 22 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,34 +1,43 @@
# SYNOPSIS
[![NPM Package](https://img.shields.io/npm/v/ethereumjs-tx.svg?style=flat-square)](https://www.npmjs.org/package/ethereumjs-tx)
[![Build Status](https://travis-ci.org/ethereumjs/ethereumjs-tx.svg?branch=master)](https://travis-ci.org/ethereumjs/ethereumjs-tx)
[![Coverage Status](https://img.shields.io/coveralls/ethereumjs/ethereumjs-tx.svg?style=flat-square)](https://coveralls.io/r/ethereumjs/ethereumjs-tx)
[![Gitter](https://img.shields.io/gitter/room/ethereum/ethereumjs-lib.svg?style=flat-square)](https://gitter.im/ethereum/ethereumjs-lib) or #ethereumjs on freenode

[![js-standard-style](https://cdn.rawgit.com/feross/standard/master/badge.svg)](https://github.com/feross/standard)
# wanchainjs-tx

[![CircleCI][circle-image]][circle-url]
[![Coverage Status][coveralls-image]][coveralls-url]
[![dependency status][dep-image]][dep-url]
[![NPM][npm-image]][npm-url]

[circle-image]: https://circleci.com/gh/WanJS/wanchainjs-tx.svg?style=svg
[circle-url]: https://circleci.com/gh/WanJS/wanchainjs-tx
[dep-image]: https://david-dm.org/WanJS/wanchainjs-tx.svg
[dep-url]: https://david-dm.org/WanJS/wanchainjs-tx
[coveralls-image]: https://coveralls.io/repos/github/WanJS/wanchainjs-tx/badge.svg?branch=dev
[coveralls-url]: https://coveralls.io/github/WanJS/wanchainjs-tx?branch=dev
[npm-image]: http://img.shields.io/npm/v/wanchainjs-tx.svg
[npm-url]: https://www.npmjs.org/package/wanchainjs-tx

# INSTALL
`npm install ethereumjs-tx`
`npm install wanchainjs-tx`

# USAGE

- [example](https://github.com/ethereumjs/ethereumjs-tx/blob/master/examples/transactions.js)
- [example](https://github.com/WanJS/wanchainjs-tx/blob/master/examples/transactions.js)

```javascript
const EthereumTx = require('ethereumjs-tx')
const WanchainTx = require('wanchainjs-tx')
const privateKey = Buffer.from('e331b6d69882b4cb4ea581d88e0b604039a3de5967688d3dcffdd2270c0fd109', 'hex')

const txParams = {
Txtype: '0x01',
nonce: '0x00',
gasPrice: '0x09184e72a000',
gasLimit: '0x2710',
to: '0x0000000000000000000000000000000000000000',
value: '0x00',
data: '0x7f7465737432000000000000000000000000000000000000000000000000000000600057',
// EIP 155 chainId - mainnet: 1, ropsten: 3
chainId: 3
// EIP 155 chainId - mainnet: 1, testnet: 3
chainId: 1
}

const tx = new EthereumTx(txParams)
const tx = new WanchainTx(txParams)
tx.sign(privateKey)
const serializedTx = tx.serialize()
```
Expand Down
22 changes: 12 additions & 10 deletions examples/transactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var tx = new Transaction(null, 1) // mainnet Tx EIP155
// So now we have created a blank transaction but Its not quiet valid yet. We
// need to add some things to it. Lets start:
// notice we don't set the `to` field because we are creating a new contract.
tx.Txtype = '0x01'
tx.nonce = 0
tx.gasPrice = 100
tx.gasLimit = 1000
Expand All @@ -32,6 +33,7 @@ console.log('Total Amount of wei needed:' + feeCost.toString())

console.log('---Serialized TX----')
console.log(tx.serialize().toString('hex'))
console.log('Senders Address: ' + tx.getSenderAddress().toString('hex'))
console.log('--------------------')

// Now that we have the serialized transaction we can get AlethZero to except by
Expand All @@ -43,17 +45,17 @@ console.log('--------------------')
// it directly from the network it will be rlp encoded. You can decode you the rlp
// module. After that you should have something like
var rawTx = [
'0x00',
'0x09184e72a000',
'0x2710',
'0x0000000000000000000000000000000000000000',
'0x00',
'0x7f7465737432000000000000000000000000000000000000000000000000000000600057',
'0x1c',
'0x5e1d3a76fbf824220eafc8c79ad578ad2b67d01b0c2425eb1f1347e8f50882ab',
'0x5bd428537f05f9830e93792f90ea6a3e2d1ee84952dd96edbae9f658f831ab13'
new Buffer('01', 'hex'),
new Buffer('', 'hex'),
new Buffer('64', 'hex'),
new Buffer('0186a0', 'hex'),
new Buffer('', 'hex'),
new Buffer('', 'hex'),
new Buffer('7f4e616d65526567000000000000000000000000000000000000000000000000003057307f4e616d6552656700000000000000000000000000000000000000000000000000573360455760415160566000396000f20036602259604556330e0f600f5933ff33560f601e5960003356576000335700604158600035560f602b590033560f60365960003356573360003557600035335700', 'hex'),
new Buffer('1c', 'hex'),
new Buffer('dcacc31404aa8d5ef6b1757bbe415a22f1a329d8eb97899cfad7cfd9836b6050', 'hex'),
new Buffer('690f6bd64bcc647a38992b6061d8a36f126711912db3dded6cc034e294a0110a', 'hex')
]

var tx2 = new Transaction(rawTx)

// Note rlp.decode will actully produce an array of buffers `new Transaction` will
Expand Down
3 changes: 2 additions & 1 deletion fake.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const Transaction = require('./index.js')
const ethUtil = require('ethereumjs-util')
const ethUtil = require('wanchainjs-util')

/**
* Creates a new transaction object that doesn't need to be signed
Expand All @@ -11,6 +11,7 @@ const ethUtil = require('ethereumjs-util')
* For Object and Arrays each of the elements can either be a Buffer, a hex-prefixed (0x) String , Number, or an object with a toBuffer method such as Bignum
* @example
* var rawTx = {
* Txtype: '0x01',
* nonce: '0x00',
* gasPrice: '0x09184e72a000',
* gasLimit: '0x2710',
Expand Down
34 changes: 21 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'
const ethUtil = require('ethereumjs-util')
const fees = require('ethereum-common/params.json')
const BN = ethUtil.BN
const wanUtil = require('wanchainjs-util')
const fees = require('wanchain-common/params.json')
const BN = wanUtil.BN

// secp256k1n/2
const N_DIV_2 = new BN('7fffffffffffffffffffffffffffffff5d576e7357a4501ddfe92f46681b20a0', 16)
Expand All @@ -11,6 +11,7 @@ const N_DIV_2 = new BN('7fffffffffffffffffffffffffffffff5d576e7357a4501ddfe92f46
*
* @example
* var rawTx = {
* Txtype: '0x01',
* nonce: '0x00',
* gasPrice: '0x09184e72a000',
* gasLimit: '0x2710',
Expand All @@ -31,6 +32,7 @@ const N_DIV_2 = new BN('7fffffffffffffffffffffffffffffff5d576e7357a4501ddfe92f46
* For Object and Arrays each of the elements can either be a Buffer, a hex-prefixed (0x) String , Number, or an object with a toBuffer method such as Bignum
*
* @property {Buffer} raw The raw rlp encoded transaction
* @param {Buffer} data.Txtype Txtype (0x01 for normal, 0x06 for private)
* @param {Buffer} data.nonce nonce number
* @param {Buffer} data.gasLimit transaction gas limit
* @param {Buffer} data.gasPrice transaction gas price
Expand All @@ -40,14 +42,19 @@ const N_DIV_2 = new BN('7fffffffffffffffffffffffffffffff5d576e7357a4501ddfe92f46
* @param {Buffer} data.v EC recovery ID
* @param {Buffer} data.r EC signature parameter
* @param {Buffer} data.s EC signature parameter
* @param {Number} data.chainId EIP 155 chainId - mainnet: 1, ropsten: 3
* @param {Number} data.chainId EIP 155 chainId - mainnet: 1, testnet: 3
* */

class Transaction {
constructor (data) {
data = data || {}
// Define Properties
const fields = [{
name: 'Txtype',
length: 32,
allowLess: true,
default: new Buffer([])
}, {
name: 'nonce',
length: 32,
allowLess: true,
Expand Down Expand Up @@ -104,7 +111,7 @@ class Transaction {
* @name serialize
*/
// attached serialize
ethUtil.defineProperties(this, fields, data)
wanUtil.defineProperties(this, fields, data)

/**
* @property {Buffer} from (read only) sender address of this transaction, mathematically derived from other parameters.
Expand All @@ -118,7 +125,7 @@ class Transaction {
})

// calculate chainId from signature
let sigV = ethUtil.bufferToInt(this.v)
let sigV = wanUtil.bufferToInt(this.v)
let chainId = Math.floor((sigV - 35) / 2)
if (chainId < 0) chainId = 0

Expand All @@ -137,7 +144,7 @@ class Transaction {

/**
* Computes a sha3-256 hash of the serialized tx
* @param {Boolean} [includeSignature=true] whether or not to inculde the signature
* @param {Boolean} [includeSignature=true] whether or not to include the signature
* @return {Buffer}
*/
hash (includeSignature) {
Expand All @@ -160,12 +167,12 @@ class Transaction {
items = this.raw
this.raw = raw
} else {
items = this.raw.slice(0, 6)
items = this.raw.slice(0, 7)
}
}

// create hash
return ethUtil.rlphash(items)
return wanUtil.rlphash(items)
}

/**
Expand All @@ -185,7 +192,7 @@ class Transaction {
return this._from
}
const pubkey = this.getSenderPublicKey()
this._from = ethUtil.publicToAddress(pubkey)
this._from = wanUtil.publicToAddress(pubkey)
return this._from
}

Expand All @@ -212,11 +219,11 @@ class Transaction {
}

try {
let v = ethUtil.bufferToInt(this.v)
let v = wanUtil.bufferToInt(this.v)
if (this._chainId > 0) {
v -= this._chainId * 2 + 8
}
this._senderPubKey = ethUtil.ecrecover(msgHash, v, this.r, this.s)
this._senderPubKey = wanUtil.ecrecover(msgHash, v, this.r, this.s)
} catch (e) {
return false
}
Expand All @@ -229,8 +236,9 @@ class Transaction {
* @param {Buffer} privateKey
*/
sign (privateKey) {

const msgHash = this.hash(false)
const sig = ethUtil.ecsign(msgHash, privateKey)
const sig = wanUtil.ecsign(msgHash, privateKey)
if (this._chainId > 0) {
sig.v += this._chainId * 2 + 8
}
Expand Down
Loading

0 comments on commit 71e9fa4

Please sign in to comment.