From 19a79bd5379677bad1eeaf40d64b4387733d3329 Mon Sep 17 00:00:00 2001 From: Yak Date: Mon, 23 Sep 2024 18:35:07 -0300 Subject: [PATCH] stuff --- index.d.ts | 9 +++++++++ src/lib.rs | 25 +++++++++++++++++++++++++ src/wallet.rs | 22 ++++++++++++++++++++++ 3 files changed, 56 insertions(+) diff --git a/index.d.ts b/index.d.ts index 7c9d311..75ec3f4 100644 --- a/index.d.ts +++ b/index.d.ts @@ -459,6 +459,15 @@ export declare class Peer { * @returns {Promise} The sync store response. */ syncStoreFromLauncherId(launcherId: Buffer, lastHeight: number | undefined | null, lastHeaderHash: Buffer, withHistory: boolean): Promise + /** + * Fetch a store's creation height. + * + * @param {Buffer} launcherId - The store's launcher/singleton ID. + * @param {Option} lastHeight - Min. height to search records from. If null, sync will be done from the genesis block. + * @param {Buffer} lastHeaderHash - Header hash corresponding to `lastHeight`. If null, this should be the genesis challenge of the current chain. + * @returns {Promise} The store's creation height. + */ + getStoreCreationHeight(launcherId: Buffer, lastHeight: number | undefined | null, lastHeaderHash: Buffer): Promise /** * Broadcasts a spend bundle to the mempool. * diff --git a/src/lib.rs b/src/lib.rs index 0d3313b..883772d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -578,6 +578,31 @@ impl Peer { res.to_js() } + #[napi] + /// Fetch a store's creation height. + /// + /// @param {Buffer} launcherId - The store's launcher/singleton ID. + /// @param {Option} lastHeight - Min. height to search records from. If null, sync will be done from the genesis block. + /// @param {Buffer} lastHeaderHash - Header hash corresponding to `lastHeight`. If null, this should be the genesis challenge of the current chain. + /// @returns {Promise} The store's creation height. + pub async fn get_store_creation_height( + &self, + launcher_id: Buffer, + last_height: Option, + last_header_hash: Buffer, + ) -> napi::Result { + let res = wallet::get_store_creation_height( + &self.inner.clone(), + RustBytes32::from_js(launcher_id)?, + last_height, + RustBytes32::from_js(last_header_hash)?, + ) + .await + .map_err(js::err)?; + + (res as u64).to_js() + } + #[napi] /// Broadcasts a spend bundle to the mempool. /// diff --git a/src/wallet.rs b/src/wallet.rs index 7d5651c..2854eb8 100644 --- a/src/wallet.rs +++ b/src/wallet.rs @@ -648,6 +648,28 @@ pub async fn sync_store_using_launcher_id( }) } +pub async fn get_store_creation_height( + peer: &Peer, + launcher_id: Bytes32, + last_height: Option, + last_header_hash: Bytes32, +) -> Result { + let response = peer + .request_coin_state(vec![launcher_id], last_height, last_header_hash, false) + .await + .map_err(WalletError::Client)? + .map_err(|_| WalletError::RejectCoinState)?; + let last_coin_record = response + .coin_states + .into_iter() + .next() + .ok_or(WalletError::UnknownCoin)?; + + last_coin_record + .created_height + .ok_or(WalletError::UnknownCoin) +} + pub enum DataStoreInnerSpend { Owner(PublicKey), Admin(PublicKey),