Skip to content
This repository has been archived by the owner on Sep 7, 2023. It is now read-only.

Commit

Permalink
Integrate gpl session (#12)
Browse files Browse the repository at this point in the history
* add: gpl_session for connection

* add: gpl_session for reaction

* fix: anchor tests

* cleanup is_valid

* add: reaction session

* add: post session

* fix: add session_token

* add: test cases for comments
  • Loading branch information
abishekk92 authored Mar 27, 2023
1 parent 489f797 commit 2602735
Show file tree
Hide file tree
Showing 17 changed files with 588 additions and 66 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"build:all": "anchor build"
},
"dependencies": {
"@project-serum/anchor": "^0.25.0"
"@project-serum/anchor": "^0.26.0"
},
"devDependencies": {
"@solana/spl-account-compression": "^0.1.5",
Expand Down
3 changes: 2 additions & 1 deletion programs/gpl_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ cpi = ["no-entrypoint"]
default = []

[dependencies]
anchor-lang = "0.26.0"
anchor-lang = { version = "0.26.0" }
gpl_session = { version = "0.1.0", path = "../gpl_session", features = ["no-entrypoint"] }
solana-security-txt = "1.1.0"
strum = "0.24.1"
strum_macros = "0.24.3"
42 changes: 39 additions & 3 deletions programs/gpl_core/src/instructions/connection.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::state::{Connection, Profile, User};
use anchor_lang::prelude::*;
use gpl_session::program::GplSession;
use gpl_session::SessionToken;
use std::convert::AsRef;

use crate::constants::*;
Expand Down Expand Up @@ -47,9 +49,24 @@ pub struct CreateConnection<'info> {
user.random_hash.as_ref(),
],
bump,
has_one = authority
// Better implemented as a function
constraint = (user.authority == authority.key() || user.authority == session_token.as_ref().unwrap().authority.key()),
)]
pub user: Account<'info, User>,

#[account(
seeds = [
SessionToken::SEED_PREFIX.as_bytes(),
session_token.target_program.key().as_ref(),
session_token.session_signer.key().as_ref(),
session_token.authority.key().as_ref()
],
seeds::program = GplSession::id(),
bump,
constraint = session_token.is_valid()?,
)]
pub session_token: Option<Account<'info, SessionToken>>,

#[account(mut)]
pub authority: Signer<'info>,
// The system program
Expand Down Expand Up @@ -94,7 +111,7 @@ pub struct DeleteConnection<'info> {
bump,
has_one = from_profile,
has_one = to_profile,
close = authority,
close = refund_receiver,
)]
pub connection: Account<'info, Connection>,
#[account(
Expand Down Expand Up @@ -122,11 +139,30 @@ pub struct DeleteConnection<'info> {
user.random_hash.as_ref(),
],
bump,
has_one = authority
constraint = (user.authority == authority.key() || user.authority == session_token.as_ref().unwrap().authority.key()),
)]
pub user: Account<'info, User>,
#[account(
seeds = [
SessionToken::SEED_PREFIX.as_bytes(),
session_token.target_program.key().as_ref(),
session_token.session_signer.key().as_ref(),
session_token.authority.key().as_ref()
],
seeds::program = GplSession::id(),
bump,
constraint = session_token.is_valid()?,
)]
pub session_token: Option<Account<'info, SessionToken>>,

#[account(mut)]
pub authority: Signer<'info>,

#[account(mut, constraint = refund_receiver.key() == user.authority)]
pub refund_receiver: SystemAccount<'info>,

// The system program
pub system_program: Program<'info, System>,
}

// Handler to delete a Connection account
Expand Down
70 changes: 65 additions & 5 deletions programs/gpl_core/src/instructions/post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ use std::convert::AsRef;

use crate::constants::*;

use gpl_session::program::GplSession;
use gpl_session::SessionToken;

