Skip to content

Commit

Permalink
Remove sjcl-extended/ripple-wallet-generator. Use hash.js & sjcl-codec.
Browse files Browse the repository at this point in the history
  • Loading branch information
sublimator committed Aug 14, 2015
1 parent 8c431b4 commit 5837aa2
Show file tree
Hide file tree
Showing 19 changed files with 96 additions and 166 deletions.
32 changes: 17 additions & 15 deletions npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Expand Up @@ -21,15 +21,15 @@
"bn.js": "^3.1.1",
"es6-promisify": "^2.0.0",
"extend": "~1.2.1",
"hash.js": "^1.0.3",
"https-proxy-agent": "^1.0.0",
"is-my-json-valid": "^2.12.0",
"lodash": "^3.1.0",
"lru-cache": "~2.5.0",
"ripple-address-codec": "^1.6.0",
"ripple-keypairs": "^0.7.3",
"ripple-keypairs": "^0.8.0",
"ripple-lib-transactionparser": "^0.5.0",
"ripple-wallet-generator": "^1.0.3",
"sjcl-extended": "ripple/sjcl-extended#1.0.3",
"sjcl-codec": "0.1.0",
"ws": "~0.7.1"
},
"devDependencies": {
Expand Down
1 change: 1 addition & 0 deletions src/api/common/index.js
Expand Up @@ -9,6 +9,7 @@ module.exports = {
dropsToXrp: utils.dropsToXrp,
xrpToDrops: utils.xrpToDrops,
toRippledAmount: utils.toRippledAmount,
generateWallet: utils.generateWallet,
composeAsync: utils.composeAsync,
wrapCatch: utils.wrapCatch,
convertExceptions: utils.convertExceptions,
Expand Down
7 changes: 7 additions & 0 deletions src/api/common/utils.js
Expand Up @@ -5,6 +5,7 @@ const BigNumber = require('bignumber.js');
const core = require('../../core');
const errors = require('./errors');
const es6promisify = require('es6-promisify');
const keypairs = require('ripple-keypairs');

type Amount = {currency: string, issuer: string, value: string}

Expand All @@ -27,6 +28,11 @@ function toRippledAmount(amount: Amount): string|Amount {
};
}

function generateWallet(options?: Object): Object {
const {accountID, seed} = keypairs.generateWallet(options);
return {secret: seed, address: accountID};
}

type AsyncFunction = (...x: any) => void

function wrapCatch(asyncFunction: AsyncFunction): AsyncFunction {
Expand Down Expand Up @@ -95,6 +101,7 @@ module.exports = {
dropsToXrp,
xrpToDrops,
toRippledAmount,
generateWallet,
composeAsync,
wrapCatch,
convertExceptions,
Expand Down
2 changes: 1 addition & 1 deletion src/api/index.js
Expand Up @@ -28,7 +28,7 @@ const sign = require('./transaction/sign');
const submit = require('./transaction/submit');
const errors = require('./common').errors;
const convertExceptions = require('./common').convertExceptions;
const generateWallet = convertExceptions(common.core.Wallet.generate);
const generateWallet = convertExceptions(common.generateWallet);
const getLedgerHeader = require('./ledger/ledger-header');

function RippleAPI(options: {}) {
Expand Down
24 changes: 10 additions & 14 deletions src/core/account.js
Expand Up @@ -12,13 +12,14 @@

const _ = require('lodash');
const async = require('async');
const util = require('util');
const extend = require('extend');
const EventEmitter = require('events').EventEmitter;
const UInt160 = require('./uint160').UInt160;
const TransactionManager = require('./transactionmanager').TransactionManager;
const sjcl = require('./utils').sjcl;
const Base = require('./base').Base;
const util = require('util');
const {createAccountID} = require('ripple-keypairs');
const {encodeAccountID} = require('ripple-address-codec');
const {EventEmitter} = require('events');
const {hexToArray} = require('./utils');
const {TransactionManager} = require('./transactionmanager');
const {UInt160} = require('./uint160');

/**
* @constructor Account
Expand All @@ -41,7 +42,7 @@ function Account(remote, account) {
this._entry = { };

function listenerAdded(type) {
if (~Account.subscribeEvents.indexOf(type)) {
if (_.includes(Account.subscribeEvents, type)) {
if (!self._subs && self._remote._connected) {
self._remote.requestSubscribe()
.addAccount(self._account_id)
Expand All @@ -54,7 +55,7 @@ function Account(remote, account) {
this.on('newListener', listenerAdded);

function listenerRemoved(type) {
if (~Account.subscribeEvents.indexOf(type)) {
if (_.includes(Account.subscribeEvents, type)) {
self._subs -= 1;
if (!self._subs && self._remote._connected) {
self._remote.requestUnsubscribe()
Expand Down Expand Up @@ -376,12 +377,7 @@ Account.prototype.publicKeyIsActive = function(public_key, callback) {
Account._publicKeyToAddress = function(public_key) {
// Based on functions in /src/js/ripple/keypair.js
function hexToUInt160(publicKey) {
const bits = sjcl.codec.hex.toBits(publicKey);
const hash = sjcl.hash.ripemd160.hash(sjcl.hash.sha256.hash(bits));
const address = UInt160.from_bits(hash);
address.set_version(Base.VER_ACCOUNT_ID);

return address.to_json();
return encodeAccountID(createAccountID(hexToArray(publicKey)));
}

if (UInt160.is_valid(public_key)) {
Expand Down
9 changes: 0 additions & 9 deletions src/core/index.js
Expand Up @@ -27,13 +27,4 @@ exports._test = {
RangeSet: require('./rangeset').RangeSet
};

// Important: We do not guarantee any specific version of SJCL or for any
// specific features to be included. The version and configuration may change at
// any time without warning.
//
// However, for programs that are tied to a specific version of ripple.js like
// the official client, it makes sense to expose the SJCL instance so we don't
// have to include it twice.
exports.sjcl = require('./utils').sjcl;
exports.Wallet = require('ripple-wallet-generator')({sjcl: exports.sjcl});
exports.types = require('./serializedtypes');
18 changes: 8 additions & 10 deletions src/core/seed.js
Expand Up @@ -5,16 +5,15 @@
//

const {KeyPair, KeyType} = require('ripple-keypairs');
const codec = require('ripple-address-codec');
const {decodeSeed, encodeSeed} = require('ripple-address-codec');
const extend = require('extend');
const sjclcodec = require('sjcl-codec');
const BN = require('bn.js');
const utils = require('./utils');
const hashjs = require('hash.js');

const sjcl = utils.sjcl;
const UInt = require('./uint').UInt;

const Seed = extend(function() {
this._curve = sjcl.ecc.curves.k256;
this._value = NaN;
this._type = KeyType.secp256k1;
}, UInt);
Expand Down Expand Up @@ -54,7 +53,7 @@ Seed.prototype.parse_base58 = function(j) {
this._value = NaN;
} else {
try {
const {bytes, type} = codec.decodeSeed(j);
const {bytes, type} = decodeSeed(j);
this._value = new BN(bytes);
this._type = type;
} catch (e) {
Expand All @@ -74,10 +73,9 @@ Seed.prototype.parse_passphrase = function(j) {
throw new Error('Passphrase must be a string');
}

const hash = sjcl.hash.sha512.hash(sjcl.codec.utf8String.toBits(j));
const bits = sjcl.bitArray.bitSlice(hash, 0, 128);

this.parse_bits(bits);
const phraseBytes = sjclcodec.bytes.fromBits(sjclcodec.utf8String.toBits(j));
const hash = hashjs.sha512().update(phraseBytes).digest();
this.parse_bytes(hash.slice(0, 16));

return this;
};
Expand All @@ -86,7 +84,7 @@ Seed.prototype.to_json = function() {
if (!(this.is_valid())) {
return NaN;
}
return codec.encodeSeed(this.to_bytes(), this._type);
return encodeSeed(this.to_bytes(), this._type);
};

Seed.prototype.get_key = function() {
Expand Down
16 changes: 7 additions & 9 deletions src/core/serializedobject.js
Expand Up @@ -3,13 +3,13 @@
const assert = require('assert');
const extend = require('extend');
const BN = require('bn.js');
const hashjs = require('hash.js');
const sjclcodec = require('sjcl-codec');
const binformat = require('./binformat');
const stypes = require('./serializedtypes');
const utils = require('./utils');
const UInt256 = require('./uint256').UInt256;

const sjcl = utils.sjcl;

const TRANSACTION_TYPES = { };

Object.keys(binformat.tx).forEach(function(key) {
Expand Down Expand Up @@ -37,7 +37,7 @@ function SerializedObject(buf) {
if (Array.isArray(buf) || (Buffer && Buffer.isBuffer(buf))) {
this.buffer = buf;
} else if (typeof buf === 'string') {
this.buffer = sjcl.codec.bytes.fromBits(sjcl.codec.hex.toBits(buf));
this.buffer = sjclcodec.bytes.fromBits(sjclcodec.hex.toBits(buf));
} else if (!buf) {
this.buffer = [];
} else {
Expand Down Expand Up @@ -201,11 +201,11 @@ SerializedObject.prototype.read = readOrPeek(true);
SerializedObject.prototype.peek = readOrPeek(false);

SerializedObject.prototype.to_bits = function() {
return sjcl.codec.bytes.toBits(this.buffer);
return sjclcodec.bytes.toBits(this.buffer);
};

SerializedObject.prototype.to_hex = function() {
return sjcl.codec.hex.fromBits(this.to_bits()).toUpperCase();
return sjclcodec.hex.fromBits(this.to_bits()).toUpperCase();
};

SerializedObject.prototype.to_json = function() {
Expand Down Expand Up @@ -302,11 +302,9 @@ SerializedObject.prototype.hash = function(prefix) {

// Copy buffer to temporary buffer
sign_buffer.append(this.buffer);
const bytes = hashjs.sha512().update(sign_buffer.buffer).digest();

const bits = sjcl.codec.bytes.toBits(sign_buffer.buffer);
const sha512hex = sjcl.codec.hex.fromBits(sjcl.hash.sha512.hash(bits));

return UInt256.from_hex(sha512hex.substr(0, 64).toUpperCase());
return UInt256.from_bytes(bytes.slice(0, 32));
};

// DEPRECATED
Expand Down
12 changes: 6 additions & 6 deletions src/core/serializedtypes.js
Expand Up @@ -12,11 +12,11 @@ const assert = require('assert');
const extend = require('extend');
const BN = require('bn.js');
const GlobalBigNumber = require('bignumber.js');
const sjclcodec = require('sjcl-codec');
const Amount = require('./amount').Amount;
const Currency = require('./currency').Currency;
const binformat = require('./binformat');
const utils = require('./utils');
const sjcl = utils.sjcl;

const UInt128 = require('./uint128').UInt128;
const UInt160 = require('./uint160').UInt160;
Expand Down Expand Up @@ -56,8 +56,8 @@ function serializeHex(so, hexData, noLength) {
}

function convertHexToString(hexString) {
const bits = sjcl.codec.hex.toBits(hexString);
return sjcl.codec.utf8String.fromBits(bits);
const bits = sjclcodec.hex.toBits(hexString);
return sjclcodec.utf8String.fromBits(bits);
}

function sort_fields(keys) {
Expand Down Expand Up @@ -432,7 +432,7 @@ exports.Quality = new SerializedType({
lo = parseInt(mantissaHex.slice(-8), 16);
}

const valueBytes = sjcl.codec.bytes.fromBits([hi, lo]);
const valueBytes = sjclcodec.bytes.fromBits([hi, lo]);

so.append(valueBytes);
}
Expand Down Expand Up @@ -472,7 +472,7 @@ const STAmount = exports.Amount = new SerializedType({
valueHex = '0' + valueHex;
}

valueBytes = sjcl.codec.bytes.fromBits(sjcl.codec.hex.toBits(valueHex));
valueBytes = sjclcodec.bytes.fromBits(sjclcodec.hex.toBits(valueHex));
// Clear most significant two bits - these bits should already be 0 if
// Amount enforces the range correctly, but we'll clear them anyway just
// so this code can make certain guarantees about the encoded value.
Expand Down Expand Up @@ -506,7 +506,7 @@ const STAmount = exports.Amount = new SerializedType({
lo = parseInt(mantissaHex.slice(-8), 16);
}

valueBytes = sjcl.codec.bytes.fromBits([hi, lo]);
valueBytes = sjclcodec.bytes.fromBits([hi, lo]);
}

so.append(valueBytes);
Expand Down
6 changes: 3 additions & 3 deletions src/core/transaction.js
Expand Up @@ -4,7 +4,7 @@ const util = require('util');
const lodash = require('lodash');
const EventEmitter = require('events').EventEmitter;
const utils = require('./utils');
const sjcl = require('./utils').sjcl;
const sjclcodec = require('sjcl-codec');
const Amount = require('./amount').Amount;
const Currency = require('./amount').Currency;
const UInt160 = require('./amount').UInt160;
Expand Down Expand Up @@ -779,8 +779,8 @@ Transaction.prototype.addMemo = function(options_) {
}

function convertStringToHex(string) {
const utf8String = sjcl.codec.utf8String.toBits(string);
return sjcl.codec.hex.fromBits(utf8String).toUpperCase();
const utf8String = sjclcodec.utf8String.toBits(string);
return sjclcodec.hex.fromBits(utf8String).toUpperCase();
}

const memo = {};
Expand Down
16 changes: 3 additions & 13 deletions src/core/uint.js
Expand Up @@ -4,11 +4,10 @@

const assert = require('assert');
const lodash = require('lodash');
const sjclcodec = require('sjcl-codec');
const utils = require('./utils');
const BN = require('bn.js');

const sjcl = utils.sjcl;

//
// Abstract UInt class
//
Expand Down Expand Up @@ -217,16 +216,7 @@ UInt.prototype.parse_hex = function(j) {
};

UInt.prototype.parse_bits = function(j) {
if (sjcl.bitArray.bitLength(j) === this.constructor.width * 8) {
const bytes = sjcl.codec.bytes.fromBits(j);
this.parse_bytes(bytes);
} else {
this._value = NaN;
}

this._update();

return this;
return this.parse_bytes(sjclcodec.bytes.fromBits(j));
};

UInt.prototype.parse_bytes = function(j) {
Expand Down Expand Up @@ -280,7 +270,7 @@ UInt.prototype.to_bits = function() {
return null;
}

return sjcl.codec.bytes.toBits(this.to_bytes());
return sjclcodec.bytes.toBits(this.to_bytes());
};

exports.UInt = UInt;
Expand Down

0 comments on commit 5837aa2

Please sign in to comment.