Skip to content

Commit 141fdf2

Browse files
committed
fix(wifToAddr): select both private and public version bytes
1 parent 5b02656 commit 141fdf2

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

dashkeys.js

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,7 +1195,7 @@ var DashKeys = ("object" === typeof module && exports) || {};
11951195
/** @type {PrivateKeyToWif} */
11961196
_DashKeys.privKeyToWif = async function (privBytes, opts) {
11971197
let privateKey = Utils.bytesToHex(privBytes);
1198-
let version = opts?.version;
1198+
let version = opts?.version || "mainnet";
11991199

12001200
switch (version) {
12011201
case "mainnet":
@@ -1245,29 +1245,43 @@ var DashKeys = ("object" === typeof module && exports) || {};
12451245

12461246
/** @type {WifToAddress} */
12471247
_DashKeys.wifToAddr = async function (wif, opts) {
1248-
let version = opts?.version || "mainnet";
1248+
let versionCode = opts?.version || "mainnet";
1249+
/** @type {VERSION_PRIVATE} */
1250+
let privVersion = DASH_PRIV_KEY;
1251+
/** @type {VERSION} */
1252+
let pubVersion = DASH_PKH;
12491253

1250-
switch (version) {
1254+
switch (versionCode) {
1255+
case DASH_PRIV_KEY:
1256+
/* fallsthrough */
1257+
case DASH_PKH:
1258+
/* fallsthrough */
12511259
case "mainnet":
1252-
version = DASH_PRIV_KEY;
1260+
privVersion = DASH_PRIV_KEY;
1261+
pubVersion = DASH_PKH;
12531262
break;
1263+
case DASH_PKH_TESTNET:
1264+
/* fallsthrough */
1265+
case DASH_PRIV_KEY_TESTNET:
1266+
/* fallsthrough */
12541267
case "testnet":
1255-
version = DASH_PRIV_KEY_TESTNET;
1268+
privVersion = DASH_PRIV_KEY_TESTNET;
1269+
pubVersion = DASH_PKH_TESTNET;
12561270
break;
12571271
default:
1258-
let msg = `'version' must be "mainnet" or "testnet", not '${version}', or use priv => priv or pub => pub methods for more control`;
1272+
let msg = `'version' must be "mainnet" or "testnet", not '${versionCode}', or use priv => priv or pub => pub methods for more control`;
12591273
throw new Error(msg);
12601274
}
12611275

1262-
let privBytes = await _DashKeys.wifToPrivKey(wif, { version });
1276+
let privBytes = await _DashKeys.wifToPrivKey(wif, { version: privVersion });
12631277

12641278
let pubBytes = await Utils.toPublicKey(privBytes);
12651279
let pubKeyHash = await _DashKeys.pubkeyToPkh(pubBytes);
12661280
let pubKeyHashHex = Utils.bytesToHex(pubKeyHash);
12671281

12681282
let addr = await dash58check.encode({
12691283
pubKeyHash: pubKeyHashHex,
1270-
version: version,
1284+
version: pubVersion,
12711285
});
12721286
return addr;
12731287
};
@@ -1417,7 +1431,7 @@ if ("object" === typeof module) {
14171431
* (of the same coin type, of course)
14181432
* @callback WifToAddress
14191433
* @param {String} wif - private key
1420-
* @param {PrivateKeyToWifOpts} [opts]
1434+
* @param {EncodeKeyUint8ArrayOpts} [opts]
14211435
* @returns {Promise<String>} - address
14221436
*/
14231437

0 commit comments

Comments
 (0)