-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathApp.js
191 lines (174 loc) · 4.58 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// A shim for TextEncoder is required for the wasm-pack module to load OK.
import "text-encoding";
// This particular shim complains when the label parameter is passed in.
import { LogBox } from "react-native";
LogBox.ignoreLogs([/TextEncoder constructor called with encoding label/]);
import { StatusBar } from "expo-status-bar";
import { useEffect, useState, useMemo } from "react";
import { StyleSheet, Text, View, TextInput, ScrollView } from "react-native";
// Import the wasm module.
import * as keyagg from "./keyagg";
const pubkeyInputStyles = StyleSheet.create({
valid: {
borderWidth: 2,
borderColor: "green",
},
invalid: {
borderWidth: 2,
borderColor: "red",
},
});
function PubKeyInput({ onChange, value }) {
const validityStyle = useMemo(() => {
if (!value) return null;
return keyagg.is_valid_pubkey(value)
? pubkeyInputStyles.valid
: pubkeyInputStyles.invalid;
}, [value]);
return (
<TextInput
value={value}
onChangeText={onChange}
maxLength={66}
style={[styles.pubkeyInput, validityStyle]}
/>
);
}
const aggregatedKeyDisplayStyles = StyleSheet.create({
title: {
color: "white",
fontSize: 20,
},
key: {
color: "white",
fontFamily: "monospace",
fontSize: 12,
maxWidth: "100%",
},
});
function AggregatedKeyDisplay({ pubkeys }) {
const [invalidKeyIndex, setInvalidKeyIndex] = useState(null);
const [aggregatedPubkey, setAggregatedPubkey] = useState(null);
useEffect(() => {
try {
const filteredPubkeys = pubkeys
.map((key) => key.replace(/ /g, ""))
.filter((key) => !!key);
setAggregatedPubkey(keyagg.aggregate(filteredPubkeys));
setInvalidKeyIndex(null);
} catch (i) {
setAggregatedPubkey(null);
setInvalidKeyIndex(i);
}
}, [pubkeys]);
return (
<View style={styles.aggregatedKeyDisplay}>
{aggregatedPubkey && (
<>
<Text style={aggregatedKeyDisplayStyles.title}>Aggregated Key</Text>
<Text style={aggregatedKeyDisplayStyles.key} selectable>
{aggregatedPubkey}
</Text>
</>
)}
{invalidKeyIndex && (
<>
<Text style={aggregatedKeyDisplayStyles.title}>Invalid Key</Text>
<Text style={aggregatedKeyDisplayStyles.key}>
"{pubkeys[invalidKeyIndex]}" is not a valid public key.
</Text>
</>
)}
</View>
);
}
const DEFAULT_PUBKEYS = [
"026e14224899cf9c780fef5dd200f92a28cc67f71c0af6fe30b5657ffc943f08f4",
"02f3b071c064f115ca762ed88c3efd1927ea657c7949698b77255ea25751331f0b",
"03204ea8bc3425b2cbc9cb20617f67dc6b202467591d0b26d059e370b71ee392eb",
"",
];
export default function App() {
const [pubkeys, setPubkeys] = useState(DEFAULT_PUBKEYS);
const pubkeyInputs = pubkeys.map((pubkey, i) => {
const onChange = (newPubkey) => {
let newPubkeys = [
...pubkeys.slice(0, i),
newPubkey,
...pubkeys.slice(i + 1),
];
if (i === pubkeys.length - 1) {
newPubkeys.push("");
}
while (newPubkeys.slice(-2).every((key) => !key)) {
newPubkeys = newPubkeys.slice(0, -1);
}
setPubkeys(newPubkeys);
};
return <PubKeyInput key={i} value={pubkey} onChange={onChange} />;
});
return (
<View style={styles.container}>
<Text style={styles.title}>MuSig Pubkey Aggregation</Text>
<Text style={styles.subtitle}>Enter a set of pubkeys</Text>
<ScrollView
style={styles.pubkeyScrollView}
contentContainerStyle={styles.pubkeyInputsBox}
>
{pubkeyInputs}
</ScrollView>
<AggregatedKeyDisplay pubkeys={pubkeys} />
<StatusBar style="auto" />
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#333",
alignItems: "center",
},
title: {
marginTop: 50,
color: "white",
fontSize: 25,
padding: 10,
textAlign: "center",
justifyContent: "space-around",
},
subtitle: {
color: "white",
textAlign: "center",
margin: 20,
},
pubkeyInput: {
backgroundColor: "#555",
borderRadius: 10,
padding: 10,
margin: 15,
color: "white",
width: 600,
maxWidth: "100%",
fontFamily: "monospace",
textAlign: "center",
},
pubkeyScrollView: {
flex: 5,
maxWidth: "100%",
},
pubkeyInputsBox: {
width: "90%",
paddingBottom: 30,
},
aggregatedKeyDisplay: {
margin: 40,
height: 100,
backgroundColor: "#444",
padding: 10,
width: 550,
maxWidth: "95%",
alignItems: "center",
justifyContent: "space-evenly",
borderRadius: 5,
},
});