Skip to content

Commit

Permalink
Merge pull request #14 from dephy-io/remove_sysvar
Browse files Browse the repository at this point in the history
Remove instructions sysvar
  • Loading branch information
jasl authored Jun 23, 2024
2 parents 5d69e3f + 13059c4 commit 67d4ef4
Show file tree
Hide file tree
Showing 56 changed files with 312 additions and 347 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
13 changes: 5 additions & 8 deletions clients/js/dist/src/index.js

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

2 changes: 1 addition & 1 deletion clients/js/dist/src/index.js.map

Large diffs are not rendered by default.

13 changes: 5 additions & 8 deletions clients/js/dist/src/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,10 @@ function deviceActivationSignature(kind, data) {
function isDeviceActivationSignature(kind, value) {
return value.__kind === kind;
}
var DeviceSigningAlgorithm = /* @__PURE__ */ ((DeviceSigningAlgorithm3) => {
DeviceSigningAlgorithm3[DeviceSigningAlgorithm3["Ed25519"] = 0] = "Ed25519";
DeviceSigningAlgorithm3[DeviceSigningAlgorithm3["Secp256k1"] = 1] = "Secp256k1";
return DeviceSigningAlgorithm3;
var DeviceSigningAlgorithm = /* @__PURE__ */ ((DeviceSigningAlgorithm2) => {
DeviceSigningAlgorithm2[DeviceSigningAlgorithm2["Ed25519"] = 0] = "Ed25519";
DeviceSigningAlgorithm2[DeviceSigningAlgorithm2["Secp256k1"] = 1] = "Secp256k1";
return DeviceSigningAlgorithm2;
})(DeviceSigningAlgorithm || {});
function getDeviceSigningAlgorithmEncoder() {
return getEnumEncoder(DeviceSigningAlgorithm);
Expand Down Expand Up @@ -368,7 +368,6 @@ function getActivateDeviceInstruction(input) {
isWritable: false
},
ataProgram: { value: input.ataProgram ?? null, isWritable: false },
instructions: { value: input.instructions ?? null, isWritable: false },
payer: { value: input.payer ?? null, isWritable: true },
vendor: { value: input.vendor ?? null, isWritable: false },
productMint: { value: input.productMint ?? null, isWritable: false },
Expand Down Expand Up @@ -398,7 +397,6 @@ function getActivateDeviceInstruction(input) {
getAccountMeta(accounts.systemProgram),
getAccountMeta(accounts.token2022Program),
getAccountMeta(accounts.ataProgram),
getAccountMeta(accounts.instructions),
getAccountMeta(accounts.payer),
getAccountMeta(accounts.vendor),
getAccountMeta(accounts.productMint),
Expand All @@ -416,7 +414,7 @@ function getActivateDeviceInstruction(input) {
return instruction;
}
function parseActivateDeviceInstruction(instruction) {
if (instruction.accounts.length < 12) {
if (instruction.accounts.length < 11) {
throw new Error("Not enough accounts");
}
let accountIndex = 0;
Expand All @@ -431,7 +429,6 @@ function parseActivateDeviceInstruction(instruction) {
systemProgram: getNextAccount(),
token2022Program: getNextAccount(),
ataProgram: getNextAccount(),
instructions: getNextAccount(),
payer: getNextAccount(),
vendor: getNextAccount(),
productMint: getNextAccount(),
Expand Down
2 changes: 1 addition & 1 deletion clients/js/dist/src/index.mjs.map

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
*
* @see https://github.com/kinobi-so/kinobi
*/
import { Account, Address, Codec, Decoder, EncodedAccount, Encoder, FetchAccountConfig, FetchAccountsConfig, MaybeAccount, MaybeEncodedAccount, fetchEncodedAccount, fetchEncodedAccounts } from '@solana/web3.js';
import { Key, ProgramData, ProgramDataArgs } from '../types';
import { fetchEncodedAccount, fetchEncodedAccounts, type Account, type Address, type Codec, type Decoder, type EncodedAccount, type Encoder, type FetchAccountConfig, type FetchAccountsConfig, type MaybeAccount, type MaybeEncodedAccount } from '@solana/web3.js';
import { Key, type ProgramData, type ProgramDataArgs } from '../types';
export type ProgramDataAccount = {
key: Key;
authority: Address;
Expand Down

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

Loading

0 comments on commit 67d4ef4

Please sign in to comment.