intergrate with new interep

This commit is contained in:
0xtsukino
2022-02-25 11:53:00 -08:00
parent 262af2cdca
commit 75d85a78ce
9 changed files with 6272 additions and 6940 deletions

12905
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -6,8 +6,6 @@
"scripts": {
"dev-ui": "webpack-dev-server --config webpack.ui.config.js --hot --inline",
"build-ui": "webpack --config webpack.ui.config.js",
"predev": "cp ./static/bn128.js ./node_modules/websnark/src/bn128.js & cp ./static/main.cjs ./node_modules/ffjavascript/build/main.cjs",
"prebuild": "cp ./static/bn128.js ./node_modules/websnark/src/bn128.js & cp ./static/main.cjs ./node_modules/ffjavascript/build/main.cjs",
"dev": "NODE_ENV=development concurrently --kill-others-on-fail npm:dev-ui",
"build": "NODE_ENV=production npm run build-ui"
},
@@ -17,13 +15,12 @@
"@draft-js-plugins/linkify": "^4.1.1",
"@draft-js-plugins/mention": "^4.6.1",
"@ensdomains/ensjs": "^2.0.1",
"@interrep/semethid": "^0.10.0",
"@libsem/identity": "^1.0.17",
"@libsem/types": "^1.0.5",
"@libsem/protocols": "^1.0.22",
"@interep/identity": "^0.1.1",
"@snapshot-labs/snapshot.js": "^0.2.0",
"@types/crypto-js": "^4.0.2",
"@types/react-qr-reader": "^2.1.4",
"@zk-kit/identity": "^1.4.1",
"@zk-kit/protocols": "^1.8.2",
"bn.js": "^5.2.0",
"classnames": "^2.3.1",
"crypto-js": "^4.1.1",
@@ -39,8 +36,6 @@
"lodash.debounce": "^4.0.8",
"markdown-draft-js": "^2.3.0",
"moment": "^2.29.1",
"passport": "^0.5.0",
"passport-twitter": "^1.0.4",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-qr-code": "^2.0.2",
@@ -54,10 +49,8 @@
"redux-thunk": "^2.3.0",
"remarkable": "^2.0.1",
"semaphore-lib": "git+https://github.com/akinovak/semaphore-lib.git#dev",
"snarkjs": "^0.4.10",
"web3": "^1.5.1",
"web3modal": "^1.9.4",
"websnark": "0.0.5"
"web3modal": "^1.9.4"
},
"devDependencies": {
"@types/classnames": "^2.2.11",

View File

@@ -1,5 +1,15 @@
import {useSelector} from "react-redux";
import {ZkIdentity} from "@libsem/identity";
import {
Semaphore,
genExternalNullifier,
MerkleProof,
SemaphoreFullProof,
SemaphoreSolidityProof,
SemaphorePublicSignals,
genSignalHash,
generateMerkleProof,
} from '@zk-kit/protocols';
import {Strategy, ZkIdentity} from '@zk-kit/identity'
import {AppRootState} from "../store/configureAppStore";
import deepEqual from "fast-deep-equal";
import {convertToRaw, EditorState} from "draft-js";
@@ -16,18 +26,12 @@ import {
ProfileMessageSubType
} from "../util/message";
import gun from "../util/gun";
import {OrdinarySemaphore} from "semaphore-lib";
import {ThunkDispatch} from "redux-thunk";
import {markdownConvertOptions} from "../components/DraftEditor";
import config from "../util/config";
import {setFollowed} from "./users";
import {genSemaphoreProof} from "../util/crypto";
import {updateStatus} from "../util/twitter";
const { Semaphore, genExternalNullifier, genSignalHash } = require("@libsem/protocols");
OrdinarySemaphore.setHasher('poseidon');
const { draftToMarkdown } = require('markdown-draft-js');
enum ActionTypes {
@@ -88,8 +92,9 @@ export const submitSemaphorePost = (post: Post) => async (dispatch: Dispatch, ge
if (selected?.type !== 'interrep') throw new Error('Not in incognito mode');
const zkIdentity = ZkIdentity.genFromSerialized(selected.serializedIdentity);
const {identityTrapdoor, identityNullifier} = zkIdentity.getIdentity();
const zkIdentity = new ZkIdentity(Strategy.SERIALIZED, selected.serializedIdentity);
const identityTrapdoor = zkIdentity.getTrapdoor();
const identityNullifier = zkIdentity.getNullifier();
const identityCommitment = selected.identityCommitment;
const identityPathElements = selected.identityPath?.path_elements;
const identityPathIndex = selected.identityPath?.path_index;
@@ -110,21 +115,22 @@ export const submitSemaphorePost = (post: Post) => async (dispatch: Dispatch, ge
const finalZkeyPath = `${config.indexerAPI}/dev/semaphore_final_zkey`;
const witness = Semaphore.genWitness(
zkIdentity,
identityTrapdoor,
identityNullifier,
{
root: root,
indices: identityPathIndex,
pathElements: identityPathElements,
leaf: BigInt(identityCommitment),
pathIndices: identityPathIndex,
siblings: identityPathElements,
},
externalNullifier,
hash,
true
);
const {
proof,
publicSignals,
} = await genSemaphoreProof(witness, wasmFilePath, finalZkeyPath);
} = await Semaphore.genProof(witness, wasmFilePath, finalZkeyPath);
try {
// @ts-ignore

View File

@@ -3,22 +3,19 @@ import {useSelector} from "react-redux";
import deepEqual from "fast-deep-equal";
import {AppRootState} from "../store/configureAppStore";
import {Subscription} from "web3-core-subscriptions";
import {Identity} from 'libsemaphore';
import {OrdinarySemaphore,} from "semaphore-lib";
import {ThunkDispatch} from "redux-thunk";
import {Dispatch} from "redux";
import Web3Modal from "web3modal";
import {generateGunKeyPairFromHex, validateGunPublicKey,} from "../util/crypto";
import {defaultENS, defaultWeb3, fetchNameByAddress} from "../util/web3";
import {generateGunKeyPairFromHex} from "../util/crypto";
import {defaultWeb3, fetchNameByAddress} from "../util/web3";
import gun, {authenticateGun} from "../util/gun";
import semethid from "@interrep/semethid";
import createIdentity from "@interep/identity"
import config from "../util/config";
import {getUser} from "./users";
import {getIdentityHash} from "../util/arb3";
import {postWorkerMessage} from "../util/sw";
import {setIdentity} from "../serviceWorkers/util";
OrdinarySemaphore.setHasher('poseidon');
import {checkPath} from "../util/interrep";
export const web3Modal = new Web3Modal({
network: "main", // optional
@@ -352,35 +349,22 @@ export const genSemaphore = (web2Provider: 'Twitter' | 'Github' | 'Reddit' = 'Tw
const { web3: { account }} = getState();
const result: any = await dispatch(generateSemaphoreID(web2Provider, nonce));
const commitment = await result.genIdentityCommitment();
const resp = await fetch(`${config.indexerAPI}/interrep/${commitment.toString()}`);
const { payload: {data, name}, error } = await resp.json();
let path = null;
let groupName = '';
if (!error && data) {
path = {
path_elements: data.siblingNodes,
path_index: data.path,
root: data.root,
};
groupName = name;
}
const data: any = await checkPath(commitment.toString());
postWorkerMessage(setIdentity({
type: 'interrep',
address: account,
nonce: nonce,
provider: web2Provider,
name: groupName,
identityPath: path,
name: data?.name,
identityPath: data?.path,
identityCommitment: commitment.toString(),
serializedIdentity: result.serializeIdentity(),
}))
dispatch(setUnlocking(false));
return !!path;
return !!data?.path;
} catch (e) {
dispatch(setUnlocking(false));
throw e;
@@ -467,7 +451,7 @@ const generateSemaphoreID = (
return Promise.reject(new Error('not connected to web3'));
}
const identity = await semethid(
const identity = await createIdentity(
// @ts-ignore
(message: string) => web3.eth.personal.sign(message, account),
web2Provider,

View File

@@ -1,9 +1,9 @@
import EC from "elliptic";
import {genPubKey, Identity} from 'libsemaphore';
import {IProof, IWitnessData} from "semaphore-lib/src/index";
import {builder} from "./witness_calculator";
const snarkjs = require('snarkjs');
const { groth16 } = snarkjs;
// import {genPubKey, Identity} from 'libsemaphore';
// import {IProof, IWitnessData} from "semaphore-lib/src/index";
// import {builder} from "./witness_calculator";
// const snarkjs = require('snarkjs');
// const { groth16 } = snarkjs;
export const hexToUintArray = (hex: string): Uint8Array => {
const a = [];
@@ -59,34 +59,6 @@ export const arrayBufToBase64UrlEncode = (buf: ArrayBuffer) => {
// y: arrayBufToBase64UrlEncode(hexToArrayBuf(pubHex).slice(33, 66))
// });
export const validateGunPublicKey = async (pub: string) => {
const x = pub.split('.')[0];
const y = pub.split('.')[1];
if (x.length !== 43 || y.length !== 43) {
return false;
}
await crypto.subtle.importKey(
'jwk',
{
kty: "EC",
crv: "P-256",
ext: true,
x: pub.split('.')[0],
y: pub.split('.')[1],
},
{
name: "ECDSA",
namedCurve: "P-256",
},
true,
["verify"]
);
return true;
};
export const generateGunKeyPairFromHex = async (hashHex: string): Promise<{pub: string; priv: string}> => {
const ec = new EC.ec('p256');
const key = ec.keyFromPrivate(hashHex);
@@ -100,51 +72,25 @@ export const generateGunKeyPairFromHex = async (hashHex: string): Promise<{pub:
};
}
export const generateSemaphoreIDFromHex = async (hashHex: string) => {
const privKey = Buffer.from(hashHex, 'hex');
const pubKey = genPubKey(privKey);
const identityNullifierSeed = await crypto.subtle.digest(
'SHA-256',
new TextEncoder().encode(hashHex + 'identity_nullifier'),
);
const identityTrapdoorSeed = await crypto.subtle.digest(
'SHA-256',
new TextEncoder().encode(hashHex + 'identity_trapdoor'),
);
const identityNullifierSeedBuf = Buffer.from(identityNullifierSeed);
const identityTrapdoorSeedBuf = Buffer.from(identityTrapdoorSeed);
return {
keypair: {
pubKey,
privKey,
},
identityNullifier: snarkjs.bigInt.leBuff2int(identityNullifierSeedBuf.slice(0, 31)),
identityTrapdoor: snarkjs.bigInt.leBuff2int(identityTrapdoorSeedBuf.slice(0, 31)),
}
}
export const genWnts = async(input: any, wasmFilePath: string): Promise<Uint8Array> => {
const resp = await fetch(wasmFilePath);
const buffer = await resp.arrayBuffer();
return new Promise((resolve, reject) => {
builder(buffer)
.then(async witnessCalculator => {
const buff= await witnessCalculator.calculateWTNSBin(input, 0);
resolve(buff);
}).catch((error) => {
reject(error);
});
})
}
export const genSemaphoreProof = async(grothInput: any, wasmFilePath: string, finalZkeyPath: string) => {
const wntsBuff = await genWnts(grothInput, wasmFilePath);
const resp = await fetch(finalZkeyPath);
const arrayBuffer = await resp.arrayBuffer();
const { proof, publicSignals } = await groth16.prove(new Uint8Array(arrayBuffer), wntsBuff, null);
return { proof, publicSignals };
}
// export const genWnts = async(input: any, wasmFilePath: string): Promise<Uint8Array> => {
// const resp = await fetch(wasmFilePath);
// const buffer = await resp.arrayBuffer();
//
// return new Promise((resolve, reject) => {
// builder(buffer)
// .then(async witnessCalculator => {
// const buff= await witnessCalculator.calculateWTNSBin(input, 0);
// resolve(buff);
// }).catch((error) => {
// reject(error);
// });
// })
// }
//
// export const genSemaphoreProof = async(grothInput: any, wasmFilePath: string, finalZkeyPath: string) => {
// const wntsBuff = await genWnts(grothInput, wasmFilePath);
// const resp = await fetch(finalZkeyPath);
// const arrayBuffer = await resp.arrayBuffer();
// const { proof, publicSignals } = await groth16.prove(new Uint8Array(arrayBuffer), wntsBuff, null);
// return { proof, publicSignals };
// }

View File

@@ -14,9 +14,9 @@ export const checkPath = async (commitment: string) => {
if (!error && data) {
path = {
path_elements: data.pathElements,
path_index: data.indices,
root: data.root,
path_elements: data.siblingNodes.map((el: string) => BigInt(el)),
path_index: data.path,
root: BigInt(data.root),
};
groupName = name;

Binary file not shown.

View File

@@ -1,109 +1 @@
{
"protocol": "groth16",
"curve": "bn128",
"nPublic": 4,
"vk_alpha_1": [
"20491192805390485299153009773594534940189261866228447918068658471970481763042",
"9383485363053290200918347156157836566562967994039712273449902621266178545958",
"1"
],
"vk_beta_2": [
[
"6375614351688725206403948262868962793625744043794305715222011528459656738731",
"4252822878758300859123897981450591353533073413197771768651442665752259397132"
],
[
"10505242626370262277552901082094356697409835680220590971873171140371331206856",
"21847035105528745403288232691147584728191162732299865338377159692350059136679"
],
[
"1",
"0"
]
],
"vk_gamma_2": [
[
"10857046999023057135944570762232829481370756359578518086990519993285655852781",
"11559732032986387107991004021392285783925812861821192530917403151452391805634"
],
[
"8495653923123431417604973247489272438418190587263600148770280649306958101930",
"4082367875863433681332203403145435568316851327593401208105741076214120093531"
],
[
"1",
"0"
]
],
"vk_delta_2": [
[
"13369398263790948714119826670560921661814261611422071324113031139148683642083",
"11814365987154206115765734025702701983231399675936846115694686076562170051816"
],
[
"13403402838749381297055559140843113699009254544348562421698933178961122076691",
"2339318846859078616015294304198896595715693559484570918842385475469712887674"
],
[
"1",
"0"
]
],
"vk_alphabeta_12": [
[
[
"2029413683389138792403550203267699914886160938906632433982220835551125967885",
"21072700047562757817161031222997517981543347628379360635925549008442030252106"
],
[
"5940354580057074848093997050200682056184807770593307860589430076672439820312",
"12156638873931618554171829126792193045421052652279363021382169897324752428276"
],
[
"7898200236362823042373859371574133993780991612861777490112507062703164551277",
"7074218545237549455313236346927434013100842096812539264420499035217050630853"
]
],
[
[
"7077479683546002997211712695946002074877511277312570035766170199895071832130",
"10093483419865920389913245021038182291233451549023025229112148274109565435465"
],
[
"4595479056700221319381530156280926371456704509942304414423590385166031118820",
"19831328484489333784475432780421641293929726139240675179672856274388269393268"
],
[
"11934129596455521040620786944827826205713621633706285934057045369193958244500",
"8037395052364110730298837004334506829870972346962140206007064471173334027475"
]
]
],
"IC": [
[
"2063429681943670093654325913695927437969733860447953801119107319559591418587",
"3438409700153385960663790957752693324510032033327775792290827791929237511464",
"1"
],
[
"2769188201159157563688426100819805115292019321476491313637713505024075051457",
"3324866279362608491154698381219709455243327244898591535422698418987997197270",
"1"
],
[
"19213589447203579025251507407512383087066150062376166160516958557665810311292",
"13324991742791815029795561699743318691422074596911269914284449639355892922731",
"1"
],
[
"20325132123131481976739430430325995930683245508035327720711615568857146984740",
"16379330724442918730479691442220716264931316028659486652318241651146472200429",
"1"
],
[
"17892916250484982410674206564489740676256976777873663401012933360156397460693",
"17104080508455025026088836658552576626002622127985502873834396888306618763299",
"1"
]
]
}
{"protocol":"groth16","curve":"bn128","nPublic":4,"vk_alpha_1":["20491192805390485299153009773594534940189261866228447918068658471970481763042","9383485363053290200918347156157836566562967994039712273449902621266178545958","1"],"vk_beta_2":[["6375614351688725206403948262868962793625744043794305715222011528459656738731","4252822878758300859123897981450591353533073413197771768651442665752259397132"],["10505242626370262277552901082094356697409835680220590971873171140371331206856","21847035105528745403288232691147584728191162732299865338377159692350059136679"],["1","0"]],"vk_gamma_2":[["10857046999023057135944570762232829481370756359578518086990519993285655852781","11559732032986387107991004021392285783925812861821192530917403151452391805634"],["8495653923123431417604973247489272438418190587263600148770280649306958101930","4082367875863433681332203403145435568316851327593401208105741076214120093531"],["1","0"]],"vk_delta_2":[["7912208710313447447762395792098481825752520616755888860068004689933335666613","12599857379517512478445603412764121041984228075771497593287716170335433683702"],["21679208693936337484429571887537508926366191105267550375038502782696042114705","11502426145685875357967720478366491326865907869902181704031346886834786027007"],["1","0"]],"vk_alphabeta_12":[[["2029413683389138792403550203267699914886160938906632433982220835551125967885","21072700047562757817161031222997517981543347628379360635925549008442030252106"],["5940354580057074848093997050200682056184807770593307860589430076672439820312","12156638873931618554171829126792193045421052652279363021382169897324752428276"],["7898200236362823042373859371574133993780991612861777490112507062703164551277","7074218545237549455313236346927434013100842096812539264420499035217050630853"]],[["7077479683546002997211712695946002074877511277312570035766170199895071832130","10093483419865920389913245021038182291233451549023025229112148274109565435465"],["4595479056700221319381530156280926371456704509942304414423590385166031118820","19831328484489333784475432780421641293929726139240675179672856274388269393268"],["11934129596455521040620786944827826205713621633706285934057045369193958244500","8037395052364110730298837004334506829870972346962140206007064471173334027475"]]],"IC":[["19918517214839406678907482305035208173510172567546071380302965459737278553528","7151186077716310064777520690144511885696297127165278362082219441732663131220","1"],["690581125971423619528508316402701520070153774868732534279095503611995849608","21271996888576045810415843612869789314680408477068973024786458305950370465558","1"],["16461282535702132833442937829027913110152135149151199860671943445720775371319","2814052162479976678403678512565563275428791320557060777323643795017729081887","1"],["4319780315499060392574138782191013129592543766464046592208884866569377437627","13920930439395002698339449999482247728129484070642079851312682993555105218086","1"],["3554830803181375418665292545416227334138838284686406179598687755626325482686","5951609174746846070367113593675211691311013364421437923470787371738135276998","1"]]}