Skip to content
Draft
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
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion js/compressed-token/tests/e2e/mint-to.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,10 @@ describe('mintTo', () => {
await assertMintTo(rpc, mint, amount, bob.publicKey);

/// wrong authority
/// is not checked in cToken program, so it throws invalid owner inside spl token program.
await expect(
mintTo(rpc, payer, mint, bob.publicKey, Keypair.generate(), amount),
).rejects.toThrowError(/custom program error: 0x1782/);
).rejects.toThrowError(/custom program error: 0x4/);

/// with output state merkle tree defined
await mintTo(
Expand Down
75 changes: 74 additions & 1 deletion program-libs/compressed-account/src/pubkey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use borsh::{BorshDeserialize, BorshSerialize};
use bytemuck::{Pod, Zeroable};
use light_zero_copy::{borsh::Deserialize, errors::ZeroCopyError};
use solana_program::pubkey;
use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout, Ref, Unaligned};
use zerocopy::{little_endian::U64, FromBytes, Immutable, IntoBytes, KnownLayout, Ref, Unaligned};
#[cfg(feature = "bytemuck-des")]
#[derive(
Pod,
Expand Down Expand Up @@ -54,6 +54,12 @@ impl Pubkey {
}
}

impl AsRef<Pubkey> for Pubkey {
fn as_ref(&self) -> &Self {
self
}
}

impl<'a> Deserialize<'a> for Pubkey {
type Output = Ref<&'a [u8], Pubkey>;

Expand Down Expand Up @@ -113,6 +119,7 @@ impl From<&anchor_lang::prelude::Pubkey> for Pubkey {
Self(pubkey.to_bytes())
}
}

impl Pubkey {
pub fn new_unique() -> Self {
Self(pubkey::Pubkey::new_unique().to_bytes())
Expand All @@ -122,3 +129,69 @@ impl Pubkey {
self.0
}
}

pub trait PubkeyTrait {
fn trait_to_bytes(&self) -> [u8; 32];
#[cfg(feature = "anchor")]
fn to_anchor_pubkey(&self) -> anchor_lang::prelude::Pubkey;
}

impl PubkeyTrait for Pubkey {
fn trait_to_bytes(&self) -> [u8; 32] {
self.to_bytes()
}
#[cfg(feature = "anchor")]
fn to_anchor_pubkey(&self) -> anchor_lang::prelude::Pubkey {
self.into()
}
}

#[cfg(feature = "anchor")]
impl PubkeyTrait for anchor_lang::prelude::Pubkey {
fn trait_to_bytes(&self) -> [u8; 32] {
self.to_bytes()
}

#[cfg(feature = "anchor")]
fn to_anchor_pubkey(&self) -> Self {
*self
}
}

#[cfg(not(feature = "anchor"))]
impl PubkeyTrait for solana_program::pubkey::Pubkey {
fn trait_to_bytes(&self) -> [u8; 32] {
self.to_bytes()
}
}

use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};
pub trait ZeroCopyNumTrait:
Add
+ Sub
+ AddAssign
+ SubAssign
+ Div
+ DivAssign
+ Mul
+ MulAssign
+ std::marker::Sized
+ From<u64>
+ Into<u64>
+ Copy
+ std::convert::TryFrom<u64>
{
fn to_bytes_le(&self) -> [u8; 8];
}

impl ZeroCopyNumTrait for u64 {
fn to_bytes_le(&self) -> [u8; 8] {
self.to_le_bytes()
}
}

impl ZeroCopyNumTrait for U64 {
fn to_bytes_le(&self) -> [u8; 8] {
self.to_bytes()
}
}
6 changes: 5 additions & 1 deletion program-libs/zero-copy/src/borsh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ use core::{
};
use std::vec::Vec;

use zerocopy::{little_endian::U32, FromBytes, Immutable, KnownLayout, Ref};
use zerocopy::{
little_endian::{U16, U32, U64},
FromBytes, Immutable, KnownLayout, Ref,
};

use crate::errors::ZeroCopyError;

Expand Down Expand Up @@ -77,6 +80,7 @@ macro_rules! impl_deserialize_for_primitive {
}

impl_deserialize_for_primitive!(u16, i16, u32, i32, u64, i64);
impl_deserialize_for_primitive!(U16, U32, U64);

impl<'a, T: Deserialize<'a>> Deserialize<'a> for Vec<T> {
type Output = Vec<T::Output>;
Expand Down
3 changes: 2 additions & 1 deletion program-tests/compressed-token-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ light-compressed-token = { workspace = true }
light-system-program = { workspace = true }
account-compression = { workspace = true }
light-compressed-account = { workspace = true }

light-registry = { workspace = true }
light-batched-merkle-tree = { workspace = true }
[target.'cfg(not(target_os = "solana"))'.dependencies]
solana-sdk = { workspace = true }

Expand Down
Loading