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-core": "^5.3.2",
|
||||||
"babel-loader": "^5.0.0",
|
"babel-loader": "^5.0.0",
|
||||||
"coveralls": "~2.10.0",
|
"coveralls": "~2.10.0",
|
||||||
"eslint": "^0.23.0",
|
"eslint": "^1.0.0",
|
||||||
"eventemitter2": "^0.4.14",
|
"eventemitter2": "^0.4.14",
|
||||||
"istanbul": "~0.3.5",
|
"istanbul": "~0.3.5",
|
||||||
"lodash": "^3.10.0",
|
"lodash": "^3.10.0",
|
||||||
@@ -45,14 +45,14 @@
|
|||||||
"prepublish": "npm test && npm run lint && npm run compile",
|
"prepublish": "npm test && npm run lint && npm run compile",
|
||||||
"test": "istanbul test _mocha",
|
"test": "istanbul test _mocha",
|
||||||
"coveralls": "cat ./coverage/lcov.info | coveralls",
|
"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": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "git://github.com/ripple/ripple-keypairs.git"
|
"url": "git://github.com/ripple/ripple-keypairs.git"
|
||||||
},
|
},
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">=0.10.0"
|
"node": ">=0.12.0"
|
||||||
},
|
},
|
||||||
"bugs": {
|
"bugs": {
|
||||||
"url": "https://github.com/ripple/ripple-keypairs/issues"
|
"url": "https://github.com/ripple/ripple-keypairs/issues"
|
||||||
@@ -60,8 +60,5 @@
|
|||||||
"homepage": "https://github.com/ripple/ripple-keypairs#readme",
|
"homepage": "https://github.com/ripple/ripple-keypairs#readme",
|
||||||
"author": "ndudfield@gmail.com",
|
"author": "ndudfield@gmail.com",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"readmeFilename": "README.md",
|
"readmeFilename": "README.md"
|
||||||
"engines": {
|
|
||||||
"node": ">=0.12.0"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ const Ed25519 = elliptic.eddsa('ed25519');
|
|||||||
const {KeyPair, KeyType} = require('./keypair');
|
const {KeyPair, KeyType} = require('./keypair');
|
||||||
const {
|
const {
|
||||||
Sha512,
|
Sha512,
|
||||||
hasCachedProperty,
|
cachedProperty
|
||||||
} = require('./utils');
|
} = require('./utils');
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -43,7 +43,7 @@ Ed25519Pair.fromPublic = function(publicKey) {
|
|||||||
return new Ed25519Pair({pubBytes: parseBytes(publicKey)});
|
return new Ed25519Pair({pubBytes: parseBytes(publicKey)});
|
||||||
};
|
};
|
||||||
|
|
||||||
hasCachedProperty(Ed25519Pair, 'key', function() {
|
cachedProperty(Ed25519Pair, function key() {
|
||||||
if (this.seedBytes) {
|
if (this.seedBytes) {
|
||||||
const seed256 = deriveEdKeyPairSeed(this.seedBytes);
|
const seed256 = deriveEdKeyPairSeed(this.seedBytes);
|
||||||
return Ed25519.keyFromSecret(seed256);
|
return Ed25519.keyFromSecret(seed256);
|
||||||
@@ -51,7 +51,7 @@ hasCachedProperty(Ed25519Pair, 'key', function() {
|
|||||||
return Ed25519.keyFromPublic(this.pubKeyCanonicalBytes().slice(1));
|
return Ed25519.keyFromPublic(this.pubKeyCanonicalBytes().slice(1));
|
||||||
});
|
});
|
||||||
|
|
||||||
hasCachedProperty(Ed25519Pair, 'pubKeyCanonicalBytes', function() {
|
cachedProperty(Ed25519Pair, function pubKeyCanonicalBytes() {
|
||||||
return [0xED].concat(this.key().pubBytes());
|
return [0xED].concat(this.key().pubBytes());
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -2,14 +2,13 @@
|
|||||||
|
|
||||||
const assert = require('assert');
|
const assert = require('assert');
|
||||||
const rand = require('brorand');
|
const rand = require('brorand');
|
||||||
|
const codec = require('ripple-address-codec');
|
||||||
|
|
||||||
const {seedFromPhrase, createAccountID} = require('./utils');
|
const {seedFromPhrase, createAccountID} = require('./utils');
|
||||||
const {KeyPair, KeyType} = require('./keypair');
|
const {KeyPair, KeyType} = require('./keypair');
|
||||||
const {Ed25519Pair} = require('./ed25519');
|
const {Ed25519Pair} = require('./ed25519');
|
||||||
const {K256Pair, accountPublicFromPublicGenerator} = require('./secp256k1');
|
const {K256Pair, accountPublicFromPublicGenerator} = require('./secp256k1');
|
||||||
|
const {decodeSeed, encodeNodePublic, decodeNodePublic, encodeAccountID} = codec;
|
||||||
const {decodeSeed, encodeNodePublic, decodeNodePublic, encodeAccountID} =
|
|
||||||
require('ripple-address-codec');
|
|
||||||
|
|
||||||
KeyPair.fromSeed = function(seedBytes, type, options) {
|
KeyPair.fromSeed = function(seedBytes, type, options) {
|
||||||
assert(type === 'secp256k1' || type === 'ed25519');
|
assert(type === 'secp256k1' || type === 'ed25519');
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
const codec = require('ripple-address-codec');
|
const codec = require('ripple-address-codec');
|
||||||
const {
|
const {
|
||||||
bytesToHex,
|
bytesToHex,
|
||||||
hasCachedProperty,
|
cachedProperty,
|
||||||
isVirtual,
|
isVirtual,
|
||||||
createAccountID
|
createAccountID
|
||||||
} = require('./utils');
|
} = require('./utils');
|
||||||
@@ -15,7 +15,7 @@ const KeyType = {
|
|||||||
|
|
||||||
function KeyPair({seedBytes, pubBytes}) {
|
function KeyPair({seedBytes, pubBytes}) {
|
||||||
this.seedBytes = seedBytes;
|
this.seedBytes = seedBytes;
|
||||||
this.pubKeyCanonicalBytes__ = pubBytes;
|
this._pubKeyCanonicalBytes = pubBytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -38,19 +38,19 @@ KeyPair.prototype.verify = isVirtual;
|
|||||||
*/
|
*/
|
||||||
KeyPair.prototype.pubKeyCanonicalBytes = isVirtual;
|
KeyPair.prototype.pubKeyCanonicalBytes = isVirtual;
|
||||||
|
|
||||||
hasCachedProperty(KeyPair, 'pubKeyHex', function() {
|
cachedProperty(KeyPair, function pubKeyHex() {
|
||||||
return bytesToHex(this.pubKeyCanonicalBytes());
|
return bytesToHex(this.pubKeyCanonicalBytes());
|
||||||
});
|
});
|
||||||
|
|
||||||
hasCachedProperty(KeyPair, 'accountBytes', function() {
|
cachedProperty(KeyPair, function accountBytes() {
|
||||||
return createAccountID(this.pubKeyCanonicalBytes());
|
return createAccountID(this.pubKeyCanonicalBytes());
|
||||||
});
|
});
|
||||||
|
|
||||||
hasCachedProperty(KeyPair, 'accountID', function() {
|
cachedProperty(KeyPair, function accountID() {
|
||||||
return codec.encodeAccountID(this.accountBytes());
|
return codec.encodeAccountID(this.accountBytes());
|
||||||
});
|
});
|
||||||
|
|
||||||
hasCachedProperty(KeyPair, 'seed', function() {
|
cachedProperty(KeyPair, function seed() {
|
||||||
return codec.encodeSeed(this.seedBytes, this.type);
|
return codec.encodeSeed(this.seedBytes, this.type);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ const hashjs = require('hash.js');
|
|||||||
const {KeyPair, KeyType} = require('./keypair');
|
const {KeyPair, KeyType} = require('./keypair');
|
||||||
const {
|
const {
|
||||||
Sha512,
|
Sha512,
|
||||||
hasCachedProperty
|
cachedProperty
|
||||||
} = require('./utils');
|
} = require('./utils');
|
||||||
|
|
||||||
function deriveScalar(bytes, discrim) {
|
function deriveScalar(bytes, discrim) {
|
||||||
@@ -80,7 +80,7 @@ K256Pair.fromSeed = function(seedBytes, opts={}) {
|
|||||||
return new K256Pair({seedBytes, validator: opts.validator});
|
return new K256Pair({seedBytes, validator: opts.validator});
|
||||||
};
|
};
|
||||||
|
|
||||||
hasCachedProperty(K256Pair, 'key', function() {
|
cachedProperty(K256Pair, function key() {
|
||||||
if (this.seedBytes) {
|
if (this.seedBytes) {
|
||||||
const options = {validator: this.validator};
|
const options = {validator: this.validator};
|
||||||
return secp256k1.keyFromPrivate(deriveSecret(this.seedBytes, options));
|
return secp256k1.keyFromPrivate(deriveSecret(this.seedBytes, options));
|
||||||
@@ -88,7 +88,7 @@ hasCachedProperty(K256Pair, 'key', function() {
|
|||||||
return secp256k1.keyFromPublic(this.pubKeyCanonicalBytes());
|
return secp256k1.keyFromPublic(this.pubKeyCanonicalBytes());
|
||||||
});
|
});
|
||||||
|
|
||||||
hasCachedProperty(K256Pair, 'pubKeyCanonicalBytes', function() {
|
cachedProperty(K256Pair, function pubKeyCanonicalBytes() {
|
||||||
return this.key().getPublic().encodeCompressed();
|
return this.key().getPublic().encodeCompressed();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -7,12 +7,13 @@ function isVirtual() {
|
|||||||
throw new Error('virtual method not implemented ');
|
throw new Error('virtual method not implemented ');
|
||||||
}
|
}
|
||||||
|
|
||||||
function hasCachedProperty(obj, name, computer) {
|
function cachedProperty(obj, computer) {
|
||||||
const key = name + '__';
|
const name = computer.name;
|
||||||
|
const key = '_' + name;
|
||||||
obj.prototype[name] = function() {
|
obj.prototype[name] = function() {
|
||||||
let cached = this[key];
|
let cached = this[key];
|
||||||
if (cached === undefined) {
|
if (cached === undefined) {
|
||||||
cached = this[key] = computer.apply(this, arguments);
|
cached = this[key] = computer.call(this);
|
||||||
}
|
}
|
||||||
return cached;
|
return cached;
|
||||||
};
|
};
|
||||||
@@ -73,7 +74,7 @@ function createAccountID(pubKeyBytes) {
|
|||||||
module.exports = {
|
module.exports = {
|
||||||
arrayToHex,
|
arrayToHex,
|
||||||
bytesToHex,
|
bytesToHex,
|
||||||
hasCachedProperty,
|
cachedProperty,
|
||||||
isVirtual,
|
isVirtual,
|
||||||
Sha512,
|
Sha512,
|
||||||
toGenericArray,
|
toGenericArray,
|
||||||
|
|||||||
@@ -167,7 +167,7 @@ describe('generateValidatorKeys', function() {
|
|||||||
|
|
||||||
describe('K256Pair', function() {
|
describe('K256Pair', function() {
|
||||||
describe('generated tests', function() {
|
describe('generated tests', function() {
|
||||||
/*eslint-disable max-len*/
|
/* eslint-disable max-len */
|
||||||
const expected = [
|
const expected = [
|
||||||
'30440220312B2E0894B81A2E070ACE566C5DFC70CDD18E67D44E2CFEF2EB5495F7DE2DAC02205E155C0019502948C265209DFDD7D84C4A05BD2C38CEE6ECD7C33E9C9B12BEC2',
|
'30440220312B2E0894B81A2E070ACE566C5DFC70CDD18E67D44E2CFEF2EB5495F7DE2DAC02205E155C0019502948C265209DFDD7D84C4A05BD2C38CEE6ECD7C33E9C9B12BEC2',
|
||||||
'304402202A5860A12C15EBB8E91AA83F8E19D85D4AC05B272FC0C4083519339A7A76F2B802200852F9889E1284CF407DC7F73D646E62044C5AB432EAEF3FFF3F6F8EE9A0F24C',
|
'304402202A5860A12C15EBB8E91AA83F8E19D85D4AC05B272FC0C4083519339A7A76F2B802200852F9889E1284CF407DC7F73D646E62044C5AB432EAEF3FFF3F6F8EE9A0F24C',
|
||||||
@@ -175,7 +175,7 @@ describe('K256Pair', function() {
|
|||||||
'3045022100F3E541330FF79FFC42EB0491EDE1E47106D94ECFE3CDB2D9DD3BC0E8861F6D45022013F62942DD626D6C9731E317F372EC5C1F72885C4727FDBEE9D9321BC530D7B2',
|
'3045022100F3E541330FF79FFC42EB0491EDE1E47106D94ECFE3CDB2D9DD3BC0E8861F6D45022013F62942DD626D6C9731E317F372EC5C1F72885C4727FDBEE9D9321BC530D7B2',
|
||||||
'3045022100998ABE378F4119D8BEE9843482C09F0D5CE5C6012921548182454C610C57A269022036BD8EB71235C4B2C67339DE6A59746B1F7E5975987B7AB99B313D124A69BB9F'
|
'3045022100998ABE378F4119D8BEE9843482C09F0D5CE5C6012921548182454C610C57A269022036BD8EB71235C4B2C67339DE6A59746B1F7E5975987B7AB99B313D124A69BB9F'
|
||||||
];
|
];
|
||||||
/*eslint-enable max-len*/
|
/* eslint-enable max-len */
|
||||||
const key = K256Pair.fromSeed(seedFromPhrase('niq'));
|
const key = K256Pair.fromSeed(seedFromPhrase('niq'));
|
||||||
function test_factory(i) {
|
function test_factory(i) {
|
||||||
it('can deterministically sign/verify message [' + i + ']', function() {
|
it('can deterministically sign/verify message [' + i + ']', function() {
|
||||||
|
|||||||
Reference in New Issue
Block a user