Decouple UInt160 from account.js

This commit is contained in:
Chris Clark
2015-09-24 17:29:42 -07:00
parent 88a3f3d43b
commit b1dbdc03dd
4 changed files with 68 additions and 30 deletions

56
npm-shrinkwrap.json generated
View File

@@ -9,8 +9,8 @@
"resolved": "https://registry.npmjs.org/async/-/async-0.9.2.tgz" "resolved": "https://registry.npmjs.org/async/-/async-0.9.2.tgz"
}, },
"babel-runtime": { "babel-runtime": {
"version": "5.8.24", "version": "5.8.25",
"resolved": "https://registry.npmjs.org/babel-runtime/-/babel-runtime-5.8.24.tgz", "resolved": "https://registry.npmjs.org/babel-runtime/-/babel-runtime-5.8.25.tgz",
"dependencies": { "dependencies": {
"core-js": { "core-js": {
"version": "1.1.4", "version": "1.1.4",
@@ -117,12 +117,12 @@
"resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-2.5.2.tgz" "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-2.5.2.tgz"
}, },
"ripple-address-codec": { "ripple-address-codec": {
"version": "1.6.0", "version": "2.0.1",
"resolved": "https://registry.npmjs.org/ripple-address-codec/-/ripple-address-codec-1.6.0.tgz", "resolved": "https://registry.npmjs.org/ripple-address-codec/-/ripple-address-codec-2.0.1.tgz",
"dependencies": { "dependencies": {
"x-address-codec": { "x-address-codec": {
"version": "0.6.0", "version": "0.7.0",
"resolved": "https://registry.npmjs.org/x-address-codec/-/x-address-codec-0.6.0.tgz", "resolved": "https://registry.npmjs.org/x-address-codec/-/x-address-codec-0.7.0.tgz",
"dependencies": { "dependencies": {
"base-x": { "base-x": {
"version": "1.0.1", "version": "1.0.1",
@@ -149,6 +149,22 @@
"resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.1.tgz" "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", "version": "0.7.2",
"resolved": "https://registry.npmjs.org/ws/-/ws-0.7.2.tgz", "resolved": "https://registry.npmjs.org/ws/-/ws-0.7.2.tgz",
"dependencies": { "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": { "options": {
"version": "0.0.6", "version": "0.0.6",
"resolved": "https://registry.npmjs.org/options/-/options-0.0.6.tgz" "resolved": "https://registry.npmjs.org/options/-/options-0.0.6.tgz"
@@ -175,6 +205,20 @@
"ultron": { "ultron": {
"version": "1.0.2", "version": "1.0.2",
"resolved": "https://registry.npmjs.org/ultron/-/ultron-1.0.2.tgz" "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"
}
}
} }
} }
} }

View File

@@ -26,7 +26,7 @@
"is-my-json-valid": "^2.12.2", "is-my-json-valid": "^2.12.2",
"lodash": "^3.1.0", "lodash": "^3.1.0",
"lru-cache": "~2.5.0", "lru-cache": "~2.5.0",
"ripple-address-codec": "^1.6.0", "ripple-address-codec": "^2.0.1",
"ripple-keypairs": "^0.9.0", "ripple-keypairs": "^0.9.0",
"ripple-lib-transactionparser": "^0.5.1", "ripple-lib-transactionparser": "^0.5.1",
"ripple-lib-value": "0.1.0", "ripple-lib-value": "0.1.0",

View File

@@ -17,7 +17,7 @@ const util = require('util');
const {deriveAddress} = require('ripple-keypairs'); const {deriveAddress} = require('ripple-keypairs');
const {EventEmitter} = require('events'); const {EventEmitter} = require('events');
const {TransactionManager} = require('./transactionmanager'); const {TransactionManager} = require('./transactionmanager');
const {UInt160} = require('./uint160'); const {isValidAddress} = require('ripple-address-codec');
/** /**
* @constructor Account * @constructor Account
@@ -25,14 +25,13 @@ const {UInt160} = require('./uint160');
* @param {String} account * @param {String} account
*/ */
function Account(remote, account) { function Account(remote, address) {
EventEmitter.call(this); EventEmitter.call(this);
const self = this; const self = this;
this._remote = remote; this._remote = remote;
this._account = UInt160.from_json(account); this._address = address;
this._account_id = this._account.to_json();
this._subs = 0; this._subs = 0;
// Ledger entry object // Ledger entry object
@@ -43,7 +42,7 @@ function Account(remote, account) {
if (_.includes(Account.subscribeEvents, type)) { if (_.includes(Account.subscribeEvents, type)) {
if (!self._subs && self._remote._connected) { if (!self._subs && self._remote._connected) {
self._remote.requestSubscribe() self._remote.requestSubscribe()
.addAccount(self._account_id) .addAccount(self._address)
.broadcast().request(); .broadcast().request();
} }
self._subs += 1; self._subs += 1;
@@ -57,7 +56,7 @@ function Account(remote, account) {
self._subs -= 1; self._subs -= 1;
if (!self._subs && self._remote._connected) { if (!self._subs && self._remote._connected) {
self._remote.requestUnsubscribe() self._remote.requestUnsubscribe()
.addAccount(self._account_id) .addAccount(self._address)
.broadcast().request(); .broadcast().request();
} }
} }
@@ -66,8 +65,8 @@ function Account(remote, account) {
this.on('removeListener', listenerRemoved); this.on('removeListener', listenerRemoved);
function attachAccount(request) { function attachAccount(request) {
if (self._account.is_valid() && self._subs) { if (isValidAddress(self._address) && self._subs) {
request.addAccount(self._account_id); request.addAccount(self._address);
} }
} }
@@ -81,7 +80,7 @@ function Account(remote, account) {
let changed = false; let changed = false;
transaction.mmeta.each(function(an) { 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'); const isAccountRoot = isAccount && (an.entryType === 'AccountRoot');
if (isAccountRoot) { if (isAccountRoot) {
@@ -111,7 +110,7 @@ util.inherits(Account, EventEmitter);
Account.subscribeEvents = ['transaction', 'entry']; Account.subscribeEvents = ['transaction', 'entry'];
Account.prototype.toJson = function() { Account.prototype.toJson = function() {
return this._account.to_json(); return this._address;
}; };
/** /**
@@ -121,7 +120,7 @@ Account.prototype.toJson = function() {
*/ */
Account.prototype.isValid = 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) { 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; return this;
}; };
@@ -275,7 +274,7 @@ Account.prototype.notifyTx = function(transaction) {
return; return;
} }
const isThisAccount = (account === this._account_id); const isThisAccount = (account === this._address);
this.emit(isThisAccount ? 'transaction-outbound' : 'transaction-inbound', this.emit(isThisAccount ? 'transaction-outbound' : 'transaction-inbound',
transaction); transaction);
@@ -331,7 +330,7 @@ Account.prototype.publicKeyIsActive = function(public_key, callback) {
// Catch the case of unfunded accounts // Catch the case of unfunded accounts
if (!account_info_res) { if (!account_info_res) {
if (public_key_as_uint160 === self._account_id) { if (public_key_as_uint160 === self._address) {
async_callback(null, true); async_callback(null, true);
} else { } else {
async_callback(null, false); async_callback(null, false);
@@ -373,15 +372,10 @@ Account.prototype.publicKeyIsActive = function(public_key, callback) {
* @returns {RippleAddress} Ripple Address * @returns {RippleAddress} Ripple Address
*/ */
Account._publicKeyToAddress = function(public_key) { Account._publicKeyToAddress = function(public_key) {
// Based on functions in /src/js/ripple/keypair.js if (isValidAddress(public_key)) {
function hexToUInt160(publicKey) {
return deriveAddress(publicKey);
}
if (UInt160.is_valid(public_key)) {
return public_key; return public_key;
} else if (/^[0-9a-fA-F]+$/.test(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 } else { // eslint-disable-line no-else-return
throw new Error('Public key is invalid. Must be a UInt160 or a hex string'); throw new Error('Public key is invalid. Must be a UInt160 or a hex string');
} }

View File

@@ -21,7 +21,7 @@ function TransactionManager(account) {
const self = this; const self = this;
this._account = account; this._account = account;
this._accountID = account._account_id; this._accountID = account._address;
this._remote = account._remote; this._remote = account._remote;
this._nextSequence = undefined; this._nextSequence = undefined;
this._maxFee = this._remote.max_fee; this._maxFee = this._remote.max_fee;