Skip to content

Commit

Permalink
remove sysvar
Browse files Browse the repository at this point in the history
  • Loading branch information
Kabie committed Jun 23, 2024
1 parent 5d69e3f commit ee9b0d0
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 87 deletions.
66 changes: 45 additions & 21 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
use std::{error::Error, str::FromStr, time::Duration};
use std::time::{SystemTime, UNIX_EPOCH};
use std::{error::Error, str::FromStr, time::Duration};

use arrayref::array_ref;
use clap::{Args, Parser, Subcommand, ValueEnum};
use dephy_id_program_client::{instructions::{
ActivateDeviceBuilder, CreateDeviceBuilder, CreateProductBuilder, InitializeBuilder,
}, types::{self, DeviceActivationSignature}, DEVICE_MESSAGE_PREFIX, DEVICE_MINT_SEED_PREFIX, ID as PROGRAM_ID, PRODUCT_MINT_SEED_PREFIX, PROGRAM_PDA_SEED_PREFIX, EIP191_MESSAGE_PREFIX};
use dephy_id_program_client::{
instructions::{
ActivateDeviceBuilder, CreateDeviceBuilder, CreateProductBuilder, InitializeBuilder,
},
types::{self, DeviceActivationSignature},
DEVICE_MESSAGE_PREFIX, DEVICE_MINT_SEED_PREFIX, EIP191_MESSAGE_PREFIX, ID as PROGRAM_ID,
PRODUCT_MINT_SEED_PREFIX, PROGRAM_PDA_SEED_PREFIX,
};
use solana_client::rpc_client::RpcClient;
use solana_sdk::{
commitment_config::CommitmentConfig, keccak, pubkey::Pubkey, signature::{Keypair, Signature}, signer::{EncodableKey, Signer}, sysvar::instructions, transaction::Transaction
commitment_config::CommitmentConfig,
keccak,
pubkey::Pubkey,
signature::{Keypair, Signature},
signer::{EncodableKey, Signer},
transaction::Transaction,
};

#[derive(Debug, Parser)]
Expand Down Expand Up @@ -192,7 +202,6 @@ fn main() {
}
}


