Add more wasm helpers to the token bridge

Change-Id: I484361ef4b0eb2cd52913e695cf83ddccb5bfe7e
This commit is contained in:
Hendrik Hofstadt 2021-08-03 15:27:43 +02:00
parent 24f7780883
commit 8f1e980b4c
2 changed files with 67 additions and 3 deletions

View File

@ -2,6 +2,10 @@ use borsh::{
BorshDeserialize, BorshDeserialize,
BorshSerialize, BorshSerialize,
}; };
use serde::{
Deserialize,
Serialize,
};
use solana_program::pubkey::Pubkey; use solana_program::pubkey::Pubkey;
use solitaire::{ use solitaire::{
pack_type, pack_type,
@ -18,7 +22,7 @@ use spl_token::state::{
pub type Address = [u8; 32]; pub type Address = [u8; 32];
pub type ChainID = u16; pub type ChainID = u16;
#[derive(Default, Clone, Copy, BorshDeserialize, BorshSerialize)] #[derive(Default, Clone, Copy, BorshDeserialize, BorshSerialize, Serialize, Deserialize)]
pub struct Config { pub struct Config {
pub wormhole_bridge: Pubkey, pub wormhole_bridge: Pubkey,
} }
@ -29,7 +33,7 @@ impl Owned for Config {
} }
} }
#[derive(Default, Clone, Copy, BorshDeserialize, BorshSerialize)] #[derive(Default, Clone, Copy, BorshDeserialize, BorshSerialize, Serialize, Deserialize)]
pub struct EndpointRegistration { pub struct EndpointRegistration {
pub chain: ChainID, pub chain: ChainID,
pub contract: Address, pub contract: Address,
@ -41,7 +45,7 @@ impl Owned for EndpointRegistration {
} }
} }
#[derive(Default, Clone, Copy, BorshDeserialize, BorshSerialize)] #[derive(Default, Clone, Copy, BorshDeserialize, BorshSerialize, Serialize, Deserialize)]
pub struct WrappedMeta { pub struct WrappedMeta {
pub chain: ChainID, pub chain: ChainID,
pub token_address: Address, pub token_address: Address,

View File

@ -1,4 +1,11 @@
use crate::{ use crate::{
accounts::{
AuthoritySigner,
WrappedDerivationData,
WrappedMetaDerivationData,
WrappedMint,
WrappedTokenMeta,
},
instructions::{ instructions::{
attest, attest,
complete_native, complete_native,
@ -15,6 +22,10 @@ use crate::{
PayloadGovernanceRegisterChain, PayloadGovernanceRegisterChain,
PayloadTransfer, PayloadTransfer,
}, },
types::{
EndpointRegistration,
WrappedMeta,
},
CompleteNativeData, CompleteNativeData,
CompleteWrappedData, CompleteWrappedData,
CreateWrappedData, CreateWrappedData,
@ -22,6 +33,7 @@ use crate::{
TransferNativeData, TransferNativeData,
TransferWrappedData, TransferWrappedData,
}; };
use borsh::BorshDeserialize;
use bridge::{ use bridge::{
accounts::MessageDerivationData, accounts::MessageDerivationData,
vaa::VAA, vaa::VAA,
@ -372,3 +384,51 @@ pub fn register_chain_ix(
.unwrap(); .unwrap();
return JsValue::from_serde(&ix).unwrap(); return JsValue::from_serde(&ix).unwrap();
} }
#[wasm_bindgen]
pub fn approval_authority_address(program_id: String) -> Vec<u8> {
let program_id = Pubkey::from_str(program_id.as_str()).unwrap();
let approval_authority = AuthoritySigner::key(None, &program_id);
approval_authority.to_bytes().to_vec()
}
#[wasm_bindgen]
pub fn wrapped_address(program_id: String, token_address: Vec<u8>, token_chain: u16) -> Vec<u8> {
let program_id = Pubkey::from_str(program_id.as_str()).unwrap();
let mut t_addr = [0u8; 32];
t_addr.copy_from_slice(&token_address);
let wrapped_addr = WrappedMint::<'_, { AccountState::Initialized }>::key(
&WrappedDerivationData {
token_address: t_addr,
token_chain,
},
&program_id,
);
wrapped_addr.to_bytes().to_vec()
}
#[wasm_bindgen]
pub fn wrapped_meta_address(program_id: String, mint_address: Vec<u8>) -> Vec<u8> {
let program_id = Pubkey::from_str(program_id.as_str()).unwrap();
let mint_key = Pubkey::new(mint_address.as_slice());
let wrapped_meta_addr = WrappedTokenMeta::<'_, { AccountState::Initialized }>::key(
&WrappedMetaDerivationData { mint_key },
&program_id,
);
wrapped_meta_addr.to_bytes().to_vec()
}
#[wasm_bindgen]
pub fn parse_wrapped_meta(data: Vec<u8>) -> JsValue {
JsValue::from_serde(&WrappedMeta::try_from_slice(data.as_slice()).unwrap()).unwrap()
}
#[wasm_bindgen]
pub fn parse_endpoint_registration(data: Vec<u8>) -> JsValue {
JsValue::from_serde(&EndpointRegistration::try_from_slice(data.as_slice()).unwrap()).unwrap()
}