Skip to content

Commit

Permalink
feat: send inner tx hash in TxCompleted event (#671)
Browse files Browse the repository at this point in the history
Closes #666.
  • Loading branch information
emccorson committed Mar 25, 2024
1 parent 6c3d7dc commit f8a7ec5
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 30 deletions.
39 changes: 27 additions & 12 deletions apps/extension/src/background/keyring/keyring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,7 @@ export class KeyRing {
return privateKey;
}

async submitBond(bondMsg: Uint8Array, txMsg: Uint8Array): Promise<void> {
async submitBond(bondMsg: Uint8Array, txMsg: Uint8Array): Promise<string> {
await this.vaultService.assertIsUnlocked();
try {
const { source } = deserialize(Buffer.from(bondMsg), BondMsgValue);
Expand All @@ -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}`);
}
Expand All @@ -701,7 +703,10 @@ export class KeyRing {
);
}

async submitUnbond(unbondMsg: Uint8Array, txMsg: Uint8Array): Promise<void> {
async submitUnbond(
unbondMsg: Uint8Array,
txMsg: Uint8Array
): Promise<string> {
await this.vaultService.assertIsUnlocked();
const sdk = await this.sdkService.getSdk();
try {
Expand All @@ -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}`);
}
Expand All @@ -721,7 +728,7 @@ export class KeyRing {
async submitWithdraw(
withdrawMsg: Uint8Array,
txMsg: Uint8Array
): Promise<void> {
): Promise<string> {
await this.vaultService.assertIsUnlocked();
const sdk = await this.sdkService.getSdk();
try {
Expand All @@ -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}`);
}
Expand All @@ -744,7 +753,7 @@ export class KeyRing {
async submitVoteProposal(
voteProposalMsg: Uint8Array,
txMsg: Uint8Array
): Promise<void> {
): Promise<string> {
await this.vaultService.assertIsUnlocked();
const sdk = await this.sdkService.getSdk();
try {
Expand All @@ -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}`);
}
Expand Down Expand Up @@ -802,7 +813,7 @@ export class KeyRing {
async submitIbcTransfer(
ibcTransferMsg: Uint8Array,
txMsg: Uint8Array
): Promise<void> {
): Promise<string> {
await this.vaultService.assertIsUnlocked();
const sdk = await this.sdkService.getSdk();
try {
Expand All @@ -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}`);
}
Expand All @@ -825,7 +838,7 @@ export class KeyRing {
async submitEthBridgeTransfer(
ethBridgeTransferMsg: Uint8Array,
txMsg: Uint8Array
): Promise<void> {
): Promise<string> {
await this.vaultService.assertIsUnlocked();
const sdk = await this.sdkService.getSdk();
try {
Expand All @@ -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}`);
}
Expand Down
52 changes: 40 additions & 12 deletions apps/extension/src/background/keyring/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,11 @@ export class KeyRingService {
): Promise<void> {
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) {
Expand All @@ -195,11 +198,16 @@ export class KeyRingService {
): Promise<void> {
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) {
Expand All @@ -216,11 +224,16 @@ export class KeyRingService {
): Promise<void> {
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) {
Expand All @@ -237,11 +250,16 @@ export class KeyRingService {
): Promise<void> {
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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
7 changes: 5 additions & 2 deletions apps/extension/src/background/ledger/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
5 changes: 3 additions & 2 deletions packages/shared/lib/src/sdk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<JsValue, JsError> {
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
Expand Down

0 comments on commit f8a7ec5

Please sign in to comment.