diff --git a/apps/extension/src/background/ledger/service.ts b/apps/extension/src/background/ledger/service.ts index bc6a58a9b..666fb7d63 100644 --- a/apps/extension/src/background/ledger/service.ts +++ b/apps/extension/src/background/ledger/service.ts @@ -20,7 +20,7 @@ import { TabStore, syncTabs, } from "background/keyring"; -import { generateId } from "utils"; +import { encodeSignature, generateId } from "utils"; import { ExtensionRequester } from "extension"; import { Ports } from "router"; import { UpdatedStakingEventMsg } from "content/events"; @@ -77,25 +77,18 @@ export class LedgerService { bytes: string, signatures: ResponseSign ): Promise { - const { - wrapperSignature: { raw: wrapperSig }, - rawSignature: { raw: rawSig }, - } = signatures; - - if (!wrapperSig) { - throw new Error("No wrapper signature was produced!"); - } - - if (!rawSig) { - throw new Error("No raw signature was produced!"); - } + const { wrapperSignature, rawSignature } = signatures; try { + // Serialize signatures + const rawSig = encodeSignature(rawSignature); + const wrapperSig = encodeSignature(wrapperSignature); + await this.sdk.submit_signed_reveal_pk( fromBase64(txMsg), fromBase64(bytes), - new Uint8Array(wrapperSig), - new Uint8Array(rawSig) + rawSig, + wrapperSig ); } catch (e) { console.warn(e); @@ -223,25 +216,16 @@ export class LedgerService { throw new Error(`Bond Transaction ${msgId} not found!`); } - const { - wrapperSignature: { raw: wrapperSig }, - rawSignature: { raw: rawSig }, - } = signatures; - - if (!wrapperSig) { - throw new Error("No wrapper signature was produced!"); - } - - if (!rawSig) { - throw new Error("No raw signature was produced!"); - } + const { wrapperSignature, rawSignature } = signatures; try { + const rawSig = encodeSignature(rawSignature); + const wrapperSig = encodeSignature(wrapperSignature); await this.sdk.submit_signed_bond( fromBase64(txMsg), fromBase64(bytes), - new Uint8Array(wrapperSig), - new Uint8Array(rawSig) + rawSig, + wrapperSig ); await this.broadcastUpdateStaking(); diff --git a/apps/extension/src/utils/index.ts b/apps/extension/src/utils/index.ts index e907fd4c4..1cee17504 100644 --- a/apps/extension/src/utils/index.ts +++ b/apps/extension/src/utils/index.ts @@ -1,9 +1,10 @@ import browser from "webextension-polyfill"; import { v5 as uuid } from "uuid"; -import { DerivedAccount } from "@namada/types"; +import { DerivedAccount, Message, SignatureMsgValue } from "@namada/types"; import { pick } from "@namada/utils"; import { AccountStore } from "background/keyring"; +import { ISignature } from "@namada/ledger-namada"; /** * Query the current extension tab and close it @@ -48,3 +49,31 @@ export const generateId = ( ): string => { return uuid(args.join(":"), namespace); }; + +/** + * Convert ISignature into serialized and encoded signature + */ +export const encodeSignature = (sig: ISignature): Uint8Array => { + const { salt, indicies, pubkey, signature } = sig; + + // Validate props + if (!salt || !indicies || !pubkey || !signature) { + throw new Error("Invalid signature!"); + } + + // TODO: Note that the following "any" type usage below is a result of the buffer responses + // from the Ledger do not match the ISignature type! This will be fixed in a future release. + + /* eslint-disable */ + const props = { + salt: new Uint8Array((salt as any).data), + indicies: new Uint8Array((indicies as any).data), + pubkey: new Uint8Array((pubkey as any).data), + signature: new Uint8Array((signature as any).data), + }; + /* eslint-enable */ + + const value = new SignatureMsgValue(props); + const msg = new Message(); + return msg.encode(value); +}; diff --git a/packages/shared/lib/src/sdk/mod.rs b/packages/shared/lib/src/sdk/mod.rs index 07b35c253..d459a5f82 100644 --- a/packages/shared/lib/src/sdk/mod.rs +++ b/packages/shared/lib/src/sdk/mod.rs @@ -184,10 +184,10 @@ impl Sdk { &mut self, tx_msg: &[u8], tx_bytes: &[u8], - wrapper_sig_bytes: &[u8], raw_sig_bytes: &[u8], + wrapper_sig_bytes: &[u8], ) -> Result<(), JsError> { - let reveal_pk_tx = self.sign_tx(tx_bytes, wrapper_sig_bytes, raw_sig_bytes)?; + let reveal_pk_tx = self.sign_tx(tx_bytes, raw_sig_bytes, wrapper_sig_bytes)?; let args = tx::reveal_pk_tx_args(tx_msg).map_err(JsError::from)?; namada::ledger::tx::process_tx(&self.client, &mut self.wallet, &args.tx, reveal_pk_tx) @@ -245,8 +245,8 @@ impl Sdk { fn sign_tx( &self, tx_bytes: &[u8], - wrapper_sig_bytes: &[u8], raw_sig_bytes: &[u8], + wrapper_sig_bytes: &[u8], ) -> Result { let mut tx: Tx = Tx::try_from_slice(tx_bytes).map_err(JsError::from)?; diff --git a/packages/shared/lib/src/sdk/signature.rs b/packages/shared/lib/src/sdk/signature.rs index 180b167de..a881dc85e 100644 --- a/packages/shared/lib/src/sdk/signature.rs +++ b/packages/shared/lib/src/sdk/signature.rs @@ -26,8 +26,8 @@ pub fn construct_signature(sig_msg: &[u8], tx: &Tx) -> Result