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

Tx delegate #1134

Merged
merged 14 commits into from
Nov 2, 2023
2 changes: 1 addition & 1 deletion Cargo.lock

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

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as anchor from '@project-serum/anchor'
import * as anchor from '@coral-xyz/anchor'
import { findOrCreateAssociatedTokenAccount } from './web3'

import { decodeNonEncryptedByteArray } from './encrypt'
Expand Down
2 changes: 1 addition & 1 deletion programs/nina/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "nina"
version = "0.2.16"
version = "0.3.0"
description = "Nina - A self-publishing protocol"
edition = "2018"

Expand Down
28 changes: 27 additions & 1 deletion programs/nina/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,31 @@ pub enum ErrorCode {
#[msg("Subscription Delegated Payer Mismatch")]
SubscriptionDelegatedPayerMismatch,
#[msg("Release Init Delegated Payer Mismatch")]
ReleaseInitDelegatedPayerMismatch
ReleaseInitDelegatedPayerMismatch,
#[msg("Hub Add Collaborator Delegated Payer Mismatch")]
HubAddCollaboratorDelegatedPayerMismatch,
#[msg("Hub Remove Collaborator Delegated Payer Mismatch")]
HubRemoveCollaboratorDelegatedPayerMismatch,
#[msg("Hub Add Release Delegated Payer Mismatch")]
HubAddReleaseDelegatedPayerMismatch,
#[msg("HubContentToggleVisibility Delegated Payer Mismatch")]
HubContentToggleVisibilityDelegatedPayerMismatch,
#[msg("Hub Withdraw Delegated Payer Mismatch")]
HubWithdrawDelegatedPayerMismatch,
#[msg("Release Revenue Share Collect Delegate Payer Mismatch")]
ReleaseRevenueShareCollectDelegatePayerMismatch,
#[msg("Hub Update Collaborator Permissions Delegated Payer Mismatch")]
HubUpdateCollaboratorPermissionsDelegatePayerMismatch,
#[msg("Hub Update Config Delegated Payer Mismatch")]
HubUpdateConfigDelegatePayerMismatch,
#[msg("Release Close Edition Delegated Payer Mismatch")]
ReleaseCloseEditionDelegatePayerMismatch,
#[msg("Release Revenue Share Transfer Delegate Payer Mismatch")]
ReleaseRevenueShareTransferDelegatePayerMismatch,
#[msg("Release Update Metadata Delegated Payer Mismatch")]
ReleaseUpdateMetadataDelegatePayerMismatch,
#[msg("Post Update Via Hub Delegated Payer Mismatch")]
PostUpdateViaHubPostDelegatePayerMismatch,
#[msg("Post Init Via Hub Delegated Payer Mismatch")]
PostInitViaHubDelegatePayerMismatch
}
9 changes: 9 additions & 0 deletions programs/nina/src/instructions/hub_add_collaborator.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use anchor_lang::prelude::*;

use crate::state::*;
use crate::utils::{file_service_account};
use crate::errors::ErrorCode;

#[derive(Accounts)]
Expand All @@ -11,6 +12,8 @@ use crate::errors::ErrorCode;
hub_handle: String,
)]
pub struct HubAddCollaborator<'info> {
#[account(mut)]
pub payer: Signer<'info>,
#[account(mut)]
pub authority: Signer<'info>,
#[account(
Expand Down Expand Up @@ -44,6 +47,12 @@ pub fn handler (
allowance: i8,
_hub_handle: String,
) -> Result<()> {
if ctx.accounts.payer.key() != ctx.accounts.authority.key() {
if ctx.accounts.payer.key() != file_service_account::ID {
return Err(ErrorCode::HubAddCollaboratorDelegatedPayerMismatch.into());
}
}

let authority_hub_collaborator = &ctx.accounts.authority_hub_collaborator;

if !authority_hub_collaborator.can_add_collaborator {
Expand Down
10 changes: 10 additions & 0 deletions programs/nina/src/instructions/hub_add_release.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
use anchor_lang::prelude::*;
use crate::state::*;
use crate::utils::{file_service_account};
use crate::errors::ErrorCode;

#[derive(Accounts)]
#[instruction(hub_handle: String)]
pub struct HubAddRelease<'info> {
#[account(mut)]
pub payer: Signer<'info>,
#[account(mut)]
pub authority: Signer<'info>,
#[account(
Expand Down Expand Up @@ -44,6 +48,12 @@ pub fn handler (
ctx: Context<HubAddRelease>,
_hub_handle: String,
) -> Result<()> {
if ctx.accounts.payer.key() != ctx.accounts.authority.key() {
if ctx.accounts.payer.key() != file_service_account::ID {
return Err(ErrorCode::HubAddReleaseDelegatedPayerMismatch.into());
}
}

Hub::hub_collaborator_can_add_or_publish_content(
&mut ctx.accounts.hub_collaborator,
false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ use anchor_lang::prelude::*;

use crate::state::*;
use crate::errors::ErrorCode;
use crate::utils::{file_service_account};

#[derive(Accounts)]
#[instruction(hub_handle: String)]
pub struct HubContentToggleVisibility<'info> {
#[account(mut)]
pub payer: Signer<'info>,
#[account(mut)]
pub authority: Signer<'info>,
#[account(
Expand All @@ -28,6 +31,12 @@ pub fn handler (
ctx: Context<HubContentToggleVisibility>,
_hub_handle: String,
) -> Result<()> {
if ctx.accounts.payer.key() != ctx.accounts.authority.key() {
if ctx.accounts.payer.key() != file_service_account::ID {
return Err(ErrorCode::HubContentToggleVisibilityDelegatedPayerMismatch.into());
}
}

let hub = ctx.accounts.hub.load()?;
let hub_content = &mut ctx.accounts.hub_content;

Expand Down
9 changes: 9 additions & 0 deletions programs/nina/src/instructions/hub_remove_collaborator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ use anchor_lang::prelude::*;

use crate::state::*;
use crate::errors::ErrorCode;
use crate::utils::{file_service_account};

#[derive(Accounts)]
#[instruction(hub_handle: String)]
pub struct HubRemoveCollaborator<'info> {
#[account(mut)]
pub payer: Signer<'info>,
#[account(mut)]
pub authority: Signer<'info>,
#[account(
Expand All @@ -32,6 +35,12 @@ pub fn handler (
ctx: Context<HubRemoveCollaborator>,
_hub_handle: String,
) -> Result<()> {
if ctx.accounts.payer.key() != ctx.accounts.authority.key() {
if ctx.accounts.payer.key() != file_service_account::ID {
return Err(ErrorCode::HubRemoveCollaboratorDelegatedPayerMismatch.into());
}
}

let hub = ctx.accounts.hub.load()?;

// Only authority of hub and collaborator in the HubCollaborator account can remove the account
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use anchor_lang::prelude::*;

use crate::state::*;
use crate::errors::ErrorCode;
use crate::utils::{file_service_account};

#[derive(Accounts)]
#[instruction(
Expand All @@ -11,6 +12,8 @@ use crate::errors::ErrorCode;
hub_handle: String,
)]
pub struct HubUpdateCollaboratorPermissions<'info> {
#[account(mut)]
pub payer: Signer<'info>,
#[account(mut)]
pub authority: Signer<'info>,
#[account(
Expand Down Expand Up @@ -40,6 +43,12 @@ pub fn handler (
allowance: i8,
_hub_handle: String,
) -> Result<()> {
if ctx.accounts.payer.key() != ctx.accounts.authority.key() {
if ctx.accounts.payer.key() != file_service_account::ID {
return Err(ErrorCode::HubUpdateCollaboratorPermissionsDelegatePayerMismatch.into());
}
}

if ctx.accounts.authority.key() != ctx.accounts.hub.load()?.authority {
return Err(error!(ErrorCode::HubCollaboratorCannotUpdateHubCollaboratorUnauthorized))
}
Expand Down
10 changes: 10 additions & 0 deletions programs/nina/src/instructions/hub_update_config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use anchor_lang::prelude::*;
use crate::state::*;
use crate::utils::{file_service_account};
use crate::errors::ErrorCode;

#[derive(Accounts)]
#[instruction(
Expand All @@ -9,6 +11,8 @@ use crate::state::*;
_referral_fee: u64,
)]
pub struct HubUpdateConfig<'info> {
#[account(mut)]
pub payer: Signer<'info>,
#[account(mut)]
pub authority: Signer<'info>,
#[account(
Expand All @@ -27,6 +31,12 @@ pub fn handler (
publish_fee: u64,
referral_fee: u64,
) -> Result<()> {
if ctx.accounts.payer.key() != ctx.accounts.authority.key() {
if ctx.accounts.payer.key() != file_service_account::ID {
return Err(ErrorCode::HubUpdateConfigDelegatePayerMismatch.into());
}
}

Hub::check_hub_fees(
publish_fee,
referral_fee
Expand Down
9 changes: 9 additions & 0 deletions programs/nina/src/instructions/hub_withdraw.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use anchor_lang::prelude::*;
use anchor_spl::token::{self, TokenAccount, Token, Transfer, Mint};

use crate::utils::{file_service_account};
use crate::state::*;
use crate::errors::ErrorCode;

Expand All @@ -10,6 +11,8 @@ use crate::errors::ErrorCode;
hub_handle: String
)]
pub struct HubWithdraw<'info> {
#[account(mut)]
pub payer: Signer<'info>,
#[account(mut)]
pub authority: Signer<'info>,
#[account(
Expand Down Expand Up @@ -47,6 +50,12 @@ pub fn handler(
amount: u64,
_hub_handle: String,
) -> Result<()> {
if ctx.accounts.payer.key() != ctx.accounts.authority.key() {
if ctx.accounts.payer.key() != file_service_account::ID {
return Err(ErrorCode::HubWithdrawDelegatedPayerMismatch.into());
}
}

if ctx.accounts.withdraw_target.amount < amount {
return Err(error!(ErrorCode::HubWithdrawAmountTooHigh));
}
Expand Down
27 changes: 17 additions & 10 deletions programs/nina/src/instructions/post_init_via_hub.rs
Original file line number Diff line number Diff line change
@@ -1,41 +1,39 @@
use anchor_lang::prelude::*;
use crate::state::*;
use crate::utils::{file_service_account};
use crate::errors::ErrorCode;

#[derive(Accounts)]
#[instruction(
hub_handle: String,
_hub_handle: String,
slug: String,
_uri: String,
)]
pub struct PostInitViaHub<'info> {
#[account(mut)]
pub author: Signer<'info>,
#[account(
seeds = [b"nina-hub".as_ref(), hub_handle.as_bytes()],
bump,
)]
pub payer: Signer<'info>,
pub hub: AccountLoader<'info, Hub>,
#[account(
init,
seeds = [b"nina-post".as_ref(), hub.key().as_ref(), slug.as_ref()],
bump,
payer = author,
payer = payer,
space = 328
)]
pub post: AccountLoader<'info, Post>,
#[account(
init,
seeds = [b"nina-hub-post".as_ref(), hub.key().as_ref(), post.key().as_ref()],
bump,
payer = author,
payer = payer,
space = 244
)]
pub hub_post: AccountLoader<'info, HubPost>,
#[account(
init,
seeds = [b"nina-hub-content".as_ref(), hub.key().as_ref(), post.key().as_ref()],
bump,
payer = author,
payer = payer,
space = 153
)]
pub hub_content: Account<'info, HubContent>,
Expand All @@ -47,6 +45,9 @@ pub struct PostInitViaHub<'info> {
pub hub_collaborator: Account<'info, HubCollaborator>,
pub system_program: Program<'info, System>,
pub rent: Sysvar<'info, Rent>,
/// CHECK: This is safe because we check in the handler that author === payer
/// or that payer is nina operated file-service wallet
pub author: UncheckedAccount<'info>,
}

