UNPKG

@swtc/keypairs

Version:
3 lines (2 loc) 22.4 kB
import t from"brorand";import e from"inherits";import*as i from"elliptic";import{eddsa as r,ec as n}from"elliptic";import{sm2 as s,Signature as h}from"@swtc/sm.js";export{sm2}from"@swtc/sm.js";import o from"bn.js";import{Factory as u}from"@swtc/address-codec";import{SM3 as c,funcAssert as a,funcHexToBytes as d,funcBytesToHex as p}from"@swtc/common";var f=l;function l(t,e){if(!t)throw new Error(e||"Assertion failed")}function g(t,e){return 55296==(64512&t.charCodeAt(e))&&(!(e<0||e+1>=t.length)&&56320==(64512&t.charCodeAt(e+1)))}function v(t){return(t>>>24|t>>>8&65280|t<<8&16711680|(255&t)<<24)>>>0}function y(t){return 1===t.length?"0"+t:t}function m(t){return 7===t.length?"0"+t:6===t.length?"00"+t:5===t.length?"000"+t:4===t.length?"0000"+t:3===t.length?"00000"+t:2===t.length?"000000"+t:1===t.length?"0000000"+t:t}l.equal=function(t,e,i){if(t!=e)throw new Error(i||"Assertion failed: "+t+" != "+e)};var _={inherits:e,toArray:function(t,e){if(Array.isArray(t))return t.slice();if(!t)return[];var i=[];if("string"==typeof t)if(e){if("hex"===e)for((t=t.replace(/[^a-z0-9]+/gi,"")).length%2!=0&&(t="0"+t),n=0;n<t.length;n+=2)i.push(parseInt(t[n]+t[n+1],16))}else for(var r=0,n=0;n<t.length;n++){var s=t.charCodeAt(n);s<128?i[r++]=s:s<2048?(i[r++]=s>>6|192,i[r++]=63&s|128):g(t,n)?(s=65536+((1023&s)<<10)+(1023&t.charCodeAt(++n)),i[r++]=s>>18|240,i[r++]=s>>12&63|128,i[r++]=s>>6&63|128,i[r++]=63&s|128):(i[r++]=s>>12|224,i[r++]=s>>6&63|128,i[r++]=63&s|128)}else for(n=0;n<t.length;n++)i[n]=0|t[n];return i},toHex:function(t){for(var e="",i=0;i<t.length;i++)e+=y(t[i].toString(16));return e},htonl:v,toHex32:function(t,e){for(var i="",r=0;r<t.length;r++){var n=t[r];"little"===e&&(n=v(n)),i+=m(n.toString(16))}return i},zero2:y,zero8:m,join32:function(t,e,i,r){var n=i-e;f(n%4==0);for(var s=new Array(n/4),h=0,o=e;h<s.length;h++,o+=4){var u;u="big"===r?t[o]<<24|t[o+1]<<16|t[o+2]<<8|t[o+3]:t[o+3]<<24|t[o+2]<<16|t[o+1]<<8|t[o],s[h]=u>>>0}return s},split32:function(t,e){for(var i=new Array(4*t.length),r=0,n=0;r<t.length;r++,n+=4){var s=t[r];"big"===e?(i[n]=s>>>24,i[n+1]=s>>>16&255,i[n+2]=s>>>8&255,i[n+3]=255&s):(i[n+3]=s>>>24,i[n+2]=s>>>16&255,i[n+1]=s>>>8&255,i[n]=255&s)}return i},rotr32:function(t,e){return t>>>e|t<<32-e},rotl32:function(t,e){return t<<e|t>>>32-e},sum32:function(t,e){return t+e>>>0},sum32_3:function(t,e,i){return t+e+i>>>0},sum32_4:function(t,e,i,r){return t+e+i+r>>>0},sum32_5:function(t,e,i,r,n){return t+e+i+r+n>>>0},sum64:function(t,e,i,r){var n=t[e],s=r+t[e+1]>>>0,h=(s<r?1:0)+i+n;t[e]=h>>>0,t[e+1]=s},sum64_hi:function(t,e,i,r){return(e+r>>>0<e?1:0)+t+i>>>0},sum64_lo:function(t,e,i,r){return e+r>>>0},sum64_4_hi:function(t,e,i,r,n,s,h,o){var u=0,c=e;return u+=(c=c+r>>>0)<e?1:0,u+=(c=c+s>>>0)<s?1:0,t+i+n+h+(u+=(c=c+o>>>0)<o?1:0)>>>0},sum64_4_lo:function(t,e,i,r,n,s,h,o){return e+r+s+o>>>0},sum64_5_hi:function(t,e,i,r,n,s,h,o,u,c){var a=0,d=e;return a+=(d=d+r>>>0)<e?1:0,a+=(d=d+s>>>0)<s?1:0,a+=(d=d+o>>>0)<o?1:0,t+i+n+h+u+(a+=(d=d+c>>>0)<c?1:0)>>>0},sum64_5_lo:function(t,e,i,r,n,s,h,o,u,c){return e+r+s+o+c>>>0},rotr64_hi:function(t,e,i){return(e<<32-i|t>>>i)>>>0},rotr64_lo:function(t,e,i){return(t<<32-i|e>>>i)>>>0},shr64_hi:function(t,e,i){return t>>>i},shr64_lo:function(t,e,i){return(t<<32-i|e>>>i)>>>0}};function b(){this.pending=null,this.pendingTotal=0,this.blockSize=this.constructor.blockSize,this.outSize=this.constructor.outSize,this.hmacStrength=this.constructor.hmacStrength,this.padLength=this.constructor.padLength/8,this.endian="big",this._delta8=this.blockSize/8,this._delta32=this.blockSize/32}var w=b;b.prototype.update=function(t,e){if(t=_.toArray(t,e),this.pending?this.pending=this.pending.concat(t):this.pending=t,this.pendingTotal+=t.length,this.pending.length>=this._delta8){var i=(t=this.pending).length%this._delta8;this.pending=t.slice(t.length-i,t.length),0===this.pending.length&&(this.pending=null),t=_.join32(t,0,t.length-i,this.endian);for(var r=0;r<t.length;r+=this._delta32)this._update(t,r,r+this._delta32)}return this},b.prototype.digest=function(t){return this.update(this._pad()),f(null===this.pending),this._digest(t)},b.prototype._pad=function(){var t=this.pendingTotal,e=this._delta8,i=e-(t+this.padLength)%e,r=new Array(i+this.padLength);r[0]=128;for(var n=1;n<i;n++)r[n]=0;if(t<<=3,"big"===this.endian){for(var s=8;s<this.padLength;s++)r[n++]=0;r[n++]=0,r[n++]=0,r[n++]=0,r[n++]=0,r[n++]=t>>>24&255,r[n++]=t>>>16&255,r[n++]=t>>>8&255,r[n++]=255&t}else for(r[n++]=255&t,r[n++]=t>>>8&255,r[n++]=t>>>16&255,r[n++]=t>>>24&255,r[n++]=0,r[n++]=0,r[n++]=0,r[n++]=0,s=8;s<this.padLength;s++)r[n++]=0;return r};var k={BlockHash:w},S=_.rotr32;function x(t,e,i){return t&e^~t&i}function K(t,e,i){return t&e^t&i^e&i}function A(t,e,i){return t^e^i}var z={ft_1:function(t,e,i,r){return 0===t?x(e,i,r):1===t||3===t?A(e,i,r):2===t?K(e,i,r):void 0},ch32:x,maj32:K,p32:A,s0_256:function(t){return S(t,2)^S(t,13)^S(t,22)},s1_256:function(t){return S(t,6)^S(t,11)^S(t,25)},g0_256:function(t){return S(t,7)^S(t,18)^t>>>3},g1_256:function(t){return S(t,17)^S(t,19)^t>>>10}},E=_.rotl32,B=_.sum32,C=_.sum32_5,P=z.ft_1,H=k.BlockHash,T=[1518500249,1859775393,2400959708,3395469782];function D(){if(!(this instanceof D))return new D;H.call(this),this.h=[1732584193,4023233417,2562383102,271733878,3285377520],this.W=new Array(80)}_.inherits(D,H);var U=D;D.blockSize=512,D.outSize=160,D.hmacStrength=80,D.padLength=64,D.prototype._update=function(t,e){for(var i=this.W,r=0;r<16;r++)i[r]=t[e+r];for(;r<i.length;r++)i[r]=E(i[r-3]^i[r-8]^i[r-14]^i[r-16],1);var n=this.h[0],s=this.h[1],h=this.h[2],o=this.h[3],u=this.h[4];for(r=0;r<i.length;r++){var c=~~(r/20),a=C(E(n,5),P(c,s,h,o),u,i[r],T[c]);u=o,o=h,h=E(s,30),s=n,n=a}this.h[0]=B(this.h[0],n),this.h[1]=B(this.h[1],s),this.h[2]=B(this.h[2],h),this.h[3]=B(this.h[3],o),this.h[4]=B(this.h[4],u)},D.prototype._digest=function(t){return"hex"===t?_.toHex32(this.h,"big"):_.split32(this.h,"big")};var L=_.sum32,W=_.sum32_4,F=_.sum32_5,j=z.ch32,I=z.maj32,q=z.s0_256,M=z.s1_256,N=z.g0_256,R=z.g1_256,V=k.BlockHash,J=[1116352408,1899447441,3049323471,3921009573,961987163,1508970993,2453635748,2870763221,3624381080,310598401,607225278,1426881987,1925078388,2162078206,2614888103,3248222580,3835390401,4022224774,264347078,604807628,770255983,1249150122,1555081692,1996064986,2554220882,2821834349,2952996808,3210313671,3336571891,3584528711,113926993,338241895,666307205,773529912,1294757372,1396182291,1695183700,1986661051,2177026350,2456956037,2730485921,2820302411,3259730800,3345764771,3516065817,3600352804,4094571909,275423344,430227734,506948616,659060556,883997877,958139571,1322822218,1537002063,1747873779,1955562222,2024104815,2227730452,2361852424,2428436474,2756734187,3204031479,3329325298];function O(){if(!(this instanceof O))return new O;V.call(this),this.h=[1779033703,3144134277,1013904242,2773480762,1359893119,2600822924,528734635,1541459225],this.k=J,this.W=new Array(64)}_.inherits(O,V);var G=O;function Q(){if(!(this instanceof Q))return new Q;G.call(this),this.h=[3238371032,914150663,812702999,4144912697,4290775857,1750603025,1694076839,3204075428]}O.blockSize=512,O.outSize=256,O.hmacStrength=192,O.padLength=64,O.prototype._update=function(t,e){for(var i=this.W,r=0;r<16;r++)i[r]=t[e+r];for(;r<i.length;r++)i[r]=W(R(i[r-2]),i[r-7],N(i[r-15]),i[r-16]);var n=this.h[0],s=this.h[1],h=this.h[2],o=this.h[3],u=this.h[4],c=this.h[5],a=this.h[6],d=this.h[7];for(f(this.k.length===i.length),r=0;r<i.length;r++){var p=F(d,M(u),j(u,c,a),this.k[r],i[r]),l=L(q(n),I(n,s,h));d=a,a=c,c=u,u=L(o,p),o=h,h=s,s=n,n=L(p,l)}this.h[0]=L(this.h[0],n),this.h[1]=L(this.h[1],s),this.h[2]=L(this.h[2],h),this.h[3]=L(this.h[3],o),this.h[4]=L(this.h[4],u),this.h[5]=L(this.h[5],c),this.h[6]=L(this.h[6],a),this.h[7]=L(this.h[7],d)},O.prototype._digest=function(t){return"hex"===t?_.toHex32(this.h,"big"):_.split32(this.h,"big")},_.inherits(Q,G);var X=Q;Q.blockSize=512,Q.outSize=224,Q.hmacStrength=192,Q.padLength=64,Q.prototype._digest=function(t){return"hex"===t?_.toHex32(this.h.slice(0,7),"big"):_.split32(this.h.slice(0,7),"big")};var Y=_.rotr64_hi,Z=_.rotr64_lo,$=_.shr64_hi,tt=_.shr64_lo,et=_.sum64,it=_.sum64_hi,rt=_.sum64_lo,nt=_.sum64_4_hi,st=_.sum64_4_lo,ht=_.sum64_5_hi,ot=_.sum64_5_lo,ut=k.BlockHash,ct=[1116352408,3609767458,1899447441,602891725,3049323471,3964484399,3921009573,2173295548,961987163,4081628472,1508970993,3053834265,2453635748,2937671579,2870763221,3664609560,3624381080,2734883394,310598401,1164996542,607225278,1323610764,1426881987,3590304994,1925078388,4068182383,2162078206,991336113,2614888103,633803317,3248222580,3479774868,3835390401,2666613458,4022224774,944711139,264347078,2341262773,604807628,2007800933,770255983,1495990901,1249150122,1856431235,1555081692,3175218132,1996064986,2198950837,2554220882,3999719339,2821834349,766784016,2952996808,2566594879,3210313671,3203337956,3336571891,1034457026,3584528711,2466948901,113926993,3758326383,338241895,168717936,666307205,1188179964,773529912,1546045734,1294757372,1522805485,1396182291,2643833823,1695183700,2343527390,1986661051,1014477480,2177026350,1206759142,2456956037,344077627,2730485921,1290863460,2820302411,3158454273,3259730800,3505952657,3345764771,106217008,3516065817,3606008344,3600352804,1432725776,4094571909,1467031594,275423344,851169720,430227734,3100823752,506948616,1363258195,659060556,3750685593,883997877,3785050280,958139571,3318307427,1322822218,3812723403,1537002063,2003034995,1747873779,3602036899,1955562222,1575990012,2024104815,1125592928,2227730452,2716904306,2361852424,442776044,2428436474,593698344,2756734187,3733110249,3204031479,2999351573,3329325298,3815920427,3391569614,3928383900,3515267271,566280711,3940187606,3454069534,4118630271,4000239992,116418474,1914138554,174292421,2731055270,289380356,3203993006,460393269,320620315,685471733,587496836,852142971,1086792851,1017036298,365543100,1126000580,2618297676,1288033470,3409855158,1501505948,4234509866,1607167915,987167468,1816402316,1246189591];function at(){if(!(this instanceof at))return new at;ut.call(this),this.h=[1779033703,4089235720,3144134277,2227873595,1013904242,4271175723,2773480762,1595750129,1359893119,2917565137,2600822924,725511199,528734635,4215389547,1541459225,327033209],this.k=ct,this.W=new Array(160)}_.inherits(at,ut);var dt=at;function pt(t,e,i,r,n){var s=t&i^~t&n;return s<0&&(s+=4294967296),s}function ft(t,e,i,r,n,s){var h=e&r^~e&s;return h<0&&(h+=4294967296),h}function lt(t,e,i,r,n){var s=t&i^t&n^i&n;return s<0&&(s+=4294967296),s}function gt(t,e,i,r,n,s){var h=e&r^e&s^r&s;return h<0&&(h+=4294967296),h}function vt(t,e){var i=Y(t,e,28)^Y(e,t,2)^Y(e,t,7);return i<0&&(i+=4294967296),i}function yt(t,e){var i=Z(t,e,28)^Z(e,t,2)^Z(e,t,7);return i<0&&(i+=4294967296),i}function mt(t,e){var i=Y(t,e,14)^Y(t,e,18)^Y(e,t,9);return i<0&&(i+=4294967296),i}function _t(t,e){var i=Z(t,e,14)^Z(t,e,18)^Z(e,t,9);return i<0&&(i+=4294967296),i}function bt(t,e){var i=Y(t,e,1)^Y(t,e,8)^$(t,e,7);return i<0&&(i+=4294967296),i}function wt(t,e){var i=Z(t,e,1)^Z(t,e,8)^tt(t,e,7);return i<0&&(i+=4294967296),i}function kt(t,e){var i=Y(t,e,19)^Y(e,t,29)^$(t,e,6);return i<0&&(i+=4294967296),i}function St(t,e){var i=Z(t,e,19)^Z(e,t,29)^tt(t,e,6);return i<0&&(i+=4294967296),i}function xt(){if(!(this instanceof xt))return new xt;dt.call(this),this.h=[3418070365,3238371032,1654270250,914150663,2438529370,812702999,355462360,4144912697,1731405415,4290775857,2394180231,1750603025,3675008525,1694076839,1203062813,3204075428]}at.blockSize=1024,at.outSize=512,at.hmacStrength=192,at.padLength=128,at.prototype._prepareBlock=function(t,e){for(var i=this.W,r=0;r<32;r++)i[r]=t[e+r];for(;r<i.length;r+=2){var n=kt(i[r-4],i[r-3]),s=St(i[r-4],i[r-3]),h=i[r-14],o=i[r-13],u=bt(i[r-30],i[r-29]),c=wt(i[r-30],i[r-29]),a=i[r-32],d=i[r-31];i[r]=nt(n,s,h,o,u,c,a,d),i[r+1]=st(n,s,h,o,u,c,a,d)}},at.prototype._update=function(t,e){this._prepareBlock(t,e);var i=this.W,r=this.h[0],n=this.h[1],s=this.h[2],h=this.h[3],o=this.h[4],u=this.h[5],c=this.h[6],a=this.h[7],d=this.h[8],p=this.h[9],l=this.h[10],g=this.h[11],v=this.h[12],y=this.h[13],m=this.h[14],_=this.h[15];f(this.k.length===i.length);for(var b=0;b<i.length;b+=2){var w=m,k=_,S=mt(d,p),x=_t(d,p),K=pt(d,p,l,g,v),A=ft(d,p,l,g,v,y),z=this.k[b],E=this.k[b+1],B=i[b],C=i[b+1],P=ht(w,k,S,x,K,A,z,E,B,C),H=ot(w,k,S,x,K,A,z,E,B,C);w=vt(r,n),k=yt(r,n),S=lt(r,n,s,h,o),x=gt(r,n,s,h,o,u);var T=it(w,k,S,x),D=rt(w,k,S,x);m=v,_=y,v=l,y=g,l=d,g=p,d=it(c,a,P,H),p=rt(a,a,P,H),c=o,a=u,o=s,u=h,s=r,h=n,r=it(P,H,T,D),n=rt(P,H,T,D)}et(this.h,0,r,n),et(this.h,2,s,h),et(this.h,4,o,u),et(this.h,6,c,a),et(this.h,8,d,p),et(this.h,10,l,g),et(this.h,12,v,y),et(this.h,14,m,_)},at.prototype._digest=function(t){return"hex"===t?_.toHex32(this.h,"big"):_.split32(this.h,"big")},_.inherits(xt,dt);var Kt=xt;xt.blockSize=1024,xt.outSize=384,xt.hmacStrength=192,xt.padLength=128,xt.prototype._digest=function(t){return"hex"===t?_.toHex32(this.h.slice(0,12),"big"):_.split32(this.h.slice(0,12),"big")};var At={sha1:U,sha224:X,sha256:G,sha384:Kt,sha512:dt},zt=_.rotl32,Et=_.sum32,Bt=_.sum32_3,Ct=_.sum32_4,Pt=k.BlockHash;function Ht(){if(!(this instanceof Ht))return new Ht;Pt.call(this),this.h=[1732584193,4023233417,2562383102,271733878,3285377520],this.endian="little"}_.inherits(Ht,Pt);var Tt=Ht;function Dt(t,e,i,r){return t<=15?e^i^r:t<=31?e&i|~e&r:t<=47?(e|~i)^r:t<=63?e&r|i&~r:e^(i|~r)}function Ut(t){return t<=15?0:t<=31?1518500249:t<=47?1859775393:t<=63?2400959708:2840853838}function Lt(t){return t<=15?1352829926:t<=31?1548603684:t<=47?1836072691:t<=63?2053994217:0}Ht.blockSize=512,Ht.outSize=160,Ht.hmacStrength=192,Ht.padLength=64,Ht.prototype._update=function(t,e){for(var i=this.h[0],r=this.h[1],n=this.h[2],s=this.h[3],h=this.h[4],o=i,u=r,c=n,a=s,d=h,p=0;p<80;p++){var f=Et(zt(Ct(i,Dt(p,r,n,s),t[Wt[p]+e],Ut(p)),jt[p]),h);i=h,h=s,s=zt(n,10),n=r,r=f,f=Et(zt(Ct(o,Dt(79-p,u,c,a),t[Ft[p]+e],Lt(p)),It[p]),d),o=d,d=a,a=zt(c,10),c=u,u=f}f=Bt(this.h[1],n,a),this.h[1]=Bt(this.h[2],s,d),this.h[2]=Bt(this.h[3],h,o),this.h[3]=Bt(this.h[4],i,u),this.h[4]=Bt(this.h[0],r,c),this.h[0]=f},Ht.prototype._digest=function(t){return"hex"===t?_.toHex32(this.h,"little"):_.split32(this.h,"little")};var Wt=[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,7,4,13,1,10,6,15,3,12,0,9,5,2,14,11,8,3,10,14,4,9,15,8,1,2,7,0,6,13,11,5,12,1,9,11,10,0,8,12,4,13,3,7,15,14,5,6,2,4,0,5,9,7,12,2,10,14,1,3,8,11,6,15,13],Ft=[5,14,7,0,9,2,11,4,13,6,15,8,1,10,3,12,6,11,3,7,0,13,5,10,14,15,8,12,4,9,1,2,15,5,1,3,7,14,6,9,11,8,12,2,10,0,4,13,8,6,4,1,3,11,15,0,5,12,2,13,9,7,10,14,12,15,10,4,1,5,8,7,6,2,13,14,0,3,9,11],jt=[11,14,15,12,5,8,7,9,11,13,14,15,6,7,9,8,7,6,8,13,11,9,7,15,7,12,15,9,11,7,13,12,11,13,6,7,14,9,13,15,14,8,13,6,5,12,7,5,11,12,14,15,14,15,9,8,9,14,5,6,8,6,5,12,9,15,5,11,6,8,13,12,5,12,13,14,11,8,5,6],It=[8,9,9,11,13,15,15,5,7,7,8,11,14,14,12,6,9,13,15,7,12,8,9,11,7,7,12,7,6,15,13,11,9,7,15,11,8,6,6,14,12,13,5,14,13,13,7,5,15,5,8,11,14,14,6,14,6,9,12,9,12,5,15,8,8,5,12,9,12,5,14,6,8,13,6,5,15,13,11,11],qt={ripemd160:Tt};function Mt(t,e,i){if(!(this instanceof Mt))return new Mt(t,e,i);this.Hash=t,this.blockSize=t.blockSize/8,this.outSize=t.outSize/8,this.inner=null,this.outer=null,this._init(_.toArray(e,i))}var Nt=Mt;Mt.prototype._init=function(t){t.length>this.blockSize&&(t=(new this.Hash).update(t).digest()),f(t.length<=this.blockSize);for(var e=t.length;e<this.blockSize;e++)t.push(0);for(e=0;e<t.length;e++)t[e]^=54;for(this.inner=(new this.Hash).update(t),e=0;e<t.length;e++)t[e]^=106;this.outer=(new this.Hash).update(t)},Mt.prototype.update=function(t,e){return this.inner.update(t,e),this},Mt.prototype.digest=function(t){return this.outer.update(this.inner.digest()),this.outer.digest(t)};var Rt,Vt,Jt=(function(t,e){var i=e;i.utils=_,i.common=k,i.sha=At,i.ripemd=qt,i.hmac=Nt,i.sha1=i.sha.sha1,i.sha256=i.sha.sha256,i.sha224=i.sha.sha224,i.sha384=i.sha.sha384,i.sha512=i.sha.sha512,i.ripemd160=i.ripemd.ripemd160}(Vt={path:Rt,exports:{},require:function(t,e){return function(){throw new Error("Dynamic requires are not currently supported by @rollup/plugin-commonjs")}(null==e&&Vt.path)}},Vt.exports),Vt.exports);class Ot{constructor(){this.hash=Jt.sha512()}add(t){return this.hash.update(t),this}addU32(t){return this.add([t>>>24&255,t>>>16&255,t>>>8&255,255&t])}finish(){return this.hash.digest()}first256(){return this.finish().slice(0,32)}first256BN(){return new o(this.first256())}}const Gt=i.ec("secp256k1");function Qt(t,e){const i=Gt.curve.n;for(let r=0;r<=4294967295;r++){const n=(new Ot).add(t);void 0!==e&&n.addU32(e),n.addU32(r);const s=n.first256BN();if(s.cmpn(0)>0&&s.cmp(i)<0)return s}throw new Error("impossible unicorn ;)")}function Xt(t,e={}){const i=e.validator,r=Gt.curve.n,n=Qt(t);if(i)return n;const s=Gt.g.mul(n),h=e.accountIndex||0;return Qt(s.encodeCompressed(),h).add(n).mod(r)}function Yt(t){const e=Gt.curve.decodePoint(t),i=Qt(t,0),r=Gt.g.mul(i);return e.add(r).encodeCompressed()}function Zt(t,e){const i=s.curve.n;for(let r=0;r<=4294967295;r++){const n=void 0!==e?Buffer.alloc(41):Buffer.alloc(20);n.fill(Buffer.from(t),0,t.length),void 0!==e&&(n.writeUIntBE(e,t.length,4),n.writeUIntBE(r,t.length+4,4)),n.writeUIntBE(r,t.length,4);const s=(new c).update(n).digest("hex"),h=new o(s,"hex");if(h.cmpn(0)>0&&h.cmp(i)<0)return h}throw new Error("impossible unicorn ;)")}function $t(t){return Jt.sha512().update(t).digest().slice(0,16)}function te(t){return(new c).update(t).digest().slice(0,16)}const ee=r("ed25519"),ie=n("secp256k1");function re(t){return Jt.sha512().update(t).digest().slice(0,32)}function ne(t){return(new c).update(t).digest().slice(0,32)}function se(e="jingtum"){const i=u(e),r=i.guomi,n=i.token,f=r?ne:re,l={deriveKeypair:(t,e={})=>{const i="00"+Xt(t,e).toString(16,64).toUpperCase(),r=p(ie.keyFromPrivate(i.slice(2)).getPublic().encodeCompressed());return{privateKey:i,publicKey:r}},deriveKeypairWithPrivateKey:t=>{const e="00"+t.toUpperCase(),i=p(ie.keyFromPrivate(e.slice(2)).getPublic().encodeCompressed());return{privateKey:e,publicKey:i}},sign:(t,e)=>p(ie.sign(f(t),d(e),{canonical:!0}).toDER()),verify:(t,e,i)=>ie.verify(f(t),e,d(i)),signTx:(t,e)=>p(ie.sign(t,d(e),{canonical:!0}).toDER()),verifyTx:(t,e,i)=>ie.verify(t,e,d(i))},g={deriveKeypair:(t,e={})=>{const i=function(t,e={}){const i=e.validator,r=s.curve.n,n=Zt(t);if(i)return n;const h=s.curve.g.mul(n),o=e.accountIndex||0;return Zt(h.encodeCompressed(),o).add(n).mod(r)}(t,e);return{privateKey:"00"+i.toString(16,64).toUpperCase(),publicKey:p(s.SM2KeyPair(null,i).pub.encodeCompressed())}},deriveKeypairWithPrivateKey:t=>{const e=new o(t,"hex");return{privateKey:"00"+t.toUpperCase(),publicKey:p(s.SM2KeyPair(null,e).pub.encodeCompressed())}},sign:(t,e)=>{const i=s.SM2KeyPair(null,new o(e,"hex"));return p(new h(i.sign(f(t))).toDER())},verify:(t,e,i)=>{const r=s.SM2KeyPair(i),n=new h(e,"hex");return r.verify(f(t),n.r,n.s)},signTx:(t,e)=>{"string"==typeof t&&/^[0-9A-F]{16}/i.test(t)&&(t=d(t));const i=s.SM2KeyPair(null,new o(e,"hex"));return p(new h(i.sign(t)).toDER())},verifyTx:(t,e,i)=>{"string"==typeof t&&/^[0-9A-F]{16}/i.test(t)&&(t=d(t));const r=s.SM2KeyPair(i),n=new h(e,"hex");return r.verify(f(t),n.r,n.s)}},v={deriveKeypair:t=>{const e=f(t);return{privateKey:"ED"+p(e),publicKey:"ED"+p(ee.keyFromSecret(e).pubBytes())}},deriveKeypairWithPrivateKey:t=>({privateKey:"ED"+t.toUpperCase(),publicKey:"ED"+p(ee.keyFromSecret(d(t)).pubBytes())}),sign:(t,e)=>(a(Array.isArray(t),"message must be array of octets"),p(ee.sign(t,d(e).slice(1)).toBytes())),verify:(t,e,i)=>ee.verify(t,d(e),d(i).slice(1)),signTx:(t,e)=>ee.sign(t,d(e).slice(1)).toHex(),verifyTx:(t,e,i)=>ee.verify(t,d(e),d(i).slice(1))},y={deriveKeypair:t=>{const e=f(t);return{privateKey:"ED"+p(e),publicKey:"ED"+p(ee.keyFromSecret(e).pubBytes())}},deriveKeypairWithPrivateKey:t=>({privateKey:"ED"+t.toUpperCase(),publicKey:"ED"+p(ee.keyFromSecret(d(t)).pubBytes())}),sign:(t,e)=>(a(Array.isArray(t),"message must be array of octets"),p(ee.sign(t,d(e).slice(1)).toBytes())),verify:(t,e,i)=>ee.verify(t,d(e),d(i).slice(1)),signTx:(t,e)=>ee.sign(t,d(e).slice(1)).toHex(),verifyTx:(t,e,i)=>ee.verify(t,d(e),d(i).slice(1))};function m(t){const e={secp256k1:l,sm2p256v1:g,ed25519:v,ed25519guomi:y};return"ed25519"===t?r?e.ed25519guomi:e.ed25519:e[t]}function _(t){const e=d(t);return 33===e.length&&237===e[0]?"ed25519":r?"sm2p256v1":"secp256k1"}function b(t,e=(r?"sm2p256v1":"secp256k1")){const n=t;let s,h,o=t;if("string"!=typeof t)throw new Error("deriving keypair requires secret or private key");if(/^s/.test(t)){const t=i.decodeSeed(n);h=m(e="ed25519"===t.type?"ed25519":r?"sm2p256v1":"secp256k1"),s=h.deriveKeypair(t.bytes)}else{if(64===t.length)e="ed25519"===e?"ed25519":r?"sm2p256v1":"secp256k1";else{if(66!==t.length)throw new Error("deriving keypair requires valid private key");if("00"===t.slice(0,2))e=r?"sm2p256v1":"secp256k1";else{if("ED"!==t.slice(0,2).toUpperCase())throw new Error("deriving keypair requires correct prefixed private key");e="ed25519"}o=t.slice(2)}h=m(e),s=h.deriveKeypairWithPrivateKey(o)}const u=f("This test message should verify."),c=h.sign(u,s.privateKey);if(!0!==h.verify(u,c,s.publicKey))throw new Error("derived keypair did not generate verifiable signature");return s}function w(t){return i.encodeAccountID(Buffer.from(r?function(t){const e=(new c).update(t).digest();return Jt.ripemd160().update(e).digest()}(t):function(t){const e=Jt.sha256().update(t).digest();return Jt.ripemd160().update(e).digest()}(t)))}return{guomi:r,token:n,addressCodec:i,deriveKeyPair:b,hash:f,seedFromPhrase:r?te:$t,signTx:function(t,e){return m(_(e)).signTx(t,e)},verifyTx:function(t,e,i){return m(_(i)).verifyTx(t,e,i)},convertAddressToBytes:function(t){return i.decodeAddress(t).toJSON().data},convertBytesToAddress:function(t){return i.encodeAddress(t)},checkAddress:i.isValidAddress,isValidAddress:i.isValidAddress,deriveKeypair:b,generateSeed:function(e={}){a(!e.entropy||e.entropy.length>=16,"entropy too short");const n=e.entropy?e.entropy.slice(0,16):t(16),s="ed25519"===e.algorithm?"ed25519":r?"sm2p256v1":"secp256k1";return i.encodeSeed(n,s)},sign:function(t,e){return m(_(e)).sign(d(t),e)},verify:function(t,e,i){return m(_(i)).verify(d(t),e,i)},deriveAddress:function(t){return w(d(t))},deriveNodeAddress:function(t){const e=i.decodeNodePublic(t);return w(r?function(t){const e=s.curve.decodePoint(t),i=Zt(t,0),r=s.curve.g.mul(i);return e.add(r).encodeCompressed()}(e):Yt(e))}}}const he=se(),oe=se("guomi");export{se as Factory,he as Keypairs,oe as KeypairsGm}; //# sourceMappingURL=index.js.map