UNPKG

@secux/utility

Version:

SecuX Hardware Wallet internal tools for SDK

18 lines (15 loc) 3.62 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 _a,_Signature_r,_Signature_s,_Signature_v,_Signature_toDER,__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.Signature=void 0;const bignumber_js_1=require("bignumber.js");class Signature{constructor(r,s,v){_Signature_r.set(this,void 0),_Signature_s.set(this,void 0),_Signature_v.set(this,void 0),__classPrivateFieldSet(this,_Signature_r,r,"f"),__classPrivateFieldSet(this,_Signature_s,s,"f"),__classPrivateFieldSet(this,_Signature_v,v,"f")}static fromSignature(sig){const rBuffer=sig.slice(0,32),sBuffer=sig.slice(32,64),vBuffer=sig.slice(64,65);return new Signature(rBuffer,sBuffer,vBuffer)}toDER(){__classPrivateFieldSet(this,_Signature_r,__classPrivateFieldGet(Signature,_a,"m",_Signature_toDER).call(Signature,__classPrivateFieldGet(this,_Signature_r,"f")),"f"),__classPrivateFieldSet(this,_Signature_s,__classPrivateFieldGet(Signature,_a,"m",_Signature_toDER).call(Signature,__classPrivateFieldGet(this,_Signature_s,"f")),"f")}flipS(isLowerHalf=!0){const SECP256K1N=new bignumber_js_1.BigNumber("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141"),SECP256K1N_half=new bignumber_js_1.BigNumber("0x0FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD036414");let s=new bignumber_js_1.BigNumber(`0x${__classPrivateFieldGet(this,_Signature_s,"f").toString("hex")}`);if(isLowerHalf&&s>SECP256K1N_half||!isLowerHalf&&s<=SECP256K1N_half){s=SECP256K1N.minus(s),__classPrivateFieldGet(this,_Signature_s,"f").write(s.toString(16),"hex");let v=__classPrivateFieldGet(this,_Signature_v,"f").readUInt8();v=1-v,__classPrivateFieldGet(this,_Signature_v,"f").writeUInt8(v)}}get r(){return __classPrivateFieldGet(this,_Signature_r,"f")}get s(){return __classPrivateFieldGet(this,_Signature_s,"f")}get v(){return __classPrivateFieldGet(this,_Signature_v,"f")}}exports.Signature=Signature,_a=Signature,_Signature_r=new WeakMap,_Signature_s=new WeakMap,_Signature_v=new WeakMap,_Signature_toDER=function(x){const ZERO=Buffer.alloc(1,0);let i=0;for(;0===x[i];)++i;return i===x.length?ZERO:128&(x=x.slice(i))[0]?Buffer.concat([ZERO,x],1+x.length):x};