Derive SplTokenMeta addresses

Change-Id: I4a988b0778c741c1405d096ea0d7aa67a2a31190
This commit is contained in:
Reisen 2021-08-09 15:06:54 +00:00
parent 8718e31fb4
commit f8a0184a1d
5 changed files with 55 additions and 17 deletions

View File

@ -86,3 +86,19 @@ impl<'b, const State: AccountState> Seeded<&EndpointDerivationData> for Endpoint
]
}
}
pub type SplTokenMeta<'b> = Info<'b>;
pub struct SplTokenMetaDerivationData {
pub mint: Pubkey,
}
impl<'b> Seeded<&SplTokenMetaDerivationData> for SplTokenMeta<'b> {
fn seeds(data: &SplTokenMetaDerivationData) -> Vec<Vec<u8>> {
vec![
"metadata".as_bytes().to_vec(),
spl_token_metadata::id().as_ref().to_vec(),
data.mint.as_ref().to_vec(),
]
}
}

View File

@ -2,6 +2,8 @@ use crate::{
accounts::{
ConfigAccount,
EmitterAccount,
SplTokenMeta,
SplTokenMetaDerivationData,
WrappedMetaDerivationData,
WrappedTokenMeta,
},
@ -71,7 +73,7 @@ pub struct AttestToken<'b> {
pub wrapped_meta: WrappedTokenMeta<'b, { AccountState::Uninitialized }>,
/// SPL Metadata for the associated Mint
pub spl_metadata: Info<'b>,
pub spl_metadata: SplTokenMeta<'b>,
/// CPI Context
pub bridge: Mut<Info<'b>>,
@ -102,6 +104,14 @@ impl<'a> From<&AttestToken<'a>> for WrappedMetaDerivationData {
}
}
impl<'a> From<&AttestToken<'a>> for SplTokenMetaDerivationData {
fn from(accs: &AttestToken<'a>) -> Self {
SplTokenMetaDerivationData {
mint: *accs.mint.info().key,
}
}
}
#[derive(BorshDeserialize, BorshSerialize, Default)]
pub struct AttestTokenData {
pub nonce: u32,
@ -134,6 +144,10 @@ pub fn attest_token(
// Assign metadata if an SPL Metadata account exists for the SPL token in question.
if !accs.spl_metadata.data_is_empty() {
let derivation_data: SplTokenMetaDerivationData = (&*accs).into();
accs.spl_metadata
.verify_derivation(&spl_token_metadata::id(), &derivation_data)?;
if *accs.spl_metadata.owner != spl_token_metadata::id() {
return Err(WrongAccountOwner.into());
}

View File

@ -9,6 +9,8 @@ use crate::{
Endpoint,
EndpointDerivationData,
MintSigner,
SplTokenMeta,
SplTokenMetaDerivationData,
WrappedDerivationData,
WrappedMetaDerivationData,
WrappedMint,
@ -447,24 +449,27 @@ pub fn attest(
message_key: Pubkey,
mint: Pubkey,
decimals: u8,
mint_meta: Pubkey,
spl_metadata: Pubkey,
symbol: String,
name: String,
nonce: u32,
) -> solitaire::Result<Instruction> {
let config_key = ConfigAccount::<'_, { AccountState::Uninitialized }>::key(None, &program_id);
let emitter_key = EmitterAccount::key(None, &program_id);
// Bridge keys
// SPL Metadata
let spl_metadata = SplTokenMeta::key(
&SplTokenMetaDerivationData { mint },
&spl_token_metadata::id(),
);
// Mint Metadata
let mint_meta = WrappedTokenMeta::<'_, { AccountState::Uninitialized }>::key(
&WrappedMetaDerivationData {
mint_key: mint,
},
&bridge_id,
);
// Bridge Keys
let bridge_config = Bridge::<'_, { AccountState::Uninitialized }>::key(None, &bridge_id);
let payload = PayloadAssetMeta {
token_address: mint.to_bytes(),
token_chain: 1,
decimals,
symbol,
name,
};
let sequence_key = Sequence::key(
&SequenceDerivationData {
emitter_key: &emitter_key,

View File

@ -19,7 +19,6 @@ use spl_token::state::{
Mint,
};
use spl_token_metadata::state::Metadata;
use std::str::FromStr;
pub type Address = [u8; 32];
pub type ChainID = u16;

View File

@ -58,7 +58,6 @@ pub fn attest_ix(
message: String,
mint: String,
decimals: u8,
mint_meta: String,
nonce: u32,
) -> JsValue {
let program_id = Pubkey::from_str(program_id.as_str()).unwrap();
@ -66,10 +65,15 @@ pub fn attest_ix(
let payer = Pubkey::from_str(payer.as_str()).unwrap();
let message = Pubkey::from_str(message.as_str()).unwrap();
let mint = Pubkey::from_str(mint.as_str()).unwrap();
let mint_meta = Pubkey::from_str(mint_meta.as_str()).unwrap();
let ix = attest(
program_id, bridge_id, payer, message, mint, decimals, mint_meta, nonce,
program_id,
bridge_id,
payer,
message,
mint,
decimals,
nonce,
)
.unwrap();