Update ripple-address-codec dependency

This commit is contained in:
Nicholas Dudfield
2015-07-31 16:46:55 +07:00
parent 4ff18d28d8
commit 4b36832b5a
4 changed files with 19 additions and 23 deletions

View File

@@ -19,7 +19,7 @@
"brorand": "^1.0.5", "brorand": "^1.0.5",
"elliptic": "^5.1.0", "elliptic": "^5.1.0",
"hash.js": "^1.0.3", "hash.js": "^1.0.3",
"ripple-address-codec": "^1.5.0" "ripple-address-codec": "^1.6.0"
}, },
"devDependencies": { "devDependencies": {
"assert-diff": "^1.0.1", "assert-diff": "^1.0.1",
@@ -42,7 +42,7 @@
"build": "gulp", "build": "gulp",
"compile": "babel -i runtime -d dist/npm/ src/", "compile": "babel -i runtime -d dist/npm/ src/",
"compile-with-source-maps": "babel -i runtime -s -t -d dist/npm/ src/", "compile-with-source-maps": "babel -i runtime -s -t -d dist/npm/ src/",
"prepublish": "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 --reset -c eslintrc src/*.js test/*.js"

View File

@@ -1,7 +1,5 @@
'use strict'; 'use strict';
/* -------------------------------- REQUIRES -------------------------------- */
const assert = require('assert'); const assert = require('assert');
const codec = require('ripple-address-codec'); const codec = require('ripple-address-codec');
const rand = require('brorand'); const rand = require('brorand');
@@ -11,28 +9,22 @@ const {KeyPair, KeyType} = require('./keypair');
const Ed25519Pair = require('./ed25519'); const Ed25519Pair = require('./ed25519');
const K256Pair = require('./secp256k1'); const K256Pair = require('./secp256k1');
KeyPair.fromSeed = function(seedBytes, type, options) {
const Pair = type === 'ed25519' ? Ed25519Pair : K256Pair;
return Pair.fromSeed(seedBytes, options);
};
function keyPairFromSeed(seedString, options) { function keyPairFromSeed(seedString, options) {
const decoded = codec.decodeSeed(seedString); const decoded = codec.decodeSeed(seedString);
const Pair = decoded.type === 'ed25519' ? Ed25519Pair : K256Pair; return KeyPair.fromSeed(decoded.bytes, decoded.type, options);
return Pair.fromSeed(decoded.bytes, options);
} }
function deriveWallet(type, seedBytes) { function deriveWallet(type, seedBytes) {
assert(type === 'secp256k1' || type === 'ed25519'); assert(type === 'secp256k1' || type === 'ed25519');
const pair = KeyPair.fromSeed(seedBytes, type);
let pair;
let seed;
if (type === 'secp256k1') {
seed = codec.encodeK256Seed(seedBytes);
pair = K256Pair.fromSeed(seedBytes);
} else {
seed = codec.encodeEdSeed(seedBytes);
pair = Ed25519Pair.fromSeed(seedBytes);
}
return { return {
seed, seed: pair.seed(),
accountID: pair.accountID(), accountID: pair.accountID(),
publicKey: pair.pubKeyHex() publicKey: pair.pubKeyHex()
}; };

View File

@@ -50,6 +50,10 @@ hasCachedProperty(KeyPair, 'accountID', function() {
return codec.encodeAccountID(this.accountBytes()); return codec.encodeAccountID(this.accountBytes());
}); });
hasCachedProperty(KeyPair, 'seed', function() {
return codec.encodeSeed(this.seedBytes, this.type);
});
KeyPair.prototype.signHex = function(message) { KeyPair.prototype.signHex = function(message) {
return bytesToHex(this.sign(message)); return bytesToHex(this.sign(message));
}; };

View File

@@ -10,7 +10,7 @@ const {
hasCachedProperty hasCachedProperty
} = require('./utils'); } = require('./utils');
function findk256Key(bytes, discrim) { function deriveScalar(bytes, discrim) {
const order = secp256k1.curve.n; const order = secp256k1.curve.n;
for (let i = 0; i <= 0xFFFFFFFF; i++) { for (let i = 0; i <= 0xFFFFFFFF; i++) {
// We hash the bytes to find a 256 bit number, looping until we are sure it // We hash the bytes to find a 256 bit number, looping until we are sure it
@@ -38,13 +38,13 @@ function findk256Key(bytes, discrim) {
* @return {bn.js} - 256 bit scalar value * @return {bn.js} - 256 bit scalar value
* *
*/ */
function derivek256Scalar(seed, opts={}) { function deriveSecret(seed, opts={}) {
const root = opts.validator; const root = opts.validator;
const order = secp256k1.curve.n; const order = secp256k1.curve.n;
// This private generator represents the `root` private key, and is what's // 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. // used by validators for signing when a keypair is generated from a seed.
const privateGen = findk256Key(seed); const privateGen = deriveScalar(seed);
if (root) { if (root) {
// As returned by validation_create for a given seed // As returned by validation_create for a given seed
return privateGen; return privateGen;
@@ -53,7 +53,7 @@ function derivek256Scalar(seed, opts={}) {
// A seed can generate many keypairs as a function of the seed and a uint32. // A seed can generate many keypairs as a function of the seed and a uint32.
// Almost everyone just uses the first account, `0`. // Almost everyone just uses the first account, `0`.
const accountIndex = opts.accountIndex || 0; const accountIndex = opts.accountIndex || 0;
return findk256Key(publicGen.encodeCompressed(), accountIndex) return deriveScalar(publicGen.encodeCompressed(), accountIndex)
.add(privateGen).mod(order); .add(privateGen).mod(order);
} }
@@ -75,7 +75,7 @@ K256Pair.fromSeed = function(seedBytes, opts={}) {
hasCachedProperty(K256Pair, 'key', function() { hasCachedProperty(K256Pair, 'key', function() {
if (this.seedBytes) { if (this.seedBytes) {
const options = {validator: this.validator}; const options = {validator: this.validator};
return secp256k1.keyFromPrivate(derivek256Scalar(this.seedBytes, options)); return secp256k1.keyFromPrivate(deriveSecret(this.seedBytes, options));
} }
return secp256k1.keyFromPublic(this.pubKeyCanonicalBytes()); return secp256k1.keyFromPublic(this.pubKeyCanonicalBytes());
}); });