UNPKG

@secux/utility

Version:

SecuX Hardware Wallet internal tools for SDK

18 lines (15 loc) 9.65 kB
"use strict"; /*! Copyright 2022 SecuX Technology Inc Copyright Chen Wei-En Copyright Wu Tsung-Yu Licensed under the Apache License, Version 2.0 (the License); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an AS IS BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and 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.matches(/^[1-9A-HJ-NP-Za-km-z]+$/).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});