Commit 7dc65f38 authored by SilentCicero's avatar SilentCicero

version 0.1.0 -- basic coverage, docs, builds, config

parents
root = true
[*]
end_of_line = lf
insert_final_newline = false
indent_style = space
indent_size = 2
# From https://github.com/Danimoth/gitattributes/blob/master/Web.gitattributes
# Handle line endings automatically for files detected as text
# and leave all files detected as binary untouched.
* text=auto
#
# The above will handle all files NOT found below
#
#
## These files are text and should be normalized (Convert crlf => lf)
#
# source code
*.php text
*.css text
*.sass text
*.scss text
*.less text
*.styl text
*.js text eol=lf
*.coffee text
*.json text
*.htm text
*.html text
*.xml text
*.svg text
*.txt text
*.ini text
*.inc text
*.pl text
*.rb text
*.py text
*.scm text
*.sql text
*.sh text
*.bat text
# templates
*.ejs text
*.hbt text
*.jade text
*.haml text
*.hbs text
*.dot text
*.tmpl text
*.phtml text
# server config
.htaccess text
# git config
.gitattributes text
.gitignore text
.gitconfig text
# code analysis config
.jshintrc text
.jscsrc text
.jshintignore text
.csslintrc text
# misc config
*.yaml text
*.yml text
.editorconfig text
# build config
*.npmignore text
*.bowerrc text
# Heroku
Procfile text
.slugignore text
# Documentation
*.md text
LICENSE text
AUTHORS text
#
## These files are binary and should be left untouched
#
# (binary is a macro for -text -diff)
*.png binary
*.jpg binary
*.jpeg binary
*.gif binary
*.ico binary
*.mov binary
*.mp4 binary
*.mp3 binary
*.flv binary
*.fla binary
*.swf binary
*.gz binary
*.zip binary
*.7z binary
*.ttf binary
*.eot binary
*.woff binary
*.pyc binary
*.pdf binary
# Contributing to ethjs-signer
Love ethjs-signer and want to help? Thanks so much, there's something to do for everybody!
Please take a moment to review this document in order to make the contribution process easy and effective for everyone involved.
Following these guidelines helps to communicate that you respect the time of the developers managing and developing this open source project. In return, they should reciprocate that respect in addressing your issue or assessing patches and features.
## Using the issue tracker
The [issue tracker](https://github.com/ethjs/ethjs-signer/issues) is
the preferred channel for [bug reports](#bugs), [features requests](#features)
and [submitting pull requests](#pull-requests).
<a name="bugs"></a>
## Bug reports
A bug is a _demonstrable problem_ that is caused by the code in the repository.
Good bug reports are extremely helpful - thank you!
Guidelines for bug reports:
1. **Use the GitHub issue search** &mdash; check if the issue has already been reported.
2. **Check if the issue has been fixed** &mdash; try to reproduce it using the latest `master` or development branch in the repository.
3. **Isolate the problem** &mdash;
A good bug report shouldn't leave others needing to chase you up for more information. Please try to be as detailed as possible in your report. What is your environment? What steps will reproduce the issue? What browser(s) and OS
experience the problem? What would you expect to be the outcome? All these details will help people to fix any potential bugs.
Example:
> Short and descriptive example bug report title
>
> A summary of the issue and the browser/OS environment in which it occurs. If
> suitable, include the steps required to reproduce the bug.
>
> 1. This is the first step
> 2. This is the second step
> 3. Further steps, etc.
>
> `<url>` - a link to the reduced test case
>
> Any other information you want to share that is relevant to the issue being
> reported. This might include the lines of code that you have identified as
> causing the bug, and potential solutions (and your opinions on their
> merits).
<a name="features"></a>
## Feature requests
Feature requests are welcome. But take a moment to find out whether your idea fits with the scope and aims of the project. It's up to *you* to make a strong case to convince the project's developers of the merits of this feature. Please provide as much detail and context as possible.
<a name="pull-requests"></a>
## Pull requests
Good pull requests - patches, improvements, new features - are a fantastic
help. They should remain focused in scope and avoid containing unrelated
commits.
**Please ask first** before embarking on any significant pull request (e.g.
implementing features, refactoring code, porting to a different language),
otherwise you risk spending a lot of time working on something that the
project's developers might not want to merge into the project.
Please adhere to the coding conventions used throughout a project (indentation,
accurate comments, etc.) and any other requirements (such as test coverage).
Adhering to the following process is the best way to get your work
included in the project:
1. [Fork](https://help.github.com/articles/fork-a-repo/) the project, clone your fork, and configure the remotes:
```bash
# Clone your fork of the repo into the current directory
git clone https://github.com/<your-username>/ethjs-signer.git
# Navigate to the newly cloned directory
cd ethjs-signer
# Assign the original repo to a remote called "upstream"
git remote add upstream https://github.com/ethjs/ethjs-signer.git
```
2. If you cloned a while ago, get the latest changes from upstream:
```bash
git checkout master
git pull upstream master
```
3. Create a new topic branch (off the main project development branch) to contain your feature, change, or fix:
```bash
git checkout -b <topic-branch-name>
```
4. Commit your changes in logical chunks. Please adhere to these [git commit message guidelines](http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html) or your code is unlikely be merged into the main project. Use Git's [interactive rebase](https://help.github.com/articles/about-git-rebase/) feature to tidy up your commits before making them public.
5. Locally merge (or rebase) the upstream development branch into your topic branch:
```bash
git pull [--rebase] upstream master
```
6. Push your topic branch up to your fork:
```bash
git push origin <topic-branch-name>
```
7. [Open a Pull Request](https://help.github.com/articles/using-pull-requests/)
with a clear title and description.
**DESIGN NOTE**: ethjs-signer follows the UNIX programming philosophy. Please consider this before contributing, keep your commits/modules concise and to the point.
Read more here:
http://www.catb.org/esr/writings/taoup/html/ch01s06.html
**IMPORTANT**: By submitting a patch, you agree to allow the project
owners to license your work under the terms of the [MIT License](LICENSE.txt).
# ethjs-signer
Before opening a new issue, please take a moment to review our [**community guidelines**](https://github.com/ethjs/ethjs-signer/blob/master/.github/CONTRIBUTING.md) to make the contribution process easy and effective for everyone involved.
**Before opening a new issue, you may find an answer in already closed issues**:
https://github.com/ethjs/ethjs-signer/issues?q=is%3Aissue+is%3Aclosed
## Issue Type
- [ ] Bug (https://github.com/ethjs/ethjs-signer/blob/master/.github/CONTRIBUTING.md#bug-reports)
- [ ] Feature (https://github.com/ethjs/ethjs-signer/blob/master/.github/CONTRIBUTING.md#feature-requests)
## Description
(Add images if possible)
## Steps to reproduce
(Add link to a demo on https://jsfiddle.net or similar if possible)
# Versions
- Node/NPM:
- Browser:
## ethjs-signer
Thank you for contributing! Please take a moment to review our [**contributing guidelines**](https://github.com/ethjs/ethjs-signer/blob/master/.github/CONTRIBUTING.md)
to make the process easy and effective for everyone involved.
**Please open an issue** before embarking on any significant pull request, especially those that
add a new library or change existing tests, otherwise you risk spending a lot of time working
on something that might not end up being merged into the project.
Before opening a pull request, please ensure:
- [ ] You have followed our [**contributing guidelines**](https://github.com/ethjs/ethjs-signer/blob/master/.github/CONTRIBUTING.md)
- [ ] Pull request has tests (we are going for 100% coverage!)
- [ ] Code is well-commented, linted and follows project conventions
- [ ] Documentation is updated (if necessary)
- [ ] Internal code generators and templates are updated (if necessary)
- [ ] Description explains the issue/use-case resolved and auto-closes related issues
Be kind to code reviewers, please try to keep pull requests as small and focused as possible :)
**IMPORTANT**: By submitting a patch, you agree to allow the project
owners to license your work under the terms of the [MIT License](https://github.com/ethjs/ethjs-signer/blob/master/LICENSE.md).
# Don't check auto-generated stuff into git
node_modules
coverage
lib
# Cruft
.DS_Store
npm-debug.log
sudo: true
language: node_js
node_js:
- "6"
compiler:
- gcc
- clang
install:
env:
- CXX=g++-4.8
addons:
apt:
sources:
- ubuntu-toolchain-r-test
packages:
- gcc-4.8
- g++-4.8
- clang
after_success: npm run coveralls
# 0.1.0 -- first release
1. Basic testing
2. Basic docs
3. License
4. Linting
5. Basic exports
# Contributor Code of Conduct
As contributors and maintainers of this project, and in the interest of
fostering an open and welcoming community, we pledge to respect all people who
contribute through reporting issues, posting feature requests, updating
documentation, submitting pull requests or patches, and other activities.
We are committed to making participation in this project a harassment-free
experience for everyone, regardless of level of experience, gender, gender
identity and expression, sexual orientation, disability, personal appearance,
body size, race, ethnicity, age, religion, or nationality.
Examples of unacceptable behavior by participants include:
* The use of sexualized language or imagery
* Personal attacks
* Trolling or insulting/derogatory comments
* Public or private harassment
* Publishing other's private information, such as physical or electronic
addresses, without explicit permission
* Other unethical or unprofessional conduct
Project maintainers have the right and responsibility to remove, edit, or
reject comments, commits, code, wiki edits, issues, and other contributions
that are not aligned to this Code of Conduct, or to ban temporarily or
permanently any contributor for other behaviors that they deem inappropriate,
threatening, offensive, or harmful.
By adopting this Code of Conduct, project maintainers commit themselves to
fairly and consistently applying these principles to every aspect of managing
this project. Project maintainers who do not follow or enforce the Code of
Conduct may be permanently removed from the project team.
This Code of Conduct applies both within project spaces and in public spaces
when an individual is representing the project or its community.
Instances of abusive, harassing, or otherwise unacceptable behavior may be
reported by contacting the project maintainer at [email protected]. All
complaints will be reviewed and investigated and will result in a response that
is deemed necessary and appropriate to the circumstances. Maintainers are
obligated to maintain confidentiality with regard to the reporter of an
incident.
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
version 1.3.0, available at
[http://contributor-covenant.org/version/1/3/0/][version]
[homepage]: http://contributor-covenant.org
[version]: http://contributor-covenant.org/version/1/3/0/
The MIT License
Copyright (c) 2016 Nick Dodson. nickdodson.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
## ethjs-signer
<div>
<!-- Dependency Status -->
<a href="https://david-dm.org/ethjs/ethjs-signer">
<img src="https://david-dm.org/ethjs/ethjs-signer.svg"
alt="Dependency Status" />
</a>
<!-- devDependency Status -->
<a href="https://david-dm.org/ethjs/ethjs-signer#info=devDependencies">
<img src="https://david-dm.org/ethjs/ethjs-signer/dev-status.svg" alt="devDependency Status" />
</a>
<!-- Build Status -->
<a href="https://travis-ci.org/ethjs/ethjs-signer">
<img src="https://travis-ci.org/ethjs/ethjs-signer.svg"
alt="Build Status" />
</a>
<!-- NPM Version -->
<a href="https://www.npmjs.org/package/ethjs-signer">
<img src="http://img.shields.io/npm/v/ethjs-signer.svg"
alt="NPM version" />
</a>
<!-- Test Coverage -->
<a href="https://coveralls.io/r/ethjs/ethjs-signer">
<img src="https://coveralls.io/repos/github/ethjs/ethjs-signer/badge.svg" alt="Test Coverage" />
</a>
<!-- Javascript Style -->
<a href="http://airbnb.io/javascript/">
<img src="https://img.shields.io/badge/code%20style-airbnb-brightgreen.svg" alt="js-airbnb-style" />
</a>
</div>
<br />
A simple Ethereum signing and recovery utility.
## Install
```
npm install --save ethjs-signer
```
## Usage
```js
const HttpProvider = require('ethjs-provider-http');
const Eth = require('ethjs-query');
const eth = new Eth(HttpProvider('http://localhost:8545'));
const sign = require('ethjs-signer');
const address = '0x0F6af8F8D7AAD198a7607C96fb74Ffa02C5eD86B';
const privateKey = '0xecbcd9838f7f2afa6e809df8d7cdae69aa5dfc14d563ee98e97effd3f6a652f2';
eth.getTransactionCount(address).then((nonce) => {
eth.sendRawTransaction(sign({
to: '0xcG31a19193d4b23f4e9d6163d7247243bAF801c3',
value: 300000,
gas: new BN('43092000'),
nonce: nonce,
}, privateKey)).then((txHash) => {
console.log('Transaction Hash', txHash);
});
});
```
Note, that address and private key are a valid address and private key. Only use this example address for local testing and setup. You will loose your Ether if you send it to this address.
## About
This module is meant to help sign and recover Ethereum transactions. You can either sign the transaction and have it return a serilized hex payload (the default), or return the raw array of Buffer values.
## Method API
```
sign <Function (Object, String [, Boolean]) : (String|Array)>
recover <Function (Object|String, Number, Object, Object) : (Object)>
```
## Contributing
Please help better the ecosystem by submitting issues and pull requests to `ethjs-signer`. We need all the help we can get to build the absolute best linting standards and utilities. We follow the AirBNB linting standard and the unix philosophy.
## Guides
You'll find more detailed information on using `ethjs-signer` and tailoring it to your needs in our guides:
- [User guide](docs/user-guide.md) - Usage, configuration, FAQ and complementary tools.
- [Developer guide](docs/developer-guide.md) - Contributing to `ethjs-signer` and writing your own code and coverage.
## Help out
There is always a lot of work to do, and will have many rules to maintain. So please help out in any way that you can:
- Create, enhance, and debug ethjs rules (see our guide to ["Working on rules"](./github/CONTRIBUTING.md)).
- Improve documentation.
- Chime in on any open issue or pull request.
- Open new issues about your ideas for making `ethjs-signer` better, and pull requests to show us how your idea works.
- Add new tests to *absolutely anything*.
- Create or contribute to ecosystem tools.
- Spread the word!
Please consult our [Code of Conduct](CODE_OF_CONDUCT.md) docs before helping out.
We communicate via [issues](https://github.com/ethjs/ethjs-signer/issues) and [pull requests](https://github.com/ethjs/ethjs-signer/pulls).
## Important documents
- [Changelog](CHANGELOG.md)
- [Code of Conduct](CODE_OF_CONDUCT.md)
- [License](https://raw.githubusercontent.com/ethjs/ethjs-signer/master/LICENSE)
## Licence
This project is licensed under the MIT license, Copyright (c) 2016 Nick Dodson. For more information see LICENSE.md.
```
The MIT License
Copyright (c) 2016 Nick Dodson. nickdodson.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
```
This diff is collapsed.
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
# Documentation
## Table of Contents
- [General](general)
- [**Developer Guide**](developer-guide.md)
- [**User Guide**](user-guide.md)
## Overview
### Structure
The [`src/`](../../../tree/master/src) directory contains your entire application code, including JavaScript, and tests.
The rest of the folders and files only exist to make your life easier, and
should not need to be touched.
For more in-depth structure, see the developer-guide.md.
*(If they do have to be changed, please [submit an issue](https://github.com/ethjs/ethjs-signer/issues)!)*
### Testing
For a thorough explanation of the testing procedure, see the
[testing documentation](./developer-guide/README.md)!
#### Unit testing
Unit tests live in `src/tests/` directories right next to the components being tested
and are run with `npm test`.
# Developer Guide
All information regarding contributing to and progressing `ethjs-signer` module can be found in this document.
## Install
```
npm install --save ethjs-signer
```
## Install from Source
```
git clone http://github.com/ethjs/ethjs-signer
npm install
```
## Test
```
npm test
```
## Build
```
npm run build
```
## Linting
```
npm run lint
```
## Travis-ci and Coveralls Testing
Note, this will generate a `coveralls` report locally.
```
npm run test-travis
```
You can find the coveralls report and view the percentages and stats, by going to the [index.html](coverage/lcov-report/index.html) file generated after running the `test-travis` script. Open this in Chrome to see the generated report. Travis will run mocha as usual, but collect information about the testing coverage. This report will be sent by TravisCI during the automated build process.
## Build Staging
The build staging for this module is as follows:
1. Cleanup
2. Linting
3. Testing
4. Babel processing (output to lib)
5. Webpack (output to dist)
6. Webpack production (output to dist)
7. Retest lib folder for babel processing solidity
8. Report build stats
## Folder Structure
All module source code is found in the `src` directory. All module helper scripts can be found in the `scripts` folder. These will not need to be touched, and are purely configuration for this repository.
```
./ethjs-signer
./.github
./dist
./lib
./tests
./internals
./webpack
./coverage
./docs
./src
./tests
```
Note, the `./lib` dir is generated from the babel build staging. `./coverage` is generated from the `npm run test-travis` script. All internals and helper scripts (i.e. `webpack`) are in `./internals`. All distribution builds are in `./dist` (usually a minified and unminified production build of the package).
## NPM Practice
Across all `ethjs-` repos, we enforce version hardening (i.e. "0.0.3" not "^0.0.3"). We want to reduce potential hazardous install changes from dependancies as much as possible to ensure package preformace, testing, security and design. Please make sure all your commits and PR's are version hardend if you are installing or removing new packages.
After build staging it is the `lib` folder which actually gets published to NPM. This allows for easy inclusion into other modules which may not use babel transpiling or which may not support es2015+.
## Webpack
`ethjs` uses webpack across all its browser focused repos. Webpack is used to package down project files into distribution builds for the browser. You can see the builds it produces by going to the [dist](dist) folder.
Read more about webpack here:
https://github.com/webpack/docs
## Testing ethereumjs
This module is tested against ethereumjs-util.
## NPM/Node Version Requirements
`ethjs` requires you have:
- `nodejs` -v 6.5.0+
- `npm` -v 3.0+
This is a requirement to run, test, lint and build this module.
## Changelog
All relevant changes are notated in the `CHANGELOG.md` file, moniter this file for changes to this repository.
## Travis-ci and Coveralls Practice
Across all `ethjs-` repos, we enforce mandatory travis-ci and coveralls testing. We never `commit to master`. As a general policy, Coveralls.io results must always be above 95% for any `ethjs-` PR or commit. We want to ensure complete coverage across the board.
## Contributing
Please help better the ecosystem by submitting issues and pull requests. We need all the help we can get to build the absolute best linting standards and utilities. We follow the AirBNB linting standard. Please read more about contributing to `ethjs-signer` in the `.github/CONTRIBUTING.md`.
## Licence
This project is licensed under the MIT license, Copyright (c) 2016 Nick Dodson. For more information see LICENSE.
# User Guide
All information for developers using `ethjs-signer` should consult this document.
## Install
```
npm install --save ethjs-signer
```
## Usage
```js
const HttpProvider = require('ethjs-provider-http');
const Eth = require('ethjs-query');
const eth = new Eth(HttpProvider('http://localhost:8545'));
const sign = require('ethjs-signer');
const address = '0x0F6af8F8D7AAD198a7607C96fb74Ffa02C5eD86B';
const privateKey = '0xecbcd9838f7f2afa6e809df8d7cdae69aa5dfc14d563ee98e97effd3f6a652f2';
eth.getTransactionCount(address).then((nonce) => {
eth.sendRawTransaction(sign({
to: '0xcG31a19193d4b23f4e9d6163d7247243bAF801c3',
value: 300000,
gas: new BN('43092000'),
nonce: nonce,
}, privateKey)).then((txHash) => {
console.log('Transaction Hash', txHash);
});
});
```
## API Design
### sign
[index.js:ethjs-signer](../../../blob/master/src/index.js "Source code on GitHub")
Intakes a raw transaction object and private key, outputs either a signed transaction hex string (by default) or a raw signed array of Buffer values (if specified). The hexified string payload can be used in conjunction with methods like `eth_sendRawTransaction`.
**Parameters**
- `rawTransaction` **Object** - a single raw transaction object.
- `privateKey` **String** - a single 32 byte prefixed hex string that is a Ethereum standard private key
- `toArray` **Boolean** (optional) - return the signed payload in its raw array format
Result output signed tx payload **String|Object**.
```js
const sign = require('ethjs-signer').sign;
console.log(sign({ gas: 300000, data: '0x' }, '0x..privte key'));
// result '0x.....'
console.log(sign({ from: '0x..', gas: '0x...', data: '0x' }, '0x..privte key', true));
// result [ <Buffer...>, <Buffer ...>, <Buffer ...> ...]
```
### recover
[index.js:ethjs-signer](../../../blob/master/src/index.js "Source code on GitHub")
Intakes a rawTransaction hex string or Buffer instance, the recovery param (Number), and the transaction signature r (Buffer) and s (Buffer) values. Returns the public key as a Buffer instance for the signers account.
**Parameters**
- `rawTransaction` **String|Buffer** - the raw transaction either as a hex string or Buffer instance
- `v` **Number** - the recovery param
- `r` **Buffer** - the signature 'r' value
- `s` **Buffer** - the signature 's' value
Result output public key **Buffer**.
```js
const recover = require('ethjs-signer').recover;
const sign = require('ethjs-signer').sign;
const stripHexPrefix = require('strip-hex-prefix');
const generate = require('ethjs-account').generate;
const publicToAddress = require('ethjs-account').publicToAddress;
const testAccount = generate('sdkjfhskjhskhjsfkjhsf093j9sdjfpisjdfoisjdfisdfsfkjhsfkjhskjfhkshdf');
const rawTx = {
to: testAccount.address.toLowerCase(),
nonce: `0x${new BN(0).toString(16)}`,
gasPrice: `0x${new BN(0).toString(16)}`,
gasLimit: `0x${new BN(0).toString(16)}`,
value: `0x${new BN(0).toString(16)}`,
data: '0x',
};
const signedTx = sign(rawTx, testAccount.privateKey, true);
const signedTxBuffer = new Buffer(stripHexPrefix(sign(rawTx, testAccount.privateKey)), 'hex');
const publicKey = recover(signedTxBuffer,
(new BN(signedTx[6].toString('hex'), 16).toNumber(10)),
signedTx[7],
signedTx[8]);
const address = publicToAddress(publicKey);
if (address == testAccount.address) {
console.log('Recovery success!');
}
```
## Browser Builds
`ethjs` provides production distributions for all of its modules that are ready for use in the browser right away. Simply include either `dist/ethjs-signer.js` or `dist/ethjs-signer.min.js` directly into an HTML file to start using this module. Note, an `ethSigner` object is made available globally.
```html
<script type="text/javascript" src="ethjs-signer.min.js"></script>
<script type="text/javascript">
ethSigner(...);
</script>
```
Note, even though `ethjs` should have transformed and polyfilled most of the requirements to run this module across most modern browsers. You may want to look at an additional polyfill for extra support.
Use a polyfill service such as `Polyfill.io` to ensure complete cross-browser support:
https://polyfill.io/
## Latest Webpack Figures
```
Hash: ab378ace2dcbcdc84923
Version: webpack 2.1.0-beta.15
Time: 1041ms
Asset Size Chunks Chunk Names
ethjs-signer.js 313 kB 0 [emitted] main
ethjs-signer.js.map 385 kB 0 [emitted] main
[37] multi main 28 bytes {0} [built]
+ 37 hidden modules
Hash: f6b69ff438836239c7a6
Version: webpack 2.1.0-beta.15
Time: 4124ms
Asset Size Chunks Chunk Names
ethjs-signer.min.js 164 kB 0 [emitted] main
[37] multi main 28 bytes {0} [built]
+ 37 hidden modules
```
## Other Awesome Modules, Tools and Frameworks
### Foundation
- [web3.js](https://github.com/ethereum/web3.js) -- the original Ethereum JS swiss army knife **Ethereum Foundation**
- [ethereumjs](https://github.com/ethereumjs) -- critical ethereum javascript infrastructure **Ethereum Foundation**
- [browser-solidity](https://ethereum.github.io/browser-solidity) -- an in browser Solidity IDE **Ethereum Foundation**
### Nodes
- [geth](https://github.com/ethereum/go-ethereum) Go-Ethereum
- [parity](https://github.com/ethcore/parity) Rust-Ethereum build in Rust
- [testrpc](https://github.com/ethereumjs/testrpc) Testing Node (ethereumjs-vm)
### Testing
- [wafr](https://github.com/silentcicero/wafr) -- a super simple Solidity testing framework
- [truffle](https://github.com/ConsenSys/truffle) -- a solidity/js dApp framework
- [embark](https://github.com/iurimatias/embark-framework) -- a solidity/js dApp framework
- [dapple](https://github.com/nexusdev/dapple) -- a solidity dApp framework
- [chaitherium](https://github.com/SafeMarket/chaithereum) -- a JS web3 unit testing framework
- [contest](https://github.com/DigixGlobal/contest) -- a JS testing framework for contracts
### Wallets
- [ethers-wallet](https://github.com/ethers-io/ethers-wallet) -- an amazingly small Ethereum wallet
- [metamask](https://metamask.io/) -- turns your browser into an Ethereum enabled browser =D
## Our Relationship with Ethereum & EthereumJS
We would like to mention that we are not in any way affiliated with the Ethereum Foundation or `ethereumjs`. However, we love the work they do and work with them often to make Ethereum great! Our aim is to support the Ethereum ecosystem with a policy of diversity, modularity, simplicity, transparency, clarity, optimization and extensibility.
Many of our modules use code from `web3.js` and the `ethereumjs-` repositories. We thank the authors where we can in the relevant repositories. We use their code carefully, and make sure all test coverage is ported over and where possible, expanded on.
## A Special Thanks
A special thanks to Richard Moore for building `ethers-wallet` and other amazing things. Aaron Davis (@kumavis) for his guidence.
var webpack = require('webpack'); // eslint-disable-line
var env = process.env.NODE_ENV; // eslint-disable-line
var filename = 'ethjs-signer'; // eslint-disable-line
var library = 'ethSigner'; // eslint-disable-line
var config = { // eslint-disable-line
entry: [
'./lib/index.js',
],
module: {
loaders: [
{
test: /\.js$/,
loaders: ['babel-loader'],
exclude: /node_modules/,
},
{
test: /\.json$/,
loader: 'json',
},
],
},
devtool: 'cheap-module-source-map',
output: {
path: 'dist',
filename: filename + '.js', // eslint-disable-line
library: library, // eslint-disable-line
libraryTarget: 'umd',
umdNamedDefine: true,
},
plugins: [
new webpack.BannerPlugin({ banner: ' /* eslint-disable */ ', raw: true, entryOnly: true }),
new webpack.BannerPlugin({ banner: ' /* eslint-disable */ ', raw: true }),
new webpack.optimize.OccurrenceOrderPlugin(),
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify(env),
}),
],
};
if (env === 'production') {
config.output.filename = filename + '.min.js'; // eslint-disable-line
config.plugins
.push(new webpack.optimize.UglifyJsPlugin({
compressor: {
pure_getters: true,
unsafe: true,
unsafe_comps: true,
warnings: false,
screw_ie8: false,
},
mangle: {
screw_ie8: false,
},
output: {
screw_ie8: false,
},
}));
config.plugins.push(new webpack.optimize.DedupePlugin());
}
module.exports = config;
{
"name": "ethjs-signer",
"version": "0.1.0",
"description": "A simple module for signing Ethereum transactions.",
"main": "lib/index.js",
"files": [
"dist",
"internals",
"lib",
"src"
],
"scripts": {
"start": "npm test",
"release": "npmpub",
"pretest": "npm run lint",
"prepublish": "npm run build",
"prebuild": "npm run build:clean && npm run test",
"build:clean": "npm run test:clean && rimraf ./dist",
"build:commonjs": "cross-env BABEL_ENV=commonjs babel src --out-dir lib --copy-files",
"build:umd": "cross-env BABEL_ENV=commonjs NODE_ENV=development webpack --config ./internals/webpack/webpack.config.js --progress",
"build:umd:min": "cross-env BABEL_ENV=commonjs NODE_ENV=production webpack --config ./internals/webpack/webpack.config.js --progress",
"build:stats": "cross-env BABEL_ENV=commonjs NODE_ENV=development webpack --config ./internals/webpack/webpack.config.js --progress --profile --json > dist/stats.json",
"build": "npm run build:commonjs && npm run test:lib && npm run build:umd && npm run build:umd:min",
"lint": "npm run lint:js",
"lint:eslint": "eslint --ignore-path .gitignore --ignore-pattern **/**.min.js",
"lint:js": "npm run lint:eslint -- . ",
"lint:staged": "lint-staged",
"test:clean": "rimraf ./coverage",
"test": "mocha ./src/tests/**/*.js -R spec --timeout 2000000",
"test:lib": "mocha ./lib/tests/**/*.js -R spec --timeout 2000000",
"test-travis": "node ./node_modules/istanbul/lib/cli.js cover ./node_modules/mocha/bin/_mocha -- src/tests/**/*.js -R spec --timeout 2000000",
"coveralls": "npm run test-travis && cat ./coverage/lcov.info | coveralls"
},
"repository": {
"type": "git",
"url": "git+ssh://[email protected]/ethjs/ethjs-signer.git"
},
"keywords": [
"ethereum",
"rpc",
"formatter",
"format",
"ethereum",
"encoding",
"decoding"
],
"authors": [
"Nick Dodson <[email protected]>"
],
"contributors": [
{
"name": "Richard Moore",
"email": "[email protected]",
"url": "https://ethers.io"
}
],
"engines": {
"npm": ">=3",
"node": ">=6.5.0"
},
"license": "MIT",
"bugs": {
"url": "https://github.com/ethjs/ethjs-signer/issues"
},
"homepage": "https://github.com/ethjs/ethjs-signer#readme",
"babel": {
"plugins": [
["transform-es2015-template-literals", { "loose": true }],
"transform-es2015-literals",
"transform-es2015-function-name",
"transform-es2015-arrow-functions",
"transform-es2015-block-scoped-functions",
["transform-es2015-classes", { "loose": true }],
"transform-es2015-object-super",
"transform-es2015-shorthand-properties",
["transform-es2015-computed-properties", { "loose": true }],
["transform-es2015-for-of", { "loose": true }],
"transform-es2015-sticky-regex",
"transform-es2015-unicode-regex",
"check-es2015-constants",
["transform-es2015-spread", { "loose": true }],
"transform-es2015-parameters",
["transform-es2015-destructuring", { "loose": true }],
"transform-es2015-block-scoping",
"transform-object-rest-spread",
"transform-es3-member-expression-literals",
"transform-es3-property-literals"
],
"env": {
"commonjs": {
"plugins": [
["transform-es2015-modules-commonjs", { "loose": true }]
]
}
}
},
"dependencies": {
"ethjs-sha3": "0.1.2",
"rlp": "2.0.0",
"strip-hex-prefix": "1.0.0",
"number-to-bn": "1.1.0",
"elliptic": "6.3.2"
},
"devDependencies": {
"babel-cli": "6.18.0",
"babel-core": "6.18.2",
"babel-eslint": "7.1.0",
"babel-loader": "6.2.8",
"ethereumjs-util": "5.0.1",
"sandboxed-module": "2.0.3",
"ethereumjs-testrpc": "3.0.2",
"ethereumjs-tx": "1.1.4",
"ethjs-query": "0.1.1",
"ethjs-account": "0.1.0",
"babel-plugin-check-es2015-constants": "6.8.0",
"babel-plugin-transform-es2015-arrow-functions": "6.8.0",
"babel-plugin-transform-es2015-block-scoped-functions": "6.8.0",
"babel-plugin-transform-es2015-block-scoping": "6.18.0",
"babel-plugin-transform-es2015-classes": "6.18.0",
"babel-plugin-transform-es2015-computed-properties": "6.8.0",
"babel-plugin-transform-es2015-destructuring": "6.19.0",
"babel-plugin-transform-es2015-for-of": "6.18.0",
"babel-plugin-transform-es2015-function-name": "6.9.0",
"babel-plugin-transform-es2015-literals": "6.8.0",
"babel-plugin-transform-es2015-modules-commonjs": "6.18.0",
"babel-plugin-transform-es2015-object-super": "6.8.0",
"babel-plugin-transform-es2015-parameters": "6.18.0",
"babel-plugin-transform-es2015-shorthand-properties": "6.18.0",
"babel-plugin-transform-es2015-spread": "6.8.0",
"babel-plugin-transform-es2015-sticky-regex": "6.8.0",
"babel-plugin-transform-es2015-template-literals": "6.8.0",
"babel-plugin-transform-es2015-unicode-regex": "6.11.0",
"babel-plugin-transform-es3-member-expression-literals": "6.5.0",
"babel-plugin-transform-es3-property-literals": "6.5.0",
"babel-plugin-transform-object-rest-spread": "6.19.0",
"babel-register": "6.18.0",
"check-es3-syntax-cli": "0.1.3",
"webpack": "2.1.0-beta.15",
"json-loader": "0.5.4",
"rimraf": "2.3.4",
"cross-env": "1.0.7",
"chai": "3.5.0",
"coveralls": "2.11.9",
"js-sha3": "0.5.5",
"istanbul": "0.4.5",
"eslint": "2.10.1",
"eslint-config-airbnb": "9.0.1",
"eslint-import-resolver-webpack": "0.2.4",
"eslint-plugin-import": "1.8.0",
"eslint-plugin-jsx-a11y": "1.2.0",
"eslint-plugin-react": "5.1.1",
"eventsource-polyfill": "0.9.6",
"lint-staged": "1.0.1",
"mocha": "3.1.2",
"pre-commit": "1.1.3",
"web3": "0.17.0-beta"
},
"lint-staged": {
"lint:eslint": "*.js"
},
"eslintConfig": {
"parser": "babel-eslint",
"extends": "airbnb",
"env": {
"node": true,
"mocha": true,
"es6": true
},
"parserOptions": {
"ecmaVersion": 6,
"sourceType": "module"
},
"rules": {
"import/no-unresolved": 2,
"comma-dangle": [
2,
"always-multiline"
],
"indent": [
2,
2,
{
"SwitchCase": 1
}
],
"no-console": 1,
"max-len": 0,
"prefer-template": 2,
"no-use-before-define": 0,
"newline-per-chained-call": 0,
"arrow-body-style": [
2,
"as-needed"
]
}
},
"pre-commit": "build"
}
const rlp = require('rlp');
const elliptic = require('elliptic');
const sha3 = require('ethjs-sha3');
const secp256k1 = new (elliptic.ec)('secp256k1'); // eslint-disable-line
const stripHexPrefix = require('strip-hex-prefix');
const numberToBN = require('number-to-bn');
function stripZeros(buffer) {
var i = 0; // eslint-disable-line
for (i = 0; i < buffer.length; i++) {
if (buffer[i] !== 0) { break; }
}
return (i > 0) ? buffer.slice(i) : buffer;
}
function padToEven(str) {
return str.length % 2 ? `0${str}` : str;
}
function bnToBuffer(bn) {
return stripZeros(new Buffer(padToEven(bn.toString(16)), 'hex'));
}
const transactionFields = [
{ name: 'nonce', maxLength: 32, number: true },
{ name: 'gasPrice', maxLength: 32, number: true },
{ name: 'gasLimit', maxLength: 32, number: true },
{ name: 'to', length: 20 },
{ name: 'value', maxLength: 32, number: true },
{ name: 'data' },
];
/**
* ECDSA public key recovery from a rawTransaction
*
* @method recover
* @param {String|Buffer} rawTransaction either a hex string or buffer instance
* @param {Number} v
* @param {Buffer} r
* @param {Buffer} s
* @return {Buffer} publicKey
*/
function recover(rawTx, v, r, s) {
const rawTransaction = typeof(rawTx) === 'string' ? new Buffer(stripHexPrefix(rawTx), 'hex') : rawTx;
const signedTransaction = rlp.decode(rawTransaction);
const raw = [];
transactionFields.forEach((fieldInfo, fieldIndex) => {
raw[fieldIndex] = signedTransaction[fieldIndex];
});
const publicKey = secp256k1.recoverPubKey(sha3(rlp.encode(raw), true), { r, s }, v - 27);
return (new Buffer(publicKey.encode('hex', false), 'hex')).slice(1);
}
/**
* Will sign a raw transaction and return it either as a serlized hex string or raw tx object.
*
* @method sign
* @param {Object} transaction a valid transaction object
* @param {String} privateKey a valid 32 byte prefixed hex string private key
* @param {Boolean} toObject **Optional**
* @returns {String|Object} output either a serilized hex string or signed tx object
*/
function sign(transaction, privateKey, toObject) {
if (typeof transaction !== 'object' || transaction === null) { throw new Error(`[ethjs-signer] transaction input must be a type 'object', got '${typeof(transaction)}'`); }
if (typeof privateKey !== 'string') { throw new Error('[ethjs-signer] private key input must be a string'); }
if (!privateKey.match(/^(0x)[0-9a-fA-F]{64}$/)) { throw new Error('[ethjs-signer] invalid private key value, private key must be a prefixed hexified 32 byte string (i.e. "0x..." 64 chars long).'); }
const raw = [];
transactionFields.forEach((fieldInfo) => {
var value = new Buffer(0); // eslint-disable-line
// shim for field name gas
const txKey = (fieldInfo.name === 'gasLimit' && transaction.gas) ? 'gas' : fieldInfo.name;
if (typeof transaction[txKey] !== 'undefined') {
if (fieldInfo.number === true) {
value = bnToBuffer(numberToBN(transaction[txKey]));
} else {
value = new Buffer(padToEven(stripHexPrefix(transaction[txKey])), 'hex');
}
}
// Fixed-width field
if (fieldInfo.length && value.length !== fieldInfo.length && value.length > 0) {
throw new Error(`[ethjs-signer] while signing raw transaction, invalid '${fieldInfo.name}', invalid length should be '${fieldInfo.length}' got '${value.length}'`);
}
// Variable-width (with a maximum)
if (fieldInfo.maxLength) {
value = stripZeros(value);
if (value.length > fieldInfo.maxLength) {
throw new Error(`[ethjs-signer] while signing raw transaction, invalid '${fieldInfo.name}' length, the max length is '${fieldInfo.maxLength}', got '${value.length}'`);
}
}
raw.push(value);
});
// private key is not stored in memory
const signature = secp256k1.keyFromPrivate(new Buffer(privateKey.slice(2), 'hex'))
.sign(sha3(rlp.encode(raw), true), { canonical: true });
raw.push(new Buffer([27 + signature.recoveryParam]));
raw.push(bnToBuffer(signature.r));
raw.push(bnToBuffer(signature.s));
return toObject ? raw : `0x${rlp.encode(raw).toString('hex')}`;
}
module.exports = {
sign,
recover,
};
const sign = require('../index.js').sign; // eslint-disable-line
const recover = require('../index.js').recover;
const TestRPC = require('ethereumjs-testrpc'); // eslint-disable-line
const Eth = require('ethjs-query'); // eslint-disable-line
const EthTx = require('ethereumjs-tx'); // eslint-disable-line
const generate = require('ethjs-account').generate; // eslint-disable-line
const publicToAddress = require('ethjs-account').publicToAddress; // eslint-disable-line
const assert = require('chai').assert;
const stripHexPrefix = require('strip-hex-prefix');
const BN = require('bn.js');
describe('recover', () => {
describe('construction', () => {
it('should import normally', () => {
assert.equal(typeof recover, 'function');
});
});
describe('functionality', () => {
it('should recover from signed tx string', () => {
const testAccount = generate('sdkjfhskjhskhjsfkjhsf093j9sdjfpisjdfoisjdfisdfsfkjhsfkjhskjfhkshdf');
const rawTx = {
to: testAccount.address.toLowerCase(),
nonce: `0x${new BN(0).toString(16)}`,
gasPrice: `0x${new BN(0).toString(16)}`,
gasLimit: `0x${new BN(0).toString(16)}`,
value: `0x${new BN(0).toString(16)}`,
data: '0',
};
const signedTx = sign(rawTx, testAccount.privateKey, true);
const signedTxString = sign(rawTx, testAccount.privateKey);
const publicKey = recover(signedTxString,
(new BN(signedTx[6].toString('hex'), 16).toNumber(10)),
signedTx[7],
signedTx[8]);
const address = publicToAddress(publicKey);
assert.equal(address, testAccount.address);
});
it('should recover from signed tx buffer', () => {
const testAccount = generate('sdkjfhskjhskhjsfkjhsf093j9sdjfpisjdfoisjdfisdfsfkjhsfkjhskjfhkshdf');
const rawTx = {
to: testAccount.address.toLowerCase(),
nonce: `0x${new BN(0).toString(16)}`,
gasPrice: `0x${new BN(0).toString(16)}`,
gasLimit: `0x${new BN(0).toString(16)}`,
value: `0x${new BN(0).toString(16)}`,
data: '0x',
};
const signedTx = sign(rawTx, testAccount.privateKey, true);
const signedTxBuffer = new Buffer(stripHexPrefix(sign(rawTx, testAccount.privateKey)), 'hex');
const publicKey = recover(signedTxBuffer,
(new BN(signedTx[6].toString('hex'), 16).toNumber(10)),
signedTx[7],
signedTx[8]);
const address = publicToAddress(publicKey);
assert.equal(address, testAccount.address);
});
});
});
describe('sign', () => {
describe('construction', () => {
it('should import normally', () => {
assert.equal(typeof sign, 'function');
});
});
describe('functionality', () => {
const account = generate('sdkjfhskjhskhjsfkjhsf093j9sdjfpisjdfoisjdfisdfsfkjhsfkjhskjfhkshdf');
it('test gas shim', () => {
assert.deepEqual(sign({ to: account.address, gas: 3000000 }, account.privateKey),
sign({ to: account.address, gasLimit: 3000000 }, account.privateKey));
});
it('should sign a valid tx', () => {
assert.equal(typeof sign({ to: account.address }, account.privateKey), 'string');
assert.equal(typeof sign({ to: account.address }, account.privateKey, true), 'object');
});
it('should throw when invalid', () => {
assert.throws(() => sign(''), Error);
assert.throws(() => sign({}, ''), Error);
assert.throws(() => sign({ to: account.address, gas: '0x89724982892748972349874239847987s29sdfhkjjsfh8823927482978923793248829724397' }, account.privateKey), Error);
assert.throws(() => sign({ to: '0x00' }, account.privateKey), Error);
assert.throws(() => sign({}, '0xfsd98'), Error);
assert.throws(() => sign({}, '0xkjdsfkjfsdkjs'), Error);
assert.throws(() => sign({}, 234879243), Error);
assert.throws(() => sign({}, null), Error);
assert.throws(() => sign(null, 243249), Error);
// assert.throws(() => sign({}, account.privateKey), Error);
});
});
describe('field testing against TestRPC', () => {
it('should send a signed tx with testrpc provider', (done) => {
const testAccount = generate('sdkjfhskjhskhjsfkjhsf093j9sdjfpisjdfoisjdfisdfsfkjhsfkjhskjfhkshdf');
const provider = TestRPC.provider({
accounts: [{
address: testAccount.address,
secretKey: testAccount.privateKey,
balance: 999999999,
}],
});
const eth = new Eth(provider);
const rawTx = {
to: '0x6023E44829921590b24f458c9eE4F544507d59B6',
gas: 300000,
value: new BN(450000),
};
const signedTx = sign(rawTx, testAccount.privateKey);
eth.sendRawTransaction(signedTx, (err, txHash) => {
assert.equal(err, null);
assert.equal(typeof txHash, 'string');
setTimeout(() => {
eth.getTransactionByHash(txHash, (rErr, transaction) => {
assert.equal(rErr, null);
assert.equal(testAccount.address.toLowerCase(), transaction.from);
assert.equal(rawTx.to.toLowerCase(), transaction.to);
assert.deepEqual(rawTx.value.toString(10), transaction.value.toString(10));
assert.deepEqual(new BN(rawTx.gas).toString(10), transaction.gas.toString(10));
done();
});
}, 400);
});
});
});
describe('should work the same as ethereumjs-tx', () => {
it('should pass a thousand random signing tests', () => {
for (var i = 0; i < 1000; i++) { // eslint-disable-line
const testAccount = generate('sdkjfhskjhskhjsfkjhsf093j9sdjfpisjdfoisjdfisdfsfkjhsfkjhskjfhkshdf');
const rawTx = {
to: testAccount.address.toLowerCase(),
nonce: `0x${new BN(0).toString(16)}`,
gasPrice: `0x${new BN(0).toString(16)}`,
gasLimit: `0x${new BN(0).toString(16)}`,
value: `0x${new BN(0).toString(16)}`,
data: '0x',
};
const tx = new EthTx(rawTx);
tx.sign(new Buffer(testAccount.privateKey.slice(2), 'hex'));
const ethjsSigner = sign(rawTx, testAccount.privateKey);
const ethereumjsTx = `0x${tx.serialize().toString('hex')}`;
assert.equal(ethjsSigner, ethereumjsTx);
}
});
});
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment