mirror of
https://github.com/Xahau/xahau.js.git
synced 2025-11-20 12:15:51 +00:00
Fix eslint errors
This was done by:
- updating some devDependencies
- running:
node_modules/.bin/eslint -c ./eslintrc --fix src/ test/
- adding where necessary:
// eslint-disable-line strict
This commit is contained in:
@@ -1,109 +1,110 @@
|
||||
'use strict';
|
||||
const assert = require('assert');
|
||||
const brorand = require('brorand');
|
||||
const hashjs = require('hash.js');
|
||||
const elliptic = require('elliptic');
|
||||
const Ed25519 = elliptic.eddsa('ed25519');
|
||||
const Secp256k1 = elliptic.ec('secp256k1');
|
||||
const addressCodec = require('ripple-address-codec');
|
||||
const derivePrivateKey = require('./secp256k1').derivePrivateKey;
|
||||
'use strict' // eslint-disable-line strict
|
||||
|
||||
const assert = require('assert')
|
||||
const brorand = require('brorand')
|
||||
const hashjs = require('hash.js')
|
||||
const elliptic = require('elliptic')
|
||||
const Ed25519 = elliptic.eddsa('ed25519')
|
||||
const Secp256k1 = elliptic.ec('secp256k1')
|
||||
const addressCodec = require('ripple-address-codec')
|
||||
const derivePrivateKey = require('./secp256k1').derivePrivateKey
|
||||
const accountPublicFromPublicGenerator = require('./secp256k1')
|
||||
.accountPublicFromPublicGenerator;
|
||||
const utils = require('./utils');
|
||||
const hexToBytes = utils.hexToBytes;
|
||||
const bytesToHex = utils.bytesToHex;
|
||||
.accountPublicFromPublicGenerator
|
||||
const utils = require('./utils')
|
||||
const hexToBytes = utils.hexToBytes
|
||||
const bytesToHex = utils.bytesToHex
|
||||
|
||||
function generateSeed(options = {}) {
|
||||
assert(!options.entropy || options.entropy.length >= 16, 'entropy too short');
|
||||
const entropy = options.entropy ? options.entropy.slice(0, 16) : brorand(16);
|
||||
const type = options.algorithm === 'ed25519' ? 'ed25519' : 'secp256k1';
|
||||
return addressCodec.encodeSeed(entropy, type);
|
||||
assert(!options.entropy || options.entropy.length >= 16, 'entropy too short')
|
||||
const entropy = options.entropy ? options.entropy.slice(0, 16) : brorand(16)
|
||||
const type = options.algorithm === 'ed25519' ? 'ed25519' : 'secp256k1'
|
||||
return addressCodec.encodeSeed(entropy, type)
|
||||
}
|
||||
|
||||
function hash(message) {
|
||||
return hashjs.sha512().update(message).digest().slice(0, 32);
|
||||
return hashjs.sha512().update(message).digest().slice(0, 32)
|
||||
}
|
||||
|
||||
const secp256k1 = {
|
||||
deriveKeypair: function(entropy, options) {
|
||||
const prefix = '00';
|
||||
const prefix = '00'
|
||||
const privateKey = prefix + derivePrivateKey(entropy, options)
|
||||
.toString(16, 64).toUpperCase();
|
||||
.toString(16, 64).toUpperCase()
|
||||
const publicKey = bytesToHex(Secp256k1.keyFromPrivate(
|
||||
privateKey.slice(2)).getPublic().encodeCompressed());
|
||||
return {privateKey, publicKey};
|
||||
privateKey.slice(2)).getPublic().encodeCompressed())
|
||||
return {privateKey, publicKey}
|
||||
},
|
||||
sign: function(message, privateKey) {
|
||||
return bytesToHex(Secp256k1.sign(hash(message),
|
||||
hexToBytes(privateKey), {canonical: true}).toDER());
|
||||
hexToBytes(privateKey), {canonical: true}).toDER())
|
||||
},
|
||||
verify: function(message, signature, publicKey) {
|
||||
return Secp256k1.verify(hash(message), signature, hexToBytes(publicKey));
|
||||
return Secp256k1.verify(hash(message), signature, hexToBytes(publicKey))
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
const ed25519 = {
|
||||
deriveKeypair: function(entropy) {
|
||||
const prefix = 'ED';
|
||||
const rawPrivateKey = hash(entropy);
|
||||
const privateKey = prefix + bytesToHex(rawPrivateKey);
|
||||
const prefix = 'ED'
|
||||
const rawPrivateKey = hash(entropy)
|
||||
const privateKey = prefix + bytesToHex(rawPrivateKey)
|
||||
const publicKey = prefix + bytesToHex(
|
||||
Ed25519.keyFromSecret(rawPrivateKey).pubBytes());
|
||||
return {privateKey, publicKey};
|
||||
Ed25519.keyFromSecret(rawPrivateKey).pubBytes())
|
||||
return {privateKey, publicKey}
|
||||
},
|
||||
sign: function(message, privateKey) {
|
||||
// caution: Ed25519.sign interprets all strings as hex, stripping
|
||||
// any non-hex characters without warning
|
||||
assert(Array.isArray(message), 'message must be array of octets');
|
||||
assert(Array.isArray(message), 'message must be array of octets')
|
||||
return bytesToHex(Ed25519.sign(
|
||||
message, hexToBytes(privateKey).slice(1)).toBytes());
|
||||
message, hexToBytes(privateKey).slice(1)).toBytes())
|
||||
},
|
||||
verify: function(message, signature, publicKey) {
|
||||
return Ed25519.verify(message, hexToBytes(signature),
|
||||
hexToBytes(publicKey).slice(1));
|
||||
hexToBytes(publicKey).slice(1))
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function select(algorithm) {
|
||||
const methods = {'ecdsa-secp256k1': secp256k1, ed25519};
|
||||
return methods[algorithm];
|
||||
const methods = {'ecdsa-secp256k1': secp256k1, ed25519}
|
||||
return methods[algorithm]
|
||||
}
|
||||
|
||||
function deriveKeypair(seed, options) {
|
||||
const decoded = addressCodec.decodeSeed(seed);
|
||||
const algorithm = decoded.type === 'ed25519' ? 'ed25519' : 'ecdsa-secp256k1';
|
||||
return select(algorithm).deriveKeypair(decoded.bytes, options);
|
||||
const decoded = addressCodec.decodeSeed(seed)
|
||||
const algorithm = decoded.type === 'ed25519' ? 'ed25519' : 'ecdsa-secp256k1'
|
||||
return select(algorithm).deriveKeypair(decoded.bytes, options)
|
||||
}
|
||||
|
||||
function getAlgorithmFromKey(key) {
|
||||
const bytes = hexToBytes(key);
|
||||
const bytes = hexToBytes(key)
|
||||
return (bytes.length === 33 && bytes[0] === 0xED) ?
|
||||
'ed25519' : 'ecdsa-secp256k1';
|
||||
'ed25519' : 'ecdsa-secp256k1'
|
||||
}
|
||||
|
||||
function sign(messageHex, privateKey) {
|
||||
const algorithm = getAlgorithmFromKey(privateKey);
|
||||
return select(algorithm).sign(hexToBytes(messageHex), privateKey);
|
||||
const algorithm = getAlgorithmFromKey(privateKey)
|
||||
return select(algorithm).sign(hexToBytes(messageHex), privateKey)
|
||||
}
|
||||
|
||||
function verify(messageHex, signature, publicKey) {
|
||||
const algorithm = getAlgorithmFromKey(publicKey);
|
||||
return select(algorithm).verify(hexToBytes(messageHex), signature, publicKey);
|
||||
const algorithm = getAlgorithmFromKey(publicKey)
|
||||
return select(algorithm).verify(hexToBytes(messageHex), signature, publicKey)
|
||||
}
|
||||
|
||||
function deriveAddressFromBytes(publicKeyBytes) {
|
||||
return addressCodec.encodeAccountID(
|
||||
utils.computePublicKeyHash(publicKeyBytes));
|
||||
utils.computePublicKeyHash(publicKeyBytes))
|
||||
}
|
||||
|
||||
function deriveAddress(publicKey) {
|
||||
return deriveAddressFromBytes(hexToBytes(publicKey));
|
||||
return deriveAddressFromBytes(hexToBytes(publicKey))
|
||||
}
|
||||
|
||||
function deriveNodeAddress(publicKey) {
|
||||
const generatorBytes = addressCodec.decodeNodePublic(publicKey);
|
||||
const accountPublicBytes = accountPublicFromPublicGenerator(generatorBytes);
|
||||
return deriveAddressFromBytes(accountPublicBytes);
|
||||
const generatorBytes = addressCodec.decodeNodePublic(publicKey)
|
||||
const accountPublicBytes = accountPublicFromPublicGenerator(generatorBytes)
|
||||
return deriveAddressFromBytes(accountPublicBytes)
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
@@ -113,4 +114,4 @@ module.exports = {
|
||||
verify,
|
||||
deriveAddress,
|
||||
deriveNodeAddress
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,26 +1,26 @@
|
||||
'use strict';
|
||||
'use strict' // eslint-disable-line strict
|
||||
|
||||
const elliptic = require('elliptic');
|
||||
const secp256k1 = elliptic.ec('secp256k1');
|
||||
const Sha512 = require('./sha512');
|
||||
const elliptic = require('elliptic')
|
||||
const secp256k1 = elliptic.ec('secp256k1')
|
||||
const Sha512 = require('./sha512')
|
||||
|
||||
function deriveScalar(bytes, discrim) {
|
||||
const order = secp256k1.curve.n;
|
||||
const order = secp256k1.curve.n
|
||||
for (let i = 0; i <= 0xFFFFFFFF; i++) {
|
||||
// We hash the bytes to find a 256 bit number, looping until we are sure it
|
||||
// is less than the order of the curve.
|
||||
const hasher = new Sha512().add(bytes);
|
||||
const hasher = new Sha512().add(bytes)
|
||||
// If the optional discriminator index was passed in, update the hash.
|
||||
if (discrim !== undefined) {
|
||||
hasher.addU32(discrim);
|
||||
hasher.addU32(discrim)
|
||||
}
|
||||
hasher.addU32(i);
|
||||
const key = hasher.first256BN();
|
||||
hasher.addU32(i)
|
||||
const key = hasher.first256BN()
|
||||
if (key.cmpn(0) > 0 && key.cmp(order) < 0) {
|
||||
return key;
|
||||
return key
|
||||
}
|
||||
}
|
||||
throw new Error('impossible unicorn ;)');
|
||||
throw new Error('impossible unicorn ;)')
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -33,33 +33,33 @@ function deriveScalar(bytes, discrim) {
|
||||
*
|
||||
*/
|
||||
function derivePrivateKey(seed, opts = {}) {
|
||||
const root = opts.validator;
|
||||
const order = secp256k1.curve.n;
|
||||
const root = opts.validator
|
||||
const order = secp256k1.curve.n
|
||||
|
||||
// This private generator represents the `root` private key, and is what's
|
||||
// used by validators for signing when a keypair is generated from a seed.
|
||||
const privateGen = deriveScalar(seed);
|
||||
const privateGen = deriveScalar(seed)
|
||||
if (root) {
|
||||
// As returned by validation_create for a given seed
|
||||
return privateGen;
|
||||
return privateGen
|
||||
}
|
||||
const publicGen = secp256k1.g.mul(privateGen);
|
||||
const publicGen = secp256k1.g.mul(privateGen)
|
||||
// A seed can generate many keypairs as a function of the seed and a uint32.
|
||||
// Almost everyone just uses the first account, `0`.
|
||||
const accountIndex = opts.accountIndex || 0;
|
||||
const accountIndex = opts.accountIndex || 0
|
||||
return deriveScalar(publicGen.encodeCompressed(), accountIndex)
|
||||
.add(privateGen).mod(order);
|
||||
.add(privateGen).mod(order)
|
||||
}
|
||||
|
||||
function accountPublicFromPublicGenerator(publicGenBytes) {
|
||||
const rootPubPoint = secp256k1.curve.decodePoint(publicGenBytes);
|
||||
const scalar = deriveScalar(publicGenBytes, 0);
|
||||
const point = secp256k1.g.mul(scalar);
|
||||
const offset = rootPubPoint.add(point);
|
||||
return offset.encodeCompressed();
|
||||
const rootPubPoint = secp256k1.curve.decodePoint(publicGenBytes)
|
||||
const scalar = deriveScalar(publicGenBytes, 0)
|
||||
const point = secp256k1.g.mul(scalar)
|
||||
const offset = rootPubPoint.add(point)
|
||||
return offset.encodeCompressed()
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
derivePrivateKey,
|
||||
accountPublicFromPublicGenerator
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,27 +1,27 @@
|
||||
'use strict';
|
||||
'use strict' // eslint-disable-line strict
|
||||
|
||||
const hashjs = require('hash.js');
|
||||
const BigNum = require('bn.js');
|
||||
const hashjs = require('hash.js')
|
||||
const BigNum = require('bn.js')
|
||||
|
||||
module.exports = class Sha512 {
|
||||
constructor() {
|
||||
this.hash = hashjs.sha512();
|
||||
this.hash = hashjs.sha512()
|
||||
}
|
||||
add(bytes) {
|
||||
this.hash.update(bytes);
|
||||
return this;
|
||||
this.hash.update(bytes)
|
||||
return this
|
||||
}
|
||||
addU32(i) {
|
||||
return this.add([(i >>> 24) & 0xFF, (i >>> 16) & 0xFF,
|
||||
(i >>> 8) & 0xFF, i & 0xFF]);
|
||||
(i >>> 8) & 0xFF, i & 0xFF])
|
||||
}
|
||||
finish() {
|
||||
return this.hash.digest();
|
||||
return this.hash.digest()
|
||||
}
|
||||
first256() {
|
||||
return this.finish().slice(0, 32);
|
||||
return this.finish().slice(0, 32)
|
||||
}
|
||||
first256BN() {
|
||||
return new BigNum(this.first256());
|
||||
return new BigNum(this.first256())
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,28 +1,29 @@
|
||||
'use strict';
|
||||
const assert = require('assert');
|
||||
const hashjs = require('hash.js');
|
||||
const BN = require('bn.js');
|
||||
'use strict' // eslint-disable-line strict
|
||||
|
||||
const assert = require('assert')
|
||||
const hashjs = require('hash.js')
|
||||
const BN = require('bn.js')
|
||||
|
||||
function bytesToHex(a) {
|
||||
return a.map(function(byteValue) {
|
||||
const hex = byteValue.toString(16).toUpperCase();
|
||||
return hex.length > 1 ? hex : '0' + hex;
|
||||
}).join('');
|
||||
const hex = byteValue.toString(16).toUpperCase()
|
||||
return hex.length > 1 ? hex : '0' + hex
|
||||
}).join('')
|
||||
}
|
||||
|
||||
function hexToBytes(a) {
|
||||
assert(a.length % 2 === 0);
|
||||
return (new BN(a, 16)).toArray(null, a.length / 2);
|
||||
assert(a.length % 2 === 0)
|
||||
return (new BN(a, 16)).toArray(null, a.length / 2)
|
||||
}
|
||||
|
||||
function computePublicKeyHash(publicKeyBytes) {
|
||||
const hash256 = hashjs.sha256().update(publicKeyBytes).digest();
|
||||
const hash160 = hashjs.ripemd160().update(hash256).digest();
|
||||
return hash160;
|
||||
const hash256 = hashjs.sha256().update(publicKeyBytes).digest()
|
||||
const hash160 = hashjs.ripemd160().update(hash256).digest()
|
||||
return hash160
|
||||
}
|
||||
|
||||
function seedFromPhrase(phrase) {
|
||||
return hashjs.sha512().update(phrase).digest().slice(0, 16);
|
||||
return hashjs.sha512().update(phrase).digest().slice(0, 16)
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
@@ -30,4 +31,4 @@ module.exports = {
|
||||
hexToBytes,
|
||||
computePublicKeyHash,
|
||||
seedFromPhrase
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user