Initial commit
This commit is contained in:
9 files changed
+2978
No files matched your search
@@ -0,0 +1 @@
|
||||
node_modules/
|
||||
Vendored
+2
@@ -0,0 +1,2 @@
|
||||
/*! For license information please see bundle.js.LICENSE.txt */
|
||||
(()=>{var __webpack_modules__={"./index.js":(__unused_webpack_module,__webpack_exports__,__webpack_require__)=>{"use strict";eval("__webpack_require__.r(__webpack_exports__);\n/* harmony import */ var _roadhog_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./roadhog.js */ \"./roadhog.js\");\n\n\nconst endpoint = \"http://localhost:3000\";\n\nasync function signIn(type) {\n try {\n const result = await (0,_roadhog_js__WEBPACK_IMPORTED_MODULE_0__.signIn)(type);\n if (result.success) {\n console.log('Successfully signed in with', type);\n const b = document.createElement('button');\n b.innerText = 'Click to access protected';\n b.setAttribute('onclick', 'fetchProtected()');\n document.body.appendChild(b);\n } else {\n console.error('Sign-in failed:', result.error);\n }\n } catch (error) {\n console.error('Sign-in error:', error);\n }\n}\n\nasync function signOff() {\n\n}\n\nasync function fetchProtected() {\n const response = await fetch(`${endpoint}/protected`, {\n method: 'GET',\n headers: {\n 'Authorization': `Bearer ${localStorage.getItem('auth_token')}`\n }\n });\n if (!response.ok) {\n console.error('error fetching /protected:', response);\n } else {\n const { message, address, type } = await response.json();\n console.log('success fetching /protected:', `you are ${address} from ${type} and CCIR says ${message}.`);\n }\n}\n\nwindow.signIn = signIn;\nwindow.fetchProtected = fetchProtected;\nwindow.signOff = signOff;\n\nfunction init() {\n console.log('start');\n if (!window.ethereum) console.log('ethereum not detected');\n if (!window.solana) console.log('solana not detected');\n}\n\nwindow.addEventListener('load', init);\n\n\n//# sourceURL=webpack:///./index.js?")},"./node_modules/.pnpm/@noble+curves@1.2.0/node_modules/@noble/curves/_shortw_utils.js":(__unused_webpack_module,exports,__webpack_require__)=>{"use strict";eval('\nObject.defineProperty(exports, "__esModule", ({ value: true }));\nexports.createCurve = exports.getHash = void 0;\n/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */\nconst hmac_1 = __webpack_require__(/*! @noble/hashes/hmac */ "./node_modules/.pnpm/@noble+hashes@1.3.2/node_modules/@noble/hashes/hmac.js");\nconst utils_1 = __webpack_require__(/*! @noble/hashes/utils */ "./node_modules/.pnpm/@noble+hashes@1.3.2/node_modules/@noble/hashes/utils.js");\nconst weierstrass_js_1 = __webpack_require__(/*! ./abstract/weierstrass.js */ "./node_modules/.pnpm/@noble+curves@1.2.0/node_modules/@noble/curves/abstract/weierstrass.js");\n// connects noble-curves to noble-hashes\nfunction getHash(hash) {\n return {\n hash,\n hmac: (key, ...msgs) => (0, hmac_1.hmac)(hash, key, (0, utils_1.concatBytes)(...msgs)),\n randomBytes: utils_1.randomBytes,\n };\n}\nexports.getHash = getHash;\nfunction createCurve(curveDef, defHash) {\n const create = (hash) => (0, weierstrass_js_1.weierstrass)({ ...curveDef, ...getHash(hash) });\n return Object.freeze({ ...create(defHash), create });\n}\nexports.createCurve = createCurve;\n//# sourceMappingURL=_shortw_utils.js.map\n\n//# sourceURL=webpack:///./node_modules/.pnpm/@noble+curves@1.2.0/node_modules/@noble/curves/_shortw_utils.js?')},"./node_modules/.pnpm/@noble+curves@1.2.0/node_modules/@noble/curves/abstract/curve.js":(__unused_webpack_module,exports,__webpack_require__)=>{"use strict";eval("\nObject.defineProperty(exports, \"__esModule\", ({ value: true }));\nexports.validateBasic = exports.wNAF = void 0;\n/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */\n// Abelian group utilities\nconst modular_js_1 = __webpack_require__(/*! ./modular.js */ \"./node_modules/.pnpm/@noble+curves@1.2.0/node_modules/@noble/curves/abstract/modular.js\");\nconst utils_js_1 = __webpack_require__(/*! ./utils.js */ \"./node_modules/.pnpm/@noble+curves@1.2.0/node_modules/@noble/curves/abstract/utils.js\");\nconst _0n = BigInt(0);\nconst _1n = BigInt(1);\n// Elliptic curve multiplication of Point by scalar. Fragile.\n// Scalars should always be less than curve order: this should be checked inside of a curve itself.\n// Creates precomputation tables for fast multiplication:\n// - private scalar is split by fixed size windows of W bits\n// - every window point is collected from window's table & added to accumulator\n// - since windows are different, same point inside tables won't be accessed more than once per calc\n// - each multiplication is 'Math.ceil(CURVE_ORDER / 𝑊) + 1' point additions (fixed for any scalar)\n// - +1 window is neccessary for wNAF\n// - wNAF reduces table size: 2x less memory + 2x faster generation, but 10% slower multiplication\n// TODO: Research returning 2d JS array of windows, instead of a single window. This would allow\n// windows to be in different memory locations\nfunction wNAF(c, bits) {\n const constTimeNegate = (condition, item) => {\n const neg = item.negate();\n return co
Line truncated
|
||||
Vendored
+1291
File diff suppressed because it is too large.
Load diff
+21
@@ -0,0 +1,21 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
|
||||
<head>
|
||||
<title>ZKL Roadhog Demo</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<script src="dist/bundle.js"></script>
|
||||
<script>
|
||||
window.addEventListener('load', function () {
|
||||
document.getElementById('metamask').disabled = false;
|
||||
document.getElementById('phantom').disabled = false;
|
||||
});
|
||||
</script>
|
||||
<h1>ZKL Roadhog Demo</h1>
|
||||
<button disabled id="metamask" onclick="signIn('ethereum')">sign in with metamask</button>
|
||||
<button disabled id="phantom" onclick="signIn('solana')">sign in with solana</button>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
@@ -0,0 +1,35 @@
|
||||
import { signIn as _signIn } from './roadhog.js';
|
||||
|
||||
const endpoint = "http://localhost:3000";
|
||||
|
||||
async function signIn(type) {
|
||||
try {
|
||||
const result = await _signIn(type);
|
||||
if (result.success) {
|
||||
console.log('Successfully signed in with', type);
|
||||
const b = document.createElement('button');
|
||||
b.innerText = 'Click to access protected';
|
||||
b.setAttribute('onclick', 'fetchProtected()');
|
||||
document.body.appendChild(b);
|
||||
} else {
|
||||
console.error('Sign-in failed:', result.error);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Sign-in error:', error);
|
||||
}
|
||||
}
|
||||
|
||||
async function signOff() {
|
||||
|
||||
}
|
||||
|
||||
window.signIn = signIn;
|
||||
window.signOff = signOff;
|
||||
|
||||
function init() {
|
||||
console.log('start');
|
||||
if (!window.ethereum) console.log('ethereum not detected');
|
||||
if (!window.solana) console.log('solana not detected');
|
||||
}
|
||||
|
||||
window.addEventListener('load', init);
|
||||
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"devDependencies": {
|
||||
"terser-webpack-plugin": "^5.3.10",
|
||||
"webpack": "^5.95.0",
|
||||
"webpack-cli": "^5.1.4"
|
||||
},
|
||||
"dependencies": {
|
||||
"@solana/web3.js": "^1.95.3",
|
||||
"bs58": "^6.0.0",
|
||||
"ethers": "^6.13.3",
|
||||
"siwe": "^2.3.2"
|
||||
}
|
||||
}
|
||||
Generated
+1502
File diff suppressed because it is too large.
Load diff
+88
@@ -0,0 +1,88 @@
|
||||
import { SiweMessage } from 'siwe';
|
||||
import { BrowserProvider, JsonRpcSigner } from 'ethers';
|
||||
import * as solanaWeb3 from '@solana/web3.js';
|
||||
import bs58 from 'bs58';
|
||||
|
||||
const endpoint = 'http://localhost:3000';
|
||||
|
||||
export async function signIn(type) {
|
||||
let address, signature, message;
|
||||
|
||||
if (type === 'ethereum') {
|
||||
await window.ethereum.request({ method: 'eth_requestAccounts' });
|
||||
const provider = new BrowserProvider(window.ethereum);
|
||||
const signer = await provider.getSigner();
|
||||
address = await signer.getAddress();
|
||||
|
||||
const nonceResponse = await fetch(`${endpoint}/auth/nonce?address=${address}&type=ethereum`);
|
||||
const { nonce } = await nonceResponse.json();
|
||||
|
||||
const siweMessage = new SiweMessage({
|
||||
domain: window.location.host,
|
||||
address: address,
|
||||
statement: 'Sign in with Ethereum to zk-Lokomotive.',
|
||||
uri: window.location.origin,
|
||||
version: '1',
|
||||
chainId: 1,
|
||||
nonce: nonce
|
||||
});
|
||||
|
||||
message = siweMessage.prepareMessage();
|
||||
signature = await signer.signMessage(message);
|
||||
} else if (type === 'solana') {
|
||||
const provider = window.solana;
|
||||
await provider.connect();
|
||||
address = provider.publicKey.toString();
|
||||
|
||||
const nonceResponse = await fetch(`${endpoint}/auth/nonce?address=${address}&type=solana`);
|
||||
const { nonce } = await nonceResponse.json();
|
||||
|
||||
const encodedMessage = new TextEncoder().encode(nonce);
|
||||
const signatureBytes = await provider.signMessage(encodedMessage, 'utf8');
|
||||
signature = bs58.encode(signatureBytes.signature);
|
||||
} else {
|
||||
throw new Error('Invalid type');
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch(`${endpoint}/auth/verify`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({ type, message, address, signature }),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error((await response.json()).error);
|
||||
}
|
||||
|
||||
const { auth_token } = await response.json();
|
||||
|
||||
localStorage.setItem('auth_token', auth_token);
|
||||
localStorage.setItem('auth_type', type);
|
||||
|
||||
return { success: true, auth_token };
|
||||
} catch (error) {
|
||||
console.error('Authentication error:', error);
|
||||
return { success: false, error: error.message };
|
||||
}
|
||||
}
|
||||
|
||||
export async function signOff() {
|
||||
const auth_token = localStorage.getItem('auth_token');
|
||||
const response = await fetch(`${endpoint}/auth/signoff`, {
|
||||
method: 'DELETE',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': `Bearer ${auth_token}`
|
||||
}
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error((await response.json()).error);
|
||||
} else {
|
||||
localStorage.removeItem('auth_token');
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
const path = require("path");
|
||||
const webpack = require("webpack");
|
||||
const TerserPlugin = require("terser-webpack-plugin");
|
||||
|
||||
module.exports = {
|
||||
entry: "./index.js",
|
||||
output: {
|
||||
filename: "bundle.js",
|
||||
path: path.resolve(__dirname, "dist")
|
||||
},
|
||||
plugins: [
|
||||
new TerserPlugin({
|
||||
terserOptions: {
|
||||
keep_fnames: ['signIn', 'fetchProtected'],
|
||||
mangle: {
|
||||
reserved: [
|
||||
"signIn",
|
||||
"fetchProtected"
|
||||
]
|
||||
}
|
||||
}
|
||||
})
|
||||
],
|
||||
mode: "development"
|
||||
};
|
||||
Reference in new issue
Block a user