@hashgraph/sdk
Version:
3 lines (2 loc) • 1.63 kB
JavaScript
import"@hashgraph/proto";class t{constructor(t){this.data=new Uint8Array(t),this.pos=0,this.oids=[],this.oidMap={"1.3.132.0.10":"ecdsa","1.3.101.112":"ed25519","1.2.840.10045.2.1":"pubkey"},this.isPublicKey=!1}readLength(){let t=this.data[this.pos++];if(128&t){let s=127&t;t=0;for(let e=0;e<s;e++)t=t<<8|this.data[this.pos++]}return t}readType(){return this.data[this.pos++]}readInteger(){const t=this.readLength();let s=0;for(let e=0;e<t;e++)s=s<<8|this.data[this.pos++];return{integer:s}}readOctetString(){const t=this.readLength(),s=this.data.slice(this.pos,this.pos+t);return this.pos+=t,{pkey:s}}readBitString(){const t=this.readLength(),s=this.data[this.pos++],e=this.data.slice(this.pos,this.pos+t-1);return this.pos+=t-1,{unusedBits:s,pubkey:e}}readObjectIdentifier(){const t=this.readLength(),s=this.pos+t,e=[];let i=0;const r=this.data[this.pos++];for(e.push(Math.floor(r/40)),e.push(r%40);this.pos<s;){const t=this.data[this.pos++];i=i<<7|127&t,128&t||(e.push(i),i=0)}const h=e.join(".");return this.oids.push(h),{oid:h}}getOids(){return this.oids}getOidKeyTypes(){return this.oids.map(t=>this.oidMap[t]||"unknown")}readSequence(){const t=this.readLength(),s=this.pos+t,e=[];for(;this.pos<s;)e.push(this.read());return e}read(){const t=this.readType();switch(t){case 2:return this.readInteger();case 3:return this.readBitString();case 4:return this.readOctetString();case 6:return this.readObjectIdentifier();case 48:case 160:case 161:return this.readSequence();default:throw new Error(`Unsupported type: ${t}`)}}isPublicKeyType(){return this.isPublicKey}}export{t as ASN1Decoder};
//# sourceMappingURL=ASN1-Decoder.js.map