Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix serialization #54

Merged
merged 1 commit into from
Jun 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion token/js/client/token.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ const AccountInfoLayout = BufferLayout.struct([
Layout.publicKey('token'),
Layout.publicKey('owner'),
Layout.uint64('amount'),
BufferLayout.nu64('option'),
BufferLayout.u32('option'),
BufferLayout.u32('padding'),
Layout.publicKey('source'),
Layout.uint64('originalAmount'),
]);
Expand Down
17 changes: 16 additions & 1 deletion token/src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,33 +89,48 @@ pub enum TokenInstruction {
impl TokenInstruction {
/// Deserializes a byte buffer into an [TokenInstruction](enum.TokenInstruction.html)
pub fn deserialize(input: &[u8]) -> Result<Self, ProgramError> {
if input.len() < size_of::<TokenInstruction>() {
if input.len() < size_of::<u8>() {
return Err(ProgramError::InvalidAccountData);
}
Ok(match input[0] {
0 => {
if input.len() < size_of::<u8>() + size_of::<TokenInfo>() {
return Err(ProgramError::InvalidAccountData);
}
#[allow(clippy::cast_ptr_alignment)]
let info: &TokenInfo = unsafe { &*(&input[1] as *const u8 as *const TokenInfo) };
Self::NewToken(*info)
}
1 => Self::NewAccount,
2 => {
if input.len() < size_of::<u8>() + size_of::<u64>() {
return Err(ProgramError::InvalidAccountData);
}
#[allow(clippy::cast_ptr_alignment)]
let amount: &u64 = unsafe { &*(&input[1] as *const u8 as *const u64) };
Self::Transfer(*amount)
}
3 => {
if input.len() < size_of::<u8>() + size_of::<u64>() {
return Err(ProgramError::InvalidAccountData);
}
#[allow(clippy::cast_ptr_alignment)]
let amount: &u64 = unsafe { &*(&input[1] as *const u8 as *const u64) };
Self::Approve(*amount)
}
4 => Self::SetOwner,
5 => {
if input.len() < size_of::<u8>() + size_of::<u64>() {
return Err(ProgramError::InvalidAccountData);
}
#[allow(clippy::cast_ptr_alignment)]
let amount: &u64 = unsafe { &*(&input[1] as *const u8 as *const u64) };
Self::MintTo(*amount)
}
6 => {
if input.len() < size_of::<u8>() + size_of::<u64>() {
return Err(ProgramError::InvalidAccountData);
}
#[allow(clippy::cast_ptr_alignment)]
let amount: &u64 = unsafe { &*(&input[1] as *const u8 as *const u64) };
Self::Burn(*amount)
Expand Down
16 changes: 14 additions & 2 deletions token/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,17 +468,23 @@ impl<'a> State {

/// Deserializes a byte buffer into a Token Program [State](struct.State.html)
pub fn deserialize(input: &'a [u8]) -> Result<Self, ProgramError> {
if input.len() < size_of::<State>() {
if input.len() < size_of::<u8>() {
return Err(ProgramError::InvalidAccountData);
}
Ok(match input[0] {
0 => Self::Unallocated,
1 => {
if input.len() < size_of::<u8>() + size_of::<Token>() {
return Err(ProgramError::InvalidAccountData);
}
#[allow(clippy::cast_ptr_alignment)]
let token: &Token = unsafe { &*(&input[1] as *const u8 as *const Token) };
Self::Token(*token)
}
2 => {
if input.len() < size_of::<u8>() + size_of::<Account>() {
return Err(ProgramError::InvalidAccountData);
}
#[allow(clippy::cast_ptr_alignment)]
let account: &Account = unsafe { &*(&input[1] as *const u8 as *const Account) };
Self::Account(*account)
Expand All @@ -490,18 +496,24 @@ impl<'a> State {

/// Serializes Token Program [State](struct.State.html) into a byte buffer
pub fn serialize(self: &Self, output: &mut [u8]) -> ProgramResult {
if output.len() < size_of::<State>() {
if output.len() < size_of::<u8>() {
return Err(ProgramError::InvalidAccountData);
}
match self {
Self::Unallocated => output[0] = 0,
Self::Token(token) => {
if output.len() < size_of::<u8>() + size_of::<Token>() {
return Err(ProgramError::InvalidAccountData);
}
output[0] = 1;
#[allow(clippy::cast_ptr_alignment)]
let value = unsafe { &mut *(&mut output[1] as *mut u8 as *mut Token) };
*value = *token;
}
Self::Account(account) => {
if output.len() < size_of::<u8>() + size_of::<Account>() {
return Err(ProgramError::InvalidAccountData);
}
output[0] = 2;
#[allow(clippy::cast_ptr_alignment)]
let value = unsafe { &mut *(&mut output[1] as *mut u8 as *mut Account) };
Expand Down