Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nffl demo examples #265

Merged
merged 13 commits into from
Oct 3, 2024
1 change: 1 addition & 0 deletions demo-nffl-cli/.gitignore
Hyodar marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
41 changes: 41 additions & 0 deletions demo-nffl-cli/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
## How to build
```sh
npm install
npm link
```

## How to run
### getStorageValue
Get the storage value of a target contract at a specified block height from the source chain and verifies it on the destination chain
```sh
demo-nffl-cli getStorageValue\
--srcRpcUrl https://sepolia.optimism.io\
--contractAddress 0xB90101779CC5EB84162f72A80e44307752b778b6\
--storageKey 0x0000000000000000000000000000000000000000000000000000000000000000\
--blockHeight 13905480\
--dstRpcUrl https://sepolia-rollup.arbitrum.io/rpc\
--nfflRegistryRollup 0x23e252b4Ec7cDd3ED84f039EF53DEa494CE878E0\
--aggregator http://127.0.0.1:4002
```

### updateStateRoot
Update the state root for a given rollup in the nfflRegistryRollup contract
```sh
demo-nffl-cli updateStateRoot\
--rpcUrl https://sepolia-rollup.arbitrum.io/rpc\
--rollupId 11155420\
--blockHeight 14095733\
--nfflRegistryRollup 0x23e252b4Ec7cDd3ED84f039EF53DEa494CE878E0\
--aggregator http://127.0.0.1:4002\
--seedPhrase <signer seed phrase>
Hyodar marked this conversation as resolved.
Show resolved Hide resolved
```

### updateOperatorSet
Update the operator set for the nfflRegistryRollup contract
```sh
demo-nffl-cli updateOperatorSet\
--rpcUrl https://sepolia-rollup.arbitrum.io/rpc\
--nfflRegistryRollup 0x23e252b4Ec7cDd3ED84f039EF53DEa494CE878E0\
--aggregator http://127.0.0.1:4002\
--seedPhrase <signer seed phrase>
Hyodar marked this conversation as resolved.
Show resolved Hide resolved
```
3 changes: 3 additions & 0 deletions demo-nffl-cli/abi/NFFLRegistryRollup.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

66 changes: 66 additions & 0 deletions demo-nffl-cli/getStorageValue.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
const { ethers } = require('ethers');
const {NFFLRegistryRollupABI} = require('./abi/NFFLRegistryRollup');
//const {arbContracts} = require('./contracts');
Hyodar marked this conversation as resolved.
Show resolved Hide resolved
const RLP = require('rlp');

/*
* Get the storage value of a target contract at a specified block height
* from the source chain and verifies it on the destination chain
*/
async function getStorageValue(options) {
//
// Get proof
//
const srcProvider = new ethers.JsonRpcProvider(options.srcRpcUrl);
//chainId
const { chainId } = await srcProvider.getNetwork();
// Prepear params
Hyodar marked this conversation as resolved.
Show resolved Hide resolved
const params = [
options.contractAddress,
[options.storageKey],
`0x${Number(options.blockHeight).toString(16)}`
];
// Send the RPC request
const proof = await srcProvider.send("eth_getProof", params);
// Encode proof to RLP
const rlpStorageProof = RLP.encode(proof.storageProof[0].proof);
const rlpAccountProof = RLP.encode(proof.accountProof);

//
// Prove
//
// Get RegistryRollup contract
const dstProvider = new ethers.JsonRpcProvider(options.dstRpcUrl);
const registryRollup = new ethers.Contract(options.nfflRegistryRollup, NFFLRegistryRollupABI, dstProvider);
// Fetch data
const response = await fetch(`${options.aggregator}/aggregation/state-root-update?rollupId=${chainId}&blockHeight=${options.blockHeight}`);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
// Build message
const nearDaTransactionId = '0x'+Buffer.from(data.Message.NearDaTransactionId).toString('hex');
const nearDaCommitment = '0x'+Buffer.from(data.Message.NearDaCommitment).toString('hex');
const stateRoot = '0x'+Buffer.from(data.Message.StateRoot).toString('hex');
const message = {
rollupId: data.Message.RollupId,
blockHeight: data.Message.BlockHeight,
timestamp:data.Message.Timestamp,
nearDaTransactionId,
nearDaCommitment,
stateRoot
};
// Build proof parameters
const proofParams = {
target: options.contractAddress,
storageKey: options.storageKey,
stateTrieWitness: rlpAccountProof,
storageTrieWitness: rlpStorageProof
}

// Get storage value
const tx = await registryRollup.getStorageValue(message,proofParams);
console.log(`Account ${options.contractAddress} storage slot ${options.storageKey} equals to ${tx}`);
Hyodar marked this conversation as resolved.
Show resolved Hide resolved
}

module.exports = {getStorageValue}
45 changes: 45 additions & 0 deletions demo-nffl-cli/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/env node

const { program } = require('commander');
const pkg = require('./package.json');
const {getStorageValue} = require('./getStorageValue');
const {updateStateRoot} = require('./updateStateRoot');
const {updateOperatorSet} = require('./updateOperatorSet');
Hyodar marked this conversation as resolved.
Show resolved Hide resolved

program
.version(pkg.version)
.description('NFFL demo CLI tool');

program
.command('getStorageValue')
.description('Get the storage value of a target contract at a specified block height from the source chain and verifies it on the destination chain')
.requiredOption('--srcRpcUrl <type>', 'Source RPC URL')
.requiredOption('--contractAddress <type>', 'Address of the target contract')
.requiredOption('--storageKey <type>', 'Storage key')
.requiredOption('--blockHeight <type>', 'Block height')
.requiredOption('--dstRpcUrl <type>', 'Destination RPC URL')
.requiredOption('--nfflRegistryRollup <type>', 'nfflRegistryRollup contract address on destination chain')
.requiredOption('--aggregator <type>', 'Aggregator REST API')
.action(getStorageValue);

program
.command('updateStateRoot')
.description('Update the state root for a given rollup in the nfflRegistryRollup contract')
.requiredOption('--rpcUrl <type>', 'RPC URL')
.requiredOption('--rollupId <type>', 'Rollup Id')
.requiredOption('--blockHeight <type>', 'blockHeight')
.requiredOption('--nfflRegistryRollup <type>', 'nfflRegistryRollup contract address on destination chain')
.requiredOption('--aggregator <type>', 'Aggregator REST API')
.requiredOption('--seedPhrase <type>', 'Seed phrase for message signing')
.action(updateStateRoot);

program
.command('updateOperatorSet')
.description('Update the operator set for the nfflRegistryRollup contract')
.requiredOption('--rpcUrl <type>', 'RPC URL')
.requiredOption('--nfflRegistryRollup <type>', 'nfflRegistryRollup contract address on destination chain')
.requiredOption('--aggregator <type>', 'Aggregator REST API')
.requiredOption('--seedPhrase <type>', 'Seed phrase for message signing')
.action(updateOperatorSet);

program.parse(process.argv);
Loading
Loading