Update after QA. Added a function in Bitcoin.mjs to return valid extended key versions, which is now used in SeedToMPK, KeyToExtendedKey and ChangeExtendedKeyVersion. This will make sure only valid version options are given. Also added some input sanitization code to deal with blank input.

This commit is contained in:
David C Goldenberg 2025-06-19 09:41:52 -04:00
parent 804b799de3
commit cf0733dc6f
4 changed files with 22 additions and 13 deletions

View file

@ -28,7 +28,7 @@ function validateLengths(input, allowableLengths) {
* Returns true if input is a valid hex string, false otherwise.
* @param {*} input
*/
function isHex(input) {
export function isHex(input) {
const re = /^[0-9A-Fa-f]{2,}$/g;
return re.test(input) && input.length %2 === 0;
}
@ -467,6 +467,12 @@ export function getExtendedKeyString(input) {
return versionString[input];
}
/**
* Returns valid versions as an array.
*/
export function getVersions() {
return Object.keys(versionBytes);
}
/**
* We serialize the extended key based off of the passed in data.

View file

@ -7,7 +7,7 @@
*/
import Operation from "../Operation.mjs";
import { deserializeExtendedKeyFunc, serializeExtendedKeyFunc, getExtendedKeyVersion } from "../lib/Bitcoin.mjs";
import { deserializeExtendedKeyFunc, serializeExtendedKeyFunc, getExtendedKeyVersion, getVersions } from "../lib/Bitcoin.mjs";
/**
@ -31,7 +31,8 @@ class ChangeExtendedKeyVersion extends Operation {
{
"name": "Version Type",
"type": "option",
"value": ["xpub", "xprv", "ypub", "yprv", "zpub", "zprv", "Zpub", "Zprv", "Ypub", "Yprv", "Ltub", "Ltpv", "Mtub", "Mtpv", "ttub", "ttpv", "tpub", "tprv", "upub", "uprv", "vpub", "vprv", "Upub", "Uprv", "Vpub", "Vprv"]
"value": getVersions()
// "value": ["xpub", "xprv", "ypub", "yprv", "zpub", "zprv", "Zpub", "Zprv", "Ypub", "Yprv", "Ltub", "Ltpv", "Mtub", "Mtpv", "ttub", "ttpv", "tpub", "tprv", "upub", "uprv", "vpub", "vprv", "Upub", "Uprv", "Vpub", "Vprv"]
}
];
this.checks = [

View file

@ -6,7 +6,7 @@
import Operation from "../Operation.mjs";
import OperationError from "../errors/OperationError.mjs";
import { makeSureIsHex, serializeExtendedKeyFunc, getExtendedKeyVersion } from "../lib/Bitcoin.mjs";
import { makeSureIsHex, serializeExtendedKeyFunc, getExtendedKeyVersion, getVersions } from "../lib/Bitcoin.mjs";
/**
@ -36,7 +36,8 @@ class KeyToExtendedKey extends Operation {
{
"name": "Version Type",
"type": "option",
"value": ["xpub", "xprv", "ypub", "yprv", "zpub", "zprv", "Zpub", "Zprv", "Ypub", "Yprv", "Ltub", "Ltpv", "Mtub", "Mtpv", "ttub", "ttpv", "tpub", "tprv", "upub", "uprv", "vpub", "vprv", "Upub", "Uprv", "Vpub", "Vprv"]
"value": getVersions()
// "value": ["xpub", "xprv", "ypub", "yprv", "zpub", "zprv", "Zpub", "Zprv", "Ypub", "Yprv", "Ltub", "Ltpv", "Mtub", "Mtpv", "ttub", "ttpv", "tpub", "tprv", "upub", "uprv", "vpub", "vprv", "Upub", "Uprv", "Vpub", "Vprv"]
}
/* Example arguments. See the project wiki for full details.
{
@ -60,6 +61,10 @@ class KeyToExtendedKey extends Operation {
*/
run(input, args) {
// const [firstArg, secondArg] = args;
if (input.trim().length === 0) {
return "";
}
input = input.trim();
const inputAsHex = makeSureIsHex(input);
const isPublic = inputAsHex.length === 66 && (inputAsHex.startsWith("03") || inputAsHex.startsWith("02"));

View file

@ -7,7 +7,7 @@
*/
import Operation from "../Operation.mjs";
import { serializeExtendedKeyFunc, getExtendedKeyVersion } from "../lib/Bitcoin.mjs";
import { serializeExtendedKeyFunc, getExtendedKeyVersion, getVersions, isHex } from "../lib/Bitcoin.mjs";
import forge from "node-forge";
import Utils from "../Utils.mjs";
@ -33,7 +33,8 @@ class SeedToMPK extends Operation {
{
"name": "Version Type",
"type": "option",
"value": ["xprv", "yprv", "zprv", "Zprv", "Yprv", "Ltpv", "Mtpv", "ttpv", "tprv", "uprv", "vprv", "Uprv", "Vprv"]
"value": getVersions()
// "value": ["xprv", "yprv", "zprv", "Zprv", "Yprv", "Ltpv", "Mtpv", "ttpv", "tprv", "uprv", "vprv", "Uprv", "Vprv"]
}
];
}
@ -50,15 +51,11 @@ class SeedToMPK extends Operation {
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;
const isHex = re.test(input) && input.length %2 === 0;
const isItHex = isHex(input);
// Create the hmac.
const hmac = forge.hmac.create();
hmac.start("sha512", Utils.convertToByteString("Bitcoin seed", "UTF8"));
if (isHex) {
if (isItHex) {
hmac.update(Utils.convertToByteString(input, "hex"));
} else {
hmac.update(input);