|
| 1 | +--- |
| 2 | +title: Stellar |
| 3 | +description: Guide for using zkFetch with Stellar blockchain data and services |
| 4 | +--- |
| 5 | + |
| 6 | +import { Step, Steps } from "fumadocs-ui/components/steps"; |
| 7 | + |
| 8 | +## Pre-requisite |
| 9 | + |
| 10 | +You can access the code on Github: |
| 11 | + |
| 12 | +- [zkFetch Stellar Example](https://github.com/reclaimprotocol/zkfetch-stellar-example). |
| 13 | + |
| 14 | +You will need a valid Stellar account seed phrase. Make sure you have enough funds to sign and send transactions. [Freighter](https://www.freighter.app/) wallet is a convenient option. |
| 15 | + |
| 16 | +## Code Exploration |
| 17 | + |
| 18 | +<Steps> |
| 19 | + |
| 20 | +### /src/requestProof.js |
| 21 | + |
| 22 | +This is the core part of the process, this code snippet shows how to fetch the latest XML price from CoinGecko's API in the form of a Reclaim proof. Once a proof is fetched, it gets written to `/src/proof.json`. |
| 23 | + |
| 24 | +```js copy |
| 25 | +// Example URL to fetch the data from |
| 26 | +const url = |
| 27 | + "https://api.coingecko.com/api/v3/simple/price?ids=stellar&vs_currencies=usd"; |
| 28 | + |
| 29 | +// Generate the proof |
| 30 | +const proof = await reclaimClient.zkFetch( |
| 31 | + url, |
| 32 | + { method: "GET" }, |
| 33 | + { |
| 34 | + responseMatches: [ |
| 35 | + { |
| 36 | + type: "regex", |
| 37 | + value: '\\{"stellar":\\{"usd":(?<price>[\\d\\.]+)\\}\\}', |
| 38 | + }, |
| 39 | + ], |
| 40 | + } |
| 41 | +); |
| 42 | +``` |
| 43 | + |
| 44 | +### /src/utils.js |
| 45 | + |
| 46 | +A couple of Reclaim-specific methods for parsing proofs. |
| 47 | + |
| 48 | +```js copy |
| 49 | + |
| 50 | +// Returns the ECDSA signature recovery ID |
| 51 | +export const getRecId = (signature) => { |
| 52 | + ... |
| 53 | +} |
| 54 | + |
| 55 | +// Removes the `0x` prefix from signature |
| 56 | +export const formatSignature = (signature) => { |
| 57 | + ... |
| 58 | +} |
| 59 | + |
| 60 | +// Serializes the claim |
| 61 | +export const getSerializedClaim = (proof) => { |
| 62 | + ... |
| 63 | +} |
| 64 | + |
| 65 | +// Returns the Keccak256 hash of the prefixed original message |
| 66 | +export const getHash = (serializedClaim) => { |
| 67 | + ... |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +### /src/verifyProof.js |
| 72 | + |
| 73 | +The main script, it builds, signs, and sends the verification transaction to the network. |
| 74 | + |
| 75 | +```js copy |
| 76 | +const tx = txBuilder |
| 77 | + .addOperation( |
| 78 | + contract.call( |
| 79 | + FUNCTION_NAME, |
| 80 | + ...[ |
| 81 | + StellarSdk.nativeToScVal(message, { type: "bytes" }), |
| 82 | + StellarSdk.nativeToScVal(signature, { type: "bytes" }), |
| 83 | + StellarSdk.nativeToScVal(recId, { type: "u32" }), |
| 84 | + ] |
| 85 | + ) |
| 86 | + ) |
| 87 | + .setTimeout(StellarSdk.TimeoutInfinite) |
| 88 | + .build(); |
| 89 | +``` |
| 90 | + |
| 91 | +</Steps> |
| 92 | + |
| 93 | +## Try it |
| 94 | + |
| 95 | +<Steps> |
| 96 | + |
| 97 | +### Clone the repo |
| 98 | + |
| 99 | +```bash |
| 100 | +git clone https://github.com/reclaimprotocol/zkfetch-stellar-example.git |
| 101 | + |
| 102 | +cd zkfetch-stellar-example |
| 103 | + |
| 104 | +npm install |
| 105 | +``` |
| 106 | + |
| 107 | +### Download ZK files |
| 108 | +These are crucial for proof requesting, a device should have them locally to request Reclaim proofs. |
| 109 | + |
| 110 | +```bash copy |
| 111 | +node node_modules/@reclaimprotocol/zk-symmetric-crypto/lib/scripts/download-files |
| 112 | +``` |
| 113 | + |
| 114 | +### Add your seed phrase |
| 115 | +Add the secret phrase of your signing account to `.env`. |
| 116 | +```bash |
| 117 | +SEEDPHRASE= |
| 118 | +``` |
| 119 | + |
| 120 | +### Request a proof |
| 121 | +Change directory into `/src` and run: |
| 122 | +```bash |
| 123 | +node requestProof |
| 124 | +``` |
| 125 | + |
| 126 | +### Verify the proof |
| 127 | +Once you have the proof in `proof.json`, run the following to verify on-chain: |
| 128 | +```bash |
| 129 | +node verifyProof |
| 130 | +``` |
| 131 | +</Steps> |
0 commit comments