Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@secux/utility

Package Overview
Dependencies
Maintainers
1
Versions
27
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@secux/utility - npm Package Compare versions

Comparing version 3.0.15 to 3.0.16

2

lib/utility.js

@@ -18,2 +18,2 @@ "use strict";

limitations under the License.
*/var _FirmwareError_type,_FirmwareError_current,_FirmwareError_restrict,__classPrivateFieldSet=this&&this.__classPrivateFieldSet||function(receiver,state,value,kind,f){if("m"===kind)throw new TypeError("Private method is not writable");if("a"===kind&&!f)throw new TypeError("Private accessor was defined without a setter");if("function"==typeof state?receiver!==state||!f:!state.has(receiver))throw new TypeError("Cannot write private member to an object whose class did not declare it");return"a"===kind?f.call(receiver,value):f?f.value=value:state.set(receiver,value),value},__classPrivateFieldGet=this&&this.__classPrivateFieldGet||function(receiver,state,kind,f){if("a"===kind&&!f)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof state?receiver!==state||!f:!state.has(receiver))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===kind?f:"a"===kind?f.call(receiver):f?f.value:state.get(receiver)};Object.defineProperty(exports,"__esModule",{value:!0}),exports.checkFWVersion=exports.FirmwareError=exports.FirmwareType=exports.loadPlugin=exports.isSupportedCoin=exports.supported_coin=exports.BigIntToBuffer=exports.ow_checkBufferLength=exports.ow_accountPath=exports.ow_strictPath=exports.ow_base58String=exports.ow_xpubString=exports.ow_numberString=exports.ow_hashString=exports.ow_prefixedhexString=exports.ow_hexString=exports.ow_bip32String=exports.owTool=exports.Logger=exports.Signature=exports.toExtenededPublicKey=exports.splitPath=exports.decodePathBuffer=exports.buildPathBuffer=void 0;const bignumber_js_1=require("bignumber.js"),ow_1=require("ow"),BIP32Path_1=require("./BIP32Path"),bs58_1=require("./bs58");var BIP32Path_2=require("./BIP32Path");Object.defineProperty(exports,"buildPathBuffer",{enumerable:!0,get:function(){return BIP32Path_2.buildPathBuffer}}),Object.defineProperty(exports,"decodePathBuffer",{enumerable:!0,get:function(){return BIP32Path_2.decodePathBuffer}}),Object.defineProperty(exports,"splitPath",{enumerable:!0,get:function(){return BIP32Path_2.splitPath}});var xpub_1=require("./xpub");Object.defineProperty(exports,"toExtenededPublicKey",{enumerable:!0,get:function(){return xpub_1.toExtenededPublicKey}});var signature_1=require("./signature");Object.defineProperty(exports,"Signature",{enumerable:!0,get:function(){return signature_1.Signature}}),exports.Logger=process.env.SECUX_LOGGER;const logger=null===exports.Logger||void 0===exports.Logger?void 0:exports.Logger.child({id:"utility"});var FirmwareType;exports.owTool=class{static get bip32String(){return ow_1.default.string.matches(/^m(\/\d+')+(\/\d+)*$/)}static get hexString(){return ow_1.default.string.matches(/^[0-9A-F-a-f]+$/)}static get prefixedhexString(){return ow_1.default.string.matches(/^0x[0-9A-F-a-f]+$/)}static get hashString(){return ow_1.default.string.matches(/^[0-9A-Z-a-z]+$/)}static get numberString(){return ow_1.default.string.matches(/^[0-9]+$/)}static get xpubString(){return ow_1.default.string.matches(/^[1-9A-NP-Za-km-z]{111}$/)}static get base58String(){return ow_1.default.string.is((x=>!!bs58_1.Base58.decodeUnsafe(x)))}static get base64String(){return ow_1.default.string.nonEmpty.matches(/^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)?$/)}},exports.ow_bip32String=ow_1.default.string.matches(/^m(\/\d+')+(\/\d+)*$/),exports.ow_hexString=ow_1.default.string.matches(/^[0-9A-F-a-f]+$/),exports.ow_prefixedhexString=ow_1.default.string.matches(/^0x[0-9A-F-a-f]+$/),exports.ow_hashString=ow_1.default.string.matches(/^[0-9A-Z-a-z]+$/),exports.ow_numberString=ow_1.default.string.matches(/^[0-9]+$/),exports.ow_xpubString=ow_1.default.string.matches(/^[1-9A-NP-Za-km-z]{111}$/),exports.ow_base58String=ow_1.default.string.is((x=>!!bs58_1.Base58.decodeUnsafe(x))),exports.ow_strictPath=function(coinType,purpose){const coins=numbersToRegExp("number"==typeof coinType?[coinType]:coinType);if(purpose){const purposes=numbersToRegExp("number"==typeof purpose?[purpose]:purpose);return ow_1.default.string.matches(new RegExp(`^m/${purposes}'/${coins}'/[0-9]+'/[0-9]+/[0-9]+$`))}return ow_1.default.string.matches(new RegExp(`^m/[0-9]+'/${coins}'/[0-9]+'/[0-9]+/[0-9]+$`))},exports.ow_accountPath=function(coinType,purpose){const coins=numbersToRegExp("number"==typeof coinType?[coinType]:coinType);if(purpose){const purposes=numbersToRegExp("number"==typeof purpose?[purpose]:purpose);return ow_1.default.string.matches(new RegExp(`^m/${purposes}'/${coins}'/[0-9]+'`))}return ow_1.default.string.matches(new RegExp(`^m/[0-9]+'/${coins}'/[0-9]+'`))},exports.ow_checkBufferLength=function(length){return ow_1.default.buffer.validate((value=>({validator:value.length===length,message:label=>`ArgumentError: Expected length of ${label} is ${length}, got ${value.length}`})))},exports.BigIntToBuffer=function(value,bufferlen,isLE=!0){let hex="";"number"==typeof value?hex=value.toString(16):"string"==typeof value&&(hex=new bignumber_js_1.BigNumber(value).toString(16)),hex.length%2!=0&&(hex="0"+hex);const tmp=Buffer.from(hex,"hex");if(tmp.length>bufferlen)throw Error("value is too large");const buf=Buffer.concat([Buffer.alloc(bufferlen-tmp.length),tmp]);return isLE?buf.reverse():buf},exports.supported_coin=Object.freeze([{purpose:[44,49,84,86],cointype:0,name:"bitcoin mainnet",module:"SecuxBTC",npm:"@secux/app-btc"},{purpose:[44,49,84,86],cointype:1,name:"bitcoin testnet",module:"SecuxBTC",npm:"@secux/app-btc"},{purpose:[44,49],cointype:2,name:"litecoin",module:"SecuxBTC",npm:"@secux/app-btc"},{purpose:[44],cointype:3,name:"dogecoin",module:"SecuxBTC",npm:"@secux/app-btc"},{purpose:[44],cointype:5,name:"dash",module:"SecuxBTC",npm:"@secux/app-btc"},{purpose:[44,49],cointype:17,name:"groestl",module:"SecuxBTC",npm:"@secux/app-btc"},{purpose:[44,49],cointype:20,name:"digibyte",module:"SecuxBTC",npm:"@secux/app-btc"},{purpose:[44],cointype:60,name:"ethereum",module:"SecuxETH",npm:"@secux/app-eth"},{purpose:[44],cointype:144,name:"ripple",module:"SecuxXRP",npm:"@secux/app-xrp"},{purpose:[44,49],cointype:145,name:"bitcoincash",module:"SecuxBTC",npm:"@secux/app-btc"},{purpose:[44],cointype:148,name:"stellar",module:"SecuxXLM",npm:"@secux/app-xlm"},{purpose:[44],cointype:195,name:"tron",module:"SecuxTRX",npm:"@secux/app-trx"},{purpose:[44],cointype:714,name:"binance",module:"SecuxBNB",npm:"@secux/app-bnb"},{purpose:[1852],cointype:1815,name:"cardano",module:"SecuxADA",npm:"@secux/app-ada"},{purpose:[44],cointype:501,name:"solana",module:"SecuxSOL",npm:"@secux/app-sol"},{purpose:[44],cointype:330,name:"terra",module:"SecuxLUNA",npm:"@secux/app-luna"},{purpose:[44],cointype:235,name:"fioprotocol",module:"SecuxFIO",npm:"@secux/app-fio"}]),exports.isSupportedCoin=function(path){const bip32=(0,BIP32Path_1.splitPath)(path);for(const def of exports.supported_coin)if(def.purpose.includes(bip32.purpose.value)&&def.cointype===bip32.coinType.value)return!0;return!1},exports.loadPlugin=function(plugin,name){try{const{ITransport}=require("@secux/transport");void 0===ITransport[name]&&Object.defineProperty(ITransport,name,{enumerable:!0,configurable:!1,writable:!1,value:plugin})}catch(error){null==logger||logger.debug("The package @secux/transport is not at environment, plugin loading skipped")}},function(FirmwareType){FirmwareType.mcu="mcu",FirmwareType.se="se"}(FirmwareType=exports.FirmwareType||(exports.FirmwareType={}));class FirmwareError extends Error{constructor(type,restrict,current){super(),_FirmwareError_type.set(this,void 0),_FirmwareError_current.set(this,void 0),_FirmwareError_restrict.set(this,void 0),__classPrivateFieldSet(this,_FirmwareError_type,FirmwareType[type],"f"),__classPrivateFieldSet(this,_FirmwareError_restrict,restrict,"f"),__classPrivateFieldSet(this,_FirmwareError_current,current,"f"),this.message=`${__classPrivateFieldGet(this,_FirmwareError_type,"f")} firmware need update, version "${restrict}" needed, but got "${current}"`}get type(){return __classPrivateFieldGet(this,_FirmwareError_type,"f")}get currentVersion(){return __classPrivateFieldGet(this,_FirmwareError_current,"f")}get restrictVersion(){return __classPrivateFieldGet(this,_FirmwareError_restrict,"f")}}function numbersToRegExp(list){let a="";return list.map((c=>a+=`${c.toString()}|`)),a=`(${a.substring(0,a.length-1)})`,a}exports.FirmwareError=FirmwareError,_FirmwareError_type=new WeakMap,_FirmwareError_current=new WeakMap,_FirmwareError_restrict=new WeakMap,exports.checkFWVersion=function(type,restrict,current){if(!restrict||!current)return;const r=restrict.split(".").map((x=>parseInt(x))),c=current.split(".").map((x=>parseInt(x)));for(let i=0;i<r.length-1;i++){const _r=r[i],_c=c[i];if(_r<_c)return;if(_r>_c)throw new FirmwareError(type,restrict,current)}const last=r.length-1,minor=c[last]+(current.includes(".D.")?1:0);if(r[last]>minor)throw new FirmwareError(type,restrict,current)},process.env.SECUX_LOGGER||Object.defineProperty(process.env,"SECUX_LOGGER",{enumerable:!1,configurable:!1,writable:!1,value:void 0});
*/var _FirmwareError_type,_FirmwareError_current,_FirmwareError_restrict,__classPrivateFieldSet=this&&this.__classPrivateFieldSet||function(receiver,state,value,kind,f){if("m"===kind)throw new TypeError("Private method is not writable");if("a"===kind&&!f)throw new TypeError("Private accessor was defined without a setter");if("function"==typeof state?receiver!==state||!f:!state.has(receiver))throw new TypeError("Cannot write private member to an object whose class did not declare it");return"a"===kind?f.call(receiver,value):f?f.value=value:state.set(receiver,value),value},__classPrivateFieldGet=this&&this.__classPrivateFieldGet||function(receiver,state,kind,f){if("a"===kind&&!f)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof state?receiver!==state||!f:!state.has(receiver))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===kind?f:"a"===kind?f.call(receiver):f?f.value:state.get(receiver)};Object.defineProperty(exports,"__esModule",{value:!0}),exports.checkFWVersion=exports.FirmwareError=exports.FirmwareType=exports.loadPlugin=exports.isSupportedCoin=exports.supported_coin=exports.BigIntToBuffer=exports.ow_checkBufferLength=exports.ow_accountPath=exports.ow_strictPath=exports.ow_base58String=exports.ow_xpubString=exports.ow_numberString=exports.ow_hashString=exports.ow_prefixedhexString=exports.ow_hexString=exports.ow_bip32String=exports.owTool=exports.Logger=exports.Signature=exports.toExtenededPublicKey=exports.splitPath=exports.decodePathBuffer=exports.buildPathBuffer=void 0;const bignumber_js_1=require("bignumber.js"),ow_1=require("ow"),BIP32Path_1=require("./BIP32Path"),bs58_1=require("./bs58");var BIP32Path_2=require("./BIP32Path");Object.defineProperty(exports,"buildPathBuffer",{enumerable:!0,get:function(){return BIP32Path_2.buildPathBuffer}}),Object.defineProperty(exports,"decodePathBuffer",{enumerable:!0,get:function(){return BIP32Path_2.decodePathBuffer}}),Object.defineProperty(exports,"splitPath",{enumerable:!0,get:function(){return BIP32Path_2.splitPath}});var xpub_1=require("./xpub");Object.defineProperty(exports,"toExtenededPublicKey",{enumerable:!0,get:function(){return xpub_1.toExtenededPublicKey}});var signature_1=require("./signature");Object.defineProperty(exports,"Signature",{enumerable:!0,get:function(){return signature_1.Signature}}),exports.Logger=process.env.SECUX_LOGGER;const logger=null===exports.Logger||void 0===exports.Logger?void 0:exports.Logger.child({id:"utility"});var FirmwareType;exports.owTool=class{static get bip32String(){return ow_1.default.string.matches(/^m(\/\d+')+(\/\d+)*$/)}static get hexString(){return ow_1.default.string.matches(/^[0-9A-F-a-f]+$/)}static get prefixedhexString(){return ow_1.default.string.matches(/^0x[0-9A-F-a-f]+$/)}static get hashString(){return ow_1.default.string.matches(/^[0-9A-Z-a-z]+$/)}static get numberString(){return ow_1.default.string.matches(/^[0-9]+$/)}static get xpubString(){return ow_1.default.string.matches(/^[1-9A-NP-Za-km-z]{111}$/)}static get base58String(){return ow_1.default.string.is((x=>!!bs58_1.Base58.decodeUnsafe(x)))}static get base64String(){return ow_1.default.string.nonEmpty.matches(/^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)?$/)}},exports.ow_bip32String=ow_1.default.string.matches(/^m(\/\d+')+(\/\d+)*$/),exports.ow_hexString=ow_1.default.string.matches(/^[0-9A-F-a-f]+$/),exports.ow_prefixedhexString=ow_1.default.string.matches(/^0x[0-9A-F-a-f]+$/),exports.ow_hashString=ow_1.default.string.matches(/^[0-9A-Z-a-z]+$/),exports.ow_numberString=ow_1.default.string.matches(/^[0-9]+$/),exports.ow_xpubString=ow_1.default.string.matches(/^[1-9A-NP-Za-km-z]{111}$/),exports.ow_base58String=ow_1.default.string.is((x=>!!bs58_1.Base58.decodeUnsafe(x))),exports.ow_strictPath=function(coinType,purpose){const coins=numbersToRegExp("number"==typeof coinType?[coinType]:coinType);if(purpose){const purposes=numbersToRegExp("number"==typeof purpose?[purpose]:purpose);return ow_1.default.string.matches(new RegExp(`^m/${purposes}'/${coins}'/[0-9]+'/[0-9]+/[0-9]+$`))}return ow_1.default.string.matches(new RegExp(`^m/[0-9]+'/${coins}'/[0-9]+'/[0-9]+/[0-9]+$`))},exports.ow_accountPath=function(coinType,purpose){const coins=numbersToRegExp("number"==typeof coinType?[coinType]:coinType);if(purpose){const purposes=numbersToRegExp("number"==typeof purpose?[purpose]:purpose);return ow_1.default.string.matches(new RegExp(`^m/${purposes}'/${coins}'/[0-9]+'`))}return ow_1.default.string.matches(new RegExp(`^m/[0-9]+'/${coins}'/[0-9]+'`))},exports.ow_checkBufferLength=function(length){return ow_1.default.buffer.validate((value=>({validator:value.length===length,message:label=>`ArgumentError: Expected length of ${label} is ${length}, got ${value.length}`})))},exports.BigIntToBuffer=function(value,bufferlen,isLE=!0){let hex="";"number"==typeof value?hex=value.toString(16):"string"==typeof value&&(hex=new bignumber_js_1.BigNumber(value).toString(16)),hex.length%2!=0&&(hex="0"+hex);const tmp=Buffer.from(hex,"hex");if(tmp.length>bufferlen)throw Error("value is too large");const buf=Buffer.concat([Buffer.alloc(bufferlen-tmp.length),tmp]);return isLE?buf.reverse():buf},exports.supported_coin=Object.freeze([{purpose:[44,49,84,86],cointype:0,name:"bitcoin mainnet",module:"SecuxBTC",npm:"@secux/app-btc"},{purpose:[44,49,84,86],cointype:1,name:"bitcoin testnet",module:"SecuxBTC",npm:"@secux/app-btc"},{purpose:[44,49],cointype:2,name:"litecoin",module:"SecuxBTC",npm:"@secux/app-btc"},{purpose:[44],cointype:3,name:"dogecoin",module:"SecuxBTC",npm:"@secux/app-btc"},{purpose:[44],cointype:5,name:"dash",module:"SecuxBTC",npm:"@secux/app-btc"},{purpose:[44,49],cointype:17,name:"groestl",module:"SecuxBTC",npm:"@secux/app-btc"},{purpose:[44,49],cointype:20,name:"digibyte",module:"SecuxBTC",npm:"@secux/app-btc"},{purpose:[44],cointype:60,name:"ethereum",module:"SecuxETH",npm:"@secux/app-eth"},{purpose:[44],cointype:144,name:"ripple",module:"SecuxXRP",npm:"@secux/app-xrp"},{purpose:[44,49],cointype:145,name:"bitcoincash",module:"SecuxBTC",npm:"@secux/app-btc"},{purpose:[44],cointype:148,name:"stellar",module:"SecuxXLM",npm:"@secux/app-xlm"},{purpose:[44],cointype:195,name:"tron",module:"SecuxTRX",npm:"@secux/app-trx"},{purpose:[44],cointype:714,name:"binance",module:"SecuxBNB",npm:"@secux/app-bnb"},{purpose:[1852],cointype:1815,name:"cardano",module:"SecuxADA",npm:"@secux/app-ada"},{purpose:[44],cointype:501,name:"solana",module:"SecuxSOL",npm:"@secux/app-sol"},{purpose:[44],cointype:330,name:"terra",module:"SecuxLUNA",npm:"@secux/app-luna"},{purpose:[44],cointype:235,name:"fioprotocol",module:"SecuxFIO",npm:"@secux/app-fio"}]),exports.isSupportedCoin=function(path){const bip32=(0,BIP32Path_1.splitPath)(path);for(const def of exports.supported_coin)if(def.purpose.includes(bip32.purpose.value)&&def.cointype===bip32.coinType.value)return!0;return!1},exports.loadPlugin=function(plugin,name){try{const{ITransport}=require("@secux/transport");void 0===ITransport[name]&&Object.defineProperty(ITransport,name,{enumerable:!0,configurable:!1,writable:!1,value:plugin})}catch(error){null==logger||logger.debug("The package @secux/transport is not at environment, plugin loading skipped")}},function(FirmwareType){FirmwareType.mcu="mcu",FirmwareType.se="se"}(FirmwareType=exports.FirmwareType||(exports.FirmwareType={}));class FirmwareError extends Error{constructor(type,restrict,current){super(),_FirmwareError_type.set(this,void 0),_FirmwareError_current.set(this,void 0),_FirmwareError_restrict.set(this,void 0),__classPrivateFieldSet(this,_FirmwareError_type,FirmwareType[type],"f"),__classPrivateFieldSet(this,_FirmwareError_restrict,restrict,"f"),__classPrivateFieldSet(this,_FirmwareError_current,current,"f"),this.message=`${__classPrivateFieldGet(this,_FirmwareError_type,"f")} firmware need update, version "${restrict}" needed, but got "${current}"`}get type(){return __classPrivateFieldGet(this,_FirmwareError_type,"f")}get currentVersion(){return __classPrivateFieldGet(this,_FirmwareError_current,"f")}get restrictVersion(){return __classPrivateFieldGet(this,_FirmwareError_restrict,"f")}}function numbersToRegExp(list){let a="";return list.forEach((c=>a+=`${c.toString()}|`)),a=`(${a.substring(0,a.length-1)})`,a}exports.FirmwareError=FirmwareError,_FirmwareError_type=new WeakMap,_FirmwareError_current=new WeakMap,_FirmwareError_restrict=new WeakMap,exports.checkFWVersion=function(type,restrict,current){if(!restrict||!current)return;const r=restrict.split(".").map((x=>parseInt(x))),c=current.split(".").map((x=>parseInt(x)));for(let i=0;i<r.length-1;i++){const _r=r[i],_c=c[i];if(_r<_c)return;if(_r>_c)throw new FirmwareError(type,restrict,current)}const last=r.length-1,minor=c[last]+(current.includes(".D.")?1:0);if(r[last]>minor)throw new FirmwareError(type,restrict,current)},process.env.SECUX_LOGGER||Object.defineProperty(process.env,"SECUX_LOGGER",{enumerable:!1,configurable:!1,writable:!1,value:void 0});

@@ -28,2 +28,3 @@ /*!

export declare function toExtenededPublicKey(path: string, parentFingerPrint: Buffer, chainCode: Buffer, publicKey: Buffer): string;
export declare function convertXpubMagic(xpub: string, magic: number): string;
export declare function decodeXPUB(xpub: string): {

@@ -30,0 +31,0 @@ purpose: any;

@@ -18,2 +18,2 @@ "use strict";

limitations under the License.
*/Object.defineProperty(exports,"__esModule",{value:!0}),exports.deriveKey=exports.decodeXPUB=exports.toExtenededPublicKey=void 0;const BIP32Path_1=require("./BIP32Path"),bs58_1=require("./bs58"),hash_js_1=require("hash.js"),elliptic_1=require("elliptic"),ow_1=require("ow"),utility_1=require("./utility"),groestl=require("groestl-hash-js"),logger=null===utility_1.Logger||void 0===utility_1.Logger?void 0:utility_1.Logger.child({id:"utility"}),secp256k1=new elliptic_1.ec("secp256k1"),BN=secp256k1.curve.n.constructor;exports.toExtenededPublicKey=function(path,parentFingerPrint,chainCode,publicKey){var _a;(0,ow_1.default)(path,utility_1.owTool.bip32String),(0,ow_1.default)(parentFingerPrint,(0,utility_1.ow_checkBufferLength)(4)),(0,ow_1.default)(chainCode,(0,utility_1.ow_checkBufferLength)(32)),(0,ow_1.default)(publicKey,(0,utility_1.ow_checkBufferLength)(33));const buffer=Buffer.allocUnsafe(78),bip44=(0,BIP32Path_1.splitPath)(path);switch(bip44.purpose.value){case 44:case 86:buffer.writeUInt32BE(76067358,0);break;case 49:buffer.writeUInt32BE(77429938,0);break;case 84:buffer.writeUInt32BE(78792518,0);break;default:throw Error("unsupported purpose of path")}const depth=bip44.pathNum;let element;switch(buffer.writeUInt8(depth,4),(depth>0?parentFingerPrint:Buffer.alloc(4)).copy(buffer,5),depth){case 1:element=bip44.purpose;break;case 2:element=bip44.coinType;break;case 3:element=bip44.accountId;break;case 4:element=bip44.change;break;case 5:element=bip44.addressIndex;break;default:throw Error("Invalid Path, only support 1 to 5 depth path")}const index=element.isHardened?element.value+BIP32Path_1.HARDENED_OFFSET:null==element?void 0:element.value;return buffer.writeUInt32BE(index,9),chainCode.copy(buffer,13),publicKey.copy(buffer,45),17===(null===(_a=bip44.coinType)||void 0===_a?void 0:_a.value)?bs58_GRS.encode(buffer):bs58_BTC.encode(buffer)},exports.decodeXPUB=function(xpub){let payload,purpose;(0,ow_1.default)(xpub,utility_1.owTool.xpubString);try{payload=bs58_BTC.decode(xpub)}catch(error){null==logger||logger.warn(`The xpub is not Bitcoin compatible, try use Groestlcoin version:\n${xpub}`),payload=bs58_GRS.decode(xpub)}switch(payload.readUInt32BE(0)){case 76067358:purpose=44,null==logger||logger.warn("Please note that TapRoot type xpub using the same prefix, it cannot be considered here.");break;case 77429938:purpose=49;break;case 78792518:purpose=84;break;default:throw Error(`unsupport prefix, got 0x${payload.slice(0,4)}`)}const depth=payload.readUInt8(4),fingerprint=payload.slice(5,9),chaincode=payload.slice(13,45);return{purpose,depth,publickey:payload.slice(45),chaincode,fingerprint}},exports.deriveKey=function(publickey,chaincode,indexArray){(0,ow_1.default)(publickey,(0,utility_1.ow_checkBufferLength)(33)),(0,ow_1.default)(chaincode,(0,utility_1.ow_checkBufferLength)(32)),(0,ow_1.default)(indexArray,ow_1.default.array.ofType(ow_1.default.number.uint8));let xpub={publickey,chaincode};for(const index of indexArray)xpub=derive(xpub.publickey,xpub.chaincode,index);return xpub};const bs58_BTC=new bs58_1.bs58Check((function(data){const sha=(0,hash_js_1.sha256)().update(data).digest();return Buffer.from((0,hash_js_1.sha256)().update(sha).digest())})),bs58_GRS=new bs58_1.bs58Check((function(data){return Buffer.from(groestl.groestl_2(data,1,1))}));function derive(publickey,chaincode,index){const data=Buffer.allocUnsafe(37);publickey.copy(data,0),data.writeUInt32BE(index,33);const hmacSha512=(0,hash_js_1.hmac)(hash_js_1.sha512,chaincode).update(data).digest(),I=Buffer.from(hmacSha512),IL=I.slice(0,32),IR=I.slice(32);let Ki;try{const pair=secp256k1.keyFromPublic(publickey),tweak=new BN(IL);if(tweak.cmp(secp256k1.curve.n)>=0)throw Error("tweak error");const point=pair.getPublic().add(secp256k1.curve.g.mul(tweak));if(point.isInfinity())throw Error("point error");Ki=Buffer.from(point.encode("array",!0))}catch(err){return derive(publickey,chaincode,index+1)}return{publickey:Ki,chaincode:IR}}
*/Object.defineProperty(exports,"__esModule",{value:!0}),exports.deriveKey=exports.decodeXPUB=exports.convertXpubMagic=exports.toExtenededPublicKey=void 0;const BIP32Path_1=require("./BIP32Path"),bs58_1=require("./bs58"),hash_js_1=require("hash.js"),elliptic_1=require("elliptic"),ow_1=require("ow"),utility_1=require("./utility"),groestl=require("groestl-hash-js"),logger=null===utility_1.Logger||void 0===utility_1.Logger?void 0:utility_1.Logger.child({id:"utility"}),secp256k1=new elliptic_1.ec("secp256k1"),BN=secp256k1.curve.n.constructor;exports.toExtenededPublicKey=function(path,parentFingerPrint,chainCode,publicKey){var _a;(0,ow_1.default)(path,utility_1.owTool.bip32String),(0,ow_1.default)(parentFingerPrint,(0,utility_1.ow_checkBufferLength)(4)),(0,ow_1.default)(chainCode,(0,utility_1.ow_checkBufferLength)(32)),(0,ow_1.default)(publicKey,(0,utility_1.ow_checkBufferLength)(33));const buffer=Buffer.allocUnsafe(78),bip44=(0,BIP32Path_1.splitPath)(path);switch(bip44.purpose.value){case 44:case 86:buffer.writeUInt32BE(76067358,0);break;case 49:buffer.writeUInt32BE(77429938,0);break;case 84:buffer.writeUInt32BE(78792518,0);break;default:throw Error("unsupported purpose of path")}const depth=bip44.pathNum;let element;switch(buffer.writeUInt8(depth,4),(depth>0?parentFingerPrint:Buffer.alloc(4)).copy(buffer,5),depth){case 1:element=bip44.purpose;break;case 2:element=bip44.coinType;break;case 3:element=bip44.accountId;break;case 4:element=bip44.change;break;case 5:element=bip44.addressIndex;break;default:throw Error("Invalid Path, only support 1 to 5 depth path")}const index=element.isHardened?element.value+BIP32Path_1.HARDENED_OFFSET:null==element?void 0:element.value;return buffer.writeUInt32BE(index,9),chainCode.copy(buffer,13),publicKey.copy(buffer,45),17===(null===(_a=bip44.coinType)||void 0===_a?void 0:_a.value)?bs58_GRS.encode(buffer):bs58_BTC.encode(buffer)},exports.convertXpubMagic=function(xpub,magic){let result="";try{const payload=bs58_BTC.decode(xpub);payload.writeUInt32BE(magic,0),result=bs58_BTC.encode(payload)}catch(error){null==logger||logger.warn(`The xpub is not Bitcoin compatible, try use Groestlcoin version:\n${xpub}`);const payload=bs58_GRS.decode(xpub);payload.writeUInt32BE(magic,0),result=bs58_GRS.encode(payload)}return result},exports.decodeXPUB=function(xpub){let payload,purpose;(0,ow_1.default)(xpub,utility_1.owTool.xpubString);try{payload=bs58_BTC.decode(xpub)}catch(error){null==logger||logger.warn(`The xpub is not Bitcoin compatible, try use Groestlcoin version:\n${xpub}`),payload=bs58_GRS.decode(xpub)}switch(payload.readUInt32BE(0)){case 76067358:purpose=44,null==logger||logger.warn("Please note that TapRoot type xpub using the same prefix, it cannot be considered here.");break;case 77429938:purpose=49;break;case 78792518:purpose=84;break;default:throw Error(`unsupport prefix, got 0x${payload.slice(0,4)}`)}const depth=payload.readUInt8(4),fingerprint=payload.slice(5,9),chaincode=payload.slice(13,45);return{purpose,depth,publickey:payload.slice(45),chaincode,fingerprint}},exports.deriveKey=function(publickey,chaincode,indexArray){(0,ow_1.default)(publickey,(0,utility_1.ow_checkBufferLength)(33)),(0,ow_1.default)(chaincode,(0,utility_1.ow_checkBufferLength)(32)),(0,ow_1.default)(indexArray,ow_1.default.array.ofType(ow_1.default.number.uint8));let xpub={publickey,chaincode};for(const index of indexArray)xpub=derive(xpub.publickey,xpub.chaincode,index);return xpub};const bs58_BTC=new bs58_1.bs58Check((function(data){const sha=(0,hash_js_1.sha256)().update(data).digest();return Buffer.from((0,hash_js_1.sha256)().update(sha).digest())})),bs58_GRS=new bs58_1.bs58Check((function(data){return Buffer.from(groestl.groestl_2(data,1,1))}));function derive(publickey,chaincode,index){const data=Buffer.allocUnsafe(37);publickey.copy(data,0),data.writeUInt32BE(index,33);const hmacSha512=(0,hash_js_1.hmac)(hash_js_1.sha512,chaincode).update(data).digest(),I=Buffer.from(hmacSha512),IL=I.slice(0,32),IR=I.slice(32);let Ki;try{const pair=secp256k1.keyFromPublic(publickey),tweak=new BN(IL);if(tweak.cmp(secp256k1.curve.n)>=0)throw Error("tweak error");const point=pair.getPublic().add(secp256k1.curve.g.mul(tweak));if(point.isInfinity())throw Error("point error");Ki=Buffer.from(point.encode("array",!0))}catch(err){return derive(publickey,chaincode,index+1)}return{publickey:Ki,chaincode:IR}}
{
"name": "@secux/utility",
"version": "3.0.15",
"version": "3.0.16",
"description": "SecuX Hardware Wallet internal tools for SDK",

@@ -5,0 +5,0 @@ "keywords": [

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc