-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.tsx
294 lines (264 loc) · 9.46 KB
/
App.tsx
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
*/
import React, { useEffect, useState } from 'react';
import type {PropsWithChildren} from 'react';
import {
SafeAreaView,
ScrollView,
StatusBar,
StyleSheet,
Text,
useColorScheme,
View,
} from 'react-native';
import {
Colors,
DebugInstructions,
Header,
LearnMoreLinks,
ReloadInstructions,
} from 'react-native/Libraries/NewAppScreen';
import { Buffer } from "buffer";
import xdr from './src/xdr';
import { Networks, WebAuth } from 'stellar-sdk';
type SectionProps = PropsWithChildren<{
title: string;
}>;
function Section({children, title}: SectionProps): JSX.Element {
const isDarkMode = useColorScheme() === 'dark';
return (
<View style={styles.sectionContainer}>
<Text
style={[
styles.sectionTitle,
{
color: isDarkMode ? Colors.white : Colors.black,
},
]}>
{title}
</Text>
<Text
style={[
styles.sectionDescription,
{
color: isDarkMode ? Colors.light : Colors.dark,
},
]}>
{children}
</Text>
</View>
);
}
function App(): JSX.Element {
const isDarkMode = useColorScheme() === 'dark';
const backgroundStyle = {
backgroundColor: isDarkMode ? Colors.darker : Colors.lighter,
};
const [xdrParsingSuccess, setXdrParsingSuccess] = useState<boolean | undefined>(undefined);
const [stringOutcome, setStringOutcome] = useState("");
// See: https://laboratory.stellar.org/#xdr-viewer?input=AAAAAgAAAACujgze1ivBKlbuOmtG%2B45%2B3OZ69eQIkvdX1DYyH1GW%2FgAAAMgAAAAAAAAAAAAAAAEAAAAAZWjFDgAAAABlaMY6AAAAAAAAAAIAAAABAAAAABFIDN%2F57JxjKEzm%2BSCZi2BUm9fr71zRPwDzlRVIB1mTAAAACgAAABdkZXYudmlicmFudGFwcC5jb20gYXV0aAAAAAABAAAAQG9TUEF6aGt2S20yYVdxRTJObXhtZ05HeUR3TmRQUUVSN1lPaXJrT1RQVThIZjdORFNmejhidi9NR0FCTmFGZXYAAAABAAAAAK6ODN7WK8EqVu46a0b7jn7c5nr15AiS91fUNjIfUZb%2BAAAACgAAAA93ZWJfYXV0aF9kb21haW4AAAAAAQAAABp3ZWJhdXRoLWRldi52aWJyYW50YXBwLmNvbQAAAAAAAAAAAAEfUZb%2BAAAAQIRb0%2B0I%2Fgah5Ox4W79QzckZikjprFMUX3PQp%2BCY00MsveDnEU%2FWa2eMtBThInAER9DxBynRbryvwoH8Fg4saQo%3D&type=TransactionEnvelope&network=test
const stringEnvelope = "AAAAAgAAAACujgze1ivBKlbuOmtG+45+3OZ69eQIkvdX1DYyH1GW/gAAAMgAAAAAAAAAAAAAAAEAAAAAZWjFDgAAAABlaMY6AAAAAAAAAAIAAAABAAAAABFIDN/57JxjKEzm+SCZi2BUm9fr71zRPwDzlRVIB1mTAAAACgAAABdkZXYudmlicmFudGFwcC5jb20gYXV0aAAAAAABAAAAQG9TUEF6aGt2S20yYVdxRTJObXhtZ05HeUR3TmRQUUVSN1lPaXJrT1RQVThIZjdORFNmejhidi9NR0FCTmFGZXYAAAABAAAAAK6ODN7WK8EqVu46a0b7jn7c5nr15AiS91fUNjIfUZb+AAAACgAAAA93ZWJfYXV0aF9kb21haW4AAAAAAQAAABp3ZWJhdXRoLWRldi52aWJyYW50YXBwLmNvbQAAAAAAAAAAAAEfUZb+AAAAQIRb0+0I/gah5Ox4W79QzckZikjprFMUX3PQp+CY00MsveDnEU/Wa2eMtBThInAER9DxBynRbryvwoH8Fg4saQo=";
const expectedOperationDataValue = "oSPAzhkvKm2aWqE2NmxmgNGyDwNdPQER7YOirkOTPU8Hf7NDSfz8bv/MGABNaFev";
useEffect(() => {
try {
const buffer = Buffer.from(stringEnvelope, 'base64');
const xdrTxEnvelope = xdr.TransactionEnvelope.fromXDR(buffer);
const operation = xdrTxEnvelope.value().tx().operations()[0];
console.log(">>>>> operation: ", operation);
/*
Outcome with "js-xdr": "1.3.0":
>>>>> operation:
{
"_attributes":{
"body":{
"_arm":"manageDataOp",
"_armType":[
"Function ChildStruct"
],
"_switch":[
"ChildEnum"
],
"_value":[
"ChildStruct"
]
},
"sourceAccount":{
"_arm":"ed25519",
"_armType":[
"Opaque"
],
"_switch":[
"ChildEnum"
],
"_value":[
"Buffer"
]
}
}
}
Outcome with "js-xdr": "3.0.0":
>>>>> operation:
{
"_attributes":{
"body":{
"_arm":"manageDataOp",
"_armType":[
"Function n"
],
"_switch":[
"n"
],
"_value":[
"n"
]
},
"sourceAccount":{
"_arm":"ed25519",
"_armType":[
"F"
],
"_switch":[
"n"
],
"_value":[
Uint8Array
]
}
}
}
*/
const dataValue = operation.body().value().dataValue();
console.log(">>>>> dataValue: ", dataValue);
/*
Outcome with "js-xdr": "1.3.0":
>>>>> dataValue:
{
"data": [111, 83, 80, 65, 122, 104, 107, 118, 75, 109, 50, 97, 87, 113, 69, 50, 78, 109, 120, 109, 103, 78, 71, 121, 68, 119, 78, 100, 80, 81, 69, 82, 55, 89, 79, 105, 114, 107, 79, 84, 80, 85, 56, 72, 102, 55, 78, 68, 83, 102, 122, 56, 98, 118, 47, 77, 71, 65, 66, 78, 97, 70, 101, 118],
"type": "Buffer"
}
Outcome with "js-xdr": "3.0.0":
>>>>> dataValue:
[111, 83, 80, 65, 122, 104, 107, 118, 75, 109, 50, 97, 87, 113, 69, 50, 78, 109, 120, 109, 103, 78, 71, 121, 68, 119, 78, 100, 80, 81, 69, 82, 55, 89, 79, 105, 114, 107, 79, 84, 80, 85, 56, 72, 102, 55, 78, 68, 83, 102, 122, 56, 98, 118, 47, 77, 71, 65, 66, 78, 97, 70, 101, 118]
*/
const stringDataValue = dataValue.toString();
console.log(">>>>> stringDataValue: ", stringDataValue);
/*
Outcome with "js-xdr": "1.3.0":
>>>>> stringDataValue:
oSPAzhkvKm2aWqE2NmxmgNGyDwNdPQER7YOirkOTPU8Hf7NDSfz8bv/MGABNaFev
Outcome with "js-xdr": "3.0.0":
>>>>> stringDataValue:
111,83,80,65,122,104,107,118,75,109,50,97,87,113,69,50,78,109,120,109,103,78,71,121,68,119,78,100,80,81,69,82,55,89,79,105,114,107,79,84,80,85,56,72,102,55,78,68,83,102,122,56,98,118,47,77,71,65,66,78,97,70,101,118
*/
setStringOutcome(stringDataValue);
setXdrParsingSuccess(stringDataValue === expectedOperationDataValue);
} catch (error) {
console.log("X X X ERROR while parsing XDR transaction or operation: ", error);
}
},[]);
useEffect(() => {
try {
// ***NOTE: for this test to work you'll need to comment out the code that
// returns a "The transaction has expired" error on the stellar-sdk here:
// https://github.com/stellar/js-stellar-sdk/blob/master/src/webauth/utils.ts#L267-L269
// Since the Challenge Tx has a short expiration time so you don't need
// to create one yourself
const resp = WebAuth.readChallengeTx(
stringEnvelope,
"GCXI4DG62YV4CKSW5Y5GWRX3RZ7NZZT26XSAREXXK7KDMMQ7KGLP4DGX",
Networks.TESTNET,
["dev.vibrantapp.com"],
"webauth-dev.vibrantapp.com"
);
console.log(">>>>> readChallengeTx response: ", resp);
} catch (error) {
console.log("X X X ERROR while reading challenge transaction: ", error);
}
},[]);
return (
<SafeAreaView style={backgroundStyle}>
<StatusBar
barStyle={isDarkMode ? 'light-content' : 'dark-content'}
backgroundColor={backgroundStyle.backgroundColor}
/>
<ScrollView
contentInsetAdjustmentBehavior="automatic"
style={backgroundStyle}>
<Header />
<View
style={{
backgroundColor: isDarkMode ? Colors.black : Colors.white,
}}>
{xdrParsingSuccess !== undefined &&
<View style={styles.sectionContainer}>
<Text
style={[
styles.sectionTitle,
{
color: xdrParsingSuccess ? "green" : "red",
},
]}>
{xdrParsingSuccess ? "XDR Parsing Success! :)" : "XDR Parsing Failed :'("}
</Text>
<Text
style={[
styles.sectionDescription,
{
color: xdrParsingSuccess ? "green" : "red",
},
]}>
<Text style={styles.highlight}>EXPECTED:</Text> {expectedOperationDataValue}
</Text>
<Text
style={[
styles.sectionDescription,
{
color: xdrParsingSuccess ? "green" : "red",
},
]}>
<Text style={styles.highlight}>RECEIVED:</Text> {stringOutcome}
</Text>
</View>
}
<Section title="Step One">
Edit <Text style={styles.highlight}>App.tsx</Text> to change this
screen and then come back to see your edits. Here we go!
</Section>
<Section title="See Your Changes">
<ReloadInstructions />
</Section>
<Section title="Debug">
<DebugInstructions />
</Section>
<Section title="Learn More">
Read the docs to discover what to do next:
</Section>
<LearnMoreLinks />
</View>
</ScrollView>
</SafeAreaView>
);
}
const styles = StyleSheet.create({
sectionContainer: {
marginTop: 32,
paddingHorizontal: 24,
},
sectionTitle: {
fontSize: 24,
fontWeight: '600',
},
sectionDescription: {
marginTop: 8,
fontSize: 18,
fontWeight: '400',
},
highlight: {
fontWeight: '700',
},
});
export default App;