Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove the crypto library #169

Merged
merged 5 commits into from
Mar 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
As this project is pre 1.0, breaking changes may happen for minor version bumps.
A breaking change will get clearly notified in this log.

## In master

- Remove the `crypto` library. This reduces the number of Node built-ins we have
to shim into the production bundle, and incidentally fixes a bug with
Angular 6.

## [v0.12.0](https://github.com/stellar/js-stellar-base/compare/v0.11.0...v0.12.0)

- _Warning_ Calling TransactionBuilder without a `fee` param is now deprecated
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
"minami": "^1.1.1",
"mocha": "^5.2.0",
"prettier": "^1.16.1",
"randombytes": "^2.1.0",
"run-sequence": "^1.0.2",
"sinon": "^1.14.1",
"sinon-chai": "^2.7.0",
Expand Down
6 changes: 1 addition & 5 deletions src/transaction.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import map from 'lodash/map';
import each from 'lodash/each';
import isString from 'lodash/isString';
import crypto from 'crypto';
import { xdr, hash } from './index';

import { StrKey } from './strkey';
Expand Down Expand Up @@ -162,10 +161,7 @@ export class Transaction {
}

const signature = preimage;
const hashX = crypto
.createHash('sha256')
.update(preimage)
.digest();
const hashX = hash(preimage);
const hint = hashX.slice(hashX.length - 4);
this.signatures.push(new xdr.DecoratedSignature({ hint, signature }));
}
Expand Down
28 changes: 28 additions & 0 deletions test/unit/crypto_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import crypto from 'crypto';

function expectBuffersToBeEqual(left, right) {
let leftHex = left.toString('hex');
let rightHex = right.toString('hex');
expect(leftHex).to.eql(rightHex);
}

/*
We want to replace the crypto library (a Node built-in) with one
that works on both server and browser.

Before we do that, we should make sure the new library does the
same thing as the old one. This is basically the only way crypto is
used on the site.
*/

it('new hashing function behaves like crypto', () => {
const input = 'I really hope this works';
const cryptoHash = crypto
.createHash('sha256')
.update(input)
.digest();

const newHash = StellarBase.hash(input);

expectBuffersToBeEqual(cryptoHash, newHash);
});
21 changes: 4 additions & 17 deletions test/unit/operation_test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import BigNumber from 'bignumber.js';
import crypto from 'crypto';
import isString from 'lodash/isString';

describe('Operation', function() {
Expand Down Expand Up @@ -392,10 +391,7 @@ describe('Operation', function() {
it('creates a setOptionsOp with preAuthTx signer', function() {
var opts = {};

var hash = crypto
.createHash('sha256')
.update('Tx hash')
.digest();
var hash = StellarBase.hash('Tx hash');

opts.signer = {
preAuthTx: hash,
Expand All @@ -416,10 +412,7 @@ describe('Operation', function() {
it('creates a setOptionsOp with preAuthTx signer from a hex string', function() {
var opts = {};

var hash = crypto
.createHash('sha256')
.update('Tx hash')
.digest('hex');
var hash = StellarBase.hash('Tx hash').toString('hex');
expect(isString(hash)).to.be.true;

opts.signer = {
Expand All @@ -441,10 +434,7 @@ describe('Operation', function() {
it('creates a setOptionsOp with hash signer', function() {
var opts = {};

var hash = crypto
.createHash('sha256')
.update('Hash Preimage')
.digest();
var hash = StellarBase.hash('Hash Preimage');

opts.signer = {
sha256Hash: hash,
Expand All @@ -465,10 +455,7 @@ describe('Operation', function() {
it('creates a setOptionsOp with hash signer from a hex string', function() {
var opts = {};

var hash = crypto
.createHash('sha256')
.update('Hash Preimage')
.digest('hex');
var hash = StellarBase.hash('Hash Preimage').toString('hex');
expect(isString(hash)).to.be.true;

opts.signer = {
Expand Down
15 changes: 4 additions & 11 deletions test/unit/transaction_test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import crypto from 'crypto';
import randomBytes from 'randombytes';

describe('Transaction', function() {
it('constructs Transaction object from a TransactionEnvelope', function(done) {
Expand Down Expand Up @@ -102,11 +102,8 @@ describe('Transaction', function() {
let asset = StellarBase.Asset.native();
let amount = '2000';

let preimage = crypto.randomBytes(64);
let hash = crypto
.createHash('sha256')
.update(preimage)
.digest();
let preimage = randomBytes(64);
let hash = StellarBase.hash(preimage);

let tx = new StellarBase.TransactionBuilder(source, { fee: 100 })
.addOperation(
Expand Down Expand Up @@ -134,11 +131,7 @@ describe('Transaction', function() {
let asset = StellarBase.Asset.native();
let amount = '2000';

let preimage = crypto.randomBytes(2 * 64);
let hash = crypto
.createHash('sha256')
.update(preimage)
.digest();
let preimage = randomBytes(2 * 64);

let tx = new StellarBase.TransactionBuilder(source, { fee: 100 })
.addOperation(
Expand Down
7 changes: 7 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5914,6 +5914,13 @@ randomatic@^3.0.0:
kind-of "^6.0.0"
math-random "^1.0.1"

randombytes@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/randombytes/-/randombytes-2.1.0.tgz#df6f84372f0270dc65cdf6291349ab7a473d4f2a"
integrity sha512-vYl3iOX+4CKUWuxGi9Ukhie6fsqXqS9FE2Zaic4tNFD2N2QQaXOMFbuKK4QmDHC0JO6B1Zp41J0LpT0oR68amQ==
dependencies:
safe-buffer "^5.1.0"

range-parser@^1.0.3, range-parser@^1.2.0, range-parser@~1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/range-parser/-/range-parser-1.2.0.tgz#f49be6b487894ddc40dcc94a322f611092e00d5e"
Expand Down