Skip to content

Commit

Permalink
chore: using native javascript instead wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
meomeocoj committed May 24, 2024
1 parent 0aef2b1 commit e3894c6
Show file tree
Hide file tree
Showing 8 changed files with 203 additions and 102 deletions.
4 changes: 2 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ module.exports = {
root: true,
extends: "@react-native",
rules: {
quotes: ["error", "double"],
"prettier/prettier": ["error", { singleQuote: false }],
"avoidEscape": true,
"allowTemplateLiterals": true
},
overrides: [
{
Expand Down
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"typescript.tsdk": "node_modules/typescript/lib"
"typescript.tsdk": "node_modules/typescript/lib",
"java.compile.nullAnalysis.mode": "automatic"
}
21 changes: 17 additions & 4 deletions App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@ import React from 'react';
import type { PropsWithChildren } from 'react';

import { ThemeProvider, createTheme, Button, makeStyles, Text } from '@rneui/themed';
import { View, Platform, DimensionValue } from 'react-native';
import { View, Platform, DimensionValue, NativeModules } from 'react-native';
import { Header } from '@rneui/base';

import { GoogleSignin } from '@react-native-google-signin/google-signin';
import { appleAuth } from '@invertase/react-native-apple-authentication';
// import { interpolate } from './bls';

// import * as buffer from "buffer";
import WebView from 'react-native-webview';
// @ts-ignore
import html from './asset/interpolate.html';
import OraiServiceProvider from '@oraichain/service-provider-orai';
import { LOGIN_TYPE } from '@oraichain/customauth';
import { getPairFromSharesAndIndexes } from './bls';
console.log(process.env.ANDROID_CLIENT_ID);
GoogleSignin.configure({
iosClientId: process.env.IOS_CLIENT_ID,
Expand Down Expand Up @@ -53,8 +55,17 @@ const App = () => {
]);
console.log('triggerLoginMobile 2:', Date.now());
console.log({ v1Data });

const shares = v1Data.shares.map((share: any) => share.toString('hex'));
const indexes = v1Data.sharesIndexes.map((index: any) => index.toString('hex'));
// const toHexString = (bytes) => bytes.reduce((str, byte) => str + byte.toString(16).padStart(2, '0'), '');
// const result = interpolate(shareAndIndexes.slice(0,3), 0n).toString(16)
// console.log({result});
// console.log(toHexString(Buffer.from(result,'hex')));
const shareAndIndexes:[bigint,bigint][] = shares.slice(0, 3).map((share, index) =>
[BigInt(parseInt(indexes[index], 16)), BigInt("0x" + share)]);
console.log(getPairFromSharesAndIndexes(shareAndIndexes));
setLoginResponse(v1Data);
setInterpolateResult(getPairFromSharesAndIndexes(shareAndIndexes));
} catch (error: any) {
console.log(error.message);
console.log('LoginError');
Expand Down Expand Up @@ -121,7 +132,7 @@ const App = () => {
<Container props={{ rowGap: 20 }}>
<Button color={'primary'} radius={'md'} title={'Sign in with Google'} onPress={onGoogleButton} />
<Button color={'primary'} radius={'md'} title={'Sign in with Apple'} onPress={onAppleButtonPress} />
{loginResponse && (
{/* {loginResponse && (
<WebView
originWhitelist={['*']}
source={isAndroid ? { uri: 'file:///android_asset/interpolate.html' } : html}
Expand All @@ -139,17 +150,19 @@ const App = () => {
return console.log('🚀 ~ file: index.tsx:131 ~ error:', error);
}
console.log('triggerLoginMobile 3:', Date.now());
console.log(result);
setInterpolateResult(result);
}}
/>
)}
)} */}
</Container>
)}
{interpolateResult && (
<Container props={{ flexBasis: 400, rowGap: 20, justifyContent: 'center' }}>
<Button onPress={() => setViewObject(interpolateResult.privKey)} title={'Private key'} />
<Button title={'Address'} onPress={() => setViewObject(interpolateResult.pubKey)} />
<Button title={'User info'} onPress={() => setViewObject(loginResponse.userInfo)} />
<Button title={'Log out'} onPress={async() => GoogleSignin.signOut()} />
<Text style={{ marginTop: 20 }}>{JSON.stringify(viewObject)}</Text>
</Container>
)}
Expand Down
32 changes: 0 additions & 32 deletions android/app/src/main/java/com/reactnativeexample/MainActivity.java

This file was deleted.

This file was deleted.

149 changes: 149 additions & 0 deletions bls.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
import {Field} from '@noble/curves/abstract/modular';
import {bls12_381} from '@noble/curves/bls12-381';

const PrimeFieldModulus = 52435875175126190479447740508185965837690552500527637822603658699938581184513n;
const Fr = Field(PrimeFieldModulus,255);
const G1 = bls12_381.G1
const basePoint = G1.ProjectivePoint;

// class Poly {
// coeff:bigint[] = []

// constructor(coeff:bigint[]){
// this.coeff = coeff
// }

// static constant(coeff: bigint){
// return new Poly([coeff])
// }

// static interpolate(dataPoints:[bigint,bigint][]){
// if(dataPoints.length == 0){
// return Poly.constant(0n)
// }

// let poly = new Poly([dataPoints[0][1]]);
// let minusS0 = dataPoints[0][0];
// minusS0 = Fr.neg(minusS0);
// console.log(dataPoints[1][1].toString(16));
// let base = new Poly([minusS0, 1n]);

// for(const [x, y] of dataPoints.slice(1)){
// let diff = y;
// console.log("diff", diff.toString(16));
// diff = Fr.sub(diff, poly.evaluate(x));
// let base_val = base.evaluate(x);
// diff = Fr.mul(diff, Fr.inv(base_val));
// base.mulFr(diff);
// poly.add(base);
// base.mul(new Poly([Fr.neg(x), 1n]));
// }
// return poly;
// }

// add(rhs: Poly){
// const len = this.coeff.length;
// const rhs_len = rhs.coeff.length;

// if(rhs_len > len){
// const fillFr = new Array(rhs_len - len).fill(0n);
// this.coeff = this.coeff.concat(fillFr);
// }

// this.coeff = this.coeff.map((coeff, index) => {
// return Fr.add(coeff,rhs.coeff[index])
// })

// this.removeZeroes();
// }

// mul(rhs:Poly){
// if(this.isZero() || rhs.isZero()){
// return Poly.constant(0n)
// }
// const len = this.coeff.length + rhs.coeff.length - 1;
// let coeff = new Array(len).fill(0n);
// let tmp = 0n;
// let i, j = 0;
// for(i = 0; i < this.coeff.length; i++){
// for(j = 0; j < rhs.coeff.length; j++){
// tmp = Fr.mul(this.coeff[i], rhs.coeff[j]);
// coeff[i+j] = Fr.add(coeff[i+j], tmp);
// }
// }
// this.coeff = coeff;
// }

// mulFr(rhs:bigint){
// if(rhs === 0n){
// this.coeff = [0n];
// } else {
// this.coeff = this.coeff.map((coeff) => {
// return Fr.mul(coeff, rhs)
// });
// }
// }

// evaluate(x: bigint){
// let result = this.coeff[this.coeff.length-1];
// for(let i = this.coeff.length-2; i >= 0; i--){
// result = Fr.add(Fr.mul(result, x),this.coeff[i]);
// }
// return result
// }

// removeZeroes(){
// for(let i = this.coeff.length-1; i >= 0; i--){
// if(this.coeff[i] === 0n){
// this.coeff.pop()
// } else {
// break;
// }
// }
// }

// isZero(){
// return !this.coeff.some(coeff => !(coeff === 0n))
// }

// }
export const interpolate = (dataPoints:[bigint,bigint][], index: bigint)=>{
let result = 0n; // term result
for(let i = 0; i < dataPoints.length; i++){
let term = dataPoints[i][1]; // y value
for(let j = 0; j < dataPoints.length; j++){
if(i != j){
const diff_index = Fr.sub(index, dataPoints[j][0]);
const diff_i = Fr.sub(dataPoints[i][0], dataPoints[j][0]);
term = Fr.mul(term, diff_index);
term = Fr.div(term, diff_i);
}
}
result = Fr.add(result,term);
}
return result;
}

export function getPairFromSharesAndIndexes(dataPoints:[bigint,bigint][]){
const privateKey = interpolate(dataPoints, 0n);
const pubKey = basePoint.BASE.multiply(privateKey).toHex();
return {privKey:privateKey.toString(16) , pubKey};
}

// (()=>{
// let shares = ["6f569904269aef285688a23a4991ba590b9fe4471b0a2d64ec0cca311b6bd78a",
// "0fa02b2b0a69946bc83ee96d0df6e6656d3d87306b29872ae638e559c30d63e0",
// "1eb6fbab0706e58e5659eca4276d38253d9ee615858a93c6c7952141a3367485"];
// let indexes = ["01", "02", "03"];
// const shareAndIndexes:[bigint,bigint][] = shares.map((share, index) =>
// [BigInt(parseInt(indexes[index], 16)), BigInt("0x" + share)]);
// const poly = Poly.interpolate(shareAndIndexes.slice(0,3));
// console.log(getPairFromSharesAndIndexes(shareAndIndexes));
// console.log(poly.coeff.map(coeff => coeff.toString(16)));
// const secret = poly.evaluate(0n);
// const pubkeyKey = basePoint.BASE.multiply(secret);
// console.log(pubkeyKey.toHex());
// console.log(secret.toString(16));
// // privateKey: 55fef690085ffb339ac366fbc6fa03f5714ab553952fce76d910cfc9ac51cf81
// // pubkey: 92ccef9a7fe47e1df29d24aca9e11d00d2405a2459431349398eed46bb411115026ddb984a09b0de9cf59d1f66e5cbb7
// })()
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@
"test": "jest"
},
"dependencies": {
"@bitgo/bls-dkg": "^1.3.1",
"@invertase/react-native-apple-authentication": "^2.2.2",
"@noble/bls12-381": "^1.4.0",
"@noble/curves": "^1.4.0",
"@oraichain/customauth": "next",
"@oraichain/multifactors.js": "next",
"@oraichain/only-social-key": "next",
"@oraichain/service-provider-orai": "next",
"@oraichain/storage-layer-orai": "next",
"@oraichain/multifactors.js": "next",
"@react-native-google-signin/google-signin": "^10.0.1",
"@rneui/base": "^4.0.0-rc.8",
"@rneui/themed": "^4.0.0-rc.8",
Expand Down
Loading

0 comments on commit e3894c6

Please sign in to comment.