Skip to content

Commit

Permalink
add polyfills for assert + buffer for zk packages
Browse files Browse the repository at this point in the history
  • Loading branch information
kibagateaux committed Jan 29, 2025
1 parent d1b8d71 commit e05b614
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 112 deletions.
9 changes: 8 additions & 1 deletion metro.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// eslint-disable-next-line
const { getSentryExpoConfig } = require('@sentry/react-native/metro');

// This replaces `const config = getDefaultConfig(__dirname);`
// This replaces Expo's `const config = getDefaultConfig(__dirname);`
const config = getSentryExpoConfig(__dirname);

module.exports = {
Expand All @@ -10,7 +10,14 @@ module.exports = {
...config.resolver,
extraNodeModules: {
stream: require.resolve('react-native-stream'),
// assert: require.resolve('assert-tiny'),
// fs: require.resolve('react-native-level-fs'),
...config.resolver.extraNodeModules,
},
sourceExts: [
...config.resolver.sourceExts,
// compile CommonJS modules for circom and snark
'cjs',
],
},
};
82 changes: 7 additions & 75 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@
"@semaphore-protocol/proof": "^3.11.1",
"@sentry/react-native": "~5.22.0",
"@types/lodash": "^4.14.202",
"assert-tiny": "^1.0.2",
"axios": "^1.5.1",
"base-64": "^1.0.0",
"buffer": "^6.0.3",
"circomlib": "^2.0.5",
"circomlibjs": "^0.1.7",
"date-fns": "^2.30.0",
Expand Down
26 changes: 0 additions & 26 deletions shim.js

This file was deleted.

56 changes: 55 additions & 1 deletion src/utils/polyfills.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,70 @@
* Add to React Native entry point file
* Any polyfills here for node/browser functions
*/

if (typeof __dirname === 'undefined') global.__dirname = '/';
if (typeof __filename === 'undefined') global.__filename = '';
if (typeof process === 'undefined') {
global.process = require('process');
} else {
const bProcess = require('process');
for (const p in bProcess) {
if (!(p in process)) {
process[p] = bProcess[p];
}
}
}

if (!window) {
process.browser = false;
}

// global.location = global.location || { port: 80 }
const isDev = typeof __DEV__ === 'boolean' && __DEV__;
process.env.NODE_ENV = isDev ? 'development' : 'production';
if (typeof localStorage !== 'undefined') {
localStorage.debug = isDev ? '*' : '';
}

import { Buffer } from 'buffer';
if (typeof global.Buffer === 'undefined') {
global.Buffer = Buffer;
}

import { encode as btoa, decode as atob } from 'base-64';
global.btoa = btoa;
global.atob = atob;

if (!window) {
if (!global.crypto) global.crypto = {};
global.crypto.getRandomValues = require('expo-crypto').getRandomValues;
// global.crypto.getRandomValues = require('expo-crypto').getRandomValues;
global.crypto = require('expo-crypto');
}

process.version = 0; // no NodeJS version in React Native

// import 'react-native-get-random-values'; // expo-crypto should preempt this
// import "@ethersproject/shims" //for ethers.js

// import 'expo-crypto'; // separate from crypto imports above

// add `assert` shim for circom to run circuits in native app
// const { AssertionError, deepEqual, equal, notDeepEqual, notEqual, ok, strictEqual, throws } = require('assert-tiny');

// import { AssertionError, deepEqual, equal, notDeepEqual, notEqual, ok, strictEqual, throws } from 'assert-tiny';
// if (typeof global === 'object') {
// // Only override the global scope if it's defined.
// if (typeof global.assert !== 'function') {
// global.assert = ok;
// }
// Object.assign(global.assert, {
// AssertionError,
// deepEqual,
// equal,
// notDeepEqual,
// notEqual,
// ok,
// strictEqual,
// throws,
// });
// }
6 changes: 3 additions & 3 deletions src/utils/proving.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

import { JubjubSignature } from 'types/GameMechanics';

// import circuit from 'circuits/target/circuits.json';
// import circ from 'circomlbjs';
// import snark from 'snarkjs';
import circuit from 'circuits/circle_js/circle_constraints.json';
import circ from 'circomlibjs';
import snark from 'snarkjs';

import { buildBabyjub, buildPoseidon } from 'circomlibjs';
import { randomUUID } from 'expo-crypto';
Expand Down
2 changes: 0 additions & 2 deletions src/utils/zkpid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import {
} from './config';
import { ethers, Wallet, providers } from 'ethers';
import { memoize } from 'lodash';
import './proving';
// createProof('asjfnaf');

const defaultProvider = (): providers.Provider =>
new ethers.providers.AlchemyProvider(
Expand Down
18 changes: 14 additions & 4 deletions src/utils/zkpid.web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { debug } from './logging';
import { JubJubSigResponse } from 'types/GameMechanics';
import { ethers } from 'ethers';
import { Errorable } from 'types/UserConfig.js';
import { proveCircleSummoning } from './proving.ts';

interface BrowserWalletSignResult {
signature: string;
Expand All @@ -20,6 +21,7 @@ const signWithBrowserWallet = async (message: string): Errorable<BrowserWalletSi
const signer = provider.getSigner();
const signature = await signer.signMessage(message);
const address = await signer.getAddress();
console.log('get web wallet signer', signer);

return { signature, address };
} else {
Expand All @@ -37,15 +39,18 @@ const fallbackToWalletSignature = async (id: string | Identity): Errorable<JubJu
const result = await signWithBrowserWallet(msg);
console.log('utils:zkpid:web:wallet fallback', result);
if (!result) return { error: 'Wallet failed to sign' };
console.log('utils:zkpid:web:wallet fallback error', result.error);
if (result.error) return result;
return {
const formatted = {
signature: {
ether: result.signature,
der: '',
raw: { v: 28, s: '', r: '' },
},
etherAddress: result.address,
};
console.log('utils:zkpid:web:wallet fallback returning', formatted);
return formatted;
};

/** TODO figure out return types from HaLo lib
Expand Down Expand Up @@ -82,12 +87,17 @@ export const signWithId = async (id: string | Identity): Promise<JubJubSigRespon
},
});

console.log('utils:zkpid:web:signWithId:nfcResult: ', result);
const web = fallbackToWalletSignature(id);
const web = await fallbackToWalletSignature(id);

console.error('utils:zkpid:web:signWithId:nfcResult: ', result);
console.error('utils:zkpid:web:signWithId:walletResult: ', web);

await proveCircleSummoning(id as string, result ?? web);

if (result) return result;
// TODO should return NFC error if exists? and no valid result
// if(web.error) return result;
return web as Promise<JubJubSigResponse>;
return web as JubJubSigResponse;
} catch (err) {
console.warn('utils:zkpid:web signing error', err);

Expand Down

0 comments on commit e05b614

Please sign in to comment.