Putting Everything Together

Finally, we will see how we can call our prover to generate a custom proof, submit to Brevis, and have Brevis's contracts call our app contract with our circuit output.

Call Our Prover to Generate a Proof

Edit index.ts. Initialize two clients: one for talking to our prover, one for talking to Brevis.

const prover = new Prover('localhost:33247');
const brevis = new Brevis('appsdkv2.brevis.network:9094');

Adding Data to Prove

To prove an account's age, we only need to supply a transaction of nonce 0 that is originated from the account.

// initialize a proof request to our prover
const proofReq = new ProofRequest();
// adding the tx of nonce 0
proofReq.addTransaction(
    new TransactionData({
        hash: '0x02869126ca667c76e819078d5326feb5d17f276ce5786de47e78334f15530e74',
        chain_id: 97,
        block_num: 42501074,
        nonce: 0,
        gas_tip_cap_or_gas_price: '0x01b19fb0c0',
        gas_fee_cap: '0x01b19fb0c0',
        gas_limit: 95174,
        from: '0x1bF81EA1F2F6Afde216cD3210070936401A14Bd4',
        to: '0xF7E9CB6b7A157c14BCB6E6bcf63c1C7c92E952f5',
        value: '0x5af3107a4000',
    }),
);

Calling Our Prover

const proofRes = await prover.prove(proofReq);

index.ts#L63-L82 handles submitting proof request to our prover

Submitting Your Proof to Brevis

There are still several items left unproven in our custom circuit. They are:

  • How do we know if the transaction is valid at all (signed by 0x6c28...)?

  • How do we know that the transaction is certainly on Ethereum?

  • How do we know the the transaction happened in block 19073244?

When you submit the proof generated by your circuit, Brevis's provers will wrap a proof around yours to further prove that the transaction is valid on the specified source chain and is certainly from the block we say it's from.

const brevisRes = await brevis.submit(proofReq, proofRes, 97, 97, 0, brevisPartnerKey, callbackAddress);
await brevis.wait(brevisRes.queryKey, 97);

The arguments 97 and 97 are the chain ids of the data source chain and the "destination chain" that we want to post the final proof to, respectively

Now, run the program

ts-node index.ts 
    0x02869126ca667c76e819078d5326feb5d17f276ce5786de47e78334f15530e74 
    TEST_ACCOUNT_AGE_KEY // brevis partner key: could be empty string
    0xeec66d9b615ff84909be1cb1fe633cc26150417d // callback address: could be empty string
