Skip to content

Commit

Permalink
Tx delegate (#1134)
Browse files Browse the repository at this point in the history
* PostV2 w/ file-service upload

* fix posts in program

* fix posts in program

* add gpay and apple pay

* cleanup

* format

* merge

* new tx-delegate pattern

* have sdk actions add payer

* lint + format
  • Loading branch information
facing-n authored Nov 2, 2023
1 parent 728017a commit fa72ccc
Show file tree
Hide file tree
Showing 26 changed files with 261 additions and 52 deletions.
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.

4 changes: 2 additions & 2 deletions js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"@emotion/server": "^11.4.0",
"@emotion/styled": "^11.6.0",
"@imgix/js-core": "^3.6.0",
"@magic-ext/solana": "^12.1.0",
"@magic-ext/solana": "^15.3.1",
"@material-ui/core": "^4.11.4",
"@material-ui/icons": "^4.11.2",
"@material-ui/system": "^4.11.3",
Expand All @@ -42,7 +42,7 @@
"env-cmd": "^10.1.0",
"ga-gtag": "^1.1.1",
"glob-parent": "^5.1.2",
"magic-sdk": "^16.1.0",
"magic-sdk": "^19.3.1",
"nanoid": "^3.1.31",
"next": "12.2.0",
"node-fetch": "^2.6.7",
Expand Down
10 changes: 10 additions & 0 deletions js/sdk/src/contexts/Hub/hub.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ const hubContextHelper = ({
const tx = await program.methods
.hubInit(hubParams)
.accounts({
payer: provider.wallet.publicKey,
authority: provider.wallet.publicKey,
hub,
hubSigner,
Expand Down Expand Up @@ -287,6 +288,7 @@ const hubContextHelper = ({
new anchor.BN(referralFee * 10000),
{
accounts: {
payer: provider.wallet.publicKey,
authority: provider.wallet.publicKey,
hub: new anchor.web3.PublicKey(hubPubkey),
},
Expand Down Expand Up @@ -356,6 +358,7 @@ const hubContextHelper = ({
hub.handle,
{
accounts: {
payer: provider.wallet.publicKey,
authority: provider.wallet.publicKey,
authorityHubCollaborator,
hub: hubPubkey,
Expand Down Expand Up @@ -433,6 +436,7 @@ const hubContextHelper = ({
hub.handle,
{
accounts: {
payer: provider.wallet.publicKey,
authority: provider.wallet.publicKey,
authorityHubCollaborator,
hub: hubPubkey,
Expand Down Expand Up @@ -508,6 +512,7 @@ const hubContextHelper = ({

const request = {
accounts: {
payer: provider.wallet.publicKey,
authority: provider.wallet.publicKey,
hub: hubPubkey,
hubRelease,
Expand Down Expand Up @@ -588,6 +593,7 @@ const hubContextHelper = ({

const tx = await program.transaction.hubRemoveCollaborator(hub.handle, {
accounts: {
payer: provider.wallet.publicKey,
authority: provider.wallet.publicKey,
hub: hubPubkey,
hubCollaborator,
Expand Down Expand Up @@ -743,6 +749,7 @@ const hubContextHelper = ({
hub.handle,
{
accounts: {
payer: provider.wallet.publicKey,
authority: provider.wallet.publicKey,
hub: hubPubkey,
hubSigner,
Expand Down Expand Up @@ -827,6 +834,7 @@ const hubContextHelper = ({
const params = [handle, slugHash, uri]
const request = {
accounts: {
payer: provider.wallet.publicKey,
author: provider.wallet.publicKey,
hub: hubPubkey,
post,
Expand Down Expand Up @@ -949,6 +957,7 @@ const hubContextHelper = ({
uri,
{
accounts: {
payer: provider.wallet.publicKey,
author: provider.wallet.publicKey,
hub: hubPubkey,
post,
Expand Down Expand Up @@ -1010,6 +1019,7 @@ const hubContextHelper = ({

const request = {
accounts: {
payer: provider.wallet.publicKey,
authority: provider.wallet.publicKey,
royaltyTokenAccount: release.royaltyTokenAccount,
release: releasePubkey,
Expand Down
3 changes: 3 additions & 0 deletions js/sdk/src/contexts/Release/release.js
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,7 @@ const releaseContextHelper = ({
try {
const tx = await program.transaction.releaseCloseEdition({
accounts: {
payer: provider.wallet.publicKey,
authority: provider.wallet.publicKey,
release: new anchor.web3.PublicKey(releasePubkey),
releaseSigner: release.releaseSigner,
Expand Down Expand Up @@ -967,6 +968,7 @@ const releaseContextHelper = ({

const request = {
accounts: {
payer: provider.wallet.publicKey,
authority: provider.wallet.publicKey,
authorityTokenAccount,
release: new anchor.web3.PublicKey(releasePubkey),
Expand Down Expand Up @@ -1045,6 +1047,7 @@ const releaseContextHelper = ({

const request = {
accounts: {
payer: provider.wallet.publicKey,
authority: provider.wallet.publicKey,
authorityTokenAccount,
release: releasePublicKey,
Expand Down
13 changes: 12 additions & 1 deletion js/sdk/src/contexts/Wallet/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
import { Magic } from 'magic-sdk'
import { SolanaExtension } from '@magic-ext/solana'
import * as anchor from '@coral-xyz/anchor'
import axios from 'axios'

const WalletContext = createContext()
const WalletContextProvider = ({ children }) => {
Expand Down Expand Up @@ -106,7 +107,17 @@ const walletContextHelper = ({
const connectMagicWallet = async (magic) => {
const isLoggedIn = await magic.user.isLoggedIn()
if (isLoggedIn) {
const user = await magic.user.getMetadata()
const user = await magic.user.getInfo()
if (user) {
try {
await axios.post(`${process.env.NINA_IDENTITY_ENDPOINT}/login`, {
issuer: user.issuer,
publicKey: user.publicAddress,
})
} catch (error) {
console.error(error)
}
}
const wallet = {
connected: true,
connecting: false,
Expand Down
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
54 changes: 27 additions & 27 deletions js/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2013,29 +2013,29 @@
resolved "https://registry.yarnpkg.com/@ledgerhq/logs/-/logs-6.10.1.tgz#5bd16082261d7364eabb511c788f00937dac588d"
integrity sha512-z+ILK8Q3y+nfUl43ctCPuR4Y2bIxk/ooCQFwZxhtci1EhAtMDzMAx2W25qx8G1PPL9UUOdnUax19+F0OjXoj4w==

"@magic-ext/solana@^12.1.0":
version "12.2.0"
resolved "https://registry.yarnpkg.com/@magic-ext/solana/-/solana-12.2.0.tgz#4a3faa3d66eed6b36b1779ca76b61c422d89b12d"
integrity sha512-cKennYgQZAzvSUoQ0aRc8Mm524bDpZGBki8F7TyLwJL0FhJeU1I071XwErDORqjCRPARpJ5PUhE6RIMA3/4vhA==

"@magic-sdk/commons@^12.2.0":
version "12.2.0"
resolved "https://registry.yarnpkg.com/@magic-sdk/commons/-/commons-12.2.0.tgz#08a9c153f2e5068c13020b4e41e953b9a76c7cd3"
integrity sha512-wbbPY3D2C7EIYNkIhYu7ApGS4+7gE8YdO0AgKOwY+ShA/6vN3egeXygYMOcl4udC5LHrB8V4VcRLS3Mv96cD8Q==

"@magic-sdk/provider@^16.2.0":
version "16.2.0"
resolved "https://registry.yarnpkg.com/@magic-sdk/provider/-/provider-16.2.0.tgz#d67a46a93f4d625f1055c93452e13c623e43827a"
integrity sha512-mZsHVqkV5NT2F+Rx2vS+lSVd1SwYlBp9YG3A69sv2a34uza4nCiL+4R1Xxd398u0aEpuzDqn+PyEk0pTVnUL+w==
dependencies:
"@magic-sdk/types" "^14.2.0"
"@magic-ext/solana@^15.3.1":
version "15.4.0"
resolved "https://registry.yarnpkg.com/@magic-ext/solana/-/solana-15.4.0.tgz#0c431f4cb0f374e77ec143c3dad2db01f00d3692"
integrity sha512-nMpExsGc9QuTk/jv4rL5XP41BSvTncSe7xjOg14N/b8vvtjmgoMPXZE8SM9t4Ms9YNaDMQApyaZLyMFWTtXMlQ==

"@magic-sdk/commons@^15.4.0":
version "15.4.0"
resolved "https://registry.yarnpkg.com/@magic-sdk/commons/-/commons-15.4.0.tgz#335cc5c8602bca0fd6fdaee29bde0cb120fc6533"
integrity sha512-FawPr08tuAs0r0bOcfbmQuI3b0VGRBVLbh1+5DWJ0acdlIBv/4botqUGtfj22P6VIdOP+OawnlagcUac6oBi5Q==

"@magic-sdk/provider@^19.4.0":
version "19.4.0"
resolved "https://registry.yarnpkg.com/@magic-sdk/provider/-/provider-19.4.0.tgz#600811170d75fa84e506a7f2243af850d2ca4048"
integrity sha512-A6vw0jnNbM5jrozel/da3W8zBEtqxTcyYc5/RKB7H5x03dPtvd4V3nQzvJCYq7d7ZudW8/5R/qg4DVkJILAzeg==
dependencies:
"@magic-sdk/types" "^16.4.0"
eventemitter3 "^4.0.4"
web3-core "1.5.2"

"@magic-sdk/types@^14.2.0":
version "14.2.0"
resolved "https://registry.yarnpkg.com/@magic-sdk/types/-/types-14.2.0.tgz#5eb2d611574e1e94875d72f8cf51be3529895d22"
integrity sha512-6zts9JLdNLj03xCf4GNjpihkSxAoC6m+UzGpARHUBecSkwp6ixrAioMhIdy+vjXF4+f7Ntb2tWcrMExIfA7hFg==
"@magic-sdk/types@^16.4.0":
version "16.4.0"
resolved "https://registry.yarnpkg.com/@magic-sdk/types/-/types-16.4.0.tgz#23e097d8bf2277cbed1b88065620b742493f3376"
integrity sha512-KHBfp0EYHxYBUJL/n//N3LF1oZ1qe1My2fDKTe6QiOSjx67xycDr8EmSqa+gbuIroS1uMTYzKlS1K5+RDiRhBQ==

"@mapbox/hast-util-table-cell-style@^0.2.0":
version "0.2.0"
Expand Down Expand Up @@ -9277,14 +9277,14 @@ luxon@^2.3.0:
resolved "https://registry.yarnpkg.com/luxon/-/luxon-2.5.2.tgz#17ed497f0277e72d58a4756d6a9abee4681457b6"
integrity sha512-Yg7/RDp4nedqmLgyH0LwgGRvMEKVzKbUdkBYyCosbHgJ+kaOUx0qzSiSatVc3DFygnirTPYnMM2P5dg2uH1WvA==

magic-sdk@^16.1.0:
version "16.2.0"
resolved "https://registry.yarnpkg.com/magic-sdk/-/magic-sdk-16.2.0.tgz#d4f30564f76539882ef0374434c3396f5a5f7860"
integrity sha512-ySnnMRGZ3Yj6QAiml4SYIdrG3GNI7CP90s+hjbUGSJ3oJlv3RiDzmwO8jgl4K7TcooZiR5pvhyNbuUhEwXnfyw==
magic-sdk@^19.3.1:
version "19.4.0"
resolved "https://registry.yarnpkg.com/magic-sdk/-/magic-sdk-19.4.0.tgz#9b126137cc370004bc7bce9589134fbdeee1eab3"
integrity sha512-i9K8OMH1HpNvwtrCOlMhRiwViGN7sWJPRxs9VNfSJAyDEn6xwVjbY2VgyHt9F/wUJUQAY1f94H+N37sT7dU6pw==
dependencies:
"@magic-sdk/commons" "^12.2.0"
"@magic-sdk/provider" "^16.2.0"
"@magic-sdk/types" "^14.2.0"
"@magic-sdk/commons" "^15.4.0"
"@magic-sdk/provider" "^19.4.0"
"@magic-sdk/types" "^16.4.0"
localforage "^1.7.4"

magic-string@^0.25.0, magic-string@^0.25.2, magic-string@^0.25.7:
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
Loading

3 comments on commit fa72ccc

@vercel
Copy link

@vercel vercel bot commented on fa72ccc Nov 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on fa72ccc Nov 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on fa72ccc Nov 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.