2022-09-06 02:20:29 -07:00
|
|
|
use borsh::BorshDeserialize;
|
2021-08-30 01:37:53 -07:00
|
|
|
use byteorder::{
|
|
|
|
BigEndian,
|
|
|
|
WriteBytesExt,
|
|
|
|
};
|
2022-04-20 22:15:07 -07:00
|
|
|
use libsecp256k1::{
|
2021-08-30 01:37:53 -07:00
|
|
|
PublicKey,
|
|
|
|
SecretKey,
|
|
|
|
};
|
|
|
|
use sha3::Digest;
|
|
|
|
use solana_program::{
|
2022-06-10 09:59:15 -07:00
|
|
|
instruction::Instruction,
|
2021-08-30 01:37:53 -07:00
|
|
|
program_pack::Pack,
|
|
|
|
pubkey::Pubkey,
|
2022-06-10 09:59:15 -07:00
|
|
|
system_instruction,
|
2021-08-30 01:37:53 -07:00
|
|
|
};
|
2022-04-20 22:15:07 -07:00
|
|
|
use solana_program_test::{
|
|
|
|
BanksClient,
|
2022-07-22 06:01:41 -07:00
|
|
|
BanksClientError,
|
2022-04-20 22:15:07 -07:00
|
|
|
ProgramTest,
|
|
|
|
};
|
2021-08-30 01:37:53 -07:00
|
|
|
use solana_sdk::{
|
2022-04-20 22:15:07 -07:00
|
|
|
commitment_config::CommitmentLevel,
|
2021-08-30 01:37:53 -07:00
|
|
|
rent::Rent,
|
|
|
|
secp256k1_instruction::new_secp256k1_instruction,
|
|
|
|
signature::{
|
|
|
|
Keypair,
|
|
|
|
Signer,
|
|
|
|
},
|
2022-04-20 22:15:07 -07:00
|
|
|
signers::Signers,
|
2021-08-30 01:37:53 -07:00
|
|
|
transaction::Transaction,
|
2022-04-20 22:15:07 -07:00
|
|
|
};
|
2022-06-10 09:59:15 -07:00
|
|
|
use solitaire::processors::seeded::Seeded;
|
2021-08-30 01:37:53 -07:00
|
|
|
use std::{
|
|
|
|
env,
|
|
|
|
io::{
|
|
|
|
Cursor,
|
|
|
|
Write,
|
|
|
|
},
|
2022-06-10 09:59:15 -07:00
|
|
|
time::SystemTime,
|
2021-08-30 01:37:53 -07:00
|
|
|
};
|
|
|
|
|
2022-04-20 22:15:07 -07:00
|
|
|
use nft_bridge::{
|
2021-08-30 01:37:53 -07:00
|
|
|
instructions,
|
|
|
|
types::*,
|
|
|
|
};
|
|
|
|
|
|
|
|
pub use helpers::*;
|
|
|
|
|
|
|
|
/// Simple API wrapper for quickly preparing and sending transactions.
|
2022-04-20 22:15:07 -07:00
|
|
|
pub async fn execute<T: Signers>(
|
|
|
|
client: &mut BanksClient,
|
2021-08-30 01:37:53 -07:00
|
|
|
payer: &Keypair,
|
2022-04-20 22:15:07 -07:00
|
|
|
signers: &T,
|
2021-08-30 01:37:53 -07:00
|
|
|
instructions: &[Instruction],
|
2022-04-20 22:15:07 -07:00
|
|
|
commitment_level: CommitmentLevel,
|
2022-07-22 06:01:41 -07:00
|
|
|
) -> Result<(), BanksClientError> {
|
2021-08-30 01:37:53 -07:00
|
|
|
let mut transaction = Transaction::new_with_payer(instructions, Some(&payer.pubkey()));
|
2022-04-20 22:15:07 -07:00
|
|
|
let recent_blockhash = client.get_latest_blockhash().await?;
|
|
|
|
transaction.sign(signers, recent_blockhash);
|
|
|
|
client
|
|
|
|
.process_transaction_with_commitment(transaction, commitment_level)
|
|
|
|
.await
|
2021-08-30 01:37:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
mod helpers {
|
2022-04-20 22:15:07 -07:00
|
|
|
use super::*;
|
|
|
|
use bridge::{
|
2022-06-10 09:59:15 -07:00
|
|
|
accounts::FeeCollector,
|
2022-04-20 22:15:07 -07:00
|
|
|
types::ConsistencyLevel,
|
|
|
|
PostVAAData,
|
2021-08-30 01:37:53 -07:00
|
|
|
};
|
2022-04-20 22:15:07 -07:00
|
|
|
use nft_bridge::{
|
2021-08-30 01:37:53 -07:00
|
|
|
CompleteNativeData,
|
|
|
|
CompleteWrappedData,
|
2022-04-20 22:15:07 -07:00
|
|
|
CompleteWrappedMetaData,
|
2021-08-30 01:37:53 -07:00
|
|
|
RegisterChainData,
|
|
|
|
TransferNativeData,
|
|
|
|
TransferWrappedData,
|
|
|
|
};
|
2022-04-20 22:15:07 -07:00
|
|
|
use primitive_types::U256;
|
|
|
|
use solana_program_test::processor;
|
2021-08-30 01:37:53 -07:00
|
|
|
|
2022-04-20 22:15:07 -07:00
|
|
|
use nft_bridge::messages::{
|
2021-08-30 01:37:53 -07:00
|
|
|
PayloadGovernanceRegisterChain,
|
|
|
|
PayloadTransfer,
|
|
|
|
};
|
2022-04-20 22:15:07 -07:00
|
|
|
|
|
|
|
/// 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>) {
|
|
|
|
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| {
|
2022-06-10 09:59:15 -07:00
|
|
|
let public_key = PublicKey::from_secret_key(key);
|
2022-04-20 22:15:07 -07:00
|
|
|
let mut h = sha3::Keccak256::default();
|
2022-06-10 09:59:15 -07:00
|
|
|
h.write_all(&public_key.serialize()[1..]).unwrap();
|
2022-04-20 22:15:07 -07:00
|
|
|
let key: [u8; 32] = h.finalize().into();
|
|
|
|
let mut address = [0u8; 20];
|
|
|
|
address.copy_from_slice(&key[12..]);
|
|
|
|
address
|
|
|
|
})
|
|
|
|
.collect(),
|
|
|
|
secret_keys,
|
|
|
|
)
|
|
|
|
}
|
2021-08-30 01:37:53 -07:00
|
|
|
|
|
|
|
/// Initialize the test environment, spins up a solana-test-validator in the background so that
|
|
|
|
/// each test has a fresh environment to work within.
|
2022-04-20 22:15:07 -07:00
|
|
|
pub async fn setup() -> (BanksClient, Keypair, Pubkey, Pubkey) {
|
2021-08-30 01:37:53 -07:00
|
|
|
let (program, token_program) = (
|
|
|
|
env::var("BRIDGE_PROGRAM")
|
2022-06-10 09:59:15 -07:00
|
|
|
.unwrap_or_else(|_| "Bridge1p5gheXUvJ6jGWGeCsgPKgnE3YgdGKRVCMY9o".to_string())
|
2021-08-30 01:37:53 -07:00
|
|
|
.parse::<Pubkey>()
|
|
|
|
.unwrap(),
|
2022-04-20 22:15:07 -07:00
|
|
|
env::var("NFT_BRIDGE_PROGRAM")
|
2022-06-10 09:59:15 -07:00
|
|
|
.unwrap_or_else(|_| "NFTWqJR8YnRVqPDvTJrYuLrQDitTG5AScqbeghi4zSA".to_string())
|
2021-08-30 01:37:53 -07:00
|
|
|
.parse::<Pubkey>()
|
|
|
|
.unwrap(),
|
|
|
|
);
|
|
|
|
|
2022-04-20 22:15:07 -07:00
|
|
|
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),
|
|
|
|
);
|
2021-08-30 01:37:53 -07:00
|
|
|
|
2022-04-20 22:15:07 -07:00
|
|
|
// Some instructions go over the limit when tracing is enabled but we need that for better
|
|
|
|
// logging. We don't really care about the limit during these tests anyway.
|
|
|
|
builder.set_compute_max_units(u64::MAX);
|
|
|
|
|
|
|
|
let (client, payer, _) = builder.start().await;
|
|
|
|
(client, payer, program, token_program)
|
2021-08-30 01:37:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Fetch account data, the loop is there to re-attempt until data is available.
|
2022-04-20 22:15:07 -07:00
|
|
|
pub async fn get_account_data<T: BorshDeserialize>(
|
|
|
|
client: &mut BanksClient,
|
|
|
|
account: Pubkey,
|
2021-08-30 01:37:53 -07:00
|
|
|
) -> Option<T> {
|
|
|
|
let account = client
|
2022-04-20 22:15:07 -07:00
|
|
|
.get_account_with_commitment(account, CommitmentLevel::Processed)
|
|
|
|
.await
|
|
|
|
.unwrap()
|
2021-08-30 01:37:53 -07:00
|
|
|
.unwrap();
|
2022-04-20 22:15:07 -07:00
|
|
|
T::try_from_slice(&account.data).ok()
|
2021-08-30 01:37:53 -07:00
|
|
|
}
|
|
|
|
|
2022-04-20 22:15:07 -07:00
|
|
|
pub async fn initialize_bridge(
|
|
|
|
client: &mut BanksClient,
|
|
|
|
program: Pubkey,
|
2021-08-30 01:37:53 -07:00
|
|
|
payer: &Keypair,
|
2022-04-20 22:15:07 -07:00
|
|
|
initial_guardians: &[[u8; 20]],
|
2022-07-22 06:01:41 -07:00
|
|
|
) -> Result<(), BanksClientError> {
|
2021-08-30 01:37:53 -07:00
|
|
|
execute(
|
|
|
|
client,
|
|
|
|
payer,
|
|
|
|
&[payer],
|
|
|
|
&[bridge::instructions::initialize(
|
2022-04-20 22:15:07 -07:00
|
|
|
program,
|
2021-08-30 01:37:53 -07:00
|
|
|
payer.pubkey(),
|
|
|
|
50,
|
|
|
|
2_000_000_000,
|
|
|
|
initial_guardians,
|
|
|
|
)
|
|
|
|
.unwrap()],
|
2022-04-20 22:15:07 -07:00
|
|
|
CommitmentLevel::Processed,
|
2021-08-30 01:37:53 -07:00
|
|
|
)
|
2022-04-20 22:15:07 -07:00
|
|
|
.await
|
2021-08-30 01:37:53 -07:00
|
|
|
}
|
|
|
|
|
2022-04-20 22:15:07 -07:00
|
|
|
pub async fn initialize(
|
|
|
|
client: &mut BanksClient,
|
|
|
|
program: Pubkey,
|
2021-08-30 01:37:53 -07:00
|
|
|
payer: &Keypair,
|
2022-04-20 22:15:07 -07:00
|
|
|
bridge: Pubkey,
|
2022-07-22 06:01:41 -07:00
|
|
|
) -> Result<(), BanksClientError> {
|
2022-04-20 22:15:07 -07:00
|
|
|
let instruction = instructions::initialize(program, payer.pubkey(), bridge)
|
2021-08-30 01:37:53 -07:00
|
|
|
.expect("Could not create Initialize instruction");
|
|
|
|
|
|
|
|
execute(
|
|
|
|
client,
|
|
|
|
payer,
|
|
|
|
&[payer],
|
|
|
|
&[instruction],
|
2022-04-20 22:15:07 -07:00
|
|
|
CommitmentLevel::Processed,
|
2021-08-30 01:37:53 -07:00
|
|
|
)
|
2022-04-20 22:15:07 -07:00
|
|
|
.await
|
2021-08-30 01:37:53 -07:00
|
|
|
}
|
|
|
|
|
2022-06-10 09:59:15 -07:00
|
|
|
#[allow(clippy::too_many_arguments)]
|
2022-04-20 22:15:07 -07:00
|
|
|
pub async fn transfer_native(
|
|
|
|
client: &mut BanksClient,
|
|
|
|
program: Pubkey,
|
|
|
|
bridge: Pubkey,
|
2021-08-30 01:37:53 -07:00
|
|
|
payer: &Keypair,
|
|
|
|
message: &Keypair,
|
|
|
|
from: &Keypair,
|
|
|
|
from_owner: &Keypair,
|
|
|
|
mint: Pubkey,
|
2022-07-22 06:01:41 -07:00
|
|
|
) -> Result<(), BanksClientError> {
|
2021-08-30 01:37:53 -07:00
|
|
|
let instruction = instructions::transfer_native(
|
2022-04-20 22:15:07 -07:00
|
|
|
program,
|
|
|
|
bridge,
|
2021-08-30 01:37:53 -07:00
|
|
|
payer.pubkey(),
|
|
|
|
message.pubkey(),
|
|
|
|
from.pubkey(),
|
|
|
|
mint,
|
|
|
|
TransferNativeData {
|
|
|
|
nonce: 0,
|
|
|
|
target_address: [0u8; 32],
|
|
|
|
target_chain: 2,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.expect("Could not create Transfer Native");
|
|
|
|
|
|
|
|
execute(
|
|
|
|
client,
|
|
|
|
payer,
|
|
|
|
&[payer, from_owner, message],
|
|
|
|
&[
|
|
|
|
spl_token::instruction::approve(
|
|
|
|
&spl_token::id(),
|
|
|
|
&from.pubkey(),
|
2022-04-20 22:15:07 -07:00
|
|
|
&nft_bridge::accounts::AuthoritySigner::key(None, &program),
|
2021-08-30 01:37:53 -07:00
|
|
|
&from_owner.pubkey(),
|
|
|
|
&[],
|
2022-04-20 22:15:07 -07:00
|
|
|
1,
|
2021-08-30 01:37:53 -07:00
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
instruction,
|
|
|
|
],
|
2022-04-20 22:15:07 -07:00
|
|
|
CommitmentLevel::Processed,
|
2021-08-30 01:37:53 -07:00
|
|
|
)
|
2022-04-20 22:15:07 -07:00
|
|
|
.await
|
2021-08-30 01:37:53 -07:00
|
|
|
}
|
|
|
|
|
2022-06-10 09:59:15 -07:00
|
|
|
#[allow(clippy::too_many_arguments)]
|
2022-04-20 22:15:07 -07:00
|
|
|
pub async fn transfer_wrapped(
|
|
|
|
client: &mut BanksClient,
|
|
|
|
program: Pubkey,
|
|
|
|
bridge: Pubkey,
|
2021-08-30 01:37:53 -07:00
|
|
|
payer: &Keypair,
|
|
|
|
message: &Keypair,
|
|
|
|
from: Pubkey,
|
|
|
|
from_owner: &Keypair,
|
|
|
|
token_chain: u16,
|
|
|
|
token_address: Address,
|
2022-04-20 22:15:07 -07:00
|
|
|
token_id: U256,
|
2022-07-22 06:01:41 -07:00
|
|
|
) -> Result<(), BanksClientError> {
|
2021-08-30 01:37:53 -07:00
|
|
|
let instruction = instructions::transfer_wrapped(
|
2022-04-20 22:15:07 -07:00
|
|
|
program,
|
|
|
|
bridge,
|
2021-08-30 01:37:53 -07:00
|
|
|
payer.pubkey(),
|
|
|
|
message.pubkey(),
|
|
|
|
from,
|
|
|
|
from_owner.pubkey(),
|
|
|
|
token_chain,
|
|
|
|
token_address,
|
2022-04-20 22:15:07 -07:00
|
|
|
token_id,
|
2021-08-30 01:37:53 -07:00
|
|
|
TransferWrappedData {
|
|
|
|
nonce: 0,
|
|
|
|
target_address: [5u8; 32],
|
|
|
|
target_chain: 2,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.expect("Could not create Transfer Native");
|
|
|
|
|
|
|
|
execute(
|
|
|
|
client,
|
|
|
|
payer,
|
|
|
|
&[payer, from_owner, message],
|
|
|
|
&[
|
|
|
|
spl_token::instruction::approve(
|
|
|
|
&spl_token::id(),
|
|
|
|
&from,
|
2022-04-20 22:15:07 -07:00
|
|
|
&nft_bridge::accounts::AuthoritySigner::key(None, &program),
|
2021-08-30 01:37:53 -07:00
|
|
|
&from_owner.pubkey(),
|
|
|
|
&[],
|
2022-04-20 22:15:07 -07:00
|
|
|
1,
|
2021-08-30 01:37:53 -07:00
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
instruction,
|
|
|
|
],
|
2022-04-20 22:15:07 -07:00
|
|
|
CommitmentLevel::Processed,
|
2021-08-30 01:37:53 -07:00
|
|
|
)
|
2022-04-20 22:15:07 -07:00
|
|
|
.await
|
2021-08-30 01:37:53 -07:00
|
|
|
}
|
|
|
|
|
2022-04-20 22:15:07 -07:00
|
|
|
pub async fn register_chain(
|
|
|
|
client: &mut BanksClient,
|
|
|
|
program: Pubkey,
|
|
|
|
bridge: Pubkey,
|
|
|
|
message_acc: Pubkey,
|
2021-08-30 01:37:53 -07:00
|
|
|
vaa: PostVAAData,
|
|
|
|
payload: PayloadGovernanceRegisterChain,
|
|
|
|
payer: &Keypair,
|
2022-07-22 06:01:41 -07:00
|
|
|
) -> Result<(), BanksClientError> {
|
2021-08-30 01:37:53 -07:00
|
|
|
let instruction = instructions::register_chain(
|
2022-04-20 22:15:07 -07:00
|
|
|
program,
|
|
|
|
bridge,
|
2021-08-30 01:37:53 -07:00
|
|
|
payer.pubkey(),
|
2022-04-20 22:15:07 -07:00
|
|
|
message_acc,
|
2021-08-30 01:37:53 -07:00
|
|
|
vaa,
|
|
|
|
payload,
|
|
|
|
RegisterChainData {},
|
|
|
|
)
|
|
|
|
.expect("Could not create Transfer Native");
|
|
|
|
|
|
|
|
execute(
|
|
|
|
client,
|
|
|
|
payer,
|
|
|
|
&[payer],
|
|
|
|
&[instruction],
|
2022-04-20 22:15:07 -07:00
|
|
|
CommitmentLevel::Processed,
|
2021-08-30 01:37:53 -07:00
|
|
|
)
|
2022-04-20 22:15:07 -07:00
|
|
|
.await
|
2021-08-30 01:37:53 -07:00
|
|
|
}
|
|
|
|
|
2022-06-10 09:59:15 -07:00
|
|
|
#[allow(clippy::too_many_arguments)]
|
2022-04-20 22:15:07 -07:00
|
|
|
pub async fn complete_native(
|
|
|
|
client: &mut BanksClient,
|
|
|
|
program: Pubkey,
|
|
|
|
bridge: Pubkey,
|
|
|
|
message_acc: Pubkey,
|
2021-08-30 01:37:53 -07:00
|
|
|
vaa: PostVAAData,
|
2022-06-10 09:59:15 -07:00
|
|
|
_payload: PayloadTransfer,
|
2021-08-30 01:37:53 -07:00
|
|
|
payer: &Keypair,
|
2022-04-20 22:15:07 -07:00
|
|
|
to_authority: Pubkey,
|
|
|
|
mint: Pubkey,
|
2022-07-22 06:01:41 -07:00
|
|
|
) -> Result<(), BanksClientError> {
|
2021-08-30 01:37:53 -07:00
|
|
|
let instruction = instructions::complete_native(
|
2022-04-20 22:15:07 -07:00
|
|
|
program,
|
|
|
|
bridge,
|
2021-08-30 01:37:53 -07:00
|
|
|
payer.pubkey(),
|
2022-04-20 22:15:07 -07:00
|
|
|
message_acc,
|
2021-08-30 01:37:53 -07:00
|
|
|
vaa,
|
2022-04-20 22:15:07 -07:00
|
|
|
to_authority,
|
|
|
|
mint,
|
2021-08-30 01:37:53 -07:00
|
|
|
CompleteNativeData {},
|
|
|
|
)
|
|
|
|
.expect("Could not create Complete Native instruction");
|
|
|
|
|
|
|
|
execute(
|
|
|
|
client,
|
|
|
|
payer,
|
|
|
|
&[payer],
|
|
|
|
&[instruction],
|
2022-04-20 22:15:07 -07:00
|
|
|
CommitmentLevel::Processed,
|
2021-08-30 01:37:53 -07:00
|
|
|
)
|
2022-04-20 22:15:07 -07:00
|
|
|
.await
|
2021-08-30 01:37:53 -07:00
|
|
|
}
|
|
|
|
|
2022-06-10 09:59:15 -07:00
|
|
|
#[allow(clippy::too_many_arguments)]
|
2022-04-20 22:15:07 -07:00
|
|
|
pub async fn complete_wrapped(
|
|
|
|
client: &mut BanksClient,
|
|
|
|
program: Pubkey,
|
|
|
|
bridge: Pubkey,
|
|
|
|
message_acc: Pubkey,
|
2021-08-30 01:37:53 -07:00
|
|
|
vaa: PostVAAData,
|
|
|
|
payload: PayloadTransfer,
|
2022-04-20 22:15:07 -07:00
|
|
|
to: Pubkey,
|
2021-08-30 01:37:53 -07:00
|
|
|
payer: &Keypair,
|
2022-07-22 06:01:41 -07:00
|
|
|
) -> Result<(), BanksClientError> {
|
2021-08-30 01:37:53 -07:00
|
|
|
let instruction = instructions::complete_wrapped(
|
2022-04-20 22:15:07 -07:00
|
|
|
program,
|
|
|
|
bridge,
|
2021-08-30 01:37:53 -07:00
|
|
|
payer.pubkey(),
|
2022-04-20 22:15:07 -07:00
|
|
|
message_acc,
|
2021-08-30 01:37:53 -07:00
|
|
|
vaa,
|
|
|
|
payload,
|
|
|
|
to,
|
|
|
|
CompleteWrappedData {},
|
|
|
|
)
|
|
|
|
.expect("Could not create Complete Wrapped instruction");
|
|
|
|
|
|
|
|
execute(
|
|
|
|
client,
|
|
|
|
payer,
|
|
|
|
&[payer],
|
|
|
|
&[instruction],
|
2022-04-20 22:15:07 -07:00
|
|
|
CommitmentLevel::Processed,
|
2021-08-30 01:37:53 -07:00
|
|
|
)
|
2022-04-20 22:15:07 -07:00
|
|
|
.await
|
2021-08-30 01:37:53 -07:00
|
|
|
}
|
|
|
|
|
2022-04-20 22:15:07 -07:00
|
|
|
pub async fn complete_wrapped_meta(
|
|
|
|
client: &mut BanksClient,
|
|
|
|
program: Pubkey,
|
|
|
|
bridge: Pubkey,
|
|
|
|
message_acc: Pubkey,
|
2021-08-30 01:37:53 -07:00
|
|
|
vaa: PostVAAData,
|
2022-04-20 22:15:07 -07:00
|
|
|
payload: PayloadTransfer,
|
2021-08-30 01:37:53 -07:00
|
|
|
payer: &Keypair,
|
2022-07-22 06:01:41 -07:00
|
|
|
) -> Result<(), BanksClientError> {
|
2022-04-20 22:15:07 -07:00
|
|
|
let instruction = instructions::complete_wrapped_meta(
|
|
|
|
program,
|
|
|
|
bridge,
|
2021-08-30 01:37:53 -07:00
|
|
|
payer.pubkey(),
|
2022-04-20 22:15:07 -07:00
|
|
|
message_acc,
|
2021-08-30 01:37:53 -07:00
|
|
|
vaa,
|
|
|
|
payload,
|
2022-04-20 22:15:07 -07:00
|
|
|
CompleteWrappedMetaData {},
|
2021-08-30 01:37:53 -07:00
|
|
|
)
|
2022-04-20 22:15:07 -07:00
|
|
|
.expect("Could not create Complete Wrapped Meta instruction");
|
2021-08-30 01:37:53 -07:00
|
|
|
|
|
|
|
execute(
|
|
|
|
client,
|
|
|
|
payer,
|
|
|
|
&[payer],
|
|
|
|
&[instruction],
|
2022-04-20 22:15:07 -07:00
|
|
|
CommitmentLevel::Processed,
|
2021-08-30 01:37:53 -07:00
|
|
|
)
|
2022-04-20 22:15:07 -07:00
|
|
|
.await
|
2021-08-30 01:37:53 -07:00
|
|
|
}
|
|
|
|
|
2022-04-20 22:15:07 -07:00
|
|
|
pub async fn create_mint(
|
|
|
|
client: &mut BanksClient,
|
2021-08-30 01:37:53 -07:00
|
|
|
payer: &Keypair,
|
|
|
|
mint_authority: &Pubkey,
|
|
|
|
mint: &Keypair,
|
2022-07-22 06:01:41 -07:00
|
|
|
) -> Result<(), BanksClientError> {
|
2022-04-20 22:15:07 -07:00
|
|
|
let mint_key = mint.pubkey();
|
2021-08-30 01:37:53 -07:00
|
|
|
execute(
|
|
|
|
client,
|
|
|
|
payer,
|
|
|
|
&[payer, mint],
|
|
|
|
&[
|
|
|
|
solana_sdk::system_instruction::create_account(
|
|
|
|
&payer.pubkey(),
|
2022-04-20 22:15:07 -07:00
|
|
|
&mint_key,
|
2021-08-30 01:37:53 -07:00
|
|
|
Rent::default().minimum_balance(spl_token::state::Mint::LEN),
|
|
|
|
spl_token::state::Mint::LEN as u64,
|
|
|
|
&spl_token::id(),
|
|
|
|
),
|
|
|
|
spl_token::instruction::initialize_mint(
|
|
|
|
&spl_token::id(),
|
2022-04-20 22:15:07 -07:00
|
|
|
&mint_key,
|
2021-08-30 01:37:53 -07:00
|
|
|
mint_authority,
|
|
|
|
None,
|
|
|
|
0,
|
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
],
|
2022-04-20 22:15:07 -07:00
|
|
|
CommitmentLevel::Processed,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn create_token_account(
|
|
|
|
client: &mut BanksClient,
|
|
|
|
payer: &Keypair,
|
|
|
|
token_acc: &Keypair,
|
|
|
|
token_authority: &Pubkey,
|
|
|
|
mint: &Pubkey,
|
2022-07-22 06:01:41 -07:00
|
|
|
) -> Result<(), BanksClientError> {
|
2022-04-20 22:15:07 -07:00
|
|
|
let token_key = token_acc.pubkey();
|
|
|
|
execute(
|
|
|
|
client,
|
|
|
|
payer,
|
|
|
|
&[payer, token_acc],
|
|
|
|
&[
|
|
|
|
solana_sdk::system_instruction::create_account(
|
|
|
|
&payer.pubkey(),
|
|
|
|
&token_key,
|
|
|
|
Rent::default().minimum_balance(spl_token::state::Account::LEN),
|
|
|
|
spl_token::state::Account::LEN as u64,
|
|
|
|
&spl_token::id(),
|
|
|
|
),
|
|
|
|
spl_token::instruction::initialize_account(
|
|
|
|
&spl_token::id(),
|
|
|
|
&token_key,
|
|
|
|
mint,
|
|
|
|
token_authority,
|
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
],
|
|
|
|
CommitmentLevel::Processed,
|
2021-08-30 01:37:53 -07:00
|
|
|
)
|
2022-04-20 22:15:07 -07:00
|
|
|
.await
|
2021-08-30 01:37:53 -07:00
|
|
|
}
|
|
|
|
|
2022-06-10 09:59:15 -07:00
|
|
|
#[allow(clippy::too_many_arguments)]
|
2022-04-20 22:15:07 -07:00
|
|
|
pub async fn create_spl_metadata(
|
|
|
|
client: &mut BanksClient,
|
2021-08-30 01:37:53 -07:00
|
|
|
payer: &Keypair,
|
2022-04-20 22:15:07 -07:00
|
|
|
metadata_account: Pubkey,
|
2021-08-30 01:37:53 -07:00
|
|
|
mint_authority: &Keypair,
|
2022-04-20 22:15:07 -07:00
|
|
|
mint: Pubkey,
|
|
|
|
update_authority: Pubkey,
|
2021-08-30 01:37:53 -07:00
|
|
|
name: String,
|
|
|
|
symbol: String,
|
2022-04-20 22:15:07 -07:00
|
|
|
uri: String,
|
2022-07-22 06:01:41 -07:00
|
|
|
) -> Result<(), BanksClientError> {
|
2021-08-30 01:37:53 -07:00
|
|
|
execute(
|
|
|
|
client,
|
|
|
|
payer,
|
|
|
|
&[payer, mint_authority],
|
|
|
|
&[spl_token_metadata::instruction::create_metadata_accounts(
|
|
|
|
spl_token_metadata::id(),
|
2022-04-20 22:15:07 -07:00
|
|
|
metadata_account,
|
|
|
|
mint,
|
2021-08-30 01:37:53 -07:00
|
|
|
mint_authority.pubkey(),
|
|
|
|
payer.pubkey(),
|
2022-04-20 22:15:07 -07:00
|
|
|
update_authority,
|
2021-08-30 01:37:53 -07:00
|
|
|
name,
|
|
|
|
symbol,
|
2022-04-20 22:15:07 -07:00
|
|
|
uri,
|
2021-08-30 01:37:53 -07:00
|
|
|
None,
|
|
|
|
0,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
)],
|
2022-04-20 22:15:07 -07:00
|
|
|
CommitmentLevel::Processed,
|
2021-08-30 01:37:53 -07:00
|
|
|
)
|
2022-04-20 22:15:07 -07:00
|
|
|
.await
|
2021-08-30 01:37:53 -07:00
|
|
|
}
|
|
|
|
|
2022-04-20 22:15:07 -07:00
|
|
|
pub async fn mint_tokens(
|
|
|
|
client: &mut BanksClient,
|
2021-08-30 01:37:53 -07:00
|
|
|
payer: &Keypair,
|
|
|
|
mint_authority: &Keypair,
|
|
|
|
mint: &Keypair,
|
|
|
|
token_account: &Pubkey,
|
|
|
|
amount: u64,
|
2022-07-22 06:01:41 -07:00
|
|
|
) -> Result<(), BanksClientError> {
|
2021-08-30 01:37:53 -07:00
|
|
|
execute(
|
|
|
|
client,
|
|
|
|
payer,
|
2022-04-20 22:15:07 -07:00
|
|
|
&[payer, mint_authority],
|
2021-08-30 01:37:53 -07:00
|
|
|
&[spl_token::instruction::mint_to(
|
|
|
|
&spl_token::id(),
|
|
|
|
&mint.pubkey(),
|
|
|
|
token_account,
|
|
|
|
&mint_authority.pubkey(),
|
|
|
|
&[],
|
|
|
|
amount,
|
|
|
|
)
|
|
|
|
.unwrap()],
|
2022-04-20 22:15:07 -07:00
|
|
|
CommitmentLevel::Processed,
|
2021-08-30 01:37:53 -07:00
|
|
|
)
|
2022-04-20 22:15:07 -07:00
|
|
|
.await
|
2021-08-30 01:37:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Utility function for generating VAA's from message data.
|
2022-04-20 22:15:07 -07:00
|
|
|
pub fn generate_vaa<T: Into<Vec<u8>>>(
|
2021-08-30 01:37:53 -07:00
|
|
|
emitter: Address,
|
|
|
|
emitter_chain: u16,
|
2022-04-20 22:15:07 -07:00
|
|
|
data: T,
|
2021-08-30 01:37:53 -07:00
|
|
|
nonce: u32,
|
|
|
|
sequence: u64,
|
|
|
|
) -> (PostVAAData, [u8; 32], [u8; 32]) {
|
2022-06-10 09:59:15 -07:00
|
|
|
let vaa = PostVAAData {
|
2021-08-30 01:37:53 -07:00
|
|
|
version: 0,
|
|
|
|
guardian_set_index: 0,
|
|
|
|
|
|
|
|
// Body part
|
|
|
|
emitter_chain,
|
|
|
|
emitter_address: emitter,
|
|
|
|
sequence,
|
2022-04-20 22:15:07 -07:00
|
|
|
payload: data.into(),
|
2021-08-30 01:37:53 -07:00
|
|
|
timestamp: SystemTime::now()
|
|
|
|
.duration_since(SystemTime::UNIX_EPOCH)
|
|
|
|
.unwrap()
|
|
|
|
.as_secs() as u32,
|
|
|
|
nonce,
|
|
|
|
consistency_level: ConsistencyLevel::Confirmed as u8,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Hash data, the thing we wish to actually sign.
|
|
|
|
let body = {
|
|
|
|
let mut v = Cursor::new(Vec::new());
|
|
|
|
v.write_u32::<BigEndian>(vaa.timestamp).unwrap();
|
|
|
|
v.write_u32::<BigEndian>(vaa.nonce).unwrap();
|
|
|
|
v.write_u16::<BigEndian>(vaa.emitter_chain).unwrap();
|
2022-06-10 09:59:15 -07:00
|
|
|
v.write_all(&vaa.emitter_address).unwrap();
|
2021-08-30 01:37:53 -07:00
|
|
|
v.write_u64::<BigEndian>(vaa.sequence).unwrap();
|
|
|
|
v.write_u8(vaa.consistency_level).unwrap();
|
2022-06-10 09:59:15 -07:00
|
|
|
v.write_all(&vaa.payload).unwrap();
|
2021-08-30 01:37:53 -07:00
|
|
|
v.into_inner()
|
|
|
|
};
|
|
|
|
|
|
|
|
// Hash this body, which is expected to be the same as the hash currently stored in the
|
|
|
|
// signature account, binding that set of signatures to this VAA.
|
|
|
|
let body: [u8; 32] = {
|
|
|
|
let mut h = sha3::Keccak256::default();
|
2022-06-10 09:59:15 -07:00
|
|
|
h.write_all(body.as_slice()).unwrap();
|
2021-08-30 01:37:53 -07:00
|
|
|
h.finalize().into()
|
|
|
|
};
|
|
|
|
|
|
|
|
let body_hash: [u8; 32] = {
|
|
|
|
let mut h = sha3::Keccak256::default();
|
2022-06-10 09:59:15 -07:00
|
|
|
h.write_all(&body).unwrap();
|
2021-08-30 01:37:53 -07:00
|
|
|
h.finalize().into()
|
|
|
|
};
|
|
|
|
|
|
|
|
(vaa, body, body_hash)
|
|
|
|
}
|
|
|
|
|
2022-04-20 22:15:07 -07:00
|
|
|
pub async fn verify_signatures(
|
|
|
|
client: &mut BanksClient,
|
2021-08-30 01:37:53 -07:00
|
|
|
program: &Pubkey,
|
|
|
|
payer: &Keypair,
|
2022-04-20 22:15:07 -07:00
|
|
|
body: [u8; 32],
|
|
|
|
secret_keys: &[SecretKey],
|
|
|
|
guardian_set_version: u32,
|
2022-07-22 06:01:41 -07:00
|
|
|
) -> Result<Pubkey, BanksClientError> {
|
2022-04-20 22:15:07 -07:00
|
|
|
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;
|
|
|
|
|
|
|
|
execute(
|
|
|
|
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,
|
2021-08-30 01:37:53 -07:00
|
|
|
vaa: PostVAAData,
|
2022-07-22 06:01:41 -07:00
|
|
|
) -> Result<(), BanksClientError> {
|
2021-08-30 01:37:53 -07:00
|
|
|
let instruction =
|
2022-04-20 22:15:07 -07:00
|
|
|
bridge::instructions::post_vaa(program, payer.pubkey(), signature_set, vaa);
|
2021-08-30 01:37:53 -07:00
|
|
|
|
|
|
|
execute(
|
|
|
|
client,
|
|
|
|
payer,
|
|
|
|
&[payer],
|
|
|
|
&[instruction],
|
2022-04-20 22:15:07 -07:00
|
|
|
CommitmentLevel::Processed,
|
|
|
|
)
|
|
|
|
.await
|
2021-08-30 01:37:53 -07:00
|
|
|
}
|
|
|
|
|
2022-06-10 09:59:15 -07:00
|
|
|
#[allow(clippy::too_many_arguments)]
|
|
|
|
// This function is not used, but looks useful...
|
|
|
|
#[allow(dead_code)]
|
2022-04-20 22:15:07 -07:00
|
|
|
pub async fn post_message(
|
|
|
|
client: &mut BanksClient,
|
|
|
|
program: Pubkey,
|
2021-08-30 01:37:53 -07:00
|
|
|
payer: &Keypair,
|
|
|
|
emitter: &Keypair,
|
|
|
|
message: &Keypair,
|
|
|
|
nonce: u32,
|
|
|
|
data: Vec<u8>,
|
|
|
|
fee: u64,
|
2022-07-22 06:01:41 -07:00
|
|
|
) -> Result<(), BanksClientError> {
|
2021-08-30 01:37:53 -07:00
|
|
|
// Transfer money into the fee collector as it needs a balance/must exist.
|
2022-04-20 22:15:07 -07:00
|
|
|
let fee_collector = FeeCollector::<'_>::key(None, &program);
|
2021-08-30 01:37:53 -07:00
|
|
|
|
|
|
|
// Capture the resulting message, later functions will need this.
|
|
|
|
let instruction = bridge::instructions::post_message(
|
2022-04-20 22:15:07 -07:00
|
|
|
program,
|
2021-08-30 01:37:53 -07:00
|
|
|
payer.pubkey(),
|
|
|
|
emitter.pubkey(),
|
|
|
|
message.pubkey(),
|
|
|
|
nonce,
|
|
|
|
data,
|
|
|
|
ConsistencyLevel::Confirmed,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
execute(
|
|
|
|
client,
|
|
|
|
payer,
|
|
|
|
&[payer, emitter, message],
|
|
|
|
&[
|
|
|
|
system_instruction::transfer(&payer.pubkey(), &fee_collector, fee),
|
|
|
|
instruction,
|
|
|
|
],
|
2022-04-20 22:15:07 -07:00
|
|
|
CommitmentLevel::Processed,
|
|
|
|
)
|
|
|
|
.await
|
2021-08-30 01:37:53 -07:00
|
|
|
}
|
|
|
|
}
|