Skip to content

Commit 5a51006

Browse files
author
Maxwell Dulin
committed
Add parsing tests to files
1 parent 8710b33 commit 5a51006

File tree

4 files changed

+243
-64
lines changed

4 files changed

+243
-64
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
2+
module wormhole_transceiver::wormhole_transceiver_info {
3+
use wormhole::external_address::{Self,ExternalAddress};
4+
use wormhole::bytes;
5+
use wormhole::cursor::{Self, Cursor};
6+
use ntt_common::bytes4::{Self};
7+
use ntt::mode::{Self, Mode};
8+
9+
const INFO_PREFIX: vector<u8> = x"9C23BD3B";
10+
11+
#[error]
12+
const EIncorrectPrefix: vector<u8>
13+
= b"incorrect prefix";
14+
15+
// https://github.com/wormhole-foundation/native-token-transfers/blob/b6b681a77e8289869f35862b261b8048e3f5d398/evm/src/libraries/TransceiverStructs.sol#L409C12-L409C27
16+
public struct WormholeTransceiverInfo has drop{
17+
manager_address: ExternalAddress,
18+
manager_mode: Mode,
19+
token_address: ExternalAddress,
20+
token_decimals: u8
21+
}
22+
23+
public(package) fun new(manager_address: ExternalAddress, mode: Mode, token_address: ExternalAddress, decimals: u8): WormholeTransceiverInfo{
24+
WormholeTransceiverInfo {
25+
manager_address: manager_address,
26+
manager_mode: mode,
27+
token_address: token_address,
28+
token_decimals: decimals
29+
}
30+
}
31+
32+
public fun to_bytes(self: &WormholeTransceiverInfo): vector<u8> {
33+
let mut buf = vector::empty<u8>();
34+
35+
buf.append(INFO_PREFIX);
36+
buf.append(self.manager_address.to_bytes()); // decimals and amount
37+
buf.append(self.manager_mode.serialize()); // 32 bytes
38+
buf.append(self.token_address.to_bytes()); // 32 bytes
39+
bytes::push_u8(&mut buf, self.token_decimals); // 2 bytes
40+
buf
41+
}
42+
43+
public fun take_bytes(cur: &mut Cursor<u8>): WormholeTransceiverInfo {
44+
let ntt_prefix = bytes4::take(cur);
45+
assert!(ntt_prefix.to_bytes() == INFO_PREFIX, EIncorrectPrefix);
46+
let manager_address = external_address::take_bytes(cur);
47+
let mode = mode::parse(bytes::take_bytes(cur, 1));
48+
let token_address = external_address::take_bytes(cur);
49+
let token_decimals = bytes::take_u8(cur);
50+
51+
WormholeTransceiverInfo {
52+
manager_address: manager_address,
53+
manager_mode: mode,
54+
token_address: token_address,
55+
token_decimals: token_decimals
56+
}
57+
}
58+
59+
public fun parse(buf: vector<u8>): WormholeTransceiverInfo {
60+
let mut cur = cursor::new(buf);
61+
let info = take_bytes(&mut cur);
62+
cur.destroy_empty();
63+
info
64+
}
65+
66+
#[test]
67+
public fun test_round_trip() {
68+
let reg = new(external_address::from_address(@102), mode::burning(), external_address::from_address(@304), 9);
69+
70+
let reg_bytes = reg.to_bytes();
71+
72+
let reg_round_trip = parse(reg_bytes);
73+
74+
assert!(reg.manager_address == reg_round_trip.manager_address);
75+
assert!(reg.manager_mode == reg_round_trip.manager_mode);
76+
assert!(reg.token_address == reg_round_trip.token_address);
77+
assert!(reg.token_decimals == reg_round_trip.token_decimals);
78+
}
79+
80+
#[test]
81+
public fun test_raw_to_bytes() {
82+
let mut raw_bytes = vector::empty<u8>();
83+
84+
let prefix = vector<u8>[0x9c, 0x23, 0xbd, 0x3b];
85+
let manager_address = vector<u8>[0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2];
86+
let mode = vector<u8>[0x1];
87+
88+
let token_address = vector<u8>[0x4, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x5];
89+
let token_decimals = vector<u8>[0x3];
90+
91+
vector::append(&mut raw_bytes, prefix);
92+
vector::append(&mut raw_bytes, manager_address);
93+
vector::append(&mut raw_bytes, mode);
94+
vector::append(&mut raw_bytes, token_address);
95+
vector::append(&mut raw_bytes, token_decimals);
96+
97+
98+
let reg = parse(raw_bytes);
99+
100+
assert!(reg.manager_address.to_bytes() == manager_address);
101+
assert!(reg.manager_mode == mode::burning());
102+
assert!(reg.token_address.to_bytes() == token_address);
103+
assert!(reg.token_decimals == 3);
104+
}
105+
106+
#[test]
107+
public fun test_reg_to_raw_bytes(){
108+
let mut raw_bytes = vector::empty<u8>();
109+
110+
111+
let prefix = vector<u8>[0x9c, 0x23, 0xbd, 0x3b];
112+
let manager_address = vector<u8>[0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2];
113+
let mode = vector<u8>[0x0];
114+
115+
let token_address = vector<u8>[0x4, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x5];
116+
let token_decimals = vector<u8>[0x9];
117+
118+
vector::append(&mut raw_bytes, prefix);
119+
vector::append(&mut raw_bytes, manager_address);
120+
vector::append(&mut raw_bytes, mode);
121+
vector::append(&mut raw_bytes, token_address);
122+
vector::append(&mut raw_bytes, token_decimals);
123+
124+
// Create value
125+
let reg = new(external_address::from_address(@0x0100000000000000000000000000000000000000000000000000000000000002), mode::locking(), external_address::from_address(@0x0400000000000000000000000000000000000000000000000000000000000005), 9);
126+
127+
assert!(reg.manager_address.to_bytes() == manager_address);
128+
assert!(reg.manager_mode == mode::locking());
129+
assert!(reg.token_address.to_bytes() == token_address);
130+
assert!(reg.token_decimals == 9);
131+
132+
let derived = to_bytes(&reg);
133+
assert!(derived == raw_bytes);
134+
}
135+
136+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
2+
module wormhole_transceiver::wormhole_transceiver_registration {
3+
use wormhole::external_address::{Self,ExternalAddress};
4+
use wormhole::bytes;
5+
use wormhole::cursor::{Self, Cursor};
6+
use ntt_common::bytes4::{Self};
7+
const REGISTRATION_PREFIX: vector<u8> = x"18fc67c2";
8+
9+
#[error]
10+
const EIncorrectPrefix: vector<u8>
11+
= b"incorrect prefix";
12+
13+
// https://github.com/wormhole-foundation/native-token-transfers/blob/b6b681a77e8289869f35862b261b8048e3f5d398/evm/src/libraries/TransceiverStructs.sol#L441
14+
public struct WormholeTransceiverRegistration has drop {
15+
transceiver_chain_id: u16,
16+
transceiver_address: ExternalAddress
17+
}
18+
19+
public(package) fun new(transceiver_chain_id: u16, transceiver_address: ExternalAddress): WormholeTransceiverRegistration{
20+
WormholeTransceiverRegistration {
21+
transceiver_chain_id: transceiver_chain_id,
22+
transceiver_address: transceiver_address
23+
}
24+
}
25+
26+
public fun to_bytes(self: &WormholeTransceiverRegistration): vector<u8> {
27+
let mut buf = vector::empty<u8>();
28+
29+
buf.append(REGISTRATION_PREFIX);
30+
bytes::push_u16_be(&mut buf, self.transceiver_chain_id);
31+
buf.append(self.transceiver_address.to_bytes());
32+
buf
33+
}
34+
35+
public fun take_bytes(cur: &mut Cursor<u8>): WormholeTransceiverRegistration {
36+
let ntt_prefix = bytes4::take(cur);
37+
assert!(ntt_prefix.to_bytes() == REGISTRATION_PREFIX, EIncorrectPrefix);
38+
let chain_id = bytes::take_u16_be(cur);
39+
let transceiver_address = external_address::take_bytes(cur);
40+
41+
WormholeTransceiverRegistration {
42+
transceiver_chain_id: chain_id,
43+
transceiver_address: transceiver_address
44+
}
45+
}
46+
47+
public fun parse(buf: vector<u8>): WormholeTransceiverRegistration {
48+
let mut cur = cursor::new(buf);
49+
let reg = take_bytes(&mut cur);
50+
cur.destroy_empty();
51+
reg
52+
}
53+
54+
#[test]
55+
public fun test_round_trip() {
56+
let reg = new(1,external_address::from_address(@102));
57+
58+
let reg_bytes = reg.to_bytes();
59+
60+
let reg_round_trip = parse(reg_bytes);
61+
62+
assert!(reg.transceiver_address == reg_round_trip.transceiver_address);
63+
assert!(reg.transceiver_chain_id == reg_round_trip.transceiver_chain_id);
64+
}
65+
66+
#[test]
67+
public fun test_raw_to_bytes() {
68+
let mut raw_bytes = vector::empty<u8>();
69+
70+
let prefix = vector<u8>[0x18, 0xfc, 0x67, 0xc2];
71+
let transceiver_address = vector<u8>[0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2];
72+
let chain_id = vector<u8>[0x4, 0x56];
73+
vector::append(&mut raw_bytes, prefix);
74+
vector::append(&mut raw_bytes, chain_id);
75+
vector::append(&mut raw_bytes, transceiver_address);
76+
77+
let reg = parse(raw_bytes);
78+
79+
assert!(reg.transceiver_address.to_bytes() == transceiver_address);
80+
assert!(reg.transceiver_chain_id == 0x456);
81+
}
82+
83+
#[test]
84+
public fun test_reg_to_raw_bytes(){
85+
let mut raw_bytes = vector::empty<u8>();
86+
87+
// Test bytes
88+
let prefix = vector<u8>[0x18, 0xfc, 0x67, 0xc2];
89+
let transceiver_address = vector<u8>[0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2];
90+
let chain_id = vector<u8>[0x4, 0x56];
91+
vector::append(&mut raw_bytes, prefix);
92+
vector::append(&mut raw_bytes, chain_id);
93+
vector::append(&mut raw_bytes, transceiver_address);
94+
95+
// Create value
96+
let reg = new(0x456,external_address::from_address(@0x0100000000000000000000000000000000000000000000000000000000000002));
97+
assert!(reg.transceiver_address.to_bytes() == transceiver_address);
98+
assert!(reg.transceiver_chain_id == 0x456);
99+
100+
let derived = to_bytes(&reg);
101+
assert!(derived == raw_bytes);
102+
}
103+
}

sui/packages/wormhole_transceiver/sources/wormhole_transceiver.move

+4-4
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,12 @@ module wormhole_transceiver::wormhole_transceiver {
140140

141141
let external_address_manager_address = wormhole::external_address::from_address(manager_address);
142142

143-
let transceiver_info_struct = wormhole_transceiver::transceiver_structs::new_transceiver_info(external_address_manager_address, *manager_state.borrow_mode(), wormhole::external_address::from_id(object::id(coin_meta)), coin_meta.get_decimals());
143+
let transceiver_info_struct = wormhole_transceiver::wormhole_transceiver_info::new(external_address_manager_address, *manager_state.borrow_mode(), wormhole::external_address::from_id(object::id(coin_meta)), coin_meta.get_decimals());
144144

145145
let message_ticket = wormhole::publish_message::prepare_message(
146146
&mut state.emitter_cap,
147147
0,
148-
transceiver_info_struct.transceiver_info_to_bytes(),
148+
transceiver_info_struct.to_bytes(),
149149
);
150150
option::some(message_ticket)
151151
}
@@ -156,11 +156,11 @@ module wormhole_transceiver::wormhole_transceiver {
156156
*/
157157
fun broadcast_peer(chain_id: u16, peer_address: ExternalAddress, state: &mut State): Option<MessageTicket>{
158158

159-
let transceiver_registration_struct = wormhole_transceiver::transceiver_structs::new_transceiver_registration(chain_id, peer_address);
159+
let transceiver_registration_struct = wormhole_transceiver::wormhole_transceiver_registration::new(chain_id, peer_address);
160160
let message_ticket = wormhole::publish_message::prepare_message(
161161
&mut state.emitter_cap,
162162
0,
163-
transceiver_registration_struct.transceiver_registration_to_bytes(),
163+
transceiver_registration_struct.to_bytes(),
164164
);
165165
option::some(message_ticket)
166166
}

sui/packages/wormhole_transceiver/sources/wormhole_transceiver_structs.move

-60
This file was deleted.

0 commit comments

Comments
 (0)