Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Bug related shielded action for transfer #734

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions apps/extension/src/background/approvals/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,17 @@ export const getHandler: (service: ApprovalsService) => Handler = (service) => {
const handleApproveTxMsg: (
service: ApprovalsService
) => InternalHandler<ApproveTxMsg> = (service) => {
return async (_, { txType, specificMsg, txMsg, accountType }) => {
return await service.approveTx(txType, specificMsg, txMsg, accountType);
return async (
_,
{ txType, specificMsg, txMsg, accountType, transparentAddress }
) => {
return await service.approveTx(
txType,
specificMsg,
txMsg,
accountType,
transparentAddress
);
};
};

Expand Down
20 changes: 16 additions & 4 deletions apps/extension/src/background/approvals/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,16 @@ export class ApprovalsService {
txType: SupportedTx,
txMsg: string,
specificMsg: string,
type: AccountType
type: AccountType,
transparentAddress?: string
): Promise<void> {
const msgId = uuid();
await this.txStore.set(msgId, { txType, txMsg, specificMsg });
await this.txStore.set(msgId, {
txType,
txMsg,
specificMsg,
transparentAddress,
});

// Decode tx details and launch approval screen
const txMsgBuffer = Buffer.from(fromBase64(txMsg));
Expand Down Expand Up @@ -317,7 +323,7 @@ export class ApprovalsService {
throw new Error("Pending tx not found!");
}

const { txType, specificMsg, txMsg } = tx;
const { txType, specificMsg, txMsg, transparentAddress } = tx;

const submitFn =
txType === TxType.Bond
Expand All @@ -336,7 +342,13 @@ export class ApprovalsService {
? this.keyRingService.submitVoteProposal
: assertNever(txType);

await submitFn.call(this.keyRingService, specificMsg, txMsg, msgId);
await submitFn.call(
this.keyRingService,
specificMsg,
txMsg,
msgId,
transparentAddress
);

return await this._clearPendingTx(msgId);
}
Expand Down
1 change: 1 addition & 0 deletions apps/extension/src/background/approvals/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ export type TxStore = {
txType: SupportedTx;
txMsg: string;
specificMsg: string;
transparentAddress?: string;
};
4 changes: 2 additions & 2 deletions apps/extension/src/background/chains/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class ChainsService {
console.warn(`Chain is unreachable: ${e}`);
}

await this.localStorage.setChain(chain);
await this.localStorage.setChain(chain as any);
return chain;
}

Expand Down Expand Up @@ -59,7 +59,7 @@ export class ChainsService {
chainId,
rpc: url,
};
await this.localStorage.setChain(await this._queryNativeToken(chain));
await this.localStorage.setChain(await this._queryNativeToken(chain) as any);
await this.broadcaster.updateNetwork();
}
}
114 changes: 97 additions & 17 deletions apps/extension/src/background/keyring/keyring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import {
UtilityStore,
} from "./types";

import { fromBase64 } from "@cosmjs/encoding";
import { SdkService } from "background/sdk";
import { VaultService } from "background/vault";
import { KeyStore, VaultStorage } from "storage";
Expand Down Expand Up @@ -768,14 +769,58 @@ export class KeyRing {
}
}

async submit(
transferMsg: string,
txMsg: string,
msgId: string,
signingKey: SigningKey
) {
const sdk = await this.sdkService.getSdk();
const data = {
transferMsg,
txMsg,
msgId,
signingKey,
};
console.log("data", data);
try {
const {
signingKey: { privateKey, xsk },
} = data;
let txMsg = fromBase64(data.txMsg);
await sdk.load_masp_params();
let finalPrivate;

if (privateKey) {
await sdk.reveal_pk(privateKey, txMsg);
finalPrivate = privateKey;
}

const builtTx = await sdk.build_transfer(
fromBase64(data.transferMsg),
txMsg,
xsk
);
const txBytes = await sdk.sign_tx(builtTx, txMsg, finalPrivate);
return await sdk.process_tx(txBytes, txMsg);
} catch (error) {
console.error("error build", error);
return "";
}
}

async submitTransfer(
transferMsg: Uint8Array,
submit: (signingKey: SigningKey) => Promise<void>
transferMsgBase64: Uint8Array,
transferMsg: string,
txMsg: string,
msgId: string,
transparentAddress?: string
): Promise<void> {
await this.vaultService.assertIsUnlocked();

// We need to get the source address to find either the private key or spending key
const { source } = deserialize(Buffer.from(transferMsg), TransferMsgValue);
const { source } = deserialize(
Buffer.from(transferMsgBase64),
TransferMsgValue
);

const account = await this.vaultStorage.findOneOrFail(
KeyStore,
Expand All @@ -786,25 +831,31 @@ export class KeyRing {
await this.vaultService.reveal<SensitiveAccountStoreData>(
account.sensitive
);

console.log("sensitiveProps", sensitiveProps);
if (!sensitiveProps) {
throw new Error("Error decrypting AccountStore data");
}

if (account.public.type === AccountType.ShieldedKeys) {
const isShielded = account.public.type === AccountType.ShieldedKeys;
if (isShielded && transparentAddress) {
const xsk = JSON.parse(sensitiveProps.text).spendingKey;
const privateKey = await this.getSigningKey(transparentAddress);

await submit({
const key = {
xsk,
});
privateKey,
};
await this.submit(transferMsg, txMsg, msgId, key);
} else {
await submit({ privateKey: await this.getSigningKey(source) });
await this.submit(transferMsg, txMsg, msgId, {
privateKey: await this.getSigningKey(source),
});
}
}

async submitIbcTransfer(
ibcTransferMsg: Uint8Array,
txMsg: Uint8Array
txMsg: Uint8Array,
transparentAddress?: string
): Promise<void> {
await this.vaultService.assertIsUnlocked();
const sdk = await this.sdkService.getSdk();
Expand All @@ -813,14 +864,43 @@ export class KeyRing {
Buffer.from(ibcTransferMsg),
IbcTransferMsgValue
);
const signingKey = await this.getSigningKey(source);

await sdk.reveal_pk(signingKey, txMsg);
console.log("source", source);
console.log("transparentAddress", transparentAddress);
const account = await this.vaultStorage.findOneOrFail(
KeyStore,
"address",
source
);
console.log("account", account);
let privateKey;
let xsk;
await sdk.load_masp_params();
const isShieldedTransaction =
account.public.type === AccountType.ShieldedKeys;
if (isShieldedTransaction && transparentAddress) {
const sensitiveProps =
await this.vaultService.reveal<SensitiveAccountStoreData>(
account.sensitive
);
console.log("sensitiveProps", sensitiveProps);
if (!sensitiveProps) {
throw new Error("Error decrypting AccountStore data");
}

const builtTx = await sdk.build_ibc_transfer(ibcTransferMsg, txMsg);
const txBytes = await sdk.sign_tx(builtTx, txMsg, signingKey);
xsk = JSON.parse(sensitiveProps.text).spendingKey;
privateKey = await this.getSigningKey(transparentAddress);
} else {
privateKey = await this.getSigningKey(source);
await sdk.reveal_pk(privateKey, txMsg);
}
console.log("Before byte");
const builtTx = await sdk.build_ibc_transfer(ibcTransferMsg, txMsg, xsk);
console.log("builtTx", builtTx);
const txBytes = await sdk.sign_tx(builtTx, txMsg, privateKey);
console.log("txBytes", txBytes);
await sdk.process_tx(txBytes, txMsg);
} catch (e) {
console.log("submitIbcTransfer error", e);
throw new Error(`Could not submit ibc transfer tx: ${e}`);
}
}
Expand Down
Loading