Skip to content

Commit

Permalink
minor cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
eshaben committed Apr 5, 2024
1 parent f2549e3 commit 877b98c
Show file tree
Hide file tree
Showing 6 changed files with 200 additions and 191 deletions.
Original file line number Diff line number Diff line change
@@ -1,97 +1,97 @@
[
{
"inputs": [],
"name": "latestRelayBlockNumber",
"outputs": [
{
"internalType": "uint32",
"name": "relayBlockNumber",
"type": "uint32"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint32",
"name": "relayBlockNumber",
"type": "uint32"
},
{
"components": [
{
"internalType": "bytes32",
"name": "at",
"type": "bytes32"
},
{
"internalType": "bytes[]",
"name": "proof",
"type": "bytes[]"
}
],
"internalType": "struct RelayDataVerifier.ReadProof",
"name": "readProof",
"type": "tuple"
},
{
"internalType": "bytes[]",
"name": "keys",
"type": "bytes[]"
}
],
"name": "verifyEntries",
"outputs": [
{
"internalType": "bytes[]",
"name": "values",
"type": "bytes[]"
}
],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint32",
"name": "relayBlockNumber",
"type": "uint32"
},
{
"components": [
{
"internalType": "bytes32",
"name": "at",
"type": "bytes32"
},
{
"internalType": "bytes[]",
"name": "proof",
"type": "bytes[]"
}
],
"internalType": "struct RelayDataVerifier.ReadProof",
"name": "readProof",
"type": "tuple"
},
{
"internalType": "bytes",
"name": "key",
"type": "bytes"
}
],
"name": "verifyEntry",
"outputs": [
{
"internalType": "bytes",
"name": "value",
"type": "bytes"
}
],
"stateMutability": "nonpayable",
"type": "function"
}
]
{
inputs: [],
name: 'latestRelayBlockNumber',
outputs: [
{
internalType: 'uint32',
name: 'relayBlockNumber',
type: 'uint32',
},
],
stateMutability: 'view',
type: 'function',
},
{
inputs: [
{
internalType: 'uint32',
name: 'relayBlockNumber',
type: 'uint32',
},
{
components: [
{
internalType: 'bytes32',
name: 'at',
type: 'bytes32',
},
{
internalType: 'bytes[]',
name: 'proof',
type: 'bytes[]',
},
],
internalType: 'struct RelayDataVerifier.ReadProof',
name: 'readProof',
type: 'tuple',
},
{
internalType: 'bytes[]',
name: 'keys',
type: 'bytes[]',
},
],
name: 'verifyEntries',
outputs: [
{
internalType: 'bytes[]',
name: 'values',
type: 'bytes[]',
},
],
stateMutability: 'nonpayable',
type: 'function',
},
{
inputs: [
{
internalType: 'uint32',
name: 'relayBlockNumber',
type: 'uint32',
},
{
components: [
{
internalType: 'bytes32',
name: 'at',
type: 'bytes32',
},
{
internalType: 'bytes[]',
name: 'proof',
type: 'bytes[]',
},
],
internalType: 'struct RelayDataVerifier.ReadProof',
name: 'readProof',
type: 'tuple',
},
{
internalType: 'bytes',
name: 'key',
type: 'bytes',
},
],
name: 'verifyEntry',
outputs: [
{
internalType: 'bytes',
name: 'value',
type: 'bytes',
},
],
stateMutability: 'nonpayable',
type: 'function',
},
];
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// For reading local ABI file
import * as fs from 'fs';
import * as fs from 'fs';
// Import Ethers library, to interact with Moonbeam networks
import { ethers } from 'ethers';
import { ethers } from 'ethers';
// Import Polkadot library, to interact with relay chain
import { ApiPromise, WsProvider } from '@polkadot/api';;
import { ApiPromise, WsProvider } from '@polkadot/api';

const abi = JSON.parse(fs.readFileSync('./RelayChainDataVerifierABI.json'));
const privateKey = 'INSERT_PRIVATE_KEY';
Expand All @@ -14,11 +14,7 @@ const relayURL = 'wss://frag-moonbase-relay-rpc-ws.g.moonbase.moonbeam.network';
// Create Ethers provider and signer
const provider = new ethers.JsonRpcProvider(moonbeamURL);
const signer = new ethers.Wallet(privateKey, provider);
const precompileContract = new ethers.Contract(
precompileAddress,
abi,
signer
);
const precompileContract = new ethers.Contract(precompileAddress, abi, signer);