pub fn handler (
Expand All @@ -55,8 +56,14 @@ pub fn handler (
slug: String,
uri: String,
) -> Result<()> {
if ctx.accounts.payer.key() != ctx.accounts.author.key() {
if ctx.accounts.payer.key() != file_service_account::ID {
return Err(ErrorCode::PostInitViaHubDelegatePayerMismatch.into());
}
}

Post::post_init_helper(
&mut ctx.accounts.author,
ctx.accounts.author.key(),
ctx.accounts.hub.clone(),
&mut ctx.accounts.post,
&mut ctx.accounts.hub_post,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub struct PostInitViaHubWithReferenceRelease<'info> {
#[account(mut)]
pub author: Signer<'info>,
#[account(
seeds = [b"nina-hub".as_ref(), hub_handle.as_bytes()],
seeds = [b"nina-hub".as_ref(), hub_handle.as_ref()],
bump,
)]
pub hub: AccountLoader<'info, Hub>,
Expand Down Expand Up @@ -79,7 +79,7 @@ pub fn handler (
let release = &ctx.accounts.reference_release;

Post::post_init_helper(
&mut ctx.accounts.author,
ctx.accounts.author.key(),
ctx.accounts.hub.clone(),
&mut ctx.accounts.post,
&mut ctx.accounts.hub_post,
Expand Down
14 changes: 12 additions & 2 deletions programs/nina/src/instructions/post_update_via_hub_post.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use anchor_lang::prelude::*;
use crate::state::*;
use crate::utils::{file_service_account};
use crate::errors::ErrorCode;

#[derive(Accounts)]
#[instruction(
Expand All @@ -8,11 +10,13 @@ use crate::state::*;
_uri: String,
)]
pub struct PostUpdateViaHubPost<'info> {
#[account(mut)]
pub payer: Signer<'info>,
///CHECK: This is safe bc we check constraints
#[account(
mut,
constraint = author.key() == post.load()?.author
)]
pub author: Signer<'info>,
pub author: UncheckedAccount<'info>,
#[account(
seeds = [b"nina-hub".as_ref(), hub_handle.as_bytes()],
bump,
Expand Down Expand Up @@ -43,6 +47,12 @@ pub fn handler (
_slug: String,
uri: String,
) -> Result<()> {
if ctx.accounts.payer.key() != ctx.accounts.author.key() {
if ctx.accounts.payer.key() != file_service_account::ID {
return Err(ErrorCode::PostUpdateViaHubPostDelegatePayerMismatch.into());
}
}

let mut post = ctx.accounts.post.load_mut()?;

let mut uri_array = [0u8; 100];
Expand Down
Loading