Skip to content

Commit

Permalink
Make claim and onboarding use v0 messages (#455)
Browse files Browse the repository at this point in the history
* claim_instructions

* dataonly versioned_txn

* tag foundation as supporting helius priority api
  • Loading branch information
madninja authored Feb 12, 2025
1 parent aa3879c commit ed0a469
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 95 deletions.
35 changes: 23 additions & 12 deletions helium-lib/src/hotspot/dataonly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,21 @@ use crate::{
helium_entity_manager, helium_sub_daos, hotspot,
hotspot::{HotspotInfoUpdate, ECC_VERIFIER},
keypair::{Keypair, Pubkey},
kta, message, mk_transaction_with_blockhash, priority_fee,
kta, message, priority_fee,
programs::{
SPL_ACCOUNT_COMPRESSION_PROGRAM_ID, SPL_NOOP_PROGRAM_ID, TOKEN_METADATA_PROGRAM_ID,
},
solana_sdk::{instruction::Instruction, signature::Signer, transaction::Transaction},
solana_sdk::{
instruction::Instruction,
signature::{NullSigner, Signer},
transaction::VersionedTransaction,
},
token::Token,
TransactionOpts,
};
use helium_crypto::{PublicKey, Sign};
use helium_proto::{BlockchainTxn, BlockchainTxnAddGatewayV1, Message, Txn};
use serde::{Deserialize, Serialize};
use solana_sdk::transaction::VersionedTransaction;

mod iot {
use super::*;
Expand Down Expand Up @@ -240,7 +243,7 @@ pub async fn issue_transaction<C: AsRef<SolanaRpcClient> + GetAnchorAccount>(
add_tx: &mut BlockchainTxnAddGatewayV1,
owner: Pubkey,
opts: &TransactionOpts,
) -> Result<(Transaction, u64), Error> {
) -> Result<(VersionedTransaction, u64), Error> {
fn mk_accounts(
config_account: helium_entity_manager::DataOnlyConfigV0,
owner: Pubkey,
Expand Down Expand Up @@ -296,14 +299,21 @@ pub async fn issue_transaction<C: AsRef<SolanaRpcClient> + GetAnchorAccount>(
issue_ix,
];

let (txn, latest_block_height) = mk_transaction_with_blockhash(client, ixs, &owner).await?;

let (msg, block_height) = message::mk_message(client, ixs, &opts.lut_addresses, &owner).await?;
let txn = VersionedTransaction::try_new(
msg,
&[
// Set payer as first account key to allow callers to sign
&NullSigner::new(&owner),
&NullSigner::new(&ECC_VERIFIER),
],
)?;
let sig = add_tx.gateway_signature.clone();
add_tx.gateway_signature = vec![];
let msg = add_tx.encode_to_vec();

let signed_txn = verify_helium_key(verifier, &msg, &sig, txn).await?;
Ok((signed_txn, latest_block_height))
Ok((signed_txn, block_height))
}

#[derive(Debug, serde::Serialize)]
Expand Down Expand Up @@ -363,20 +373,21 @@ pub async fn issue<C: AsRef<SolanaRpcClient> + GetAnchorAccount>(
add_tx: &mut BlockchainTxnAddGatewayV1,
keypair: &Keypair,
opts: &TransactionOpts,
) -> Result<(Transaction, u64), Error> {
) -> Result<(VersionedTransaction, u64), Error> {
let (mut txn, block_height) =
issue_transaction(client, verifier, add_tx, keypair.pubkey(), opts).await?;
let blockhash = txn.message.recent_blockhash;
txn.try_partial_sign(&[keypair], blockhash)?;
let message_data = txn.message.serialize();
let signature = keypair.try_sign_message(&message_data)?;
txn.signatures[0] = signature;
Ok((txn, block_height))
}

async fn verify_helium_key(
verifier: &str,
msg: &[u8],
signature: &[u8],
tx: Transaction,
) -> Result<Transaction, Error> {
tx: VersionedTransaction,
) -> Result<VersionedTransaction, Error> {
#[derive(Deserialize, Serialize, Default)]
struct VerifyRequest<'a> {
// hex encoded solana transaction
Expand Down
22 changes: 16 additions & 6 deletions helium-lib/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,20 @@ pub async fn get_lut_accounts<C: AsRef<SolanaRpcClient>>(
.try_collect()
}

pub fn mk_raw_message(
ixs: &[Instruction],
lut_accounts: &[AddressLookupTableAccount],
payer: &Pubkey,
) -> Result<VersionedMessage, Error> {
let msg = VersionedMessage::V0(v0::Message::try_compile(
payer,
ixs,
lut_accounts,
Default::default(),
)?);
Ok(msg)
}

pub async fn mk_message<C: AsRef<SolanaRpcClient>>(
client: &C,
ixs: &[Instruction],
Expand All @@ -47,11 +61,7 @@ pub async fn mk_message<C: AsRef<SolanaRpcClient>>(
let (recent_blockhash, recent_blockheight) = solana_client
.get_latest_blockhash_with_commitment(solana_client.commitment())
.await?;
let msg = VersionedMessage::V0(v0::Message::try_compile(
payer,
ixs,
&lut_accounts,
recent_blockhash,
)?);
let mut msg = mk_raw_message(ixs, &lut_accounts, payer)?;
msg.set_recent_blockhash(recent_blockhash);
Ok((msg, recent_blockheight))
}
7 changes: 5 additions & 2 deletions helium-lib/src/priority_fee.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use std::ops::RangeInclusive;

use crate::{
anchor_lang::ToAccountMetas, client::SolanaRpcClient, error::Error, keypair::Pubkey,
anchor_lang::ToAccountMetas,
client::{SolanaRpcClient, SOLANA_URL_MAINNET},
error::Error,
keypair::Pubkey,
solana_client,
};
use itertools::Itertools;
Expand All @@ -16,7 +19,7 @@ pub async fn get_estimate<C: AsRef<SolanaRpcClient>>(
fee_range: RangeInclusive<u64>,
) -> Result<u64, Error> {
let client_url = client.as_ref().url();
if client_url.contains("mainnet.helius") {
if client_url == SOLANA_URL_MAINNET || client_url.contains("mainnet.helius") {
helius::get_estimate(client, accounts, fee_range).await
} else {
base::get_estimate(client, accounts, fee_range).await
Expand Down
Loading

0 comments on commit ed0a469

Please sign in to comment.