async function run() {
// Create provider for relay chain
Expand All @@ -28,18 +24,18 @@ async function run() {
// Get the storage key for a random account on relay chain
const key = api.query.system.account.key(
'5CBATpb3yvEM4mhX9Dw3tyuqiWKhq9YBG6ugSbodRUSbodoU'
)
// Find the latest available relay chain block number
const blockNum = await precompileContract.latestRelayBlockNumber()
);
// Find the latest available relay chain block number from Moonbeam
const blockNum = await precompileContract.latestRelayBlockNumber();

// Get the blockHash and storage proof from relay chain
// Get the block hash and storage proof from relay chain
const blockHash = await api.rpc.chain.getBlockHash(blockNum);
const proof = await api.rpc.state.getReadProof([key], blockHash);

// This tx will be rejected if the verification failed
let receipt = await precompileContract.verifyEntry(blockNum, proof, key)
const receipt = await precompileContract.verifyEntry(blockNum, proof, key);
await receipt.wait();
console.log(receipt.hash)
console.log(receipt.hash);
}

await run();
await run();
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import * as fs from 'fs';
import { Web3 } from 'web3';
// Import Polkadot library, to interact with relay chain
import { ApiPromise, WsProvider } from '@polkadot/api';
import { constants } from 'os';

const abi = JSON.parse(fs.readFileSync('./RelayChainDataVerifierABI.json'));
const privateKey = 'INSERT_PRIVATE_KEY';
Expand All @@ -14,39 +13,47 @@ const relayURL = 'wss://frag-moonbase-relay-rpc-ws.g.moonbase.moonbeam.network';

// Create Web3js provider and signer
const web3 = new Web3(moonbeamURL);
const precompileContract = new web3.eth.Contract(abi, precompileAddress)
const precompileContract = new web3.eth.Contract(abi, precompileAddress);

const account = web3.eth.accounts.privateKeyToAccount(privateKey);

async function run() {
// Create provider for relay chain
const wsProvider = new WsProvider(relayURL);
const api = await ApiPromise.create({ provider: wsProvider });

// Get the storage key for a random account on relay chain
const key = api.query.system.account.key(
'5CBATpb3yvEM4mhX9Dw3tyuqiWKhq9YBG6ugSbodRUSbodoU'
)
// Find the latest available block number(relay chain) from moonbeam
const blockNum = await precompileContract.methods.latestRelayBlockNumber().call()

// Get the blockHash and storage proof from relay chain
const blockHash = await api.rpc.chain.getBlockHash(blockNum);
const proof = await api.rpc.state.getReadProof([key], blockHash);

const callObject = {
to: precompileAddress,
data: precompileContract.methods.verifyEntry(blockNum, proof, key).encodeABI(),
gas: await precompileContract.methods.verifyEntry(blockNum, proof, key).estimateGas(),
gasPrice: await web3.eth.getGasPrice(),
nonce: await web3.eth.getTransactionCount(account.address),
}

// This tx will be rejected if the verification failed
const tx = await web3.eth.accounts.signTransaction(callObject, account.privateKey);
const receipt = await web3.eth.sendSignedTransaction(tx.rawTransaction)
console.log(receipt.transactionHash)

// Create provider for relay chain
const wsProvider = new WsProvider(relayURL);
const api = await ApiPromise.create({ provider: wsProvider });

// Get the storage key for a random account on relay chain
const key = api.query.system.account.key(
'5CBATpb3yvEM4mhX9Dw3tyuqiWKhq9YBG6ugSbodRUSbodoU'
);
// Find the latest available relay chain block number from Moonbeam
const blockNum = await precompileContract.methods
.latestRelayBlockNumber()
.call();

// Get the block hash and storage proof from relay chain
const blockHash = await api.rpc.chain.getBlockHash(blockNum);
const proof = await api.rpc.state.getReadProof([key], blockHash);

const callObject = {
to: precompileAddress,
data: precompileContract.methods
.verifyEntry(blockNum, proof, key)
.encodeABI(),
gas: await precompileContract.methods
.verifyEntry(blockNum, proof, key)
.estimateGas(),
gasPrice: await web3.eth.getGasPrice(),
nonce: await web3.eth.getTransactionCount(account.address),
};

// This tx will be rejected if the verification failed
const tx = await web3.eth.accounts.signTransaction(
callObject,
account.privateKey
);
const receipt = await web3.eth.sendSignedTransaction(tx.rawTransaction);
console.log(receipt.transactionHash);
}

await run();
await run();
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
params=["5CBATpb3yvEM4mhX9Dw3tyuqiWKhq9YBG6ugSbodRUSbodoU"],
)

# Find the latest available block number(relay chain) from moonbeam
# Find the latest available relay chain block number from Moonbeam
blockNum = precompileContract.functions.latestRelayBlockNumber().call()

# Get the blockHash from relay chain
# Get the block hash from relay chain
blockHash = substrate.get_block_hash(blockNum)

# Get the storage proof from relay chain
Expand Down

This file was deleted.

Loading

0 comments on commit 877b98c

Please sign in to comment.