Use cleaner syntax for cachedProperty

This commit is contained in:
Nicholas Dudfield
2015-08-04 09:04:46 +07:00
parent 5daeecfb0e
commit 1cad004359
8 changed files with 25 additions and 34 deletions

View File

@@ -114,9 +114,3 @@ console.log(prettyJSON(signTxJson(seed, tx_json)));
}
}
```
### Coverage
This keeps coveralls happy (we think)
[![Coverage Status](https://coveralls.io/repos/sublimator/ripple-keypairs/badge.svg?branch=master&service=github)](https://coveralls.io/github/sublimator/ripple-keypairs?branch=master)

View File

@@ -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"
}

View File

@@ -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());
});

View File

@@ -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');

View File

@@ -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);
});

View File

@@ -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();
});

View File

@@ -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,

View File

@@ -167,7 +167,7 @@ describe('generateValidatorKeys', function() {
describe('K256Pair', function() {
describe('generated tests', function() {
/*eslint-disable max-len*/
/* eslint-disable max-len */
const expected = [
'30440220312B2E0894B81A2E070ACE566C5DFC70CDD18E67D44E2CFEF2EB5495F7DE2DAC02205E155C0019502948C265209DFDD7D84C4A05BD2C38CEE6ECD7C33E9C9B12BEC2',
'304402202A5860A12C15EBB8E91AA83F8E19D85D4AC05B272FC0C4083519339A7A76F2B802200852F9889E1284CF407DC7F73D646E62044C5AB432EAEF3FFF3F6F8EE9A0F24C',
@@ -175,7 +175,7 @@ describe('K256Pair', function() {
'3045022100F3E541330FF79FFC42EB0491EDE1E47106D94ECFE3CDB2D9DD3BC0E8861F6D45022013F62942DD626D6C9731E317F372EC5C1F72885C4727FDBEE9D9321BC530D7B2',
'3045022100998ABE378F4119D8BEE9843482C09F0D5CE5C6012921548182454C610C57A269022036BD8EB71235C4B2C67339DE6A59746B1F7E5975987B7AB99B313D124A69BB9F'
];
/*eslint-enable max-len*/
/* eslint-enable max-len */
const key = K256Pair.fromSeed(seedFromPhrase('niq'));
function test_factory(i) {
it('can deterministically sign/verify message [' + i + ']', function() {