Skip to content

Commit

Permalink
Merge pull request #28 from dephy-io/feat/puppet
Browse files Browse the repository at this point in the history
Feat/puppet
  • Loading branch information
jasl authored Sep 27, 2024
2 parents 686ba36 + d05636d commit b067ca5
Show file tree
Hide file tree
Showing 35 changed files with 5,193 additions and 62 deletions.
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ members = [
"program",
"clients/rust",
"cli",
"examples/kwil",
"examples/kwil"
]
exclude = [
"extensions",
"templates",
]

[workspace.dependencies]
Expand Down
102 changes: 101 additions & 1 deletion cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ use clap::{Args, Parser, Subcommand, ValueEnum};
use dephy_id_program_client::{
instructions::{
ActivateDeviceBuilder, CreateDeviceBuilder, CreateProductBuilder, InitializeBuilder,
CreateActivatedDeviceNonSignerBuilder,
},
types::{self, DeviceActivationSignature},
types::{self, DeviceActivationSignature, CreateActivatedDeviceArgs},
DEVICE_MESSAGE_PREFIX, DEVICE_MINT_SEED_PREFIX, EIP191_MESSAGE_PREFIX, ID as PROGRAM_ID,
PRODUCT_MINT_SEED_PREFIX, PROGRAM_PDA_SEED_PREFIX,
};
Expand All @@ -35,6 +36,7 @@ enum Commands {
CreateProduct(CreateProductCliArgs),
CalcDevicePubkey(CalcDevicePubkeyCliArgs),
CreateDevice(CreateDeviceCliArgs),
DevCreateActivatedDevice(DevCreateActivatedDeviceCliArgs),
GenerateMessage(GenerateMessageCliArgs),
SignMessage(SignMessageCliArgs),
ActivateDeviceOffchain(ActivateDeviceOffchainCliArgs),
Expand Down Expand Up @@ -114,6 +116,29 @@ struct CreateDeviceCliArgs {
common: CommonArgs,
}

#[derive(Debug, Args)]
struct DevCreateActivatedDeviceCliArgs {
#[arg(long = "vendor")]
vendor_keypair: String,
#[arg(long = "product", value_parser = parse_pubkey)]
product_pubkey: Pubkey,
#[arg(long = "device")]
device_keypair: String,
#[arg(value_enum, long, default_value_t = SignatureType::Secp256k1)]
signature_type: SignatureType,
#[arg(long = "user")]
user_keypair: String,
#[arg(value_enum, long, default_value_t = DeviceSigningAlgorithm::Secp256k1)]
signing_alg: DeviceSigningAlgorithm,
name: String,
#[arg(default_value = "")]
metadata_uri: String,
#[arg(short = 'm', value_parser = parse_key_val::<String, String>)]
additional_metadata: Vec<(String, String)>,
#[command(flatten)]
common: CommonArgs,
}

#[derive(Debug, Clone, Copy, ValueEnum)]
enum SignatureType {
Ed25519,
Expand Down Expand Up @@ -224,6 +249,7 @@ fn main() {
Commands::SignMessage(args) => sign_message(args),
Commands::ActivateDeviceOffchain(args) => activate_device_offchain(args),
Commands::DevActivateDevice(args) => dev_activate_device(args),
Commands::DevCreateActivatedDevice(args) => dev_create_activated_device(args),
}
}

Expand Down Expand Up @@ -406,6 +432,78 @@ fn create_device(args: CreateDeviceCliArgs) {
};
}

