]> git.ardis.lu - erc-165-calculator/commitdiff
Initial commit
authorArdis Lu <ardisglu@gmail.com>
Sat, 19 Aug 2023 06:15:01 +0000 (23:15 -0700)
committerArdis Lu <ardisglu@gmail.com>
Sat, 19 Aug 2023 06:15:01 +0000 (23:15 -0700)
README.md [new file with mode: 0644]
index.html [new file with mode: 0644]
keccak256.js [new file with mode: 0644]

diff --git a/README.md b/README.md
new file mode 100644 (file)
index 0000000..366004c
--- /dev/null
+++ b/README.md
@@ -0,0 +1,3 @@
+# ERC-165 Calculator
+
+Minimal web app to calculate [ERC-165](https://eips.ethereum.org/EIPS/eip-165) interface IDs from Solidity functions.
diff --git a/index.html b/index.html
new file mode 100644 (file)
index 0000000..8862a43
--- /dev/null
@@ -0,0 +1,60 @@
+<!DOCTYPE html>
+<html lang="en">
+
+<head>
+  <meta charset="UTF-8">
+  <meta name="viewport" content="width=device-width, initial-scale=1.0">
+  <link rel="icon" href="data:,">
+  <title>ERC-165 Calculator</title>
+  <style>
+    body {
+      font-family: -apple-system, BlinkMacSystemFont, avenir next, avenir, segoe ui, helvetica neue, helvetica, Cantarell, Ubuntu, roboto, noto, arial, sans-serif;
+      background: hsl(216deg 50% 90%);
+      color: hsl(216deg 50% 10%);
+    }
+  </style>
+</head>
+
+<body>
+  <h1>ERC-165 Calculator</h1>
+
+  <form>
+    <label for="functions">Functions</label>
+    <textarea name="functions" id="functions"></textarea>
+
+    <button>Calculate</button>
+
+    <output></output>
+  </form>
+
+  <script type="module">
+    // From noble-hashes v1.3.1
+    import { keccak256 } from '/keccak256.js';
+
+    document.querySelector('form').addEventListener('submit', e => {
+      e.preventDefault();
+      const functions = new FormData(e.target).get('functions');
+      const hashes = [];
+      for (const fn of functions.split('\n')) {
+        const functionRegex = /^(function )?(?<name>[a-zA-Z$_][a-zA-Z0-9$_]*)\((?<parameters>.*?)\)/;
+        const { name, parameters } = functionRegex.exec(fn).groups;
+        const parametersTypes = parameters.split(',').map(param => param.trim().split(' ')[0]).join(',');
+        const normalizedName = `${name}(${parametersTypes})`;
+        const byteArray = keccak256(new TextEncoder().encode(normalizedName)).slice(0, 4);
+        hashes.push(byteArray);
+      }
+
+      const xor = Uint8Array.from([0, 0, 0, 0]);
+      for (const hash of hashes) {
+        for (const [index, byte] of hash.entries()) {
+          xor[index] ^= byte;
+        }
+      }
+
+      const xorHash = `0x${[...xor].map(b => b.toString(16).padStart(2, '0')).join('')}`;
+      console.log(xorHash);
+    });
+  </script>
+</body>
+
+</html>
\ No newline at end of file
diff --git a/keccak256.js b/keccak256.js
new file mode 100644 (file)
index 0000000..0918a76
--- /dev/null
@@ -0,0 +1,6 @@
+function k(t){if(!Number.isSafeInteger(t)||t<0)throw new Error(`Wrong positive integer: ${t}`)}function F(t){if(typeof t!="boolean")throw new Error(`Expected boolean, not ${t}`)}function m(t,...e){if(!(t instanceof Uint8Array))throw new Error("Expected Uint8Array");if(e.length>0&&!e.includes(t.length))throw new Error(`Expected Uint8Array of length ${e}, not of length=${t.length}`)}function X(t){if(typeof t!="function"||typeof t.create!="function")throw new Error("Hash should be wrapped by utils.wrapConstructor");k(t.outputLen),k(t.blockLen)}function $(t,e=!0){if(t.destroyed)throw new Error("Hash instance has been destroyed");if(e&&t.finished)throw new Error("Hash#digest() has already been called")}function R(t,e){m(t);let o=e.outputLen;if(t.length<o)throw new Error(`digestInto() expects output buffer of length at least ${o}`)}var V={number:k,bool:F,bytes:m,hash:X,exists:$,output:R},p=V;var w=BigInt(4294967295),L=BigInt(32);function O(t,e=!1){return e?{h:Number(t&w),l:Number(t>>L&w)}:{h:Number(t>>L&w)|0,l:Number(t&w)|0}}function M(t,e=!1){let o=new Uint32Array(t.length),n=new Uint32Array(t.length);for(let r=0;r<t.length;r++){let{h:i,l:s}=O(t[r],e);[o[r],n[r]]=[i,s]}return[o,n]}var P=(t,e)=>BigInt(t>>>0)<<L|BigInt(e>>>0),W=(t,e,o)=>t>>>o,v=(t,e,o)=>t<<32-o|e>>>o,C=(t,e,o)=>t>>>o|e<<32-o,D=(t,e,o)=>t<<32-o|e>>>o,q=(t,e,o)=>t<<64-o|e>>>o-32,z=(t,e,o)=>t>>>o-32|e<<64-o,G=(t,e)=>e,J=(t,e)=>t,K=(t,e,o)=>t<<o|e>>>32-o,Q=(t,e,o)=>e<<o|t>>>32-o,Y=(t,e,o)=>e<<o-32|t>>>64-o,Z=(t,e,o)=>t<<o-32|e>>>64-o;function tt(t,e,o,n){let r=(e>>>0)+(n>>>0);return{h:t+o+(r/2**32|0)|0,l:r|0}}var et=(t,e,o)=>(t>>>0)+(e>>>0)+(o>>>0),ot=(t,e,o,n)=>e+o+n+(t/2**32|0)|0,nt=(t,e,o,n)=>(t>>>0)+(e>>>0)+(o>>>0)+(n>>>0),rt=(t,e,o,n,r)=>e+o+n+r+(t/2**32|0)|0,st=(t,e,o,n,r)=>(t>>>0)+(e>>>0)+(o>>>0)+(n>>>0)+(r>>>0),it=(t,e,o,n,r,i)=>e+o+n+r+i+(t/2**32|0)|0,ct={fromBig:O,split:M,toBig:P,shrSH:W,shrSL:v,rotrSH:C,rotrSL:D,rotrBH:q,rotrBL:z,rotr32H:G,rotr32L:J,rotlSH:K,rotlSL:Q,rotlBH:Y,rotlBL:Z,add:tt,add3L:et,add3H:ot,add4L:nt,add4H:rt,add5H:it,add5L:st},h=ct;var ft=typeof globalThis=="object"&&"crypto"in globalThis?globalThis.crypto:void 0;var ut=t=>t instanceof Uint8Array;var A=t=>new Uint32Array(t.buffer,t.byteOffset,Math.floor(t.byteLength/4));var pt=new Uint8Array(new Uint32Array([287454020]).buffer)[0]===68;if(!pt)throw new Error("Non little-endian hardware is not supported");var Bt=Array.from({length:256},(t,e)=>e.toString(16).padStart(2,"0"));function ht(t){if(typeof t!="string")throw new Error(`utf8ToBytes expected string, got ${typeof t}`);return new Uint8Array(new TextEncoder().encode(t))}function g(t){if(typeof t=="string"&&(t=ht(t)),!ut(t))throw new Error(`expected Uint8Array, got ${typeof t}`);return t}var y=class{clone(){return this._cloneInto()}};function B(t){let e=n=>t().update(g(n)).digest(),o=t();return e.outputLen=o.outputLen,e.blockLen=o.blockLen,e.create=()=>t(),e}function I(t){let e=(n,r)=>t(r).update(g(n)).digest(),o=t({});return e.outputLen=o.outputLen,e.blockLen=o.blockLen,e.create=n=>t(n),e}var[H,U,S]=[[],[],[]],lt=BigInt(0),l=BigInt(1),at=BigInt(2),dt=BigInt(7),xt=BigInt(256),wt=BigInt(113);for(let t=0,e=l,o=1,n=0;t<24;t++){[o,n]=[n,(2*o+3*n)%5],H.push(2*(5*n+o)),U.push((t+1)*(t+2)/2%64);let r=lt;for(let i=0;i<7;i++)e=(e<<l^(e>>dt)*wt)%xt,e&at&&(r^=l<<(l<<BigInt(i))-l);S.push(r)}var[yt,gt]=h.split(S,!0),E=(t,e,o)=>o>32?h.rotlBH(t,e,o):h.rotlSH(t,e,o),_=(t,e,o)=>o>32?h.rotlBL(t,e,o):h.rotlSL(t,e,o);function bt(t,e=24){let o=new Uint32Array(10);for(let n=24-e;n<24;n++){for(let s=0;s<10;s++)o[s]=t[s]^t[s+10]^t[s+20]^t[s+30]^t[s+40];for(let s=0;s<10;s+=2){let c=(s+8)%10,a=(s+2)%10,d=o[a],u=o[a+1],j=E(d,u,1)^o[c],N=_(d,u,1)^o[c+1];for(let x=0;x<50;x+=10)t[s+x]^=j,t[s+x+1]^=N}let r=t[2],i=t[3];for(let s=0;s<24;s++){let c=U[s],a=E(r,i,c),d=_(r,i,c),u=H[s];r=t[u],i=t[u+1],t[u]=a,t[u+1]=d}for(let s=0;s<50;s+=10){for(let c=0;c<10;c++)o[c]=t[s+c];for(let c=0;c<10;c++)t[s+c]^=~o[(c+2)%10]&o[(c+4)%10]}t[0]^=yt[n],t[1]^=gt[n]}o.fill(0)}var b=class t extends y{constructor(e,o,n,r=!1,i=24){if(super(),this.blockLen=e,this.suffix=o,this.outputLen=n,this.enableXOF=r,this.rounds=i,this.pos=0,this.posOut=0,this.finished=!1,this.destroyed=!1,p.number(n),0>=this.blockLen||this.blockLen>=200)throw new Error("Sha3 supports only keccak-f1600 function");this.state=new Uint8Array(200),this.state32=A(this.state)}keccak(){bt(this.state32,this.rounds),this.posOut=0,this.pos=0}update(e){p.exists(this);let{blockLen:o,state:n}=this;e=g(e);let r=e.length;for(let i=0;i<r;){let s=Math.min(o-this.pos,r-i);for(let c=0;c<s;c++)n[this.pos++]^=e[i++];this.pos===o&&this.keccak()}return this}finish(){if(this.finished)return;this.finished=!0;let{state:e,suffix:o,pos:n,blockLen:r}=this;e[n]^=o,o&128&&n===r-1&&this.keccak(),e[r-1]^=128,this.keccak()}writeInto(e){p.exists(this,!1),p.bytes(e),this.finish();let o=this.state,{blockLen:n}=this;for(let r=0,i=e.length;r<i;){this.posOut>=n&&this.keccak();let s=Math.min(n-this.posOut,i-r);e.set(o.subarray(this.posOut,this.posOut+s),r),this.posOut+=s,r+=s}return e}xofInto(e){if(!this.enableXOF)throw new Error("XOF is not possible for this instance");return this.writeInto(e)}xof(e){return p.number(e),this.xofInto(new Uint8Array(e))}digestInto(e){if(p.output(e,this),this.finished)throw new Error("digest() was already called");return this.writeInto(e),this.destroy(),e}digest(){return this.digestInto(new Uint8Array(this.outputLen))}destroy(){this.destroyed=!0,this.state.fill(0)}_cloneInto(e){let{blockLen:o,suffix:n,outputLen:r,rounds:i,enableXOF:s}=this;return e||(e=new t(o,n,r,s,i)),e.state32.set(this.state32),e.pos=this.pos,e.posOut=this.posOut,e.finished=this.finished,e.rounds=i,e.suffix=n,e.outputLen=r,e.enableXOF=s,e.destroyed=this.destroyed,e}},f=(t,e,o)=>B(()=>new b(e,t,o)),Ut=f(6,144,224/8),St=f(6,136,256/8),Tt=f(6,104,384/8),jt=f(6,72,512/8),Nt=f(1,144,224/8),kt=f(1,136,256/8),Ft=f(1,104,384/8),Xt=f(1,72,512/8),T=(t,e,o)=>I((n={})=>new b(e,t,n.dkLen===void 0?o:n.dkLen,!0)),$t=T(31,168,128/8),Rt=T(31,136,256/8);export{kt as keccak256};
+/*! Bundled license information:
+
+@noble/hashes/esm/utils.js:
+  (*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
+*/
This page took 0.036813 seconds and 4 git commands to generate.