mirror of
https://github.com/Xahau/xahau.js.git
synced 2025-11-20 12:15:51 +00:00
Allow KeyPair.fromSeed/walletFromSeed to accept arrays for seed
This commit is contained in:
@@ -10,18 +10,26 @@ const {Ed25519Pair} = require('./ed25519');
|
||||
const {K256Pair, accountPublicFromPublicGenerator} = require('./secp256k1');
|
||||
const {decodeSeed, encodeNodePublic, decodeNodePublic, encodeAccountID} = codec;
|
||||
|
||||
KeyPair.fromSeed = function(seedBytes, type, options) {
|
||||
assert(type === 'secp256k1' || type === 'ed25519');
|
||||
const Pair = type === 'ed25519' ? Ed25519Pair : K256Pair;
|
||||
return Pair.fromSeed(seedBytes, options);
|
||||
};
|
||||
|
||||
function keyPairFromSeed(seedString, options) {
|
||||
const decoded = decodeSeed(seedString);
|
||||
return KeyPair.fromSeed(decoded.bytes, decoded.type, options);
|
||||
function parseSeed(seed, type=KeyType.secp256k1) {
|
||||
if (typeof seed !== 'string') {
|
||||
return {bytes: seed, type};
|
||||
}
|
||||
return decodeSeed(seed);
|
||||
}
|
||||
|
||||
function deriveWallet(type, seedBytes) {
|
||||
KeyPair.fromSeed = function(seed, type=KeyType.secp256k1, options) {
|
||||
if (typeof seed === 'string') {
|
||||
const decoded = decodeSeed(seed);
|
||||
const optionsArg = type;
|
||||
return this.fromSeed(decoded.bytes, decoded.type, optionsArg);
|
||||
}
|
||||
|
||||
assert(type === KeyType.secp256k1 || type === KeyType.ed25519);
|
||||
const Pair = type === 'ed25519' ? Ed25519Pair : K256Pair;
|
||||
return Pair.fromSeed(seed, options);
|
||||
};
|
||||
|
||||
function deriveWallet(seedBytes, type) {
|
||||
const pair = KeyPair.fromSeed(seedBytes, type);
|
||||
|
||||
return {
|
||||
@@ -31,21 +39,6 @@ function deriveWallet(type, seedBytes) {
|
||||
};
|
||||
}
|
||||
|
||||
function generateWallet(opts={}) {
|
||||
const {type='secp256k1', randGen=rand} = opts;
|
||||
const seedBytes = randGen(16);
|
||||
return deriveWallet(type, seedBytes);
|
||||
}
|
||||
|
||||
function walletFromSeed(seed) {
|
||||
const {type, bytes} = decodeSeed(seed);
|
||||
return deriveWallet(type, bytes);
|
||||
}
|
||||
|
||||
function walletFromPhrase(phrase, type='secp256k1') {
|
||||
return deriveWallet(type, seedFromPhrase(phrase));
|
||||
}
|
||||
|
||||
function deriveValidator(seedBytes) {
|
||||
const pair = K256Pair.fromSeed(seedBytes, {validator: true});
|
||||
return {
|
||||
@@ -54,6 +47,21 @@ function deriveValidator(seedBytes) {
|
||||
};
|
||||
}
|
||||
|
||||
function generateWallet(opts={}) {
|
||||
const {type='secp256k1', randGen=rand} = opts;
|
||||
const seedBytes = randGen(16);
|
||||
return deriveWallet(seedBytes, type);
|
||||
}
|
||||
|
||||
function walletFromSeed(seed, seedType) {
|
||||
const {type, bytes} = parseSeed(seed, seedType);
|
||||
return deriveWallet(bytes, type);
|
||||
}
|
||||
|
||||
function walletFromPhrase(phrase, type) {
|
||||
return walletFromSeed(seedFromPhrase(phrase), type);
|
||||
}
|
||||
|
||||
function generateValidatorKeys(opts={}) {
|
||||
const {randGen=rand} = opts;
|
||||
return deriveValidator(randGen(16));
|
||||
@@ -65,8 +73,8 @@ function nodePublicAccountID(publicKey) {
|
||||
return encodeAccountID(createAccountID(accountPublicBytes));
|
||||
}
|
||||
|
||||
function validatorKeysFromSeed(seed) {
|
||||
const {type, bytes} = decodeSeed(seed);
|
||||
function validatorKeysFromSeed(seed, seedType) {
|
||||
const {type, bytes} = parseSeed(seed, seedType);
|
||||
assert(type === KeyType.secp256k1);
|
||||
return deriveValidator(bytes);
|
||||
}
|
||||
@@ -75,6 +83,10 @@ function validatorKeysFromPhrase(phrase) {
|
||||
return deriveValidator(seedFromPhrase(phrase));
|
||||
}
|
||||
|
||||
function keyPairFromSeed(seedString, options) {
|
||||
return KeyPair.fromSeed(seedString, options);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
KeyPair,
|
||||
K256Pair,
|
||||
|
||||
@@ -3,13 +3,13 @@
|
||||
const hashjs = require('hash.js');
|
||||
const Sha512 = require('./sha512');
|
||||
|
||||
function isVirtual(target, name, descriptor) {
|
||||
function isVirtual(_, __, descriptor) {
|
||||
descriptor.value = function() {
|
||||
throw new Error('virtual method not implemented ');
|
||||
};
|
||||
}
|
||||
|
||||
function cached(target, name, descriptor) {
|
||||
function cached(_, name, descriptor) {
|
||||
const computer = descriptor.value;
|
||||
const key = '_' + name;
|
||||
descriptor.value = function() {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
'use strict';
|
||||
|
||||
const codec = require('ripple-address-codec');
|
||||
const assert = require('assert-diff');
|
||||
const utils = require('../src/utils');
|
||||
const keypairs = require('../src');
|
||||
@@ -100,6 +101,13 @@ describe('keyPairFromSeed', function() {
|
||||
const pair = keyPairFromSeed('sn259rEFXrQrWyx3Q7XneWcwV6dfL');
|
||||
assert(pair instanceof K256Pair);
|
||||
});
|
||||
it('can be intantiated with an array of bytes', function() {
|
||||
const seed = 'sn259rEFXrQrWyx3Q7XneWcwV6dfL';
|
||||
const {bytes} = codec.decodeSeed(seed);
|
||||
const pair = keyPairFromSeed(bytes);
|
||||
assert(pair instanceof K256Pair);
|
||||
assert.equal(pair.seed(), seed);
|
||||
});
|
||||
});
|
||||
|
||||
describe('walletFromPhrase', function() {
|
||||
|
||||
Reference in New Issue
Block a user