Skip to content

Instantly share code, notes, and snippets.

@SuperstrongBE
Created August 23, 2023 23:31
Show Gist options
  • Save SuperstrongBE/a0b3ca99a325628128d0ca6acb2a8c20 to your computer and use it in GitHub Desktop.
Save SuperstrongBE/a0b3ca99a325628128d0ca6acb2a8c20 to your computer and use it in GitHub Desktop.
Attempt to resolve PSR in an expo app
import React, {useState, useEffect} from "react";
import {Api, JsonRpc, JsSignatureProvider} from "@proton/js";
import {SigningRequest} from "@proton/signing-request";
import pako from "pako";
import {Text, View, StyleSheet, Button} from "react-native";
import {BarCodeScanner} from "expo-barcode-scanner";
import * as encodeAndDecodeModule from "fastestsmallesttextencoderdecoder";
const fetch = require("node-fetch"); // ou d'autres alternatives selon l'environnement
const endpoints = ["https://api.protonnz.com"]; // Remplacez par l'URL de votre nœud EOSIO
const rpc = new JsonRpc(endpoints);
const api = new Api({rpc, signatureProvider: null}); // Pas de fournisseur de signature car nous ne signons rien ici
export default function App() {
const [hasPermission, setHasPermission] = useState(null);
const [scanned, setScanned] = useState(false);
const textEncoder = encodeAndDecodeModule.encode;
// string decoder
const textDecoder = encodeAndDecodeModule.decode;
const opts = {
/*
when textEncoder and text decoder are set, the SigningRequest.identity fail with an [AbortError: Aborted]
When not set it end with [Failed to construct 'TextDecoder': the 'fatal' option is unsupported.] and the ABI is empty
*/
//textEncoder,
//textDecoder,
zlib: {
deflateRaw: data => pako.deflate(data),
inflateRaw: data => pako.inflate(data),
},
// Customizable ABI Provider used to retrieve contract data
abiProvider: {
getAbi: async account => await api.rpc.get_abi(account),
},
scheme: "proton",
};
async function decodePSR(uri, opts = {}) {
const decoded = SigningRequest.identity(uri, opts);
console.log("is decoded", decoded);
const head = (await rpc.get_info(true)).head_block_num;
console.log("have head", head);
const block = await rpc.get_block(head);
console.log("have bloc", block);
const abis = await decoded.fetchAbis();
console.log("have ABI", abis);
const authorization = {
actor: "rockerone",
permission: "active",
};
const resolved = await decoded.resolve(abis, authorization, block);
console.log(resolved);
}
useEffect(() => {
(async () => {
const {status} = await BarCodeScanner.requestPermissionsAsync();
setHasPermission(status === "granted");
})();
}, []);
const handleBarCodeScanned = async ({type, data}) => {
setScanned(true);
console.log(data);
await decodePSR(data, opts);
};
if (hasPermission === null) {
return <Text>Requesting for camera permission</Text>;
}
if (hasPermission === false) {
return <Text>No access to camera</Text>;
}
return (
<View style={styles.container}>
<BarCodeScanner
onBarCodeScanned={scanned ? undefined : handleBarCodeScanned}
style={StyleSheet.absoluteFillObject}
/>
{scanned && (
<Button title={"Tap to Scan Again"} onPress={() => setScanned(false)} />
)}
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: "column",
justifyContent: "center",
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment