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

Set authority as optional signer #17

Merged
merged 6 commits into from
Mar 13, 2024
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
2 changes: 1 addition & 1 deletion clients/cli/src/commands/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub fn handle_create(args: CreateArgs) -> Result<()> {

let ix = Create {
asset,
authority,
authority: (authority, false),
owner,
payer: Some(authority),
group: None,
Expand Down
4 changes: 2 additions & 2 deletions clients/js/asset/src/generated/instructions/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export type CreateInstructionAccounts = {
/** Asset account */
asset: Signer;
/** The authority of the asset */
authority?: PublicKey | Pda;
authority?: PublicKey | Pda | Signer;
/** The owner of the asset */
owner?: PublicKey | Pda;
/** Asset account of the group */
Expand Down Expand Up @@ -135,7 +135,7 @@ export function create(

// Default values.
if (!resolvedAccounts.authority.value) {
resolvedAccounts.authority.value = context.identity.publicKey;
resolvedAccounts.authority.value = context.identity;
}
if (!resolvedAccounts.owner.value) {
resolvedAccounts.owner.value = context.identity.publicKey;
Expand Down
94 changes: 92 additions & 2 deletions clients/js/asset/test/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,9 @@ test('it can create a soulbound asset', async (t) => {
});

test('it can create an asset with a group', async (t) => {
// Given a Umi instance.
// Given a Umi instance and an authority signer.
const umi = await createUmi();
const authority = generateSigner(umi);

// And we create a group asset.
const groupAsset = generateSigner(umi);
Expand All @@ -193,6 +194,7 @@ test('it can create an asset with a group', async (t) => {

await create(umi, {
asset: groupAsset,
authority: authority.publicKey,
name: 'Group',
}).sendAndConfirm(umi);

Expand All @@ -207,11 +209,12 @@ test('it can create an asset with a group', async (t) => {
],
});

// When we create an asset with a group.
// When we create an asset with a group with the same authority.
const asset = generateSigner(umi);
await create(umi, {
asset,
payer: umi.identity,
authority,
name: 'Asset',
group: groupAsset.publicKey,
}).sendAndConfirm(umi);
Expand All @@ -234,3 +237,90 @@ test('it can create an asset with a group', async (t) => {
],
});
});

test('it cannot set a group on create with wrong authority', async (t) => {
// Given a Umi instance and an authority signer.
const umi = await createUmi();
const authority = generateSigner(umi);

// And we create a group asset.
const groupAsset = generateSigner(umi);
await initialize(umi, {
asset: groupAsset,
payer: umi.identity,
extension: grouping(10),
}).sendAndConfirm(umi);

await create(umi, {
asset: groupAsset,
authority: authority.publicKey,
name: 'Group',
}).sendAndConfirm(umi);

t.like(await fetchAsset(umi, groupAsset.publicKey), <Asset>{
group: null,
extensions: [
{
type: ExtensionType.Grouping,
size: 0n,
maxSize: 10n,
},
],
});

// When we create an asset with a group using the wrong authority.
const asset = generateSigner(umi);
const promise = create(umi, {
asset,
payer: umi.identity,
name: 'Asset',
group: groupAsset.publicKey,
}).sendAndConfirm(umi);

// Then we expect an error.
await t.throwsAsync(promise, { message: /Invalid authority/ });
});

test('it cannot set a group on create with authority not a signer', async (t) => {
// Given a Umi instance and an authority signer.
const umi = await createUmi();
const authority = generateSigner(umi);

// And we create a group asset.
const groupAsset = generateSigner(umi);
await initialize(umi, {
asset: groupAsset,
payer: umi.identity,
extension: grouping(10),
}).sendAndConfirm(umi);

await create(umi, {
asset: groupAsset,
authority: authority.publicKey,
name: 'Group',
}).sendAndConfirm(umi);

t.like(await fetchAsset(umi, groupAsset.publicKey), <Asset>{
group: null,
extensions: [
{
type: ExtensionType.Grouping,
size: 0n,
maxSize: 10n,
},
],
});

// When we create an asset with a group without the authority as a signer.
const asset = generateSigner(umi);
const promise = create(umi, {
asset,
authority: authority.publicKey,
payer: umi.identity,
name: 'Asset',
group: groupAsset.publicKey,
}).sendAndConfirm(umi);

// Then we expect an error.
await t.throwsAsync(promise, { message: /missing required signature/ });
});
35 changes: 20 additions & 15 deletions clients/rust/asset/src/generated/instructions/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub struct Create {
/// Asset account
pub asset: solana_program::pubkey::Pubkey,
/// The authority of the asset
pub authority: solana_program::pubkey::Pubkey,
pub authority: (solana_program::pubkey::Pubkey, bool),
/// The owner of the asset
pub owner: solana_program::pubkey::Pubkey,
/// Asset account of the group
Expand Down Expand Up @@ -43,8 +43,8 @@ impl Create {
self.asset, true,
));
accounts.push(solana_program::instruction::AccountMeta::new_readonly(
self.authority,
false,
self.authority.0,
self.authority.1,
));
accounts.push(solana_program::instruction::AccountMeta::new_readonly(
self.owner, false,
Expand Down Expand Up @@ -113,15 +113,15 @@ pub struct CreateInstructionArgs {
/// ### Accounts:
///
/// 0. `[writable, signer]` asset
/// 1. `[]` authority
/// 1. `[signer]` authority
/// 2. `[]` owner
/// 3. `[writable, optional]` group
/// 4. `[writable, signer, optional]` payer
/// 5. `[optional]` system_program
#[derive(Default)]
pub struct CreateBuilder {
asset: Option<solana_program::pubkey::Pubkey>,
authority: Option<solana_program::pubkey::Pubkey>,
authority: Option<(solana_program::pubkey::Pubkey, bool)>,
owner: Option<solana_program::pubkey::Pubkey>,
group: Option<solana_program::pubkey::Pubkey>,
payer: Option<solana_program::pubkey::Pubkey>,
Expand All @@ -144,8 +144,12 @@ impl CreateBuilder {
}
/// The authority of the asset
#[inline(always)]
pub fn authority(&mut self, authority: solana_program::pubkey::Pubkey) -> &mut Self {
self.authority = Some(authority);
pub fn authority(
&mut self,
authority: solana_program::pubkey::Pubkey,
as_signer: bool,
) -> &mut Self {
self.authority = Some((authority, as_signer));
self
}
/// The owner of the asset
Expand Down Expand Up @@ -238,7 +242,7 @@ pub struct CreateCpiAccounts<'a, 'b> {
/// Asset account
pub asset: &'b solana_program::account_info::AccountInfo<'a>,
/// The authority of the asset
pub authority: &'b solana_program::account_info::AccountInfo<'a>,
pub authority: (&'b solana_program::account_info::AccountInfo<'a>, bool),
/// The owner of the asset
pub owner: &'b solana_program::account_info::AccountInfo<'a>,
/// Asset account of the group
Expand All @@ -256,7 +260,7 @@ pub struct CreateCpi<'a, 'b> {
/// Asset account
pub asset: &'b solana_program::account_info::AccountInfo<'a>,
/// The authority of the asset
pub authority: &'b solana_program::account_info::AccountInfo<'a>,
pub authority: (&'b solana_program::account_info::AccountInfo<'a>, bool),
/// The owner of the asset
pub owner: &'b solana_program::account_info::AccountInfo<'a>,
/// Asset account of the group
Expand Down Expand Up @@ -325,8 +329,8 @@ impl<'a, 'b> CreateCpi<'a, 'b> {
true,
));
accounts.push(solana_program::instruction::AccountMeta::new_readonly(
*self.authority.key,
false,
*self.authority.0.key,
self.authority.1,
));
accounts.push(solana_program::instruction::AccountMeta::new_readonly(
*self.owner.key,
Expand Down Expand Up @@ -382,7 +386,7 @@ impl<'a, 'b> CreateCpi<'a, 'b> {
let mut account_infos = Vec::with_capacity(6 + 1 + remaining_accounts.len());
account_infos.push(self.__program.clone());
account_infos.push(self.asset.clone());
account_infos.push(self.authority.clone());
account_infos.push(self.authority.0.clone());
account_infos.push(self.owner.clone());
if let Some(group) = self.group {
account_infos.push(group.clone());
Expand Down Expand Up @@ -410,7 +414,7 @@ impl<'a, 'b> CreateCpi<'a, 'b> {
/// ### Accounts:
///
/// 0. `[writable, signer]` asset
/// 1. `[]` authority
/// 1. `[signer]` authority
/// 2. `[]` owner
/// 3. `[writable, optional]` group
/// 4. `[writable, signer, optional]` payer
Expand Down Expand Up @@ -447,8 +451,9 @@ impl<'a, 'b> CreateCpiBuilder<'a, 'b> {
pub fn authority(
&mut self,
authority: &'b solana_program::account_info::AccountInfo<'a>,
as_signer: bool,
) -> &mut Self {
self.instruction.authority = Some(authority);
self.instruction.authority = Some((authority, as_signer));
self
}
/// The owner of the asset
Expand Down Expand Up @@ -580,7 +585,7 @@ impl<'a, 'b> CreateCpiBuilder<'a, 'b> {
struct CreateCpiBuilderInstruction<'a, 'b> {
__program: &'b solana_program::account_info::AccountInfo<'a>,
asset: Option<&'b solana_program::account_info::AccountInfo<'a>>,
authority: Option<&'b solana_program::account_info::AccountInfo<'a>>,
authority: Option<(&'b solana_program::account_info::AccountInfo<'a>, bool)>,
owner: Option<&'b solana_program::account_info::AccountInfo<'a>>,
group: Option<&'b solana_program::account_info::AccountInfo<'a>>,
payer: Option<&'b solana_program::account_info::AccountInfo<'a>>,
Expand Down
2 changes: 1 addition & 1 deletion clients/rust/asset/src/mint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ pub fn mint(args: MintIxArgs) -> Result<Vec<Instruction>, MintError> {
instructions.push(
Create {
asset: args.accounts.asset,
authority: args.accounts.owner,
authority: (args.accounts.owner, false),
owner: args.accounts.owner,
payer: Some(payer),
group: None,
Expand Down
4 changes: 2 additions & 2 deletions clients/rust/asset/tests/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ mod create {

let ix = CreateBuilder::new()
.asset(asset.pubkey())
.authority(context.payer.pubkey())
.authority(context.payer.pubkey(), false)
.owner(context.payer.pubkey())
.payer(Some(context.payer.pubkey()))
.system_program(Some(system_program::id()))
Expand Down Expand Up @@ -108,7 +108,7 @@ mod create {

let ix = CreateBuilder::new()
.asset(asset.pubkey())
.authority(context.payer.pubkey())
.authority(context.payer.pubkey(), false)
.owner(context.payer.pubkey())
.payer(Some(context.payer.pubkey()))
.system_program(Some(system_program::id()))
Expand Down
1 change: 1 addition & 0 deletions idls/asset_program.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
"name": "authority",
"isMut": false,
"isSigner": false,
"isOptionalSigner": true,
"docs": [
"The authority of the asset"
]
Expand Down
18 changes: 14 additions & 4 deletions programs/asset/program/src/entrypoint.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use solana_program::{
account_info::AccountInfo, entrypoint, entrypoint::ProgramResult,
program_error::PrintProgramError, pubkey::Pubkey,
account_info::AccountInfo,
entrypoint,
entrypoint::ProgramResult,
program_error::{PrintProgramError, ProgramError},
pubkey::Pubkey,
};

use crate::{error::AssetError, processor};
Expand All @@ -12,9 +15,16 @@ fn process_instruction<'a>(
instruction_data: &[u8],
) -> ProgramResult {
if let Err(error) = processor::process_instruction(program_id, accounts, instruction_data) {
// catch the error so we can print it
error.print::<AssetError>();
match error {
ProgramError::Custom(_) => {
error.print::<AssetError>();
}
_ => {
solana_program::msg!("⛔️ {} ({:?})", &error.to_string(), &error);
}
}
return Err(error);
}

Ok(())
}
2 changes: 1 addition & 1 deletion programs/asset/program/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ pub enum AssetError {

impl PrintProgramError for AssetError {
fn print<E>(&self) {
msg!("⛔️ {}", &self.to_string());
msg!("⛔️ {} ({:?})", &self.to_string(), &self);
}
}

Expand Down
2 changes: 1 addition & 1 deletion programs/asset/program/src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub enum Instruction {

/// Creates a new asset.
#[account(0, signer, writable, name="asset", desc = "Asset account")]
#[account(1, name="authority", desc = "The authority of the asset")]
#[account(1, optional_signer, name="authority", desc = "The authority of the asset")]
#[account(2, name="owner", desc = "The owner of the asset")]
#[account(3, optional, writable, name="group", desc = "Asset account of the group")]
#[account(4, optional, signer, writable, name="payer", desc = "The account paying for the storage fees")]
Expand Down
2 changes: 1 addition & 1 deletion programs/bridge/src/processor/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ pub fn process_create(

CreateCpiBuilder::new(ctx.accounts.nifty_asset_program)
.asset(ctx.accounts.asset)
.authority(ctx.accounts.update_authority)
.authority(ctx.accounts.update_authority, false)
.owner(ctx.accounts.vault)
.payer(Some(ctx.accounts.payer))
.system_program(Some(ctx.accounts.system_program))
Expand Down