Skip to content

Commit 8395240

Browse files
author
Maxwell Dulin
committed
Add demo of accountant changes
1 parent 980b40c commit 8395240

File tree

8 files changed

+120
-127
lines changed

8 files changed

+120
-127
lines changed

sui/packages/ntt/Move.lock

-45
This file was deleted.

sui/packages/ntt/sources/datatypes/mode.move

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module ntt::mode {
55
const EInvalidMode: vector<u8> =
66
b"Invalid mode byte in serialized state.";
77

8-
public enum Mode has copy, store {
8+
public enum Mode has copy, store, drop {
99
Locking,
1010
Burning
1111
}

sui/packages/ntt_common/Move.lock

-26
This file was deleted.

sui/packages/ntt_common/sources/contract_auth.move

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ module ntt_common::contract_auth {
3838
const EInvalidAuthType: vector<u8> =
3939
b"Invalid auth type";
4040

41-
fun get_auth_address<Auth>(): Option<address> {
41+
public fun get_auth_address<Auth>(): Option<address> {
4242
let fqt = type_name::get<Auth>();
4343

4444
let address_hex = fqt.get_address().into_bytes();

sui/packages/wormhole_transceiver/Move.lock

-45
This file was deleted.

sui/packages/wormhole_transceiver/Move.toml

+3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ subdir = "sui/wormhole"
2020
[dependencies.NttCommon]
2121
local = "../ntt_common"
2222

23+
[dependencies.Ntt]
24+
local = "../ntt"
25+
2326
[addresses]
2427
wormhole_transceiver = "0x0"
2528

sui/packages/wormhole_transceiver/sources/wormhole_transceiver.move

+54-9
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ module wormhole_transceiver::wormhole_transceiver {
88
use ntt_common::validated_transceiver_message::{Self, ValidatedTransceiverMessage};
99
use ntt_common::transceiver_message::{Self, PrefixOf};
1010
use ntt_common::transceiver_message_data;
11+
use ntt::state::{State as ManagerState};
12+
use sui::coin::{CoinMetadata};
1113

1214
public struct Auth has drop {}
1315

@@ -28,23 +30,24 @@ module wormhole_transceiver::wormhole_transceiver {
2830
State {
2931
id: object::new(ctx),
3032
peers: table::new(ctx),
31-
emitter_cap: wormhole::emitter::new(wormhole_state, ctx),
33+
emitter_cap: wormhole::emitter::new(wormhole_state, ctx), // Creates a new emitter cap for WH core. Acts as the *peer* on the other side.
3234
}
3335
}
3436

3537
public struct DeployerCap has key, store {
3638
id: UID
3739
}
3840

39-
fun init(ctx: &mut TxContext) {
41+
// Only callable by the 'creator' of the module.
42+
fun init(ctx: &mut TxContext) { // Made on creation of module
4043
let deployer = DeployerCap { id: object::new(ctx) };
41-
transfer::transfer(deployer, ctx.sender());
44+
transfer::transfer(deployer, tx_context::sender(ctx));
4245
}
4346

4447
#[allow(lint(share_owned))]
4548
public fun complete(deployer: DeployerCap, wormhole_state: &wormhole::state::State, ctx: &mut TxContext): AdminCap {
4649
let DeployerCap { id } = deployer;
47-
object::delete(id);
50+
object::delete(id); // Deletion means that nothing can redeploy this again...
4851

4952
let state = new(wormhole_state, ctx);
5053
transfer::public_share_object(state);
@@ -56,6 +59,7 @@ module wormhole_transceiver::wormhole_transceiver {
5659
state: &mut State,
5760
message: OutboundMessage<Auth>,
5861
): Option<MessageTicket> {
62+
5963
let (ntt_manager_message, source_ntt_manager, recipient_ntt_manager)
6064
= message.unwrap_outbound_message(&Auth {});
6165

@@ -79,7 +83,7 @@ module wormhole_transceiver::wormhole_transceiver {
7983

8084
public fun validate_message(
8185
state: &State,
82-
vaa: VAA,
86+
vaa: VAA,
8387
): ValidatedTransceiverMessage<Auth, vector<u8>> {
8488
let (emitter_chain, emitter_address, payload)
8589
= vaa::take_emitter_info_and_payload(vaa);
@@ -112,13 +116,54 @@ module wormhole_transceiver::wormhole_transceiver {
112116
if (state.peers.contains(chain)) {
113117
state.peers.remove(chain);
114118
};
115-
state.peers.add(chain, peer)
119+
state.peers.add(chain, peer);
116120
}
117121

118-
}
122+
public fun set_peer_with_accountant(_ : &AdminCap, state: &mut State, chain: u16, peer: ExternalAddress): Option<MessageTicket>{
123+
124+
// Cannot replace WH peers because of complexities with the accountant
125+
assert!(!state.peers.contains(chain));
126+
127+
broadcast_peer(chain, peer, state)
128+
}
129+
130+
/*
131+
TransceiverInit on EVM
132+
BroadCastId on Solana
133+
*/
134+
public fun broadcast_id<CoinType, Auth>(_: &AdminCap, coin_meta: &CoinMetadata<CoinType>, state: &mut State, manager_state: &ManagerState<CoinType>): Option<MessageTicket> {
135+
// MessageTicket cannot be dropped. Means that the WH message is forced to be emitted, since only the `publish_message` call can get rid of it.
136+
137+
let mut manager_address_opt: Option<address> = ntt_common::contract_auth::get_auth_address<Auth>();
138+
let manager_address = option::extract(&mut manager_address_opt);
139+
140+
let external_address_manager_address = wormhole::external_address::from_address(manager_address);
141+
142+
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+
144+
let message_ticket = wormhole::publish_message::prepare_message(
145+
&mut state.emitter_cap,
146+
0,
147+
transceiver_info_struct.transceiver_info_to_bytes(),
148+
);
149+
option::some(message_ticket)
150+
}
151+
152+
/*
153+
Broadcast Peer in Solana
154+
Transceiver Registration in EVM
155+
*/
156+
fun broadcast_peer(chain_id: u16, peer_address: ExternalAddress, state: &mut State): Option<MessageTicket>{
157+
158+
let transceiver_registration_struct = wormhole_transceiver::transceiver_structs::new_transceiver_registration(chain_id, peer_address);
159+
let message_ticket = wormhole::publish_message::prepare_message(
160+
&mut state.emitter_cap,
161+
0,
162+
transceiver_registration_struct.transceiver_registration_to_bytes(),
163+
);
164+
option::some(message_ticket)
165+
}
119166

120-
#[test_only]
121-
module wormhole_transceiver::tests {
122167
#[test]
123168
public fun test_auth_type() {
124169
assert!(ntt_common::contract_auth::is_auth_type<wormhole_transceiver::wormhole_transceiver::Auth>(), 0);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
2+
module wormhole_transceiver::transceiver_structs {
3+
use wormhole::external_address::ExternalAddress;
4+
use ntt::mode::Mode;
5+
use wormhole::bytes;
6+
7+
const INFO_PREFIX: vector<u8> = x"9C23BD3B";
8+
const REGISTRATION_PREFIX: vector<u8> = x"18fc67c2";
9+
10+
// https://github.com/wormhole-foundation/native-token-transfers/blob/b6b681a77e8289869f35862b261b8048e3f5d398/evm/src/libraries/TransceiverStructs.sol#L409C12-L409C27
11+
public struct WormholeTransceiverInfo has drop{
12+
manager_address: ExternalAddress,
13+
manager_mode: Mode,
14+
token_address: ExternalAddress,
15+
token_decimals: u8
16+
}
17+
18+
// https://github.com/wormhole-foundation/native-token-transfers/blob/b6b681a77e8289869f35862b261b8048e3f5d398/evm/src/libraries/TransceiverStructs.sol#L441
19+
public struct WormholeTransceiverRegistration has drop {
20+
transceiver_chain_id: u16,
21+
transceiver_address: ExternalAddress
22+
}
23+
24+
public(package) fun new_transceiver_info(manager_address: ExternalAddress, mode: Mode, token_address: ExternalAddress, decimals: u8): WormholeTransceiverInfo{
25+
WormholeTransceiverInfo {
26+
manager_address: manager_address,
27+
manager_mode: mode,
28+
token_address: token_address,
29+
token_decimals: decimals
30+
}
31+
}
32+
33+
public fun transceiver_info_to_bytes(self: &WormholeTransceiverInfo): vector<u8> {
34+
let mut buf = vector::empty<u8>();
35+
36+
buf.append(INFO_PREFIX);
37+
buf.append(self.manager_address.to_bytes()); // decimals and amount
38+
buf.append(self.manager_mode.serialize()); // 32 bytes
39+
buf.append(self.token_address.to_bytes()); // 32 bytes
40+
bytes::push_u8(&mut buf, self.token_decimals); // 2 bytes
41+
buf
42+
}
43+
44+
45+
public(package) fun new_transceiver_registration(transceiver_chain_id: u16, transceiver_address: ExternalAddress): WormholeTransceiverRegistration{
46+
WormholeTransceiverRegistration {
47+
transceiver_chain_id: transceiver_chain_id,
48+
transceiver_address: transceiver_address
49+
}
50+
}
51+
52+
public fun transceiver_registration_to_bytes(self: &WormholeTransceiverRegistration): vector<u8> {
53+
let mut buf = vector::empty<u8>();
54+
55+
buf.append(REGISTRATION_PREFIX);
56+
bytes::push_u16_be(&mut buf, self.transceiver_chain_id);
57+
buf.append(self.transceiver_address.to_bytes());
58+
buf
59+
}
60+
61+
}

0 commit comments

Comments
 (0)