fn dev_create_activated_device(args: DevCreateActivatedDeviceCliArgs) {
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 device = read_key(&args.device_keypair);
let device_pubkey = get_device_pubkey(&device, &args.signature_type.into());

let vendor = read_key(&args.vendor_keypair);
let user = read_key(&args.user_keypair);
let payer = read_key(&args.common.payer.unwrap_or(args.user_keypair));
let latest_block = client.get_latest_blockhash().unwrap();

let product_atoken_pubkey =
spl_associated_token_account::get_associated_token_address_with_program_id(
&device_pubkey,
&args.product_pubkey,
&token_program_id,
);

let (did_mint_pubkey, _bump) = Pubkey::find_program_address(
&[
DEVICE_MINT_SEED_PREFIX,
args.product_pubkey.as_ref(),
device_pubkey.as_ref(),
],
&program_id,
);

let did_atoken_pubkey =
spl_associated_token_account::get_associated_token_address_with_program_id(
&user.pubkey(),
&did_mint_pubkey,
&token_program_id,
);

let transaction = Transaction::new_signed_with_payer(
&[CreateActivatedDeviceNonSignerBuilder::new()
.payer(payer.pubkey())
.vendor(vendor.pubkey())
.product_mint(args.product_pubkey)
.product_associated_token(product_atoken_pubkey)
.device(device_pubkey)
.device_mint(did_mint_pubkey)
.device_associated_token(did_atoken_pubkey)
.create_activated_device_args(CreateActivatedDeviceArgs {
name: args.name,
uri: args.metadata_uri,
additional_metadata: args.additional_metadata,
})
.owner(user.pubkey())
.instruction()],
Some(&payer.pubkey()),
&[&payer, &vendor],
latest_block
);

match client.send_and_confirm_transaction(&transaction) {
Ok(sig) => {
eprintln!("Success: {:?}", sig);
eprintln!("User: {}", user.pubkey());
eprintln!("Device: {}", device_pubkey);
eprintln!("Mint: {}", did_mint_pubkey);
eprintln!("AToken: {}", did_atoken_pubkey);
println!("{},{},{}", device_pubkey, did_mint_pubkey, did_atoken_pubkey);
}
Err(err) => {
eprintln!("Error: {:?}", err);
}
};
}

fn dev_activate_device(args: DevActivateDeviceCliArgs) {
let client = get_client(&args.common.url);
let program_id = args.common.program_id.unwrap_or(PROGRAM_ID);
Expand Down Expand Up @@ -444,6 +542,7 @@ fn dev_activate_device(args: DevActivateDeviceCliArgs) {
.ok()
.unwrap()
.as_secs();

let message = [
DEVICE_MESSAGE_PREFIX,
args.product_mint_pubkey.as_ref(),
Expand Down Expand Up @@ -481,6 +580,7 @@ fn dev_activate_device(args: DevActivateDeviceCliArgs) {
eprintln!("Device: {}", device_pubkey);
eprintln!("Mint: {}", did_mint_pubkey);
eprintln!("AToken: {}", did_atoken_pubkey);
println!("{}", did_atoken_pubkey)
}
Err(err) => {
eprintln!("Error: {:?}", err);
Expand Down
9 changes: 9 additions & 0 deletions extensions/puppet/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.anchor
.DS_Store
target
**/*.rs.bk
node_modules
test-ledger
.yarn
key.json
device.json
7 changes: 7 additions & 0 deletions extensions/puppet/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.anchor
.DS_Store
target
node_modules
dist
build
test-ledger
26 changes: 26 additions & 0 deletions extensions/puppet/Anchor.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[toolchain]

[features]
resolution = true
skip-lint = false

[programs.localnet]
puppet = "6nxvjh3D3kLkkb25sgRUdyomfYqeoUv7gfRFgcScy6jN"

[registry]
url = "https://api.apr.dev"

[provider]
cluster = "Localnet"
wallet = "~/.config/solana/id.json"

[[test.genesis]]
address = "hdMghjD73uASxgJXi6e1mGPsXqnADMsrqB1bveqABP1"
program = "../../target/deploy/dephy_id_program.so"

[[test.genesis]]
address = "metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s"
program = "tests/fixtures/mpl_token_metadata.so"

[scripts]
test = "bun run -b test"
Loading

0 comments on commit b067ca5

Please sign in to comment.