fn get_client(url: &String) -> RpcClient {
let timeout = Duration::from_secs(10);
let commitment_config = CommitmentConfig::processed();
Expand Down Expand Up @@ -361,7 +370,6 @@ fn dev_activate_device(args: DevActivateDeviceCliArgs) {
let client = get_client(&args.common.url);
let program_id = args.common.program_id.unwrap_or(PROGRAM_ID);
let token_program_id = spl_token_2022::ID;
let instructions_id = instructions::ID;

let device = read_key(&args.device_keypair);
let user = read_key(&args.user_keypair);
Expand Down Expand Up @@ -391,21 +399,25 @@ fn dev_activate_device(args: DevActivateDeviceCliArgs) {
&token_program_id,
);

let timestamp = SystemTime::now().duration_since(UNIX_EPOCH).ok().unwrap().as_secs();
let timestamp = SystemTime::now()
.duration_since(UNIX_EPOCH)
.ok()
.unwrap()
.as_secs();
let message = [
DEVICE_MESSAGE_PREFIX,
args.product_mint_pubkey.as_ref(),
user.pubkey().as_ref(),
&timestamp.to_le_bytes(),
].concat();
]
.concat();

let latest_block = client.get_latest_blockhash().unwrap();
let signature = sign(args.signature_type, &device, &message);

let transaction = Transaction::new_signed_with_payer(
&[ActivateDeviceBuilder::new()
.token2022_program(token_program_id)
.instructions(instructions_id)
.payer(payer.pubkey())
.device(device_pubkey)
.vendor(args.vendor_pubkey)
Expand Down Expand Up @@ -436,7 +448,11 @@ fn dev_activate_device(args: DevActivateDeviceCliArgs) {
};
}

fn sign(signature_type: SignatureType, keypair: &Keypair, message: &[u8]) -> DeviceActivationSignature {
fn sign(
signature_type: SignatureType,
keypair: &Keypair,
message: &[u8],
) -> DeviceActivationSignature {
match signature_type {
SignatureType::Ed25519 => {
let signature = keypair.sign_message(message);
Expand All @@ -459,7 +475,8 @@ fn sign(signature_type: SignatureType, keypair: &Keypair, message: &[u8]) -> Dev
EIP191_MESSAGE_PREFIX,
message.len().to_string().as_bytes(),
message,
].concat();
]
.concat();
let message_hash = keccak::hash(&eth_message);
let (signature, recovery_id) = libsecp256k1::sign(
&libsecp256k1::Message::parse(&message_hash.to_bytes()),
Expand All @@ -484,13 +501,18 @@ fn generate_message(args: GenerateMessageCliArgs) {
&program_id,
);

let timestamp = SystemTime::now().duration_since(UNIX_EPOCH).ok().unwrap().as_secs();
let timestamp = SystemTime::now()
.duration_since(UNIX_EPOCH)
.ok()
.unwrap()
.as_secs();
let message = [
DEVICE_MESSAGE_PREFIX,
did_mint_pubkey.as_ref(),
args.user_pubkey.as_ref(),
&timestamp.to_le_bytes(),
].concat();
]
.concat();

assert_eq!(message.len(), 96);
assert_eq!(array_ref![message, 0, 24], DEVICE_MESSAGE_PREFIX);
Expand Down Expand Up @@ -527,19 +549,22 @@ fn sign_message(args: SignMessageCliArgs) {
DeviceActivationSignature::Ed25519(signature_bytes) => {
eprintln!("Pubkey: {}", keypair.pubkey());
println!("Signature: 0x{}", hex::encode(signature_bytes));
},
DeviceActivationSignature::Secp256k1(signature_bytes, recovery_id) |
DeviceActivationSignature::EthSecp256k1(signature_bytes, recovery_id) => {
println!("Signature: {}{}", hex::encode(&signature_bytes), hex::encode(&[recovery_id]));
},
}
DeviceActivationSignature::Secp256k1(signature_bytes, recovery_id)
| DeviceActivationSignature::EthSecp256k1(signature_bytes, recovery_id) => {
println!(
"Signature: {}{}",
hex::encode(&signature_bytes),
hex::encode(&[recovery_id])
);
}
}
}

fn activate_device_offchain(args: ActivateDeviceOffchainCliArgs) {
let client = get_client(&args.common.url);
let program_id = args.common.program_id.unwrap_or(PROGRAM_ID);
let token_program_id = spl_token_2022::ID;
let instructions_id = instructions::ID;

let device_pubkey = args.device_pubkey;
let user = read_key(&args.user_keypair);
Expand Down Expand Up @@ -608,7 +633,6 @@ fn activate_device_offchain(args: ActivateDeviceOffchainCliArgs) {
let transaction = Transaction::new_signed_with_payer(
&[ActivateDeviceBuilder::new()
.token2022_program(token_program_id)
.instructions(instructions_id)
.payer(payer.pubkey())
.device(device_pubkey)
.vendor(args.vendor_pubkey)
Expand Down
17 changes: 8 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"programs:clean": "zx ./scripts/program/clean.mjs",
"programs:format": "zx ./scripts/program/format.mjs",
"programs:lint": "zx ./scripts/program/lint.mjs",
"generate:idls": "zx ./scripts/generate-idls.mjs",
"generate": "pnpm generate:idls && pnpm generate:clients",
"generate:idls": "bun ./scripts/generate-idls.mjs",
"generate": "bun generate:idls && bun generate:clients",
"generate:clients": "zx ./scripts/generate-clients.mjs",
"validator:start": "zx ./scripts/start-validator.mjs",
"validator:restart": "pnpm validator:start --restart",
Expand All @@ -19,13 +19,12 @@
},
"devDependencies": {
"@iarna/toml": "^2.2.5",
"@kinobi-so/nodes-from-anchor": "^0.20.5",
"@kinobi-so/renderers-js": "^0.20.5",
"@kinobi-so/renderers-js-umi": "^0.20.4",
"@kinobi-so/renderers-rust": "^0.20.6",
"@metaplex-foundation/shank-js": "^0.1.7",
"kinobi": "^0.20.3",
"typescript": "^5.4.5",
"@kinobi-so/nodes-from-anchor": "^0.20.6",
"@kinobi-so/renderers-js": "^0.20.9",
"@kinobi-so/renderers-js-umi": "^0.20.6",
"@kinobi-so/renderers-rust": "^0.20.10",
"kinobi": "^0.20.4",
"typescript": "^5.5.2",
"zx": "^7.2.3"
},
"packageManager": "[email protected]"
Expand Down
8 changes: 0 additions & 8 deletions program/idl.json
Original file line number Diff line number Diff line change
Expand Up @@ -224,14 +224,6 @@
"The associated token program"
]
},
{
"name": "instructions",
"isMut": false,
"isSigner": false,
"docs": [
"The instructions sysvar"
]
},
{
"name": "payer",
"isMut": true,
Expand Down
17 changes: 8 additions & 9 deletions program/src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,14 @@ pub enum Instruction {
#[account(0, name="system_program", desc="The system program")]
#[account(1, name="token_2022_program", desc="The SPL Token 2022 program")]
#[account(2, name="ata_program", desc="The associated token program")]
#[account(3, name="instructions", desc="The instructions sysvar")]
#[account(4, writable, signer, name="payer", desc="The account paying for the storage fees")]
#[account(5, name="vendor", desc="The vendor")]
#[account(6, name="product_mint", desc="The mint account for the product")]
#[account(7, name="product_associated_token", desc="The associated token account for the product")]
#[account(8, name="device", desc="The device")]
#[account(9, writable, name="device_mint", desc="The mint account for the device")]
#[account(10, writable, name="device_associated_token", desc="The associated token account for the device")]
#[account(11, name="owner", desc="The device's owner")]
#[account(3, writable, signer, name="payer", desc="The account paying for the storage fees")]
#[account(4, name="vendor", desc="The vendor")]
#[account(5, name="product_mint", desc="The mint account for the product")]
#[account(6, name="product_associated_token", desc="The associated token account for the product")]
#[account(7, name="device", desc="The device")]
#[account(8, writable, name="device_mint", desc="The mint account for the device")]
#[account(9, writable, name="device_associated_token", desc="The associated token account for the device")]
#[account(10, name="owner", desc="The device's owner")]
ActivateDevice(ActivateDeviceArgs),
}

Expand Down
36 changes: 9 additions & 27 deletions program/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use solana_sdk::{
keccak,
signature::Keypair,
signer::Signer,
system_program, sysvar,
system_program,
transaction::Transaction,
};
use spl_token_2022::{
Expand Down Expand Up @@ -364,22 +364,6 @@ async fn test_activate_device(
);
DeviceActivationSignature::EthSecp256k1(signature.serialize(), recovery_id.serialize())
},
// DeviceSigningAlgorithm::Secp256k1 => {
// let device_secp256k1_priv_key =
// libsecp256k1::SecretKey::parse(device.secret().as_bytes()).unwrap();
// let message = [
// DEVICE_MESSAGE_PREFIX,
// device_mint_pubkey.as_ref(),
// user.pubkey().as_ref(),
// &timestamp.to_le_bytes(),
// ].concat();
// let message_hash = keccak::hash(&message);
// let (signature, recovery_id) = libsecp256k1::sign(
// &libsecp256k1::Message::parse(&message_hash.to_bytes()),
// &device_secp256k1_priv_key,
// );
// DeviceActivationSignature::Secp256k1(signature.serialize(), recovery_id.serialize())
// }
};

let activate_device_ix = SolanaInstruction::new_with_borsh(
Expand All @@ -395,23 +379,21 @@ async fn test_activate_device(
AccountMeta::new(spl_token_2022::id(), false),
// #[account(2, name="ata_program", desc="The associated token program")]
AccountMeta::new(spl_associated_token_account::id(), false),
// #[account(3, name="instructions", desc="The instructions sys var")]
AccountMeta::new(sysvar::instructions::id(), false),
// #[account(4, writable, signer, name="payer", desc="The account paying for the storage fees")]
// #[account(3, writable, signer, name="payer", desc="The account paying for the storage fees")]
AccountMeta::new(ctx.payer.pubkey(), true),
// #[account(5, name="vendor", desc="The vendor")]
// #[account(4, name="vendor", desc="The vendor")]
AccountMeta::new(vendor.pubkey(), false),
// #[account(6, name="product_program_data", desc="The PDA for the product to store mint data")]
// #[account(5, name="product_program_data", desc="The PDA for the product to store mint data")]
AccountMeta::new(product_mint_pubkey, false),
// #[account(7, name="product_associated_token", desc="The ATA for the product")]
// #[account(6, name="product_associated_token", desc="The ATA for the product")]
AccountMeta::new(product_ata_pubkey, false),
// #[account(8, name="device", desc="The device")]
// #[account(7, name="device", desc="The device")]
AccountMeta::new(device_pubkey, false),
// #[account(9, writable, name="device_program_data", desc="The PDA for the device to store mint data")]
// #[account(8, writable, name="device_program_data", desc="The PDA for the device to store mint data")]
AccountMeta::new(device_mint_pubkey, false),
// #[account(10, writable, name="device_associated_token", desc="The ATA for the device")]
// #[account(9, writable, name="device_associated_token", desc="The ATA for the device")]
AccountMeta::new(device_ata_pubkey, false),
// #[account(11, name="owner", desc="The device's owner")]
// #[account(10, name="owner", desc="The device's owner")]
AccountMeta::new(user.pubkey(), true),
],
);
Expand Down
21 changes: 8 additions & 13 deletions scripts/generate-idls.mjs
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
#!/usr/bin/env zx
import 'zx/globals';
import { generateIdl } from '@metaplex-foundation/shank-js';
import { getCargo, getProgramFolders } from './utils.mjs';
import { $ } from "bun"

const binaryInstallDir = path.join(__dirname, '..', '.cargo');

getProgramFolders().forEach((folder) => {
getProgramFolders().forEach(async (folder) => {
const cargo = getCargo(folder);
const isShank = Object.keys(cargo.dependencies).includes('shank');
const programDir = path.join(__dirname, '..', folder);
const programDir = path.join(__dirname, '..', folder)
const programId = cargo.package.metadata.solana['program-id']

generateIdl({
generator: isShank ? 'shank' : 'anchor',
programName: cargo.package.name.replace(/-/g, '_'),
programId: cargo.package.metadata.solana['program-id'],
idlDir: programDir,
idlName: 'idl',
programDir,
binaryInstallDir,
});
});
if (isShank) {
await $`shank idl -r ${programDir} -o ${programDir} -p ${programId} --out-filename idl.json`
}
})

0 comments on commit ee9b0d0

Please sign in to comment.