From f8a7ec5129042bacbb28d9665d43c07083535c50 Mon Sep 17 00:00:00 2001 From: Eric Corson Date: Tue, 19 Mar 2024 20:23:35 +0900 Subject: [PATCH] feat: send inner tx hash in TxCompleted event (#671) Closes #666. --- .../src/background/keyring/keyring.ts | 39 +++++++++----- .../src/background/keyring/service.ts | 52 ++++++++++++++----- .../src/background/ledger/service.ts | 7 ++- .../web-workers/submit-transfer-web-worker.ts | 7 ++- packages/shared/lib/src/sdk/mod.rs | 5 +- 5 files changed, 80 insertions(+), 30 deletions(-) diff --git a/apps/extension/src/background/keyring/keyring.ts b/apps/extension/src/background/keyring/keyring.ts index 0b3a83365..b2d64ec7d 100644 --- a/apps/extension/src/background/keyring/keyring.ts +++ b/apps/extension/src/background/keyring/keyring.ts @@ -677,7 +677,7 @@ export class KeyRing { return privateKey; } - async submitBond(bondMsg: Uint8Array, txMsg: Uint8Array): Promise { + async submitBond(bondMsg: Uint8Array, txMsg: Uint8Array): Promise { await this.vaultService.assertIsUnlocked(); try { const { source } = deserialize(Buffer.from(bondMsg), BondMsgValue); @@ -687,7 +687,9 @@ export class KeyRing { const builtTx = await sdk.build_bond(bondMsg, txMsg); const txBytes = await sdk.sign_tx(builtTx, txMsg, signingKey); - await sdk.process_tx(txBytes, txMsg); + const innerTxHash: string = await sdk.process_tx(txBytes, txMsg); + + return innerTxHash; } catch (e) { throw new Error(`Could not submit bond tx: ${e}`); } @@ -701,7 +703,10 @@ export class KeyRing { ); } - async submitUnbond(unbondMsg: Uint8Array, txMsg: Uint8Array): Promise { + async submitUnbond( + unbondMsg: Uint8Array, + txMsg: Uint8Array + ): Promise { await this.vaultService.assertIsUnlocked(); const sdk = await this.sdkService.getSdk(); try { @@ -712,7 +717,9 @@ export class KeyRing { const builtTx = await sdk.build_unbond(unbondMsg, txMsg); const txBytes = await sdk.sign_tx(builtTx, txMsg, signingKey); - await sdk.process_tx(txBytes, txMsg); + const innerTxHash: string = await sdk.process_tx(txBytes, txMsg); + + return innerTxHash; } catch (e) { throw new Error(`Could not submit unbond tx: ${e}`); } @@ -721,7 +728,7 @@ export class KeyRing { async submitWithdraw( withdrawMsg: Uint8Array, txMsg: Uint8Array - ): Promise { + ): Promise { await this.vaultService.assertIsUnlocked(); const sdk = await this.sdkService.getSdk(); try { @@ -735,7 +742,9 @@ export class KeyRing { const builtTx = await sdk.build_withdraw(withdrawMsg, txMsg); const txBytes = await sdk.sign_tx(builtTx, txMsg, signingKey); - await sdk.process_tx(txBytes, txMsg); + const innerTxHash: string = await sdk.process_tx(txBytes, txMsg); + + return innerTxHash; } catch (e) { throw new Error(`Could not submit withdraw tx: ${e}`); } @@ -744,7 +753,7 @@ export class KeyRing { async submitVoteProposal( voteProposalMsg: Uint8Array, txMsg: Uint8Array - ): Promise { + ): Promise { await this.vaultService.assertIsUnlocked(); const sdk = await this.sdkService.getSdk(); try { @@ -759,7 +768,9 @@ export class KeyRing { const builtTx = await sdk.build_vote_proposal(voteProposalMsg, txMsg); const txBytes = await sdk.sign_tx(builtTx, txMsg, signingKey); - await sdk.process_tx(txBytes, txMsg); + const innerTxHash: string = await sdk.process_tx(txBytes, txMsg); + + return innerTxHash; } catch (e) { throw new Error(`Could not submit vote proposal tx: ${e}`); } @@ -802,7 +813,7 @@ export class KeyRing { async submitIbcTransfer( ibcTransferMsg: Uint8Array, txMsg: Uint8Array - ): Promise { + ): Promise { await this.vaultService.assertIsUnlocked(); const sdk = await this.sdkService.getSdk(); try { @@ -816,7 +827,9 @@ export class KeyRing { const builtTx = await sdk.build_ibc_transfer(ibcTransferMsg, txMsg); const txBytes = await sdk.sign_tx(builtTx, txMsg, signingKey); - await sdk.process_tx(txBytes, txMsg); + const innerTxHash: string = await sdk.process_tx(txBytes, txMsg); + + return innerTxHash; } catch (e) { throw new Error(`Could not submit ibc transfer tx: ${e}`); } @@ -825,7 +838,7 @@ export class KeyRing { async submitEthBridgeTransfer( ethBridgeTransferMsg: Uint8Array, txMsg: Uint8Array - ): Promise { + ): Promise { await this.vaultService.assertIsUnlocked(); const sdk = await this.sdkService.getSdk(); try { @@ -842,7 +855,9 @@ export class KeyRing { txMsg ); const txBytes = await sdk.sign_tx(builtTx, txMsg, signingKey); - await sdk.process_tx(txBytes, txMsg); + const innerTxHash: string = await sdk.process_tx(txBytes, txMsg); + + return innerTxHash; } catch (e) { throw new Error(`Could not submit submit_eth_bridge_transfer tx: ${e}`); } diff --git a/apps/extension/src/background/keyring/service.ts b/apps/extension/src/background/keyring/service.ts index 7fd2341ac..9c37c1ce6 100644 --- a/apps/extension/src/background/keyring/service.ts +++ b/apps/extension/src/background/keyring/service.ts @@ -177,8 +177,11 @@ export class KeyRingService { ): Promise { await this.broadcaster.startTx(msgId, TxType.Bond); try { - await this._keyRing.submitBond(fromBase64(bondMsg), fromBase64(txMsg)); - await this.broadcaster.completeTx(msgId, TxType.Bond, true); + const innerTxHash = await this._keyRing.submitBond( + fromBase64(bondMsg), + fromBase64(txMsg) + ); + await this.broadcaster.completeTx(msgId, TxType.Bond, true, innerTxHash); await this.broadcaster.updateStaking(); await this.broadcaster.updateBalance(); } catch (e) { @@ -195,11 +198,16 @@ export class KeyRingService { ): Promise { await this.broadcaster.startTx(msgId, TxType.Unbond); try { - await this._keyRing.submitUnbond( + const innerTxHash = await this._keyRing.submitUnbond( fromBase64(unbondMsg), fromBase64(txMsg) ); - await this.broadcaster.completeTx(msgId, TxType.Unbond, true); + await this.broadcaster.completeTx( + msgId, + TxType.Unbond, + true, + innerTxHash + ); await this.broadcaster.updateStaking(); await this.broadcaster.updateBalance(); } catch (e) { @@ -216,11 +224,16 @@ export class KeyRingService { ): Promise { await this.broadcaster.startTx(msgId, TxType.Withdraw); try { - await this._keyRing.submitWithdraw( + const innerTxHash = await this._keyRing.submitWithdraw( fromBase64(withdrawMsg), fromBase64(txMsg) ); - await this.broadcaster.completeTx(msgId, TxType.Withdraw, true); + await this.broadcaster.completeTx( + msgId, + TxType.Withdraw, + true, + innerTxHash + ); await this.broadcaster.updateStaking(); await this.broadcaster.updateBalance(); } catch (e) { @@ -237,11 +250,16 @@ export class KeyRingService { ): Promise { await this.broadcaster.startTx(msgId, TxType.VoteProposal); try { - await this._keyRing.submitVoteProposal( + const innerTxHash = await this._keyRing.submitVoteProposal( fromBase64(voteProposalMsg), fromBase64(txMsg) ); - await this.broadcaster.completeTx(msgId, TxType.VoteProposal, true); + await this.broadcaster.completeTx( + msgId, + TxType.VoteProposal, + true, + innerTxHash + ); await this.broadcaster.updateProposals(); } catch (e) { console.warn(e); @@ -361,11 +379,16 @@ export class KeyRingService { await this.broadcaster.startTx(msgId, TxType.IBCTransfer); try { - await this._keyRing.submitIbcTransfer( + const innerTxHash = await this._keyRing.submitIbcTransfer( fromBase64(ibcTransferMsg), fromBase64(txMsg) ); - await this.broadcaster.completeTx(msgId, TxType.IBCTransfer, true); + await this.broadcaster.completeTx( + msgId, + TxType.IBCTransfer, + true, + innerTxHash + ); await this.broadcaster.updateBalance(); } catch (e) { console.warn(e); @@ -387,11 +410,16 @@ export class KeyRingService { await this.broadcaster.startTx(msgId, TxType.EthBridgeTransfer); try { - await this._keyRing.submitEthBridgeTransfer( + const innerTxHash = await this._keyRing.submitEthBridgeTransfer( fromBase64(ethBridgeTransferMsg), fromBase64(txMsg) ); - await this.broadcaster.completeTx(msgId, TxType.EthBridgeTransfer, true); + await this.broadcaster.completeTx( + msgId, + TxType.EthBridgeTransfer, + true, + innerTxHash + ); await this.broadcaster.updateBalance(); } catch (e) { console.warn(e); diff --git a/apps/extension/src/background/ledger/service.ts b/apps/extension/src/background/ledger/service.ts index a753e2ed2..c07e4bf3c 100644 --- a/apps/extension/src/background/ledger/service.ts +++ b/apps/extension/src/background/ledger/service.ts @@ -121,13 +121,16 @@ export class LedgerService { const sdk = await this.sdkService.getSdk(); try { const signedTxBytes = await sdk.append_signature(fromBase64(bytes), sig); - await sdk.process_tx(signedTxBytes, fromBase64(txMsg)); + const innerTxHash: string = await sdk.process_tx( + signedTxBytes, + fromBase64(txMsg) + ); // Clear pending tx if successful await this.txStore.set(msgId, null); // Broadcast update events - await this.broadcaster.completeTx(msgId, txType, true); + await this.broadcaster.completeTx(msgId, txType, true, innerTxHash); await this.broadcaster.updateBalance(); if ([TxType.Bond, TxType.Unbond, TxType.Withdraw].includes(txType)) { diff --git a/apps/extension/src/background/web-workers/submit-transfer-web-worker.ts b/apps/extension/src/background/web-workers/submit-transfer-web-worker.ts index 9545166e4..7b9a357f8 100644 --- a/apps/extension/src/background/web-workers/submit-transfer-web-worker.ts +++ b/apps/extension/src/background/web-workers/submit-transfer-web-worker.ts @@ -47,9 +47,12 @@ import { xsk ); const txBytes = await sdk.sign_tx(builtTx, txMsg, privateKey); - await sdk.process_tx(txBytes, txMsg); + const innerTxHash: string = await sdk.process_tx(txBytes, txMsg); - postMessage({ msgName: TRANSFER_SUCCESSFUL_MSG }); + postMessage({ + msgName: TRANSFER_SUCCESSFUL_MSG, + payload: innerTxHash, + }); } catch (error) { console.error(error); postMessage({ diff --git a/packages/shared/lib/src/sdk/mod.rs b/packages/shared/lib/src/sdk/mod.rs index 1cd4dfe42..60b20b2d3 100644 --- a/packages/shared/lib/src/sdk/mod.rs +++ b/packages/shared/lib/src/sdk/mod.rs @@ -174,13 +174,14 @@ impl Sdk { to_js_result(borsh::to_vec(&tx)?) } - pub async fn process_tx(&mut self, tx_bytes: &[u8], tx_msg: &[u8]) -> Result<(), JsError> { + pub async fn process_tx(&mut self, tx_bytes: &[u8], tx_msg: &[u8]) -> Result { let args = tx::tx_args_from_slice(tx_msg)?; let tx = Tx::try_from_slice(tx_bytes)?; + let inner_tx_hash = tx.raw_header_hash().to_string(); process_tx(&self.namada, &args, tx).await?; - Ok(()) + to_js_result(inner_tx_hash) } /// Build transaction for specified type, return bytes to client