Skip to content

Commit

Permalink
Add handle blocks function
Browse files Browse the repository at this point in the history
  • Loading branch information
Duddino committed Oct 18, 2024
1 parent 1f3aaa7 commit 53aedfa
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 5 deletions.
21 changes: 21 additions & 0 deletions js/pivx_shield.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,27 @@ export class PIVXShield {
return { pivxShield, success };
}

async handleBlocks(blocks: Block[]) {
const walletTransactions: string[] = [];
const { decrypted_notes, nullifiers, commitment_tree } = await this.callWorker<TransactionResult>("handle_blocks", this.commitmentTree, blocks, this.extfvk, this.isTestnet, this.unspentNotes);
this.commitmentTree = commitment_tree;
this.unspentNotes = decrypted_notes;
for (const note of decrypted_notes) {
const nullifier = await this.generateNullifierFromNote(note);
const simplifiedNote = {
value: note[0].value,
recipient: await this.getShieldAddressFromNote(note[0]),
};

this.mapNullifierNote.set(nullifier, simplifiedNote);
}
await this.removeSpentNotes(nullifiers);
this.lastProcessedBlock = blocks[blocks.length - 1]?.height ?? this.lastProcessedBlock;
// Delete the corresponding pending transaction
// this.pendingUnspentNotes.delete(tx.txid);
return walletTransactions;
}

/**
* Loop through the txs of a block and update useful shield data
* @param block - block outputted from any PIVX node
Expand Down
74 changes: 69 additions & 5 deletions src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,74 @@ pub struct JSTxSaplingData {
pub commitment_tree: String,
}

#[derive(Serialize, Deserialize)]
pub struct Block {
height: u32,
txs: Vec<String>,
}

fn read_commitment_tree(tree_hex: &str) -> Result<CommitmentTree<Node>, Box<dyn Error>> {
let buff = Cursor::new(hex::decode(tree_hex)?);
Ok(CommitmentTree::<Node>::read(buff)?)
}

#[wasm_bindgen]
pub fn handle_blocks(
tree_hex: &str,
blocks: JsValue,
enc_extfvk: &str,
is_testnet: bool,
comp_notes: JsValue,
) -> Result<JsValue, JsValue> {
let blocks: Vec<Block> = serde_wasm_bindgen::from_value(blocks)?;
let mut tree = read_commitment_tree(tree_hex).map_err(|_| "Couldn't read commitment tree")?;
let comp_note: Vec<(Note, String)> = serde_wasm_bindgen::from_value(comp_notes)?;
let extfvk =
decode_extended_full_viewing_key(enc_extfvk, is_testnet).map_err(|e| e.to_string())?;
let key = UnifiedFullViewingKey::new(Some(extfvk.to_diversifiable_full_viewing_key()), None)
.ok_or("Failed to create unified full viewing key")?;
let mut comp_note = comp_note
.into_iter()
.map(|(note, witness)| {
let wit = Cursor::new(hex::decode(witness).unwrap());
(note, IncrementalWitness::read(wit).unwrap())
})
.collect::<Vec<_>>();
let mut nullifiers = vec![];
for block in blocks {
for tx in block.txs {
nullifiers.extend(
handle_transaction_internal(&mut tree, &tx, &key, is_testnet, &mut comp_note)
.map_err(|_| "Couldn't handle transaction")?,
);
}
}

let mut ser_comp_note: Vec<(Note, String)> = Vec::with_capacity(comp_note.len());
for (note, witness) in comp_note.iter() {
let mut buff = Vec::new();
witness
.write(&mut buff)
.map_err(|_| "Cannot write witness to buffer")?;
ser_comp_note.push((note.clone(), hex::encode(&buff)));
}

let mut ser_nullifiers: Vec<String> = Vec::with_capacity(nullifiers.len());
for nullif in nullifiers.iter() {
ser_nullifiers.push(hex::encode(nullif.0));
}

let mut buff = Vec::new();
tree.write(&mut buff)
.map_err(|_| "Cannot write tree to buffer")?;

Ok(serde_wasm_bindgen::to_value(&JSTxSaplingData {
decrypted_notes: ser_comp_note,
nullifiers: ser_nullifiers,
commitment_tree: hex::encode(buff),
})?)
}

//Input a tx and return: the updated commitment merkletree, all the nullifier found in the tx and all the node decoded with the corresponding witness
#[wasm_bindgen]
pub fn handle_transaction(
Expand All @@ -143,11 +211,7 @@ pub fn handle_transaction(
is_testnet: bool,
comp_notes: JsValue,
) -> Result<JsValue, JsValue> {
let buff = Cursor::new(
hex::decode(tree_hex).map_err(|_| "Cannot decode commitment tree from hexadecimal")?,
);
let mut tree =
CommitmentTree::<Node>::read(buff).map_err(|_| "Cannot decode commitment tree!")?;
let mut tree = read_commitment_tree(tree_hex).map_err(|_| "Couldn't read commitment tree")?;
let extfvk =
decode_extended_full_viewing_key(enc_extfvk, is_testnet).map_err(|e| e.to_string())?;
let key = UnifiedFullViewingKey::new(Some(extfvk.to_diversifiable_full_viewing_key()), None)
Expand Down

0 comments on commit 53aedfa

Please sign in to comment.