diff --git a/src/core/operations/SeedToMPK.mjs b/src/core/operations/SeedToMPK.mjs index fc7aafb0..23f6f878 100644 --- a/src/core/operations/SeedToMPK.mjs +++ b/src/core/operations/SeedToMPK.mjs @@ -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); diff --git a/tests/operations/tests/SeedToMPK.mjs b/tests/operations/tests/SeedToMPK.mjs index 00e3e586..9bf84c36 100644 --- a/tests/operations/tests/SeedToMPK.mjs +++ b/tests/operations/tests/SeedToMPK.mjs @@ -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"] + } + + ] } ]);