solana: nft-bridge: Add tests

The integration tests were previously just copied over from the
token-bridge directory and not changed at all (they still referenced the
token-bridge crate).

Clean up the files and add tests for basic functionality like sending
and receiving native + wrapped NFTs.
This commit is contained in:
Chirantan Ekbote 2022-04-21 14:15:07 +09:00 committed by Chirantan Ekbote
parent 66f031c51b
commit defd16e084
5 changed files with 584 additions and 707 deletions

2
solana/Cargo.lock generated
View File

@ -1573,8 +1573,8 @@ dependencies = [
"rocksalt", "rocksalt",
"serde", "serde",
"sha3", "sha3",
"solana-client",
"solana-program", "solana-program",
"solana-program-test",
"solana-sdk", "solana-sdk",
"solitaire", "solitaire",
"solitaire-client", "solitaire-client",

View File

@ -9,11 +9,12 @@ crate-type = ["cdylib", "lib"]
name = "nft_bridge" name = "nft_bridge"
[features] [features]
no-entrypoint = ["solitaire/no-entrypoint", "rand"] no-entrypoint = ["solitaire/no-entrypoint", "instructions", "rand"]
trace = ["solitaire/trace"] trace = ["solitaire/trace"]
wasm = ["no-entrypoint", "wasm-bindgen"] wasm = ["no-entrypoint", "wasm-bindgen"]
client = ["solitaire-client", "solitaire/client", "no-entrypoint"] client = ["solitaire-client", "solitaire/client", "no-entrypoint"]
cpi = ["no-entrypoint"] cpi = ["no-entrypoint"]
instructions = []
default = [] default = []
[dependencies] [dependencies]
@ -26,7 +27,7 @@ solitaire = { path = "../../../solitaire/program" }
sha3 = "0.9.1" sha3 = "0.9.1"
solana-program = "*" solana-program = "*"
spl-token = { version = "=3.2.0", features = ["no-entrypoint"] } spl-token = { version = "=3.2.0", features = ["no-entrypoint"] }
spl-associated-token-account = { version = "1.0.2" } spl-associated-token-account = { version = "1.0.2", features = ["no-entrypoint"] }
primitive-types = { version = "0.9.0", default-features = false } primitive-types = { version = "0.9.0", default-features = false }
solitaire-client = { path = "../../../solitaire/client", optional = true } solitaire-client = { path = "../../../solitaire/client", optional = true }
spl-token-metadata = { path = "../../token_bridge/token-metadata" } spl-token-metadata = { path = "../../token_bridge/token-metadata" }
@ -38,7 +39,8 @@ rand = { version = "0.7.3", optional = true }
hex = "*" hex = "*"
hex-literal = "0.3.1" hex-literal = "0.3.1"
libsecp256k1 = { version = "0.6.0", features = [] } libsecp256k1 = { version = "0.6.0", features = [] }
solana-client = "=1.9.4" rand = "0.7.3"
solana-program-test = "=1.9.4"
solana-sdk = "=1.9.4" solana-sdk = "=1.9.4"
spl-token = { version = "=3.2.0", features = ["no-entrypoint"] } spl-token = { version = "=3.2.0", features = ["no-entrypoint"] }
spl-token-metadata = { path = "../../token_bridge/token-metadata" } spl-token-metadata = { path = "../../token_bridge/token-metadata" }

View File

@ -4,7 +4,7 @@
#![deny(unused_must_use)] #![deny(unused_must_use)]
// #![cfg(all(target_arch = "bpf", not(feature = "no-entrypoint")))] // #![cfg(all(target_arch = "bpf", not(feature = "no-entrypoint")))]
#[cfg(feature = "no-entrypoint")] #[cfg(feature = "instructions")]
pub mod instructions; pub mod instructions;
#[cfg(feature = "wasm")] #[cfg(feature = "wasm")]

View File

@ -9,17 +9,12 @@ use byteorder::{
WriteBytesExt, WriteBytesExt,
}; };
use hex_literal::hex; use hex_literal::hex;
use secp256k1::{ use libsecp256k1::{
Message as Secp256k1Message, Message as Secp256k1Message,
PublicKey, PublicKey,
SecretKey, SecretKey,
}; };
use sha3::Digest; use sha3::Digest;
use solana_client::{
client_error::ClientError,
rpc_client::RpcClient,
rpc_config::RpcSendTransactionConfig,
};
use solana_program::{ use solana_program::{
borsh::try_from_slice_unchecked, borsh::try_from_slice_unchecked,
hash, hash,
@ -36,8 +31,12 @@ use solana_program::{
system_program, system_program,
sysvar, sysvar,
}; };
use solana_program_test::{
BanksClient,
ProgramTest,
};
use solana_sdk::{ use solana_sdk::{
commitment_config::CommitmentConfig, commitment_config::CommitmentLevel,
rent::Rent, rent::Rent,
secp256k1_instruction::new_secp256k1_instruction, secp256k1_instruction::new_secp256k1_instruction,
signature::{ signature::{
@ -46,7 +45,13 @@ use solana_sdk::{
Signature, Signature,
Signer, Signer,
}, },
signers::Signers,
transaction::Transaction, transaction::Transaction,
transport::TransportError,
};
use solitaire::{
processors::seeded::Seeded,
AccountState,
}; };
use spl_token::state::Mint; use spl_token::state::Mint;
use std::{ use std::{
@ -62,7 +67,7 @@ use std::{
}, },
}; };
use token_bridge::{ use nft_bridge::{
accounts::*, accounts::*,
instruction, instruction,
instructions, instructions,
@ -70,246 +75,192 @@ use token_bridge::{
Initialize, Initialize,
}; };
use solitaire::{
processors::seeded::Seeded,
AccountState,
};
pub use helpers::*; pub use helpers::*;
/// Simple API wrapper for quickly preparing and sending transactions. /// Simple API wrapper for quickly preparing and sending transactions.
pub fn execute( pub async fn execute<T: Signers>(
client: &RpcClient, client: &mut BanksClient,
payer: &Keypair, payer: &Keypair,
signers: &[&Keypair], signers: &T,
instructions: &[Instruction], instructions: &[Instruction],
commitment_level: CommitmentConfig, commitment_level: CommitmentLevel,
) -> Result<Signature, ClientError> { ) -> Result<(), TransportError> {
let mut transaction = Transaction::new_with_payer(instructions, Some(&payer.pubkey())); let mut transaction = Transaction::new_with_payer(instructions, Some(&payer.pubkey()));
let recent_blockhash = client.get_recent_blockhash().unwrap().0; let recent_blockhash = client.get_latest_blockhash().await?;
transaction.sign(&signers.to_vec(), recent_blockhash); transaction.sign(signers, recent_blockhash);
client.send_and_confirm_transaction_with_spinner_and_config( client
&transaction, .process_transaction_with_commitment(transaction, commitment_level)
commitment_level, .await
RpcSendTransactionConfig {
skip_preflight: true,
preflight_commitment: None,
encoding: None,
},
)
} }
mod helpers { mod helpers {
use bridge::types::{
ConsistencyLevel,
PostedVAAData,
};
use token_bridge::{
CompleteNativeData,
CompleteWrappedData,
CreateWrappedData,
RegisterChainData,
TransferNativeData,
TransferWrappedData,
};
use super::*; use super::*;
use bridge::{ use bridge::{
accounts::{ accounts::{
FeeCollector, FeeCollector,
PostedVAADerivationData, PostedVAADerivationData,
}, },
types::ConsistencyLevel,
PostVAAData, PostVAAData,
PostedVAAData,
}; };
use std::ops::Add; use nft_bridge::{
use token_bridge::messages::{ CompleteNativeData,
PayloadAssetMeta, CompleteWrappedData,
CompleteWrappedMetaData,
RegisterChainData,
TransferNativeData,
TransferWrappedData,
};
use primitive_types::U256;
use solana_program_test::processor;
use nft_bridge::messages::{
PayloadGovernanceRegisterChain, PayloadGovernanceRegisterChain,
PayloadTransfer, PayloadTransfer,
}; };
use std::ops::Add;
/// Generate `count` secp256k1 private keys, along with their ethereum-styled public key
/// encoding: 0x0123456789ABCDEF01234
pub fn generate_keys(count: u8) -> (Vec<[u8; 20]>, Vec<SecretKey>) {
use rand::Rng;
use sha3::Digest;
let mut rng = rand::thread_rng();
// Generate Guardian Keys
let secret_keys: Vec<SecretKey> = std::iter::repeat_with(|| SecretKey::random(&mut rng))
.take(count as usize)
.collect();
(
secret_keys
.iter()
.map(|key| {
let public_key = PublicKey::from_secret_key(&key);
let mut h = sha3::Keccak256::default();
h.write(&public_key.serialize()[1..]).unwrap();
let key: [u8; 32] = h.finalize().into();
let mut address = [0u8; 20];
address.copy_from_slice(&key[12..]);
address
})
.collect(),
secret_keys,
)
}
/// Initialize the test environment, spins up a solana-test-validator in the background so that /// Initialize the test environment, spins up a solana-test-validator in the background so that
/// each test has a fresh environment to work within. /// each test has a fresh environment to work within.
pub fn setup() -> (Keypair, RpcClient, Pubkey, Pubkey) { pub async fn setup() -> (BanksClient, Keypair, Pubkey, Pubkey) {
let payer = env::var("BRIDGE_PAYER").unwrap_or("./payer.json".to_string());
let rpc_address = env::var("BRIDGE_RPC").unwrap_or("http://127.0.0.1:8899".to_string());
let payer = read_keypair_file(payer).unwrap();
let rpc = RpcClient::new(rpc_address);
let (program, token_program) = ( let (program, token_program) = (
env::var("BRIDGE_PROGRAM") env::var("BRIDGE_PROGRAM")
.unwrap_or("Bridge1p5gheXUvJ6jGWGeCsgPKgnE3YgdGKRVCMY9o".to_string()) .unwrap_or("Bridge1p5gheXUvJ6jGWGeCsgPKgnE3YgdGKRVCMY9o".to_string())
.parse::<Pubkey>() .parse::<Pubkey>()
.unwrap(), .unwrap(),
env::var("TOKEN_BRIDGE_PROGRAM") env::var("NFT_BRIDGE_PROGRAM")
.unwrap_or("B6RHG3mfcckmrYN1UhmJzyS1XX3fZKbkeUcpJe9Sy3FE".to_string()) .unwrap_or("NFTWqJR8YnRVqPDvTJrYuLrQDitTG5AScqbeghi4zSA".to_string())
.parse::<Pubkey>() .parse::<Pubkey>()
.unwrap(), .unwrap(),
); );
(payer, rpc, program, token_program) let mut builder = ProgramTest::new("bridge", program, processor!(bridge::solitaire));
} builder.add_program("spl_token_metadata", spl_token_metadata::id(), None);
builder.add_program(
"nft_bridge",
token_program,
processor!(nft_bridge::solitaire),
);
/// Wait for a single transaction to fully finalize, guaranteeing chain state has been // Some instructions go over the limit when tracing is enabled but we need that for better
/// confirmed. Useful for consistently fetching data during state checks. // logging. We don't really care about the limit during these tests anyway.
pub fn sync(client: &RpcClient, payer: &Keypair) { builder.set_compute_max_units(u64::MAX);
execute(
client, let (client, payer, _) = builder.start().await;
payer, (client, payer, program, token_program)
&[payer],
&[system_instruction::transfer(
&payer.pubkey(),
&payer.pubkey(),
1,
)],
CommitmentConfig::finalized(),
)
.unwrap();
} }
/// Fetch account data, the loop is there to re-attempt until data is available. /// Fetch account data, the loop is there to re-attempt until data is available.
pub fn get_account_data<T: BorshDeserialize>( pub async fn get_account_data<T: BorshDeserialize>(
client: &RpcClient, client: &mut BanksClient,
account: &Pubkey, account: Pubkey,
) -> Option<T> { ) -> Option<T> {
let account = client let account = client
.get_account_with_commitment(account, CommitmentConfig::processed()) .get_account_with_commitment(account, CommitmentLevel::Processed)
.await
.unwrap()
.unwrap(); .unwrap();
T::try_from_slice(&account.value.unwrap().data).ok() T::try_from_slice(&account.data).ok()
} }
pub fn initialize_bridge( pub async fn initialize_bridge(
client: &RpcClient, client: &mut BanksClient,
program: &Pubkey, program: Pubkey,
payer: &Keypair, payer: &Keypair,
) -> Result<Signature, ClientError> { initial_guardians: &[[u8; 20]],
let initial_guardians = &[[1u8; 20]]; ) -> Result<(), TransportError> {
execute( execute(
client, client,
payer, payer,
&[payer], &[payer],
&[bridge::instructions::initialize( &[bridge::instructions::initialize(
*program, program,
payer.pubkey(), payer.pubkey(),
50, 50,
2_000_000_000, 2_000_000_000,
initial_guardians, initial_guardians,
) )
.unwrap()], .unwrap()],
CommitmentConfig::processed(), CommitmentLevel::Processed,
) )
.await
} }
pub fn transfer( pub async fn initialize(
client: &RpcClient, client: &mut BanksClient,
from: &Keypair, program: Pubkey,
to: &Pubkey,
lamports: u64,
) -> Result<Signature, ClientError> {
execute(
client,
from,
&[from],
&[system_instruction::transfer(&from.pubkey(), to, lamports)],
CommitmentConfig::processed(),
)
}
pub fn initialize(
client: &RpcClient,
program: &Pubkey,
payer: &Keypair, payer: &Keypair,
bridge: &Pubkey, bridge: Pubkey,
) -> Result<Signature, ClientError> { ) -> Result<(), TransportError> {
let instruction = instructions::initialize(*program, payer.pubkey(), *bridge) let instruction = instructions::initialize(program, payer.pubkey(), bridge)
.expect("Could not create Initialize instruction"); .expect("Could not create Initialize instruction");
for account in instruction.accounts.iter().enumerate() {
println!("{}: {}", account.0, account.1.pubkey);
}
execute( execute(
client, client,
payer, payer,
&[payer], &[payer],
&[instruction], &[instruction],
CommitmentConfig::processed(), CommitmentLevel::Processed,
) )
.await
} }
pub fn attest( pub async fn transfer_native(
client: &RpcClient, client: &mut BanksClient,
program: &Pubkey, program: Pubkey,
bridge: &Pubkey, bridge: Pubkey,
payer: &Keypair,
message: &Keypair,
mint: Pubkey,
nonce: u32,
) -> Result<Signature, ClientError> {
let mint_data = Mint::unpack(
&client
.get_account_with_commitment(&mint, CommitmentConfig::processed())?
.value
.unwrap()
.data,
)
.expect("Could not unpack Mint");
let instruction = instructions::attest(
*program,
*bridge,
payer.pubkey(),
message.pubkey(),
mint,
nonce,
)
.expect("Could not create Attest instruction");
for account in instruction.accounts.iter().enumerate() {
println!("{}: {}", account.0, account.1.pubkey);
}
execute(
client,
payer,
&[payer, message],
&[instruction],
CommitmentConfig::processed(),
)
}
pub fn transfer_native(
client: &RpcClient,
program: &Pubkey,
bridge: &Pubkey,
payer: &Keypair, payer: &Keypair,
message: &Keypair, message: &Keypair,
from: &Keypair, from: &Keypair,
from_owner: &Keypair, from_owner: &Keypair,
mint: Pubkey, mint: Pubkey,
amount: u64, ) -> Result<(), TransportError> {
) -> Result<Signature, ClientError> {
let instruction = instructions::transfer_native( let instruction = instructions::transfer_native(
*program, program,
*bridge, bridge,
payer.pubkey(), payer.pubkey(),
message.pubkey(), message.pubkey(),
from.pubkey(), from.pubkey(),
mint, mint,
TransferNativeData { TransferNativeData {
nonce: 0, nonce: 0,
amount,
fee: 0,
target_address: [0u8; 32], target_address: [0u8; 32],
target_chain: 2, target_chain: 2,
}, },
) )
.expect("Could not create Transfer Native"); .expect("Could not create Transfer Native");
for account in instruction.accounts.iter().enumerate() {
println!("{}: {}", account.0, account.1.pubkey);
}
execute( execute(
client, client,
payer, payer,
@ -318,53 +269,49 @@ mod helpers {
spl_token::instruction::approve( spl_token::instruction::approve(
&spl_token::id(), &spl_token::id(),
&from.pubkey(), &from.pubkey(),
&token_bridge::accounts::AuthoritySigner::key(None, program), &nft_bridge::accounts::AuthoritySigner::key(None, &program),
&from_owner.pubkey(), &from_owner.pubkey(),
&[], &[],
amount, 1,
) )
.unwrap(), .unwrap(),
instruction, instruction,
], ],
CommitmentConfig::processed(), CommitmentLevel::Processed,
) )
.await
} }
pub fn transfer_wrapped( pub async fn transfer_wrapped(
client: &RpcClient, client: &mut BanksClient,
program: &Pubkey, program: Pubkey,
bridge: &Pubkey, bridge: Pubkey,
payer: &Keypair, payer: &Keypair,
message: &Keypair, message: &Keypair,
from: Pubkey, from: Pubkey,
from_owner: &Keypair, from_owner: &Keypair,
token_chain: u16, token_chain: u16,
token_address: Address, token_address: Address,
amount: u64, token_id: U256,
) -> Result<Signature, ClientError> { ) -> Result<(), TransportError> {
let instruction = instructions::transfer_wrapped( let instruction = instructions::transfer_wrapped(
*program, program,
*bridge, bridge,
payer.pubkey(), payer.pubkey(),
message.pubkey(), message.pubkey(),
from, from,
from_owner.pubkey(), from_owner.pubkey(),
token_chain, token_chain,
token_address, token_address,
token_id,
TransferWrappedData { TransferWrappedData {
nonce: 0, nonce: 0,
amount,
fee: 0,
target_address: [5u8; 32], target_address: [5u8; 32],
target_chain: 2, target_chain: 2,
}, },
) )
.expect("Could not create Transfer Native"); .expect("Could not create Transfer Native");
for account in instruction.accounts.iter().enumerate() {
println!("{}: {}", account.0, account.1.pubkey);
}
execute( execute(
client, client,
payer, payer,
@ -373,162 +320,151 @@ mod helpers {
spl_token::instruction::approve( spl_token::instruction::approve(
&spl_token::id(), &spl_token::id(),
&from, &from,
&token_bridge::accounts::AuthoritySigner::key(None, program), &nft_bridge::accounts::AuthoritySigner::key(None, &program),
&from_owner.pubkey(), &from_owner.pubkey(),
&[], &[],
amount, 1,
) )
.unwrap(), .unwrap(),
instruction, instruction,
], ],
CommitmentConfig::processed(), CommitmentLevel::Processed,
) )
.await
} }
pub fn register_chain( pub async fn register_chain(
client: &RpcClient, client: &mut BanksClient,
program: &Pubkey, program: Pubkey,
bridge: &Pubkey, bridge: Pubkey,
message_acc: &Pubkey, message_acc: Pubkey,
vaa: PostVAAData, vaa: PostVAAData,
payload: PayloadGovernanceRegisterChain, payload: PayloadGovernanceRegisterChain,
payer: &Keypair, payer: &Keypair,
) -> Result<Signature, ClientError> { ) -> Result<(), TransportError> {
let instruction = instructions::register_chain( let instruction = instructions::register_chain(
*program, program,
*bridge, bridge,
payer.pubkey(), payer.pubkey(),
*message_acc, message_acc,
vaa, vaa,
payload, payload,
RegisterChainData {}, RegisterChainData {},
) )
.expect("Could not create Transfer Native"); .expect("Could not create Transfer Native");
for account in instruction.accounts.iter().enumerate() {
println!("{}: {}", account.0, account.1.pubkey);
}
execute( execute(
client, client,
payer, payer,
&[payer], &[payer],
&[instruction], &[instruction],
CommitmentConfig::processed(), CommitmentLevel::Processed,
) )
.await
} }
pub fn complete_native( pub async fn complete_native(
client: &RpcClient, client: &mut BanksClient,
program: &Pubkey, program: Pubkey,
bridge: &Pubkey, bridge: Pubkey,
message_acc: &Pubkey, message_acc: Pubkey,
vaa: PostVAAData, vaa: PostVAAData,
payload: PayloadTransfer, payload: PayloadTransfer,
payer: &Keypair, payer: &Keypair,
) -> Result<Signature, ClientError> { to_authority: Pubkey,
mint: Pubkey,
) -> Result<(), TransportError> {
let instruction = instructions::complete_native( let instruction = instructions::complete_native(
*program, program,
*bridge, bridge,
payer.pubkey(), payer.pubkey(),
*message_acc, message_acc,
vaa, vaa,
Pubkey::new(&payload.to[..]), to_authority,
None, mint,
Pubkey::new(&payload.token_address[..]),
CompleteNativeData {}, CompleteNativeData {},
) )
.expect("Could not create Complete Native instruction"); .expect("Could not create Complete Native instruction");
for account in instruction.accounts.iter().enumerate() {
println!("{}: {}", account.0, account.1.pubkey);
}
execute( execute(
client, client,
payer, payer,
&[payer], &[payer],
&[instruction], &[instruction],
CommitmentConfig::processed(), CommitmentLevel::Processed,
) )
.await
} }
pub fn complete_transfer_wrapped( pub async fn complete_wrapped(
client: &RpcClient, client: &mut BanksClient,
program: &Pubkey, program: Pubkey,
bridge: &Pubkey, bridge: Pubkey,
message_acc: &Pubkey, message_acc: Pubkey,
vaa: PostVAAData, vaa: PostVAAData,
payload: PayloadTransfer, payload: PayloadTransfer,
to: Pubkey,
payer: &Keypair, payer: &Keypair,
) -> Result<Signature, ClientError> { ) -> Result<(), TransportError> {
let to = Pubkey::new(&payload.to[..]);
let instruction = instructions::complete_wrapped( let instruction = instructions::complete_wrapped(
*program, program,
*bridge, bridge,
payer.pubkey(), payer.pubkey(),
*message_acc, message_acc,
vaa, vaa,
payload, payload,
to, to,
None,
CompleteWrappedData {}, CompleteWrappedData {},
) )
.expect("Could not create Complete Wrapped instruction"); .expect("Could not create Complete Wrapped instruction");
for account in instruction.accounts.iter().enumerate() {
println!("{}: {}", account.0, account.1.pubkey);
}
execute( execute(
client, client,
payer, payer,
&[payer], &[payer],
&[instruction], &[instruction],
CommitmentConfig::processed(), CommitmentLevel::Processed,
) )
.await
} }
pub fn create_wrapped( pub async fn complete_wrapped_meta(
client: &RpcClient, client: &mut BanksClient,
program: &Pubkey, program: Pubkey,
bridge: &Pubkey, bridge: Pubkey,
message_acc: &Pubkey, message_acc: Pubkey,
vaa: PostVAAData, vaa: PostVAAData,
payload: PayloadAssetMeta, payload: PayloadTransfer,
payer: &Keypair, payer: &Keypair,
) -> Result<Signature, ClientError> { ) -> Result<(), TransportError> {
let instruction = instructions::create_wrapped( let instruction = instructions::complete_wrapped_meta(
*program, program,
*bridge, bridge,
payer.pubkey(), payer.pubkey(),
*message_acc, message_acc,
vaa, vaa,
payload, payload,
CreateWrappedData {}, CompleteWrappedMetaData {},
) )
.expect("Could not create Create Wrapped instruction"); .expect("Could not create Complete Wrapped Meta instruction");
for account in instruction.accounts.iter().enumerate() {
println!("{}: {}", account.0, account.1.pubkey);
}
execute( execute(
client, client,
payer, payer,
&[payer], &[payer],
&[instruction], &[instruction],
CommitmentConfig::processed(), CommitmentLevel::Processed,
) )
.await
} }
pub fn create_mint( pub async fn create_mint(
client: &RpcClient, client: &mut BanksClient,
payer: &Keypair, payer: &Keypair,
mint_authority: &Pubkey, mint_authority: &Pubkey,
mint: &Keypair, mint: &Keypair,
) -> Result<Signature, ClientError> { ) -> Result<(), TransportError> {
let mint_key = mint.pubkey();
execute( execute(
client, client,
payer, payer,
@ -536,64 +472,33 @@ mod helpers {
&[ &[
solana_sdk::system_instruction::create_account( solana_sdk::system_instruction::create_account(
&payer.pubkey(), &payer.pubkey(),
&mint.pubkey(), &mint_key,
Rent::default().minimum_balance(spl_token::state::Mint::LEN), Rent::default().minimum_balance(spl_token::state::Mint::LEN),
spl_token::state::Mint::LEN as u64, spl_token::state::Mint::LEN as u64,
&spl_token::id(), &spl_token::id(),
), ),
spl_token::instruction::initialize_mint( spl_token::instruction::initialize_mint(
&spl_token::id(), &spl_token::id(),
&mint.pubkey(), &mint_key,
mint_authority, mint_authority,
None, None,
0, 0,
) )
.unwrap(), .unwrap(),
], ],
CommitmentConfig::processed(), CommitmentLevel::Processed,
) )
.await
} }
pub fn create_spl_metadata( pub async fn create_token_account(
client: &RpcClient, client: &mut BanksClient,
payer: &Keypair,
metadata_account: &Pubkey,
mint_authority: &Keypair,
mint: &Keypair,
update_authority: &Pubkey,
name: String,
symbol: String,
) -> Result<Signature, ClientError> {
execute(
client,
payer,
&[payer, mint_authority],
&[spl_token_metadata::instruction::create_metadata_accounts(
spl_token_metadata::id(),
*metadata_account,
mint.pubkey(),
mint_authority.pubkey(),
payer.pubkey(),
*update_authority,
name,
symbol,
"https://token.org".to_string(),
None,
0,
false,
false,
)],
CommitmentConfig::processed(),
)
}
pub fn create_token_account(
client: &RpcClient,
payer: &Keypair, payer: &Keypair,
token_acc: &Keypair, token_acc: &Keypair,
token_authority: Pubkey, token_authority: &Pubkey,
mint: Pubkey, mint: &Pubkey,
) -> Result<Signature, ClientError> { ) -> Result<(), TransportError> {
let token_key = token_acc.pubkey();
execute( execute(
client, client,
payer, payer,
@ -601,35 +506,71 @@ mod helpers {
&[ &[
solana_sdk::system_instruction::create_account( solana_sdk::system_instruction::create_account(
&payer.pubkey(), &payer.pubkey(),
&token_acc.pubkey(), &token_key,
Rent::default().minimum_balance(spl_token::state::Account::LEN), Rent::default().minimum_balance(spl_token::state::Account::LEN),
spl_token::state::Account::LEN as u64, spl_token::state::Account::LEN as u64,
&spl_token::id(), &spl_token::id(),
), ),
spl_token::instruction::initialize_account( spl_token::instruction::initialize_account(
&spl_token::id(), &spl_token::id(),
&token_acc.pubkey(), &token_key,
&mint, mint,
&token_authority, token_authority,
) )
.unwrap(), .unwrap(),
], ],
CommitmentConfig::processed(), CommitmentLevel::Processed,
) )
.await
} }
pub fn mint_tokens( pub async fn create_spl_metadata(
client: &RpcClient, client: &mut BanksClient,
payer: &Keypair,
metadata_account: Pubkey,
mint_authority: &Keypair,
mint: Pubkey,
update_authority: Pubkey,
name: String,
symbol: String,
uri: String,
) -> Result<(), TransportError> {
execute(
client,
payer,
&[payer, mint_authority],
&[spl_token_metadata::instruction::create_metadata_accounts(
spl_token_metadata::id(),
metadata_account,
mint,
mint_authority.pubkey(),
payer.pubkey(),
update_authority,
name,
symbol,
uri,
None,
0,
false,
false,
)],
CommitmentLevel::Processed,
)
.await
}
pub async fn mint_tokens(
client: &mut BanksClient,
payer: &Keypair, payer: &Keypair,
mint_authority: &Keypair, mint_authority: &Keypair,
mint: &Keypair, mint: &Keypair,
token_account: &Pubkey, token_account: &Pubkey,
amount: u64, amount: u64,
) -> Result<Signature, ClientError> { ) -> Result<(), TransportError> {
execute( execute(
client, client,
payer, payer,
&[payer, &mint_authority], &[payer, mint_authority],
&[spl_token::instruction::mint_to( &[spl_token::instruction::mint_to(
&spl_token::id(), &spl_token::id(),
&mint.pubkey(), &mint.pubkey(),
@ -639,15 +580,16 @@ mod helpers {
amount, amount,
) )
.unwrap()], .unwrap()],
CommitmentConfig::processed(), CommitmentLevel::Processed,
) )
.await
} }
/// Utility function for generating VAA's from message data. /// Utility function for generating VAA's from message data.
pub fn generate_vaa( pub fn generate_vaa<T: Into<Vec<u8>>>(
emitter: Address, emitter: Address,
emitter_chain: u16, emitter_chain: u16,
data: Vec<u8>, data: T,
nonce: u32, nonce: u32,
sequence: u64, sequence: u64,
) -> (PostVAAData, [u8; 32], [u8; 32]) { ) -> (PostVAAData, [u8; 32], [u8; 32]) {
@ -659,7 +601,7 @@ mod helpers {
emitter_chain, emitter_chain,
emitter_address: emitter, emitter_address: emitter,
sequence, sequence,
payload: data, payload: data.into(),
timestamp: SystemTime::now() timestamp: SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH) .duration_since(SystemTime::UNIX_EPOCH)
.unwrap() .unwrap()
@ -698,46 +640,81 @@ mod helpers {
(vaa, body, body_hash) (vaa, body, body_hash)
} }
pub fn post_vaa( pub async fn verify_signatures(
client: &RpcClient, client: &mut BanksClient,
program: &Pubkey, program: &Pubkey,
payer: &Keypair, payer: &Keypair,
vaa: PostVAAData, body: [u8; 32],
) -> Result<(), ClientError> { secret_keys: &[SecretKey],
let instruction = guardian_set_version: u32,
bridge::instructions::post_vaa(*program, payer.pubkey(), Pubkey::new_unique(), vaa); ) -> Result<Pubkey, TransportError> {
let signature_set = Keypair::new();
let tx_signers = [payer, &signature_set];
// Push Secp256k1 instructions for each signature we want to verify.
for (i, key) in secret_keys.iter().enumerate() {
// Set this signers signature position as present at 0.
let mut signers = [-1; 19];
signers[i] = 0;
for account in instruction.accounts.iter().enumerate() { execute(
println!("{}: {}", account.0, account.1.pubkey); client,
payer,
&tx_signers,
&[
new_secp256k1_instruction(key, &body),
bridge::instructions::verify_signatures(
*program,
payer.pubkey(),
guardian_set_version,
signature_set.pubkey(),
bridge::VerifySignaturesData { signers },
)
.unwrap(),
],
CommitmentLevel::Processed,
)
.await?;
} }
Ok(signature_set.pubkey())
}
pub async fn post_vaa(
client: &mut BanksClient,
program: Pubkey,
payer: &Keypair,
signature_set: Pubkey,
vaa: PostVAAData,
) -> Result<(), TransportError> {
let instruction =
bridge::instructions::post_vaa(program, payer.pubkey(), signature_set, vaa);
execute( execute(
client, client,
payer, payer,
&[payer], &[payer],
&[instruction], &[instruction],
CommitmentConfig::processed(), CommitmentLevel::Processed,
)?; )
.await
Ok(())
} }
pub fn post_message( pub async fn post_message(
client: &RpcClient, client: &mut BanksClient,
program: &Pubkey, program: Pubkey,
payer: &Keypair, payer: &Keypair,
emitter: &Keypair, emitter: &Keypair,
message: &Keypair, message: &Keypair,
nonce: u32, nonce: u32,
data: Vec<u8>, data: Vec<u8>,
fee: u64, fee: u64,
) -> Result<(), ClientError> { ) -> Result<(), TransportError> {
// Transfer money into the fee collector as it needs a balance/must exist. // Transfer money into the fee collector as it needs a balance/must exist.
let fee_collector = FeeCollector::<'_>::key(None, program); let fee_collector = FeeCollector::<'_>::key(None, &program);
// Capture the resulting message, later functions will need this. // Capture the resulting message, later functions will need this.
let instruction = bridge::instructions::post_message( let instruction = bridge::instructions::post_message(
*program, program,
payer.pubkey(), payer.pubkey(),
emitter.pubkey(), emitter.pubkey(),
message.pubkey(), message.pubkey(),
@ -747,10 +724,6 @@ mod helpers {
) )
.unwrap(); .unwrap();
for account in instruction.accounts.iter().enumerate() {
println!("{}: {}", account.0, account.1.pubkey);
}
execute( execute(
client, client,
payer, payer,
@ -759,9 +732,8 @@ mod helpers {
system_instruction::transfer(&payer.pubkey(), &fee_collector, fee), system_instruction::transfer(&payer.pubkey(), &fee_collector, fee),
instruction, instruction,
], ],
CommitmentConfig::processed(), CommitmentLevel::Processed,
)?; )
.await
Ok(())
} }
} }

View File

@ -1,19 +1,66 @@
#![allow(warnings)] #![allow(warnings)]
use borsh::BorshSerialize; use borsh::BorshSerialize;
use bridge::{
accounts::{
Bridge,
FeeCollector,
GuardianSet,
GuardianSetDerivationData,
PostedVAA,
PostedVAADerivationData,
SignatureSet,
},
instruction,
types::{
GovernancePayloadGuardianSetChange,
GovernancePayloadSetMessageFee,
GovernancePayloadTransferFees,
},
BridgeConfig,
BridgeData,
GuardianSetData,
Initialize,
MessageData,
PostVAA,
PostVAAData,
PostedVAAData,
SequenceTracker,
SerializePayload,
Signature,
SignatureSet as SignatureSetData,
};
use byteorder::{ use byteorder::{
BigEndian, BigEndian,
WriteBytesExt, WriteBytesExt,
}; };
use hex_literal::hex; use hex_literal::hex;
use rand::Rng; use libsecp256k1::{
use secp256k1::{
Message as Secp256k1Message, Message as Secp256k1Message,
PublicKey, PublicKey,
SecretKey, SecretKey,
}; };
use nft_bridge::{
accounts::{
ConfigAccount,
EmitterAccount,
SplTokenMeta,
SplTokenMetaDerivationData,
WrappedDerivationData,
WrappedMint,
},
messages::{
PayloadGovernanceRegisterChain,
PayloadTransfer,
},
types::{
Address,
Config,
},
};
use primitive_types::U256;
use rand::Rng;
use sha3::Digest; use sha3::Digest;
use solana_client::rpc_client::RpcClient;
use solana_program::{ use solana_program::{
borsh::try_from_slice_unchecked, borsh::try_from_slice_unchecked,
hash, hash,
@ -30,14 +77,19 @@ use solana_program::{
system_program, system_program,
sysvar, sysvar,
}; };
use solana_program_test::{
tokio,
BanksClient,
};
use solana_sdk::{ use solana_sdk::{
commitment_config::CommitmentConfig, commitment_config::CommitmentLevel,
signature::{ signature::{
read_keypair_file, read_keypair_file,
Keypair, Keypair,
Signer, Signer,
}, },
transaction::Transaction, transaction::Transaction,
transport::TransportError,
}; };
use solitaire::{ use solitaire::{
processors::seeded::Seeded, processors::seeded::Seeded,
@ -45,66 +97,20 @@ use solitaire::{
}; };
use spl_token::state::Mint; use spl_token::state::Mint;
use std::{ use std::{
collections::HashMap,
convert::TryInto, convert::TryInto,
io::{ io::{
Cursor, Cursor,
Write, Write,
}, },
str::FromStr,
time::{ time::{
Duration, Duration,
SystemTime, SystemTime,
UNIX_EPOCH,
}, },
}; };
use bridge::{
accounts::{
Bridge,
FeeCollector,
GuardianSet,
GuardianSetDerivationData,
PostedVAA,
PostedVAADerivationData,
SignatureSet,
},
instruction,
types::{
BridgeConfig,
BridgeData,
GovernancePayloadGuardianSetChange,
GovernancePayloadSetMessageFee,
GovernancePayloadTransferFees,
GuardianSetData,
MessageData,
PostedVAAData,
SequenceTracker,
SignatureSet as SignatureSetData,
},
Initialize,
PostVAA,
PostVAAData,
SerializePayload,
Signature,
};
use primitive_types::U256;
use std::{
collections::HashMap,
str::FromStr,
time::UNIX_EPOCH,
};
use token_bridge::{
accounts::{
EmitterAccount,
WrappedDerivationData,
WrappedMint,
},
messages::{
PayloadAssetMeta,
PayloadGovernanceRegisterChain,
PayloadTransfer,
},
types::Address,
};
mod common; mod common;
const GOVERNANCE_KEY: [u8; 64] = [ const GOVERNANCE_KEY: [u8; 64] = [
@ -115,20 +121,23 @@ const GOVERNANCE_KEY: [u8; 64] = [
]; ];
struct Context { struct Context {
/// Guardian public keys.
guardians: Vec<[u8; 20]>,
/// Guardian secret keys.
guardian_keys: Vec<SecretKey>,
/// Address of the core bridge contract. /// Address of the core bridge contract.
bridge: Pubkey, bridge: Pubkey,
/// Shared RPC client for tests to make transactions with. /// Shared RPC client for tests to make transactions with.
client: RpcClient, client: BanksClient,
/// Payer key with a ton of lamports to ease testing with. /// Payer key with a ton of lamports to ease testing with.
payer: Keypair, payer: Keypair,
/// Track nonces throughout the tests.
seq: Sequencer,
/// Address of the token bridge itself that we wish to test. /// Address of the token bridge itself that we wish to test.
token_bridge: Pubkey, nft_bridge: Pubkey,
/// Keypairs for mint information, required in multiple tests. /// Keypairs for mint information, required in multiple tests.
mint_authority: Keypair, mint_authority: Keypair,
@ -141,31 +150,13 @@ struct Context {
metadata_account: Pubkey, metadata_account: Pubkey,
} }
/// Small helper to track and provide sequences during tests. This is in particular needed for async fn set_up() -> Result<Context, TransportError> {
/// guardian operations that require them for derivations. let (guardians, guardian_keys) = common::generate_keys(6);
struct Sequencer {
sequences: HashMap<[u8; 32], u64>,
}
impl Sequencer { let (mut client, payer, bridge, nft_bridge) = common::setup().await;
fn next(&mut self, emitter: [u8; 32]) -> u64 {
let entry = self.sequences.entry(emitter).or_insert(0);
*entry += 1;
*entry - 1
}
fn peek(&mut self, emitter: [u8; 32]) -> u64 {
*self.sequences.entry(emitter).or_insert(0)
}
}
#[test]
fn run_integration_tests() {
let (payer, client, bridge, token_bridge) = common::setup();
// Setup a Bridge to test against. // Setup a Bridge to test against.
println!("Bridge: {}", bridge); common::initialize_bridge(&mut client, bridge, &payer, &guardians).await?;
common::initialize_bridge(&client, &bridge, &payer);
// Context for test environment. // Context for test environment.
let mint = Keypair::new(); let mint = Keypair::new();
@ -185,22 +176,21 @@ fn run_integration_tests() {
); );
// Token Bridge Meta // Token Bridge Meta
use token_bridge::accounts::WrappedTokenMeta; use nft_bridge::accounts::WrappedTokenMeta;
let metadata_account = WrappedTokenMeta::<'_, { AccountState::Uninitialized }>::key( let metadata_account = WrappedTokenMeta::<'_, { AccountState::Uninitialized }>::key(
&token_bridge::accounts::WrappedMetaDerivationData { &nft_bridge::accounts::WrappedMetaDerivationData {
mint_key: mint_pubkey.clone(), mint_key: mint_pubkey.clone(),
}, },
&token_bridge, &nft_bridge,
); );
let mut context = Context { let mut context = Context {
seq: Sequencer { guardians,
sequences: HashMap::new(), guardian_keys,
},
bridge, bridge,
client, client,
payer, payer,
token_bridge, nft_bridge,
mint_authority: Keypair::new(), mint_authority: Keypair::new(),
mint, mint,
mint_meta: metadata_account, mint_meta: metadata_account,
@ -211,207 +201,110 @@ fn run_integration_tests() {
// Create a mint for use within tests. // Create a mint for use within tests.
common::create_mint( common::create_mint(
&context.client, &mut context.client,
&context.payer, &context.payer,
&context.mint_authority.pubkey(), &context.mint_authority.pubkey(),
&context.mint, &context.mint,
) )
.unwrap(); .await?;
// Create Token accounts for use within tests. // Create Token accounts for use within tests.
common::create_token_account( common::create_token_account(
&context.client, &mut context.client,
&context.payer, &context.payer,
&context.token_account, &context.token_account,
context.token_authority.pubkey(), &context.token_authority.pubkey(),
context.mint.pubkey(), &context.mint.pubkey(),
) )
.unwrap(); .await?;
// Mint tokens // Create an SPL metadata account for native NFTs.
common::create_spl_metadata(
&mut context.client,
&context.payer,
context.metadata_account,
&context.mint_authority,
context.mint.pubkey(),
context.payer.pubkey(),
"Non-Fungible Token".into(),
"NFT".into(),
"https://example.com".into(),
)
.await?;
// Mint an NFT.
common::mint_tokens( common::mint_tokens(
&context.client, &mut context.client,
&context.payer, &context.payer,
&context.mint_authority, &context.mint_authority,
&context.mint, &context.mint,
&context.token_account.pubkey(), &context.token_account.pubkey(),
1000, 1,
) )
.unwrap(); .await?;
// Initialize the bridge and verify the bridges state. // Initialize the nft bridge.
test_initialize(&mut context); common::initialize(
test_transfer_native(&mut context); &mut context.client,
test_attest(&mut context); context.nft_bridge,
test_register_chain(&mut context);
test_transfer_native_in(&mut context);
// Create an SPL Metadata account to test attestations for wrapped tokens.
common::create_spl_metadata(
&context.client,
&context.payer, &context.payer,
&context.metadata_account, context.bridge,
&context.mint_authority,
&context.mint,
&context.payer.pubkey(),
"BTC".to_string(),
"Bitcoin".to_string(),
) )
.await
.unwrap(); .unwrap();
let wrapped = test_create_wrapped(&mut context); // Verify NFT Bridge State
let wrapped_acc = Keypair::new(); let config_key = ConfigAccount::<'_, { AccountState::Uninitialized }>::key(None, &nft_bridge);
common::create_token_account( let config: Config = common::get_account_data(&mut context.client, config_key)
&context.client, .await
&context.payer,
&wrapped_acc,
context.token_authority.pubkey(),
wrapped,
)
.unwrap(); .unwrap();
test_transfer_wrapped_in(&mut context, wrapped_acc.pubkey()); assert_eq!(config.wormhole_bridge, bridge);
test_transfer_wrapped(&mut context, wrapped_acc.pubkey());
Ok(context)
} }
fn test_attest(context: &mut Context) -> () { #[tokio::test]
println!("Attest"); async fn transfer_native() {
use token_bridge::{
accounts::ConfigAccount,
types::Config,
};
let Context { let Context {
ref payer, ref payer,
ref client, ref mut client,
ref bridge,
ref token_bridge,
ref mint_authority,
ref mint,
ref mint_meta,
ref metadata_account,
..
} = context;
let message = &Keypair::new();
common::attest(
client,
token_bridge,
bridge, bridge,
payer, nft_bridge,
message,
mint.pubkey(),
0,
)
.unwrap();
let emitter_key = EmitterAccount::key(None, &token_bridge);
let mint_data = Mint::unpack(
&client
.get_account_with_commitment(&mint.pubkey(), CommitmentConfig::processed())
.unwrap()
.value
.unwrap()
.data,
)
.unwrap();
let payload = PayloadAssetMeta {
token_address: mint.pubkey().to_bytes(),
token_chain: 1,
decimals: mint_data.decimals,
symbol: "USD".to_string(),
name: "Bitcoin".to_string(),
};
let payload = payload.try_to_vec().unwrap();
}
fn test_transfer_native(context: &mut Context) -> () {
println!("Transfer Native");
use token_bridge::{
accounts::ConfigAccount,
types::Config,
};
let Context {
ref payer,
ref client,
ref bridge,
ref token_bridge,
ref mint_authority, ref mint_authority,
ref mint, ref mint,
ref mint_meta, ref mint_meta,
ref token_account, ref token_account,
ref token_authority, ref token_authority,
.. ..
} = context; } = set_up().await.unwrap();
let message = &Keypair::new(); let message = &Keypair::new();
common::transfer_native( common::transfer_native(
client, client,
token_bridge, nft_bridge,
bridge, bridge,
payer, payer,
message, message,
token_account, token_account,
token_authority, token_authority,
mint.pubkey(), mint.pubkey(),
100,
) )
.await
.unwrap(); .unwrap();
} }
fn test_transfer_wrapped(context: &mut Context, token_account: Pubkey) -> () { async fn register_chain(context: &mut Context) {
println!("TransferWrapped");
use token_bridge::{
accounts::ConfigAccount,
types::Config,
};
let Context { let Context {
ref payer, ref payer,
ref client, ref mut client,
ref bridge, ref bridge,
ref token_bridge, ref nft_bridge,
ref mint_authority,
ref token_authority,
..
} = context;
let message = &Keypair::new();
common::transfer_wrapped(
client,
token_bridge,
bridge,
payer,
message,
token_account,
token_authority,
2,
[1u8; 32],
10000000,
)
.unwrap();
}
fn test_register_chain(context: &mut Context) -> () {
println!("Register Chain");
use token_bridge::{
accounts::ConfigAccount,
types::Config,
};
let Context {
ref payer,
ref client,
ref bridge,
ref token_bridge,
ref mint_authority, ref mint_authority,
ref mint, ref mint,
ref mint_meta, ref mint_meta,
ref token_account,
ref token_authority, ref token_authority,
ref guardian_keys,
.. ..
} = context; } = context;
@ -423,209 +316,219 @@ fn test_register_chain(context: &mut Context) -> () {
}; };
let message = payload.try_to_vec().unwrap(); let message = payload.try_to_vec().unwrap();
let (vaa, _, _) = common::generate_vaa(emitter.pubkey().to_bytes(), 1, message, nonce, 0); let (vaa, body, _) = common::generate_vaa(emitter.pubkey().to_bytes(), 1, message, nonce, 0);
common::post_vaa(client, bridge, payer, vaa.clone()).unwrap(); let signature_set = common::verify_signatures(client, &bridge, payer, body, guardian_keys, 0)
.await
.unwrap();
common::post_vaa(client, *bridge, payer, signature_set, vaa.clone())
.await
.unwrap();
let mut msg_derivation_data = &PostedVAADerivationData { let mut msg_derivation_data = &PostedVAADerivationData {
payload_hash: bridge::instructions::hash_vaa(&vaa).to_vec(), payload_hash: body.to_vec(),
}; };
let message_key = let message_key =
PostedVAA::<'_, { AccountState::MaybeInitialized }>::key(&msg_derivation_data, &bridge); PostedVAA::<'_, { AccountState::MaybeInitialized }>::key(&msg_derivation_data, bridge);
common::register_chain( common::register_chain(
client, client,
token_bridge, *nft_bridge,
bridge, *bridge,
&message_key, message_key,
vaa, vaa,
payload, payload,
payer, payer,
) )
.await
.unwrap(); .unwrap();
} }
fn test_transfer_native_in(context: &mut Context) -> () { #[tokio::test]
println!("TransferNativeIn"); async fn transfer_native_in() {
use token_bridge::{ let mut context = set_up().await.unwrap();
accounts::ConfigAccount, register_chain(&mut context).await;
types::Config,
};
let Context { let Context {
ref payer, ref payer,
ref client, ref mut client,
ref bridge, bridge,
ref token_bridge, nft_bridge,
ref mint_authority, ref mint_authority,
ref mint, ref mint,
ref mint_meta, ref mint_meta,
ref token_account, ref token_account,
ref token_authority, ref token_authority,
ref guardian_keys,
.. ..
} = context; } = context;
// Do an initial transfer so that the bridge account owns the NFT.
let message = &Keypair::new();
common::transfer_native(
client,
nft_bridge,
bridge,
payer,
message,
token_account,
token_authority,
mint.pubkey(),
)
.await
.unwrap();
let nonce = rand::thread_rng().gen(); let nonce = rand::thread_rng().gen();
let token_address = [1u8; 32];
let token_chain = 1;
let token_id = U256::from_big_endian(&mint.pubkey().to_bytes());
let associated_addr = spl_associated_token_account::get_associated_token_address(
&token_authority.pubkey(),
&mint.pubkey(),
);
let payload = PayloadTransfer { let payload = PayloadTransfer {
amount: U256::from(100), token_address: [1u8; 32],
token_address: mint.pubkey().to_bytes(),
token_chain: 1, token_chain: 1,
to: token_account.pubkey().to_bytes(), symbol: "NFT".into(),
name: "Non-Fungible Token".into(),
token_id,
uri: "https://example.com".to_string(),
to: associated_addr.to_bytes(),
to_chain: 1, to_chain: 1,
fee: U256::from(0),
}; };
let message = payload.try_to_vec().unwrap(); let message = payload.try_to_vec().unwrap();
let (vaa, _, _) = common::generate_vaa([0u8; 32], 2, message, nonce, 1); let (vaa, body, _) = common::generate_vaa([0u8; 32], 2, message, nonce, 1);
common::post_vaa(client, bridge, payer, vaa.clone()).unwrap(); let signature_set = common::verify_signatures(client, &bridge, payer, body, guardian_keys, 0)
let mut msg_derivation_data = &PostedVAADerivationData { .await
payload_hash: bridge::instructions::hash_vaa(&vaa).to_vec(), .unwrap();
common::post_vaa(client, bridge, payer, signature_set, vaa.clone())
.await
.unwrap();
let msg_derivation_data = &PostedVAADerivationData {
payload_hash: body.to_vec(),
}; };
let message_key = let message_key =
PostedVAA::<'_, { AccountState::MaybeInitialized }>::key(&msg_derivation_data, &bridge); PostedVAA::<'_, { AccountState::MaybeInitialized }>::key(&msg_derivation_data, &bridge);
common::complete_native( common::complete_native(
client, client,
token_bridge, nft_bridge,
bridge, bridge,
&message_key, message_key,
vaa, vaa,
payload, payload,
payer, payer,
token_authority.pubkey(),
mint.pubkey(),
) )
.await
.unwrap(); .unwrap();
} }
fn test_transfer_wrapped_in(context: &mut Context, to: Pubkey) -> () { #[tokio::test]
println!("TransferWrappedIn"); async fn transfer_wrapped() {
use token_bridge::{ let mut context = set_up().await.unwrap();
accounts::ConfigAccount, register_chain(&mut context).await;
types::Config,
};
let Context { let Context {
ref payer, ref payer,
ref client, ref mut client,
ref bridge, bridge,
ref token_bridge, nft_bridge,
ref mint_authority, ref mint_authority,
ref mint, ref mint,
ref mint_meta, ref mint_meta,
ref token_account, ref token_account,
ref token_authority, ref token_authority,
ref guardian_keys,
metadata_account,
.. ..
} = context; } = context;
let nonce = rand::thread_rng().gen(); let nonce = rand::thread_rng().gen();
let payload = PayloadTransfer { let token_chain = 2;
amount: U256::from(100000000), let token_address = [7u8; 32];
token_address: [1u8; 32], let token_id = U256::from_big_endian(&[0x2cu8; 32]);
token_chain: 2,
to: to.to_bytes(),
to_chain: 1,
fee: U256::from(0),
};
let message = payload.try_to_vec().unwrap();
let (vaa, _, _) = common::generate_vaa([0u8; 32], 2, message, nonce, rand::thread_rng().gen()); let wrapped_mint_key = WrappedMint::<'_, { AccountState::Uninitialized }>::key(
common::post_vaa(client, bridge, payer, vaa.clone()).unwrap();
let mut msg_derivation_data = &PostedVAADerivationData {
payload_hash: bridge::instructions::hash_vaa(&vaa).to_vec(),
};
let message_key =
PostedVAA::<'_, { AccountState::MaybeInitialized }>::key(&msg_derivation_data, &bridge);
common::complete_transfer_wrapped(
client,
token_bridge,
bridge,
&message_key,
vaa,
payload,
payer,
)
.unwrap();
}
fn test_create_wrapped(context: &mut Context) -> (Pubkey) {
println!("CreateWrapped");
use token_bridge::{
accounts::ConfigAccount,
types::Config,
};
let Context {
ref payer,
ref client,
ref bridge,
ref token_bridge,
ref mint_authority,
ref mint,
ref mint_meta,
ref token_account,
ref token_authority,
..
} = context;
let nonce = rand::thread_rng().gen();
let payload = PayloadAssetMeta {
token_address: [1u8; 32],
token_chain: 2,
decimals: 7,
symbol: "".to_string(),
name: "".to_string(),
};
let message = payload.try_to_vec().unwrap();
let (vaa, _, _) = common::generate_vaa([0u8; 32], 2, message, nonce, 2);
common::post_vaa(client, bridge, payer, vaa.clone()).unwrap();
let mut msg_derivation_data = &PostedVAADerivationData {
payload_hash: bridge::instructions::hash_vaa(&vaa).to_vec(),
};
let message_key =
PostedVAA::<'_, { AccountState::MaybeInitialized }>::key(&msg_derivation_data, &bridge);
common::create_wrapped(
client,
token_bridge,
bridge,
&message_key,
vaa,
payload,
payer,
)
.unwrap();
return WrappedMint::<'_, { AccountState::Initialized }>::key(
&WrappedDerivationData { &WrappedDerivationData {
token_chain: 2, token_chain,
token_address: [1u8; 32], token_address,
token_id,
}, },
token_bridge, &nft_bridge,
);
let associated_addr = spl_associated_token_account::get_associated_token_address(
&token_authority.pubkey(),
&wrapped_mint_key,
); );
}
fn test_initialize(context: &mut Context) { let symbol = "UUC";
println!("Initialize"); let name = "External Token";
use token_bridge::{ let uri = "https://example.com";
accounts::ConfigAccount, let payload = PayloadTransfer {
types::Config, token_address,
token_chain,
symbol: symbol.into(),
name: name.into(),
token_id,
uri: uri.into(),
to: associated_addr.to_bytes(),
to_chain: 1,
}; };
let message = payload.try_to_vec().unwrap();
let Context { let (vaa, body, _) =
ref payer, common::generate_vaa([0u8; 32], 2, message, nonce, rand::thread_rng().gen());
ref client, let signature_set = common::verify_signatures(client, &bridge, payer, body, guardian_keys, 0)
ref bridge, .await
ref token_bridge, .unwrap();
.. common::post_vaa(client, bridge, payer, signature_set, vaa.clone())
} = context; .await
.unwrap();
let mut msg_derivation_data = &PostedVAADerivationData {
payload_hash: body.to_vec(),
};
let message_key =
PostedVAA::<'_, { AccountState::MaybeInitialized }>::key(&msg_derivation_data, &bridge);
common::initialize(client, token_bridge, payer, &bridge).unwrap(); common::complete_wrapped(
client,
nft_bridge,
bridge,
message_key,
vaa.clone(),
payload.clone(),
token_authority.pubkey(),
payer,
)
.await
.unwrap();
// Verify Token Bridge State // What this actually does is initialize the spl token metadata so that we can then burn the NFT
let config_key = ConfigAccount::<'_, { AccountState::Uninitialized }>::key(None, &token_bridge); // in the future but of course, we can't call it something useful like
let config: Config = common::get_account_data(client, &config_key).unwrap(); // `initialize_wrapped_nft_metadata`.
assert_eq!(config.wormhole_bridge, *bridge); common::complete_wrapped_meta(client, nft_bridge, bridge, message_key, vaa, payload, payer)
.await
.unwrap();
// Now transfer the wrapped nft back, which will burn it.
let message = &Keypair::new();
common::transfer_wrapped(
client,
nft_bridge,
bridge,
payer,
message,
associated_addr,
token_authority,
token_chain,
token_address,
token_id,
)
.await
.unwrap();
} }