@swtc/keypairs
Version:
swtc keypairs
3 lines (2 loc) • 22.9 kB
JavaScript
import t from"brorand";import e from"inherits";import*as r from"elliptic";import{eddsa as i,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,funcHexToBytes as a,funcAssert as f,funcBytesToHex as d}from"@swtc/common";function l(t){return t&&t.__esModule&&Object.prototype.hasOwnProperty.call(t,"default")?t.default:t}var p,g,v,y={},m={};function _(){if(g)return p;function t(t,e){if(!t)throw new Error(e||"Assertion failed")}return g=1,p=t,t.equal=function(t,e,r){if(t!=e)throw new Error(r||"Assertion failed: "+t+" != "+e)},p}function b(){if(v)return m;v=1;var t=_(),r=e;function i(t,e){return 55296==(64512&t.charCodeAt(e))&&(!(e<0||e+1>=t.length)&&56320==(64512&t.charCodeAt(e+1)))}function n(t){return(t>>>24|t>>>8&65280|t<<8&16711680|(255&t)<<24)>>>0}function s(t){return 1===t.length?"0"+t:t}function h(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}return m.inherits=r,m.toArray=function(t,e){if(Array.isArray(t))return t.slice();if(!t)return[];var r=[];if("string"==typeof t)if(e){if("hex"===e)for((t=t.replace(/[^a-z0-9]+/gi,"")).length%2!=0&&(t="0"+t),s=0;s<t.length;s+=2)r.push(parseInt(t[s]+t[s+1],16))}else for(var n=0,s=0;s<t.length;s++){var h=t.charCodeAt(s);h<128?r[n++]=h:h<2048?(r[n++]=h>>6|192,r[n++]=63&h|128):i(t,s)?(h=65536+((1023&h)<<10)+(1023&t.charCodeAt(++s)),r[n++]=h>>18|240,r[n++]=h>>12&63|128,r[n++]=h>>6&63|128,r[n++]=63&h|128):(r[n++]=h>>12|224,r[n++]=h>>6&63|128,r[n++]=63&h|128)}else for(s=0;s<t.length;s++)r[s]=0|t[s];return r},m.toHex=function(t){for(var e="",r=0;r<t.length;r++)e+=s(t[r].toString(16));return e},m.htonl=n,m.toHex32=function(t,e){for(var r="",i=0;i<t.length;i++){var s=t[i];"little"===e&&(s=n(s)),r+=h(s.toString(16))}return r},m.zero2=s,m.zero8=h,m.join32=function(e,r,i,n){var s=i-r;t(s%4==0);for(var h=new Array(s/4),o=0,u=r;o<h.length;o++,u+=4){var c;c="big"===n?e[u]<<24|e[u+1]<<16|e[u+2]<<8|e[u+3]:e[u+3]<<24|e[u+2]<<16|e[u+1]<<8|e[u],h[o]=c>>>0}return h},m.split32=function(t,e){for(var r=new Array(4*t.length),i=0,n=0;i<t.length;i++,n+=4){var s=t[i];"big"===e?(r[n]=s>>>24,r[n+1]=s>>>16&255,r[n+2]=s>>>8&255,r[n+3]=255&s):(r[n+3]=s>>>24,r[n+2]=s>>>16&255,r[n+1]=s>>>8&255,r[n]=255&s)}return r},m.rotr32=function(t,e){return t>>>e|t<<32-e},m.rotl32=function(t,e){return t<<e|t>>>32-e},m.sum32=function(t,e){return t+e>>>0},m.sum32_3=function(t,e,r){return t+e+r>>>0},m.sum32_4=function(t,e,r,i){return t+e+r+i>>>0},m.sum32_5=function(t,e,r,i,n){return t+e+r+i+n>>>0},m.sum64=function(t,e,r,i){var n=t[e],s=i+t[e+1]>>>0,h=(s<i?1:0)+r+n;t[e]=h>>>0,t[e+1]=s},m.sum64_hi=function(t,e,r,i){return(e+i>>>0<e?1:0)+t+r>>>0},m.sum64_lo=function(t,e,r,i){return e+i>>>0},m.sum64_4_hi=function(t,e,r,i,n,s,h,o){var u=0,c=e;return u+=(c=c+i>>>0)<e?1:0,u+=(c=c+s>>>0)<s?1:0,t+r+n+h+(u+=(c=c+o>>>0)<o?1:0)>>>0},m.sum64_4_lo=function(t,e,r,i,n,s,h,o){return e+i+s+o>>>0},m.sum64_5_hi=function(t,e,r,i,n,s,h,o,u,c){var a=0,f=e;return a+=(f=f+i>>>0)<e?1:0,a+=(f=f+s>>>0)<s?1:0,a+=(f=f+o>>>0)<o?1:0,t+r+n+h+u+(a+=(f=f+c>>>0)<c?1:0)>>>0},m.sum64_5_lo=function(t,e,r,i,n,s,h,o,u,c){return e+i+s+o+c>>>0},m.rotr64_hi=function(t,e,r){return(e<<32-r|t>>>r)>>>0},m.rotr64_lo=function(t,e,r){return(t<<32-r|e>>>r)>>>0},m.shr64_hi=function(t,e,r){return t>>>r},m.shr64_lo=function(t,e,r){return(t<<32-r|e>>>r)>>>0},m}var k,w={};function S(){if(k)return w;k=1;var t=b(),e=_();function r(){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}return w.BlockHash=r,r.prototype.update=function(e,r){if(e=t.toArray(e,r),this.pending?this.pending=this.pending.concat(e):this.pending=e,this.pendingTotal+=e.length,this.pending.length>=this._delta8){var i=(e=this.pending).length%this._delta8;this.pending=e.slice(e.length-i,e.length),0===this.pending.length&&(this.pending=null),e=t.join32(e,0,e.length-i,this.endian);for(var n=0;n<e.length;n+=this._delta32)this._update(e,n,n+this._delta32)}return this},r.prototype.digest=function(t){return this.update(this._pad()),e(null===this.pending),this._digest(t)},r.prototype._pad=function(){var t=this.pendingTotal,e=this._delta8,r=e-(t+this.padLength)%e,i=new Array(r+this.padLength);i[0]=128;for(var n=1;n<r;n++)i[n]=0;if(t<<=3,"big"===this.endian){for(var s=8;s<this.padLength;s++)i[n++]=0;i[n++]=0,i[n++]=0,i[n++]=0,i[n++]=0,i[n++]=t>>>24&255,i[n++]=t>>>16&255,i[n++]=t>>>8&255,i[n++]=255&t}else for(i[n++]=255&t,i[n++]=t>>>8&255,i[n++]=t>>>16&255,i[n++]=t>>>24&255,i[n++]=0,i[n++]=0,i[n++]=0,i[n++]=0,s=8;s<this.padLength;s++)i[n++]=0;return i},w}var x,K,A,z,B,E,P,C,H,T,D,U,L={},W={};function F(){if(x)return W;x=1;var t=b().rotr32;function e(t,e,r){return t&e^~t&r}function r(t,e,r){return t&e^t&r^e&r}function i(t,e,r){return t^e^r}return W.ft_1=function(t,n,s,h){return 0===t?e(n,s,h):1===t||3===t?i(n,s,h):2===t?r(n,s,h):void 0},W.ch32=e,W.maj32=r,W.p32=i,W.s0_256=function(e){return t(e,2)^t(e,13)^t(e,22)},W.s1_256=function(e){return t(e,6)^t(e,11)^t(e,25)},W.g0_256=function(e){return t(e,7)^t(e,18)^e>>>3},W.g1_256=function(e){return t(e,17)^t(e,19)^e>>>10},W}function j(){if(B)return z;B=1;var t=b(),e=S(),r=F(),i=_(),n=t.sum32,s=t.sum32_4,h=t.sum32_5,o=r.ch32,u=r.maj32,c=r.s0_256,a=r.s1_256,f=r.g0_256,d=r.g1_256,l=e.BlockHash,p=[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 g(){if(!(this instanceof g))return new g;l.call(this),this.h=[1779033703,3144134277,1013904242,2773480762,1359893119,2600822924,528734635,1541459225],this.k=p,this.W=new Array(64)}return t.inherits(g,l),z=g,g.blockSize=512,g.outSize=256,g.hmacStrength=192,g.padLength=64,g.prototype._update=function(t,e){for(var r=this.W,l=0;l<16;l++)r[l]=t[e+l];for(;l<r.length;l++)r[l]=s(d(r[l-2]),r[l-7],f(r[l-15]),r[l-16]);var p=this.h[0],g=this.h[1],v=this.h[2],y=this.h[3],m=this.h[4],_=this.h[5],b=this.h[6],k=this.h[7];for(i(this.k.length===r.length),l=0;l<r.length;l++){var w=h(k,a(m),o(m,_,b),this.k[l],r[l]),S=n(c(p),u(p,g,v));k=b,b=_,_=m,m=n(y,w),y=v,v=g,g=p,p=n(w,S)}this.h[0]=n(this.h[0],p),this.h[1]=n(this.h[1],g),this.h[2]=n(this.h[2],v),this.h[3]=n(this.h[3],y),this.h[4]=n(this.h[4],m),this.h[5]=n(this.h[5],_),this.h[6]=n(this.h[6],b),this.h[7]=n(this.h[7],k)},g.prototype._digest=function(e){return"hex"===e?t.toHex32(this.h,"big"):t.split32(this.h,"big")},z}function I(){if(H)return C;H=1;var t=b(),e=S(),r=_(),i=t.rotr64_hi,n=t.rotr64_lo,s=t.shr64_hi,h=t.shr64_lo,o=t.sum64,u=t.sum64_hi,c=t.sum64_lo,a=t.sum64_4_hi,f=t.sum64_4_lo,d=t.sum64_5_hi,l=t.sum64_5_lo,p=e.BlockHash,g=[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 v(){if(!(this instanceof v))return new v;p.call(this),this.h=[1779033703,4089235720,3144134277,2227873595,1013904242,4271175723,2773480762,1595750129,1359893119,2917565137,2600822924,725511199,528734635,4215389547,1541459225,327033209],this.k=g,this.W=new Array(160)}function y(t,e,r,i,n){var s=t&r^~t&n;return s<0&&(s+=4294967296),s}function m(t,e,r,i,n,s){var h=e&i^~e&s;return h<0&&(h+=4294967296),h}function k(t,e,r,i,n){var s=t&r^t&n^r&n;return s<0&&(s+=4294967296),s}function w(t,e,r,i,n,s){var h=e&i^e&s^i&s;return h<0&&(h+=4294967296),h}function x(t,e){var r=i(t,e,28)^i(e,t,2)^i(e,t,7);return r<0&&(r+=4294967296),r}function K(t,e){var r=n(t,e,28)^n(e,t,2)^n(e,t,7);return r<0&&(r+=4294967296),r}function A(t,e){var r=i(t,e,14)^i(t,e,18)^i(e,t,9);return r<0&&(r+=4294967296),r}function z(t,e){var r=n(t,e,14)^n(t,e,18)^n(e,t,9);return r<0&&(r+=4294967296),r}function B(t,e){var r=i(t,e,1)^i(t,e,8)^s(t,e,7);return r<0&&(r+=4294967296),r}function E(t,e){var r=n(t,e,1)^n(t,e,8)^h(t,e,7);return r<0&&(r+=4294967296),r}function P(t,e){var r=i(t,e,19)^i(e,t,29)^s(t,e,6);return r<0&&(r+=4294967296),r}function T(t,e){var r=n(t,e,19)^n(e,t,29)^h(t,e,6);return r<0&&(r+=4294967296),r}return t.inherits(v,p),C=v,v.blockSize=1024,v.outSize=512,v.hmacStrength=192,v.padLength=128,v.prototype._prepareBlock=function(t,e){for(var r=this.W,i=0;i<32;i++)r[i]=t[e+i];for(;i<r.length;i+=2){var n=P(r[i-4],r[i-3]),s=T(r[i-4],r[i-3]),h=r[i-14],o=r[i-13],u=B(r[i-30],r[i-29]),c=E(r[i-30],r[i-29]),d=r[i-32],l=r[i-31];r[i]=a(n,s,h,o,u,c,d,l),r[i+1]=f(n,s,h,o,u,c,d,l)}},v.prototype._update=function(t,e){this._prepareBlock(t,e);var i=this.W,n=this.h[0],s=this.h[1],h=this.h[2],a=this.h[3],f=this.h[4],p=this.h[5],g=this.h[6],v=this.h[7],_=this.h[8],b=this.h[9],S=this.h[10],B=this.h[11],E=this.h[12],P=this.h[13],C=this.h[14],H=this.h[15];r(this.k.length===i.length);for(var T=0;T<i.length;T+=2){var D=C,U=H,L=A(_,b),W=z(_,b),F=y(_,b,S,B,E),j=m(_,b,S,B,E,P),I=this.k[T],M=this.k[T+1],N=i[T],q=i[T+1],R=d(D,U,L,W,F,j,I,M,N,q),O=l(D,U,L,W,F,j,I,M,N,q);D=x(n,s),U=K(n,s),L=k(n,s,h,a,f),W=w(n,s,h,a,f,p);var V=u(D,U,L,W),J=c(D,U,L,W);C=E,H=P,E=S,P=B,S=_,B=b,_=u(g,v,R,O),b=c(v,v,R,O),g=f,v=p,f=h,p=a,h=n,a=s,n=u(R,O,V,J),s=c(R,O,V,J)}o(this.h,0,n,s),o(this.h,2,h,a),o(this.h,4,f,p),o(this.h,6,g,v),o(this.h,8,_,b),o(this.h,10,S,B),o(this.h,12,E,P),o(this.h,14,C,H)},v.prototype._digest=function(e){return"hex"===e?t.toHex32(this.h,"big"):t.split32(this.h,"big")},C}function M(){return U||(U=1,L.sha1=function(){if(A)return K;A=1;var t=b(),e=S(),r=F(),i=t.rotl32,n=t.sum32,s=t.sum32_5,h=r.ft_1,o=e.BlockHash,u=[1518500249,1859775393,2400959708,3395469782];function c(){if(!(this instanceof c))return new c;o.call(this),this.h=[1732584193,4023233417,2562383102,271733878,3285377520],this.W=new Array(80)}return t.inherits(c,o),K=c,c.blockSize=512,c.outSize=160,c.hmacStrength=80,c.padLength=64,c.prototype._update=function(t,e){for(var r=this.W,o=0;o<16;o++)r[o]=t[e+o];for(;o<r.length;o++)r[o]=i(r[o-3]^r[o-8]^r[o-14]^r[o-16],1);var c=this.h[0],a=this.h[1],f=this.h[2],d=this.h[3],l=this.h[4];for(o=0;o<r.length;o++){var p=~~(o/20),g=s(i(c,5),h(p,a,f,d),l,r[o],u[p]);l=d,d=f,f=i(a,30),a=c,c=g}this.h[0]=n(this.h[0],c),this.h[1]=n(this.h[1],a),this.h[2]=n(this.h[2],f),this.h[3]=n(this.h[3],d),this.h[4]=n(this.h[4],l)},c.prototype._digest=function(e){return"hex"===e?t.toHex32(this.h,"big"):t.split32(this.h,"big")},K}(),L.sha224=function(){if(P)return E;P=1;var t=b(),e=j();function r(){if(!(this instanceof r))return new r;e.call(this),this.h=[3238371032,914150663,812702999,4144912697,4290775857,1750603025,1694076839,3204075428]}return t.inherits(r,e),E=r,r.blockSize=512,r.outSize=224,r.hmacStrength=192,r.padLength=64,r.prototype._digest=function(e){return"hex"===e?t.toHex32(this.h.slice(0,7),"big"):t.split32(this.h.slice(0,7),"big")},E}(),L.sha256=j(),L.sha384=function(){if(D)return T;D=1;var t=b(),e=I();function r(){if(!(this instanceof r))return new r;e.call(this),this.h=[3418070365,3238371032,1654270250,914150663,2438529370,812702999,355462360,4144912697,1731405415,4290775857,2394180231,1750603025,3675008525,1694076839,1203062813,3204075428]}return t.inherits(r,e),T=r,r.blockSize=1024,r.outSize=384,r.hmacStrength=192,r.padLength=128,r.prototype._digest=function(e){return"hex"===e?t.toHex32(this.h.slice(0,12),"big"):t.split32(this.h.slice(0,12),"big")},T}(),L.sha512=I()),L}var N,q,R,O,V={};var J=(O||(O=1,function(t){var e=t;e.utils=b(),e.common=S(),e.sha=M(),e.ripemd=function(){if(N)return V;N=1;var t=b(),e=S(),r=t.rotl32,i=t.sum32,n=t.sum32_3,s=t.sum32_4,h=e.BlockHash;function o(){if(!(this instanceof o))return new o;h.call(this),this.h=[1732584193,4023233417,2562383102,271733878,3285377520],this.endian="little"}function u(t,e,r,i){return t<=15?e^r^i:t<=31?e&r|~e&i:t<=47?(e|~r)^i:t<=63?e&i|r&~i:e^(r|~i)}function c(t){return t<=15?0:t<=31?1518500249:t<=47?1859775393:t<=63?2400959708:2840853838}function a(t){return t<=15?1352829926:t<=31?1548603684:t<=47?1836072691:t<=63?2053994217:0}t.inherits(o,h),V.ripemd160=o,o.blockSize=512,o.outSize=160,o.hmacStrength=192,o.padLength=64,o.prototype._update=function(t,e){for(var h=this.h[0],o=this.h[1],g=this.h[2],v=this.h[3],y=this.h[4],m=h,_=o,b=g,k=v,w=y,S=0;S<80;S++){var x=i(r(s(h,u(S,o,g,v),t[f[S]+e],c(S)),l[S]),y);h=y,y=v,v=r(g,10),g=o,o=x,x=i(r(s(m,u(79-S,_,b,k),t[d[S]+e],a(S)),p[S]),w),m=w,w=k,k=r(b,10),b=_,_=x}x=n(this.h[1],g,k),this.h[1]=n(this.h[2],v,w),this.h[2]=n(this.h[3],y,m),this.h[3]=n(this.h[4],h,_),this.h[4]=n(this.h[0],o,b),this.h[0]=x},o.prototype._digest=function(e){return"hex"===e?t.toHex32(this.h,"little"):t.split32(this.h,"little")};var f=[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],d=[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],l=[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],p=[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];return V}(),e.hmac=function(){if(R)return q;R=1;var t=b(),e=_();function r(e,i,n){if(!(this instanceof r))return new r(e,i,n);this.Hash=e,this.blockSize=e.blockSize/8,this.outSize=e.outSize/8,this.inner=null,this.outer=null,this._init(t.toArray(i,n))}return q=r,r.prototype._init=function(t){t.length>this.blockSize&&(t=(new this.Hash).update(t).digest()),e(t.length<=this.blockSize);for(var r=t.length;r<this.blockSize;r++)t.push(0);for(r=0;r<t.length;r++)t[r]^=54;for(this.inner=(new this.Hash).update(t),r=0;r<t.length;r++)t[r]^=106;this.outer=(new this.Hash).update(t)},r.prototype.update=function(t,e){return this.inner.update(t,e),this},r.prototype.digest=function(t){return this.outer.update(this.inner.digest()),this.outer.digest(t)},q}(),e.sha1=e.sha.sha1,e.sha256=e.sha.sha256,e.sha224=e.sha.sha224,e.sha384=e.sha.sha384,e.sha512=e.sha.sha512,e.ripemd160=e.ripemd.ripemd160}(y)),y),G=l(J);class Q{constructor(){this.hash=G.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 X=r.ec("secp256k1");function Y(t,e){const r=X.curve.n;for(let i=0;i<=4294967295;i++){const n=(new Q).add(t);void 0!==e&&n.addU32(e),n.addU32(i);const s=n.first256BN();if(s.cmpn(0)>0&&s.cmp(r)<0)return s}throw new Error("impossible unicorn ;)")}function Z(t,e={}){const r=e.validator,i=X.curve.n,n=Y(t);if(r)return n;const s=X.g.mul(n),h=e.accountIndex||0;return Y(s.encodeCompressed(),h).add(n).mod(i)}function $(t){const e=X.curve.decodePoint(t),r=Y(t,0),i=X.g.mul(r);return e.add(i).encodeCompressed()}function tt(t,e){const r=s.curve.n;for(let i=0;i<=4294967295;i++){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(i,t.length+4,4)),n.writeUIntBE(i,t.length,4);const s=(new c).update(n).digest("hex"),h=new o(s,"hex");if(h.cmpn(0)>0&&h.cmp(r)<0)return h}throw new Error("impossible unicorn ;)")}function et(t){return G.sha512().update(t).digest().slice(0,16)}function rt(t){return(new c).update(t).digest().slice(0,16)}const it=i("ed25519"),nt=n("secp256k1");function st(t){return G.sha512().update(t).digest().slice(0,32)}function ht(t){return(new c).update(t).digest().slice(0,32)}function ot(e="jingtum"){const r=u(e),i=r.guomi,n=r.token,l=i?ht:st,p={deriveKeypair:(t,e={})=>{const r="00"+Z(t,e).toString(16,64).toUpperCase(),i=d(nt.keyFromPrivate(r.slice(2)).getPublic().encodeCompressed());return{privateKey:r,publicKey:i}},deriveKeypairWithPrivateKey:t=>{const e="00"+t.toUpperCase(),r=d(nt.keyFromPrivate(e.slice(2)).getPublic().encodeCompressed());return{privateKey:e,publicKey:r}},sign:(t,e)=>d(nt.sign(l(t),a(e),{canonical:!0}).toDER()),verify:(t,e,r)=>nt.verify(l(t),e,a(r)),signTx:(t,e)=>d(nt.sign(t,a(e),{canonical:!0}).toDER()),verifyTx:(t,e,r)=>nt.verify(t,e,a(r))},g={deriveKeypair:(t,e={})=>{const r=function(t,e={}){const r=e.validator,i=s.curve.n,n=tt(t);if(r)return n;const h=s.curve.g.mul(n),o=e.accountIndex||0;return tt(h.encodeCompressed(),o).add(n).mod(i)}(t,e);return{privateKey:"00"+r.toString(16,64).toUpperCase(),publicKey:d(s.SM2KeyPair(null,r).pub.encodeCompressed())}},deriveKeypairWithPrivateKey:t=>{const e=new o(t,"hex");return{privateKey:"00"+t.toUpperCase(),publicKey:d(s.SM2KeyPair(null,e).pub.encodeCompressed())}},sign:(t,e)=>{const r=s.SM2KeyPair(null,new o(e,"hex"));return d(new h(r.sign(l(t))).toDER())},verify:(t,e,r)=>{const i=s.SM2KeyPair(r),n=new h(e,"hex");return i.verify(l(t),n.r,n.s)},signTx:(t,e)=>{"string"==typeof t&&/^[0-9A-F]{16}/i.test(t)&&(t=a(t));const r=s.SM2KeyPair(null,new o(e,"hex"));return d(new h(r.sign(t)).toDER())},verifyTx:(t,e,r)=>{"string"==typeof t&&/^[0-9A-F]{16}/i.test(t)&&(t=a(t));const i=s.SM2KeyPair(r),n=new h(e,"hex");return i.verify(l(t),n.r,n.s)}},v={deriveKeypair:t=>{const e=l(t);return{privateKey:"ED"+d(e),publicKey:"ED"+d(it.keyFromSecret(e).pubBytes())}},deriveKeypairWithPrivateKey:t=>({privateKey:"ED"+t.toUpperCase(),publicKey:"ED"+d(it.keyFromSecret(a(t)).pubBytes())}),sign:(t,e)=>(f(Array.isArray(t),"message must be array of octets"),d(it.sign(t,a(e).slice(1)).toBytes())),verify:(t,e,r)=>it.verify(t,a(e),a(r).slice(1)),signTx:(t,e)=>it.sign(t,a(e).slice(1)).toHex(),verifyTx:(t,e,r)=>it.verify(t,a(e),a(r).slice(1))},y={deriveKeypair:t=>{const e=l(t);return{privateKey:"ED"+d(e),publicKey:"ED"+d(it.keyFromSecret(e).pubBytes())}},deriveKeypairWithPrivateKey:t=>({privateKey:"ED"+t.toUpperCase(),publicKey:"ED"+d(it.keyFromSecret(a(t)).pubBytes())}),sign:(t,e)=>(f(Array.isArray(t),"message must be array of octets"),d(it.sign(t,a(e).slice(1)).toBytes())),verify:(t,e,r)=>it.verify(t,a(e),a(r).slice(1)),signTx:(t,e)=>it.sign(t,a(e).slice(1)).toHex(),verifyTx:(t,e,r)=>it.verify(t,a(e),a(r).slice(1))};function m(t){const e={secp256k1:p,sm2p256v1:g,ed25519:v,ed25519guomi:y};return"ed25519"===t?i?e.ed25519guomi:e.ed25519:e[t]}function _(t){const e=a(t);return 33===e.length&&237===e[0]?"ed25519":i?"sm2p256v1":"secp256k1"}function b(t,e=(i?"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=r.decodeSeed(n);h=m(e="ed25519"===t.type?"ed25519":i?"sm2p256v1":"secp256k1"),s=h.deriveKeypair(t.bytes)}else{if(64===t.length)e="ed25519"===e?"ed25519":i?"sm2p256v1":"secp256k1";else{if(66!==t.length)throw new Error("deriving keypair requires valid private key");if("00"===t.slice(0,2))e=i?"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=l("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 k(t){return r.encodeAccountID(Buffer.from(i?function(t){const e=(new c).update(t).digest();return G.ripemd160().update(e).digest()}(t):function(t){const e=G.sha256().update(t).digest();return G.ripemd160().update(e).digest()}(t)))}return{guomi:i,token:n,addressCodec:r,deriveKeyPair:b,hash:l,seedFromPhrase:i?rt:et,signTx:function(t,e){return m(_(e)).signTx(t,e)},verifyTx:function(t,e,r){return m(_(r)).verifyTx(t,e,r)},convertAddressToBytes:function(t){return r.decodeAddress(t).toJSON().data},convertBytesToAddress:function(t){return r.encodeAddress(t)},checkAddress:r.isValidAddress,isValidAddress:r.isValidAddress,deriveKeypair:b,generateSeed:function(e={}){f(!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":i?"sm2p256v1":"secp256k1";return r.encodeSeed(n,s)},sign:function(t,e){return m(_(e)).sign(a(t),e)},verify:function(t,e,r){return m(_(r)).verify(a(t),e,r)},deriveAddress:function(t){return k(a(t))},deriveNodeAddress:function(t){const e=r.decodeNodePublic(t);return k(i?function(t){const e=s.curve.decodePoint(t),r=tt(t,0),i=s.curve.g.mul(r);return e.add(i).encodeCompressed()}(e):$(e))}}}const ut=ot(),ct=ot("guomi");export{ot as Factory,ut as Keypairs,ct as KeypairsGm};
//# sourceMappingURL=index.js.map