From b1dbdc03dd27528ef462fa4177b9728d4e313f9f Mon Sep 17 00:00:00 2001 From: Chris Clark Date: Thu, 24 Sep 2015 17:29:42 -0700 Subject: [PATCH] Decouple UInt160 from account.js --- npm-shrinkwrap.json | 56 ++++++++++++++++++++++++++++++---- package.json | 2 +- src/core/account.js | 38 ++++++++++------------- src/core/transactionmanager.js | 2 +- 4 files changed, 68 insertions(+), 30 deletions(-) diff --git a/npm-shrinkwrap.json b/npm-shrinkwrap.json index 0f307cd2..d3e28395 100644 --- a/npm-shrinkwrap.json +++ b/npm-shrinkwrap.json @@ -9,8 +9,8 @@ "resolved": "https://registry.npmjs.org/async/-/async-0.9.2.tgz" }, "babel-runtime": { - "version": "5.8.24", - "resolved": "https://registry.npmjs.org/babel-runtime/-/babel-runtime-5.8.24.tgz", + "version": "5.8.25", + "resolved": "https://registry.npmjs.org/babel-runtime/-/babel-runtime-5.8.25.tgz", "dependencies": { "core-js": { "version": "1.1.4", @@ -117,12 +117,12 @@ "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-2.5.2.tgz" }, "ripple-address-codec": { - "version": "1.6.0", - "resolved": "https://registry.npmjs.org/ripple-address-codec/-/ripple-address-codec-1.6.0.tgz", + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/ripple-address-codec/-/ripple-address-codec-2.0.1.tgz", "dependencies": { "x-address-codec": { - "version": "0.6.0", - "resolved": "https://registry.npmjs.org/x-address-codec/-/x-address-codec-0.6.0.tgz", + "version": "0.7.0", + "resolved": "https://registry.npmjs.org/x-address-codec/-/x-address-codec-0.7.0.tgz", "dependencies": { "base-x": { "version": "1.0.1", @@ -149,6 +149,22 @@ "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.1.tgz" } } + }, + "ripple-address-codec": { + "version": "1.6.0", + "resolved": "https://registry.npmjs.org/ripple-address-codec/-/ripple-address-codec-1.6.0.tgz", + "dependencies": { + "x-address-codec": { + "version": "0.6.0", + "resolved": "https://registry.npmjs.org/x-address-codec/-/x-address-codec-0.6.0.tgz", + "dependencies": { + "base-x": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/base-x/-/base-x-1.0.1.tgz" + } + } + } + } } } }, @@ -168,6 +184,20 @@ "version": "0.7.2", "resolved": "https://registry.npmjs.org/ws/-/ws-0.7.2.tgz", "dependencies": { + "bufferutil": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/bufferutil/-/bufferutil-1.1.0.tgz", + "dependencies": { + "bindings": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/bindings/-/bindings-1.2.1.tgz" + }, + "nan": { + "version": "1.8.4", + "resolved": "https://registry.npmjs.org/nan/-/nan-1.8.4.tgz" + } + } + }, "options": { "version": "0.0.6", "resolved": "https://registry.npmjs.org/options/-/options-0.0.6.tgz" @@ -175,6 +205,20 @@ "ultron": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/ultron/-/ultron-1.0.2.tgz" + }, + "utf-8-validate": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/utf-8-validate/-/utf-8-validate-1.1.0.tgz", + "dependencies": { + "bindings": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/bindings/-/bindings-1.2.1.tgz" + }, + "nan": { + "version": "1.8.4", + "resolved": "https://registry.npmjs.org/nan/-/nan-1.8.4.tgz" + } + } } } } diff --git a/package.json b/package.json index d422f3cc..d1046bd7 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,7 @@ "is-my-json-valid": "^2.12.2", "lodash": "^3.1.0", "lru-cache": "~2.5.0", - "ripple-address-codec": "^1.6.0", + "ripple-address-codec": "^2.0.1", "ripple-keypairs": "^0.9.0", "ripple-lib-transactionparser": "^0.5.1", "ripple-lib-value": "0.1.0", diff --git a/src/core/account.js b/src/core/account.js index 3e3267bf..a7340c1e 100644 --- a/src/core/account.js +++ b/src/core/account.js @@ -17,7 +17,7 @@ const util = require('util'); const {deriveAddress} = require('ripple-keypairs'); const {EventEmitter} = require('events'); const {TransactionManager} = require('./transactionmanager'); -const {UInt160} = require('./uint160'); +const {isValidAddress} = require('ripple-address-codec'); /** * @constructor Account @@ -25,14 +25,13 @@ const {UInt160} = require('./uint160'); * @param {String} account */ -function Account(remote, account) { +function Account(remote, address) { EventEmitter.call(this); const self = this; this._remote = remote; - this._account = UInt160.from_json(account); - this._account_id = this._account.to_json(); + this._address = address; this._subs = 0; // Ledger entry object @@ -43,7 +42,7 @@ function Account(remote, account) { if (_.includes(Account.subscribeEvents, type)) { if (!self._subs && self._remote._connected) { self._remote.requestSubscribe() - .addAccount(self._account_id) + .addAccount(self._address) .broadcast().request(); } self._subs += 1; @@ -57,7 +56,7 @@ function Account(remote, account) { self._subs -= 1; if (!self._subs && self._remote._connected) { self._remote.requestUnsubscribe() - .addAccount(self._account_id) + .addAccount(self._address) .broadcast().request(); } } @@ -66,8 +65,8 @@ function Account(remote, account) { this.on('removeListener', listenerRemoved); function attachAccount(request) { - if (self._account.is_valid() && self._subs) { - request.addAccount(self._account_id); + if (isValidAddress(self._address) && self._subs) { + request.addAccount(self._address); } } @@ -81,7 +80,7 @@ function Account(remote, account) { let changed = false; transaction.mmeta.each(function(an) { - const isAccount = an.fields.Account === self._account_id; + const isAccount = an.fields.Account === self._address; const isAccountRoot = isAccount && (an.entryType === 'AccountRoot'); if (isAccountRoot) { @@ -111,7 +110,7 @@ util.inherits(Account, EventEmitter); Account.subscribeEvents = ['transaction', 'entry']; Account.prototype.toJson = function() { - return this._account.to_json(); + return this._address; }; /** @@ -121,7 +120,7 @@ Account.prototype.toJson = function() { */ Account.prototype.isValid = function() { - return this._account.is_valid(); + return isValidAddress(this._address); }; /** @@ -131,7 +130,7 @@ Account.prototype.isValid = function() { */ Account.prototype.getInfo = function(callback) { - return this._remote.requestAccountInfo({account: this._account_id}, callback); + return this._remote.requestAccountInfo({account: this._address}, callback); }; /** @@ -210,7 +209,7 @@ Account.prototype.lines = function(callback_) { } } - this._remote.requestAccountLines({account: this._account_id}, accountLines); + this._remote.requestAccountLines({account: this._address}, accountLines); return this; }; @@ -275,7 +274,7 @@ Account.prototype.notifyTx = function(transaction) { return; } - const isThisAccount = (account === this._account_id); + const isThisAccount = (account === this._address); this.emit(isThisAccount ? 'transaction-outbound' : 'transaction-inbound', transaction); @@ -331,7 +330,7 @@ Account.prototype.publicKeyIsActive = function(public_key, callback) { // Catch the case of unfunded accounts if (!account_info_res) { - if (public_key_as_uint160 === self._account_id) { + if (public_key_as_uint160 === self._address) { async_callback(null, true); } else { async_callback(null, false); @@ -373,15 +372,10 @@ Account.prototype.publicKeyIsActive = function(public_key, callback) { * @returns {RippleAddress} Ripple Address */ Account._publicKeyToAddress = function(public_key) { - // Based on functions in /src/js/ripple/keypair.js - function hexToUInt160(publicKey) { - return deriveAddress(publicKey); - } - - if (UInt160.is_valid(public_key)) { + if (isValidAddress(public_key)) { return public_key; } else if (/^[0-9a-fA-F]+$/.test(public_key)) { - return hexToUInt160(public_key); + return deriveAddress(public_key); } else { // eslint-disable-line no-else-return throw new Error('Public key is invalid. Must be a UInt160 or a hex string'); } diff --git a/src/core/transactionmanager.js b/src/core/transactionmanager.js index 3406e30f..fb5c241d 100644 --- a/src/core/transactionmanager.js +++ b/src/core/transactionmanager.js @@ -21,7 +21,7 @@ function TransactionManager(account) { const self = this; this._account = account; - this._accountID = account._account_id; + this._accountID = account._address; this._remote = account._remote; this._nextSequence = undefined; this._maxFee = this._remote.max_fee;