Skip to content

Commit 9ccf559

Browse files
committed
fix: pass version opts down to Base58Check
1 parent 9082c57 commit 9ccf559

File tree

2 files changed

+97
-16
lines changed

2 files changed

+97
-16
lines changed

base58check.types.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,15 +125,25 @@ module.exports._types = true;
125125
/**
126126
* @callback Decode
127127
* @param {String} base58check - WIF, Payment Address, xPrv, or xPub
128+
* @param {Object} opts
129+
* @param {[String, String]} opts.versions
130+
* @param {[String, String]} opts.xversions
128131
* @returns {Parts}
129132
*/
130133

131134
/**
132135
* @callback DecodeHex
133136
* @param {String} hex - magic version bytes + data + checksum
137+
* @param {DecodeOpts} [opts]
134138
* @returns {Parts}
135139
*/
136140

141+
/**
142+
* @typedef DecodeOpts
143+
* @prop {[String, String]} [versions]
144+
* @prop {[String, String]} [xversions]
145+
*/
146+
137147
/**
138148
* @callback Verify
139149
* @param {String} base58check - WIF, Payment Address, xPrv, or xPub
@@ -153,6 +163,8 @@ module.exports._types = true;
153163
/**
154164
* @typedef VerifyOpts
155165
* @prop {Boolean} [verify] - set 'false' to set 'valid' false rather than throw
166+
* @param {[String, String]} [versions]
167+
* @param {[String, String]} [xversions]
156168
*/
157169

158170
/**

dashkeys.js

Lines changed: 85 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -457,14 +457,14 @@ var DashKeys = ("object" === typeof module && exports) || {};
457457

458458
/** @type {Base58CheckVerify} */
459459
b58c.verify = async function (b58Addr, opts) {
460-
let bytes = bs58.decode(b58Addr);
460+
let bytes = bs58.decode(b58Addr, opts);
461461
let hex = Utils.bytesToHex(bytes);
462462
return await b58c.verifyHex(hex, opts);
463463
};
464464

465465
/** @type {Base58CheckVerifyHex} */
466466
b58c.verifyHex = async function (base58check, opts) {
467-
let parts = b58c.decodeHex(base58check);
467+
let parts = b58c.decodeHex(base58check, opts);
468468
let check = await b58c.checksum(parts);
469469
let valid = parts.check === check;
470470

@@ -479,17 +479,17 @@ var DashKeys = ("object" === typeof module && exports) || {};
479479
};
480480

