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

Convert wormhole payload to borsh #109

Merged
merged 4 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 26 additions & 13 deletions evm/bridge-token-factory/contracts/BridgeTokenFactoryWormhole.sol
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,31 @@ contract BridgeTokenFactoryWormhole is BridgeTokenFactory {
}

function deployTokenExtension(string memory token, address tokenAddress) internal override {
bytes memory payload = bytes.concat(
bytes1(uint8(MessageType.DeployToken)),
Borsh.encodeString(token),
Borsh.encodeAddress(tokenAddress)
);
_wormhole.publishMessage{value: msg.value}(
wormholeNonce,
abi.encode(MessageType.DeployToken, token, tokenAddress),
payload,
_consistencyLevel
);

wormholeNonce++;
}

function finTransferExtension(BridgeTypes.FinTransferPayload memory payload) internal override {
bytes memory messagePayload = bytes.concat(
bytes1(uint8(MessageType.FinTransfer)),
Borsh.encodeAddress(payload.tokenAddress),
Borsh.encodeUint128(payload.amount),
Borsh.encodeString(payload.feeRecipient),
Borsh.encodeUint128(payload.nonce)
);
_wormhole.publishMessage{value: msg.value}(
wormholeNonce,
abi.encode(MessageType.FinTransfer, payload.token, payload.amount, payload.feeRecipient, payload.nonce),
messagePayload,
_consistencyLevel
);

Expand All @@ -68,19 +80,20 @@ contract BridgeTokenFactoryWormhole is BridgeTokenFactory {
string calldata message,
uint256 value
) internal override {
bytes memory payload = bytes.concat(
bytes1(uint8(MessageType.InitTransfer)),
Borsh.encodeAddress(sender),
Borsh.encodeAddress(tokenAddress),
Borsh.encodeUint128(nonce),
Borsh.encodeUint128(amount),
Borsh.encodeUint128(fee),
Borsh.encodeUint128(nativeFee),
Borsh.encodeString(recipient),
Borsh.encodeString(message)
);
_wormhole.publishMessage{value: value}(
wormholeNonce,
abi.encode(
MessageType.InitTransfer,
sender,
tokenAddress,
nonce,
amount,
fee,
nativeFee,
recipient,
message
),
payload,
_consistencyLevel
);

Expand Down
61 changes: 31 additions & 30 deletions near/omni-prover/wormhole-omni-prover-proxy/src/parsed_vaa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

use {
crate::byte_utils::ByteUtils,
alloy_sol_types::{sol, SolType},
borsh::BorshDeserialize,
near_sdk::env,
omni_types::{
prover_result::{DeployTokenMessage, FinTransferMessage, InitTransferMessage},
sol_address::SolAddress,
stringify, EvmAddress, Fee, OmniAddress, TransferMessage, H160,
},
}
};

// Validator Action Approval(VAA) data
Expand Down Expand Up @@ -111,49 +111,50 @@ impl ParsedVAA {
}
}

sol! {
struct InitTransferWh {
address sender;
address tokenAddress;
uint128 nonce;
uint128 amount;
uint128 fee;
uint128 nativeFee;
string recipient;
string message;
}
#[derive(Debug, BorshDeserialize)]
struct DeployTokenWh {
token: String,
token_address: EvmAddress,
}

struct FinTransferWh {
string token;
uint128 amount;
string recipient;
uint128 nonce;
}
#[derive(Debug, BorshDeserialize)]
struct FinTransferWh {
_token: String,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets add the type (first byte) to this struct and move the verification to the try_into.
see this #106 (comment)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still like verifying inside verify_vaa_callback. We can forget and remove the check inside the conversion methods and introduce a vulnerability this way. However, I don't mind making this check in both places

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

amount: u128,
recipient: String,
nonce: u128,
}

struct DeployTokenWh {
string token;
address tokenAddress;
}
#[derive(Debug, BorshDeserialize)]
struct InitTransferWh {
sender: EvmAddress,
token_address: EvmAddress,
nonce: u128,
amount: u128,
fee: u128,
native_fee: u128,
recipient: String,
message: String,
}

impl TryInto<InitTransferMessage> for ParsedVAA {
type Error = String;

fn try_into(self) -> Result<InitTransferMessage, String> {
let data: &[u8] = &self.payload[1..];
let transfer = InitTransferWh::abi_decode(data, true).map_err(stringify)?;
let transfer: InitTransferWh = borsh::from_slice(data).map_err(stringify)?;

Ok(InitTransferMessage {
transfer: TransferMessage {
token: to_omni_address(self.emitter_chain, &transfer.tokenAddress.0 .0),
token: to_omni_address(self.emitter_chain, &transfer.token_address.0),
amount: transfer.amount.into(),
fee: Fee {
fee: transfer.fee.into(),
native_fee: transfer.nativeFee.into(),
native_fee: transfer.native_fee.into(),
},
recipient: transfer.recipient.parse().map_err(stringify)?,
origin_nonce: transfer.nonce.into(),
sender: to_omni_address(self.emitter_chain, &transfer.sender.0 .0),
sender: to_omni_address(self.emitter_chain, &transfer.sender.0),
msg: transfer.message,
},
emitter_address: to_omni_address(self.emitter_chain, &self.emitter_address),
Expand All @@ -166,7 +167,7 @@ impl TryInto<FinTransferMessage> for ParsedVAA {

fn try_into(self) -> Result<FinTransferMessage, String> {
let data: &[u8] = &self.payload[1..];
let transfer = FinTransferWh::abi_decode(data, true).map_err(stringify)?;
let transfer: FinTransferWh = borsh::from_slice(data).map_err(stringify)?;

Ok(FinTransferMessage {
nonce: transfer.nonce.into(),
Expand All @@ -182,11 +183,11 @@ impl TryInto<DeployTokenMessage> for ParsedVAA {

fn try_into(self) -> Result<DeployTokenMessage, String> {
let data: &[u8] = &self.payload[1..];
let transfer = DeployTokenWh::abi_decode(data, true).map_err(stringify)?;
let transfer: DeployTokenWh = borsh::from_slice(data).map_err(stringify)?;

Ok(DeployTokenMessage {
token: transfer.token.parse().map_err(stringify)?,
token_address: to_omni_address(self.emitter_chain, &transfer.tokenAddress.0 .0),
token_address: to_omni_address(self.emitter_chain, &transfer.token_address.0),
emitter_address: to_omni_address(self.emitter_chain, &self.emitter_address),
})
}
Expand Down
Loading