Click to see console output
proof 0x0125f554ac2daebb4ef14f64d13d7da018a73bca3e4251cedef1811d685b6bb8301d32cac3fca17e8b6dc7e309bdb6990011db60f4914e3f75feaee73cc28dd69cb26ccd3fe4d9fa40504ddfaf5fc5bde40620305f45f6a4945466ded200d697008e27d8672d5a3a0a8e90b64b2b11d453b924f9922769c3c8e931a1c88bf8c18c6dee23950434937cc45a6d45fc54f1017860483d04ccf462ff009cc448781b901064478e2ee08b4b91648c8482b5b8a189577212ba28c212b12a04bab97f89001e5fa983da4c4964e57064cd35b18030fd7c73510d3cc93228d5cf2a505d589d428cbabe0abf49c442e527c88151ea014c5fadf27e574614cbd81192da7a5a647637ad5a668737e5d08c3452878eb637db5130475dd47d04dd213235877a8a014b2a28ca606afea735f922d3c9e2254a5beabf79b2e826271e365b42626bd70ca90b973d56ae7ba411adde877f2dea00f107c31c21966a81d199646bae2aeb91d13dd1849eb0a0eb9431b425221d2d247cec446a78de831d57793d5c3cfd9f01466acc987314446540f255318d2597e7096f044ba4955b1ec6432df7cf66442c2a499db126834ffc5cfe9f6f017bd800967141736f8710c063fd34c92973a4cc07c20a59923dcf5d2d6ca711027653f96ec8779995988dda66487d40d7978a002cc54000bf48f535141f435dc034689802b145ffb5d5b6b9a52671cf658b06f28667cc8f3ee871d3189675131d1b2a004f6208a4a504054c6940af5de9b07bc05398f8392d37543375f71fb50e4875700aa9523d06d8e63079014887d715960039b7a03e96ffbed39e2e320461eca6c39ea115f2eca262e5dde0d29756c05b941069b8c5d8b39b289ff554d34e39750075541e5aed6b8770e9dabd13d3afbcd53646d213ef2347da5271fda5708e39832bc2067f5265920b0d3624eb9bf2db008b02d4fab06c8f0a247e9ccf5a9835d3333232414517e9b6d9adb7b06ec8a6ff33a2a79c3fd5db6143c2a06a6b2abf0186af35a6663e0c83036ed3adeb678b9e67c3a96cb8f0c04a7d766cb20f46c0b6066ecd9edfa05eb8d0b9257dcae854000000080b272da4ff6ec1b0f663a159e25f414a2a607ed74100a9b8a326430303bf56d209676cabcba615d1f09ca334fbf3679215ed8eb587a27cf094514d86e089cee808a1ceef13406b09e97008c79a2588b25fe59b565ed4cc405e22ff74524dddec09d8e541a77c3b6e52ef0c8862c0a7556f7ec031d85de5263f25fd5ef69e94930f7a4e208c8fd6be24368c35ffbe8bb06f49b7e8fa481f9674a7f751244793790296dad23092cf0a3ed5bbcf68360b7146a9bb0c28488b9b1b7da5fe14cb22900c0844e3c5dd01c5327a31db62c877baa581d14c60dab807c7a1f325a490de4405e9e96279dd0266bcc225c8b6940bda801cdd6bdc30c290f476f766d3334e0c001269bf2de630aefc1b817e297ede748146e90f768f0399b01697cdb016bf59ae6366e73a120f048ff05e4c4f542b1c00ba396c30a300dcb333170d1c2c5eb79bcf3fdc2e1d1ea09ca40f90f40b7efbcb0c5cbf985c691a0f2fef70dd5ac3dd0830e57a9a48b7281eafe079e3609cfa61862b46bfede1c381b65710e219799900000001011ade821071a9b0ade2c87463c80ebf43a95a218c9d5e3f0ccbf9ae2858564993945880df5dd8fe46252710611fb537017af9a18ddb3ea1d35a47ef3a24c0c2f35bf72b0da6d418929b8757855af63c1a232cf720d03e7c30c00f77c84829b5
brevis query key {"wrappers_":null,"arrayIndexOffset_":-1,"array":["0x270fa93c3b9dc5cff6af36ead725368e74450ef9482f51e2d8080454b4fedaac",1723712659],"pivot_":1.7976931348623157e+308,"convertedPrimitiveFields_":{}}
query waiting for final tx
request success, tx 0x709992bb4bb4278d3c469c5a7c5fdbc0de5718c10ce6d91e959e2bf50c040fa3

If there is a partnership between the developer's project and Brevis, the developer can hand over the corresponding BREVIS_PARTNER_KEY and CALLBACK_ADDRESS to Brevis gateway when using submit provided by Brevis SDK. Then you can skip the subsequent sendRequest on-chain transaction

BREVIS_PARTNER_KEY and CALLBACK_ADDRESS are NOT required to send requests to the Brevis gateway. Developers can leave them as empty string

As we can see, our request is assigned a request id and the fee we are going to pay is 0 (this is only zero for the testnet). Our program is currently waiting for us to pay the fee.

Now, you can call BrevisRequest.sendRequest on the destination chain to pay for the proof. For demo purposes, we will just send the transaction through the BST Testnet explorer. You can find the address of BrevisRequest under Contract Addresses. You may also find the sample contract here.

After the transaction is sent, it takes around two minutes for the transaction to be confirmed and Brevis's final proof to be generated. We should see the console outputs keep updating.

...
request 0x2c9fb082f0e3873b1f63da45a2963e9bb2746baae3dab553e70c3e856e2cbac2 waiting for final tx
request 0x2c9fb082f0e3873b1f63da45a2963e9bb2746baae3dab553e70c3e856e2cbac2 success, tx 0x2631596212a162c633116ceaa58891b43a7cc02dcbf3e83b77d5c77bf596b6f7

Last updated