@@ -457,14 +457,14 @@ var DashKeys = ("object" === typeof module && exports) || {};
457
457
458
458
/** @type {Base58CheckVerify } */
459
459
b58c . verify = async function ( b58Addr , opts ) {
460
- let bytes = bs58 . decode ( b58Addr ) ;
460
+ let bytes = bs58 . decode ( b58Addr , opts ) ;
461
461
let hex = Utils . bytesToHex ( bytes ) ;
462
462
return await b58c . verifyHex ( hex , opts ) ;
463
463
} ;
464
464
465
465
/** @type {Base58CheckVerifyHex } */
466
466
b58c . verifyHex = async function ( base58check , opts ) {
467
- let parts = b58c . decodeHex ( base58check ) ;
467
+ let parts = b58c . decodeHex ( base58check , opts ) ;
468
468
let check = await b58c . checksum ( parts ) ;
469
469
let valid = parts . check === check ;
470
470
@@ -479,17 +479,17 @@ var DashKeys = ("object" === typeof module && exports) || {};
479
479
} ;
480
480
481
481
/** @type {Base58CheckDecode } */
482
- b58c . decode = function ( b58Addr ) {
482
+ b58c . decode = function ( b58Addr , opts ) {
483
483
let bytes = bs58 . decode ( b58Addr ) ;
484
484
let hex = Utils . bytesToHex ( bytes ) ;
485
- return b58c . decodeHex ( hex ) ;
485
+ return b58c . decodeHex ( hex , opts ) ;
486
486
} ;
487
487
488
488
/** @type {Base58CheckDecodeHex } */
489
- b58c . decodeHex = function ( addr ) {
489
+ b58c . decodeHex = function ( addr , opts ) {
490
490
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 ] ;
493
493
let isXKey = false ;
494
494
495
495
if ( ! versions . includes ( version ) ) {
@@ -516,7 +516,7 @@ var DashKeys = ("object" === typeof module && exports) || {};
516
516
let rawAddr = addr . slice ( version . length , - 8 ) ;
517
517
if ( 50 === addr . length ) {
518
518
return {
519
- version : version ,
519
+ version : opts ?. version || version ,
520
520
pubKeyHash : rawAddr ,
521
521
check : addr . slice ( - 8 ) ,
522
522
} ;
@@ -525,20 +525,20 @@ var DashKeys = ("object" === typeof module && exports) || {};
525
525
if ( isXKey ) {
526
526
if ( version === xprvVersion ) {
527
527
return {
528
- version : version ,
528
+ version : opts ?. version || version ,
529
529
xprv : rawAddr ,
530
530
check : addr . slice ( - 8 ) ,
531
531
} ;
532
532
}
533
533
return {
534
- version : version ,
534
+ version : opts ?. version || version ,
535
535
xpub : rawAddr ,
536
536
check : addr . slice ( - 8 ) ,
537
537
} ;
538
538
}
539
539
540
540
return {
541
- version : version ,
541
+ version : opts ?. version || version ,
542
542
privateKey : rawAddr . slice ( 0 , 64 ) ,
543
543
compressed : true , // "01" === rawAddr.slice(64),
544
544
check : addr . slice ( - 8 ) ,
@@ -964,22 +964,87 @@ var DashKeys = ("object" === typeof module && exports) || {};
964
964
_DashKeys . _dash58check = dash58check ;
965
965
966
966
/** @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 ) ;
969
969
let shaRipeBytes = Utils . hexToBytes ( addrParts . pubKeyHash ) ;
970
970
971
971
return shaRipeBytes ;
972
972
} ;
973
973
974
974
/** @type {DecodeBase58Check } */
975
975
_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 ) ;
977
1042
let check = await dash58check . checksum ( parts ) ;
978
1043
let valid = parts . check === check ;
979
1044
if ( ! valid ) {
980
1045
if ( false !== opts . validate ) {
981
1046
// to throw the inner error
982
- await dash58check . verify ( keyB58c ) ;
1047
+ await dash58check . verify ( keyB58c , _opts ) ;
983
1048
}
984
1049
}
985
1050
parts . valid = valid ;
@@ -1193,15 +1258,17 @@ var DashKeys = ("object" === typeof module && exports) || {};
1193
1258
} ;
1194
1259
1195
1260
/** @type {WifToAddress } */
1196
- _DashKeys . wifToAddr = async function ( wif ) {
1261
+ _DashKeys . wifToAddr = async function ( wif , opts ) {
1197
1262
let privBytes = await _DashKeys . wifToPrivKey ( wif ) ;
1263
+ let version = opts ?. version ;
1198
1264
1199
1265
let pubBytes = await Utils . toPublicKey ( privBytes ) ;
1200
1266
let pubKeyHash = await _DashKeys . pubkeyToPkh ( pubBytes ) ;
1201
1267
let pubKeyHashHex = Utils . bytesToHex ( pubKeyHash ) ;
1202
1268
1203
1269
let addr = await dash58check . encode ( {
1204
1270
pubKeyHash : pubKeyHashHex ,
1271
+ version : version ,
1205
1272
} ) ;
1206
1273
return addr ;
1207
1274
} ;
@@ -1262,6 +1329,8 @@ if ("object" === typeof module) {
1262
1329
/**
1263
1330
* @typedef DecodeOpts
1264
1331
* @prop {Boolean } validate - throw if check fails, true by default
1332
+ * @prop {Array<VERSION|Number> } [versions]
1333
+ * @prop {VERSION|Number } [version]
1265
1334
*/
1266
1335
1267
1336
/**
0 commit comments