481481
/** @type {Base58CheckDecode} */
482-
b58c.decode = function (b58Addr) {
482+
b58c.decode = function (b58Addr, opts) {
483483
let bytes = bs58.decode(b58Addr);
484484
let hex = Utils.bytesToHex(bytes);
485-
return b58c.decodeHex(hex);
485+
return b58c.decodeHex(hex, opts);
486486
};
487487

488488
/** @type {Base58CheckDecodeHex} */
489-
b58c.decodeHex = function (addr) {
489+
b58c.decodeHex = function (addr, opts) {
490490
let version = addr.slice(0, privateKeyVersion.length);
491-
let versions = [pubKeyHashVersion, privateKeyVersion];
492-
let xversions = [xpubVersion, xprvVersion];
491+
let versions = opts?.versions || [pubKeyHashVersion, privateKeyVersion];
492+
let xversions = opts?.xversions || [xpubVersion, xprvVersion];
493493
let isXKey = false;
494494

495495
if (!versions.includes(version)) {
@@ -516,7 +516,7 @@ var DashKeys = ("object" === typeof module && exports) || {};
516516
let rawAddr = addr.slice(version.length, -8);
517517
if (50 === addr.length) {
518518
return {
519-
version: version,
519+
version: opts?.version || version,
520520
pubKeyHash: rawAddr,
521521
check: addr.slice(-8),
522522
};
@@ -525,20 +525,20 @@ var DashKeys = ("object" === typeof module && exports) || {};
525525
if (isXKey) {
526526
if (version === xprvVersion) {
527527
return {
528-
version: version,
528+
version: opts?.version || version,
529529
xprv: rawAddr,
530530
check: addr.slice(-8),
531531
};
532532
}
533533
return {
534-
version: version,
534+
version: opts?.version || version,
535535
xpub: rawAddr,
536536
check: addr.slice(-8),
537537
};
538538
}
539539

540540
return {
541-
version: version,
541+
version: opts?.version || version,
542542
privateKey: rawAddr.slice(0, 64),
543543
compressed: true, // "01" === rawAddr.slice(64),
544544
check: addr.slice(-8),
@@ -964,22 +964,87 @@ var DashKeys = ("object" === typeof module && exports) || {};
964964
_DashKeys._dash58check = dash58check;
965965

966966
/** @type {AddressToPubKeyHash} */
967-
_DashKeys.addrToPkh = async function (address) {
968-
let addrParts = await _DashKeys.decode(address);
967+
_DashKeys.addrToPkh = async function (address, opts) {
968+
let addrParts = await _DashKeys.decode(address, opts);
969969
let shaRipeBytes = Utils.hexToBytes(addrParts.pubKeyHash);
970970

971971
return shaRipeBytes;
972972
};
973973

974974
/** @type {DecodeBase58Check} */
975975
_DashKeys.decode = async function (keyB58c, opts) {
976-
let parts = await dash58check.decode(keyB58c);
976+
console.log("################ found it!");
977+
978+
let _opts = {};
979+
if (opts?.version) {
980+
switch (opts.version) {
981+
case XPRV:
982+
/* fallsthrough */
983+
case "xprv":
984+
/* fallsthrough */
985+
case 0x0488ade4:
986+
/* fallsthrough */
987+
case XPUB:
988+
/* fallsthrough */
989+
case "xpub":
990+
/* fallsthrough */
991+
case 0x0488b21e:
992+
/* fallsthrough */
993+
case DASH_PRIV_KEY:
994+
/* fallsthrough */
995+
case 0xcc:
996+
/* fallsthrough */
997+
case DASH_PKH:
998+
/* fallsthrough */
999+
case 0x4c:
1000+
/* fallsthrough */
1001+
case "mainnet":
1002+
Object.assign(_opts, {
1003+
versions: [DASH_PKH, DASH_PRIV_KEY],
1004+
xversions: [XPRV, XPUB],
1005+
});
1006+
break;
1007+
1008+
case TPRV:
1009+
/* fallsthrough */
1010+
case "tprv":
1011+
/* fallsthrough */
1012+
case 0x04358394:
1013+
/* fallsthrough */
1014+
case TPUB:
1015+
/* fallsthrough */
1016+
case "tpub":
1017+
/* fallsthrough */
1018+
case 0x043587cf:
1019+
/* fallsthrough */
1020+
case DASH_PRIV_KEY_TESTNET:
1021+
/* fallsthrough */
1022+
case 0xef:
1023+
/* fallsthrough */
1024+
case DASH_PKH_TESTNET:
1025+
/* fallsthrough */
1026+
case 0x8c:
1027+
/* fallsthrough */
1028+
case "testnet":
1029+
Object.assign(_opts, {
1030+
versions: [DASH_PKH_TESTNET, DASH_PRIV_KEY_TESTNET],
1031+
xversions: [TPRV, TPUB],
1032+
});
1033+
break;
1034+
default:
1035+
throw new Error(`unknown version ${opts.version}`);
1036+
}
1037+
}
1038+
if (opts.versions) {
1039+
Object.assign(_opts, opts);
1040+
}
1041+
let parts = await dash58check.decode(keyB58c, _opts);
9771042
let check = await dash58check.checksum(parts);
9781043
let valid = parts.check === check;
9791044
if (!valid) {
9801045
if (false !== opts.validate) {
9811046
// to throw the inner error
982-
await dash58check.verify(keyB58c);
1047+
await dash58check.verify(keyB58c, _opts);
9831048
}
9841049
}
9851050
parts.valid = valid;
@@ -1193,15 +1258,17 @@ var DashKeys = ("object" === typeof module && exports) || {};
11931258
};
11941259

11951260
/** @type {WifToAddress} */
1196-
_DashKeys.wifToAddr = async function (wif) {
1261+
_DashKeys.wifToAddr = async function (wif, opts) {
11971262
let privBytes = await _DashKeys.wifToPrivKey(wif);
1263+
let version = opts?.version;
11981264

11991265
let pubBytes = await Utils.toPublicKey(privBytes);
12001266
let pubKeyHash = await _DashKeys.pubkeyToPkh(pubBytes);
12011267
let pubKeyHashHex = Utils.bytesToHex(pubKeyHash);
12021268

12031269
let addr = await dash58check.encode({
12041270
pubKeyHash: pubKeyHashHex,
1271+
version: version,
12051272
});
12061273
return addr;
12071274
};
@@ -1262,6 +1329,8 @@ if ("object" === typeof module) {
12621329
/**
12631330
* @typedef DecodeOpts
12641331
* @prop {Boolean} validate - throw if check fails, true by default
1332+
* @prop {Array<VERSION|Number>} [versions]
1333+
* @prop {VERSION|Number} [version]
12651334
*/
12661335

12671336
/**

0 commit comments

Comments
 (0)