Skip to content

Commit e3894c6

Browse files
committed
chore: using native javascript instead wasm
1 parent 0aef2b1 commit e3894c6

File tree

8 files changed

+203
-102
lines changed

8 files changed

+203
-102
lines changed

.eslintrc.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ module.exports = {
22
root: true,
33
extends: "@react-native",
44
rules: {
5-
quotes: ["error", "double"],
6-
"prettier/prettier": ["error", { singleQuote: false }],
5+
"avoidEscape": true,
6+
"allowTemplateLiterals": true
77
},
88
overrides: [
99
{

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
{
2-
"typescript.tsdk": "node_modules/typescript/lib"
2+
"typescript.tsdk": "node_modules/typescript/lib",
3+
"java.compile.nullAnalysis.mode": "automatic"
34
}

App.tsx

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,20 @@ import React from 'react';
33
import type { PropsWithChildren } from 'react';
44

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

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

1213
// import * as buffer from "buffer";
1314
import WebView from 'react-native-webview';
1415
// @ts-ignore
1516
import html from './asset/interpolate.html';
1617
import OraiServiceProvider from '@oraichain/service-provider-orai';
1718
import { LOGIN_TYPE } from '@oraichain/customauth';
19+
import { getPairFromSharesAndIndexes } from './bls';
1820
console.log(process.env.ANDROID_CLIENT_ID);
1921
GoogleSignin.configure({
2022
iosClientId: process.env.IOS_CLIENT_ID,
@@ -53,8 +55,17 @@ const App = () => {
5355
]);
5456
console.log('triggerLoginMobile 2:', Date.now());
5557
console.log({ v1Data });
56-
58+
const shares = v1Data.shares.map((share: any) => share.toString('hex'));
59+
const indexes = v1Data.sharesIndexes.map((index: any) => index.toString('hex'));
60+
// const toHexString = (bytes) => bytes.reduce((str, byte) => str + byte.toString(16).padStart(2, '0'), '');
61+
// const result = interpolate(shareAndIndexes.slice(0,3), 0n).toString(16)
62+
// console.log({result});
63+
// console.log(toHexString(Buffer.from(result,'hex')));
64+
const shareAndIndexes:[bigint,bigint][] = shares.slice(0, 3).map((share, index) =>
65+
[BigInt(parseInt(indexes[index], 16)), BigInt("0x" + share)]);
66+
console.log(getPairFromSharesAndIndexes(shareAndIndexes));
5767
setLoginResponse(v1Data);
68+
setInterpolateResult(getPairFromSharesAndIndexes(shareAndIndexes));
5869
} catch (error: any) {
5970
console.log(error.message);
6071
console.log('LoginError');
@@ -121,7 +132,7 @@ const App = () => {
121132
<Container props={{ rowGap: 20 }}>
122133
<Button color={'primary'} radius={'md'} title={'Sign in with Google'} onPress={onGoogleButton} />
123134
<Button color={'primary'} radius={'md'} title={'Sign in with Apple'} onPress={onAppleButtonPress} />
124-
{loginResponse && (
135+
{/* {loginResponse && (
125136
<WebView
126137
originWhitelist={['*']}
127138
source={isAndroid ? { uri: 'file:///android_asset/interpolate.html' } : html}
@@ -139,17 +150,19 @@ const App = () => {
139150
return console.log('🚀 ~ file: index.tsx:131 ~ error:', error);
140151
}
141152
console.log('triggerLoginMobile 3:', Date.now());
153+
console.log(result);
142154
setInterpolateResult(result);
143155
}}
144156
/>
145-
)}
157+
)} */}
146158
</Container>
147159
)}
148160
{interpolateResult && (
149161
<Container props={{ flexBasis: 400, rowGap: 20, justifyContent: 'center' }}>
150162
<Button onPress={() => setViewObject(interpolateResult.privKey)} title={'Private key'} />
151163
<Button title={'Address'} onPress={() => setViewObject(interpolateResult.pubKey)} />
152164
<Button title={'User info'} onPress={() => setViewObject(loginResponse.userInfo)} />
165+
<Button title={'Log out'} onPress={async() => GoogleSignin.signOut()} />
153166
<Text style={{ marginTop: 20 }}>{JSON.stringify(viewObject)}</Text>
154167
</Container>
155168
)}

android/app/src/main/java/com/reactnativeexample/MainActivity.java

Lines changed: 0 additions & 32 deletions
This file was deleted.

android/app/src/main/java/com/reactnativeexample/MainApplication.java

Lines changed: 0 additions & 62 deletions
This file was deleted.

bls.ts

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
import {Field} from '@noble/curves/abstract/modular';
2+
import {bls12_381} from '@noble/curves/bls12-381';
3+
4+
const PrimeFieldModulus = 52435875175126190479447740508185965837690552500527637822603658699938581184513n;
5+
const Fr = Field(PrimeFieldModulus,255);
6+
const G1 = bls12_381.G1
7+
const basePoint = G1.ProjectivePoint;
8+
9+
// class Poly {
10+
// coeff:bigint[] = []
11+
12+
// constructor(coeff:bigint[]){
13+
// this.coeff = coeff
14+
// }
15+
16+
// static constant(coeff: bigint){
17+
// return new Poly([coeff])
18+
// }
19+
20+
// static interpolate(dataPoints:[bigint,bigint][]){
21+
// if(dataPoints.length == 0){
22+
// return Poly.constant(0n)
23+
// }
24+
25+
// let poly = new Poly([dataPoints[0][1]]);
26+
// let minusS0 = dataPoints[0][0];
27+
// minusS0 = Fr.neg(minusS0);
28+
// console.log(dataPoints[1][1].toString(16));
29+
// let base = new Poly([minusS0, 1n]);
30+
31+
// for(const [x, y] of dataPoints.slice(1)){
32+
// let diff = y;
33+
// console.log("diff", diff.toString(16));
34+
// diff = Fr.sub(diff, poly.evaluate(x));
35+
// let base_val = base.evaluate(x);
36+
// diff = Fr.mul(diff, Fr.inv(base_val));
37+
// base.mulFr(diff);
38+
// poly.add(base);
39+
// base.mul(new Poly([Fr.neg(x), 1n]));
40+
// }
41+
// return poly;
42+
// }
43+
44+
// add(rhs: Poly){
45+
// const len = this.coeff.length;
46+
// const rhs_len = rhs.coeff.length;
47+
48+
// if(rhs_len > len){
49+
// const fillFr = new Array(rhs_len - len).fill(0n);
50+
// this.coeff = this.coeff.concat(fillFr);
51+
// }
52+
53+
// this.coeff = this.coeff.map((coeff, index) => {
54+
// return Fr.add(coeff,rhs.coeff[index])
55+
// })
56+
57+
// this.removeZeroes();
58+
// }
59+
60+
// mul(rhs:Poly){
61+
// if(this.isZero() || rhs.isZero()){
62+
// return Poly.constant(0n)
63+
// }
64+
// const len = this.coeff.length + rhs.coeff.length - 1;
65+
// let coeff = new Array(len).fill(0n);
66+
// let tmp = 0n;
67+
// let i, j = 0;
68+
// for(i = 0; i < this.coeff.length; i++){
69+
// for(j = 0; j < rhs.coeff.length; j++){
70+
// tmp = Fr.mul(this.coeff[i], rhs.coeff[j]);
71+
// coeff[i+j] = Fr.add(coeff[i+j], tmp);
72+
// }
73+
// }
74+
// this.coeff = coeff;
75+
// }
76+
77+
// mulFr(rhs:bigint){
78+
// if(rhs === 0n){
79+
// this.coeff = [0n];
80+
// } else {
81+
// this.coeff = this.coeff.map((coeff) => {
82+
// return Fr.mul(coeff, rhs)
83+
// });
84+
// }
85+
// }
86+
87+
// evaluate(x: bigint){
88+
// let result = this.coeff[this.coeff.length-1];
89+
// for(let i = this.coeff.length-2; i >= 0; i--){
90+
// result = Fr.add(Fr.mul(result, x),this.coeff[i]);
91+
// }
92+
// return result
93+
// }
94+
95+
// removeZeroes(){
96+
// for(let i = this.coeff.length-1; i >= 0; i--){
97+
// if(this.coeff[i] === 0n){
98+
// this.coeff.pop()
99+
// } else {
100+
// break;
101+
// }
102+
// }
103+
// }
104+
105+
// isZero(){
106+
// return !this.coeff.some(coeff => !(coeff === 0n))
107+
// }
108+
109+
// }
110+
export const interpolate = (dataPoints:[bigint,bigint][], index: bigint)=>{
111+
let result = 0n; // term result
112+
for(let i = 0; i < dataPoints.length; i++){
113+
let term = dataPoints[i][1]; // y value
114+
for(let j = 0; j < dataPoints.length; j++){
115+
if(i != j){
116+
const diff_index = Fr.sub(index, dataPoints[j][0]);
117+
const diff_i = Fr.sub(dataPoints[i][0], dataPoints[j][0]);
118+
term = Fr.mul(term, diff_index);
119+
term = Fr.div(term, diff_i);
120+
}
121+
}
122+
result = Fr.add(result,term);
123+
}
124+
return result;
125+
}
126+
127+
export function getPairFromSharesAndIndexes(dataPoints:[bigint,bigint][]){
128+
const privateKey = interpolate(dataPoints, 0n);
129+
const pubKey = basePoint.BASE.multiply(privateKey).toHex();
130+
return {privKey:privateKey.toString(16) , pubKey};
131+
}
132+
133+
// (()=>{
134+
// let shares = ["6f569904269aef285688a23a4991ba590b9fe4471b0a2d64ec0cca311b6bd78a",
135+
// "0fa02b2b0a69946bc83ee96d0df6e6656d3d87306b29872ae638e559c30d63e0",
136+
// "1eb6fbab0706e58e5659eca4276d38253d9ee615858a93c6c7952141a3367485"];
137+
// let indexes = ["01", "02", "03"];
138+
// const shareAndIndexes:[bigint,bigint][] = shares.map((share, index) =>
139+
// [BigInt(parseInt(indexes[index], 16)), BigInt("0x" + share)]);
140+
// const poly = Poly.interpolate(shareAndIndexes.slice(0,3));
141+
// console.log(getPairFromSharesAndIndexes(shareAndIndexes));
142+
// console.log(poly.coeff.map(coeff => coeff.toString(16)));
143+
// const secret = poly.evaluate(0n);
144+
// const pubkeyKey = basePoint.BASE.multiply(secret);
145+
// console.log(pubkeyKey.toHex());
146+
// console.log(secret.toString(16));
147+
// // privateKey: 55fef690085ffb339ac366fbc6fa03f5714ab553952fce76d910cfc9ac51cf81
148+
// // pubkey: 92ccef9a7fe47e1df29d24aca9e11d00d2405a2459431349398eed46bb411115026ddb984a09b0de9cf59d1f66e5cbb7
149+
// })()

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,15 @@
1010
"test": "jest"
1111
},
1212
"dependencies": {
13+
"@bitgo/bls-dkg": "^1.3.1",
1314
"@invertase/react-native-apple-authentication": "^2.2.2",
15+
"@noble/bls12-381": "^1.4.0",
16+
"@noble/curves": "^1.4.0",
1417
"@oraichain/customauth": "next",
18+
"@oraichain/multifactors.js": "next",
1519
"@oraichain/only-social-key": "next",
1620
"@oraichain/service-provider-orai": "next",
1721
"@oraichain/storage-layer-orai": "next",
18-
"@oraichain/multifactors.js": "next",
1922
"@react-native-google-signin/google-signin": "^10.0.1",
2023
"@rneui/base": "^4.0.0-rc.8",
2124
"@rneui/themed": "^4.0.0-rc.8",

0 commit comments

Comments
 (0)