// Create Post
#[derive(Accounts)]
#[instruction(metadata_uri: String, random_hash: [u8;32])]
Expand Down Expand Up @@ -39,9 +42,24 @@ pub struct CreatePost<'info> {
user.random_hash.as_ref(),
],
bump,
has_one = authority,
// Better implemented as a function
constraint = (user.authority == authority.key() || user.authority == session_token.as_ref().unwrap().authority.key()),
)]
pub user: Account<'info, User>,

#[account(
seeds = [
SessionToken::SEED_PREFIX.as_bytes(),
session_token.target_program.key().as_ref(),
session_token.session_signer.key().as_ref(),
session_token.authority.key().as_ref()
],
seeds::program = GplSession::id(),
bump,
constraint = session_token.is_valid()?,
)]
pub session_token: Option<Account<'info, SessionToken>>,

#[account(mut)]
pub authority: Signer<'info>,
// The system program
Expand Down Expand Up @@ -104,9 +122,22 @@ pub struct UpdatePost<'info> {
user.random_hash.as_ref(),
],
bump,
has_one = authority,
// Better implemented as a function
constraint = (user.authority == authority.key() || user.authority == session_token.as_ref().unwrap().authority.key()),
)]
pub user: Account<'info, User>,
#[account(
seeds = [
SessionToken::SEED_PREFIX.as_bytes(),
session_token.target_program.key().as_ref(),
session_token.session_signer.key().as_ref(),
session_token.authority.key().as_ref()
],
seeds::program = GplSession::id(),
bump,
constraint = session_token.is_valid()?,
)]
pub session_token: Option<Account<'info, SessionToken>>,
#[account(mut)]
pub authority: Signer<'info>,
pub system_program: Program<'info, System>,
Expand Down Expand Up @@ -161,7 +192,8 @@ pub struct CreateComment<'info> {
user.random_hash.as_ref(),
],
bump,
has_one = authority,
// Better implemented as a function
constraint = (user.authority == authority.key() || user.authority == session_token.as_ref().unwrap().authority.key()),
)]
pub user: Account<'info, User>,
#[account(
Expand All @@ -172,6 +204,18 @@ pub struct CreateComment<'info> {
bump,
)]
pub reply_to: Account<'info, Post>,
#[account(
seeds = [
SessionToken::SEED_PREFIX.as_bytes(),
session_token.target_program.key().as_ref(),
session_token.session_signer.key().as_ref(),
session_token.authority.key().as_ref()
],
seeds::program = GplSession::id(),
bump,
constraint = session_token.is_valid()?,
)]
pub session_token: Option<Account<'info, SessionToken>>,
#[account(mut)]
pub authority: Signer<'info>,
// The system program
Expand Down Expand Up @@ -217,7 +261,7 @@ pub struct DeletePost<'info> {
],
bump,
has_one = profile,
close = authority,
close = refund_receiver,
)]
pub post: Account<'info, Post>,
#[account(
Expand All @@ -236,11 +280,27 @@ pub struct DeletePost<'info> {
user.random_hash.as_ref(),
],
bump,
has_one = authority,
// Better implemented as a function
constraint = (user.authority == authority.key() || user.authority == session_token.as_ref().unwrap().authority.key()),
)]
pub user: Account<'info, User>,

#[account(
seeds = [
SessionToken::SEED_PREFIX.as_bytes(),
session_token.target_program.key().as_ref(),
session_token.session_signer.key().as_ref(),
session_token.authority.key().as_ref()
],
seeds::program = GplSession::id(),
bump,
constraint = session_token.is_valid()?,
)]
pub session_token: Option<Account<'info, SessionToken>>,
#[account(mut)]
pub authority: Signer<'info>,
#[account(mut, constraint = refund_receiver.key() == user.authority)]
pub refund_receiver: SystemAccount<'info>,
pub system_program: Program<'info, System>,
}

Expand Down
43 changes: 40 additions & 3 deletions programs/gpl_core/src/instructions/reaction.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use crate::state::{Post, Profile, Reaction, ReactionType, User};
use anchor_lang::prelude::*;
use gpl_session::program::GplSession;
use gpl_session::SessionToken;
use std::convert::AsRef;
use std::str::FromStr;

