@hashgraph/sdk
Version:
3 lines (2 loc) • 5.46 kB
JavaScript
import e from"long";import{isHexString as r,decode as t,encode as n}from"./encoding/hex.js";import o from"./BadEntityIdError.js";import{isLongZeroAddress as l,safeView as i,convertToNumber as s}from"./util.js";import a from"./base32.js";import*as u from"@hashgraph/proto";import c from"./PublicKey.js";import{arrayify as m}from"@ethersproject/bytes";import f from"./EvmAddress.js";const h=/"^(0|(?:[1-9]\\d*))\\.(0|(?:[1-9]\\d*))\\.(0|(?:[1-9]\\d*))(?:-([a-z]{5}))?$/,d=/^(\d+)(?:\.(\d+)\.([a-fA-F0-9]+))?(?:-([a-z]{5}))?$/;function g(r,t,n){if(null==t&&null!=n||null!=t&&null==n)throw new Error("invalid entity ID");const[o,l,i]="number"==typeof r||e.isLong(r)?[null!=n?e.fromValue(r):e.ZERO,null!=t?e.fromValue(t):e.ZERO,null!=n?e.fromValue(n):e.fromValue(r)]:[null!=r.shard?e.fromValue(r.shard):e.ZERO,null!=r.realm?e.fromValue(r.realm):e.ZERO,e.fromValue(r.num)];if(o.isNegative()||l.isNegative()||i.isNegative())throw new Error("negative numbers are not allowed in IDs");return{shard:o,realm:l,num:i}}function w(e,r){let t=e[0].compare(r[0]);return 0!=t?t:(t=e[1].compare(r[1]),0!=t?t:e[2].compare(r[2]))}function y(e){const r=d.exec(e);if(null==r)throw new Error(`failed to parse entity id: ${e}`);return null==r[2]&&null==r[3]?{shard:"0",realm:"0",numOrHex:r[1],checksum:r[4]}:{shard:r[1],realm:r[2],numOrHex:r[3],checksum:r[4]}}function p(r){const t=y(r);if(Number.isNaN(t.shard)||Number.isNaN(t.realm)||Number.isNaN(t.numOrHex))throw new Error("invalid format for entity ID");return{shard:null!=t.shard?e.fromString(t.shard):e.ZERO,realm:null!=t.realm?e.fromString(t.realm):e.ZERO,num:e.fromString(t.numOrHex),checksum:t.checksum}}function E(r){const n=r.startsWith("0x")?t(r.slice(2)):t(r);if(20!==n.length)throw new Error(`Invalid hex encoded solidity address length:\n expected length 40, got length ${r.length}`);return[e.fromBytesBE([0,0,0,0,...n.slice(0,4)]),e.fromBytesBE(Array.from(n.slice(4,12))),e.fromBytesBE(Array.from(n.slice(12,20)))]}function S(n,o,i){if(!r(i))throw new Error(`Invalid EVM address hex string: ${i}`);const s=i.startsWith("0x")?t(i.slice(2)):t(i);if(20!==s.length)throw new Error(`Invalid hex encoded evm address length:\n expected length 20, got length ${i.length}`);let a=e.ZERO;return l(s)&&(a=e.fromBytesBE(Array.from(s.slice(12,20)))),[n instanceof e?n:e.fromNumber(n),o instanceof e?o:e.fromNumber(o),a,a.isZero()?f.fromBytes(s):null]}function x(e){const r=new Uint8Array(20),t=i(r),[o,l,a]=e;return t.setUint32(0,s(o)),t.setUint32(8,s(l)),t.setUint32(16,s(a)),n(r)}function k(e){if(e instanceof Uint8Array)return n(e);const r=e,t=new Uint8Array(20);return i(t).setUint32(16,s(r)),n(t)}function A(r,t){let n=h.exec(t);if(null===n){return{status:0}}let o=[e.fromString(n[1]),e.fromString(n[2]),e.fromString(n[3])],l=`${o[0].toString()}.${o[1].toString()}.${o[2].toString()}`,i=$(r,l);return{status:void 0===n[4]?2:i==n[4]?3:1,num1:o[0],num2:o[1],num3:o[2],givenChecksum:n[4],correctChecksum:i,noChecksumFormat:l,withChecksumFormat:`${l}-${i}`}}function $(e,r){let t="",n=[],o=0,l=0,i=0,s=0,a=0;const u=11881376,c="a".charCodeAt(0);let m=new Uint8Array(e.length+6);m.set(e,0),m.set([0,0,0,0,0,0],e.length);for(let e=0;e<r.length;e++)n.push("."===r[e]?10:parseInt(r[e],10));for(let e=0;e<n.length;e++)i=(31*i+n[e])%17576,e%2==0?o=(o+n[e])%11:l=(l+n[e])%11;for(let e=0;e<m.length;e++)s=(31*s+m[e])%u;a=(17576*(11*(r.length%5*11+o)+l)+i+s)%u,a=1000003*a%u;for(let e=0;e<5;e++)t=String.fromCharCode(c+a%26)+t,a/=26;return t}function b(e,r,t,n,l){if(null==l._network._ledgerId||null==n)return;const i=$(l._network._ledgerId._ledgerId,`${e.toString()}.${r.toString()}.${t.toString()}`);if(n!=i)throw new o(e,r,t,n,i)}function v(e,r){if(null==r)throw new Error("client cannot be null");if(null==r._network._ledgerId)throw new Error("cannot calculate checksum with a client that does not contain a recognzied ledger ID");return`${e}-${$(r._network._ledgerId._ledgerId,e)}`}function I(e){const r=a.decode(e);if(!r)return null;let t;try{t=u.proto.Key.decode(r)}catch(e){throw new Error("The alias is created with hollow account. Please use aliasToEvmAddress!")}return null!=t.ed25519&&t.ed25519.byteLength>0?c.fromBytes(t.ed25519):null!=t.ECDSASecp256k1&&t.ECDSASecp256k1.byteLength>0?c.fromBytes(t.ECDSASecp256k1):null}function N(e){const r=a.decode(e);if(!r)return null;try{throw u.proto.Key.decode(r),new Error("The alias is created with ed25519 or ECDSASecp256k1 account. Please use aliasToPublicKey!")}catch(e){return function(e){var r="0x";return e.forEach(function(e){r+=("0"+(255&e).toString(16)).slice(-2)}),r}(r)}}function _(e){if("string"==typeof e&&(e.startsWith("0x")&&42==e.length||40==e.length)){e.startsWith("0x")||(e=`0x${e}`);const r=m(e);return r?a.encode(r):null}const r="string"==typeof e?c.fromString(e):e;let t=r.toStringRaw(),n="";"secp256k1"===r._key._type&&(n="0x3A21"),"ED25519"===r._key._type&&(n="0x1220"),t.startsWith("0x")||(t=`0x${t}`);const o=m(n),l=m(t),i=(s=o,u=l,(f=new Uint8Array(s.byteLength+u.byteLength)).set(new Uint8Array(s),0),f.set(new Uint8Array(u),s.byteLength),f);var s,u,f;return a.encode(i)}export{$ as _checksum,A as _parseAddress,N as aliasToEvmAddress,I as aliasToPublicKey,w as compare,g as constructor,S as fromEvmAddress,E as fromSolidityAddress,p as fromString,y as fromStringSplitter,_ as publicKeyToAlias,k as toEvmAddress,x as toSolidityAddress,v as toStringWithChecksum,b as validateChecksum};
//# sourceMappingURL=EntityIdHelper.js.map