fully working evm to evm project

This commit is contained in:
spacemandev 2022-05-19 04:47:47 -05:00
parent a350744738
commit 5514a2e301
6 changed files with 126 additions and 34 deletions

View File

@ -4,7 +4,7 @@ pragma solidity ^0.8.0;
import "./Wormhole/IWormhole.sol";
contract Messenger {
string private solana_msg;
string private current_msg;
address private wormhole_core_bridge_address = address(0xC89Ce4735882C9F0f0FE26686c53074E09B0D550);
IWormhole core_bridge = IWormhole(wormhole_core_bridge_address);
uint32 nonce = 0;
@ -21,7 +21,7 @@ contract Messenger {
nonce = nonce+1;
}
function recieveEncodedMsg(bytes memory encodedMsg) public {
function receiveEncodedMsg(bytes memory encodedMsg) public {
(IWormhole.VM memory vm, bool valid, string memory reason) = core_bridge.parseAndVerifyVM(encodedMsg);
//1. Check Wormhole Guardian Signatures
@ -37,11 +37,11 @@ contract Messenger {
_completedMessages[vm.hash] = true;
//Do the thing
solana_msg = string(vm.payload);
current_msg = string(vm.payload);
}
function getSolanaMsg() public view returns (string memory){
return solana_msg;
function getCurrentMsg() public view returns (string memory){
return current_msg;
}
/**
Registers it's sibling applications on other chains as the only ones that can send this instance messages

View File

@ -2,9 +2,10 @@ import { exec } from "child_process";
import fs from "fs";
import { ethers } from 'ethers';
import {
CHAIN_ID_ETH,
getEmitterAddressEth
getEmitterAddressEth,
parseSequenceFromLogEth
} from "@certusone/wormhole-sdk";
import fetch from 'node-fetch';
async function main(){
let config = JSON.parse(fs.readFileSync('./xdapp.config.json').toString());
@ -26,7 +27,8 @@ async function main(){
if(out) {
console.log(out);
network.deployed_address = out.split("Deployed to: ")[1].split('\n')[0].trim();
network.deployedAddress = out.split("Deployed to: ")[1].split('\n')[0].trim();
network.emittedVAAs = []; //Resets the emittedVAAs
config.networks[process.argv[2]] = network;
fs.writeFileSync('./xdapp.config.json', JSON.stringify(config, null, 4));
}
@ -34,7 +36,7 @@ async function main(){
);
}
} else if (process.argv[3] == "register_chain") {
if(!network.deployed_address){
if(!network.deployedAddress){
throw new Error("Deploy to this network first!");
}
@ -43,26 +45,90 @@ async function main(){
.connect(new ethers.providers.JsonRpcProvider(network.rpc));
const targetNetwork = config.networks[process.argv[4]];
if(!targetNetwork.deployed_address){
if(!targetNetwork.deployedAddress){
throw new Error("Target Network not deployed yet!");
}
if(targetNetwork.type == "evm"){
const emitter_address = Buffer.from(getEmitterAddressEth(targetNetwork.deployed_address), "hex");
const emitter_address = Buffer.from(getEmitterAddressEth(targetNetwork.deployedAddress), "hex");
const messenger = new ethers.Contract(
network.deployed_address,
network.deployedAddress,
JSON.parse(fs.readFileSync('./chains/evm/out/Messenger.sol/Messenger.json').toString()).abi,
signer
);
const tx = await messenger.registerApplicationContracts(CHAIN_ID_ETH, emitter_address);
console.log("Registered EVM style Emitter: ", tx.hash);
const tx = await messenger.registerApplicationContracts(targetNetwork.wormholeChainId, emitter_address);
console.log(`Network(${process.argv[2]}) Registered EVM style Emitter: `, tx.hash);
}
}
} else if (process.argv[2] == "send_msg") {
} else if (process.argv[3] == "send_msg") {
if(!network.deployedAddress){
throw new Error("Deploy to this network first!");
}
} else if (process.argv[2] == "submit_vaa") {
} else if (process.argv[2] == "get_current_msg") {
if(network.type == "evm"){
const signer = new ethers.Wallet(network.privateKey)
.connect(new ethers.providers.JsonRpcProvider(network.rpc));
const messenger = new ethers.Contract(
network.deployedAddress,
JSON.parse(fs.readFileSync('./chains/evm/out/Messenger.sol/Messenger.json').toString()).abi,
signer
);
const tx = await (await messenger.sendMsg(Buffer.from(process.argv[4]))).wait();
await new Promise((r) => setTimeout(r, 5000));
const emitterAddr = getEmitterAddressEth(messenger.address);
const seq = parseSequenceFromLogEth(
tx,
network.bridgeAddress
);
const vaaBytes = await (
await fetch(
`${config.wormhole.restAddress}/v1/signed_vaa/${network.wormholeChainId}/${emitterAddr}/${seq}`
)
).json();
if(!network.emittedVAAs){
network.emittedVAAs = [vaaBytes.vaaBytes];
} else {
network.emittedVAAs.push(vaaBytes.vaaBytes);
}
config.networks[process.argv[2]] = network;
fs.writeFileSync('./xdapp.config.json', JSON.stringify(config, null, 2));
console.log(`Network(${process.argv[2]}) Emitted VAA: `, vaaBytes.vaaBytes);
}
} else if (process.argv[3] == "submit_vaa") {
if(!network.deployedAddress){
throw new Error("Deploy to this network first!");
}
if(network.type == "evm"){
const targetNetwork = config.networks[process.argv[4]];
const vaaBytes = isNaN(parseInt(process.argv[5])) ?
targetNetwork.emittedVAAs.pop() :
targetNetwork.emittedVAAs[parseInt(process.argv[5])];
const signer = new ethers.Wallet(network.privateKey)
.connect(new ethers.providers.JsonRpcProvider(network.rpc));
const messenger = new ethers.Contract(
network.deployedAddress,
JSON.parse(fs.readFileSync('./chains/evm/out/Messenger.sol/Messenger.json').toString()).abi,
signer
);
const tx = await messenger.receiveEncodedMsg(Buffer.from(vaaBytes, "base64"));
console.log(`Submitted VAA: ${vaaBytes}\nTX: ${tx.hash}`);
}
} else if (process.argv[3] == "get_current_msg") {
if(!network.deployedAddress){
throw new Error("Deploy to this network first!");
}
if(network.type == "evm"){
const signer = new ethers.Wallet(network.privateKey)
.connect(new ethers.providers.JsonRpcProvider(network.rpc));
const messenger = new ethers.Contract(
network.deployedAddress,
JSON.parse(fs.readFileSync('./chains/evm/out/Messenger.sol/Messenger.json').toString()).abi,
signer
);
console.log(`${process.argv[2]} Current Msg: `, await messenger.getCurrentMsg());
}
} else {
throw new Error("Unkown command!")
}

View File

@ -13,7 +13,8 @@
],
"dependencies": {
"@certusone/wormhole-sdk": "^0.3.3",
"ethers": "^5.6.6"
"ethers": "^5.6.6",
"node-fetch": "^2.6.7"
}
},
"node_modules/@babel/runtime": {

View File

@ -4,7 +4,7 @@
"description": "",
"main": "index.js",
"scripts": {
"deploy evm": "node xdapp.js deploy evm"
"test": "sh test.sh"
},
"keywords": [],
"author": "",
@ -15,6 +15,7 @@
"type": "module",
"dependencies": {
"@certusone/wormhole-sdk": "^0.3.3",
"ethers": "^5.6.6"
"ethers": "^5.6.6",
"node-fetch": "^2.6.7"
}
}

View File

@ -0,0 +1,11 @@
node messenger.js eth0 deploy
node messenger.js eth1 deploy
node messenger.js eth0 register_chain eth1
node messenger.js eth1 register_chain eth0
node messenger.js eth0 send_msg "From: eth0\nMsg: Hello World!"
node messenger.js eth1 submit_vaa eth0 latest
node messenger.js eth1 send_msg "From: eth1\nMsg: Hello World!"
node messenger.js eth0 submit_vaa eth1 latest
sleep 10
node messenger.js eth0 get_current_msg
node messenger.js eth1 get_current_msg

View File

@ -1,16 +1,29 @@
{
"networks": {
"eth0": {
"type": "evm",
"rpc": "http://147.182.160.210:8545",
"privateKey": "0x4f3edf983ac636a65a842ce7c78d9aa706d3b113bce9c46f30d7d21715b23b1d",
"deployed_address": "0xc0b3b62dd0400e4baa721ddec9b8a384147b23ff"
},
"eth1": {
"type": "evm",
"rpc": "http://147.182.160.210:8546",
"privateKey": "0x4f3edf983ac636a65a842ce7c78d9aa706d3b113bce9c46f30d7d21715b23b1d",
"deployed_address": "0x4339316e04cffb5961d1c41fef8e44bfa2a7fbd1"
}
"networks": {
"eth0": {
"type": "evm",
"wormholeChainId": 2,
"rpc": "http://147.182.160.210:8545",
"privateKey": "0x4f3edf983ac636a65a842ce7c78d9aa706d3b113bce9c46f30d7d21715b23b1d",
"bridgeAddress": "0xC89Ce4735882C9F0f0FE26686c53074E09B0D550",
"deployedAddress": "0xdd37b2eb92f97dd09ced1f1d20a73aa340b2311a",
"emittedVAAs": [
"AQAAAAABAIdmeszZttqU5FVcyea0U43u7+aP7W5VJYXm5SfeWV3yCCdFZft2CKMf+N9ZcVe8xAkvUOS5r1bLwifJH5OaXyEAAAAwAgAAAAAAAgAAAAAAAAAAAAAAAN03suuS+X3QnO0fHSCnOqNAsjEaAAAAAAAAAAABRnJvbTogZXRoMFxuTXNnOiBIZWxsbyBXb3JsZCE="
]
},
"eth1": {
"type": "evm",
"wormholeChainId": 4,
"rpc": "http://147.182.160.210:8546",
"privateKey": "0x4f3edf983ac636a65a842ce7c78d9aa706d3b113bce9c46f30d7d21715b23b1d",
"bridgeAddress": "0xC89Ce4735882C9F0f0FE26686c53074E09B0D550",
"deployedAddress": "0xed447acb0ceb49d77f317dd6d37cb64da7a7e7f8",
"emittedVAAs": [
"AQAAAAABAIg4d8PvMFF37BarCvW0jSUxvxEtvnDeWU9xp+dps8EUB6OGwOggQU+lVowzPI39biro5W7lc5iKZ1I7z6n4uu8BAAEJ/QAAAAAABAAAAAAAAAAAAAAAAO1EessM60nXfzF91tN8tk2np+f4AAAAAAAAAAABRnJvbTogZXRoMVxuTXNnOiBIZWxsbyBXb3JsZCE="
]
}
},
"wormhole": {
"restAddress": "http://147.182.160.210:7071"
}
}