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

Use PDA for verified_messages account #10

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
18 changes: 14 additions & 4 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ use audius_reward_manager::{
add_sender, create_sender, delete_sender, init, transfer, verify_transfer_signature,
},
processor::SENDER_SEED_PREFIX,
state::{RewardManager, SenderAccount, VerifiedMessages},
state::{RewardManager, SenderAccount, VerifiedMessages, VerifiedMessage},
utils::find_derived_pair,
error::to_audius_program_error
};

use hex::FromHex;
Expand Down Expand Up @@ -265,6 +266,7 @@ fn command_verify_transfer_signature(
let bot_oracle_account = config.rpc_client.get_account_data(&bot_oracle_pubkey)?;
let bot_oracle = SenderAccount::unpack(bot_oracle_account.as_slice())?;

println!("Signing as normal sender");
// Sender message
[
decoded_recipient_address.as_ref(),
Expand All @@ -277,6 +279,7 @@ fn command_verify_transfer_signature(
]
.concat()
} else {
println!("Signing as bot oracle!");
// Bot oracle message
[
decoded_recipient_address.as_ref(),
Expand Down Expand Up @@ -339,6 +342,13 @@ fn command_transfer(
) -> CommandResult {
let reward_manager = config.rpc_client.get_account_data(&reward_manager_pubkey)?;
let reward_manager = RewardManager::unpack(reward_manager.as_slice())?;
println!("RewardManager num votes: {:?}", reward_manager.min_votes);

let verified_messages = config.rpc_client.get_account_data(&verified_messages_pubkey)?;
let verified_messages = VerifiedMessages::unpack(verified_messages.as_slice())?;
println!("VerifiedMsgs reward_manager: {:?}", verified_messages.reward_manager);
println!("VerifiedMsgs number of messages: {:?}", verified_messages.messages.len());
println!("VerifiedMsgs : {:?}", verified_messages);

let decoded_recipient_address =
<[u8; 20]>::from_hex(recipient_eth_address).expect(HEX_ETH_ADDRESS_DECODING_ERROR);
Expand Down Expand Up @@ -586,7 +596,7 @@ fn main() {
)
.arg(
Arg::with_name("verified_messages_pubkey")
.long("pubkey")
.long("verified-messages-pubkey")
.validator(is_pubkey)
.value_name("ADDRESS")
.takes_value(true)
Expand Down Expand Up @@ -851,7 +861,7 @@ fn main() {
Ok(())
})
.map_err(|err| {
eprintln!("{}", err);
exit(1);
eprintln!("{:?}", err);
exit(2)
});
}
1 change: 1 addition & 0 deletions cli/test1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[154,171,17,10,116,210,17,225,61,243,190,214,174,5,192,5,199,190,110,16,236,116,198,122,126,140,120,162,37,34,83,250,95,254,201,89,3,252,32,15,72,251,27,103,180,116,79,207,221,37,188,87,51,82,138,79,113,209,85,247,254,117,5,81]
43 changes: 37 additions & 6 deletions program/src/instruction.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
//! Instruction types

use crate::{
processor::{SENDER_SEED_PREFIX, TRANSFER_SEED_PREFIX},
processor::{
SENDER_SEED_PREFIX,
TRANSFER_SEED_PREFIX,
VERIFY_TRANSFER_SEED_PREFIX
},
utils::{find_derived_pair, find_program_address, EthereumAddress},
};
use borsh::{BorshDeserialize, BorshSerialize};
Expand Down Expand Up @@ -37,6 +41,13 @@ pub struct AddSenderArgs {
pub operator: EthereumAddress,
}

/// Verify `Transfer` instruction args
#[derive(BorshSerialize, BorshDeserialize, PartialEq, Debug, Clone)]
pub struct VerifyTransferSignatureArgs {
/// ID generated on backend
pub id: String
}

/// `Transfer` instruction args
#[derive(BorshSerialize, BorshDeserialize, PartialEq, Debug, Clone)]
pub struct TransferArgs {
Expand Down Expand Up @@ -96,9 +107,12 @@ pub enum Instructions {
///
/// 0. `[writable]` New or existing account storing verified messages
/// 1. `[]` Reward manager
/// 1. `[]` Reward manager authority
/// 1. `[signer]` Funder
/// 2. `[]` Sender
/// 3. `[]` Sysvar instruction id
VerifyTransferSignature,
/// 8. `[]` Sysvar rent
/// 2. `[]` Instruction info
hareeshnagaraj marked this conversation as resolved.
Show resolved Hide resolved
VerifyTransferSignature(VerifyTransferSignatureArgs),

/// Transfer tokens to pointed receiver
///
Expand Down Expand Up @@ -270,17 +284,34 @@ where
/// Create `VerifyTransferSignature` instruction
pub fn verify_transfer_signature(
program_id: &Pubkey,
verified_messages: &Pubkey,
reward_manager: &Pubkey,
sender: &Pubkey,
funder: &Pubkey,
id: String
) -> Result<Instruction, ProgramError> {
let data = Instructions::VerifyTransferSignature.try_to_vec()?;
let data = Instructions::VerifyTransferSignature(
VerifyTransferSignatureArgs {
id: id.clone()
}
).try_to_vec()?;

let (reward_manager_authority, verified_messages, _) = find_derived_pair(
program_id,
reward_manager,
[VERIFY_TRANSFER_SEED_PREFIX.as_bytes().as_ref(), id.as_ref()]
.concat()
.as_ref(),
);

let accounts = vec![
AccountMeta::new(*verified_messages, false),
AccountMeta::new(verified_messages, false),
AccountMeta::new_readonly(*reward_manager, false),
AccountMeta::new_readonly(reward_manager_authority, false),
AccountMeta::new(*funder, true),
AccountMeta::new_readonly(*sender, false),
AccountMeta::new_readonly(sysvar::rent::id(), false),
AccountMeta::new_readonly(sysvar::instructions::id(), false),
AccountMeta::new_readonly(system_program::id(), false),
];

Ok(Instruction {
Expand Down
2 changes: 1 addition & 1 deletion program/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ pub mod entrypoint;
// Export current sdk types for downstream users building with a different sdk version
pub use solana_program;

solana_program::declare_id!("p1AVmrdt7m6AGCxDfiDQqxBVhY1Zjk7avk1nGLQyjMC");
solana_program::declare_id!("3CV3yryZKWkGegREpyEgsvAJjHS2FFVFxTQreskCXK2S");
61 changes: 58 additions & 3 deletions program/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use crate::{
error::AudiusProgramError,
instruction::{
AddSenderArgs, CreateSenderArgs, InitRewardManagerArgs, Instructions, TransferArgs,
AddSenderArgs, CreateSenderArgs, InitRewardManagerArgs, Instructions, TransferArgs, VerifyTransferSignatureArgs
},
state::{RewardManager, SenderAccount, VerifiedMessage, VerifiedMessages},
utils::*,
Expand All @@ -25,6 +25,8 @@ use solana_program::{
pub const SENDER_SEED_PREFIX: &str = "S_";
/// Transfer program account seed
pub const TRANSFER_SEED_PREFIX: &str = "T_";
/// Verify transfer program account seed
pub const VERIFY_TRANSFER_SEED_PREFIX: &str = "V_";
/// Transfer account space
pub const TRANSFER_ACC_SPACE: usize = 0;

Expand Down Expand Up @@ -219,16 +221,53 @@ impl Processor {
program_id: &Pubkey,
verified_messages_info: &AccountInfo<'a>,
reward_manager_info: &AccountInfo<'a>,
authority_info: &AccountInfo<'a>,
funder_info: &AccountInfo<'a>,
rent_info: &AccountInfo<'a>,
sender_info: &AccountInfo<'a>,
instruction_info: &AccountInfo<'a>,
verify_transfer_data: VerifyTransferSignatureArgs,
) -> ProgramResult {
assert_owned_by(verified_messages_info, program_id)?;
// TODO:
assert_owned_by(reward_manager_info, program_id)?;
assert_owned_by(sender_info, program_id)?;

let sender_account = SenderAccount::unpack(&sender_info.data.borrow())?;
assert_account_key(reward_manager_info, &sender_account.reward_manager)?;

// Verify derived address matches expected value
let derived_seed = [
VERIFY_TRANSFER_SEED_PREFIX.as_bytes().as_ref(),
verify_transfer_data.id.as_ref(),
]
.concat();

let (reward_manager_authority, derived_address, bump_seed) =
find_derived_pair(program_id, reward_manager_info.key, derived_seed.as_ref());

assert_account_key(authority_info, &reward_manager_authority)?;
assert_account_key(verified_messages_info, &derived_address)?;

let signers_seeds = &[
&reward_manager_authority.to_bytes()[..32],
&derived_seed.as_slice(),
&[bump_seed],
];

if verified_messages_info.data_len() == 0 && verified_messages_info.lamports() == 0 {
let rent = Rent::from_account_info(rent_info)?;
create_account(
program_id,
funder_info.clone(),
verified_messages_info.clone(),
VerifiedMessages::LEN,
&[signers_seeds],
&rent,
)?;
} else {
assert_owned_by(verified_messages_info, program_id)?;
}

let mut verified_messages =
VerifiedMessages::unpack_unchecked(&verified_messages_info.data.borrow())?;
if verified_messages.is_initialized() {
Expand Down Expand Up @@ -339,6 +378,8 @@ impl Processor {
&[bump_seed],
];

// TODO: Verify transfer target address

// Create deterministic account on-chain
create_account(
program_id,
Expand Down Expand Up @@ -467,20 +508,34 @@ impl Processor {
operator,
)
}
Instructions::VerifyTransferSignature => {
Instructions::VerifyTransferSignature(
VerifyTransferSignatureArgs {
id
}
) => {
msg!("Instruction: VerifyTransferSignature");

let verified_messages = next_account_info(account_info_iter)?;
let reward_manager = next_account_info(account_info_iter)?;
let authority = next_account_info(account_info_iter)?;
let funder_info = next_account_info(account_info_iter)?;
let sender = next_account_info(account_info_iter)?;
let rent_info = next_account_info(account_info_iter)?;
let instructions_info = next_account_info(account_info_iter)?;
let _system_program_id = next_account_info(account_info_iter)?;

Self::process_verify_transfer_signature(
program_id,
verified_messages,
reward_manager,
authority,
funder_info,
rent_info,
sender,
instructions_info,
VerifyTransferSignatureArgs {
id
},
)
}
Instructions::Transfer(TransferArgs {
Expand Down
Loading