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:
parent
804b799de3
commit
cf0733dc6f
@ -28,7 +28,7 @@ function validateLengths(input, allowableLengths) {
|
|||||||
* Returns true if input is a valid hex string, false otherwise.
|
* Returns true if input is a valid hex string, false otherwise.
|
||||||
* @param {*} input
|
* @param {*} input
|
||||||
*/
|
*/
|
||||||
function isHex(input) {
|
export function isHex(input) {
|
||||||
const re = /^[0-9A-Fa-f]{2,}$/g;
|
const re = /^[0-9A-Fa-f]{2,}$/g;
|
||||||
return re.test(input) && input.length %2 === 0;
|
return re.test(input) && input.length %2 === 0;
|
||||||
}
|
}
|
||||||
@ -467,6 +467,12 @@ export function getExtendedKeyString(input) {
|
|||||||
return versionString[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.
|
* We serialize the extended key based off of the passed in data.
|
||||||
|
|||||||
@ -7,7 +7,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import Operation from "../Operation.mjs";
|
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",
|
"name": "Version Type",
|
||||||
"type": "option",
|
"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 = [
|
this.checks = [
|
||||||
|
|||||||
@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
import Operation from "../Operation.mjs";
|
import Operation from "../Operation.mjs";
|
||||||
import OperationError from "../errors/OperationError.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",
|
"name": "Version Type",
|
||||||
"type": "option",
|
"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.
|
/* Example arguments. See the project wiki for full details.
|
||||||
{
|
{
|
||||||
@ -60,6 +61,10 @@ class KeyToExtendedKey extends Operation {
|
|||||||
*/
|
*/
|
||||||
run(input, args) {
|
run(input, args) {
|
||||||
// const [firstArg, secondArg] = args;
|
// const [firstArg, secondArg] = args;
|
||||||
|
if (input.trim().length === 0) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
input = input.trim();
|
||||||
|
|
||||||
const inputAsHex = makeSureIsHex(input);
|
const inputAsHex = makeSureIsHex(input);
|
||||||
const isPublic = inputAsHex.length === 66 && (inputAsHex.startsWith("03") || inputAsHex.startsWith("02"));
|
const isPublic = inputAsHex.length === 66 && (inputAsHex.startsWith("03") || inputAsHex.startsWith("02"));
|
||||||
|
|||||||
@ -7,7 +7,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import Operation from "../Operation.mjs";
|
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 forge from "node-forge";
|
||||||
import Utils from "../Utils.mjs";
|
import Utils from "../Utils.mjs";
|
||||||
|
|
||||||
@ -33,7 +33,8 @@ class SeedToMPK extends Operation {
|
|||||||
{
|
{
|
||||||
"name": "Version Type",
|
"name": "Version Type",
|
||||||
"type": "option",
|
"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();
|
input = input.trim();
|
||||||
|
|
||||||
|
|
||||||
// We check to see if the input is hex or not.
|
const isItHex = isHex(input);
|
||||||
// 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;
|
|
||||||
|
|
||||||
// Create the hmac.
|
// Create the hmac.
|
||||||
const hmac = forge.hmac.create();
|
const hmac = forge.hmac.create();
|
||||||
hmac.start("sha512", Utils.convertToByteString("Bitcoin seed", "UTF8"));
|
hmac.start("sha512", Utils.convertToByteString("Bitcoin seed", "UTF8"));
|
||||||
if (isHex) {
|
if (isItHex) {
|
||||||
hmac.update(Utils.convertToByteString(input, "hex"));
|
hmac.update(Utils.convertToByteString(input, "hex"));
|
||||||
} else {
|
} else {
|
||||||
hmac.update(input);
|
hmac.update(input);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user