Updated SeedToMPK to not require seed length.

This commit is contained in:
David C Goldenberg 2025-06-13 11:07:00 -04:00
parent 2233127f43
commit a060ec446e
2 changed files with 49 additions and 5 deletions

View file

@ -48,16 +48,17 @@ class SeedToMPK extends Operation {
return "";
}
input = input.trim();
// We check to see if the input is hex or not.
// If it is not, we convert it back to hex
const re = /[0-9A-Fa-f]{2,}/g;
if (!(input.length === 128 && re.test(input)) && !(input.length === 64)) {
return "Must pass a hex string of length 128, or a byte string of length 64. Got length: " + input.length;
}
const re = /^[0-9A-Fa-f]{2,}$/g;
const isHex = re.test(input) && input.length %2 === 0;
// Create the hmac.
const hmac = forge.hmac.create();
hmac.start("sha512", Utils.convertToByteString("Bitcoin seed", "UTF8"));
if (input.length === 128) {
if (isHex) {
hmac.update(Utils.convertToByteString(input, "hex"));
} else {
hmac.update(input);

View file

@ -57,6 +57,49 @@ TestRegister.addTests([
"args": ["Ltpv"]
}
],
},
{
name: "Seed To Master Private Key (XPrv, short seed)",
input: "1ed5b6f0dcf88085add90fbb138d5e16f661d5b738f842232fce5980fc4592",
expectedOutput: "xprv9s21ZrQH143K3mvkwf3aiiCQ6mpTnAh5ZPvhHFLXSaSRxAg5uAuHVvaFVSGoyE7U3UJ3knZM12AvNcF9a9xoARWCkCF9MYXjyN1ZJUC6ssa",
recipeConfig: [
{
"op": "Seed To Master Key",
"args": ["xprv"]
}
],
},
{
name: "Seed to Master Private Key (XPrv, decoded Chinese)",
input: "霍 里 攻 繁 混 注 杀 侦 具 涤 涤 手",
expectedOutput: "xprv9s21ZrQH143K3NGAQKJovUKk9ty8HFDxoK2A8kseKUEQnQA2xuS1UiXj67d7jPS153aBSeqbJpGC4etDSmAJrD2MuN3pncdjFNh85PEPwS2",
recipeConfig: [
{
"op": "Normalise Unicode",
"args": ["NFKD"]
},
{
"op": "To Hex",
"args": ["None", 0]
},
{
"op": "Seed To Master Key",
"args": ["xprv"]
}
]
},
{
name: "Seed to Master Private Key (XPrv, Non-Hex Dealing With Raw Bytes)",
input: "abc=_*~$%^$^*&^$%@#$^^%&*^&(YIU%^$#@",
expectedOutput: "xprv9s21ZrQH143K2zVX3HhaQJZPeunqrxitrCQh5ZaUzyv9uHtZu86AVigehGy2ZCfGcAmp7wJUSb4GMGsbH8qqJawZEz2mdUVV8q7VH2AqgH6",
recipeConfig: [
{
"op": "Seed To Master Key",
"args": ["xprv"]
}
]
}
]);