I am trying to write a ripple wallet. I want it to be HD wallet. So I am generating 12 word mnemonics but I am not sure how to give this as a seed to ripple javascript api. See: https://ripple.com/build/rippleapi/#generateaddress
2 Answers
I'm not 100% sure how this API works, but it looks like it lets you supply the entropy. Test to see if you always get the same address and secret if you supply the same entropy. If you do, then just use the mnemonic and any HD scheme you want to derive a secret of any kind (you can use the same algorithm you'd use for bitcoin) and pass that secret as the entropy to that API call.
Two cautions with this approach:
1) You must test to make sure that API really is deterministic. Try it on a few different machines and a few different JS implementations and make sure the same entropy always leads to the same address and secret.
2) Your ability to recover keys from the mnemonic would be tied to your access to this library. If the library ever changed its key generation algorithm, you might be unable to recover the keys. So pick a version that works and don't ever change it.
Test to see if you always get the same address and secret if you supply the same entropy.
I have tested this and can confirm that you always generate the same secret and public address when you supply the same entropy.
Here's my example code for submitting the entropy to the generateAddress method.
// This functions works on modern browswers, not sure about Node.js. Try https://www.npmjs.com/package/js-sha512 or https://nodejs.org/api/crypto.html
async function fun_SHA512_array(entropy) {
'use strict';
// Turns a string into an array of integers.
// Made possible by https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest
const msgUint8 = new TextEncoder().encode(entropy); // encode as (utf-8) Uint8Array
const hashBuffer = await crypto.subtle.digest('SHA-512', msgUint8); // hash the message
const hashArray = Array.from(new Uint8Array(hashBuffer)); // convert buffer to byte array
// const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join(''); // convert bytes to hex string
return hashArray;
}
const str_entropy = "Use a cryptographically strong, randomly created string of characters here.";
// Or, your passphrase string would go here.
// Or, you could use a BIP39 Mnemonic but you'd have to remember how you constructed the string. e.g. Did you separate the words with a comma or with a space?
// Or, on the linux terminal you can use: openssl rand -base64 n
// Where n = the number of characters you wish to randomly generate. Which are then converted to base64, therefore the amount of characters ends up being more than n.
var array_sha512 = [];
fun_SHA512_array(str_entropy).then(array_sha512 => {
var obj_new_account = api.generateAddress({"entropy": array_sha512});
var str_secret = obj_new_account.secret;
var str_public_address = obj_new_account.address;
console.log(str_secret);
console.log(str_public_address);
});