From 4a93a9c1ab54cac5ca1e982a2d326b9c8e28378f Mon Sep 17 00:00:00 2001 From: AlexanderBuzz Date: Wed, 5 Oct 2022 12:56:53 +0200 Subject: [PATCH] - Removed key generation from mnemonic, as it is deprecated and will soon be removed from rippled - Added base58 seed - Updated readme accordingly --- .../_code-samples/key-derivation/js/README.md | 18 +++++----- .../_code-samples/key-derivation/js/index.js | 34 +++++++++---------- 2 files changed, 25 insertions(+), 27 deletions(-) diff --git a/content/_code-samples/key-derivation/js/README.md b/content/_code-samples/key-derivation/js/README.md index 1a4aad6b48..53a35247b0 100644 --- a/content/_code-samples/key-derivation/js/README.md +++ b/content/_code-samples/key-derivation/js/README.md @@ -7,18 +7,18 @@ have to install the necessary node.js dedpendencies: ## Command-line usage: -### Password like seed +### Base58 formatted seed: + + npm start "snJj9fYixUfpNCBn9LzLdLv5QqUKZ" + +### Hex formatted seed: + + npm start "BB664A14F510A366404BC4352A2230A5" + +### Password like seed: npm start "sEdSKaCy2JT7JaM7v95H9SxkhP9wS2r" -### Rippled RFC1751 Mnemonic - - npm start "KANT FISH GENE COST WEB JAKE CHUM HAD SOD MID KICK BOTH" - -### Hex formatted seed - - npm start "21edc3dec3ef1873cf8f333381c5f360c3" - ### Random seed npm start \ No newline at end of file diff --git a/content/_code-samples/key-derivation/js/index.js b/content/_code-samples/key-derivation/js/index.js index 7d6623e428..86233d1c88 100644 --- a/content/_code-samples/key-derivation/js/index.js +++ b/content/_code-samples/key-derivation/js/index.js @@ -1,13 +1,12 @@ 'use strict'; -//organize imports +// Organize imports const assert = require("assert") const brorand = require("brorand") const BN = require("bn.js") const elliptic = require("elliptic"); const Ed25519 = elliptic.eddsa('ed25519'); const Secp256k1 = elliptic.ec('secp256k1'); -const { rfc1751MnemonicToKey, keyToRFC1751Mnemonic } = require("xrpl/dist/npm/Wallet/rfc1751"); const hashjs = require("hash.js"); const Sha512 = require("ripple-keypairs/dist/Sha512") const { codec, encodeAccountPublic, encodeNodePublic } = require("ripple-address-codec"); @@ -49,34 +48,34 @@ class Seed { _initSeed(seedValue) { - //default to random seed + // No input given - default to random seed if (!seedValue || seedValue === '') { const randomBuffer = brorand(16) return [...randomBuffer] } - //from hex formatted seed + // From base58 formatted seed + try { + const base58decoded = codec.decodeChecked(seedValue) + if (base58decoded[0] === XRPL_SEED_PREFIX && base58decoded.length === 17) { + return [...base58decoded.slice(1)]; + } + } catch (exception) { + // Continue probing the seed for different format + } + + // From hex formatted seed if (isHex(seedValue)) { const decoded = hexToBytes(seedValue); if(decoded.length === 16) { return decoded } else { - //Raise Error + // Raise Error throw new Error("incorrect decoded seed length") } } - //from rfc1751 Mnemonic - try { - const rippledMnemonic = rfc1751MnemonicToKey(seedValue) - if (rippledMnemonic.length === 16) { - return [...rippledMnemonic] - } - } catch (error) { - //try next seed mode - } - - //from password seed + // From password seed const encoded = encodeUTF8(seedValue) return hashjs.sha512().update(encoded).digest().slice(0, 16); } @@ -131,6 +130,7 @@ class Seed { hasher.addU32(i); const key = hasher.first256BN(); /* istanbul ignore else */ + /* istanbul ignore else */ if (key.cmpn(0) > 0 && key.cmp(order) < 0) { return key; } @@ -164,7 +164,6 @@ class Seed { toString() { const base58Seed = codec.encodeChecked([XRPL_SEED_PREFIX].concat(this.bytes)) const hexSeed = Buffer.from(this.bytes).toString('hex').toUpperCase() - const rfc1751Rippled = keyToRFC1751Mnemonic(bytesToHex(this.bytes)) const base58ED25519Account = this.getBase58ED25519Account(); @@ -174,7 +173,6 @@ class Seed { const out = ` Seed (base58): ${base58Seed} Seed (hex): ${hexSeed} - Seed (rippled RFC-1751): ${rfc1751Rippled} Ed25519 Secret Key (hex): ${this.ED25519Keypair.privateKey} Ed25519 Public Key (hex): ${this.ED25519Keypair.publicKey}