use crate::constants::*;
use crate::events::{ReactionDeleted, ReactionNew};

// Create a reaction to a post from a profile
#[derive(Accounts)]
#[instruction(reaction_type: String)]
Expand Down Expand Up @@ -47,11 +50,27 @@ pub struct CreateReaction<'info> {
user.random_hash.as_ref(),
],
bump,
has_one = authority,
// Better implemented as a function
constraint = (user.authority == authority.key() || user.authority == session_token.as_ref().unwrap().authority.key()),
)]
pub user: Account<'info, User>,

#[account(
seeds = [
SessionToken::SEED_PREFIX.as_bytes(),
session_token.target_program.key().as_ref(),
session_token.session_signer.key().as_ref(),
session_token.authority.key().as_ref()
],
seeds::program = GplSession::id(),
bump,
constraint = session_token.is_valid()?,
)]
pub session_token: Option<Account<'info, SessionToken>>,

#[account(mut)]
pub authority: Signer<'info>,

// The system program
pub system_program: Program<'info, System>,
}
Expand Down Expand Up @@ -90,7 +109,7 @@ pub struct DeleteReaction<'info> {
bump,
has_one = to_post,
has_one = from_profile,
close = authority,
close = refund_receiver,
)]
pub reaction: Account<'info, Reaction>,
#[account(
Expand All @@ -117,11 +136,29 @@ pub struct DeleteReaction<'info> {
user.random_hash.as_ref(),
],
bump,
has_one = authority,
// Better implemented as a function
constraint = (user.authority == authority.key() || user.authority == session_token.as_ref().unwrap().authority.key()),
)]
pub user: Account<'info, User>,

#[account(
seeds = [
SessionToken::SEED_PREFIX.as_bytes(),
session_token.target_program.key().as_ref(),
session_token.session_signer.key().as_ref(),
session_token.authority.key().as_ref()
],
seeds::program = GplSession::id(),
bump,
constraint = session_token.is_valid()?,
)]
pub session_token: Option<Account<'info, SessionToken>>,
#[account(mut)]
pub authority: Signer<'info>,

#[account(mut, constraint = refund_receiver.key() == user.authority)]
pub refund_receiver: SystemAccount<'info>,

// The system program
pub system_program: Program<'info, System>,
}
Expand Down
5 changes: 4 additions & 1 deletion programs/gpl_session/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use anchor_lang::prelude::*;
use anchor_lang::solana_program::native_token::LAMPORTS_PER_SOL;
use anchor_lang::system_program;

declare_id!("3ao63wcSRNa76bncC2M3KupNtXBFiDyNbgK52VG7dLaE");
Expand Down Expand Up @@ -43,6 +44,7 @@ pub struct CreateSessionToken<'info> {
)]
pub session_token: Account<'info, SessionToken>,

#[account(mut)]
pub session_signer: Signer<'info>,
#[account(mut)]
pub authority: Signer<'info>,
Expand Down Expand Up @@ -85,7 +87,7 @@ pub fn create_session_token_handler(
to: ctx.accounts.session_signer.to_account_info(),
},
),
100000,
LAMPORTS_PER_SOL.checked_div(100).unwrap(),
)?;
}

Expand Down Expand Up @@ -130,6 +132,7 @@ pub fn revoke_session_token_handler(_: Context<RevokeSessionToken>) -> Result<()

// SessionToken Account
#[account]
#[derive(Copy)]
pub struct SessionToken {
pub authority: Pubkey,
pub target_program: Pubkey,
Expand Down
1 change: 1 addition & 0 deletions tests/gpl_compression/reaction.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ describe("Reaction Compression", async () => {
user: userPDA,
profile: profilePDA,
authority: payer.publicKey,
sessionToken: null,
});
const postPubKeys = await post.pubkeys();
postPDA = postPubKeys.post as anchor.web3.PublicKey;
Expand Down
Loading

0 comments on commit 2602735

Please sign in to comment.