1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
use anchor_client::solana_sdk::hash::Hash;
use std::sync::Arc;
use crate::error::Err;
use bytemuck::Pod;
use bytemuck::Zeroable;
use anchor_client::solana_sdk::message::Message;
use anchor_client::anchor_lang::prelude::*;
use anchor_client::anchor_lang::Discriminator;
use anchor_client::anchor_lang::InstructionData;
use anchor_client::solana_sdk::signature::Signer;
use anchor_client::solana_sdk::signer::keypair::Keypair;
use anchor_client::solana_sdk::instruction::Instruction;
use anchor_client::solana_sdk::transaction::Transaction;
use anchor_client::solana_sdk::pubkey;
use anchor_client::anchor_lang;
use anchor_client::solana_sdk::pubkey::Pubkey;
use std::result::Result;
pub use anchor_client::solana_sdk;
pub struct QuoteInitSimple {
pub quote: Pubkey,
pub verifier_queue: Pubkey,
pub authority: Pubkey,
pub payer: Pubkey,
pub system_program: Pubkey,
}
#[derive(Clone, AnchorSerialize, AnchorDeserialize)]
pub struct QuoteInitSimpleParams {
pub data: [u8; 512],
pub total_len: u32,
pub chunk_start: u32,
pub chunk_end: u32,
}
pub struct QuoteInitSimpleArgs {
pub quote: Pubkey,
pub verifier_queue: Pubkey,
pub authority: Pubkey,
pub data: Vec<u8>,
}
impl Discriminator for QuoteInitSimpleParams {
const DISCRIMINATOR: [u8; 8] = [0; 8];
fn discriminator() -> [u8; 8] {
ix_discriminator("quote_init_simple")
}
}
impl InstructionData for QuoteInitSimpleParams {}
impl ToAccountMetas for QuoteInitSimple {
fn to_account_metas(&self, _: Option<bool>) -> Vec<AccountMeta> {
vec![
AccountMeta {
pubkey: self.quote,
is_signer: true,
is_writable: true,
},
AccountMeta {
pubkey: self.verifier_queue,
is_signer: false,
is_writable: true,
},
AccountMeta {
pubkey: self.authority,
is_signer: false, is_writable: false,
},
AccountMeta {
pubkey: self.payer,
is_signer: true,
is_writable: true,
},
AccountMeta {
pubkey: self.system_program,
is_signer: false,
is_writable: false,
},
]
}
}
impl QuoteInitSimple {
pub fn build(
client: &anchor_client::Client<Arc<Keypair>>,
args: QuoteInitSimpleArgs,
signers: Vec<&Keypair>,
) -> Result<Vec<Instruction>, Err> {
let mut ixs = Vec::new();
let payer = signers[0];
let queue_data: ServiceQueueAccountData = load(client, args.verifier_queue)?;
let mut i = 0;
let data = args.data;
while i < data.len() {
let back = std::cmp::min(i + 512, data.len());
let mut chunk = data[i..back].to_vec();
chunk.resize(512, 0);
ixs.push(build_ix(
QuoteInitSimple {
quote: args.quote,
verifier_queue: args.verifier_queue,
authority: args.authority,
payer: payer.pubkey(),
system_program: anchor_client::solana_sdk::system_program::ID,
},
QuoteInitSimpleParams {
data: chunk.try_into().unwrap(),
total_len: data.len() as u32,
chunk_start: i as u32,
chunk_end: back as u32,
},
));
i += 512;
}
Ok(ixs)
}
}
pub const PID: Pubkey = pubkey!("Hxfwq7cxss4Ef9iDvaLb617dhageGyNWbDLLrg2sdQgT");
pub fn ix_discriminator(name: &str) -> [u8; 8] {
let preimage = format!("global:{}", name);
let mut sighash = [0u8; 8];
sighash.copy_from_slice(
&anchor_lang::solana_program::hash::hash(preimage.as_bytes()).to_bytes()[..8],
);
sighash
}
pub fn ix_to_tx(ixs: &[Instruction], signers: &[&Keypair], blockhash: Hash) -> Transaction {
let msg = Message::new(ixs, Some(&signers[0].pubkey()));
Transaction::new(&signers.to_vec(), msg, blockhash)
}
pub fn build_ix<A: ToAccountMetas, I: InstructionData + Discriminator>(
accounts: A,
params: I,
) -> Instruction {
Instruction {
program_id: PID,
accounts: accounts.to_account_metas(None),
data: params.data(),
}
}
pub fn build_tx<A: ToAccountMetas, I: InstructionData + Discriminator>(
anchor_client: &anchor_client::Client<Arc<Keypair>>,
accounts: A,
params: I,
signers: Vec<&Keypair>,
) -> Transaction {
let payer = signers[0];
let ix = Instruction {
program_id: PID,
accounts: accounts.to_account_metas(None),
data: params.data(),
};
let mut tx = Transaction::new_with_payer(&[ix], Some(&payer.pubkey()));
let blockhash = anchor_client
.program(PID)
.rpc()
.get_latest_blockhash()
.unwrap();
tx.sign(&signers, blockhash);
println!("{:?}", tx.message.account_keys);
tx
}
pub fn load<T: bytemuck::Pod>(
client: &anchor_client::Client<Arc<Keypair>>,
key: Pubkey,
) -> Result<T, Err> {
let data = client
.program(PID)
.rpc()
.get_account_data(&key)
.unwrap();
Ok(*bytemuck::from_bytes::<T>(&data[8..]))
}
#[repr(packed)]
#[derive(Copy, Clone, Debug)]
pub struct ServiceQueueAccountData {
pub authority: Pubkey,
pub verifier_queue: Pubkey,
pub mr_enclaves: [[u8; 32]; 32],
pub mr_enclaves_len: u32,
pub data: [Pubkey; 32],
pub data_len: u32,
pub allow_authority_override_after: i64,
pub require_authority_heartbeat_permission: bool,
pub require_usage_permissions: bool,
pub max_quote_verification_age: i64,
pub reward: u32, pub last_heartbeat: i64,
pub node_timeout: i64,
pub curr_idx: u32,
pub gc_idx: u32,
pub _ebuf: [u8; 1024],
}
unsafe impl Pod for ServiceQueueAccountData {}
unsafe impl Zeroable for ServiceQueueAccountData {}