Skip to content

Commit

Permalink
ref: update types and fix checker issues
Browse files Browse the repository at this point in the history
  • Loading branch information
coolaj86 committed Apr 27, 2024
1 parent 3adbdc3 commit eeef44c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
19 changes: 8 additions & 11 deletions base58check.types.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,13 @@ module.exports._types = true;
/**
* @callback Checksum
* @param {Parts|EncodeParts} parts - private key or public hash or xkey parts
* @returns {String} - 8 hex chars (4 bytes) of checksum
* @returns {Promise<String>} - 8 hex chars (4 bytes) of checksum
*/

/**
* @callback Decode
* @param {String} base58check - WIF, Payment Address, xPrv, or xPub
* @param {Object} opts
* @param {[String, String]} opts.versions
* @param {[String, String]} opts.xversions
* @param {DecodeOpts} opts
* @returns {Parts}
*/

Expand All @@ -148,33 +146,32 @@ module.exports._types = true;
* @callback Verify
* @param {String} base58check - WIF, Payment Address, xPrv, or xPub
* @param {VerifyOpts} [opts]
* @returns {Parts}
* @returns {Promise<Parts>}
* @throws {Error}
*/

/**
* @callback VerifyHex
* @param {String} hex - magic version bytes + data + checksum
* @param {VerifyOpts} [opts]
* @returns {Parts}
* @returns {Promise<Parts>}
* @throws {Error}
*/

/**
* @typedef VerifyOpts
* @typedef {DecodeOpts & VerifyOptsPartial} VerifyOpts
* @typedef VerifyOptsPartial
* @prop {Boolean} [verify] - set 'false' to set 'valid' false rather than throw
* @param {[String, String]} [versions]
* @param {[String, String]} [xversions]
*/

/**
* @callback Encode
* @param {EncodeParts} parts
* @returns {String} - base58check WIF, Payment Address, xPrv, or xPub
* @returns {Promise<String>} - base58check WIF, Payment Address, xPrv, or xPub
*/

/**
* @callback EncodeHex
* @param {EncodeParts} parts
* @returns {String} - hex magic version bytes + key + checksum
* @returns {Promise<String>} - hex magic version bytes + key + checksum
*/
20 changes: 16 additions & 4 deletions dashkeys.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
/** @typedef {import('./base-x.types.js').DecodeUnsafe} BaseXDecodeUnsafe */
/** @typedef {import('./base-x.types.js').Encode} BaseXEncode */
/** @typedef {import('./base58check.types.js').Base58Check} Base58Check */
/** @typedef {import('./base58check.types.js').base58Check} Base58CheckInstance */
/** @typedef {import('./base58check.types.js').Checksum} Base58CheckChecksum */
/** @typedef {import('./base58check.types.js').Create} Base58CheckCreate */
/** @typedef {import('./base58check.types.js').Decode} Base58CheckDecode */
Expand Down Expand Up @@ -37,6 +38,8 @@
* @prop {WifToAddress} wifToAddr
* @prop {WifToPrivateKey} wifToPrivKey
* @prop {DashKeysUtils} utils
* @prop {EncodeKeyUint8Array} _encodeXKey
* @prop {Base58CheckInstance} _dash58check
*/

/**
Expand Down Expand Up @@ -456,7 +459,7 @@ var DashKeys = ("object" === typeof module && exports) || {};

/** @type {Base58CheckVerify} */
b58c.verify = async function (b58Addr, opts) {
let bytes = bs58.decode(b58Addr, opts);
let bytes = bs58.decode(b58Addr);
let hex = Utils.bytesToHex(bytes);
return await b58c.verifyHex(hex, opts);
};
Expand Down Expand Up @@ -964,6 +967,8 @@ var DashKeys = ("object" === typeof module && exports) || {};

/** @type {AddressToPubKeyHash} */
_DashKeys.addrToPkh = async function (address, opts) {
/** @type {Base58CheckPubKeyHashParts} */
//@ts-ignore - address has pkh parts
let addrParts = await _DashKeys.decode(address, opts);
let shaRipeBytes = Utils.hexToBytes(addrParts.pubKeyHash);

Expand All @@ -972,6 +977,7 @@ var DashKeys = ("object" === typeof module && exports) || {};

/** @type {DecodeBase58Check} */
_DashKeys.decode = async function (keyB58c, opts) {
/* jshint maxcomplexity:35 */
let _opts = {};
if (opts?.version) {
switch (opts.version) {
Expand Down Expand Up @@ -1039,7 +1045,7 @@ var DashKeys = ("object" === typeof module && exports) || {};
let check = await dash58check.checksum(parts);
let valid = parts.check === check;
if (!valid) {
if (false !== opts.validate) {
if (false !== opts?.validate) {
// to throw the inner error
await dash58check.verify(keyB58c, _opts);
}
Expand Down Expand Up @@ -1220,7 +1226,11 @@ var DashKeys = ("object" === typeof module && exports) || {};
/** @type {PublicKeyToAddress} */
_DashKeys.pubkeyToAddr = async function (pubBytes, opts) {
let shaRipeBytes = await _DashKeys.pubkeyToPkh(pubBytes);
let addr = await _DashKeys.pkhToAddr(shaRipeBytes, opts);
let addr = await _DashKeys.pkhToAddr(
shaRipeBytes,
//@ts-ignore - has version property
opts,
);

return addr;
};
Expand Down Expand Up @@ -1276,6 +1286,7 @@ var DashKeys = ("object" === typeof module && exports) || {};
/**
* @callback PrivateKeyToWif
* @param {Uint8Array} privBytes
* @param {EncodeKeyUint8ArrayOpts} [opts]
* @returns {Promise<String>} - wif
*/

Expand Down Expand Up @@ -1305,13 +1316,14 @@ if ("object" === typeof module) {
/**
* @callback AddressToPubKeyHash
* @param {String} addr - Base58Check encoded version + pkh + check
* @param {DecodeOpts} [opts]
* @returns {Promise<Uint8Array>} - pkh bytes (no version or check, NOT Base58Check)
*/

/**
* @callback DecodeBase58Check
* @param {String} keyB58c - addr, wif, or xkey (xprv, xpub)
* @param {DecodeOpts} opts
* @param {DecodeOpts} [opts]
* @returns {Promise<Base58CheckParts>}
*/

Expand Down

0 comments on commit eeef44c

Please sign in to comment.