mirror of
https://github.com/Xahau/xahau.js.git
synced 2025-11-20 12:15:51 +00:00
Use cleaner syntax for cachedProperty
This commit is contained in:
@@ -114,9 +114,3 @@ console.log(prettyJSON(signTxJson(seed, tx_json)));
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Coverage
|
||||
|
||||
This keeps coveralls happy (we think)
|
||||
|
||||
[](https://coveralls.io/github/sublimator/ripple-keypairs?branch=master)
|
||||
|
||||
@@ -27,7 +27,7 @@
|
||||
"babel-core": "^5.3.2",
|
||||
"babel-loader": "^5.0.0",
|
||||
"coveralls": "~2.10.0",
|
||||
"eslint": "^0.23.0",
|
||||
"eslint": "^1.0.0",
|
||||
"eventemitter2": "^0.4.14",
|
||||
"istanbul": "~0.3.5",
|
||||
"lodash": "^3.10.0",
|
||||
@@ -45,14 +45,14 @@
|
||||
"prepublish": "npm test && npm run lint && npm run compile",
|
||||
"test": "istanbul test _mocha",
|
||||
"coveralls": "cat ./coverage/lcov.info | coveralls",
|
||||
"lint": "if ! [ -f eslintrc ]; then curl -o eslintrc 'https://raw.githubusercontent.com/ripple/javascript-style-guide/es6/eslintrc'; fi; eslint --reset -c eslintrc src/*.js test/*.js"
|
||||
"lint": "if ! [ -f eslintrc ]; then curl -o eslintrc 'https://raw.githubusercontent.com/ripple/javascript-style-guide/es6/eslintrc'; fi; eslint -c eslintrc src/*.js test/*.js"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git://github.com/ripple/ripple-keypairs.git"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=0.10.0"
|
||||
"node": ">=0.12.0"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/ripple/ripple-keypairs/issues"
|
||||
@@ -60,8 +60,5 @@
|
||||
"homepage": "https://github.com/ripple/ripple-keypairs#readme",
|
||||
"author": "ndudfield@gmail.com",
|
||||
"license": "ISC",
|
||||
"readmeFilename": "README.md",
|
||||
"engines": {
|
||||
"node": ">=0.12.0"
|
||||
}
|
||||
"readmeFilename": "README.md"
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ const Ed25519 = elliptic.eddsa('ed25519');
|
||||
const {KeyPair, KeyType} = require('./keypair');
|
||||
const {
|
||||
Sha512,
|
||||
hasCachedProperty,
|
||||
cachedProperty
|
||||
} = require('./utils');
|
||||
|
||||
/*
|
||||
@@ -43,7 +43,7 @@ Ed25519Pair.fromPublic = function(publicKey) {
|
||||
return new Ed25519Pair({pubBytes: parseBytes(publicKey)});
|
||||
};
|
||||
|
||||
hasCachedProperty(Ed25519Pair, 'key', function() {
|
||||
cachedProperty(Ed25519Pair, function key() {
|
||||
if (this.seedBytes) {
|
||||
const seed256 = deriveEdKeyPairSeed(this.seedBytes);
|
||||
return Ed25519.keyFromSecret(seed256);
|
||||
@@ -51,7 +51,7 @@ hasCachedProperty(Ed25519Pair, 'key', function() {
|
||||
return Ed25519.keyFromPublic(this.pubKeyCanonicalBytes().slice(1));
|
||||
});
|
||||
|
||||
hasCachedProperty(Ed25519Pair, 'pubKeyCanonicalBytes', function() {
|
||||
cachedProperty(Ed25519Pair, function pubKeyCanonicalBytes() {
|
||||
return [0xED].concat(this.key().pubBytes());
|
||||
});
|
||||
|
||||
|
||||
@@ -2,14 +2,13 @@
|
||||
|
||||
const assert = require('assert');
|
||||
const rand = require('brorand');
|
||||
const codec = require('ripple-address-codec');
|
||||
|
||||
const {seedFromPhrase, createAccountID} = require('./utils');
|
||||
const {KeyPair, KeyType} = require('./keypair');
|
||||
const {Ed25519Pair} = require('./ed25519');
|
||||
const {K256Pair, accountPublicFromPublicGenerator} = require('./secp256k1');
|
||||
|
||||
const {decodeSeed, encodeNodePublic, decodeNodePublic, encodeAccountID} =
|
||||
require('ripple-address-codec');
|
||||
const {decodeSeed, encodeNodePublic, decodeNodePublic, encodeAccountID} = codec;
|
||||
|
||||
KeyPair.fromSeed = function(seedBytes, type, options) {
|
||||
assert(type === 'secp256k1' || type === 'ed25519');
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
const codec = require('ripple-address-codec');
|
||||
const {
|
||||
bytesToHex,
|
||||
hasCachedProperty,
|
||||
cachedProperty,
|
||||
isVirtual,
|
||||
createAccountID
|
||||
} = require('./utils');
|
||||
@@ -15,7 +15,7 @@ const KeyType = {
|
||||
|
||||
function KeyPair({seedBytes, pubBytes}) {
|
||||
this.seedBytes = seedBytes;
|
||||
this.pubKeyCanonicalBytes__ = pubBytes;
|
||||
this._pubKeyCanonicalBytes = pubBytes;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -38,19 +38,19 @@ KeyPair.prototype.verify = isVirtual;
|
||||
*/
|
||||
KeyPair.prototype.pubKeyCanonicalBytes = isVirtual;
|
||||
|
||||
hasCachedProperty(KeyPair, 'pubKeyHex', function() {
|
||||
cachedProperty(KeyPair, function pubKeyHex() {
|
||||
return bytesToHex(this.pubKeyCanonicalBytes());
|
||||
});
|
||||
|
||||
hasCachedProperty(KeyPair, 'accountBytes', function() {
|
||||
cachedProperty(KeyPair, function accountBytes() {
|
||||
return createAccountID(this.pubKeyCanonicalBytes());
|
||||
});
|
||||
|
||||
hasCachedProperty(KeyPair, 'accountID', function() {
|
||||
cachedProperty(KeyPair, function accountID() {
|
||||
return codec.encodeAccountID(this.accountBytes());
|
||||
});
|
||||
|
||||
hasCachedProperty(KeyPair, 'seed', function() {
|
||||
cachedProperty(KeyPair, function seed() {
|
||||
return codec.encodeSeed(this.seedBytes, this.type);
|
||||
});
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ const hashjs = require('hash.js');
|
||||
const {KeyPair, KeyType} = require('./keypair');
|
||||
const {
|
||||
Sha512,
|
||||
hasCachedProperty
|
||||
cachedProperty
|
||||
} = require('./utils');
|
||||
|
||||
function deriveScalar(bytes, discrim) {
|
||||
@@ -80,7 +80,7 @@ K256Pair.fromSeed = function(seedBytes, opts={}) {
|
||||
return new K256Pair({seedBytes, validator: opts.validator});
|
||||
};
|
||||
|
||||
hasCachedProperty(K256Pair, 'key', function() {
|
||||
cachedProperty(K256Pair, function key() {
|
||||
if (this.seedBytes) {
|
||||
const options = {validator: this.validator};
|
||||
return secp256k1.keyFromPrivate(deriveSecret(this.seedBytes, options));
|
||||
@@ -88,7 +88,7 @@ hasCachedProperty(K256Pair, 'key', function() {
|
||||
return secp256k1.keyFromPublic(this.pubKeyCanonicalBytes());
|
||||
});
|
||||
|
||||
hasCachedProperty(K256Pair, 'pubKeyCanonicalBytes', function() {
|
||||
cachedProperty(K256Pair, function pubKeyCanonicalBytes() {
|
||||
return this.key().getPublic().encodeCompressed();
|
||||
});
|
||||
|
||||
|
||||
@@ -7,12 +7,13 @@ function isVirtual() {
|
||||
throw new Error('virtual method not implemented ');
|
||||
}
|
||||
|
||||
function hasCachedProperty(obj, name, computer) {
|
||||
const key = name + '__';
|
||||
function cachedProperty(obj, computer) {
|
||||
const name = computer.name;
|
||||
const key = '_' + name;
|
||||
obj.prototype[name] = function() {
|
||||
let cached = this[key];
|
||||
if (cached === undefined) {
|
||||
cached = this[key] = computer.apply(this, arguments);
|
||||
cached = this[key] = computer.call(this);
|
||||
}
|
||||
return cached;
|
||||
};
|
||||
@@ -73,7 +74,7 @@ function createAccountID(pubKeyBytes) {
|
||||
module.exports = {
|
||||
arrayToHex,
|
||||
bytesToHex,
|
||||
hasCachedProperty,
|
||||
cachedProperty,
|
||||
isVirtual,
|
||||
Sha512,
|
||||
toGenericArray,
|
||||
|
||||
Reference in New Issue
Block a user