mirror of
https://github.com/Xahau/xahau.js.git
synced 2025-11-20 04:05:52 +00:00
Use cleaner syntax for cachedProperty
This commit is contained in:
@@ -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